diff --git a/basil/pom.xml b/basil/pom.xml index e5746e4..993f68a 100644 --- a/basil/pom.xml +++ b/basil/pom.xml @@ -150,7 +150,6 @@ mysql mysql-connector-java - 5.1.6 diff --git a/it/pom.xml b/it/pom.xml index 1fbea2c..1bccdda 100644 --- a/it/pom.xml +++ b/it/pom.xml @@ -19,7 +19,13 @@ - ../basil.ini + basil.ini + + + true @@ -29,9 +35,9 @@ src/test/resources/log4j2.xml - + - -Xmx1g -Dbasil.configurationFile=${basil.configurationFile} -Dlog4j.configurationFile=${log4j.configurationFile} + -Xmx1g -Dlog4j.configurationFile=${log4j.configurationFile} @@ -70,6 +76,31 @@ + + maven-resources-plugin + 2.7 + + + copy-resources + + generate-test-resources + + copy-resources + + + ${project.basedir}/target/dependency + + + ${project.basedir}/.. + db.sql + ${basil.configurationFile} + + + + + + + org.codehaus.mojo @@ -95,6 +126,9 @@ ${test.server.url} + ${test.db.init} + ${project.basedir}/target/dependency/${basil.configurationFile} + ${project.basedir}/target/dependency/db.sql ${http.port} ${jar.executor.vm.options} ${project.basedir}/target/dependency @@ -102,7 +136,7 @@ ${project.basedir} ${keepJarRunning} 180 - /basil:{ + /basil:[ @@ -140,18 +174,10 @@ 1.0.0-SNAPSHOT - + org.apache.logging.log4j log4j-slf4j-impl @@ -161,6 +187,24 @@ org.apache.logging.log4j log4j-core + + org.apache.shiro + shiro-core + + + org.apache.commons + commons-lang3 + 3.4 + + + org.springframework + spring-jdbc + 4.2.4.RELEASE + + + mysql + mysql-connector-java + diff --git a/it/src/main/java/uk/ac/open/kmi/basil/it/BasilTestBase.java b/it/src/main/java/uk/ac/open/kmi/basil/it/BasilTestBase.java index cb5446b..c84c660 100644 --- a/it/src/main/java/uk/ac/open/kmi/basil/it/BasilTestBase.java +++ b/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; @@ -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; /** * @@ -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); @@ -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 @@ -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); diff --git a/it/src/test/java/uk/ac/open/kmi/basil/it/AccountTest.java b/it/src/test/java/uk/ac/open/kmi/basil/it/AccountTest.java new file mode 100644 index 0000000..a782aad --- /dev/null +++ b/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! + } +} diff --git a/it/src/test/java/uk/ac/open/kmi/basil/it/CollectionTest.java b/it/src/test/java/uk/ac/open/kmi/basil/it/CollectionTest.java index 661e5e5..5967628 100644 --- a/it/src/test/java/uk/ac/open/kmi/basil/it/CollectionTest.java +++ b/it/src/test/java/uk/ac/open/kmi/basil/it/CollectionTest.java @@ -24,7 +24,7 @@ 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 @@ -32,7 +32,7 @@ public void askJsonGetJson() throws ParseException, ClientProtocolException, IOE 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 diff --git a/it/src/test/java/uk/ac/open/kmi/basil/it/SpecTest.java b/it/src/test/java/uk/ac/open/kmi/basil/it/SpecTest.java new file mode 100644 index 0000000..b57a7da --- /dev/null +++ b/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 { + +} diff --git a/parent/pom.xml b/parent/pom.xml index 598f199..4174709 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -208,6 +208,11 @@ jsr311-api 1.1.1 + + mysql + mysql-connector-java + 5.1.6 +