private void partialUpdatePhoto(PrintWriter respWriter, long photoId) throws RemoteInvocationException { final Request<Photo> getReq = _photoBuilders.get().id(photoId).build(); final ResponseFuture<Photo> getFuture = _restClient.sendRequest(getReq); final Response<Photo> getResp = getFuture.getResponse(); final Photo originalPhoto = getResp.getEntity(); final Photo updatedPhoto = new Photo().setTitle("Partially Updated Photo"); final PatchRequest<Photo> patch = PatchGenerator.diff(originalPhoto, updatedPhoto); final Request<EmptyRecord> partialUpdateRequest = _photoBuilders.partialUpdate().id(photoId).input(patch).build(); final int status = _restClient.sendRequest(partialUpdateRequest).getResponse().getStatus(); respWriter.println("Partial update photo is successful: " + (status == 202)); }
@Test( dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestMembershipsBuilderDataProvider") public void testAssociationBatchCreateGetUpdatePatchDelete( ProtocolVersion version, RootBuilderWrapper<CompoundKey, GroupMembership> membershipBuilders) throws RemoteInvocationException { // Setup - batch create two group memberships CompoundKey key1 = buildCompoundKey(1, 1); CompoundKey key2 = buildCompoundKey(2, 1); GroupMembership groupMembership1 = buildGroupMembership(null, "*****@*****.**", "Alfred", "Hitchcock"); GroupMembership groupMembership2 = buildGroupMembership(null, "*****@*****.**", "Bruce", "Willis"); Map<CompoundKey, UpdateStatus> results = getClient() .sendRequest( membershipBuilders .batchUpdate() .input(key1, groupMembership1) .input(key2, groupMembership2) .build()) .getResponse() .getEntity() .getResults(); Assert.assertEquals(results.get(key1).getStatus().intValue(), 204); Assert.assertEquals(results.get(key2).getStatus().intValue(), 204); // BatchGet memberships final RestliRequestOptions requestOptions = membershipBuilders.getRequestOptions(); Request<BatchKVResponse<CompoundKey, EntityResponse<GroupMembership>>> request = new GroupMembershipsRequestBuilders(requestOptions) .batchGet() .ids(key1, key2) .fields(GroupMembership.fields().contactEmail()) .build(); Map<CompoundKey, EntityResponse<GroupMembership>> groupMemberships = getClient().sendRequest(request).getResponse().getEntity().getResults(); Assert.assertTrue(groupMemberships.containsKey(key1)); Assert.assertEquals( groupMemberships.get(key1).getEntity().getContactEmail(), "*****@*****.**"); Assert.assertTrue(groupMemberships.containsKey(key2)); Assert.assertEquals( groupMemberships.get(key2).getEntity().getContactEmail(), "*****@*****.**"); // Batch partial update GroupMembership patchedGroupMembership1 = buildGroupMembership(null, "*****@*****.**", "ALFRED", "Hitchcock"); GroupMembership patchedGroupMembership2 = buildGroupMembership(null, "*****@*****.**", "BRUCE", "Willis"); Map<CompoundKey, PatchRequest<GroupMembership>> patchInputs = new HashMap<CompoundKey, PatchRequest<GroupMembership>>(); patchInputs.put(key1, PatchGenerator.diff(groupMembership1, patchedGroupMembership1)); patchInputs.put(key2, PatchGenerator.diff(groupMembership2, patchedGroupMembership2)); Map<CompoundKey, UpdateStatus> patchResults = getClient() .sendRequest(membershipBuilders.batchPartialUpdate().patchInputs(patchInputs).build()) .getResponse() .getEntity() .getResults(); Assert.assertEquals(patchResults.get(key1).getStatus().intValue(), 204); Assert.assertEquals(patchResults.get(key2).getStatus().intValue(), 204); // Batch get to make sure our patch applied Request<BatchKVResponse<CompoundKey, EntityResponse<GroupMembership>>> batchGetRequest = new GroupMembershipsRequestBuilders(requestOptions) .batchGet() .ids(key1, key2) .fields(GroupMembership.fields().contactEmail(), GroupMembership.fields().firstName()) .build(); BatchKVResponse<CompoundKey, EntityResponse<GroupMembership>> entity = getClient().sendRequest(batchGetRequest).getResponse().getEntity(); Assert.assertEquals(entity.getErrors().size(), 0); Assert.assertEquals(entity.getResults().size(), 2); Assert.assertEquals( entity.getResults().get(key1).getEntity().getContactEmail(), "*****@*****.**"); Assert.assertEquals(entity.getResults().get(key1).getEntity().getFirstName(), "ALFRED"); Assert.assertEquals( entity.getResults().get(key2).getEntity().getContactEmail(), "*****@*****.**"); Assert.assertEquals(entity.getResults().get(key2).getEntity().getFirstName(), "BRUCE"); // GetAll memberships Request<CollectionResponse<GroupMembership>> getAllRequest = membershipBuilders .getAll() .paginate(1, 2) .fields(GroupMembership.fields().contactEmail()) .build(); List<GroupMembership> elements = getClient().sendRequest(getAllRequest).getResponse().getEntity().getElements(); Assert.assertEquals(elements.size(), 1); // Delete the newly created group memberships Map<CompoundKey, UpdateStatus> deleteResult = getClient() .sendRequest(membershipBuilders.batchDelete().ids(key1, key2).build()) .getResponse() .getEntity() .getResults(); Assert.assertEquals(deleteResult.get(key1).getStatus().intValue(), 204); Assert.assertEquals(deleteResult.get(key2).getStatus().intValue(), 204); // Make sure they are gone BatchKVResponse<CompoundKey, EntityResponse<GroupMembership>> getResponse = getClient().sendRequest(request).getResponse().getEntity(); Assert.assertEquals(getResponse.getResults().size(), getResponse.getErrors().size()); Assert.assertTrue(getResponse.getErrors().containsKey(key1)); Assert.assertTrue(getResponse.getErrors().containsKey(key2)); Assert.assertEquals(getResponse.getErrors().get(key1).getStatus().intValue(), 404); Assert.assertEquals(getResponse.getErrors().get(key2).getStatus().intValue(), 404); }
@Test( dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider") public void testCollectionCreateGetUpdateDeleteId(RestliRequestOptions requestOptions) throws RemoteInvocationException { // find with optional params Group group = new Group(); String name = "test"; int memberID = 1; group.setName(name); group.setOwner(buildGroupMembership(memberID, "[email protected]", "f", "l")); GroupMembershipParam param = new GroupMembershipParam(); param.setIntParameter(1); param.setStringParameter("String"); final GroupsRequestBuilders groupBuilders = new GroupsRequestBuilders(requestOptions); final GroupMembershipsRequestBuilders membershipBuilders = new GroupMembershipsRequestBuilders(requestOptions); // Create Response<IdResponse<Integer>> response = getClient().sendRequest(groupBuilders.create().input(group).build()).getResponse(); Assert.assertEquals(response.getStatus(), 201); Integer createdId = response.getEntity().getId(); Assert.assertNotNull(createdId); @SuppressWarnings("deprecation") String stringId = response.getId(); Assert.assertEquals(createdId.intValue(), Integer.parseInt(stringId)); // Get newly created group and verify name Assert.assertEquals( getClient() .sendRequest(groupBuilders.get().id(createdId).build()) .getResponse() .getEntity() .getName(), name); // Partial update - change name String newName = "new name"; group.setName(newName); PatchRequest<Group> patch = PatchGenerator.diffEmpty(group); ResponseFuture<EmptyRecord> responseFuture = getClient().sendRequest(groupBuilders.partialUpdate().id(createdId).input(patch).build()); Assert.assertEquals(204, responseFuture.getResponse().getStatus()); // Get updated group and verify name Assert.assertEquals( getClient() .sendRequest(groupBuilders.get().id(createdId).build()) .getResponse() .getEntity() .getName(), newName); // Delete responseFuture = getClient().sendRequest(groupBuilders.delete().id(createdId).build()); Assert.assertEquals(204, responseFuture.getResponse().getStatus()); // Verify deleted try { getClient().sendRequest(groupBuilders.get().id(createdId).build()).getResponse(); Assert.fail("Expected RestLiResponseException"); } catch (RestLiResponseException e) { Assert.assertEquals(e.getStatus(), 404); } // Cleanup - delete the owner's membership that was created along with the group responseFuture = getClient() .sendRequest( membershipBuilders.delete().id(buildCompoundKey(memberID, createdId)).build()); Assert.assertEquals(204, responseFuture.getResponse().getStatus()); }