1. Chempound
  2. Untitled project
  3. acceptance-tests

Commits

Sam Adams  committed 69e4fe5

Initial commit

  • Participants
  • Branches default

Comments (0)

Files changed (13)

File .hgignore

View file
+
+# maven
+^target/
+/target/
+
+# intellij
+^.idea/
+\.iml$
+
+# eclipse
+^\.classpath$
+/\.classpath$
+^\.project$
+/\.project$
+^\.settings/
+/\.settings/
+
+# temp files
+~$
+\.old$
+^temp/
+/temp/

File acceptance-test-driver/pom.xml

View file
+<?xml version="1.0" encoding="UTF-8"?>
+<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>net.chempound.acceptance</groupId>
+        <artifactId>acceptance</artifactId>
+        <version>0.1-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>acceptance-test-driver</artifactId>
+    <name>Chempound : Acceptance Test Driver</name>
+
+    <dependencies>
+
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>compile</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.hamcrest</groupId>
+            <artifactId>hamcrest-core</artifactId>
+            <version>1.2.1</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.seleniumhq.selenium</groupId>
+            <artifactId>selenium-htmlunit-driver</artifactId>
+            <version>2.19.0</version>
+        </dependency>
+        <dependency>
+            <groupId>org.seleniumhq.selenium</groupId>
+            <artifactId>selenium-firefox-driver</artifactId>
+            <version>2.19.0</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.httpcomponents</groupId>
+            <artifactId>httpclient</artifactId>
+            <version>4.1.2</version>
+        </dependency>
+
+        <dependency>
+            <groupId>net.chempound</groupId>
+            <artifactId>chempound-webapp</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>net.chempound</groupId>
+            <artifactId>chempound-client</artifactId>
+            <version>0.1-SNAPSHOT</version>
+        </dependency>
+
+    </dependencies>
+
+</project>

File acceptance-test-driver/src/main/java/net/chempound/acceptance/dsl/AbstractAcceptanceTest.java

View file
+package net.chempound.acceptance.dsl;
+
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.google.inject.Stage;
+import org.apache.commons.io.FileUtils;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.restlet.Application;
+import org.restlet.Component;
+import org.restlet.data.Protocol;
+
+import java.io.File;
+import java.util.Random;
+import java.util.UUID;
+
+/**
+ * @author Sam Adams
+ */
+public abstract class AbstractAcceptanceTest {
+
+    private static Component component;
+    private static File workspace;
+    private static int port;
+
+    protected final DriverFactory driverFactory;
+
+    protected AbstractAcceptanceTest() {
+        this(new SeleniumDriverFactory());
+    }
+
+    public AbstractAcceptanceTest(final DriverFactory driverFactory) {
+        this.driverFactory = driverFactory;
+    }
+
+    @After
+    public void tearDown() {
+        driverFactory.shutdown();
+    }
+
+    @BeforeClass
+    public static void startChempound() throws Exception {
+        port = 12345;
+
+        workspace = new File("target/ats/" + UUID.randomUUID());
+        FileUtils.forceMkdir(workspace);
+
+        final Injector injector = Guice.createInjector(Stage.DEVELOPMENT,
+            new ChempoundTestModule(port, workspace)
+        );
+
+        component = injector.getInstance(Component.class);
+        component.getServers().add(Protocol.HTTP, port)
+            .getContext().getParameters().add("maxThreads", "256");
+        component.getDefaultHost()
+            .attach("/chempound", injector.getInstance(Application.class));
+
+        component.start();
+    }
+
+    @AfterClass
+    public static void stopChempound() throws Exception {
+        try {
+            if (component != null) {
+                component.stop();
+                component = null;
+            }
+        } finally {
+            FileUtils.deleteQuietly(workspace);
+        }
+    }
+
+}

File acceptance-test-driver/src/main/java/net/chempound/acceptance/dsl/ChempoundTestModule.java

