Skip to content

Commit

Permalink
automated the process of creating the quickstart/distro h2 database
Browse files Browse the repository at this point in the history
  • Loading branch information
EricWittmann committed Dec 23, 2014
1 parent 3c5ddf1 commit f381f19
Show file tree
Hide file tree
Showing 26 changed files with 191 additions and 107 deletions.
48 changes: 48 additions & 0 deletions distro/db/pom.xml
@@ -0,0 +1,48 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.apiman</groupId>
<artifactId>apiman-distro</artifactId>
<version>1.0.1-SNAPSHOT</version>
</parent>
<artifactId>apiman-distro-db</artifactId>
<name>apiman-distro-db</name>

<dependencies>
<!-- Project Dependencies -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>apiman-test-common</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>apiman-manager-test-api</artifactId>
</dependency>

<!-- Third Party Dependencies -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,61 @@
/*
* Copyright 2014 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.apiman.distro.db;

import io.apiman.manager.test.util.AbstractTestPlanTest;

import java.io.File;

import org.junit.BeforeClass;
import org.junit.Test;

/**
* Unit test that creates an H2 database file by firing up the API Manager and sending a
* bunch of REST requests to configure it. When this test is complete there should be a
* valid H2 database located in target/classes (and thus be included in the JAR).
*
* @author eric.wittmann@redhat.com
*/
@SuppressWarnings("nls")
public class CreateH2DatabaseTest extends AbstractTestPlanTest {

@BeforeClass
public static void setup() throws Exception {
File targetClassesDir = new File("target/classes").getAbsoluteFile();
if (!targetClassesDir.exists()) {
targetClassesDir.mkdirs();
}

System.out.println("------------------------------------------------");
System.out.println("Setting H2 db output path: " + targetClassesDir.toString());
System.out.println("------------------------------------------------");

System.setProperty("apiman.test.h2-output-dir", targetClassesDir.toString());
AbstractTestPlanTest.setup();
}

@Test
public void test() {
try {
System.setProperty("apiman.suite.api-username", "admin");
System.setProperty("apiman.suite.api-password", "admin");
runTestPlan("scripts/api-manager-init-testPlan.xml", CreateH2DatabaseTest.class.getClassLoader());
} finally {
System.clearProperty("apiman.test.h2-output-dir");
}
}

}
@@ -0,0 +1,22 @@
POST /policyDefs ${apiman.suite.api-username}/${apiman.suite.api-password}
Content-Type: application/json

{
"name" : "Ignored Resources Policy",
"description" : "Requests satisfying the provided regular expression will be ignored.",
"policyImpl" : "class:io.apiman.gateway.engine.policies.IgnoredResourcesPolicy",
"icon" : "eye-slash",
"templates" : [
{
"language" : null,
"template" : "Requests matching any of the @{pathsToIgnore.size()} regular expressions provided will receive a 404 error code."
}
]
}
----
200
Content-Type: application/json

{
"id" : "IgnoredResourcesPolicy"
}
Expand Up @@ -15,6 +15,7 @@
<test name="Create Blacklist Policy Def">data/policyDefs/001_blacklist-policydef.resttest</test>
<test name="Create BASIC Authentication Policy Def">data/policyDefs/002_basicauth-policydef.resttest</test>
<test name="Create BASIC Authentication Policy Def">data/policyDefs/003_ratelimiting-policydef.resttest</test>
<test name="Create Ignored Resources Policy Def">data/policyDefs/04_ignoredresources-policydef.resttest</test>
</testGroup>

<!-- Gateways -->
Expand Down
1 change: 1 addition & 0 deletions distro/pom.xml
Expand Up @@ -12,6 +12,7 @@

<modules>
<module>data</module>
<module>db</module>
<module>wildfly8</module>
</modules>
</project>
6 changes: 4 additions & 2 deletions distro/wildfly8/pom.xml
Expand Up @@ -38,12 +38,14 @@
<artifactId>apiman-gateway-platforms-war-wildfly8-modules</artifactId>
<type>zip</type>
</dependency>

<!-- apiman data -->
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>apiman-distro-data</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>apiman-distro-db</artifactId>
</dependency>

