Skip to content

Commit

Permalink
Refined integration tests. Creating a temporary test db. Issue #4
Browse files Browse the repository at this point in the history
  • Loading branch information
enridaga committed Dec 31, 2015
1 parent fbdc8ba commit 1ab5a01
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 21 deletions.
1 change: 0 additions & 1 deletion basil/pom.xml
Expand Up @@ -150,7 +150,6 @@
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>

<dependency>
Expand Down
76 changes: 60 additions & 16 deletions it/pom.xml
Expand Up @@ -19,7 +19,13 @@

<properties>
<!-- Set this to the basil configuration file -->
<basil.configurationFile>../basil.ini</basil.configurationFile>
<basil.configurationFile>basil.ini</basil.configurationFile>

<!-- Create a new test database or use an existing one. When true, a new
database is created using dbname from the config file plus a random postfix.
After tests, the db is deleted. Note that DB User must have sufficient privileges
to perform this. -->
<test.db.init>true</test.db.init>

<!-- Set this to run the server on a specific port -->
<http.port></http.port>
Expand All @@ -29,9 +35,9 @@

<!-- Log4J Configuration -->
<log4j.configurationFile>src/test/resources/log4j2.xml</log4j.configurationFile>

<!-- Options for the VM that executes our runnable jar -->
<jar.executor.vm.options>-Xmx1g -Dbasil.configurationFile=${basil.configurationFile} -Dlog4j.configurationFile=${log4j.configurationFile}</jar.executor.vm.options>
<jar.executor.vm.options>-Xmx1g -Dlog4j.configurationFile=${log4j.configurationFile}</jar.executor.vm.options>

