Skip to content

Commit

Permalink
Adjust PATCH requests to operate on externalized IRIs
Browse files Browse the repository at this point in the history
Resolves #386

This also adds an integration test to check this behavior
  • Loading branch information
acoburn committed Apr 4, 2019
1 parent 784138d commit 7c21d03
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ default void testPatchRDF() {
assertAll("Check an LDP-RS resource", checkRdfResponse(res, LDP.RDFSource, null));
}

await().until(() -> !initialETag.equals(getETag(getResourceLocation())));
final EntityTag etag2 = getETag(getResourceLocation());
await().until(() -> !initialETag.equals(etag2));

// Fetch the updated resource
try (final Response res = target(getResourceLocation()).request().accept("application/n-triples").get()) {
Expand All @@ -277,6 +278,26 @@ default void testPatchRDF() {
assertTrue(res.getEntityTag().isWeak(), "Check that the ETag is weak");
assertNotEquals(initialETag, res.getEntityTag(), "Compare the first and second ETags");
}

// Now remove the triple
try (final Response res = target(getResourceLocation()).request().method("PATCH",
entity("DELETE DATA { <" + getResourceLocation() + "> <http://purl.org/dc/terms/title> " +
"\"Title\" }", APPLICATION_SPARQL_UPDATE))) {
assertAll("Check an LDP-RS resource", checkRdfResponse(res, LDP.RDFSource, null));
}

await().until(() -> !etag2.equals(getETag(getResourceLocation())));

// Fetch the updated resource
try (final Response res = target(getResourceLocation()).request().accept("application/n-triples").get()) {
assertAll("Check an updated resource", checkRdfResponse(res, LDP.RDFSource, APPLICATION_N_TRIPLES_TYPE));
final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), NTRIPLES);
assertEquals(3L, g.size(), "Check the graph size");
assertFalse(g.contains(rdf.createIRI(getResourceLocation()), DC.title, rdf.createLiteral("Title")),
"Check for a dc:title triple");
assertTrue(res.getEntityTag().isWeak(), "Check that the ETag is weak");
assertNotEquals(etag2, res.getEntityTag(), "Compare the second and third ETags");
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import static org.slf4j.LoggerFactory.getLogger;
import static org.trellisldp.api.Resource.SpecialResources.DELETED_RESOURCE;
import static org.trellisldp.api.Resource.SpecialResources.MISSING_RESOURCE;
import static org.trellisldp.api.TrellisUtils.TRELLIS_DATA_PREFIX;
import static org.trellisldp.http.core.HttpConstants.ACL;
import static org.trellisldp.http.core.HttpConstants.PREFERENCE_APPLIED;
import static org.trellisldp.http.core.Prefer.PREFER_REPRESENTATION;
Expand Down Expand Up @@ -185,14 +184,16 @@ private List<Triple> updateGraph(final RDFSyntax syntax, final IRI graphName) {
// Update existing graph
try (final TrellisGraph graph = TrellisGraph.createGraph()) {
try (final Stream<Quad> stream = getResource().stream(graphName)) {
stream.map(Quad::asTriple).forEachOrdered(graph::add);
stream.map(Quad::asTriple)
.map(unskolemizeTriples(getServices().getResourceService(), getBaseUrl()))
.forEachOrdered(graph::add);
}

getServices().getIOService().update(graph.asGraph(), updateBody, syntax,
TRELLIS_DATA_PREFIX + getRequest().getPath() + (ACL.equals(getRequest().getExt()) ? "?ext=acl" : ""));
getBaseUrl() + getRequest().getPath() + (ACL.equals(getRequest().getExt()) ? "?ext=acl" : ""));
triples = graph.stream().filter(triple -> !RDF.type.equals(triple.getPredicate())
|| !triple.getObject().ntriplesString().startsWith("<" + LDP.getNamespace())).collect(toList());
|| !triple.getObject().ntriplesString().startsWith("<" + LDP.getNamespace())).collect(toList());
}

return triples;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ private void setUpResourceService() {
when(mockResourceService.replace(any(Metadata.class), any(Dataset.class))).thenReturn(completedFuture(null));
when(mockResourceService.delete(any(Metadata.class))).thenReturn(completedFuture(null));
when(mockResourceService.add(any(IRI.class), any(Dataset.class))).thenReturn(completedFuture(null));
when(mockResourceService.unskolemize(any(Literal.class))).then(returnsFirstArg());
when(mockResourceService.unskolemize(any(IRI.class))).thenCallRealMethod();
when(mockResourceService.skolemize(any(Literal.class))).then(returnsFirstArg());
when(mockResourceService.skolemize(any(IRI.class))).then(returnsFirstArg());
when(mockResourceService.skolemize(any(BlankNode.class))).thenAnswer(inv ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,15 @@ public void testEntity() {
when(mockTrellisRequest.getContentType()).thenReturn(APPLICATION_SPARQL_UPDATE);
when(mockResource.stream(eq(PreferUserManaged))).thenAnswer(x -> of(quad));
when(mockTrellisRequest.getPath()).thenReturn("resource");
when(mockTrellisRequest.getBaseUrl()).thenReturn("http://localhost/");

final PatchHandler patchHandler = new PatchHandler(mockTrellisRequest, insert, mockBundler, null, null);
final Response res = patchHandler.updateResource(patchHandler.initialize(mockParent, mockResource))
.toCompletableFuture().join().build();

assertEquals(NO_CONTENT, res.getStatusInfo(), "Incorrect response code!");

verify(mockIoService).update(any(Graph.class), eq(insert), eq(SPARQL_UPDATE), eq(identifier.getIRIString()));
verify(mockIoService).update(any(Graph.class), eq(insert), eq(SPARQL_UPDATE), eq("http://localhost/resource"));
verify(mockResourceService).replace(any(Metadata.class), any(Dataset.class));
}

Expand Down Expand Up @@ -188,7 +189,7 @@ public void testError2() {
when(mockTrellisRequest.getContentType()).thenReturn(APPLICATION_SPARQL_UPDATE);
when(mockTrellisRequest.getPath()).thenReturn("resource");
doThrow(RuntimeTrellisException.class).when(mockIoService)
.update(any(Graph.class), eq(insert), eq(SPARQL_UPDATE), eq(identifier.getIRIString()));
.update(any(Graph.class), eq(insert), eq(SPARQL_UPDATE), eq(baseUrl + "resource"));

final PatchHandler patchHandler = new PatchHandler(mockTrellisRequest, insert, mockBundler, null, baseUrl);
final Response res = assertThrows(BadRequestException.class, () ->
Expand Down

0 comments on commit 7c21d03

Please sign in to comment.