Skip to content

Commit

Permalink
PUT requires either new resource or RDF to update properties, adding …
Browse files Browse the repository at this point in the history
…ITs for PUT and POST/slug to existing resources
  • Loading branch information
escowles committed May 20, 2014
1 parent ca593fe commit 3dbcac2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Expand Up @@ -394,7 +394,7 @@ public Response updateSparql(@PathParam("path")
}

/**
* Replace triples with triples from a new model
* Create a resource at a specified path, or replace triples with provided RDF.
* @param pathList
* @param uriInfo
* @param requestContentType
Expand All @@ -411,8 +411,7 @@ public Response createOrReplaceObjectRdf(
@HeaderParam("Content-Type")
final MediaType requestContentType,
final InputStream requestBodyStream,
@Context
final Request request,
@Context final Request request,
@Context final HttpServletResponse servletResponse) throws RepositoryException, ParseException,
IOException, InvalidChecksumException, URISyntaxException {
final String path = toPath(pathList);
Expand All @@ -425,9 +424,11 @@ public Response createOrReplaceObjectRdf(

final MediaType contentType = getSimpleContentType(requestContentType);

final boolean preexisting;
if (nodeService.exists(session, path)) {
resource = nodeService.getObject(session, path);
response = noContent();
preexisting = true;
} else {
final MediaType effectiveContentType
= requestBodyStream == null || requestContentType == null ? null : contentType;
Expand All @@ -438,6 +439,7 @@ public Response createOrReplaceObjectRdf(
final URI location = new URI(idTranslator.getSubject(resource.getNode().getPath()).getURI());

response = created(location).entity(location.toString());
preexisting = false;
}

evaluateRequestPreconditions(request, resource);
Expand All @@ -455,6 +457,8 @@ public Response createOrReplaceObjectRdf(

resource.replaceProperties(graphSubjects, inputModel);

} else if (preexisting) {
return status(SC_CONFLICT).entity("No RDF provided and the resource already exists!").build();
}

session.save();
Expand Down
Expand Up @@ -214,6 +214,17 @@ public void testIngestWithSlug() throws Exception {
getStatus(new HttpGet(location)));
}

@Test
public void testIngestWithRepeatedSlug() throws Exception {
final String pid = getRandomUniquePid();
final HttpPut put = new HttpPut(serverAddress + pid);
assertEquals(201, getStatus(put));

final HttpPost method = postObjMethod("");
method.addHeader("Slug", pid);
assertEquals(409, getStatus(method));
}

@Test
public void testIngestWithBinary() throws Exception {
final HttpPost method = postObjMethod("");
Expand Down Expand Up @@ -740,6 +751,16 @@ public void testUpdateObjectGraphWithProblems() throws Exception {

}

@Test
public void testRepeatedPut() throws Exception {
final String pid = getRandomUniquePid();
final HttpPut firstPut = new HttpPut(serverAddress + pid);
assertEquals(201, getStatus(firstPut));

final HttpPut secondPut = new HttpPut(serverAddress + pid);
assertEquals(409, getStatus(secondPut));
}

@Test
public void testFilteredLDPTypes() throws Exception {
final String pid = getRandomUniquePid();
Expand Down

0 comments on commit 3dbcac2

Please sign in to comment.