Skip to content

Commit

Permalink
Add better IT tests:
Browse files Browse the repository at this point in the history
- for binary content
- for containers
- with an external triplestore
- with solr

Rename Namespaces class so as not to conflict with the
  o.a.c.builder.xml.Namespaces class

Resolves: https://jira.duraspace.org/browse/FCREPO-1269
  • Loading branch information
acoburn authored and Andrew Woods committed Jan 6, 2015
1 parent 66848d2 commit a361ca1
Show file tree
Hide file tree
Showing 76 changed files with 15,216 additions and 417 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -3,7 +3,7 @@ Fcrepo Component

The **fcrepo:** component provides access to an external
[Fedora4](http://fcrepo.org) Object
[API](https://wiki.duraspace.org/display/FF/RESTful+HTTP+API+-+Objects)
[API](https://wiki.duraspace.org/display/FF/RESTful+HTTP+API+-+Containers)
for use with [Apache Camel](https://camel.apache.org).

[![Build Status](https://travis-ci.org/fcrepo4-labs/fcrepo-camel.png?branch=master)](https://travis-ci.org/fcrepo4-labs/fcrepo-camel)
Expand Down
96 changes: 94 additions & 2 deletions pom.xml
Expand Up @@ -36,6 +36,7 @@
<jersey.version>2.13</jersey.version>
<mockito.version>1.10.8</mockito.version>
<slf4j.version>1.7.9</slf4j.version>
<solr.version>4.10.2</solr.version>
<javax.ws.rs.version>2.0.1</javax.ws.rs.version>
<spring.version>4.0.7.RELEASE</spring.version>
<clerezza.rdf.jena.serializer.version>0.11</clerezza.rdf.jena.serializer.version>
Expand Down Expand Up @@ -281,6 +282,7 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down Expand Up @@ -397,10 +399,97 @@
</plugins>
</pluginManagement>


<defaultGoal>install</defaultGoal>

<plugins>
<!-- Launch jetty for integration testing with Solr -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.6.v20141205</version>
<configuration>
<systemProperties>
<systemProperty>
<name>solr.solr.home</name>
<value>${project.build.directory}/test-classes/solr</value>
</systemProperty>
</systemProperties>
<scanIntervalSeconds>10</scanIntervalSeconds>
<stopKey>STOP</stopKey>
<stopPort>${jetty.port.stop}</stopPort>
<daemon>true</daemon>
</configuration>
<dependencies>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
<configuration>
<httpConnector>
<port>${test.solr.port}</port>
</httpConnector>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
<contextHandlers>
<contextHandler implementation="org.eclipse.jetty.webapp.WebAppContext">
<war>${project.build.directory}/solr.war</war>
<contextPath>/solr</contextPath>
</contextHandler>
</contextHandlers>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>

<!--Bring in solr webapp for integration testing-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.apache.solr</groupId>
<artifactId>solr</artifactId>
<version>${solr.version}</version>
<type>war</type>
<outputDirectory>${project.build.directory}</outputDirectory>
<destFileName>solr.war</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
Expand All @@ -409,12 +498,14 @@
<portNames>
<portName>test.port</portName>
<portName>test.fuseki.port</portName>
<portName>test.solr.port</portName>
<portName>jetty.port.stop</portName>
</portNames>
</configuration>
<executions>
<execution>
<id>reserve-port</id>
<phase>pre-integration-test</phase>
<phase>package</phase>
<goals>
<goal>reserve-network-port</goal>
</goals>
Expand All @@ -431,6 +522,7 @@
<systemPropertyVariables>
<test.port>${test.port}</test.port>
<test.fuseki.port>${test.fuseki.port}</test.fuseki.port>
<test.solr.port>${test.solr.port}</test.solr.port>
</systemPropertyVariables>
</configuration>
<executions>
Expand Down
Expand Up @@ -19,7 +19,7 @@
/**
* @author acoburn
*/
public final class Namespaces {
public final class RdfNamespaces {

public static final String REPOSITORY = "http://fedora.info/definitions/v4/repository#";

Expand All @@ -29,7 +29,7 @@ public final class Namespaces {

public static final String LDP = "http://www.w3.org/ns/ldp#";

private Namespaces() {
private RdfNamespaces() {
// Prevent instantiation
}

Expand Down
16 changes: 12 additions & 4 deletions src/main/java/org/fcrepo/camel/processor/ProcessorUtils.java
Expand Up @@ -35,6 +35,14 @@ public final class ProcessorUtils {
private ProcessorUtils() {
}

private static String trimTrailingSlash(final String path) {
String trimmed = path;
while (trimmed.endsWith("/")) {
trimmed = trimmed.substring(0, trimmed.length() - 1);
}
return trimmed;
}

/**
* Extract the subject URI from the incoming message headers.
* @param in the incoming Message
Expand All @@ -43,17 +51,17 @@ public static String getSubjectUri(final Message in) throws IOException {
final StringBuilder base = new StringBuilder("");

if (in.getHeader(FcrepoHeaders.FCREPO_BASE_URL) != null) {
base.append(in.getHeader(FcrepoHeaders.FCREPO_BASE_URL, String.class));
base.append(trimTrailingSlash(in.getHeader(FcrepoHeaders.FCREPO_BASE_URL, String.class)));
} else if (in.getHeader(JmsHeaders.BASE_URL) != null) {
base.append(in.getHeader(JmsHeaders.BASE_URL, String.class));
base.append(trimTrailingSlash(in.getHeader(JmsHeaders.BASE_URL, String.class)));
} else {
throw new IOException("No baseURL header available!");
}

if (in.getHeader(FcrepoHeaders.FCREPO_IDENTIFIER) != null) {
base.append(in.getHeader(FcrepoHeaders.FCREPO_IDENTIFIER));
base.append(in.getHeader(FcrepoHeaders.FCREPO_IDENTIFIER, String.class));
} else if (in.getHeader(JmsHeaders.IDENTIFIER) != null) {
base.append(in.getHeader(JmsHeaders.IDENTIFIER));
base.append(in.getHeader(JmsHeaders.IDENTIFIER, String.class));
}
return base.toString();
}
Expand Down
Expand Up @@ -40,7 +40,24 @@ public void process(final Exchange exchange) throws IOException {
final Message in = exchange.getIn();
final String subject = ProcessorUtils.getSubjectUri(in);

in.setBody("DELETE WHERE { <" + subject + "> ?p ?o }");
/*
* N.B. The Sparql update command below deletes all triples with
* the defined subject uri (coming from the FCREPO_IDENTIFIER
* and FCREPO_BASE_URL headers). It also deletes triples that
* have a subject corresponding to that Fcrepo URI plus the
* "/fcr:export?format=jcr/xml" string appended to it. This
* makes it possible to more completely remove any triples
* for a given resource that were added earlier. If fcrepo
* ever stops producing triples that are appended with
* /fcr:export?format..., then that extra line can be removed.
* It would also be possible to recursively delete triples
* (by removing any triple whose subject is also an object of
* the starting (or context) URI, but that approach tends to delete
* too many triples from the triplestore. This command does
* not remove blank nodes.
*/
in.setBody("DELETE WHERE { <" + subject + "> ?p ?o };\n" +
"DELETE WHERE { <" + subject + "/fcr:export?format=jcr/xml> ?p ?o }");
in.setHeader(HTTP_METHOD, "POST");
in.setHeader(CONTENT_TYPE, "application/sparql-update");

Expand Down
Expand Up @@ -57,7 +57,24 @@ public void process(final Exchange exchange) throws IOException {
"application/n-triples".equals(contentType) ? "text/rdf+nt" : contentType, null);
serializer.serialize(serializedGraph, graph.getGraph(), "text/rdf+nt");

/*
* Before inserting updated triples, the Sparql update command
* below deletes all triples with the defined subject uri
* (coming from the FCREPO_IDENTIFIER and FCREPO_BASE_URL headers).
* It also deletes triples that have a subject corresponding to
* that Fcrepo URI plus the "/fcr:export?format=jcr/xml" string
* appended to it. This makes it possible to more completely
* remove any triples for a given resource that were added
* earlier. If fcrepo ever stops producing triples that are
* appended with /fcr:export?format..., then that extra line
* can be removed. It would also be possible to recursively delete
* triples (by removing any triple whose subject is also an object
* of the starting (or context) URI, but that approach tends to
* delete too many triples from the triplestore. This command does
* not delete blank nodes.
*/
exchange.getIn().setBody("DELETE WHERE { <" + subject + "> ?p ?o };\n" +
"DELETE WHERE { <" + subject + "/fcr:export?format=jcr/xml> ?p ?o };\n" +
"INSERT { " + serializedGraph.toString("UTF-8") + " }\n" +
"WHERE { }");
exchange.getIn().setHeader(HTTP_METHOD, "POST");
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/org/fcrepo/camel/FcrepoConstantsTest.java
Expand Up @@ -51,10 +51,10 @@ public void testFcrepoHeaders() {

@Test
public void testRdfLexicon() {
assertEquals(Namespaces.REPOSITORY, "http://fedora.info/definitions/v4/repository#");
assertEquals(Namespaces.INDEXING, "http://fedora.info/definitions/v4/indexing#");
assertEquals(Namespaces.RDF, "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
assertEquals(Namespaces.LDP, "http://www.w3.org/ns/ldp#");
assertEquals(RdfNamespaces.REPOSITORY, "http://fedora.info/definitions/v4/repository#");
assertEquals(RdfNamespaces.INDEXING, "http://fedora.info/definitions/v4/indexing#");
assertEquals(RdfNamespaces.RDF, "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
assertEquals(RdfNamespaces.LDP, "http://www.w3.org/ns/ldp#");
}

}
Expand Up @@ -83,7 +83,8 @@ public void testDelete() throws IOException, InterruptedException {

// Assertions
resultEndpoint.expectedBodiesReceived(
"DELETE WHERE { <" + base + path + "> ?p ?o }");
"DELETE WHERE { <" + base + path + "> ?p ?o };\n" +
"DELETE WHERE { <" + base + path + "/fcr:export?format=jcr/xml> ?p ?o }");
resultEndpoint.expectedHeaderReceived(Exchange.CONTENT_TYPE, "application/sparql-update");
resultEndpoint.expectedHeaderReceived(Exchange.HTTP_METHOD, "POST");

Expand Down
12 changes: 7 additions & 5 deletions src/test/java/org/fcrepo/camel/SparqlUpdateProcessorTest.java
Expand Up @@ -61,21 +61,23 @@ public void testDescribe() throws IOException, InterruptedException {
reverse(lines);

// Assertions
resultEndpoint.expectedBodiesReceived("DELETE WHERE { <" + base + path + "> ?p ?o }; " +
"INSERT { " + join(lines, " ") + " } " +
"WHERE { }");
resultEndpoint.expectedBodiesReceived(
"DELETE WHERE { <" + base + path + "> ?p ?o }; " +
"DELETE WHERE { <" + base + path + "/fcr:export?format=jcr/xml> ?p ?o }; " +
"INSERT { " + join(lines, " ") + " } " +
"WHERE { }");
resultEndpoint.expectedHeaderReceived("Content-Type", "application/sparql-update");
resultEndpoint.expectedHeaderReceived(Exchange.HTTP_METHOD, "POST");

// Test
final Map<String, Object> headers = new HashMap<>();
headers.put(FcrepoHeaders.FCREPO_BASE_URL, base);
headers.put(FcrepoHeaders.FCREPO_BASE_URL, base + "/");
headers.put(FcrepoHeaders.FCREPO_IDENTIFIER, path);
headers.put(Exchange.CONTENT_TYPE, "application/n-triples");
template.sendBodyAndHeaders(document, headers);

headers.clear();
headers.put(JmsHeaders.BASE_URL, base);
headers.put(JmsHeaders.BASE_URL, base + "/");
headers.put(JmsHeaders.IDENTIFIER, path);
headers.put(Exchange.CONTENT_TYPE, "text/turtle");
template.sendBodyAndHeaders(getTurtleDocument(), headers);
Expand Down

0 comments on commit a361ca1

Please sign in to comment.