Search in sources :

Example 36 with UpdateStatus

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

the class TestParseqBasedFluentClientApi method testBatchUpdateWithErrors.

@Test
public void testBatchUpdateWithErrors() throws Exception {
    Greetings greetings = new GreetingsFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
    final Map<Long, Greeting> inputs = new HashMap<>();
    inputs.put(-6L, getGreeting());
    CompletionStage<Long> createResult = greetings.create(getGreeting());
    CompletionStage<Map<Long, UpdateStatus>> result = createResult.thenCompose(id -> {
        inputs.put(id, getGreeting("Batch update test"));
        return greetings.batchUpdate(inputs);
    });
    CompletableFuture<Map<Long, UpdateStatus>> future = result.toCompletableFuture();
    Map<Long, UpdateStatus> ids = future.get(5000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(ids.size(), 2);
    Assert.assertEquals(ids.get(createResult.toCompletableFuture().get()).getStatus().intValue(), 204);
    Assert.assertEquals(ids.get(-6L).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) UpdateStatus(com.linkedin.restli.common.UpdateStatus) HashMap(java.util.HashMap) CustomLong(com.linkedin.restli.examples.custom.types.CustomLong) Greetings(com.linkedin.restli.examples.greetings.client.Greetings) GreetingsFluentClient(com.linkedin.restli.examples.greetings.client.GreetingsFluentClient) Map(java.util.Map) HashMap(java.util.HashMap) StringMap(com.linkedin.data.template.StringMap) Test(org.testng.annotations.Test)

Example 37 with UpdateStatus

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

the class TestDefaultScatterGatherStrategy method createBatchKVResponse.

private static Response<BatchKVResponse<Long, UpdateStatus>> createBatchKVResponse(ProtocolVersion version, Set<Long> resultKeys, Set<Long> errorKeys) {
    DataMap resultMap = new DataMap();
    DataMap errorMap = new DataMap();
    for (Long id : resultKeys) {
        resultMap.put(id.toString(), new UpdateStatus().setStatus(HttpStatus.S_200_OK.getCode()).data());
    }
    for (Long id : errorKeys) {
        ErrorResponse err = new ErrorResponse().setStatus(HttpStatus.S_404_NOT_FOUND.getCode());
        errorMap.put(id.toString(), err.data());
    }
    DataMap responseMap = new DataMap();
    responseMap.put(BatchResponse.RESULTS, resultMap);
    responseMap.put(BatchResponse.ERRORS, errorMap);
    DataMap mergedMap = ResponseDecoderUtil.mergeUpdateStatusResponseData(responseMap);
    BatchKVResponse<Long, UpdateStatus> response = new BatchKVResponse<>(mergedMap, new TypeSpec<>(Long.class), new TypeSpec<>(UpdateStatus.class), Collections.emptyMap(), version);
    return new ResponseImpl<>(HttpStatus.S_200_OK.getCode(), Collections.emptyMap(), Collections.emptyList(), response, null);
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) ResponseImpl(com.linkedin.restli.internal.client.ResponseImpl) DataMap(com.linkedin.data.DataMap) ErrorResponse(com.linkedin.restli.common.ErrorResponse)

Example 38 with UpdateStatus

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

the class TestBatchUpdateResponseBuilder method testUnsupportedNullKeyMap.

/* Note that we use also need to test using java.util.concurrent.ConcurrentHashMap. This is because rest.li checks
  * for the presence of nulls returned from maps which are returned from resource methods. The checking for nulls
  * is prone to a NullPointerException since contains(null) can throw an NPE from certain map implementations such as
  * java.util.concurrent.ConcurrentHashMap. We want to make sure our check for the presence of nulls is done in a
  * way that doesn't throw an NullPointerException.
  */
@Test(dataProvider = "unsupportedNullKeyMapData")
@SuppressWarnings("unchecked")
public void testUnsupportedNullKeyMap(Object results, ProtocolVersion protocolVersion, Map<String, UpdateStatus> expectedResults) {
    ServerResourceContext mockContext = getMockResourceContext(protocolVersion, null);
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
    RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
    Map<String, String> headers = ResponseBuilderUtil.getHeaders();
    BatchUpdateResponseBuilder batchUpdateResponseBuilder = new BatchUpdateResponseBuilder(new ErrorResponseBuilder());
    RestLiResponseData<BatchUpdateResponseEnvelope> responseData = batchUpdateResponseBuilder.buildRestLiResponseData(null, routingResult, results, headers, Collections.emptyList());
    RestLiResponse restResponse = batchUpdateResponseBuilder.buildResponse(routingResult, responseData);
    BatchResponse<UpdateStatus> batchResponse = (BatchResponse<UpdateStatus>) restResponse.getEntity();
    EasyMock.verify(mockContext, mockDescriptor);
    ResponseBuilderUtil.validateHeaders(restResponse, headers);
    Assert.assertEquals(batchResponse.getResults(), expectedResults);
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) BatchResponse(com.linkedin.restli.common.BatchResponse) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) Test(org.testng.annotations.Test)

Example 39 with UpdateStatus

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

the class TestComplexKeysResource method testBatchUpdateMain.

private void testBatchUpdateMain(RootBuilderWrapper.MethodBuilderWrapper<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message, BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus>> requestBuilder, BatchGetEntityRequestBuilder<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchGetRequestBuilder) throws RemoteInvocationException {
    final String messageText = StringTestKeys.SIMPLEKEY + " " + StringTestKeys.SIMPLEKEY2;
    final Message message = new Message();
    message.setId(StringTestKeys.SIMPLEKEY + " " + StringTestKeys.SIMPLEKEY2);
    message.setMessage(messageText);
    message.setTone(Tone.INSULTING);
    final String messageText2 = StringTestKeys.URL + " " + StringTestKeys.URL2;
    final Message message2 = new Message();
    message2.setId(StringTestKeys.URL + " " + StringTestKeys.URL2);
    message2.setMessage(messageText2);
    message2.setTone(Tone.INSULTING);
    final Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> inputs = new HashMap<>();
    ComplexResourceKey<TwoPartKey, TwoPartKey> key1 = getComplexKey(StringTestKeys.SIMPLEKEY, StringTestKeys.SIMPLEKEY2);
    ComplexResourceKey<TwoPartKey, TwoPartKey> key2 = getComplexKey(StringTestKeys.URL, StringTestKeys.URL2);
    ComplexResourceKey<TwoPartKey, TwoPartKey> key3 = getComplexKey(StringTestKeys.ERROR, StringTestKeys.ERROR);
    inputs.put(key1, message);
    inputs.put(key2, message2);
    // key3 should error anyway, so message is irrelevant.
    inputs.put(key3, message);
    final Request<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus>> request = requestBuilder.inputs(inputs).build();
    final ResponseFuture<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus>> future = getClient().sendRequest(request);
    final BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus> response = future.getResponse().getEntity();
    Assert.assertEquals(response.getResults().size(), inputs.size());
    for (Map.Entry<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus> resp : response.getResults().entrySet()) {
        Assert.assertTrue(inputs.containsKey(resp.getKey()));
        final UpdateStatus status = resp.getValue();
        if (status.hasError()) {
            Assert.assertTrue(status.getStatus() == status.getError().getStatus(), "Update status should match the status of the error, if there is any.");
        } else {
            Assert.assertEquals((int) status.getStatus(), 200);
        }
    }
    Assert.assertNotNull(response.getResults().get(key1));
    Assert.assertNotNull(response.getResults().get(key2));
    Assert.assertNotNull(response.getErrors().get(key3));
    ArrayList<ComplexResourceKey<TwoPartKey, TwoPartKey>> ids = new ArrayList<>();
    ids.add(key1);
    ids.add(key2);
    BatchGetEntityRequest<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchGetRequest = batchGetRequestBuilder.ids(ids).build();
    ResponseFuture<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>>> batchGetFuture = getClient().sendRequest(batchGetRequest);
    BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>> batchGetResponse = batchGetFuture.getResponse().getEntity();
    Assert.assertEquals(batchGetResponse.getResults().get(key1).getEntity().getTone(), Tone.INSULTING);
    Assert.assertEquals(batchGetResponse.getResults().get(key2).getEntity().getTone(), Tone.INSULTING);
}
Also used : TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) UpdateStatus(com.linkedin.restli.common.UpdateStatus) Message(com.linkedin.restli.examples.greetings.api.Message) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) EntityResponse(com.linkedin.restli.common.EntityResponse) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) Map(java.util.Map) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap)

