Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Check that we can PUT and GET the same triples
  • Loading branch information
cbeer committed Oct 23, 2014
1 parent 0c61619 commit ba20064
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 14 deletions.
Expand Up @@ -15,17 +15,30 @@
*/
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;

/**
Expand All @@ -43,13 +56,72 @@ protected HttpResponse createLDPRSAndCheckResponse(final String pid, final Strin
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());
Graph betterGraph = getTidiedGraph(graphStore);

Model expected = ModelFactory.createDefaultModel().read(IOUtils.toInputStream(body), location, "TTL");
assertTrue(betterGraph.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);
Expand Down
Expand Up @@ -65,7 +65,6 @@ public void testExample10() throws IOException {
"omit=\"http://fedora.info/definitions/v4/repository#ServerManaged\"");
final GraphStore graphStore = getGraphStore(httpGet);

logger.error(graphStore.toString());
assertFalse(graphStore.isEmpty());
}
}
Expand Up @@ -15,15 +15,10 @@
*/
package org.fcrepo.integration.rdf;

import com.hp.hpl.jena.update.GraphStore;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.assertFalse;

/**
* @author cabeer
*/
Expand All @@ -39,13 +34,6 @@ public void testExample() throws IOException {
" acl:mode acl:Read, acl:Write; \n" +
" acl:agent <" + card + "#me> .";

final HttpResponse response = createLDPRSAndCheckResponse(getRandomUniquePid(), s);
final String location = response.getFirstHeader("Location").getValue();

final HttpGet httpGet = new HttpGet(location);
httpGet.addHeader("Prefer", "return=minimal");
final GraphStore graphStore = getGraphStore(httpGet);

assertFalse(graphStore.isEmpty());
createLDPRSAndCheckResponse(getRandomUniquePid(), s);
}
}

0 comments on commit ba20064

Please sign in to comment.