Migrated MySQL schema creation to test package. Small doc improvements
on testdatafactory. Signed-off-by: Kai Zimmermann <kai.zimmermann@bosch-si.com>
This commit is contained in:
@@ -29,7 +29,9 @@ import org.eclipse.hawkbit.security.DosFilter;
|
||||
import org.eclipse.hawkbit.security.SystemSecurityContext;
|
||||
import org.eclipse.hawkbit.tenancy.TenantAware;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Rule;
|
||||
import org.junit.rules.MethodRule;
|
||||
import org.junit.rules.TestWatchman;
|
||||
@@ -211,4 +213,25 @@ public abstract class AbstractIntegrationTest implements EnvironmentAware {
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private static CIMySqlTestDatabase tesdatabase;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() {
|
||||
createTestdatabaseAndStart();
|
||||
}
|
||||
|
||||
private static void createTestdatabaseAndStart() {
|
||||
if ("MYSQL".equals(System.getProperty("spring.jpa.database"))) {
|
||||
tesdatabase = new CIMySqlTestDatabase();
|
||||
tesdatabase.before();
|
||||
}
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() {
|
||||
if (tesdatabase != null) {
|
||||
tesdatabase.after();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
/**
|
||||
* Copyright (c) 2015 Bosch Software Innovations GmbH 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.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;
|
||||
|
||||
/**
|
||||
* {@link Testdatabase} implementation for MySQL.
|
||||
*
|
||||
*/
|
||||
public class CIMySqlTestDatabase implements Testdatabase {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(CIMySqlTestDatabase.class);
|
||||
private String schemaName;
|
||||
private String uri;
|
||||
private final String username;
|
||||
private final String password;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public CIMySqlTestDatabase() {
|
||||
this.username = System.getProperty("spring.datasource.username");
|
||||
this.password = System.getProperty("spring.datasource.password");
|
||||
this.uri = System.getProperty("spring.datasource.url");
|
||||
createSchemaUri();
|
||||
initSystemProperties();
|
||||
}
|
||||
|
||||
private final void initSystemProperties() {
|
||||
System.setProperty("spring.datasource.driverClassName", getDriverClassName());
|
||||
System.setProperty("spring.jpa.database", "MYSQL");
|
||||
}
|
||||
|
||||
private void createSchemaUri() {
|
||||
schemaName = "SP" + RandomStringUtils.randomAlphanumeric(10);
|
||||
this.uri = this.uri.substring(0, uri.lastIndexOf('/') + 1);
|
||||
|
||||
System.setProperty("spring.datasource.url", uri + schemaName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void before() {
|
||||
createSchema();
|
||||
}
|
||||
|
||||
private void createSchema() {
|
||||
try (Connection connection = DriverManager.getConnection(uri, username, password)) {
|
||||
try (PreparedStatement statement = connection.prepareStatement("CREATE SCHEMA " + schemaName + ";")) {
|
||||
statement.execute();
|
||||
LOG.info("Schema {} created on uri {}", schemaName, uri);
|
||||
}
|
||||
} catch (final SQLException e) {
|
||||
LOG.error("Schema creation failed!", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void after() {
|
||||
dropSchema();
|
||||
}
|
||||
|
||||
private void dropSchema() {
|
||||
try (Connection connection = DriverManager.getConnection(uri, username, password)) {
|
||||
try (PreparedStatement statement = connection.prepareStatement("DROP SCHEMA " + schemaName + ";")) {
|
||||
statement.execute();
|
||||
LOG.info("Schema {} dropped on uri {}", schemaName, uri);
|
||||
}
|
||||
} catch (final SQLException e) {
|
||||
LOG.error("Schema drop failed!", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDriverClassName() {
|
||||
return "org.mariadb.jdbc.Driver";
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUri() {
|
||||
return uri;
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,7 @@ import org.eclipse.hawkbit.repository.model.Action;
|
||||
import org.eclipse.hawkbit.repository.model.Action.Status;
|
||||
import org.eclipse.hawkbit.repository.model.ActionStatus;
|
||||
import org.eclipse.hawkbit.repository.model.Artifact;
|
||||
import org.eclipse.hawkbit.repository.model.BaseEntity;
|
||||
import org.eclipse.hawkbit.repository.model.DistributionSet;
|
||||
import org.eclipse.hawkbit.repository.model.DistributionSetTag;
|
||||
import org.eclipse.hawkbit.repository.model.DistributionSetType;
|
||||
@@ -219,7 +220,7 @@ public class TestdataFactory {
|
||||
* @param version
|
||||
* {@link DistributionSet#getVersion()} and
|
||||
* {@link SoftwareModule#getVersion()} extended by a random
|
||||
* number.
|
||||
* number.updat
|
||||
* @param tags
|
||||
* {@link DistributionSet#getTags()}
|
||||
*
|
||||
@@ -352,9 +353,14 @@ public class TestdataFactory {
|
||||
* iterative number and {@link DistributionSet#isRequiredMigrationStep()}
|
||||
* <code>false</code>.
|
||||
*
|
||||
* In addition it updates the ccreated {@link DistributionSet}s and
|
||||
* {@link SoftwareModule}s to ensure that
|
||||
* {@link BaseEntity#getLastModifiedAt()} and
|
||||
* {@link BaseEntity#getLastModifiedBy()} is filled.
|
||||
*
|
||||
* @return persisted {@link DistributionSet}.
|
||||
*/
|
||||
public DistributionSet createTestDistributionSet() {
|
||||
public DistributionSet createUpdatedDistributionSet() {
|
||||
DistributionSet set = createDistributionSet("");
|
||||
set.setVersion(DEFAULT_VERSION);
|
||||
set = distributionSetManagement.updateDistributionSet(set);
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Copyright (c) 2015 Bosch Software Innovations GmbH 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.util;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
public interface Testdatabase {
|
||||
|
||||
void before();
|
||||
|
||||
void after();
|
||||
|
||||
public String getUri();
|
||||
|
||||
String getDriverClassName();
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user