Search in sources :

Example 16 with UpdateStatus

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

the class TestBatchUpdateResponseBuilder method unsupportedNullKeyMapData.

@DataProvider(name = "unsupportedNullKeyMapData")
public Object[][] unsupportedNullKeyMapData() {
    final CompoundKey c1 = new CompoundKey().append("a", "a1").append("b", 1);
    final Map<CompoundKey, UpdateResponse> results = new ConcurrentHashMap<CompoundKey, UpdateResponse>();
    results.put(c1, new UpdateResponse(HttpStatus.S_202_ACCEPTED));
    final BatchUpdateResult<CompoundKey, Foo> batchUpdateResult = new BatchUpdateResult<CompoundKey, Foo>(results, new ConcurrentHashMap<CompoundKey, RestLiServiceException>());
    final UpdateStatus updateStatus = new UpdateStatus().setStatus(202);
    final Map<String, UpdateStatus> expectedProtocol1Results = new HashMap<String, UpdateStatus>();
    expectedProtocol1Results.put("a=a1&b=1", updateStatus);
    final Map<String, UpdateStatus> expectedProtocol2Results = new HashMap<String, UpdateStatus>();
    expectedProtocol2Results.put("(a:a1,b:1)", updateStatus);
    return new Object[][] { { batchUpdateResult, AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), expectedProtocol1Results }, { batchUpdateResult, AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion(), expectedProtocol2Results } };
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) CompoundKey(com.linkedin.restli.common.CompoundKey) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Foo(com.linkedin.pegasus.generator.examples.Foo) UpdateResponse(com.linkedin.restli.server.UpdateResponse) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DataProvider(org.testng.annotations.DataProvider)

Example 17 with UpdateStatus

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

the class BatchUpdateResponseBuilder method toBatchResponse.

private static <K> BatchResponse<AnyRecord> toBatchResponse(Map<K, UpdateStatus> statuses, ProtocolVersion protocolVersion) {
    final DataMap splitResponseData = new DataMap();
    final DataMap splitStatuses = new DataMap();
    final DataMap splitErrors = new DataMap();
    for (Map.Entry<K, UpdateStatus> statusEntry : statuses.entrySet()) {
        final DataMap statusData = statusEntry.getValue().data();
        final String stringKey = URIParamUtils.encodeKeyForBody(statusEntry.getKey(), false, protocolVersion);
        final DataMap error = statusData.getDataMap("error");
        if (error == null) {
            // status and error should be mutually exclusive for now
            CheckedUtil.putWithoutChecking(splitStatuses, stringKey, statusData);
        } else {
            CheckedUtil.putWithoutChecking(splitErrors, stringKey, error);
        }
    }
    CheckedUtil.putWithoutChecking(splitResponseData, BatchResponse.RESULTS, splitStatuses);
    CheckedUtil.putWithoutChecking(splitResponseData, BatchResponse.ERRORS, splitErrors);
    return new BatchResponse<AnyRecord>(splitResponseData, AnyRecord.class);
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) BatchResponse(com.linkedin.restli.common.BatchResponse) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) DataMap(com.linkedin.data.DataMap)

Example 18 with UpdateStatus

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

the class BatchUpdateResponseBuilder method buildResponse.

@Override
@SuppressWarnings("unchecked")
public PartialRestResponse buildResponse(RoutingResult routingResult, RestLiResponseData responseData) {
    Map<Object, UpdateStatus> mergedResults = new HashMap<Object, UpdateStatus>();
    final Map<Object, BatchResponseEntry> responses = (Map<Object, BatchResponseEntry>) responseData.getBatchResponseEnvelope().getBatchResponseMap();
    generateResultEntityResponse(routingResult, responses, mergedResults);
    PartialRestResponse.Builder builder = new PartialRestResponse.Builder();
    final ProtocolVersion protocolVersion = ((ServerResourceContext) routingResult.getContext()).getRestliProtocolVersion();
    @SuppressWarnings("unchecked") final BatchResponse<AnyRecord> response = toBatchResponse(mergedResults, protocolVersion);
    return builder.entity(response).headers(responseData.getHeaders()).cookies(responseData.getCookies()).build();
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) AnyRecord(com.linkedin.restli.internal.server.methods.AnyRecord) HashMap(java.util.HashMap) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) BatchResponseEntry(com.linkedin.restli.internal.server.response.BatchResponseEnvelope.BatchResponseEntry) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map)

