From 06464962a832bb14b7a3a005d8598201344deb8e Mon Sep 17 00:00:00 2001 From: akenihan Date: Wed, 14 Jan 2026 13:09:43 +0000 Subject: [PATCH] Refactor Liquibase Tests Issue-ID: POLICY-5527 Change-Id: I1f8d61243c5b7fba4fa6ade2c9ce4ea9bb0def08 Signed-off-by: akenihan --- .../liquibase/AbstractLiquibaseTestBase.java | 49 ++++++++++++++++++++++ .../runtime/liquibase/HibernateValidationTest.java | 9 +--- .../runtime/liquibase/LiquibaseRollbackTest.java | 19 +-------- .../liquibase/LiquibaseSessionLockTest.java | 26 +----------- .../src/test/resources/testcontainers.properties | 1 + 5 files changed, 53 insertions(+), 51 deletions(-) create mode 100644 runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/liquibase/AbstractLiquibaseTestBase.java create mode 100644 runtime-acm/src/test/resources/testcontainers.properties diff --git a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/liquibase/AbstractLiquibaseTestBase.java b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/liquibase/AbstractLiquibaseTestBase.java new file mode 100644 index 000000000..b01d30fa6 --- /dev/null +++ b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/liquibase/AbstractLiquibaseTestBase.java @@ -0,0 +1,49 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2026 OpenInfra Foundation Europe. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.clamp.acm.runtime.liquibase; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import liquibase.Liquibase; +import liquibase.database.DatabaseFactory; +import liquibase.database.jvm.JdbcConnection; +import liquibase.exception.DatabaseException; +import liquibase.resource.ClassLoaderResourceAccessor; +import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.junit.jupiter.Container; + +public abstract class AbstractLiquibaseTestBase { + @Container + protected static final PostgreSQLContainer postgres = new PostgreSQLContainer<>("postgres"); + + protected static Liquibase initLiquibase(Connection connection) throws DatabaseException { + var database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection)); + return new Liquibase("db/changelog/db.changelog-master.yaml", new ClassLoaderResourceAccessor(), database); + } + + protected static Connection initConnection() throws SQLException { + return DriverManager.getConnection( + postgres.getJdbcUrl(), + postgres.getUsername(), + postgres.getPassword()); + } +} diff --git a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/liquibase/HibernateValidationTest.java b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/liquibase/HibernateValidationTest.java index 1a498f51e..b72a3b6fc 100644 --- a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/liquibase/HibernateValidationTest.java +++ b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/liquibase/HibernateValidationTest.java @@ -26,10 +26,7 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.DynamicPropertyRegistry; import org.springframework.test.context.DynamicPropertySource; -import org.testcontainers.containers.PostgreSQLContainer; -import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; -import org.testcontainers.utility.DockerImageName; /** * This test enables Hibernate validation during context startup. @@ -43,11 +40,7 @@ import org.testcontainers.utility.DockerImageName; @SpringBootTest @ActiveProfiles("hibernate-validation") @Testcontainers -class HibernateValidationTest { - - @Container - private static final PostgreSQLContainer postgres = new PostgreSQLContainer<>( - DockerImageName.parse("registry.nordix.org/onaptest/postgres:14.1").asCompatibleSubstituteFor("postgres")); +class HibernateValidationTest extends AbstractLiquibaseTestBase { @DynamicPropertySource static void overrideProperties(DynamicPropertyRegistry registry) { diff --git a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/liquibase/LiquibaseRollbackTest.java b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/liquibase/LiquibaseRollbackTest.java index 2f997bda1..bc18e3334 100644 --- a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/liquibase/LiquibaseRollbackTest.java +++ b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/liquibase/LiquibaseRollbackTest.java @@ -22,8 +22,6 @@ package org.onap.policy.clamp.acm.runtime.liquibase; import java.io.PrintStream; import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; import java.sql.Statement; import java.util.stream.Stream; import liquibase.Liquibase; @@ -41,19 +39,12 @@ import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -import org.testcontainers.containers.PostgreSQLContainer; -import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; -import org.testcontainers.utility.DockerImageName; // This test class verifies that rollbacks for each Liquibase release tag works correctly. @Testcontainers @TestInstance(TestInstance.Lifecycle.PER_CLASS) -class LiquibaseRollbackTest { - - @Container - private static final PostgreSQLContainer postgres = new PostgreSQLContainer<>( - DockerImageName.parse("registry.nordix.org/onaptest/postgres:14.1").asCompatibleSubstituteFor("postgres")); +class LiquibaseRollbackTest extends AbstractLiquibaseTestBase { private Liquibase liquibase; @@ -158,12 +149,4 @@ class LiquibaseRollbackTest { database.setDefaultSchemaName(schema); return new Liquibase("db/changelog/db.changelog-master.yaml", new ClassLoaderResourceAccessor(), database); } - - private Connection initConnection() throws SQLException { - return DriverManager.getConnection( - postgres.getJdbcUrl(), - postgres.getUsername(), - postgres.getPassword()); - } - } diff --git a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/liquibase/LiquibaseSessionLockTest.java b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/liquibase/LiquibaseSessionLockTest.java index 87b74ee72..f73e03657 100644 --- a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/liquibase/LiquibaseSessionLockTest.java +++ b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/liquibase/LiquibaseSessionLockTest.java @@ -26,27 +26,15 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.fail; import java.sql.Connection; -import java.sql.DriverManager; import java.sql.SQLException; import java.util.concurrent.TimeUnit; import liquibase.Liquibase; -import liquibase.database.DatabaseFactory; -import liquibase.database.jvm.JdbcConnection; -import liquibase.exception.DatabaseException; import liquibase.exception.LiquibaseException; -import liquibase.resource.ClassLoaderResourceAccessor; import org.junit.jupiter.api.Test; -import org.testcontainers.containers.PostgreSQLContainer; -import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; -import org.testcontainers.utility.DockerImageName; @Testcontainers -class LiquibaseSessionLockTest { - - @Container - private static final PostgreSQLContainer postgres = new PostgreSQLContainer<>( - DockerImageName.parse("registry.nordix.org/onaptest/postgres:14.1").asCompatibleSubstituteFor("postgres")); +class LiquibaseSessionLockTest extends AbstractLiquibaseTestBase { @Test void shouldCleanupStaleLockWhenConnectionFails() throws Exception { @@ -88,16 +76,4 @@ class LiquibaseSessionLockTest { private static int getLockCount(Liquibase liquibase) throws LiquibaseException { return liquibase.listLocks().length; } - - private static Liquibase initLiquibase(Connection connection) throws DatabaseException { - var database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection)); - return new Liquibase("db/changelog/db.changelog-master.yaml", new ClassLoaderResourceAccessor(), database); - } - - private static Connection initConnection() throws SQLException { - return DriverManager.getConnection( - postgres.getJdbcUrl(), - postgres.getUsername(), - postgres.getPassword()); - } } diff --git a/runtime-acm/src/test/resources/testcontainers.properties b/runtime-acm/src/test/resources/testcontainers.properties new file mode 100644 index 000000000..6e4c5a351 --- /dev/null +++ b/runtime-acm/src/test/resources/testcontainers.properties @@ -0,0 +1 @@ +postgres.container.image=registry.nordix.org/onaptest/postgres:14.1 \ No newline at end of file -- 2.16.6