<!-- Set this to true to keep the runnable jar running - useful to debug
tests -->
Expand Down Expand Up @@ -70,6 +76,31 @@
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>generate-test-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/target/dependency</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/..</directory>
<include>db.sql</include>
<include>${basil.configurationFile}</include>
<!-- <filtering>true</filtering> -->
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<!-- Find free ports to run our server -->
<groupId>org.codehaus.mojo</groupId>
Expand All @@ -95,14 +126,17 @@
<configuration>
<systemPropertyVariables>
<test.server.url>${test.server.url}</test.server.url>
<test.db.init>${test.db.init}</test.db.init>
<basil.configurationFile>${project.basedir}/target/dependency/${basil.configurationFile}</basil.configurationFile>
<test.db.init.script>${project.basedir}/target/dependency/db.sql</test.db.init.script>
<jar.executor.server.port>${http.port}</jar.executor.server.port>
<jar.executor.vm.options>${jar.executor.vm.options}</jar.executor.vm.options>
<jar.executor.jar.folder>${project.basedir}/target/dependency</jar.executor.jar.folder>
<jar.executor.jar.name.regexp>basil-server-.*.jar$</jar.executor.jar.name.regexp>
<jar.executor.workingdirectory>${project.basedir}</jar.executor.workingdirectory>
<keepJarRunning>${keepJarRunning}</keepJarRunning>
<server.ready.timeout.seconds>180</server.ready.timeout.seconds>
<server.ready.path.1>/basil:{</server.ready.path.1>
<server.ready.path.1>/basil:[</server.ready.path.1>
</systemPropertyVariables>
<excludes>

Expand Down Expand Up @@ -140,18 +174,10 @@
</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<!-- <dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore-osgi</artifactId>
<version>4.3</version>
<type>bundle</type>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-osgi</artifactId>
<version>4.3.1</version>
<type>bundle</type>
</dependency> -->
<!-- <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore-osgi</artifactId>
<version>4.3</version> <type>bundle</type> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-osgi</artifactId> <version>4.3.1</version> <type>bundle</type>
</dependency> -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
Expand All @@ -161,6 +187,24 @@
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>

</project>
Expand Down
116 changes: 114 additions & 2 deletions it/src/main/java/uk/ac/open/kmi/basil/it/BasilTestBase.java
@@ -1,11 +1,22 @@
package uk.ac.open.kmi.basil.it;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.TreeSet;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
Expand All @@ -18,14 +29,19 @@
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.shiro.config.Ini;
import org.apache.stanbol.commons.testing.http.RequestBuilder;
import org.apache.stanbol.commons.testing.http.RequestExecutor;
import org.apache.stanbol.commons.testing.jarexec.JarExecutor;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.init.ScriptUtils;

/**
*
Expand All @@ -43,8 +59,16 @@ public class BasilTestBase {
public static final String SERVER_READY_TIMEOUT_PROP = "server.ready.timeout.seconds";
public static final String SERVER_READY_PROP_PREFIX = "server.ready.path";
public static final String KEEP_JAR_RUNNING_PROP = "keepJarRunning";
public static final String BASIL_CONFIGURATION_FILE = "basil.configurationFile";
public static final String TEST_DB_INIT = "test.db.init";
public static final String TEST_DB_INIT_SCRIPT = "test.db.init.script";

protected static String serverBaseUrl;
protected static String dbInit = "false";
protected static String jdbcConnectionUrl = null;
protected static String databaseName = null;
protected static String user = null;
protected static String password = null;

private static final Logger log = LoggerFactory.getLogger(BasilTestBase.class);

Expand All @@ -64,7 +88,89 @@ protected String getCredentials() {
}

@BeforeClass
public static synchronized void startRunnableJar() throws Exception {
public static void initialize() throws Exception {
dbInit = System.getProperty(TEST_DB_INIT);
String conf = System.getProperty(BASIL_CONFIGURATION_FILE);
log.info("Configuration: {}", conf);
log.info("Init a test db: {}", dbInit);
if ("true".equals(dbInit)) {
log.debug("{} is true", TEST_DB_INIT);
conf = createTestDb();
}
startServer(conf);
}

@AfterClass
public static void cleanup() {
if(!dbInit.equals("true")){
return;
}
log.info("Cleaning up: dropping db {}", databaseName);
try {
// Delete the test Database
Class.forName("com.mysql.jdbc.Driver");
try (Connection conn = DriverManager.getConnection(jdbcConnectionUrl, user, password)) {
try (Statement create = conn.createStatement()) {
create.executeUpdate("DROP DATABASE `" + databaseName + "`");
log.info("Database {} dropped", databaseName);
}
}
} catch (Exception e) {
log.error("IGNORED (cannot cleanup test db!)", e);
}
}

/**
* Returns the location of the new configuration file
*
* @return
* @throws Exception
*/
private static String createTestDb() throws Exception {
log.info("Creating test db");
final String inputConfigurationFile = System.getProperty(BASIL_CONFIGURATION_FILE);
String str = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
final String dbPostfix = '_' + RandomStringUtils.random(8, str.toCharArray());
File f = new File(inputConfigurationFile);
if (!f.exists() || !f.canRead()) {
throw new Exception("Cannot use basil configuration file: " + f);
}
Ini ini = Ini.fromResourcePath(f.getAbsolutePath());
jdbcConnectionUrl = new StringBuilder().append("jdbc:mysql://").append(ini.get("").get("ds.serverName"))
.append(":").append(ini.get("").get("ds.port")).append("/").toString();
databaseName = ini.get("").get("ds.databaseName") + dbPostfix;
user = ini.get("").get("ds.user");
password = ini.get("").get("ds.password");

log.info("Attemp to init a test db {}", databaseName);

Class.forName("com.mysql.jdbc.Driver");
try (Connection conn = DriverManager.getConnection(jdbcConnectionUrl, user, password)) {
try (Statement create = conn.createStatement()) {
create.executeUpdate("CREATE DATABASE `" + databaseName + "`");
log.info("Database {} created", databaseName);
}
try (Statement create = conn.createStatement()) {
create.executeQuery("USE `" + databaseName + "`");
}
// Now init db
Resource r = new FileSystemResource(System.getProperty(TEST_DB_INIT_SCRIPT));
log.info("Running init script: {}", r);
ScriptUtils.executeSqlScript(conn, r);
}
String newConfigurationFile = inputConfigurationFile + dbPostfix;
log.info("Write configuration file for test server instance: {}", newConfigurationFile);
Path from = Paths.get(inputConfigurationFile);
Path to = Paths.get(newConfigurationFile);
Charset charset = StandardCharsets.UTF_8;
String content = new String(Files.readAllBytes(from), charset);
content = content.replaceAll(ini.get("").get("ds.databaseName"), databaseName);
Files.write(to, content.getBytes(charset));
// Configuration file
return newConfigurationFile;
}

public static synchronized void startServer(String configurationFile) throws Exception {
log.info("Starting testing server");
if (serverBaseUrl != null) {
// concurrent initialization by loading subclasses
Expand All @@ -81,7 +187,13 @@ public static synchronized void startRunnableJar() throws Exception {
}
log.info(TEST_SERVER_URL_PROP + " is set: not starting server jar (" + serverBaseUrl + ")");
} else {
final JarExecutor j = JarExecutor.getInstance(System.getProperties());

Properties properties = System.getProperties();
// Add jvm option for basil configuration file
String opts = properties.getProperty("jar.executor.vm.options");
opts += " -Dbasil.configurationFile=" + configurationFile;
properties.setProperty("jar.executor.vm.options", opts);
final JarExecutor j = JarExecutor.getInstance(properties);
j.start();
serverBaseUrl = "http://localhost:" + j.getServerPort();
log.info("Forked subprocess server listening to: " + serverBaseUrl);
Expand Down
22 changes: 22 additions & 0 deletions it/src/test/java/uk/ac/open/kmi/basil/it/AccountTest.java
@@ -0,0 +1,22 @@
package uk.ac.open.kmi.basil.it;

import org.junit.Rule;
import org.junit.rules.TestName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AccountTest {

private static final Logger log = LoggerFactory.getLogger(AccountTest.class);

@Rule
public TestName name = new TestName();

public void createAccount(){

}

public void deleteAccount(){
// Not implemented yet!
}
}
4 changes: 2 additions & 2 deletions it/src/test/java/uk/ac/open/kmi/basil/it/CollectionTest.java
Expand Up @@ -24,15 +24,15 @@ public void askAnyGetJson() throws ParseException, ClientProtocolException, IOEx
log.info("#{}", name.getMethodName());
executor.execute(builder.buildGetRequest("/basil").withHeader("Accept", "*/*"));
log.debug(" ... returned content: {}", executor.getContent());
executor.assertStatus(200).assertContentType("application/json").assertContentRegexp("\\{.*\\}");
executor.assertStatus(200).assertContentType("application/json").assertContentRegexp("\\[.*\\]");
}

@Test
public void askJsonGetJson() throws ParseException, ClientProtocolException, IOException {
log.info("#{}", name.getMethodName());
executor.execute(builder.buildGetRequest("/basil").withHeader("Accept", "application/json"));
log.debug(" ... returned content: {}", executor.getContent());
executor.assertStatus(200).assertContentType("application/json").assertContentRegexp("\\{.*\\}");
executor.assertStatus(200).assertContentType("application/json").assertContentRegexp("\\[.*\\]");
}

@Test
Expand Down
5 changes: 5 additions & 0 deletions it/src/test/java/uk/ac/open/kmi/basil/it/SpecTest.java
@@ -0,0 +1,5 @@
package uk.ac.open.kmi.basil.it;

public class SpecTest {

}
5 changes: 5 additions & 0 deletions parent/pom.xml
Expand Up @@ -208,6 +208,11 @@
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down

0 comments on commit 1ab5a01

Please sign in to comment.