diff --git a/.github/workflows/maven-publish.yml b/.github/workflows/maven-publish.yml index a8373f2..856cd8c 100644 --- a/.github/workflows/maven-publish.yml +++ b/.github/workflows/maven-publish.yml @@ -17,9 +17,7 @@ jobs: - name: Set up JDK uses: actions/setup-java@v4 with: - # using java 17 for now, to match original - # todo: migrate to java 21 (LTS) - java-version: 17 + java-version: 21 distribution: 'temurin' cache: maven # https://central.sonatype.org/publish/generate-portal-token/ diff --git a/README.md b/README.md index f43f5c8..fee75ba 100644 --- a/README.md +++ b/README.md @@ -10,9 +10,9 @@ Firstly, you need to define a migration runner bean in your configuration. ```java @Bean -public RdfProductionMigrationRunner rdfProductionMigrationRunner(RdfMigrationRepository rdfMigrationRepository, +public RdfMigrationRunner rdfMigrationRunner(RdfMigrationRepository rdfMigrationRepository, ApplicationContext appContext) { - RdfProductionMigrationRunner mr = new RdfProductionMigrationRunner(rdfMigrationRepository, appContext); + RdfMigrationRunner mr = new RdfMigrationRunner(rdfMigrationRepository, appContext); mr.run(); return mr; } @@ -36,7 +36,7 @@ That's all! You can start creating your migrations. See the example below. name = "Init migration", description = "Load initial data into Triple Store") @Service -public class Rdf_Migration_0001_Init implements RdfProductionMigration { +public class Rdf_Migration_0001_Init implements Migratable { @Autowired protected Repository repository; @@ -56,8 +56,8 @@ public class Rdf_Migration_0001_Init implements RdfProductionMigration { **Stack:** - - **Java** (recommended JDK 17) - - **Maven** (recommended 3.2.5 or higher) + - **Java** (recommended JDK 21) + - **Maven** (recommended 3.9.9 or higher) ### Package the application diff --git a/pom.xml b/pom.xml index cad6eaf..05be068 100644 --- a/pom.xml +++ b/pom.xml @@ -1,25 +1,24 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot spring-boot-starter-parent - - - 2.2.7.RELEASE - + + 3.3.5 + org.fairdatateam.rdf spring-rdf-migration - 2.0.0 + 3.0.0 jar Spring RDF Migration - Library for production migration of RDF Triple store + Library for migration of RDF Triple store https://github.com/FAIRDataTeam/spring-rdf-migration 2019 @@ -48,52 +47,59 @@ Vojtech Knaisl https://github.com/vknaisl - + - - UTF-8 - - - 17 - 17 - - - 1.18.34 - - - 4.6 - 3.10.1 - 3.3.1 - 0.6.0 - 3.2.7 + 17 + + 4.6 + 3.11.1 + 3.3.1 + 0.6.0 + 3.2.7 - - - + + org.springframework.boot + spring-boot-starter-test + test + org.springframework.boot spring-boot-starter-data-mongodb + + org.springframework.boot + spring-boot-starter-data-jpa + + + + org.postgresql + postgresql + runtime + + + com.h2database + h2 + test + - - - org.projectlombok lombok - ${lombok.version} + + + com.mycila license-maven-plugin - ${plugin.mavenlic.version} + ${plugins.license-maven-plugin.version}
com/mycila/maven/plugin/license/templates/MIT.txt
@@ -118,11 +124,12 @@ org.sonatype.central central-publishing-maven-plugin - ${plugin.publishing.version} + ${plugins.central-publishing-maven-plugin.version} true central - + false @@ -132,7 +139,7 @@ org.apache.maven.plugins maven-gpg-plugin - ${plugin.gpg.version} + ${plugins.maven-gpg-plugin.version} sign-artifacts @@ -142,7 +149,8 @@ true - + @@ -153,7 +161,7 @@ org.apache.maven.plugins maven-source-plugin - ${plugin.source.version} + ${plugins.maven-source-plugin.version} attach-sources @@ -166,7 +174,7 @@ org.apache.maven.plugins maven-javadoc-plugin - ${plugin.javadoc.version} + ${plugins.maven-javadoc-plugin.version} attach-javadocs diff --git a/src/main/java/org/fairdatateam/rdf/migration/database/RdfMigrationCrudRepository.java b/src/main/java/org/fairdatateam/rdf/migration/database/RdfMigrationCrudRepository.java new file mode 100644 index 0000000..7f84a78 --- /dev/null +++ b/src/main/java/org/fairdatateam/rdf/migration/database/RdfMigrationCrudRepository.java @@ -0,0 +1,38 @@ +/* + * The MIT License + * Copyright © 2019 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 org.fairdatateam.rdf.migration.database; + +import org.fairdatateam.rdf.migration.entity.JpaRdfMigration; + +import org.springframework.data.repository.CrudRepository; + +/** + * A repository for storing {@link JpaRdfMigration} + * entities in a relational database such as PostgreSQL. + * + * @author dennisvang + * @since 2.1.0 + */ +public interface RdfMigrationCrudRepository extends CrudRepository { + +} diff --git a/src/main/java/org/fairdatateam/rdf/migration/database/RdfMigrationRepository.java b/src/main/java/org/fairdatateam/rdf/migration/database/RdfMigrationMongoRepository.java similarity index 83% rename from src/main/java/org/fairdatateam/rdf/migration/database/RdfMigrationRepository.java rename to src/main/java/org/fairdatateam/rdf/migration/database/RdfMigrationMongoRepository.java index d2a1ab8..470f966 100644 --- a/src/main/java/org/fairdatateam/rdf/migration/database/RdfMigrationRepository.java +++ b/src/main/java/org/fairdatateam/rdf/migration/database/RdfMigrationMongoRepository.java @@ -22,15 +22,17 @@ */ package org.fairdatateam.rdf.migration.database; -import org.fairdatateam.rdf.migration.entity.RdfMigration; +import org.fairdatateam.rdf.migration.entity.MongoRdfMigration; + +import org.bson.types.ObjectId; import org.springframework.data.mongodb.repository.MongoRepository; /** - * A Mongo repository for manipulating with {@link RdfMigration} + * A Mongo repository for manipulating with {@link MongoRdfMigration} * * @author Vojtech Knaisl (vknaisl) * @since 1.0.0 */ -public interface RdfMigrationRepository extends MongoRepository { +public interface RdfMigrationMongoRepository extends MongoRepository { } diff --git a/src/main/java/org/fairdatateam/rdf/migration/entity/JpaRdfMigration.java b/src/main/java/org/fairdatateam/rdf/migration/entity/JpaRdfMigration.java new file mode 100644 index 0000000..18265d4 --- /dev/null +++ b/src/main/java/org/fairdatateam/rdf/migration/entity/JpaRdfMigration.java @@ -0,0 +1,92 @@ +/* + * The MIT License + * Copyright © 2019 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 org.fairdatateam.rdf.migration.entity; + +import java.time.Instant; + +import lombok.NoArgsConstructor; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.EntityListeners; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import lombok.Data; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; + +/** + * A record in the migration history of an RDF triple-store. + * + * The migration history itself is stored in a relational database, for example PostgreSQL, + * via the {@link org.fairdatateam.rdf.migration.database.RdfMigrationCrudRepository} + * repository interface. + * + * @author dennisvang + * @since 2.1.0 + */ +@Data +@RequiredArgsConstructor +@NoArgsConstructor +@Entity +@EntityListeners(AuditingEntityListener.class) +public class JpaRdfMigration { + + /** + * An auto-generated internal database record id + */ + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + + /** + * An auto-generated creation timestamp + */ + // CreatedDate requires auditing config: + // https://docs.spring.io/spring-data/jpa/reference/auditing.html#jpa.auditing.configuration + @CreatedDate + @Column(updatable = false) + private Instant createdAt; + + /** + * A migration number + */ + @NonNull + private Integer number; + + /** + * A migration name + */ + @NonNull + private String name; + + /** + * A migration description + */ + @NonNull + private String description; + +} diff --git a/src/main/java/org/fairdatateam/rdf/migration/entity/RdfMigration.java b/src/main/java/org/fairdatateam/rdf/migration/entity/MongoRdfMigration.java similarity index 94% rename from src/main/java/org/fairdatateam/rdf/migration/entity/RdfMigration.java rename to src/main/java/org/fairdatateam/rdf/migration/entity/MongoRdfMigration.java index d4ac3b8..55dfb20 100644 --- a/src/main/java/org/fairdatateam/rdf/migration/entity/RdfMigration.java +++ b/src/main/java/org/fairdatateam/rdf/migration/entity/MongoRdfMigration.java @@ -35,7 +35,7 @@ /** * RdfMigration encapsulates information about concrete migration. The information is saved in the - * database through {@link org.fairdatateam.rdf.migration.database.RdfMigrationRepository} + * database through {@link org.fairdatateam.rdf.migration.database.RdfMigrationMongoRepository} * * @author Vojtech Knaisl (vknaisl) * @since 1.0.0 @@ -45,7 +45,7 @@ @AllArgsConstructor @Getter @Setter -public class RdfMigration { +public class MongoRdfMigration { /** * An internal Mongo DB Id @@ -73,7 +73,7 @@ public class RdfMigration { */ protected LocalDateTime createdAt; - public RdfMigration(Integer number, String name, String description) { + public MongoRdfMigration(Integer number, String name, String description) { this.number = number; this.name = name; this.description = description; diff --git a/src/main/java/org/fairdatateam/rdf/migration/entity/RdfMigrationAnnotation.java b/src/main/java/org/fairdatateam/rdf/migration/entity/RdfMigrationAnnotation.java index f834a3e..2365ef8 100644 --- a/src/main/java/org/fairdatateam/rdf/migration/entity/RdfMigrationAnnotation.java +++ b/src/main/java/org/fairdatateam/rdf/migration/entity/RdfMigrationAnnotation.java @@ -39,14 +39,14 @@ public @interface RdfMigrationAnnotation { /** - * A number of the migration in your application ({@link org.fairdatateam.rdf.migration.entity.RdfMigration#number}) + * A number of the migration in your application ({@link org.fairdatateam.rdf.migration.entity.MongoRdfMigration#number}) * * @return A getter for the value */ int number(); /** - * A name of the migration ({@link org.fairdatateam.rdf.migration.entity.RdfMigration#name}) + * A name of the migration ({@link org.fairdatateam.rdf.migration.entity.MongoRdfMigration#name}) * * @return A getter for the value */ @@ -54,7 +54,7 @@ /** * A quick description of the purpose of the migration - * ({@link org.fairdatateam.rdf.migration.entity.RdfMigration#description}) + * ({@link org.fairdatateam.rdf.migration.entity.MongoRdfMigration#description}) * * @return A getter for the value */ diff --git a/src/main/java/org/fairdatateam/rdf/migration/package-info.java b/src/main/java/org/fairdatateam/rdf/migration/package-info.java index 9c48a02..e3b7d65 100644 --- a/src/main/java/org/fairdatateam/rdf/migration/package-info.java +++ b/src/main/java/org/fairdatateam/rdf/migration/package-info.java @@ -21,7 +21,7 @@ * THE SOFTWARE. */ /** - * A package related to production migrations of RDF Triple Stores. It fits into a Spring environment, and it uses + * A package related to migrations of RDF Triple Stores. It fits into a Spring environment, and it uses * MongoDB as a database for storing metadata about already executed migrations */ package org.fairdatateam.rdf.migration; \ No newline at end of file diff --git a/src/main/java/org/fairdatateam/rdf/migration/runner/JpaRdfMigrationRunner.java b/src/main/java/org/fairdatateam/rdf/migration/runner/JpaRdfMigrationRunner.java new file mode 100644 index 0000000..4e0c4b4 --- /dev/null +++ b/src/main/java/org/fairdatateam/rdf/migration/runner/JpaRdfMigrationRunner.java @@ -0,0 +1,111 @@ +/* + * The MIT License + * Copyright © 2019 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 org.fairdatateam.rdf.migration.runner; + +import lombok.extern.slf4j.Slf4j; +import org.fairdatateam.rdf.migration.database.RdfMigrationCrudRepository; +import org.fairdatateam.rdf.migration.entity.JpaRdfMigration; +import org.fairdatateam.rdf.migration.entity.RdfMigrationAnnotation; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Service; + +import java.util.Comparator; +import java.util.Objects; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + +/** + * The class contains a logic about executing the migration + * + * @author Vojtech Knaisl (vknaisl) + * @since 1.0.0 + */ +@Slf4j +@Service +public class JpaRdfMigrationRunner { + + /** + * A repository for storing information about metadata into a database + */ + private final RdfMigrationCrudRepository rdfMigrationRepository; + + /** + * A Spring application context that is needed to retrieve a concrete annotated migration class defined by a user + */ + private final ApplicationContext appContext; + + public JpaRdfMigrationRunner(RdfMigrationCrudRepository rdfMigrationRepository, + ApplicationContext appContext) { + this.rdfMigrationRepository = rdfMigrationRepository; + this.appContext = appContext; + } + + /** + * The method executes a migration process. It loads all migrations from the Spring Application context, filters + * them with already completed annotation, and runs the migrations that weren't run yet. + */ + public void run() { + log.info("Migration of RDF Store started"); + Iterable migrationsInDB = rdfMigrationRepository.findAll(); + Stream migrations = StreamSupport.stream(migrationsInDB.spliterator(), false); + int lastMigrationNumber = migrations + .map(JpaRdfMigration::getNumber) + .max(Integer::compareTo) + .orElse(0); + + appContext.getBeansWithAnnotation(RdfMigrationAnnotation.class) + .values() + .stream() + .map(o -> { + if (!(o instanceof Migratable)) { + log.error("Defined Migration has to implement Migratable"); + return null; + } + return (Migratable) o; + }) + .filter(Objects::nonNull) + .filter(m -> getAnnotation(m).number() > lastMigrationNumber) + .sorted(Comparator.comparingInt(m -> getAnnotation(m).number())) + .forEach(m -> { + JpaRdfMigration mEntity = new JpaRdfMigration(getAnnotation(m).number(), + getAnnotation(m).name(), + getAnnotation(m).description()); + log.info("Migration (n. {}) started", mEntity.getNumber()); + m.runMigration(); + rdfMigrationRepository.save(mEntity); + log.info("Migration (n. {}) ended", mEntity.getNumber()); + }); + log.info("Migration of RDF Store ended"); + } + + /** + * A helper method for retrieving an annotation from the provided object + * + * @param migration A migration from which we want to extract annotation + * @return The desired annotation + */ + private RdfMigrationAnnotation getAnnotation(Migratable migration) { + return migration.getClass().getAnnotation(RdfMigrationAnnotation.class); + } + +} diff --git a/src/main/java/org/fairdatateam/rdf/migration/runner/RdfProductionMigration.java b/src/main/java/org/fairdatateam/rdf/migration/runner/Migratable.java similarity index 92% rename from src/main/java/org/fairdatateam/rdf/migration/runner/RdfProductionMigration.java rename to src/main/java/org/fairdatateam/rdf/migration/runner/Migratable.java index 632ca89..c571e5a 100644 --- a/src/main/java/org/fairdatateam/rdf/migration/runner/RdfProductionMigration.java +++ b/src/main/java/org/fairdatateam/rdf/migration/runner/Migratable.java @@ -24,12 +24,12 @@ /** * An interface that is required to be implemented by a target migration class. A logic of the migration should be - * included in the method {@link RdfProductionMigration#runMigration()} + * included in the method {@link Migratable#runMigration()} * * @author Vojtech Knaisl (vknaisl) * @since 1.0.0 */ -public interface RdfProductionMigration { +public interface Migratable { /** * A content of the migration diff --git a/src/main/java/org/fairdatateam/rdf/migration/runner/RdfProductionMigrationRunner.java b/src/main/java/org/fairdatateam/rdf/migration/runner/MongoRdfMigrationRunner.java similarity index 75% rename from src/main/java/org/fairdatateam/rdf/migration/runner/RdfProductionMigrationRunner.java rename to src/main/java/org/fairdatateam/rdf/migration/runner/MongoRdfMigrationRunner.java index 5e91625..f0e7e4c 100644 --- a/src/main/java/org/fairdatateam/rdf/migration/runner/RdfProductionMigrationRunner.java +++ b/src/main/java/org/fairdatateam/rdf/migration/runner/MongoRdfMigrationRunner.java @@ -23,8 +23,8 @@ package org.fairdatateam.rdf.migration.runner; import lombok.extern.slf4j.Slf4j; -import org.fairdatateam.rdf.migration.database.RdfMigrationRepository; -import org.fairdatateam.rdf.migration.entity.RdfMigration; +import org.fairdatateam.rdf.migration.database.RdfMigrationMongoRepository; +import org.fairdatateam.rdf.migration.entity.MongoRdfMigration; import org.fairdatateam.rdf.migration.entity.RdfMigrationAnnotation; import org.springframework.context.ApplicationContext; @@ -39,19 +39,19 @@ * @since 1.0.0 */ @Slf4j -public class RdfProductionMigrationRunner { +public class MongoRdfMigrationRunner { /** * A repository for storing information about metadata into a database */ - private RdfMigrationRepository rdfMigrationRepository; + private RdfMigrationMongoRepository rdfMigrationRepository; /** * A Spring application context that is needed to retrieve a concrete annotated migration class defined by a user */ private ApplicationContext appContext; - public RdfProductionMigrationRunner(RdfMigrationRepository rdfMigrationRepository, + public MongoRdfMigrationRunner(RdfMigrationMongoRepository rdfMigrationRepository, ApplicationContext appContext) { this.rdfMigrationRepository = rdfMigrationRepository; this.appContext = appContext; @@ -62,11 +62,11 @@ public RdfProductionMigrationRunner(RdfMigrationRepository rdfMigrationRepositor * them with already completed annotation, and runs the migrations that weren't run yet. */ public void run() { - log.info("Production Migration of RDF Store started"); - List migrationsInDB = rdfMigrationRepository.findAll(); + log.info("Migration of RDF Store started"); + List migrationsInDB = rdfMigrationRepository.findAll(); int lastMigrationNumber = migrationsInDB .stream() - .map(RdfMigration::getNumber) + .map(MongoRdfMigration::getNumber) .max(Integer::compareTo) .orElse(0); @@ -74,25 +74,25 @@ public void run() { .values() .stream() .map(o -> { - if (!(o instanceof RdfProductionMigration)) { - log.error("Defined Migration has to be type of RdfProductionMigration"); + if (!(o instanceof Migratable)) { + log.error("Defined Migration has to implement Migratable"); return null; } - return (RdfProductionMigration) o; + return (Migratable) o; }) .filter(Objects::nonNull) .filter(m -> getAnnotation(m).number() > lastMigrationNumber) .sorted(Comparator.comparingInt(m -> getAnnotation(m).number())) .forEach(m -> { - RdfMigration mEntity = new RdfMigration(getAnnotation(m).number(), + MongoRdfMigration mEntity = new MongoRdfMigration(getAnnotation(m).number(), getAnnotation(m).name(), getAnnotation(m).description()); - log.info("Production Migration (n. {}) started", mEntity.getNumber()); + log.info("Migration (n. {}) started", mEntity.getNumber()); m.runMigration(); rdfMigrationRepository.save(mEntity); - log.info("Production Migration (n. {}) ended", mEntity.getNumber()); + log.info("Migration (n. {}) ended", mEntity.getNumber()); }); - log.info("Production Migration of RDF Store ended"); + log.info("Migration of RDF Store ended"); } /** @@ -101,9 +101,8 @@ public void run() { * @param migration A migration from which we want to extract annotation * @return The desired annotation */ - private RdfMigrationAnnotation getAnnotation(RdfProductionMigration migration) { + private RdfMigrationAnnotation getAnnotation(Migratable migration) { return migration.getClass().getAnnotation(RdfMigrationAnnotation.class); } } - diff --git a/src/test/java/org/fairdatateam/rdf/migration/JpaRdfMigrationRunnerTests.java b/src/test/java/org/fairdatateam/rdf/migration/JpaRdfMigrationRunnerTests.java new file mode 100644 index 0000000..d1df1e7 --- /dev/null +++ b/src/test/java/org/fairdatateam/rdf/migration/JpaRdfMigrationRunnerTests.java @@ -0,0 +1,74 @@ +/* + * The MIT License + * Copyright © 2019 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 org.fairdatateam.rdf.migration; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.fairdatateam.rdf.migration.database.RdfMigrationCrudRepository; +import org.fairdatateam.rdf.migration.entity.RdfMigrationAnnotation; +import org.fairdatateam.rdf.migration.runner.JpaRdfMigrationRunner; +import org.fairdatateam.rdf.migration.runner.Migratable; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; +import org.springframework.stereotype.Service; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@SpringBootTest +@AutoConfigureTestDatabase +public class JpaRdfMigrationRunnerTests { + + @Autowired + private RdfMigrationCrudRepository repository; + + @Autowired + private JpaRdfMigrationRunner runner; + + @Test + public void testRun() { + runner.run(); + assertEquals(1, repository.count()); + } + + @SpringBootApplication + @EnableJpaAuditing + static class TestConfiguration { + // for lack of an actual application + + // dummy migration to test + @RdfMigrationAnnotation(number = 1, name = "test", description = "test") + @Service + private static class TestMigration implements Migratable { + public void runMigration() { + log.info("running test migration"); + } + } + } + + +} diff --git a/src/test/java/org/fairdatateam/rdf/migration/JpaRdfMigrationTests.java b/src/test/java/org/fairdatateam/rdf/migration/JpaRdfMigrationTests.java new file mode 100644 index 0000000..05818f7 --- /dev/null +++ b/src/test/java/org/fairdatateam/rdf/migration/JpaRdfMigrationTests.java @@ -0,0 +1,69 @@ +/* + * The MIT License + * Copyright © 2019 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 org.fairdatateam.rdf.migration; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.fairdatateam.rdf.migration.database.RdfMigrationCrudRepository; +import org.fairdatateam.rdf.migration.entity.JpaRdfMigration; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; + +import lombok.extern.slf4j.Slf4j; + + +// https://docs.spring.io/spring-boot/reference/testing/spring-boot-applications.html#testing.spring-boot-applications.autoconfigured-spring-data-jpa +// https://docs.spring.io/spring-boot/reference/testing/spring-boot-applications.html#testing.spring-boot-applications.detecting-configuration +// https://spring.io/guides/gs/multi-module + +@Slf4j +@DataJpaTest +public class JpaRdfMigrationTests { + + @Autowired + private RdfMigrationCrudRepository repository; + + @Test + public void testGeneratedFields() { + // create new migration in database + repository.save(new JpaRdfMigration(1, "foo", "bar")); + // test + assertEquals(1, this.repository.count()); + JpaRdfMigration migration = this.repository.findAll().iterator().next(); + assertNotNull(migration.getCreatedAt()); + log.info("created at: {}", migration.getCreatedAt()); + } + + @SpringBootApplication + @EnableJpaAuditing + static class TestConfiguration { + // for lack of an actual application + } + +}