Skip to content

Commit 58c503d

Browse files
committed
Adjust PATCH requests to operate on externalized IRIs
Resolves #386
1 parent 52a3369 commit 58c503d

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

components/test/src/main/java/org/trellisldp/test/LdpRdfTests.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@ default void testPatchRDF() {
265265
assertAll("Check an LDP-RS resource", checkRdfResponse(res, LDP.RDFSource, null));
266266
}
267267

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

270271
// Fetch the updated resource
271272
try (final Response res = target(getResourceLocation()).request().accept("application/n-triples").get()) {
@@ -277,6 +278,26 @@ default void testPatchRDF() {
277278
assertTrue(res.getEntityTag().isWeak(), "Check that the ETag is weak");
278279
assertNotEquals(initialETag, res.getEntityTag(), "Compare the first and second ETags");
279280
}
281+
282+
// Now remove the triple
283+
try (final Response res = target(getResourceLocation()).request().method("PATCH",
284+
entity("DELETE DATA { <" + getResourceLocation() + "> <http://purl.org/dc/terms/title> " +
285+
"\"Title\" }", APPLICATION_SPARQL_UPDATE))) {
286+
assertAll("Check an LDP-RS resource", checkRdfResponse(res, LDP.RDFSource, null));
287+
}
288+
289+
await().until(() -> !etag2.equals(getETag(getResourceLocation())));
290+
291+
// Fetch the updated resource
292+
try (final Response res = target(getResourceLocation()).request().accept("application/n-triples").get()) {
293+
assertAll("Check an updated resource", checkRdfResponse(res, LDP.RDFSource, APPLICATION_N_TRIPLES_TYPE));
294+
final Graph g = readEntityAsGraph(res.getEntity(), getBaseURL(), NTRIPLES);
295+
assertEquals(3L, g.size(), "Check the graph size");
296+
assertFalse(g.contains(rdf.createIRI(getResourceLocation()), DC.title, rdf.createLiteral("Title")),
297+
"Check for a dc:title triple");
298+
assertTrue(res.getEntityTag().isWeak(), "Check that the ETag is weak");
299+
assertNotEquals(etag2, res.getEntityTag(), "Compare the second and third ETags");
300+
}
280301
}
281302

282303
/**

core/http/src/main/java/org/trellisldp/http/impl/PatchHandler.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import static org.slf4j.LoggerFactory.getLogger;
3030
import static org.trellisldp.api.Resource.SpecialResources.DELETED_RESOURCE;
3131
import static org.trellisldp.api.Resource.SpecialResources.MISSING_RESOURCE;
32-
import static org.trellisldp.api.TrellisUtils.TRELLIS_DATA_PREFIX;
3332
import static org.trellisldp.api.TrellisUtils.toQuad;
3433
import static org.trellisldp.http.core.HttpConstants.ACL;
3534
import static org.trellisldp.http.core.HttpConstants.PREFERENCE_APPLIED;
@@ -185,10 +184,12 @@ private List<Triple> updateGraph(final RDFSyntax syntax, final IRI graphName) {
185184
// Update existing graph
186185
try (final TrellisGraph graph = TrellisGraph.createGraph()) {
187186
try (final Stream<Triple> stream = getResource().stream(graphName)) {
188-
stream.forEachOrdered(graph::add);
187+
stream.map(unskolemizeTriples(getServices().getResourceService(), getBaseUrl()))
188+
.forEachOrdered(graph::add);
189189
}
190+
190191
getServices().getIOService().update(graph.asGraph(), updateBody, syntax,
191-
TRELLIS_DATA_PREFIX + getRequest().getPath() + (ACL.equals(getRequest().getExt()) ? "?ext=acl" : ""));
192+
getBaseUrl() + getRequest().getPath() + (ACL.equals(getRequest().getExt()) ? "?ext=acl" : ""));
192193
triples = graph.stream().filter(triple -> !RDF.type.equals(triple.getPredicate())
193194
|| !triple.getObject().ntriplesString().startsWith("<" + LDP.getNamespace())).collect(toList());
194195
}

core/http/src/test/java/org/trellisldp/http/impl/BaseTestHandler.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ private void setUpResourceService() {
219219
when(mockResourceService.replace(any(Metadata.class), any(Dataset.class))).thenReturn(completedFuture(null));
220220
when(mockResourceService.delete(any(Metadata.class))).thenReturn(completedFuture(null));
221221
when(mockResourceService.add(any(IRI.class), any(Dataset.class))).thenReturn(completedFuture(null));
222+
when(mockResourceService.unskolemize(any(Literal.class))).then(returnsFirstArg());
223+
when(mockResourceService.unskolemize(any(IRI.class))).thenCallRealMethod();
222224
when(mockResourceService.skolemize(any(Literal.class))).then(returnsFirstArg());
223225
when(mockResourceService.skolemize(any(IRI.class))).then(returnsFirstArg());
224226
when(mockResourceService.skolemize(any(BlankNode.class))).thenAnswer(inv ->

core/http/src/test/java/org/trellisldp/http/impl/PatchHandlerTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,15 @@ public void testEntity() {
103103
when(mockTrellisRequest.getContentType()).thenReturn(APPLICATION_SPARQL_UPDATE);
104104
when(mockResource.stream(eq(PreferUserManaged))).thenAnswer(x -> of(triple));
105105
when(mockTrellisRequest.getPath()).thenReturn("resource");
106+
when(mockTrellisRequest.getBaseUrl()).thenReturn("http://localhost/");
106107

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

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

113-
verify(mockIoService).update(any(Graph.class), eq(insert), eq(SPARQL_UPDATE), eq(identifier.getIRIString()));
114+
verify(mockIoService).update(any(Graph.class), eq(insert), eq(SPARQL_UPDATE), eq("http://localhost/resource"));
114115
verify(mockResourceService).replace(any(Metadata.class), any(Dataset.class));
115116
}
116117

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

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

0 commit comments

Comments
 (0)