Refactor Liquibase Tests 36/142936/2
authorakenihan <adam.kenihan@est.tech>
Wed, 14 Jan 2026 13:09:43 +0000 (13:09 +0000)
committerakenihan <adam.kenihan@est.tech>
Thu, 15 Jan 2026 16:16:12 +0000 (16:16 +0000)
Issue-ID: POLICY-5527
Change-Id: I1f8d61243c5b7fba4fa6ade2c9ce4ea9bb0def08
Signed-off-by: akenihan <adam.kenihan@est.tech>
runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/liquibase/AbstractLiquibaseTestBase.java [new file with mode: 0644]
runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/liquibase/HibernateValidationTest.java
runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/liquibase/LiquibaseRollbackTest.java
runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/liquibase/LiquibaseSessionLockTest.java
runtime-acm/src/test/resources/testcontainers.properties [new file with mode: 0644]

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 (file)
index 0000000..b01d30f
--- /dev/null
@@ -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());
+    }
+}
index 1a498f5..b72a3b6 100644 (file)
@@ -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) {
index 2f997bd..bc18e33 100644 (file)
@@ -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());
-    }
-
 }
index 87b74ee..f73e036 100644 (file)
@@ -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 (file)
index 0000000..6e4c5a3
--- /dev/null
@@ -0,0 +1 @@
+postgres.container.image=registry.nordix.org/onaptest/postgres:14.1
\ No newline at end of file