Skip to content

Commit

Permalink
Fix unit tests related to databases calls
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-andrzejak committed Jul 25, 2023
1 parent df0f212 commit fdc2005
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 1 deletion.
2 changes: 1 addition & 1 deletion LICENSE-THIRD-PARTY.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Lists of 149 third-party dependencies.
(The Apache Software License, Version 2.0) Java Faker (com.github.javafaker:javafaker:1.0.2 - http://github.com/DiUS/java-faker)
(The Apache Software License, Version 2.0) Generex (com.github.mifmif:generex:1.0.2 - https://github.com/mifmif/Generex/tree/master)
(The Apache Software License, Version 2.0) project ':json-path' (com.jayway.jsonpath:json-path:2.6.0 - https://github.com/jayway/JsonPath)
(Apache License, Version 2.0) appstore-metadata-service (com.lgi.appstore.metadata:appstore-metadata-service:0.1.7-SNAPSHOT - https://spring.io/projects/spring-boot/appstore-metadata-service-parent/appstore-metadata-service)
(Apache License, Version 2.0) appstore-metadata-service (com.lgi.appstore.metadata:appstore-metadata-service:0.1.8-SNAPSHOT - https://spring.io/projects/spring-boot/appstore-metadata-service-parent/appstore-metadata-service)
(Eclipse Distribution License - v 1.0) Old JAXB Runtime (com.sun.xml.bind:jaxb-impl:2.3.3 - https://eclipse-ee4j.github.io/jaxb-ri/jaxb-bundles/jaxb-impl)
(Apache License 2.0) JSON library from Android SDK (com.vaadin.external.google:android-json:0.0.20131108.vaadin1 - http://developer.android.com/sdk)
(The Apache Software License, Version 2.0) HikariCP (com.zaxxer:HikariCP:4.0.3 - https://github.com/brettwooldridge/HikariCP)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2023 Liberty Global Technology Services BV
*
* 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 com.lgi.appstore.metadata.api.delegate;

import org.testcontainers.containers.ContainerState;
import org.testcontainers.delegate.AbstractDatabaseDelegate;
import org.testcontainers.exception.ConnectionCreationException;
import org.testcontainers.ext.ScriptUtils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

import static java.util.Objects.requireNonNull;

public class TestContainerPostgresSQLDelegate extends AbstractDatabaseDelegate<Connection> {
private final static String USER_PROPERTY_KEY = "user";
private final static String PASSWORD_PROPERTY_KEY = "password";
private final static String JDBC_URL_TEMPLATE = "jdbc:postgresql://localhost:%s/test";
private static final String SELECT_ONE_QUERY = "SELECT 1";
private final static String CONNECTION_CREATION_EXCEPTION_MSG = "Could not obtain PostgresSQL connection";
private final ContainerState container;
private final String user;
private final String password;
public TestContainerPostgresSQLDelegate(final ContainerState container, final String user, final String password) {
this.container = requireNonNull(container, "container");
this.user = requireNonNull(user, "user");
this.password = requireNonNull(password, "password");
}

@Override
protected Connection createNewConnection() {
try {
Properties connectionProps = new Properties();
connectionProps.put(USER_PROPERTY_KEY, user);
connectionProps.put(PASSWORD_PROPERTY_KEY, password);
return DriverManager.getConnection(
String.format(JDBC_URL_TEMPLATE, container.getFirstMappedPort()),
connectionProps);
} catch (Exception e) {
throw new ConnectionCreationException(CONNECTION_CREATION_EXCEPTION_MSG, e);
}
}

@Override
public void execute(String statement, String scriptPath, int lineNumber, boolean continueOnError, boolean ignoreFailedDrops) {
try {
getConnection().prepareStatement(statement).executeQuery();
} catch (Exception e) {
throw new ScriptUtils.ScriptStatementFailedException(statement, lineNumber, scriptPath, e);
}
}

public void selectOne() {
execute(SELECT_ONE_QUERY, "", 1, false, false);
}

@Override
protected void closeConnectionQuietly(Connection connection) {
try {
connection.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package com.lgi.appstore.metadata.api.persistence;

import com.lgi.appstore.metadata.api.strategy.TestContainerPostgresSQLWaitStrategy;
import com.lgi.appstore.metadata.jooq.model.DefaultCatalog;
import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.configuration.Configuration;
Expand All @@ -27,6 +28,7 @@
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.containers.wait.strategy.WaitStrategy;

import java.util.List;

Expand All @@ -36,6 +38,9 @@ public class PostgresContainerInitializer implements ApplicationContextInitializ

static {
POSTGRE_SQL_CONTAINER = new PostgreSQLContainer<>("postgres:12");
WaitStrategy postgresSQLWaitStrategy = new TestContainerPostgresSQLWaitStrategy(POSTGRE_SQL_CONTAINER.getUsername(),
POSTGRE_SQL_CONTAINER.getPassword());
POSTGRE_SQL_CONTAINER.waitingFor(postgresSQLWaitStrategy);
POSTGRE_SQL_CONTAINER.start();

final List<Schema> schemas = DefaultCatalog.DEFAULT_CATALOG.getSchemas();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2023 Liberty Global Technology Services BV
*
* 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 com.lgi.appstore.metadata.api.strategy;

import com.lgi.appstore.metadata.api.delegate.TestContainerPostgresSQLDelegate;
import org.testcontainers.containers.ContainerLaunchException;
import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy;

import java.util.concurrent.TimeUnit;

import static java.util.Objects.requireNonNull;
import static org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess;

public class TestContainerPostgresSQLWaitStrategy extends AbstractWaitStrategy {
private static final String TIMEOUT_ERROR = "Timed out waiting for PostgresSQL to be accessible for query execution";
private final String user;
private final String password;

public TestContainerPostgresSQLWaitStrategy(final String user, final String password) {
this.user = requireNonNull(user, "user");
this.password = requireNonNull(password, "password");
}

@Override
protected void waitUntilReady() {
try {
retryUntilSuccess((int) startupTimeout.getSeconds(), TimeUnit.SECONDS, () -> {
getRateLimiter().doWhenReady(() -> {
try (TestContainerPostgresSQLDelegate databaseDelegate = getDatabaseDelegate()) {
databaseDelegate.selectOne();
}
});
return true;
});
} catch (Exception e) {
throw new ContainerLaunchException(TIMEOUT_ERROR);
}
}

private TestContainerPostgresSQLDelegate getDatabaseDelegate() {
return new TestContainerPostgresSQLDelegate(waitStrategyTarget, user, password);
}
}
19 changes: 19 additions & 0 deletions charts/index.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
#
# If not stated otherwise in this file or this component's LICENSE file the
# following copyright and licenses apply:
#
# Copyright 2023 Liberty Global Technology Services BV
#
# 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.
#

apiVersion: v1
entries:
appstore-metadata-service:
Expand Down

0 comments on commit fdc2005

Please sign in to comment.