Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust PATCH requests to operate on externalized IRIs #387

Merged
merged 1 commit into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,10 +184,13 @@ 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());
}
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