b01d30fa687c28ee6ffc1461a64690a59f65a251
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2026 OpenInfra Foundation Europe. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.runtime.liquibase;
22
23 import java.sql.Connection;
24 import java.sql.DriverManager;
25 import java.sql.SQLException;
26 import liquibase.Liquibase;
27 import liquibase.database.DatabaseFactory;
28 import liquibase.database.jvm.JdbcConnection;
29 import liquibase.exception.DatabaseException;
30 import liquibase.resource.ClassLoaderResourceAccessor;
31 import org.testcontainers.containers.PostgreSQLContainer;
32 import org.testcontainers.junit.jupiter.Container;
33
34 public abstract class AbstractLiquibaseTestBase {
35     @Container
36     protected static final PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres");
37
38     protected static Liquibase initLiquibase(Connection connection) throws DatabaseException {
39         var database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
40         return new Liquibase("db/changelog/db.changelog-master.yaml", new ClassLoaderResourceAccessor(), database);
41     }
42
43     protected static Connection initConnection() throws SQLException {
44         return DriverManager.getConnection(
45                 postgres.getJdbcUrl(),
46                 postgres.getUsername(),
47                 postgres.getPassword());
48     }
49 }