Search in sources :

Example 1 with UpdateStatus

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

the class BatchUpdateResponseDecoder method wrapResponse.

@Override
public BatchKVResponse<K, UpdateStatus> wrapResponse(DataMap dataMap, Map<String, String> headers, ProtocolVersion version) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, IOException {
    if (dataMap == null) {
        return null;
    }
    final DataMap mergedResults = new DataMap();
    final DataMap inputResults = dataMap.containsKey(BatchResponse.RESULTS) ? dataMap.getDataMap(BatchResponse.RESULTS) : new DataMap();
    final DataMap inputErrors = dataMap.containsKey(BatchResponse.ERRORS) ? dataMap.getDataMap(BatchResponse.ERRORS) : new DataMap();
    final Set<String> mergedKeys = new HashSet<String>(inputResults.keySet());
    mergedKeys.addAll(inputErrors.keySet());
    for (String key : mergedKeys) {
        // DataMap for UpdateStatus
        final DataMap updateData;
        // status field is mandatory
        if (inputResults.containsKey(key)) {
            updateData = inputResults.getDataMap(key);
        } else {
            updateData = new DataMap();
        }
        // DataMap for ErrorResponse
        final DataMap errorData = (DataMap) inputErrors.get(key);
        if (errorData != null) {
            // The status from ErrorResponse overwrites the one in UpdateResponse. However, results and
            // errors are not expected to have overlapping key. See BatchUpdateResponseBuilder.
            updateData.put("status", errorData.get("status"));
            updateData.put("error", errorData);
        }
        mergedResults.put(key, updateData);
    }
    final DataMap responseData = new DataMap();
    responseData.put(BatchKVResponse.RESULTS, mergedResults);
    responseData.put(BatchKVResponse.ERRORS, inputErrors);
    return new BatchKVResponse<K, UpdateStatus>(responseData, _keyType, new TypeSpec<UpdateStatus>(UpdateStatus.class), _keyParts, _complexKeyType, version);
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) DataMap(com.linkedin.data.DataMap) HashSet(java.util.HashSet)

Example 2 with UpdateStatus

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

the class TestBatchUpdateResponseBuilder method unsupportedNullKeyMapTest.

