diff --git a/.travis.yml b/.travis.yml index f57d1ec..4dcb9cc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,9 @@ dist: xenial language: generic +services: + - mongodb + jdk: - openjdk11 diff --git a/pom.xml b/pom.xml index c876978..0258de7 100644 --- a/pom.xml +++ b/pom.xml @@ -20,6 +20,7 @@ 11 + 0.13 0.10.5 0.27.0 2.0.5 @@ -47,10 +48,6 @@ org.springframework.boot spring-boot-starter-actuator - - - - org.springframework.boot spring-boot-starter-data-mongodb @@ -59,6 +56,11 @@ + + com.github.mongobee + mongobee + ${mongobee.version} + io.jsonwebtoken jjwt-api diff --git a/src/main/java/nl/dtls/adminpanel/AdminPanelApplication.java b/src/main/java/nl/dtls/adminpanel/AdminPanelApplication.java index bf8f974..28a59a3 100644 --- a/src/main/java/nl/dtls/adminpanel/AdminPanelApplication.java +++ b/src/main/java/nl/dtls/adminpanel/AdminPanelApplication.java @@ -1,23 +1,10 @@ package nl.dtls.adminpanel; -import static org.springframework.core.env.Profiles.of; - -import nl.dtls.adminpanel.database.fixtures.DummyDataLoader; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.ApplicationArguments; -import org.springframework.boot.ApplicationRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.core.env.Environment; @SpringBootApplication -public class AdminPanelApplication implements ApplicationRunner { - - @Autowired - private DummyDataLoader dummyDataLoader; - - @Autowired - private Environment environment; +public class AdminPanelApplication { public static void main(String[] args) { String property = System.getProperties().getProperty("spring.profiles.active"); @@ -27,10 +14,4 @@ public static void main(String[] args) { SpringApplication.run(AdminPanelApplication.class, args); } - @Override - public void run(ApplicationArguments args) { - if (environment.acceptsProfiles(of(Profiles.DEVELOPMENT))) { - dummyDataLoader.load(); - } - } } diff --git a/src/main/java/nl/dtls/adminpanel/Profiles.java b/src/main/java/nl/dtls/adminpanel/Profiles.java index c9a8cc9..2ca2f85 100644 --- a/src/main/java/nl/dtls/adminpanel/Profiles.java +++ b/src/main/java/nl/dtls/adminpanel/Profiles.java @@ -8,4 +8,8 @@ public class Profiles { public static final String TESTING = "testing"; + public static final String NON_TESTING = "!testing"; + + public static final String NON_PRODUCTION = "!production"; + } diff --git a/src/main/java/nl/dtls/adminpanel/config/MongoConfig.java b/src/main/java/nl/dtls/adminpanel/config/MongoConfig.java index 2c8fb14..150df4c 100644 --- a/src/main/java/nl/dtls/adminpanel/config/MongoConfig.java +++ b/src/main/java/nl/dtls/adminpanel/config/MongoConfig.java @@ -1,12 +1,15 @@ package nl.dtls.adminpanel.config; +import com.github.mongobee.Mongobee; import com.mongodb.client.MongoDatabase; import com.mongodb.client.gridfs.GridFSBucket; import com.mongodb.client.gridfs.GridFSBuckets; import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.convert.MappingMongoConverter; import org.springframework.data.mongodb.gridfs.GridFsTemplate; @@ -14,6 +17,12 @@ @Configuration public class MongoConfig { + @Value("${spring.data.mongodb.uri}") + private String mongoUri; + + @Autowired + private Environment environment; + @Autowired private MappingMongoConverter mongoConverter; @@ -34,4 +43,13 @@ public GridFSBucket getGridFSBuckets(MongoDbFactory mongoDbFactory) { return GridFSBuckets.create(db); } + @Bean + public Mongobee mongobee() throws Exception { + Mongobee runner = new Mongobee(mongoUri); + runner.setChangeLogsScanPackage("nl.dtls.adminpanel"); + runner.setSpringEnvironment(environment); + runner.execute(); + return runner; + } + } \ No newline at end of file diff --git a/src/main/java/nl/dtls/adminpanel/database/fixtures/DummyDataLoader.java b/src/main/java/nl/dtls/adminpanel/database/fixtures/DummyDataLoader.java deleted file mode 100644 index b17431b..0000000 --- a/src/main/java/nl/dtls/adminpanel/database/fixtures/DummyDataLoader.java +++ /dev/null @@ -1,64 +0,0 @@ -package nl.dtls.adminpanel.database.fixtures; - -import nl.dtls.adminpanel.database.fixtures.data.ApplicationFixtures; -import nl.dtls.adminpanel.database.fixtures.data.InstanceFixtures; -import nl.dtls.adminpanel.database.fixtures.data.ServerFixtures; -import nl.dtls.adminpanel.database.fixtures.data.UserFixtures; -import nl.dtls.adminpanel.database.repository.ApplicationRepository; -import nl.dtls.adminpanel.database.repository.InstanceRepository; -import nl.dtls.adminpanel.database.repository.ServerRepository; -import nl.dtls.adminpanel.database.repository.UserRepository; -import nl.dtls.adminpanel.entity.Application; -import nl.dtls.adminpanel.entity.Server; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.crypto.password.PasswordEncoder; -import org.springframework.stereotype.Service; - -@Service -public class DummyDataLoader { - - @Autowired - private UserRepository userRepository; - - @Autowired - private UserFixtures userFixtures; - - @Autowired - private ServerRepository serverRepository; - - @Autowired - private ServerFixtures serverFixtures; - - @Autowired - private InstanceRepository instanceRepository; - - @Autowired - private InstanceFixtures instanceFixtures; - - @Autowired - private ApplicationRepository applicationRepository; - - @Autowired - private ApplicationFixtures applicationFixtures; - - @Autowired - private PasswordEncoder passwordEncoder; - - public void load() { - userRepository.deleteAll(); - userRepository.save(userFixtures.albert()); - userRepository.save(userFixtures.nikola()); - - applicationRepository.deleteAll(); - Application fdpApplication = applicationFixtures.fdpApplication(); - applicationRepository.save(fdpApplication); - - serverRepository.deleteAll(); - Server fdpServer = serverFixtures.fdpServer(); - serverRepository.save(fdpServer); - - instanceRepository.deleteAll(); - instanceRepository.save(instanceFixtures.stagingFdpInstance(fdpApplication, fdpServer)); - } - -} diff --git a/src/main/java/nl/dtls/adminpanel/database/migration/development/MigrationRunner.java b/src/main/java/nl/dtls/adminpanel/database/migration/development/MigrationRunner.java new file mode 100644 index 0000000..ed1950d --- /dev/null +++ b/src/main/java/nl/dtls/adminpanel/database/migration/development/MigrationRunner.java @@ -0,0 +1,37 @@ +package nl.dtls.adminpanel.database.migration.development; + +import javax.annotation.PostConstruct; +import nl.dtls.adminpanel.Profiles; +import nl.dtls.adminpanel.database.migration.development.application.ApplicationMigration; +import nl.dtls.adminpanel.database.migration.development.instance.InstanceMigration; +import nl.dtls.adminpanel.database.migration.development.server.ServerMigration; +import nl.dtls.adminpanel.database.migration.development.user.UserMigration; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Service; + +@Service +@Profile(Profiles.NON_PRODUCTION) +public class MigrationRunner { + + @Autowired + private UserMigration userMigration; + + @Autowired + private ApplicationMigration applicationMigration; + + @Autowired + private ServerMigration serverMigration; + + @Autowired + private InstanceMigration instanceMigration; + + @PostConstruct + public void run() { + userMigration.runMigration(); + applicationMigration.runMigration(); + serverMigration.runMigration(); + instanceMigration.runMigration(); + } + +} diff --git a/src/main/java/nl/dtls/adminpanel/database/migration/development/application/ApplicationMigration.java b/src/main/java/nl/dtls/adminpanel/database/migration/development/application/ApplicationMigration.java new file mode 100644 index 0000000..2b87f50 --- /dev/null +++ b/src/main/java/nl/dtls/adminpanel/database/migration/development/application/ApplicationMigration.java @@ -0,0 +1,25 @@ +package nl.dtls.adminpanel.database.migration.development.application; + +import nl.dtls.adminpanel.database.migration.development.application.data.ApplicationFixtures; +import nl.dtls.adminpanel.database.migration.development.common.Migration; +import nl.dtls.adminpanel.database.repository.ApplicationRepository; +import nl.dtls.adminpanel.entity.Application; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class ApplicationMigration implements Migration { + + @Autowired + private ApplicationFixtures applicationFixtures; + + @Autowired + private ApplicationRepository applicationRepository; + + public void runMigration() { + applicationRepository.deleteAll(); + Application fdpApplication = applicationFixtures.fdpApplication(); + applicationRepository.save(fdpApplication); + } + +} diff --git a/src/main/java/nl/dtls/adminpanel/database/fixtures/data/ApplicationFixtures.java b/src/main/java/nl/dtls/adminpanel/database/migration/development/application/data/ApplicationFixtures.java similarity index 98% rename from src/main/java/nl/dtls/adminpanel/database/fixtures/data/ApplicationFixtures.java rename to src/main/java/nl/dtls/adminpanel/database/migration/development/application/data/ApplicationFixtures.java index 330422b..8be604e 100644 --- a/src/main/java/nl/dtls/adminpanel/database/fixtures/data/ApplicationFixtures.java +++ b/src/main/java/nl/dtls/adminpanel/database/migration/development/application/data/ApplicationFixtures.java @@ -1,4 +1,4 @@ -package nl.dtls.adminpanel.database.fixtures.data; +package nl.dtls.adminpanel.database.migration.development.application.data; import java.util.ArrayList; import nl.dtls.adminpanel.entity.Application; diff --git a/src/main/java/nl/dtls/adminpanel/database/migration/development/common/Migration.java b/src/main/java/nl/dtls/adminpanel/database/migration/development/common/Migration.java new file mode 100644 index 0000000..17d9d8c --- /dev/null +++ b/src/main/java/nl/dtls/adminpanel/database/migration/development/common/Migration.java @@ -0,0 +1,7 @@ +package nl.dtls.adminpanel.database.migration.development.common; + +public interface Migration { + + void runMigration(); + +} diff --git a/src/main/java/nl/dtls/adminpanel/database/migration/development/instance/InstanceMigration.java b/src/main/java/nl/dtls/adminpanel/database/migration/development/instance/InstanceMigration.java new file mode 100644 index 0000000..ff07fff --- /dev/null +++ b/src/main/java/nl/dtls/adminpanel/database/migration/development/instance/InstanceMigration.java @@ -0,0 +1,23 @@ +package nl.dtls.adminpanel.database.migration.development.instance; + +import nl.dtls.adminpanel.database.migration.development.common.Migration; +import nl.dtls.adminpanel.database.migration.development.instance.data.InstanceFixtures; +import nl.dtls.adminpanel.database.repository.InstanceRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class InstanceMigration implements Migration { + + @Autowired + private InstanceFixtures instanceFixtures; + + @Autowired + private InstanceRepository instanceRepository; + + public void runMigration() { + instanceRepository.deleteAll(); + instanceRepository.save(instanceFixtures.fdpStaging()); + } + +} diff --git a/src/main/java/nl/dtls/adminpanel/database/fixtures/data/InstanceFixtures.java b/src/main/java/nl/dtls/adminpanel/database/migration/development/instance/data/InstanceFixtures.java similarity index 74% rename from src/main/java/nl/dtls/adminpanel/database/fixtures/data/InstanceFixtures.java rename to src/main/java/nl/dtls/adminpanel/database/migration/development/instance/data/InstanceFixtures.java index 67fe572..bedc57b 100644 --- a/src/main/java/nl/dtls/adminpanel/database/fixtures/data/InstanceFixtures.java +++ b/src/main/java/nl/dtls/adminpanel/database/migration/development/instance/data/InstanceFixtures.java @@ -1,9 +1,14 @@ -package nl.dtls.adminpanel.database.fixtures.data; +package nl.dtls.adminpanel.database.migration.development.instance.data; import java.util.HashMap; +import nl.dtls.adminpanel.database.migration.development.application.data.ApplicationFixtures; +import nl.dtls.adminpanel.database.migration.development.server.data.ServerFixtures; +import nl.dtls.adminpanel.database.repository.ApplicationRepository; +import nl.dtls.adminpanel.database.repository.ServerRepository; import nl.dtls.adminpanel.entity.Application; import nl.dtls.adminpanel.entity.Instance; import nl.dtls.adminpanel.entity.Server; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @@ -16,8 +21,22 @@ public class InstanceFixtures { @Value("${dummy.instance.jwtSecret:}") private String instanceJwtSecret; - public Instance stagingFdpInstance(Application fdpApplication, - Server fdpServer) { + @Autowired + private ApplicationFixtures applicationFixtures; + + @Autowired + private ServerFixtures serverFixtures; + + @Autowired + private ApplicationRepository applicationRepository; + + @Autowired + private ServerRepository serverRepository; + + public Instance fdpStaging() { + Application fdpApplication = applicationRepository + .findByUuid(applicationFixtures.fdpApplication().getUuid()).get(); + Server fdpServer = serverRepository.findByUuid(serverFixtures.fdpServer().getUuid()).get(); return new Instance( "6f29daa8-1c43-49c3-9d1b-dc422e333d1e", "FDP Staging Instance", diff --git a/src/main/java/nl/dtls/adminpanel/database/migration/development/server/ServerMigration.java b/src/main/java/nl/dtls/adminpanel/database/migration/development/server/ServerMigration.java new file mode 100644 index 0000000..e090114 --- /dev/null +++ b/src/main/java/nl/dtls/adminpanel/database/migration/development/server/ServerMigration.java @@ -0,0 +1,25 @@ +package nl.dtls.adminpanel.database.migration.development.server; + +import nl.dtls.adminpanel.database.migration.development.common.Migration; +import nl.dtls.adminpanel.database.migration.development.server.data.ServerFixtures; +import nl.dtls.adminpanel.database.repository.ServerRepository; +import nl.dtls.adminpanel.entity.Server; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class ServerMigration implements Migration { + + @Autowired + private ServerFixtures serverFixtures; + + @Autowired + private ServerRepository serverRepository; + + public void runMigration() { + serverRepository.deleteAll(); + Server fdpServer = serverFixtures.fdpServer(); + serverRepository.save(fdpServer); + } + +} diff --git a/src/main/java/nl/dtls/adminpanel/database/fixtures/data/ServerFixtures.java b/src/main/java/nl/dtls/adminpanel/database/migration/development/server/data/ServerFixtures.java similarity index 91% rename from src/main/java/nl/dtls/adminpanel/database/fixtures/data/ServerFixtures.java rename to src/main/java/nl/dtls/adminpanel/database/migration/development/server/data/ServerFixtures.java index 5666b2a..3281dd7 100644 --- a/src/main/java/nl/dtls/adminpanel/database/fixtures/data/ServerFixtures.java +++ b/src/main/java/nl/dtls/adminpanel/database/migration/development/server/data/ServerFixtures.java @@ -1,4 +1,4 @@ -package nl.dtls.adminpanel.database.fixtures.data; +package nl.dtls.adminpanel.database.migration.development.server.data; import nl.dtls.adminpanel.entity.Server; import org.springframework.beans.factory.annotation.Value; diff --git a/src/main/java/nl/dtls/adminpanel/database/migration/development/user/UserMigration.java b/src/main/java/nl/dtls/adminpanel/database/migration/development/user/UserMigration.java new file mode 100644 index 0000000..937b0a0 --- /dev/null +++ b/src/main/java/nl/dtls/adminpanel/database/migration/development/user/UserMigration.java @@ -0,0 +1,24 @@ +package nl.dtls.adminpanel.database.migration.development.user; + +import nl.dtls.adminpanel.database.migration.development.common.Migration; +import nl.dtls.adminpanel.database.migration.development.user.data.UserFixtures; +import nl.dtls.adminpanel.database.repository.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class UserMigration implements Migration { + + @Autowired + private UserRepository userRepository; + + @Autowired + private UserFixtures userFixtures; + + public void runMigration() { + userRepository.deleteAll(); + userRepository.save(userFixtures.albert()); + userRepository.save(userFixtures.nikola()); + } + +} diff --git a/src/main/java/nl/dtls/adminpanel/database/fixtures/data/UserFixtures.java b/src/main/java/nl/dtls/adminpanel/database/migration/development/user/data/UserFixtures.java similarity index 93% rename from src/main/java/nl/dtls/adminpanel/database/fixtures/data/UserFixtures.java rename to src/main/java/nl/dtls/adminpanel/database/migration/development/user/data/UserFixtures.java index 816d1af..df4816f 100644 --- a/src/main/java/nl/dtls/adminpanel/database/fixtures/data/UserFixtures.java +++ b/src/main/java/nl/dtls/adminpanel/database/migration/development/user/data/UserFixtures.java @@ -1,4 +1,4 @@ -package nl.dtls.adminpanel.database.fixtures.data; +package nl.dtls.adminpanel.database.migration.development.user.data; import nl.dtls.adminpanel.entity.User; import org.springframework.beans.factory.annotation.Autowired; diff --git a/src/main/java/nl/dtls/adminpanel/database/migration/production/Migration_0001_Init.java b/src/main/java/nl/dtls/adminpanel/database/migration/production/Migration_0001_Init.java new file mode 100644 index 0000000..fb7c991 --- /dev/null +++ b/src/main/java/nl/dtls/adminpanel/database/migration/production/Migration_0001_Init.java @@ -0,0 +1,51 @@ +/** + * The MIT License Copyright © 2017 DTL + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package nl.dtls.adminpanel.database.migration.production; + +import com.github.mongobee.changeset.ChangeLog; +import com.github.mongobee.changeset.ChangeSet; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; +import nl.dtls.adminpanel.Profiles; +import org.bson.Document; +import org.springframework.context.annotation.Profile; + +@ChangeLog(order = "0001") +@Profile(Profiles.PRODUCTION) +public class Migration_0001_Init { + + @ChangeSet(order = "0001", id = "0001_init", author = "migrationBot") + public void run(MongoDatabase db) { + MongoCollection userCol = db.getCollection("user"); + userCol.insertOne(userAlbert()); + } + + private Document userAlbert() { + Document user = new Document(); + user.append("uuid", "7e64818d-6276-46fb-8bb1-732e6e09f7e9"); + user.append("name", "Albert Einstein"); + user.append("email", "albert.einstein@example.com"); + user.append("passwordHash", + "$2a$10$t2foZfp7cZFQo2u/33ZqTu2WNitBqYd2EY2tQO0/rBUdf8QfsAxyW"); // password + user.append("role", "ADMIN"); + user.append("_class", "nl.dtls.fairdatapoint.entity.user.User"); + return user; + } + +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index f8cd431..b91a08f 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,8 +1,7 @@ spring: data: mongodb: - uri: mongodb://mongo:27017 - database: admin-panel + uri: mongodb://mongo:27017/admin-panel security: jwt: diff --git a/src/test/java/nl/dtls/adminpanel/AdminPanelApplicationTests.java b/src/test/java/nl/dtls/adminpanel/AdminPanelApplicationTests.java index 37bb410..dc04505 100644 --- a/src/test/java/nl/dtls/adminpanel/AdminPanelApplicationTests.java +++ b/src/test/java/nl/dtls/adminpanel/AdminPanelApplicationTests.java @@ -3,9 +3,11 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) +@ActiveProfiles(Profiles.TESTING) @SpringBootTest public class AdminPanelApplicationTests { diff --git a/src/test/resources/application-testing.yml b/src/test/resources/application-testing.yml new file mode 100644 index 0000000..f08c61a --- /dev/null +++ b/src/test/resources/application-testing.yml @@ -0,0 +1,4 @@ +spring: + data: + mongodb: + uri: mongodb://localhost:27017/admin-panel-test