View file
+package net.chempound.acceptance.dsl;
+
+import net.chempound.ChempoundConfigurationModule;
+import net.chempound.DefaultChempoundModule;
+import net.chempound.config.DefaultChempoundConfiguration;
+import net.chempound.datastore.InMemoryJenaTripleStore;
+import net.chempound.datastore.TripleStore;
+import net.chempound.storage.InMemoryResourceStore;
+import net.chempound.storage.ResourceStore;
+import net.chempound.webapp.DefaultChempoundWebModule;
+
+import java.io.File;
+import java.net.URI;
+
+import static java.lang.String.format;
+
+/**
+ * @author Sam Adams
+ */
+public class ChempoundTestModule extends DefaultChempoundWebModule {
+
+    private final int port;
+    private final File workspace;
+
+    public ChempoundTestModule(final int port, final File workspace) {
+        this.port = port;
+        this.workspace = workspace;
+    }
+
+    @Override
+    protected void configure() {
+        final URI uri = URI.create(format("http://localhost:%d/chempound/", port));
+        
+        install(new ChempoundConfigurationModule(new DefaultChempoundConfiguration(uri, workspace)));
+        install(new DefaultChempoundModule());
+        super.configure();
+    }
+
+    @Override
+    protected void bindResourceStore() {
+        bind(ResourceStore.class).to(InMemoryResourceStore.class);
+    }
+
+    @Override
+    protected void bindTripleStore() {
+        bind(TripleStore.class).to(InMemoryJenaTripleStore.class);
+    }
+
+}

File acceptance-test-driver/src/main/java/net/chempound/acceptance/dsl/DriverFactory.java

View file
+package net.chempound.acceptance.dsl;
+
+import net.chempound.acceptance.dsl.collection.CollectionDriver;
+import net.chempound.acceptance.dsl.deposit.DepositDriver;
+
+/**
+ * @author Sam Adams
+ */
+public interface DriverFactory {
+
+    CollectionDriver createCollectionDriver();
+
+    DepositDriver createDepositDriver();
+
+    void shutdown();
+}

File acceptance-test-driver/src/main/java/net/chempound/acceptance/dsl/SeleniumDriverFactory.java

View file
+package net.chempound.acceptance.dsl;
+
+import com.gargoylesoftware.htmlunit.BrowserVersion;
+import net.chempound.acceptance.dsl.collection.CollectionDriver;
+import net.chempound.acceptance.dsl.deposit.DepositDriver;
+import org.openqa.selenium.WebDriver;
+import org.openqa.selenium.firefox.FirefoxDriver;
+import org.openqa.selenium.htmlunit.HtmlUnitDriver;
+
+/**
+ * @author Sam Adams
+ */
+public class SeleniumDriverFactory implements DriverFactory {
+
+    private final WebDriver webDriver;
+
+    public SeleniumDriverFactory() {
+        this.webDriver = createWebDriver();
+    }
+
+    protected static WebDriver createWebDriver() {
+        if ("firefox".equals(System.getProperty("chempound.acceptance.browser"))) {
+            return new FirefoxDriver();
+        }
+        return new HtmlUnitDriver(BrowserVersion.INTERNET_EXPLORER_8);
+    }
+
+    @Override
+    public CollectionDriver createCollectionDriver() {
+        return new CollectionDriver(webDriver);
+    }
+
+    @Override
+    public DepositDriver createDepositDriver() {
+        return new DepositDriver(webDriver);
+    }
+
+    @Override
+    public void shutdown() {
+        webDriver.close();
+    }
+}

File acceptance-test-driver/src/main/java/net/chempound/acceptance/dsl/collection/Collection.java

View file
+package net.chempound.acceptance.dsl.collection;
+
+import java.net.URI;
+
+/**
+ * @author Sam Adams
+ */
+public class Collection {
+    
+    private final URI uri;
+    private final String title;
+
+    public Collection(final URI uri, final String title) {
+        this.uri = uri;
+        this.title = title;
+    }
+
+    public URI getUri() {
+        return uri;
+    }
+
+    public String getTitle() {
+        return title;
+    }
+
+}

File acceptance-test-driver/src/main/java/net/chempound/acceptance/dsl/collection/CollectionDriver.java