Example 19 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>, Message>();
    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<ComplexResourceKey<TwoPartKey, TwoPartKey>>();
    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 20 with UpdateStatus

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

the class TestScatterGather method testSendSGKVRequests.

private static void testSendSGKVRequests(Collection<ScatterGatherBuilder.KVRequestInfo<Long, UpdateStatus>> scatterGatherRequests, Long[] requestIds) throws InterruptedException {
    final Map<Long, UpdateStatus> results = new ConcurrentHashMap<Long, UpdateStatus>();
    final CountDownLatch latch = new CountDownLatch(scatterGatherRequests.size());
    final List<Throwable> errors = new ArrayList<Throwable>();
    final List<BatchKVResponse<Long, UpdateStatus>> responses = new ArrayList<BatchKVResponse<Long, UpdateStatus>>();
    for (ScatterGatherBuilder.KVRequestInfo<Long, UpdateStatus> requestInfo : scatterGatherRequests) {
        Callback<Response<BatchKVResponse<Long, UpdateStatus>>> cb = new Callback<Response<BatchKVResponse<Long, UpdateStatus>>>() {

            @Override
            public void onSuccess(Response<BatchKVResponse<Long, UpdateStatus>> response) {
                results.putAll(response.getEntity().getResults());
                synchronized (responses) {
                    responses.add(response.getEntity());
                }
                latch.countDown();
            }

            @Override
            public void onError(Throwable e) {
                synchronized (errors) {
                    errors.add(e);
                }
                latch.countDown();
            }
        };
        BatchRequest<BatchKVResponse<Long, UpdateStatus>> request = requestInfo.getRequest();
        RequestContext requestContext = requestInfo.getRequestContext();
        REST_CLIENT.sendRequest(request, requestContext, cb);
    }
    latch.await();
    if (!errors.isEmpty()) {
        Assert.fail("Errors in scatter/gather: " + errors.toString());
    }
    Assert.assertEquals(results.values().size(), requestIds.length);
    Set<Set<Long>> responseIdSets = new HashSet<Set<Long>>();
    Set<Long> responseIds = new HashSet<Long>();
    for (BatchKVResponse<Long, UpdateStatus> response : responses) {
        Set<Long> theseIds = response.getResults().keySet();
        //no duplicate requests
        Assert.assertFalse(responseIdSets.contains(theseIds));
        for (Long id : theseIds) {
            //no duplicate ids
            Assert.assertFalse(responseIds.contains(id));
            responseIds.add(id);
        }
        responseIdSets.add(theseIds);
    }
    Assert.assertTrue(responseIds.containsAll(Arrays.asList(requestIds)));
    Assert.assertEquals(responseIds.size(), requestIds.length);
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) EntityResponse(com.linkedin.restli.common.EntityResponse) BatchCreateIdResponse(com.linkedin.restli.common.BatchCreateIdResponse) BatchResponse(com.linkedin.restli.common.BatchResponse) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) Callback(com.linkedin.common.callback.Callback) RequestContext(com.linkedin.r2.message.RequestContext) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashSet(java.util.HashSet)

Aggregations

UpdateStatus (com.linkedin.restli.common.UpdateStatus)25 HashMap (java.util.HashMap)17 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)15 Test (org.testng.annotations.Test)11 DataMap (com.linkedin.data.DataMap)10 Map (java.util.Map)10 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)8 ArrayList (java.util.ArrayList)8 CompoundKey (com.linkedin.restli.common.CompoundKey)5 EntityResponse (com.linkedin.restli.common.EntityResponse)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 BatchResponse (com.linkedin.restli.common.BatchResponse)4 PatchRequest (com.linkedin.restli.common.PatchRequest)4 CustomLong (com.linkedin.restli.examples.custom.types.CustomLong)4 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)4 HashSet (java.util.HashSet)4 RequestContext (com.linkedin.r2.message.RequestContext)3 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)3 ErrorResponse (com.linkedin.restli.common.ErrorResponse)3 Message (com.linkedin.restli.examples.greetings.api.Message)3