Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #579 from fcrepo4/integration-rdf
Add integration tests for real-world RDF statements
  • Loading branch information
Andrew Woods committed Oct 23, 2014
2 parents 8343c26 + 51252d3 commit 5dd9a1a
Show file tree
Hide file tree
Showing 15 changed files with 694 additions and 11 deletions.
Expand Up @@ -21,6 +21,7 @@
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.vocabulary.RDF;
import org.apache.jena.riot.Lang;
import org.fcrepo.http.commons.api.rdf.HttpTripleUtil;
import org.fcrepo.http.commons.domain.Prefer;
Expand Down Expand Up @@ -77,6 +78,7 @@
import java.util.Iterator;

import static com.google.common.base.Predicates.alwaysTrue;
import static com.google.common.base.Predicates.and;
import static com.google.common.base.Predicates.not;
import static com.google.common.collect.Iterators.concat;
import static com.google.common.collect.Iterators.filter;
Expand Down Expand Up @@ -186,23 +188,19 @@ protected RdfStream getResourceTriples(final Prefer prefer) {
if (ldpPreferences.prefersServerManaged()) {
tripleFilter = alwaysTrue();
} else {
tripleFilter = not(ManagedRdf.isManagedTriple);
}

rdfStream.concat(filter(getTriples(PropertiesRdfContext.class), tripleFilter));


if (ldpPreferences.prefersServerManaged()) {
rdfStream.concat(getTriples(TypeRdfContext.class));
} else {
rdfStream.concat(filter(getTriples(TypeRdfContext.class), new Predicate<Triple>() {
tripleFilter = and(not(ManagedRdf.isManagedTriple), not(new Predicate<Triple>() {
@Override
public boolean apply(final Triple input) {
return !isManagedNamespace.apply(input.getObject().getNameSpace());
return input.getPredicate().equals(RDF.type.asNode())
&& isManagedNamespace.apply(input.getObject().getNameSpace());
}
}));
}

rdfStream.concat(filter(getTriples(PropertiesRdfContext.class), tripleFilter));

rdfStream.concat(filter(getTriples(TypeRdfContext.class), tripleFilter));


if (httpTripleUtil != null && ldpPreferences.prefersServerManaged()) {
httpTripleUtil.addHttpComponentModelsForResourceToStream(rdfStream, resource(), uriInfo, translator());
Expand Down
31 changes: 31 additions & 0 deletions fcrepo-integration-ldp/pom.xml
Expand Up @@ -199,4 +199,35 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<configuration>
<portNames>
<portName>test.port</portName>
<!-- reserves the stop port for jetty-maven-plugin -->
<portName>jetty.port.stop</portName>
<portName>jms.port</portName>
<portName>stomp.port</portName>
</portNames>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<test.port>${test.port}</test.port>
<test.context.path>${test.context.path}</test.context.path>
<jms.port>${jms.port}</jms.port>
<stomp.port>${stomp.port}</stomp.port>
<integration-test>true</integration-test>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>


</project>
146 changes: 146 additions & 0 deletions fcrepo-integration-rdf/pom.xml
@@ -0,0 +1,146 @@
<?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">
<parent>
<artifactId>fcrepo</artifactId>
<groupId>org.fcrepo</groupId>
<version>4.0.0-beta-04-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<name>Fedora Repository RDF integration tests Module</name>
<description>Fedora RDF integration scenarios</description>
<artifactId>fcrepo-integration-rdf</artifactId>


<dependencies>
<!-- test gear -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.fcrepo</groupId>
<artifactId>fcrepo-configs</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.fcrepo</groupId>
<artifactId>fcrepo-http-api</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.fcrepo</groupId>
<artifactId>fcrepo-http-api</artifactId>
<version>${project.version}</version>
<scope>test</scope>
<type>test-jar</type>
</dependency>

<dependency>
<groupId>org.fcrepo</groupId>
<artifactId>fcrepo-http-commons</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.fcrepo</groupId>
<artifactId>fcrepo-auth-common</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>


<dependency>
<groupId>org.fcrepo</groupId>
<artifactId>fcrepo-http-commons</artifactId>
<version>${project.version}</version>
<scope>test</scope>
<type>test-jar</type>
</dependency>

<dependency>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-http-server</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-http-servlet</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<scope>test</scope>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<configuration>
<portNames>
<portName>test.port</portName>
<!-- reserves the stop port for jetty-maven-plugin -->
<portName>jetty.port.stop</portName>
<portName>jms.port</portName>
<portName>stomp.port</portName>
</portNames>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<test.port>${test.port}</test.port>
<test.context.path>${test.context.path}</test.context.path>
<jms.port>${jms.port}</jms.port>
<stomp.port>${stomp.port}</stomp.port>
<integration-test>true</integration-test>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>

</project>
@@ -0,0 +1,132 @@
/**
* Copyright 2014 DuraSpace, 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 org.fcrepo.integration.rdf;

import com.hp.hpl.jena.graph.Graph;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.NodeFactory;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.sparql.graph.GraphFactory;
import com.hp.hpl.jena.update.GraphStore;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.BasicHttpEntity;
import org.fcrepo.integration.http.api.AbstractResourceIT;

import javax.ws.rs.core.Response;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static javax.ws.rs.core.Response.Status.CREATED;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* @author cabeer
*/
public abstract class AbstractIntegrationRdfIT extends AbstractResourceIT {

protected HttpResponse createLDPRSAndCheckResponse(final String pid, final String body) {
try {
final HttpPut httpPut = new HttpPut(serverAddress + pid);
httpPut.addHeader("Slug", pid);
httpPut.addHeader("Content-Type", "text/turtle");
final BasicHttpEntity e = new BasicHttpEntity();
e.setContent(IOUtils.toInputStream(body));
httpPut.setEntity(e);
final HttpResponse response = client.execute(httpPut);
checkResponse(response, CREATED);

final String location = response.getFirstHeader("Location").getValue();

final HttpGet httpGet = new HttpGet(location);
httpGet.addHeader("Prefer", "return=representation; " +
"include=\"http://www.w3.org/ns/ldp#PreferMinimalContainer\"; " +
"omit=\"http://fedora.info/definitions/v4/repository#ServerManaged\"");
final GraphStore graphStore = getGraphStore(httpGet);

assertFalse(graphStore.isEmpty());

final Graph tidiedGraph = getTidiedGraph(graphStore);
final Model expected = ModelFactory.createDefaultModel().read(IOUtils.toInputStream(body), location, "TTL");

assertTrue(tidiedGraph.isIsomorphicWith(expected.getGraph()));

return response;
} catch (final IOException e) {
assertTrue("Got IOException " + e, false);
return null;
}
}

private Graph getTidiedGraph(final GraphStore graphStore) {
final Graph betterGraph = GraphFactory.createDefaultGraph();
final ExtendedIterator<Triple> triples = graphStore.getDefaultGraph().find(Node.ANY, Node.ANY, Node.ANY);
final Map<Node, Node> bnodeMap = new HashMap<>();

while (triples.hasNext()) {
final Triple next = triples.next();

Triple replacement = next;

if (replacement.getSubject().toString().contains(".well-known")) {
if (!bnodeMap.containsKey(replacement.getSubject())) {
bnodeMap.put(replacement.getSubject(), NodeFactory.createAnon());
}

replacement = new Triple(bnodeMap.get(replacement.getSubject()),
replacement.getPredicate(),
replacement.getObject());
}

if (replacement.getObject().toString().contains(".well-known")) {

if (!bnodeMap.containsKey(replacement.getObject())) {
bnodeMap.put(replacement.getObject(), NodeFactory.createAnon());
}

replacement = new Triple(replacement.getSubject(),
replacement.getPredicate(),
bnodeMap.get(replacement.getObject()));
}

if (replacement.getObject().isLiteral()
&& replacement.getObject().getLiteral().getDatatypeURI()
.equals("http://www.w3.org/2001/XMLSchema#string")) {
replacement = new Triple(replacement.getSubject(),
replacement.getPredicate(),
NodeFactory.createLiteral(replacement.getObject().getLiteral().getLexicalForm()));
}

betterGraph.add(replacement);
}
return betterGraph;
}

protected void checkResponse(final HttpResponse response, final Response.StatusType expected) {
final int actual = response.getStatusLine().getStatusCode();
assertEquals("Didn't get a CREATED response!", expected.getStatusCode(), actual);
}


}

0 comments on commit 5dd9a1a

Please sign in to comment.