use of com.linkedin.restli.server.UpdateResponse in project rest.li by linkedin.
the class TestPhotoResource method testResourcePartialUpdate.
@Test
public void testResourcePartialUpdate() {
final Long id = createPhoto("PartialTestPhoto");
final String newTitle = "The New Title";
final Photo p = new Photo().setTitle(newTitle);
final PatchRequest<Photo> patch = PatchGenerator.diffEmpty(p);
final UpdateResponse uResp = _res.update(id, patch);
final Photo updatedPhoto = _res.get(id);
Assert.assertEquals(updatedPhoto.getTitle(), newTitle);
Assert.assertEquals(uResp.getStatus(), HttpStatus.S_202_ACCEPTED);
}
use of com.linkedin.restli.server.UpdateResponse in project rest.li by linkedin.
the class TestPhotoResource method testResourceUpdate.
@Test
public void testResourceUpdate() {
final Long id = createPhoto();
final LatLong l1 = new LatLong().setLongitude(-27.0f);
final EXIF e1 = new EXIF().setLocation(l1);
final Photo p1 = new Photo().setExif(e1);
final UpdateResponse uResp = _res.update(id, p1);
Assert.assertEquals(uResp.getStatus(), HttpStatus.S_204_NO_CONTENT);
// validate data is changed to correct value
final Photo p2 = _res.get(id);
Assert.assertNotNull(p2.hasExif());
final EXIF e2 = p2.getExif();
Assert.assertNotNull(e2.hasLocation());
final LatLong l2 = e2.getLocation();
Assert.assertEquals(l2.getLongitude(), -27.0f);
}
use of com.linkedin.restli.server.UpdateResponse in project rest.li by linkedin.
the class ExampleRequestResponseGenerator method createBatchUpdateResult.
private BatchUpdateResult<Object, RecordTemplatePlaceholder> createBatchUpdateResult(Object id1, Object id2) {
Map<Object, UpdateResponse> buResponseData = new HashMap<>();
buResponseData.put(id1, new UpdateResponse(HttpStatus.S_200_OK));
buResponseData.put(id2, new UpdateResponse(HttpStatus.S_200_OK));
return new BatchUpdateResult<>(buResponseData);
}
use of com.linkedin.restli.server.UpdateResponse in project rest.li by linkedin.
the class AlbumEntryResource method update.
/**
* Add the specified photo to the specified album.
* If a matching pair of IDs already exists, this changes the add date.
*/
@Override
@SuccessResponse(statuses = { HttpStatus.S_204_NO_CONTENT })
@ServiceErrors(INVALID_PERMISSIONS)
@ParamError(code = INVALID_ID, parameterNames = { "albumEntryId" })
public UpdateResponse update(CompoundKey key, AlbumEntry entity) {
long photoId = (Long) key.getPart("photoId");
long albumId = (Long) key.getPart("albumId");
// make sure photo and album exist
if (!_photoDb.getData().containsKey(photoId))
throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Nonexistent photo ID: " + photoId);
if (!_albumDb.getData().containsKey(albumId))
throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Nonexistent album ID: " + albumId);
// disallow changing entity ID
if (entity.hasAlbumId() || entity.hasPhotoId())
throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Photo/album ID are not acceptable in request");
// make sure the ID in the entity is consistent with the key in the database
entity.setPhotoId(photoId);
entity.setAlbumId(albumId);
_db.getData().put(key, entity);
return new UpdateResponse(HttpStatus.S_204_NO_CONTENT);
}
use of com.linkedin.restli.server.UpdateResponse in project rest.li by linkedin.
the class TestRestLiMethodInvocation method testAsyncPost.
@SuppressWarnings("unchecked")
@Test
public void testAsyncPost() throws Exception {
Map<String, ResourceModel> resourceModelMap = buildResourceModels(AsyncStatusCollectionResource.class, AsyncRepliesCollectionResource.class, AsyncLocationResource.class, AsyncDiscoveredItemsResource.class);
ResourceModel statusResourceModel = resourceModelMap.get("/asyncstatuses");
ResourceModel repliesResourceModel = statusResourceModel.getSubResource("asyncreplies");
ResourceModel locationResourceModel = statusResourceModel.getSubResource("asynclocation");
ResourceModel discoveredItemsResourceModel = resourceModelMap.get("/asyncdiscovereditems");
RestLiCallback callback = getCallback();
ResourceMethodDescriptor methodDescriptor;
AsyncStatusCollectionResource statusResource;
AsyncRepliesCollectionResource repliesResource;
AsyncLocationResource locationResource;
AsyncDiscoveredItemsResource discoveredItemsResource;
// #1
methodDescriptor = statusResourceModel.findMethod(ResourceMethod.CREATE);
statusResource = getMockResource(AsyncStatusCollectionResource.class);
statusResource.create(EasyMock.anyObject(), EasyMock.anyObject());
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
@SuppressWarnings("unchecked") Callback<CreateResponse> callback = (Callback<CreateResponse>) EasyMock.getCurrentArguments()[1];
callback.onSuccess(null);
return null;
}
});
EasyMock.replay(statusResource);
checkAsyncInvocation(statusResource, callback, methodDescriptor, "POST", version, "/asyncstatuses", "{}", null);
// #1.1: different endpoint
methodDescriptor = repliesResourceModel.findMethod(ResourceMethod.CREATE);
repliesResource = getMockResource(AsyncRepliesCollectionResource.class);
repliesResource.create(EasyMock.anyObject(), EasyMock.anyObject());
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
Callback<CreateResponse> callback = (Callback<CreateResponse>) EasyMock.getCurrentArguments()[1];
callback.onSuccess(null);
return null;
}
});
EasyMock.replay(repliesResource);
checkAsyncInvocation(repliesResource, callback, methodDescriptor, "POST", version, "/asyncstatuses/1/replies", "{}", buildPathKeys("statusID", 1L));
// #2: Collection Partial Update
methodDescriptor = statusResourceModel.findMethod(ResourceMethod.PARTIAL_UPDATE);
statusResource = getMockResource(AsyncStatusCollectionResource.class);
PatchTree p = new PatchTree();
p.addOperation(new PathSpec("foo"), PatchOpFactory.setFieldOp(42));
PatchRequest<Status> expected = PatchRequest.createFromPatchDocument(p.getDataMap());
statusResource.update(eq(1L), eq(expected), EasyMock.anyObject());
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
@SuppressWarnings("unchecked") Callback<UpdateResponse> callback = (Callback<UpdateResponse>) EasyMock.getCurrentArguments()[2];
callback.onSuccess(null);
return null;
}
});
EasyMock.replay(statusResource);
checkAsyncInvocation(statusResource, callback, methodDescriptor, "POST", version, "/asyncstatuses/1", "{\"patch\":{\"$set\":{\"foo\":42}}}", buildPathKeys("statusID", 1L));
// #3: Simple Resource Partial Update
methodDescriptor = locationResourceModel.findMethod(ResourceMethod.PARTIAL_UPDATE);
locationResource = getMockResource(AsyncLocationResource.class);
p = new PatchTree();
p.addOperation(new PathSpec("foo"), PatchOpFactory.setFieldOp(51));
PatchRequest<Location> expectedLocation = PatchRequest.createFromPatchDocument(p.getDataMap());
locationResource.update(eq(expectedLocation), EasyMock.anyObject());
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
@SuppressWarnings("unchecked") Callback<UpdateResponse> callback = (Callback<UpdateResponse>) EasyMock.getCurrentArguments()[1];
callback.onSuccess(null);
return null;
}
});
EasyMock.replay(locationResource);
checkAsyncInvocation(locationResource, callback, methodDescriptor, "POST", version, "/asyncstatuses/1/asynclocation", "{\"patch\":{\"$set\":{\"foo\":51}}}", buildPathKeys("statusID", 1L));
// #4 Complex-key resource create
methodDescriptor = discoveredItemsResourceModel.findMethod(ResourceMethod.CREATE);
discoveredItemsResource = getMockResource(AsyncDiscoveredItemsResource.class);
discoveredItemsResource.create(EasyMock.anyObject(), EasyMock.anyObject());
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
@SuppressWarnings("unchecked") Callback<CreateResponse> callback = (Callback<CreateResponse>) EasyMock.getCurrentArguments()[1];
callback.onSuccess(null);
return null;
}
});
EasyMock.replay(discoveredItemsResource);
checkAsyncInvocation(discoveredItemsResource, callback, methodDescriptor, "POST", version, "/asyncdiscovereditems", "{}", null);
// #5 Partial update on complex-key resource
methodDescriptor = discoveredItemsResourceModel.findMethod(ResourceMethod.PARTIAL_UPDATE);
discoveredItemsResource = getMockResource(AsyncDiscoveredItemsResource.class);
p = new PatchTree();
p.addOperation(new PathSpec("foo"), PatchOpFactory.setFieldOp(43));
PatchRequest<DiscoveredItem> expectedDiscoveredItem = PatchRequest.createFromPatchDocument(p.getDataMap());
ComplexResourceKey<DiscoveredItemKey, DiscoveredItemKeyParams> key = getDiscoveredItemComplexKey(1L, 2, 3L);
discoveredItemsResource.update(eq(key), eq(expectedDiscoveredItem), EasyMock.anyObject());
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
@SuppressWarnings("unchecked") Callback<CreateResponse> callback = (Callback<CreateResponse>) EasyMock.getCurrentArguments()[2];
callback.onSuccess(null);
return null;
}
});
EasyMock.replay(discoveredItemsResource);
checkAsyncInvocation(discoveredItemsResource, callback, methodDescriptor, "POST", version, "/asyncdiscovereditems/(itemId:1,type:2,userId:3)", "{\"patch\":{\"$set\":{\"foo\":43}}}", buildPathKeys("asyncDiscoveredItemId", key));
}
Aggregations