/* * Copyright (c) 2020 Aaron Coburn and individual contributors * * 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. */ package org.trellisldp.jdbc; import static java.io.File.separator; import static org.slf4j.LoggerFactory.getLogger; import com.opentable.db.postgres.embedded.EmbeddedPostgres; import java.io.IOException; import java.sql.Connection; import java.sql.SQLException; import org.apache.commons.text.RandomStringGenerator; import org.slf4j.Logger; import liquibase.Contexts; import liquibase.Liquibase; import liquibase.database.jvm.JdbcConnection; import liquibase.exception.LiquibaseException; import liquibase.resource.ClassLoaderResourceAccessor; /** * DB Test Utilities. */ final class DBTestUtils { private static final Logger LOGGER = getLogger(DBTestUtils.class); static EmbeddedPostgres setupDatabase(final String directory) { try { final EmbeddedPostgres pg = EmbeddedPostgres.builder() .setDataDirectory(directory + separator + "pgdata-" + new RandomStringGenerator .Builder().withinRange('a', 'z').build().generate(10)).start(); // Set up database migrations try (final Connection c = pg.getPostgresDatabase().getConnection()) { final Liquibase liquibase = new Liquibase("org/trellisldp/jdbc/migrations.yml", new ClassLoaderResourceAccessor(), new JdbcConnection(c)); final Contexts ctx = null; liquibase.update(ctx); } return pg; } catch (final IOException | SQLException | LiquibaseException ex) { LOGGER.error("Error setting up tests", ex); } return null; } private DBTestUtils() { // prevent instantiation } }