Search in sources :

Example 16 with PatchRequest

use of com.linkedin.restli.common.PatchRequest in project rest.li by linkedin.

the class TestParseqBasedFluentClientApi method testBatchPartialUpdateWithErrors.

@Test
public void testBatchPartialUpdateWithErrors() throws Exception {
    PartialUpdateGreeting greetings = new PartialUpdateGreetingFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
    Map<Long, PatchRequest<Greeting>> inputs = new HashMap<>();
    Greeting original = getGreeting();
    String message = "Edited message: fluent api test partialUpdateAndGet";
    Greeting update = getGreeting(message);
    inputs.put(21L, PatchGenerator.diff(original, update));
    inputs.put(-2L, PatchGenerator.diff(original, update));
    CompletionStage<Map<Long, UpdateStatus>> result = greetings.batchPartialUpdate(inputs);
    CompletableFuture<Map<Long, UpdateStatus>> future = result.toCompletableFuture();
    Assert.assertNotNull(future.get(5000, TimeUnit.MILLISECONDS));
    Assert.assertEquals(future.get().get(21L).getStatus().intValue(), 200);
    Assert.assertEquals(future.get().get(-2L).getStatus().intValue(), 404);
}
Also used : CreateGreeting(com.linkedin.restli.examples.greetings.client.CreateGreeting) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) PartialUpdateGreeting(com.linkedin.restli.examples.greetings.client.PartialUpdateGreeting) HashMap(java.util.HashMap) CustomLong(com.linkedin.restli.examples.custom.types.CustomLong) PartialUpdateGreetingFluentClient(com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingFluentClient) PatchRequest(com.linkedin.restli.common.PatchRequest) Map(java.util.Map) HashMap(java.util.HashMap) StringMap(com.linkedin.data.template.StringMap) PartialUpdateGreeting(com.linkedin.restli.examples.greetings.client.PartialUpdateGreeting) Test(org.testng.annotations.Test)

Example 17 with PatchRequest

use of com.linkedin.restli.common.PatchRequest in project rest.li by linkedin.

the class TestGroupsClient method testAssociationBatchCreateGetUpdatePatchDelete.

@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@test.linkedin.com", "Alfred", "Hitchcock");
    GroupMembership groupMembership2 = buildGroupMembership(null, "bruce@test.linkedin.com", "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(), "alfred@test.linkedin.com");
    Assert.assertTrue(groupMemberships.containsKey(key2));
    Assert.assertEquals(groupMemberships.get(key2).getEntity().getContactEmail(), "bruce@test.linkedin.com");
    // Batch partial update
    GroupMembership patchedGroupMembership1 = buildGroupMembership(null, "ALFRED@test.linkedin.com", "ALFRED", "Hitchcock");
    GroupMembership patchedGroupMembership2 = buildGroupMembership(null, "BRUCE@test.linkedin.com", "BRUCE", "Willis");
    Map<CompoundKey, PatchRequest<GroupMembership>> patchInputs = new HashMap<>();
    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(), "ALFRED@test.linkedin.com");
    Assert.assertEquals(entity.getResults().get(key1).getEntity().getFirstName(), "ALFRED");
    Assert.assertEquals(entity.getResults().get(key2).getEntity().getContactEmail(), "BRUCE@test.linkedin.com");
    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);
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) RestliRequestOptions(com.linkedin.restli.client.RestliRequestOptions) CompoundKey(com.linkedin.restli.common.CompoundKey) HashMap(java.util.HashMap) CollectionResponse(com.linkedin.restli.common.CollectionResponse) GroupMembershipsRequestBuilders(com.linkedin.restli.examples.groups.client.GroupMembershipsRequestBuilders) GroupMembership(com.linkedin.restli.examples.groups.api.GroupMembership) ComplexKeyGroupMembership(com.linkedin.restli.examples.groups.api.ComplexKeyGroupMembership) PatchRequest(com.linkedin.restli.common.PatchRequest) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) EntityResponse(com.linkedin.restli.common.EntityResponse) Test(org.testng.annotations.Test)

Example 18 with PatchRequest

use of com.linkedin.restli.common.PatchRequest in project rest.li by linkedin.

the class TestMaxBatchSize method testBatchPartialUpdateWithMaxBatchSizeBeyondLimitation.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestBuilderDataProvider")
public void testBatchPartialUpdateWithMaxBatchSizeBeyondLimitation(RootBuilderWrapper<Long, Greeting> builders) throws RemoteInvocationException {
    Greeting patchedGreetingOne = new Greeting().setTone(Tone.INSULTING).setId(1l).setMessage("Hello");
    Greeting patchedGreetingTwo = new Greeting().setTone(Tone.FRIENDLY).setId(2l).setMessage("Hi");
    Greeting patchedGreetingThree = new Greeting().setTone(Tone.SINCERE).setId(3l).setMessage("Hello world");
    Map<Long, PatchRequest<Greeting>> patchInputs = new HashMap<>();
    patchInputs.put(1l, PatchGenerator.diff(GREETING_ONE, patchedGreetingOne));
    patchInputs.put(2l, PatchGenerator.diff(GREETING_TWO, patchedGreetingTwo));
    patchInputs.put(3l, PatchGenerator.diff(GREETING_THREE, patchedGreetingThree));
    Request<BatchKVResponse<Long, UpdateStatus>> request = builders.batchPartialUpdate().patchInputs(patchInputs).build();
    try {
        getClient().sendRequest(request).getResponse();
        Assert.fail("The batch size is larger than the allowed max batch size should cause an exception.");
    } catch (RestLiResponseException e) {
        Assert.assertEquals(e.getStatus(), HttpStatus.S_400_BAD_REQUEST.getCode());
        Assert.assertEquals(e.getServiceErrorMessage(), "The request batch size: " + "3 is larger than the allowed max batch size: 2 for method: batch_partial_update");
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) HashMap(java.util.HashMap) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) PatchRequest(com.linkedin.restli.common.PatchRequest) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) Test(org.testng.annotations.Test)