<!-- apiman quickstarts -->
<dependency>
Expand Down
21 changes: 20 additions & 1 deletion distro/wildfly8/src/main/assembly/overlay.xml
Expand Up @@ -76,7 +76,26 @@
<fileMode>0755</fileMode>
<unpack>true</unpack>
</dependencySet>
<!-- Seed data -->
<!-- Pre-configured H2 database -->
<dependencySet>
<outputDirectory>standalone/data/h2</outputDirectory>
<includes>
<include>io.apiman:apiman-distro-db:jar</include>
</includes>
<useProjectArtifact>false</useProjectArtifact>
<useProjectAttachments>false</useProjectAttachments>
<useTransitiveDependencies>false</useTransitiveDependencies>
<useTransitiveFiltering>false</useTransitiveFiltering>
<directoryMode>0755</directoryMode>
<fileMode>0755</fileMode>
<unpack>true</unpack>
<unpackOptions>
<excludes>
<exclude>**/META-INF/**</exclude>
</excludes>
</unpackOptions>
</dependencySet>
<!-- Sample data -->
<dependencySet>
<outputDirectory>apiman</outputDirectory>
<includes>
Expand Down
Binary file not shown.

This file was deleted.

This file was deleted.

5 changes: 0 additions & 5 deletions gateway/platforms/vertx/pom.xml
Expand Up @@ -49,11 +49,6 @@
</dependency>

<!--Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>testtools</artifactId>
Expand Down
4 changes: 0 additions & 4 deletions gateway/test/pom.xml
Expand Up @@ -113,10 +113,6 @@
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>

</project>
Expand Up @@ -18,6 +18,7 @@
import io.apiman.common.servlet.AuthenticationFilter;
import io.apiman.manager.api.security.impl.DefaultSecurityContextFilter;

import java.io.File;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
Expand Down Expand Up @@ -110,7 +111,12 @@ protected void preStart() {
InitialContext ctx = new InitialContext();
ensureCtx(ctx, "java:/comp/env"); //$NON-NLS-1$
ensureCtx(ctx, "java:/comp/env/jdbc"); //$NON-NLS-1$
ds = createInMemoryDatasource();
String dbOutputPath = System.getProperty("apiman.test.h2-output-dir", null); //$NON-NLS-1$
if (dbOutputPath != null) {
ds = createFileDatasource(new File(dbOutputPath));
} else {
ds = createInMemoryDatasource();
}
ctx.bind("java:/comp/env/jdbc/ApiManagerDS", ds); //$NON-NLS-1$
} catch (Exception e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -148,6 +154,23 @@ private static BasicDataSource createInMemoryDatasource() throws SQLException {
return ds;
}

/**
* Creates an h2 file based datasource.
* @throws SQLException
*/
private static BasicDataSource createFileDatasource(File outputDirectory) throws SQLException {
System.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); //$NON-NLS-1$ //$NON-NLS-2$
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName(Driver.class.getName());
ds.setUsername("sa"); //$NON-NLS-1$
ds.setPassword(""); //$NON-NLS-1$
ds.setUrl("jdbc:h2:" + outputDirectory.toString() + "/apiman-manager-api;MVCC=true"); //$NON-NLS-1$ //$NON-NLS-2$
Connection connection = ds.getConnection();
connection.close();
System.out.println("DataSource created and bound to JNDI."); //$NON-NLS-1$
return ds;
}

/**
* Configure the web application(s).
* @param handlers
Expand Down
Expand Up @@ -37,6 +37,7 @@ public abstract class AbstractTestPlanTest {

@BeforeClass
public static void setup() throws Exception {
System.out.println("AbstractTestPlanTest::setup()");
if (!"true".equals(System.getProperty("apiman.junit.no-server", "false"))) {
testServer.start();
} else {
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Expand Up @@ -398,6 +398,11 @@
<artifactId>apiman-distro-data</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>apiman-distro-db</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>apiman-distro-wildfly8</artifactId>
Expand Down
4 changes: 4 additions & 0 deletions test/suite/pom.xml
Expand Up @@ -14,6 +14,10 @@
<groupId>${project.groupId}</groupId>
<artifactId>apiman-test-common</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>apiman-distro-db</artifactId>
</dependency>

<!-- Third Party Dependencies -->
<dependency>
Expand Down

0 comments on commit f381f19

Please sign in to comment.