DB and RabbitMQ integration tests and PostgreSQL testing/bug fixing (#1047)

* Initial matrixSigned-off-by: Kai Zimmermann <kai.zimmermann@microsoft.com>

* License header

* MySQL DB testSigned-off-by: Kai Zimmermann <kai.zimmermann@microsoft.com>

* Create matrix for DBsSigned-off-by: Kai Zimmermann <kai.zimmermann@microsoft.com>

* RabbitMQ and H2Signed-off-by: Kai Zimmermann <kai.zimmermann@microsoft.com>

* MySQL 8.0Signed-off-by: Kai Zimmermann <kai.zimmermann@microsoft.com>

* Postgresql test supportSigned-off-by: Kai Zimmermann <kai.zimmermann@microsoft.com>

* Postgresql test supportSigned-off-by: Kai Zimmermann <kai.zimmermann@microsoft.com>

* Fix DB issues post and mssql

Signed-off-by: Kai Zimmermann <kai.zimmermann@microsoft.com>

* Wait MSSQL

Signed-off-by: Kai Zimmermann <kai.zimmermann@microsoft.com>

* Fix postgresql tests.

Signed-off-by: Kai Zimmermann <kai.zimmermann@microsoft.com>

* MSSQL startup fix.Signed-off-by: Kai Zimmermann <kai.zimmermann@microsoft.com>

* Fix syntax error

Signed-off-by: Kai Zimmermann <kai.zimmermann@microsoft.com>

* Further fix postgres tests.Signed-off-by: Kai Zimmermann <kai.zimmermann@microsoft.com>

* Revert unnecessary changes.

Signed-off-by: Kai Zimmermann <kai.zimmermann@microsoft.com>

* Add SonarCloud Signed-off-by: Kai Zimmermann <kai.zimmermann@microsoft.com>

* Simplify devcontainer. Test JDK 15Signed-off-by: Kai Zimmermann <kai.zimmermann@microsoft.com>
This commit is contained in:
Kai Zimmermann
2021-01-14 09:07:03 +01:00
committed by GitHub
parent 4ea5d7655b
commit e9f11d2a20
27 changed files with 557 additions and 114 deletions

View File

@@ -51,6 +51,10 @@
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<!-- Add a JDBC DB2 compatabile driver if you want to run tests against DB2, e.g. -->
<!-- <dependency> -->
<!-- <groupId>com.ibm.db2.jcc</groupId> -->

View File

@@ -107,7 +107,8 @@ import com.google.common.io.Files;
// test execution. So, the order execution between EventVerifier and Cleanup is
// important!
@TestExecutionListeners(inheritListeners = true, listeners = { EventVerifier.class, CleanupTestExecutionListener.class,
MySqlTestDatabase.class, MsSqlTestDatabase.class }, mergeMode = MergeMode.MERGE_WITH_DEFAULTS)
MySqlTestDatabase.class, MsSqlTestDatabase.class,
PostgreSqlTestDatabase.class }, mergeMode = MergeMode.MERGE_WITH_DEFAULTS)
@TestPropertySource(properties = "spring.main.allow-bean-definition-overriding=true")
public abstract class AbstractIntegrationTest {
private static final Logger LOG = LoggerFactory.getLogger(AbstractIntegrationTest.class);

View File

@@ -0,0 +1,55 @@
/**
* Copyright (c) 2020 Microsoft and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.hawkbit.repository.test.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestExecutionListener;
import org.springframework.test.context.support.AbstractTestExecutionListener;
/**
* A {@link TestExecutionListener} for creating and dropping MySql schemas if
* tests are setup with MySql.
*/
public abstract class AbstractSqlTestDatabase extends AbstractTestExecutionListener {
private static final Logger LOG = LoggerFactory.getLogger(AbstractSqlTestDatabase.class);
protected String schemaName;
protected String uri;
protected String username;
protected String password;
@Override
public void beforeTestClass(final TestContext testContext) throws Exception {
if (isRunningWithSql()) {
LOG.info("Setting up database for test class {}", testContext.getTestClass().getName());
this.username = System.getProperty("spring.datasource.username");
this.password = System.getProperty("spring.datasource.password");
this.uri = System.getProperty("spring.datasource.url");
createSchemaUri();
createSchema();
}
}
@Override
public void afterTestClass(final TestContext testContext) throws Exception {
if (isRunningWithSql()) {
dropSchema();
}
}
protected abstract void createSchemaUri();
protected abstract boolean isRunningWithSql();
protected abstract void createSchema();
protected abstract void dropSchema();
}

View File

@@ -16,53 +16,31 @@ import java.sql.SQLException;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestExecutionListener;
import org.springframework.test.context.support.AbstractTestExecutionListener;
/**
* A {@link TestExecutionListener} for creating and dropping MS SQL Server
* schemas if tests are setup with MS SQL Server.
*/
public class MsSqlTestDatabase extends AbstractTestExecutionListener {
public class MsSqlTestDatabase extends AbstractSqlTestDatabase {
private static final Logger LOG = LoggerFactory.getLogger(MsSqlTestDatabase.class);
private String schemaName;
private String uri;
private String username;
private String password;
@Override
public void beforeTestClass(final TestContext testContext) throws Exception {
if (isRunningWithMsSql()) {
LOG.info("Setting up mysql schema for test class {}", testContext.getTestClass().getName());
this.username = System.getProperty("spring.datasource.username");
this.password = System.getProperty("spring.datasource.password");
this.uri = System.getProperty("spring.datasource.url");
createSchemaUri();
createSchema();
}
}
@Override
public void afterTestClass(final TestContext testContext) throws Exception {
if (isRunningWithMsSql()) {
dropSchema();
}
}
private void createSchemaUri() {
protected void createSchemaUri() {
schemaName = "SP" + RandomStringUtils.randomAlphanumeric(10);
this.uri = this.uri.substring(0, uri.indexOf(';'));
System.setProperty("spring.datasource.url", uri + ";database=" + schemaName);
}
private static boolean isRunningWithMsSql() {
@Override
protected boolean isRunningWithSql() {
return "SQL_SERVER".equals(System.getProperty("spring.jpa.database"));
}
private void createSchema() {
@Override
protected void createSchema() {
try (Connection connection = DriverManager.getConnection(uri, username, password)) {
try (PreparedStatement statement = connection.prepareStatement("CREATE DATABASE " + schemaName + ";")) {
LOG.info("Creating schema {} on uri {}", schemaName, uri);
@@ -75,7 +53,8 @@ public class MsSqlTestDatabase extends AbstractTestExecutionListener {
}
private void dropSchema() {
@Override
protected void dropSchema() {
try (Connection connection = DriverManager.getConnection(uri, username, password)) {
// Needed to avoid the DROP is rejected with "database still in use"
try (PreparedStatement statement = connection

View File

@@ -16,53 +16,31 @@ import java.sql.SQLException;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestExecutionListener;
import org.springframework.test.context.support.AbstractTestExecutionListener;
/**
* A {@link TestExecutionListener} for creating and dropping MySql schemas if
* tests are setup with MySql.
*/
public class MySqlTestDatabase extends AbstractTestExecutionListener {
public class MySqlTestDatabase extends AbstractSqlTestDatabase {
private static final Logger LOG = LoggerFactory.getLogger(MySqlTestDatabase.class);
private String schemaName;
private String uri;
private String username;
private String password;
@Override
public void beforeTestClass(final TestContext testContext) throws Exception {
if (isRunningWithMySql()) {
LOG.info("Setting up mysql schema for test class {}", testContext.getTestClass().getName());
this.username = System.getProperty("spring.datasource.username");
this.password = System.getProperty("spring.datasource.password");
this.uri = System.getProperty("spring.datasource.url");
createSchemaUri();
createSchema();
}
}
@Override
public void afterTestClass(final TestContext testContext) throws Exception {
if (isRunningWithMySql()) {
dropSchema();
}
}
private void createSchemaUri() {
protected void createSchemaUri() {
schemaName = "SP" + RandomStringUtils.randomAlphanumeric(10);
this.uri = this.uri.substring(0, uri.lastIndexOf('/') + 1);
System.setProperty("spring.datasource.url", uri + schemaName);
}
private boolean isRunningWithMySql() {
@Override
protected boolean isRunningWithSql() {
return "MYSQL".equals(System.getProperty("spring.jpa.database"));
}
private void createSchema() {
@Override
protected void createSchema() {
try (Connection connection = DriverManager.getConnection(uri, username, password)) {
try (PreparedStatement statement = connection.prepareStatement("CREATE SCHEMA " + schemaName + ";")) {
LOG.info("Creating schema {} on uri {}", schemaName, uri);
@@ -75,7 +53,8 @@ public class MySqlTestDatabase extends AbstractTestExecutionListener {
}
private void dropSchema() {
@Override
protected void dropSchema() {
try (Connection connection = DriverManager.getConnection(uri, username, password)) {
try (PreparedStatement statement = connection.prepareStatement("DROP SCHEMA " + schemaName + ";")) {
LOG.info("Dropping schema {} on uri {}", schemaName, uri);

View File

@@ -0,0 +1,68 @@
/**
* Copyright (c) 2020 Microsoft and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.hawkbit.repository.test.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.TestExecutionListener;
/**
* A {@link TestExecutionListener} for creating and dropping MySql schemas if
* tests are setup with MySql.
*/
public class PostgreSqlTestDatabase extends AbstractSqlTestDatabase {
private static final Logger LOG = LoggerFactory.getLogger(PostgreSqlTestDatabase.class);
@Override
protected void createSchemaUri() {
schemaName = "sp" + RandomStringUtils.randomAlphanumeric(10).toLowerCase();
this.uri = this.uri.substring(0, uri.indexOf('?'));
System.setProperty("spring.datasource.url", uri + "?currentSchema=" + schemaName);
}
@Override
protected boolean isRunningWithSql() {
return "POSTGRESQL".equals(System.getProperty("spring.jpa.database"));
}
@Override
protected void createSchema() {
try (Connection connection = DriverManager.getConnection(uri, username, password)) {
try (PreparedStatement statement = connection.prepareStatement("CREATE schema " + schemaName + ";")) {
LOG.info("Creating schema {} on uri {}", schemaName, uri);
statement.execute();
LOG.info("Created schema {} on uri {}", schemaName, uri);
}
} catch (final SQLException e) {
LOG.error("Schema creation failed!", e);
}
}
@Override
protected void dropSchema() {
try (Connection connection = DriverManager.getConnection(uri, username, password)) {
try (PreparedStatement statement = connection.prepareStatement("DROP schema " + schemaName + " CASCADE;")) {
LOG.info("Dropping schema {} on uri {}", schemaName, uri);
statement.execute();
LOG.info("Dropped schema {} on uri {}", schemaName, uri);
}
} catch (final SQLException e) {
LOG.error("Schema drop failed!", e);
}
}
}