Example 19 with PatchRequest

use of com.linkedin.restli.common.PatchRequest in project rest.li by linkedin.

the class TestMaxBatchSize method testBatchPartialUpdateWithMaxBatchSizeUnderLimitation.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestBuilderDataProvider")
public void testBatchPartialUpdateWithMaxBatchSizeUnderLimitation(RootBuilderWrapper<Long, Greeting> builders) throws RemoteInvocationException {
    Greeting patchedGreetingOne = new Greeting().setTone(Tone.INSULTING).setId(1l).setMessage("Hello");
    Greeting patchedGreetingTwo = new Greeting().setTone(Tone.FRIENDLY).setId(2l).setMessage("Hi");
    Map<Long, PatchRequest<Greeting>> patchInputs = new HashMap<>();
    patchInputs.put(1l, PatchGenerator.diff(GREETING_ONE, patchedGreetingOne));
    patchInputs.put(2l, PatchGenerator.diff(GREETING_TWO, patchedGreetingTwo));
    Request<BatchKVResponse<Long, UpdateStatus>> request = builders.batchPartialUpdate().patchInputs(patchInputs).build();
    Response<BatchKVResponse<Long, UpdateStatus>> response = getClient().sendRequest(request).getResponse();
    Assert.assertEquals(response.getStatus(), 200);
    Assert.assertEquals(response.getEntity().getResults().keySet().size(), 2);
    Assert.assertEquals(response.getEntity().getResults().get(1l).getStatus().intValue(), 204);
    Assert.assertEquals(response.getEntity().getResults().get(2l).getStatus().intValue(), 204);
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) HashMap(java.util.HashMap) PatchRequest(com.linkedin.restli.common.PatchRequest) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) Test(org.testng.annotations.Test)

Example 20 with PatchRequest

use of com.linkedin.restli.common.PatchRequest in project rest.li by linkedin.

the class TestCustomTypesClient method testCollectionBatchPartialUpdate.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "request2BuilderDataProvider")
public void testCollectionBatchPartialUpdate(RootBuilderWrapper<CustomLong, Greeting> builders) throws RemoteInvocationException {
    RequestBuilder<? extends Request<BatchKVResponse<CustomLong, UpdateStatus>>> request = builders.batchPartialUpdate().input(new CustomLong(1L), new PatchRequest<>()).input(new CustomLong(2L), new PatchRequest<>()).getBuilder();
    Map<CustomLong, UpdateStatus> statuses = getClient().sendRequest(request).getResponse().getEntity().getResults();
    Assert.assertEquals(statuses.size(), 2);
    Assert.assertEquals(statuses.get(new CustomLong(1L)).getStatus().intValue(), HttpStatus.S_204_NO_CONTENT.getCode());
    Assert.assertEquals(statuses.get(new CustomLong(2L)).getStatus().intValue(), HttpStatus.S_204_NO_CONTENT.getCode());
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) CustomLong(com.linkedin.restli.examples.custom.types.CustomLong) PatchRequest(com.linkedin.restli.common.PatchRequest) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) Test(org.testng.annotations.Test)

Aggregations

PatchRequest (com.linkedin.restli.common.PatchRequest)32 HashMap (java.util.HashMap)26 Test (org.testng.annotations.Test)22 Map (java.util.Map)14 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)11 DataMap (com.linkedin.data.DataMap)9 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)8 UpdateStatus (com.linkedin.restli.common.UpdateStatus)6 BatchPatchRequest (com.linkedin.restli.server.BatchPatchRequest)6 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)5 CompoundKey (com.linkedin.restli.common.CompoundKey)5 CustomLong (com.linkedin.restli.examples.custom.types.CustomLong)5 CreateGreeting (com.linkedin.restli.examples.greetings.client.CreateGreeting)5 PartialUpdateGreeting (com.linkedin.restli.examples.greetings.client.PartialUpdateGreeting)5 PartialUpdateGreetingFluentClient (com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingFluentClient)5 ValidationResult (com.linkedin.data.schema.validation.ValidationResult)4 StringMap (com.linkedin.data.template.StringMap)4 KeyValueRecord (com.linkedin.restli.common.KeyValueRecord)4 BatchUpdateResult (com.linkedin.restli.server.BatchUpdateResult)4 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)4