Skip to content

Commit

Permalink
RESTWS-367: Update request should return updated object
Browse files Browse the repository at this point in the history
  • Loading branch information
harsha89 authored and rkorytkowski committed Apr 11, 2013
1 parent 9b288de commit 8099f5f
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,13 @@ public void shouldUpdateASuperclass() throws Exception {
String newValue = "Do a CD4 Test STAT!";
Object updated = resource.update(SUPERCLASS_UUID, new SimpleObject().add("instructions", newValue), context);
Util.log("Updated subclass", updated);
Assert.assertEquals(Order.class, updated.getClass());
Assert.assertEquals(newValue, PropertyUtils.getProperty(updated, "instructions"));
}

@Test
public void shouldUpdateASubclass() throws Exception {
Object updated = resource.update(SUBCLASS_UUID, new SimpleObject().add("dose", "500"), context);
Util.log("Updated subclass", updated);
Assert.assertEquals(DrugOrder.class, updated.getClass());
Assert.assertEquals(500d, PropertyUtils.getProperty(updated, "dose"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ public void shouldEditAPerson() throws Exception {
assertFalse(person.isDead());
assertNull(person.getCauseOfDeath());
String json = "{\"gender\":\"F\",\"dead\":true, \"causeOfDeath\":\"15f83cd6-64e9-4e06-a5f9-364d3b14a43d\"}";
handle(newPostRequest(getURI() + "/" + getUuid(), json));
SimpleObject response = deserialize(handle(newPostRequest(getURI() + "/" + getUuid(), json)));
assertNotNull(response);
Object responsePersonContents=PropertyUtils.getProperty(response,"person");
assertNotNull(responsePersonContents);
assertTrue("F".equals(PropertyUtils.getProperty(responsePersonContents,"gender").toString()));
assertEquals("F", person.getGender());
assertTrue(person.isDead());
assertNotNull(person.getCauseOfDeath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ public void shouldEditName() throws Exception {
PersonName personName = service.getPersonNameByUuid(getUuid());
Assert.assertEquals("Chebaskwony", personName.getFamilyName());
String json = "{ \"familyName\":\"newName\" }";

handle(newPostRequest(getURI() + "/" + getUuid(), json));


SimpleObject response = deserialize(handle(newPostRequest(getURI() + "/" + getUuid(), json)));
Assert.assertNotNull(response);
Assert.assertEquals(PropertyUtils.getProperty(response,"familyName").toString(),"newName");
PersonName editedPersonName = service.getPersonNameByUuid(getUuid());
Assert.assertEquals(editedPersonName.getFamilyName(), "newName");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,23 @@ public static Object created(HttpServletResponse response, Object created) {
return created;
}

/**
* Sets the HTTP status for UPDATED and (if 'updated' has a uri) the Location header attribute
*
* @param response
* @param updated
* @return the object passed in
*/
public static Object updated(HttpServletResponse response, Object updated) {
response.setStatus(HttpServletResponse.SC_OK);
try {
String uri = (String) PropertyUtils.getProperty(updated, "uri");
response.addHeader("Location", uri);
}
catch (Exception ex) {}
return updated;
}

/**
* Updates the Uri prefix through which clients consuming web services will connect to the web
* app
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,15 @@ public Object update(String uuid, SimpleObject propertiesToUpdate, RequestContex

setConvertedProperties(delegate, propertiesToUpdate, handler.getUpdatableProperties(), false);
delegate = save(delegate);
return delegate;

SimpleObject ret = (SimpleObject) ConversionUtil.convertToRepresentation(delegate, Representation.DEFAULT);

// add the 'type' discriminator if we support subclasses
if (hasTypesDefined()) {
ret.add(RestConstants.PROPERTY_FOR_TYPE, getTypeName(delegate));
}

return ret;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public Object update(String parentUniqueId, String uuid, SimpleObject properties
testParent(delegate, parentUniqueId);
setConvertedProperties(delegate, propertiesToUpdate, getUpdatableProperties(), false);
delegate = save(delegate);
return delegate;
return ConversionUtil.convertToRepresentation(delegate, Representation.DEFAULT);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public Object update(@PathVariable("resource") String resource, @PathVariable("u
throws ResponseException {
RequestContext context = RestUtil.getRequestContext(request);
CrudResource res = (CrudResource) restService.getResourceByName(buildResourceName(resource));
res.update(uuid, post, context);
return RestUtil.noContent(response);
Object updated = res.update(uuid, post, context);
return RestUtil.updated(response, updated);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ public Object update(@PathVariable("resource") String resource, @PathVariable("p
throws ResponseException {
RequestContext context = RestUtil.getRequestContext(request);
SubResource res = (SubResource) restService.getResourceByName(buildResourceName(resource) + "/" + subResource);
res.update(parentUuid, uuid, post, context);
return RestUtil.noContent(response);
Object updated = res.update(parentUuid, uuid, post, context);
return RestUtil.updated(response, updated);
}

/**
Expand Down

0 comments on commit 8099f5f

Please sign in to comment.