From: danielhanrahan Date: Wed, 22 Oct 2025 12:05:59 +0000 (+0100) Subject: Hibernate validation test X-Git-Tag: 8.2.2~13 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=25a1beb23d2e1a3658256d439ce3a1b76a2b2c71;p=policy%2Fclamp.git Hibernate validation test 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 --- 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 index 000000000..28d138d64 --- /dev/null +++ b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/liquibase/HibernateValidationTest.java @@ -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 index 000000000..9975fe1fd --- /dev/null +++ b/runtime-acm/src/test/resources/application-hibernate-validation.yaml @@ -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