/* 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 unsupportedNullKeyMapTest(Object results, ProtocolVersion protocolVersion, Map<String, UpdateStatus> expectedResults) {
    ResourceContext 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 responseData = batchUpdateResponseBuilder.buildRestLiResponseData(null, routingResult, results, headers, Collections.<HttpCookie>emptyList());
    PartialRestResponse 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 : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) UpdateStatus(com.linkedin.restli.common.UpdateStatus) BatchResponse(com.linkedin.restli.common.BatchResponse) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) Test(org.testng.annotations.Test)

Example 3 with UpdateStatus

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

the class BatchUpdateResponseBuilder method generateResultEntityResponse.

// Updates the merged results with context errors and build map of UpdateStatus.
private void generateResultEntityResponse(RoutingResult routingResult, Map<Object, BatchResponseEntry> responses, Map<Object, UpdateStatus> mergedResults) {
    for (Map.Entry<?, BatchResponseEntry> entry : responses.entrySet()) {
        if (entry.getKey() == null || entry.getValue() == null) {
            throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null errors Map found inside of the result returned by the resource method: " + routingResult.getResourceMethod());
        }
        UpdateStatus status = entry.getValue().getRecord() instanceof UpdateStatus ? (UpdateStatus) entry.getValue().getRecord() : new UpdateStatus();
        status.setStatus(entry.getValue().getStatus().getCode());
        if (entry.getValue().hasException()) {
            status.setError(_errorResponseBuilder.buildErrorResponse(entry.getValue().getException()));
        }
        mergedResults.put(entry.getKey(), status);
    }
}
Also used : BatchResponseEntry(com.linkedin.restli.internal.server.response.BatchResponseEnvelope.BatchResponseEntry) UpdateStatus(com.linkedin.restli.common.UpdateStatus) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map)

Example 4 with UpdateStatus

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

the class BatchUpdateResponseBuilder method buildRestLiResponseData.

@Override
public RestLiResponseData buildRestLiResponseData(RestRequest request, RoutingResult routingResult, Object result, Map<String, String> headers, List<HttpCookie> cookies) {
    @SuppressWarnings({ "unchecked" }) final BatchUpdateResult<Object, ?> /** constrained by signature of {@link com.linkedin.restli.server.resources.CollectionResource#batchUpdate(java.util.Map)} */
    updateResult = (BatchUpdateResult<Object, ?>) result;
    final Map<Object, UpdateResponse> results = updateResult.getResults();
    //Verify the map is not null. If so, this is a developer error.
    if (results == null) {
        throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null Map found inside of the BatchUpdateResult returned by the resource method: " + routingResult.getResourceMethod());
    }
    final Map<Object, RestLiServiceException> serviceErrors = updateResult.getErrors();
    if (serviceErrors == null) {
        throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null errors Map found inside of the BatchUpdateResult returned by the resource method: " + routingResult.getResourceMethod());
    }
    Map<Object, BatchResponseEntry> batchResponseMap = new HashMap<Object, BatchResponseEntry>();
    for (Map.Entry<Object, UpdateResponse> entry : results.entrySet()) {
        if (entry.getKey() == null) {
            throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null key inside of the Map returned inside of the BatchUpdateResult returned by the resource method: " + routingResult.getResourceMethod());
        }
        if (!serviceErrors.containsKey(entry.getKey())) {
            Object finalKey = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(entry.getKey(), routingResult);
            batchResponseMap.put(finalKey, new BatchResponseEntry(entry.getValue().getStatus(), new UpdateStatus()));
        }
    }
    for (Map.Entry<Object, RestLiServiceException> entry : serviceErrors.entrySet()) {
        if (entry.getKey() == null || entry.getValue() == null) {
            throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null key or value inside of the Map returned inside of the BatchUpdateResult returned by the resource method: " + routingResult.getResourceMethod());
        }
        Object finalKey = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(entry.getKey(), routingResult);
        batchResponseMap.put(finalKey, new BatchResponseEntry(entry.getValue().getStatus(), entry.getValue()));
    }
    for (Map.Entry<Object, RestLiServiceException> entry : ((ServerResourceContext) routingResult.getContext()).getBatchKeyErrors().entrySet()) {
        Object finalKey = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(entry.getKey(), routingResult);
        batchResponseMap.put(finalKey, new BatchResponseEntry(entry.getValue().getStatus(), entry.getValue()));
    }
    RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(HttpStatus.S_200_OK, headers, cookies);
    responseData.setResponseEnvelope(new BatchUpdateResponseEnvelope(batchResponseMap, responseData));
    return responseData;
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) HashMap(java.util.HashMap) UpdateResponse(com.linkedin.restli.server.UpdateResponse) BatchResponseEntry(com.linkedin.restli.internal.server.response.BatchResponseEnvelope.BatchResponseEntry) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map)

Example 5 with UpdateStatus

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

the class TestRestLiScatterGather method testSendSGUpdateRequests.

// BatchUpdateRequest
private static void testSendSGUpdateRequests(RestClient restClient, Map<Long, Greeting> inputs, RootBuilderWrapper<Long, Greeting> builders) throws RemoteInvocationException {
    @SuppressWarnings("unchecked") BatchUpdateRequest<Long, Greeting> request = (BatchUpdateRequest<Long, Greeting>) builders.batchUpdate().inputs(inputs).setParam("foo", "bar").build();
    BatchKVResponse<Long, UpdateStatus> result = restClient.sendRequest(request).getResponse().getEntity();
    Assert.assertEquals(result.getResults().size(), inputs.size());
    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