View file
+package net.chempound.acceptance.dsl.collection;
+
+import net.chempound.client.ChempoundClient;
+import net.chempound.client.CreateCollectionRequest;
+import net.chempound.client.CreateCollectionResponse;
+import org.hamcrest.Matcher;
+import org.openqa.selenium.By;
+import org.openqa.selenium.WebDriver;
+import org.openqa.selenium.WebElement;
+
+import java.net.URI;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+
+import static org.junit.Assert.assertThat;
+
+/**
+ * @author Sam Adams
+ */
+public class CollectionDriver {
+    
+    private final WebDriver webDriver;
+    private final ChempoundClient client;
+
+    private final Map<String, String> actuals = new HashMap<String, String>();
+    
+    public CollectionDriver(final WebDriver webDriver) {
+        this.webDriver = webDriver;
+        this.client = new ChempoundClient();
+    }
+    
+    protected void loadPage(final Collection collection) {
+        webDriver.navigate().to(collection.getUri().toString());
+    }
+
+
+    public CreateCollection createCollection() {
+        return new CreateCollection(this);
+    }
+
+    public void check(final Collection collection, final Matcher<CollectionDriver>... matchers) {
+        loadPage(collection);
+        
+        webDriver.findElement(By.cssSelector(".collection"));
+        
+        for (final Matcher<CollectionDriver> matcher : matchers) {
+            assertThat(this, matcher);
+        }
+    }
+    
+
+    Collection createCollection(final CreateCollection config) {
+        final CreateCollectionRequest request = new CreateCollectionRequest()
+            .withUri(config.parent != null ? config.parent : URI.create("http://localhost:12345/chempound/sword/collection/"))
+            .withTitle(actual(config.title))
+            .withSlug(actual(config.slug));
+
+        final CreateCollectionResponse response;
+        try {
+            response = client.createCollection(request);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+
+        return new Collection(response.getUri(), response.getTitle());
+    }
+
+    boolean matchesTitle(final String title) {
+        final WebElement element = webDriver.findElement(By.cssSelector(".collection h1.title"));
+        return actual(title).equals(element.getText());
+    }
+
+    boolean hasSlug(final String slug) {
+        return webDriver.getCurrentUrl().contains('/' + actual(slug) + '/');
+    }
+
+    boolean containsChild(final Collection child) {
+        final List<WebElement> elements = webDriver.findElements(By.cssSelector(".collection .result"));
+        for (final WebElement element : elements) {
+            final WebElement title = element.findElement(By.tagName("h2"));
+            final WebElement link = title.findElement(By.tagName("a"));
+            if (child.getUri().toString().equals(link.getAttribute("href"))) {
+                if (child.getTitle() == null || child.getTitle().equals(title.getText())) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+    
+    private String actual(final String key) {
+        if (key == null) {
+            return null;
+        }
+        if (actuals.containsKey(key)) {
+            return actuals.get(key);
+        }
+        final String actual = generate(key);
+        actuals.put(key, actual);
+        return actual;
+    }
+
+    protected static String generate(final String prefix) {
+        final Random random = new Random();
+        final StringBuilder text = new StringBuilder()
+            .append(prefix).append("--");
+        for (int i = 0; i < 10; i++) {
+            text.append((char) (65 + random.nextInt(26)));
+        }
+        return text.toString();
+    }
+
+}

File acceptance-test-driver/src/main/java/net/chempound/acceptance/dsl/collection/CollectionMatchers.java

View file
+package net.chempound.acceptance.dsl.collection;
+
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
+
+/**
+ * @author Sam Adams
+ */
+public class CollectionMatchers {
+    
+    public static Matcher<CollectionDriver> hasTitle(final String title) {
+        return new TypeSafeMatcher<CollectionDriver>() {
+
+            @Override
+            protected boolean matchesSafely(final CollectionDriver collection) {
+                return collection.matchesTitle(title);
+            }
+
+            @Override
+            public void describeTo(final Description description) {
+                description.appendText("title matches '" + title + "'");
+            }
+        };
+    }
+    
+    public static Matcher<CollectionDriver> containsItem(final Collection child) {
+        return new TypeSafeMatcher<CollectionDriver>() {
+            @Override
+            protected boolean matchesSafely(final CollectionDriver collection) {
+                return collection.containsChild(child);
+            }
+
+            @Override
+            public void describeTo(final Description description) {
+                description.appendText("collection contains '" + child.getUri() + "'");
+            }
+        };
+    }
+
+    public static Matcher<CollectionDriver> hasSlug(final String slug) {
+        return new TypeSafeMatcher<CollectionDriver>() {
+            @Override
+            protected boolean matchesSafely(final CollectionDriver collection) {
+                return collection.hasSlug(slug);
+            }
+
+            @Override
+            public void describeTo(final Description description) {
+                description.appendText("slug matches '" + slug + "'");
+            }
+        };
+    }
+}

File acceptance-test-driver/src/main/java/net/chempound/acceptance/dsl/collection/CreateCollection.java

View file
+package net.chempound.acceptance.dsl.collection;
+
+import java.net.URI;
+
+/**
+ * @author Sam Adams
+ */
+public class CreateCollection {
+
+    private final CollectionDriver driver;
+
+    URI parent;
+    String title;
+    String slug;
+
+    CreateCollection(final CollectionDriver driver) {
+        this.driver = driver;
+    }
+
+    public CreateCollection withParent(final Collection parent) {
+        this.parent = parent.getUri();
+        return this;
+    }
+
+    public CreateCollection withTitle(final String title) {
+        this.title = title;
+        return this;
+    }
+
+    public CreateCollection withSlug(final String slug) {
+        this.slug = slug;
+        return this;
+    }
+
+    public Collection execute() {
+        return driver.createCollection(this);
+    }
+
+}

File acceptance-tests/pom.xml

View file
+<?xml version="1.0" encoding="UTF-8"?>
+<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>net.chempound.acceptance</groupId>
+        <artifactId>acceptance</artifactId>
+        <version>0.1-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>acceptance-tests</artifactId>
+    <name>Chempound : Acceptance Tests</name>
+
+    <dependencies>
+
+        <dependency>
+            <groupId>net.chempound.acceptance</groupId>
+            <artifactId>acceptance-test-driver</artifactId>
+            <version>0.1-SNAPSHOT</version>
+            <scope>test</scope>
+        </dependency>
+
+    </dependencies>
+
+    <build>
+        <plugins>
+
+            <plugin>
+                <groupId>org.mortbay.jetty</groupId>
+                <artifactId>maven-jetty-plugin</artifactId>
+                <version>6.1.26</version>
+                <configuration>
+                    <contextPath>/</contextPath>
+                    <scanIntervalSeconds>5</scanIntervalSeconds>
+                </configuration>
+            </plugin>
+
+            <plugin>
+                <groupId>org.codehaus.mojo</groupId>
+                <artifactId>selenium-maven-plugin</artifactId>
+                <version>2.2</version>
+                <executions>
+                    <execution>
+                        <id>setup</id>
+                        <phase>pre-integration-test</phase>
+                        <goals>
+                            <goal>start-server</goal>
+                        </goals>
+                        <configuration>
+                            <background>true</background>
+                        </configuration>
+                    </execution>
+                    <execution>
+                        <id>teardown</id>
+                        <phase>post-integration-test</phase>
+                        <goals>
+                            <goal>stop-server</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+
+            <plugin>
+                <artifactId>maven-failsafe-plugin</artifactId>
+            </plugin>
+
+        </plugins>
+    </build>
+
+</project>

File acceptance-tests/src/test/java/net/chempound/acceptance/CollectionAcceptanceTest.java

View file
+package net.chempound.acceptance;
+
+import net.chempound.acceptance.dsl.AbstractAcceptanceTest;
+import net.chempound.acceptance.dsl.collection.Collection;
+import net.chempound.acceptance.dsl.collection.CollectionDriver;
+import org.junit.Before;
+import org.junit.Test;
+
+import static net.chempound.acceptance.dsl.collection.CollectionMatchers.*;
+
+/**
+ * @author Sam Adams
+ */
+public class CollectionAcceptanceTest extends AbstractAcceptanceTest {
+
+    private CollectionDriver collectionDriver;
+
+    @Before
+    public void setUp() throws Exception {
+        collectionDriver = driverFactory.createCollectionDriver();
+    }
+
+    @Test
+    public void shouldCreateCollectionWithTitle() throws Exception {
+        Collection collection = collectionDriver.createCollection()
+            .withTitle("title")
+            .execute();
+
+        collectionDriver.check(collection, hasTitle("title"));
+    }
+
+    @Test
+    public void shouldCreateCollectionWithSlug() throws Exception {
+        Collection collection = collectionDriver.createCollection()
+            .withSlug("slug")
+            .withTitle("title")
+            .execute();
+
+        collectionDriver.check(collection, hasTitle("title"), hasSlug("slug"));
+    }
+
+    @Test
+    public void shouldLinkFromParentToChildCollection() throws Exception {
+        Collection parent = collectionDriver.createCollection()
+            .execute();
+        
+        Collection child = collectionDriver.createCollection()
+            .withParent(parent)
+            .withTitle("child-title")
+            .execute();
+
+        collectionDriver.check(parent, containsItem(child));
+    }
+
+}

File pom.xml

View file
+<?xml version="1.0" encoding="UTF-8"?>
+<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>net.chempound</groupId>
+        <artifactId>chempound-parent</artifactId>
+        <version>1-SNAPSHOT</version>
+    </parent>
+
+    <groupId>net.chempound.acceptance</groupId>
+    <artifactId>acceptance</artifactId>
+    <version>0.1-SNAPSHOT</version>
+    <packaging>pom</packaging>
+
+    <name>Chempound :: Acceptance</name>
+
+    <modules>
+        <module>acceptance-test-driver</module>
+        <module>acceptance-tests</module>
+    </modules>
+
+    <dependencyManagement>
+        <dependencies>
+
+            <dependency>
+                <groupId>net.chempound</groupId>
+                <artifactId>chempound-api</artifactId>
+                <version>${project.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>net.chempound</groupId>
+                <artifactId>chempound-app</artifactId>
+                <version>${project.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>net.chempound</groupId>
+                <artifactId>chempound-webapi</artifactId>
+                <version>${project.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>net.chempound</groupId>
+                <artifactId>chempound-webapp</artifactId>
+                <version>${project.version}</version>
+            </dependency>
+
+            <dependency>
+                <groupId>net.chempound</groupId>
+                <artifactId>chempound-rdf-vocabs</artifactId>
+                <version>${project.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>net.chempound</groupId>
+                <artifactId>chempound-rdf-cml</artifactId>
+                <version>${project.version}</version>
+            </dependency>
+
+            <dependency>
+                <groupId>org.swordapp</groupId>
+                <artifactId>server</artifactId>
+                <version>2.0-r339</version>
+            </dependency>
+            <dependency>
+                <groupId>org.swordapp</groupId>
+                <artifactId>sword-client</artifactId>
+                <version>2.0-r339</version>
+            </dependency>
+
+            <dependency>
+                <groupId>org.freemarker</groupId>
+                <artifactId>freemarker</artifactId>
+                <version>2.3.18</version>
+            </dependency>
+
+            <dependency>
+                <groupId>org.restlet.jee</groupId>
+                <artifactId>org.restlet.ext.servlet</artifactId>
+                <version>${restlet.version}</version>
+            </dependency>
+            <dependency>
+                <groupId>org.restlet.jee</groupId>
+                <artifactId>org.restlet.ext.httpclient</artifactId>
+                <version>${restlet.version}</version>
+            </dependency>
+
+        </dependencies>
+
+    </dependencyManagement>
+
+    <repositories>
+        <repository>
+            <id>ucc-repo</id>
+            <url>http://maven.ch.cam.ac.uk/m2repo</url>
+        </repository>
+    </repositories>
+
+</project>