diff --git a/src/test/java/net/hostsharing/hsadminng/hs/migration/LiquibaseCompatibilityIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/migration/LiquibaseCompatibilityIntegrationTest.java index d5c2ce7a..ddfcd7b5 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/migration/LiquibaseCompatibilityIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/migration/LiquibaseCompatibilityIntegrationTest.java @@ -1,6 +1,7 @@ package net.hostsharing.hsadminng.hs.migration; import liquibase.Liquibase; +import liquibase.exception.LiquibaseException; import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.rbac.test.JpaAttempt; import org.junit.jupiter.api.BeforeEach; @@ -15,6 +16,8 @@ import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.jdbc.Sql; import javax.sql.DataSource; +import java.util.List; +import java.util.Objects; import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.BEFORE_TEST_CLASS; @@ -22,61 +25,65 @@ import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.BEFORE_TE // TODO.impl: The reference-SQL-dump-generation needs to be automated // HOWTO: generate the prod-reference-SQL-dump during a prod-release -/** Tests, if the Liquibase scripts can be applied to a database ionitialized with schemas - and test-data from a previous version. - -

The test needs a dump, ideally from the version of the lastest prod-release:

- -
    -
  1. 1. clean the database:
    - pg-sql-reset - -
  2. populate the database:
    - ./gradlew bootRun --args='--spring.profiles.active=only-office' - -
  3. create the reference-schema SQL-file with some initializations:
  4. -
    cat >src/test/resources/db/prod-only-office-schema-with-test-data.sql <
    - -
  5. add the dump to that reference-schema SQL-file:

    -
    docker exec -i hsadmin-ng-postgres /usr/bin/pg_dump \
    -                --column-inserts --disable-dollar-quoting -U postgres postgres \
    -               >>src/test/resources/db/prod-only-office-schema-with-test-data.sql
    -            
    -
- -

The generated dump has to be committed to git and will be used in future test-runs - until it gets replaced at the next release.

-*/ +/** + * Tests, if the Liquibase scripts can be applied to a database ionitialized with schemas + * and test-data from a previous version. + * + *

The test needs a dump, ideally from the version of the lastest prod-release:

+ * + *
    + *
  1. 1. clean the database:
    + * pg-sql-reset + * + *
  2. populate the database:
    + * ./gradlew bootRun --args='--spring.profiles.active=only-office' + * + *
  3. create the reference-schema SQL-file with some initializations:
  4. + *
    cat >src/test/resources/db/prod-only-office-schema-with-test-data.sql <
    + * + *
  5. add the dump to that reference-schema SQL-file:

    + *
    docker exec -i hsadmin-ng-postgres /usr/bin/pg_dump \
    + * --column-inserts --disable-dollar-quoting -U postgres postgres \
    + * >>src/test/resources/db/prod-only-office-schema-with-test-data.sql
    + * 
    + *
+ * + *

The generated dump has to be committed to git and will be used in future test-runs + * until it gets replaced at the next release.

+ */ @Tag("importOfficeData") @DataJpaTest(properties = { - "spring.liquibase.enabled=false" // @Sql should go first, Liquibase will be initialized programmtically + "spring.liquibase.enabled=false" // @Sql should go first, Liquibase will be initialized programmatically }) @ActiveProfiles("only-office") @TestPropertySource(properties = "spring.liquibase.contexts=only-office") @DirtiesContext -@Import({ Context.class, JpaAttempt.class, LiquibaseConfig.class}) +@Import({ Context.class, JpaAttempt.class, LiquibaseConfig.class }) @Sql(value = "/db/prod-only-office-schema-with-test-data.sql", executionPhase = BEFORE_TEST_CLASS) public class LiquibaseCompatibilityIntegrationTest extends CsvDataImport { + private static final String EXPECTED_CHANGESET = "hs-hosting-SCHEMA"; + private static int initialChangeSetCount = 0; + @Autowired private DataSource dataSource; @@ -85,19 +92,34 @@ public class LiquibaseCompatibilityIntegrationTest extends CsvDataImport { @BeforeEach public void setup() throws Exception { - - final var schemas = em.createNativeQuery("SELECT * FROM pg_catalog.pg_tables WHERE schemaname='public'").getResultList(); - assertThat(schemas).hasSize(2); - - final var liquibaseScripts = em.createNativeQuery("SELECT * FROM public.databasechangelog").getResultList(); - assertThat(liquibaseScripts).hasSize(286); - - // Step 2: Run Liquibase migrations - liquibase.update(new liquibase.Contexts(), new liquibase.LabelExpression()); + assertThatDatabaseIsInitialized(); + runLiquibaseMigrations(); } @Test void test() { - // FIXME: check if changes got applied + final var liquibaseScripts = singleColumnSqlQuery("SELECT id FROM public.databasechangelog"); + assertThat(liquibaseScripts).hasSizeGreaterThan(initialChangeSetCount); + assertThat(liquibaseScripts).contains(EXPECTED_CHANGESET); + } + + private void assertThatDatabaseIsInitialized() { + final var schemas = singleColumnSqlQuery("SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname='public'"); + assertThat(schemas).containsExactly("databasechangelog", "databasechangeloglock"); + + final var liquibaseScripts = singleColumnSqlQuery("SELECT * FROM public.databasechangelog"); + assertThat(liquibaseScripts).hasSizeGreaterThan(285); + assertThat(liquibaseScripts).doesNotContain(EXPECTED_CHANGESET); + initialChangeSetCount = liquibaseScripts.size(); + } + + private void runLiquibaseMigrations() throws LiquibaseException { + liquibase.update(new liquibase.Contexts(), new liquibase.LabelExpression()); + } + + private List singleColumnSqlQuery(final String sql) { + //noinspection unchecked + final var rows = (List) em.createNativeQuery(sql).getResultList(); + return rows.stream().map(Objects::toString).toList(); } }