Example 40 with UpdateStatus

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

the class TestRestLiScatterGather method testSendSGDeleteRequests.

// BatchDeleteRequest
private static void testSendSGDeleteRequests(RestClient restClient, Long[] requestIds, RootBuilderWrapper<Long, Greeting> builders) throws RemoteInvocationException {
    @SuppressWarnings("unchecked") BatchDeleteRequest<Long, Greeting> request = (BatchDeleteRequest<Long, Greeting>) builders.batchDelete().ids(requestIds).setParam("foo", "bar").build();
    BatchKVResponse<Long, UpdateStatus> result = restClient.sendRequest(request).getResponse().getEntity();
    Assert.assertEquals(result.getResults().size(), requestIds.length);
    UpdateStatus item = result.getResults().values().iterator().next();
    Assert.assertFalse(item.hasError());
    Assert.assertEquals(result.getErrors().size(), 0);
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) UpdateStatus(com.linkedin.restli.common.UpdateStatus)

Aggregations

UpdateStatus (com.linkedin.restli.common.UpdateStatus)42 HashMap (java.util.HashMap)27 Test (org.testng.annotations.Test)22 Map (java.util.Map)18 DataMap (com.linkedin.data.DataMap)16 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)15 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)12 ArrayList (java.util.ArrayList)11 CustomLong (com.linkedin.restli.examples.custom.types.CustomLong)8 BatchResponse (com.linkedin.restli.common.BatchResponse)7 CompoundKey (com.linkedin.restli.common.CompoundKey)7 EntityResponse (com.linkedin.restli.common.EntityResponse)7 PatchRequest (com.linkedin.restli.common.PatchRequest)6 BatchResponseEntry (com.linkedin.restli.internal.server.response.BatchResponseEnvelope.BatchResponseEntry)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)5 ErrorResponse (com.linkedin.restli.common.ErrorResponse)5 Message (com.linkedin.restli.examples.greetings.api.Message)5 TwoPartKey (com.linkedin.restli.examples.greetings.api.TwoPartKey)5 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)5