Hibernate validation test 06/142306/2
authordanielhanrahan <daniel.hanrahan@est.tech>
Wed, 22 Oct 2025 12:05:59 +0000 (13:05 +0100)
committerDaniel Hanrahan <daniel.hanrahan@est.tech>
Thu, 23 Oct 2025 10:12:27 +0000 (10:12 +0000)
This adds a test where Hibernate validation is enabled.
This will detect mismatches between database schema and JPA
entity definitions. For example, if a new field is added in
Java but not in Liquibase, the test fails with an error:

  missing column [restarting] in table [AutomationComposition]

Issue-ID: POLICY-5401
Change-Id: Ieb718353ff1cdff0dd92415743aefc56d838b1e6
Signed-off-by: danielhanrahan <daniel.hanrahan@est.tech>
runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/liquibase/HibernateValidationTest.java [new file with mode: 0644]
runtime-acm/src/test/resources/application-hibernate-validation.yaml [new file with mode: 0644]

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
new file mode 100644 (file)
index 0000000..28d138d
--- /dev/null
@@ -0,0 +1,63 @@
+/*-
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2025 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 org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+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;
+
+/**
+ * This test enables Hibernate validation during context startup.
+ * Hibernate validation checks that the database schema matches the JPA entity mappings.
+ * It will detect the following issues:
+ * - missing tables or columns
+ * - incorrect column types
+ * It will NOT detect issues related to constraints (e.g. missing NOT NULL constraint),
+ * nor will it detect extra tables or columns in the database.
+ */
+@SpringBootTest
+@ActiveProfiles("hibernate-validation")
+@Testcontainers
+class HibernateValidationTest {
+
+    @Container
+    static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16");
+
+    @DynamicPropertySource
+    static void overrideProperties(DynamicPropertyRegistry registry) {
+        registry.add("spring.datasource.url", postgres::getJdbcUrl);
+        registry.add("spring.datasource.username", postgres::getUsername);
+        registry.add("spring.datasource.password", postgres::getPassword);
+        registry.add("spring.datasource.driver-class-name", postgres::getDriverClassName);
+    }
+
+    // Dummy test: Hibernate validation runs during context startup and throws exception on validation failure
+    @Test
+    void contextStartsAndHibernateValidationPasses() {
+        Assertions.assertTrue(true);
+    }
+}
diff --git a/runtime-acm/src/test/resources/application-hibernate-validation.yaml b/runtime-acm/src/test/resources/application-hibernate-validation.yaml
new file mode 100644 (file)
index 0000000..9975fe1
--- /dev/null
@@ -0,0 +1,13 @@
+spring:
+  liquibase:
+    enabled: true
+  jpa:
+    hibernate:
+      ddl-auto: validate
+    properties:
+      hibernate:
+        dialect: org.hibernate.dialect.PostgreSQLDialect
+
+logging:
+  level:
+    org.hibernate.tool.schema: INFO