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:
Kai Zimmermann
2016-05-31 12:28:24 +02:00
parent b1a2a583e9
commit 91fd94b356
9 changed files with 52 additions and 184 deletions

View File

@@ -387,7 +387,7 @@ public class MgmtDistributionSetResourceTest extends AbstractRestIntegrationTest
@WithUser(principal = "uploadTester", allSpPermissions = true)
@Description("Ensures that single DS requested by ID is listed with expected payload.")
public void getDistributionSet() throws Exception {
final DistributionSet set = testdataFactory.createTestDistributionSet();
final DistributionSet set = testdataFactory.createUpdatedDistributionSet();
// perform request
mvc.perform(get("/rest/v1/distributionsets/{dsId}", set.getId()).accept(MediaType.APPLICATION_JSON))

View File

@@ -23,7 +23,7 @@
<Logger name="org.apache.catalina.startup.DigesterFactory" level="ERROR" />
<Logger name="org.eclipse.hawkbit.rest.util.MockMvcResultPrinter" level="DEBUG" />
<!-- <Logger name="org.eclipse.hawkbit.rest.util.MockMvcResultPrinter" level="DEBUG" /> -->
<!-- Security Log with hints on potential attacks -->
<logger name="server-security" level="INFO" />

View File

@@ -13,8 +13,6 @@ import javax.persistence.PersistenceContext;
import org.eclipse.hawkbit.cache.TenantAwareCacheManager;
import org.eclipse.hawkbit.repository.util.AbstractIntegrationTest;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.data.mongodb.gridfs.GridFsOperations;
@@ -79,26 +77,4 @@ public abstract class AbstractJpaIntegrationTest extends AbstractIntegrationTest
@Autowired
protected TenantAwareCacheManager cacheManager;
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();
}
}
}

View File

@@ -13,11 +13,8 @@ import javax.persistence.PersistenceContext;
import org.eclipse.hawkbit.cache.TenantAwareCacheManager;
import org.eclipse.hawkbit.repository.util.AbstractIntegrationTestWithMongoDB;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.data.mongodb.gridfs.GridFsOperations;
@SpringApplicationConfiguration(classes = { org.eclipse.hawkbit.RepositoryApplicationConfiguration.class,
TestConfiguration.class })
@@ -68,9 +65,6 @@ public abstract class AbstractJpaIntegrationTestWithMongoDB extends AbstractInte
@Autowired
protected TargetInfoRepository targetInfoRepository;
@Autowired
protected GridFsOperations operations;
@Autowired
protected RolloutGroupRepository rolloutGroupRepository;
@@ -80,25 +74,4 @@ public abstract class AbstractJpaIntegrationTestWithMongoDB extends AbstractInte
@Autowired
protected TenantAwareCacheManager cacheManager;
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();
}
}
}

View File

@@ -1,118 +0,0 @@
/**
* 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.jpa;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.UUID;
import org.h2.tools.Server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
*
*/
public class LocalH2TestDatabase implements Testdatabase {
private final static Logger LOG = LoggerFactory.getLogger(LocalH2TestDatabase.class);
private final int port;
private Server h2server;
private boolean dbStarted;
private String uri;
public LocalH2TestDatabase(final int port) {
super();
this.port = port;
createUri();
initSystemProperties();
}
private final void initSystemProperties() {
System.setProperty("spring.datasource.driverClassName", getDriverClassName());
System.setProperty("spring.datasource.username", "");
System.setProperty("spring.datasource.password", "");
System.setProperty("hawkbit.server.database", "H2");
}
private void dropAllObjects() {
try (Connection connection = DriverManager.getConnection(uri)) {
connection.prepareCall("DROP ALL OBJECTS;").execute();
} catch (final SQLException e) {
e.printStackTrace();
}
}
@Override
public void before() {
try {
startDatabase();
} catch (ClassNotFoundException | SQLException | IOException e) {
e.printStackTrace();
}
}
@Override
public void after() {
try {
stopDatabase();
} catch (ClassNotFoundException | SQLException | IOException e) {
e.printStackTrace();
}
}
private void startDatabase() throws SQLException, ClassNotFoundException, IOException {
if (dbStarted) {
return;
}
// Start H2 database for OpenFire
h2server = Server
.createTcpServer(
new String[] { "-tcpPort", String.valueOf(port), "-tcpAllowOthers", "-tcpShutdownForce" })
.start();
dbStarted = true;
LOG.info("H2 Database started on port {} and uri {}", port, uri);
dropAllObjects();
}
private final void createUri() {
this.uri = "jdbc:h2:tcp://localhost:" + port + "/mem:SP" + UUID.randomUUID().toString() + ";MVCC=TRUE;"
+ "DB_CLOSE_DELAY=-1";
System.setProperty("spring.datasource.url", uri);
}
private void stopDatabase() throws SQLException, ClassNotFoundException, IOException {
if (!dbStarted) {
return;
}
h2server.stop();
h2server = null;
dbStarted = false;
try {
Thread.sleep(1000);
} catch (final InterruptedException e) {
}
}
@Override
public String getDriverClassName() {
return "org.h2.Driver";
}
@Override
public String getUri() {
return uri;
}
}

View File

@@ -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();
}
}
}

View File

@@ -6,10 +6,11 @@
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.hawkbit.repository.jpa;
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;
@@ -17,17 +18,20 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* {@link Testdatabase} implementation for MySQL.
*
*/
public class CIMySqlTestDatabase implements Testdatabase {
private final static Logger LOG = LoggerFactory.getLogger(CIMySqlTestDatabase.class);
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");
@@ -43,7 +47,7 @@ public class CIMySqlTestDatabase implements Testdatabase {
private void createSchemaUri() {
schemaName = "SP" + RandomStringUtils.randomAlphanumeric(10);
this.uri = this.uri.substring(0, uri.lastIndexOf("/") + 1);
this.uri = this.uri.substring(0, uri.lastIndexOf('/') + 1);
System.setProperty("spring.datasource.url", uri + schemaName);
}
@@ -55,10 +59,12 @@ public class CIMySqlTestDatabase implements Testdatabase {
private void createSchema() {
try (Connection connection = DriverManager.getConnection(uri, username, password)) {
connection.prepareStatement("CREATE SCHEMA " + schemaName + ";").execute();
LOG.info("Schema {} created on uri {}", schemaName, uri);
try (PreparedStatement statement = connection.prepareStatement("CREATE SCHEMA " + schemaName + ";")) {
statement.execute();
LOG.info("Schema {} created on uri {}", schemaName, uri);
}
} catch (final SQLException e) {
e.printStackTrace();
LOG.error("Schema creation failed!", e);
}
}
@@ -70,10 +76,12 @@ public class CIMySqlTestDatabase implements Testdatabase {
private void dropSchema() {
try (Connection connection = DriverManager.getConnection(uri, username, password)) {
connection.prepareStatement("DROP SCHEMA " + schemaName + ";").execute();
LOG.info("Schema {} dropped on uri {}", schemaName, uri);
try (PreparedStatement statement = connection.prepareStatement("DROP SCHEMA " + schemaName + ";")) {
statement.execute();
LOG.info("Schema {} dropped on uri {}", schemaName, uri);
}
} catch (final SQLException e) {
e.printStackTrace();
LOG.error("Schema drop failed!", e);
}
}

View File

@@ -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);

View File

@@ -6,7 +6,7 @@
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.hawkbit.repository.jpa;
package org.eclipse.hawkbit.repository.util;
/**
*