Search in sources :

Example 31 with ErrorResponse

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

the class TestMockBatchKVResponseFactory method complexKeyData.

@DataProvider(name = "complexKey")
public Object[][] complexKeyData() {
    Map<ComplexResourceKey<Greeting, Greeting>, Greeting> recordTemplates = new HashMap<ComplexResourceKey<Greeting, Greeting>, Greeting>();
    Map<ComplexResourceKey<Greeting, Greeting>, ErrorResponse> errorResponses = new HashMap<ComplexResourceKey<Greeting, Greeting>, ErrorResponse>();
    Greeting g1 = buildGreeting(1L);
    Greeting g2 = buildGreeting(2L);
    Greeting g3 = buildGreeting(3L);
    recordTemplates.put(new ComplexResourceKey<Greeting, Greeting>(g1, g1), g1);
    recordTemplates.put(new ComplexResourceKey<Greeting, Greeting>(g2, g2), g2);
    errorResponses.put(new ComplexResourceKey<Greeting, Greeting>(g3, g3), new ErrorResponse().setMessage("3"));
    Map<ComplexResourceKey<Greeting, Greeting>, HttpStatus> statuses = new HashMap<ComplexResourceKey<Greeting, Greeting>, HttpStatus>();
    statuses.put(new ComplexResourceKey<Greeting, Greeting>(g1, g1), HttpStatus.S_200_OK);
    statuses.put(new ComplexResourceKey<Greeting, Greeting>(g2, g2), HttpStatus.S_200_OK);
    statuses.put(new ComplexResourceKey<Greeting, Greeting>(g3, g3), HttpStatus.S_500_INTERNAL_SERVER_ERROR);
    // Strip the parameters from complex keys in expected results and expected errors.
    Map<ComplexResourceKey<Greeting, Greeting>, Greeting> expectedRecordTemplates = new HashMap<ComplexResourceKey<Greeting, Greeting>, Greeting>();
    expectedRecordTemplates.put(new ComplexResourceKey<Greeting, Greeting>(g1, new Greeting()), recordTemplates.get(new ComplexResourceKey<Greeting, Greeting>(g1, g1)));
    expectedRecordTemplates.put(new ComplexResourceKey<Greeting, Greeting>(g2, new Greeting()), recordTemplates.get(new ComplexResourceKey<Greeting, Greeting>(g2, g2)));
    Map<ComplexResourceKey<Greeting, Greeting>, EntityResponse<Greeting>> expectedResults = new HashMap<ComplexResourceKey<Greeting, Greeting>, EntityResponse<Greeting>>();
    expectedResults.put(new ComplexResourceKey<Greeting, Greeting>(g1, new Greeting()), buildEntityResponse(recordTemplates.get(new ComplexResourceKey<Greeting, Greeting>(g1, g1)), HttpStatus.S_200_OK, null));
    expectedResults.put(new ComplexResourceKey<Greeting, Greeting>(g2, new Greeting()), buildEntityResponse(recordTemplates.get(new ComplexResourceKey<Greeting, Greeting>(g2, g2)), HttpStatus.S_200_OK, null));
    expectedResults.put(new ComplexResourceKey<Greeting, Greeting>(g3, new Greeting()), buildEntityResponse(null, HttpStatus.S_500_INTERNAL_SERVER_ERROR, errorResponses.get(new ComplexResourceKey<Greeting, Greeting>(g3, g3))));
    Map<ComplexResourceKey<Greeting, Greeting>, ErrorResponse> expectedErrors = new HashMap<ComplexResourceKey<Greeting, Greeting>, ErrorResponse>();
    expectedErrors.put(new ComplexResourceKey<Greeting, Greeting>(g3, new Greeting()), errorResponses.get(new ComplexResourceKey<Greeting, Greeting>(g3, g3)));
    return new Object[][] { { recordTemplates, statuses, errorResponses, expectedRecordTemplates, expectedResults, expectedErrors } };
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) HashMap(java.util.HashMap) HttpStatus(com.linkedin.restli.common.HttpStatus) EntityResponse(com.linkedin.restli.common.EntityResponse) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) ErrorResponse(com.linkedin.restli.common.ErrorResponse) DataProvider(org.testng.annotations.DataProvider)

Example 32 with ErrorResponse

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

the class TestMockRestliResponseExceptionBuilder method testOverwriteStatus.

@Test
public void testOverwriteStatus() {
    ErrorResponse noStatusErrorResponse = new ErrorResponse();
    RestLiResponseException exception = new MockRestliResponseExceptionBuilder().setErrorResponse(noStatusErrorResponse).build();
    assertEquals(exception.getStatus(), 500);
}
Also used : RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) MockRestliResponseExceptionBuilder(com.linkedin.restli.client.testutils.MockRestliResponseExceptionBuilder) ErrorResponse(com.linkedin.restli.common.ErrorResponse) Test(org.testng.annotations.Test)

Example 33 with ErrorResponse

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

the class BatchGetRequestUtil method unbatchKVResponse.

/**
   * Extract the get response for this resource out of an auto-batched batch response.
   * This is pure rest.li logic, and it complements the auto-batching logic in BatchGetRequestBuilder.
   * @throws com.linkedin.r2.RemoteInvocationException if the server returned an error response for this resource,
   * or if it returned neither a result nor an error.
   */
public static <K, V extends RecordTemplate> Response<V> unbatchKVResponse(Request<BatchKVResponse<K, V>> request, Response<BatchKVResponse<K, V>> batchResponse, K id) throws RemoteInvocationException {
    final BatchKVResponse<K, V> batchEntity = batchResponse.getEntity();
    final ErrorResponse errorResponse = batchEntity.getErrors().get(id);
    if (errorResponse != null) {
        throw new RestLiResponseException(errorResponse);
    }
    final V entityResult = batchEntity.getResults().get(id);
    if (entityResult == null) {
        throw new RestLiDecodingException("No result or error for base URI " + request.getBaseUriTemplate() + ", id " + id + ". Verify that the batchGet endpoint returns response keys that match batchGet request IDs.", null);
    }
    return new ResponseImpl<V>(batchResponse, entityResult);
}
Also used : ResponseImpl(com.linkedin.restli.internal.client.ResponseImpl) ErrorResponse(com.linkedin.restli.common.ErrorResponse)

Example 34 with ErrorResponse

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

the class BatchKVResponse method deserializeData.

protected void deserializeData(TypeSpec<K> keyType, Map<String, CompoundKey.TypeInfo> keyParts, ComplexKeySpec<?, ?> complexKeyType, ProtocolVersion version) {
    final DataMap resultsRaw = data().getDataMap(RESULTS);
    if (resultsRaw == null) {
        _results = new ParamlessKeyHashMap<V>(complexKeyType);
    } else {
        _results = new ParamlessKeyHashMap<V>(CollectionUtils.getMapInitialCapacity(resultsRaw.size(), 0.75f), 0.75f, complexKeyType);
        for (Map.Entry<String, Object> entry : resultsRaw.entrySet()) {
            @SuppressWarnings("unchecked") final K key = (K) ResponseUtils.convertKey(entry.getKey(), keyType, keyParts, complexKeyType, version);
            final V value = deserializeValue(entry.getValue());
            _results.put(key, value);
        }
    }
    final DataMap errorsRaw = data().getDataMap(ERRORS);
    if (errorsRaw == null) {
        _errors = new ParamlessKeyHashMap<ErrorResponse>(complexKeyType);
    } else {
        _errors = new ParamlessKeyHashMap<ErrorResponse>(CollectionUtils.getMapInitialCapacity(errorsRaw.size(), 0.75f), 0.75f, complexKeyType);
        for (Map.Entry<String, Object> entry : errorsRaw.entrySet()) {
            @SuppressWarnings("unchecked") final K key = (K) ResponseUtils.convertKey(entry.getKey(), keyType, keyParts, complexKeyType, version);
            final ErrorResponse value = DataTemplateUtil.wrap(entry.getValue(), ErrorResponse.class);
            _errors.put(key, value);
        }
    }
}
Also used : HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) DataMap(com.linkedin.data.DataMap) ErrorResponse(com.linkedin.restli.common.ErrorResponse)

Example 35 with ErrorResponse

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

the class RestClientTest method testRestLiResponseFuture.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "sendRequestAndGetResponseOptions")
public void testRestLiResponseFuture(SendRequestOption sendRequestOption, GetResponseOption getResponseOption, TimeoutOption timeoutOption, ProtocolVersionOption versionOption, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws ExecutionException, RemoteInvocationException, TimeoutException, InterruptedException, IOException {
    final String ERR_KEY = "someErr";
    final String ERR_VALUE = "WHOOPS!";
    final String ERR_MSG = "whoops2";
    final int HTTP_CODE = 200;
    final int APP_CODE = 666;
    RestClient client = mockClient(ERR_KEY, ERR_VALUE, ERR_MSG, HTTP_CODE, APP_CODE, protocolVersion, errorResponseHeaderName);
    Request<ErrorResponse> request = mockRequest(ErrorResponse.class, versionOption);
    RequestBuilder<Request<ErrorResponse>> requestBuilder = mockRequestBuilder(request);
    ResponseFuture<ErrorResponse> future = sendRequest(sendRequestOption, determineErrorHandlingBehavior(getResponseOption), client, request, requestBuilder);
    Response<ErrorResponse> response = getOkResponse(getResponseOption, future, timeoutOption);
    ErrorResponse e = response.getEntity();
    Assert.assertNull(response.getError());
    Assert.assertFalse(response.hasError());
    Assert.assertEquals(HTTP_CODE, response.getStatus());
    Assert.assertEquals(ERR_VALUE, e.getErrorDetails().data().getString(ERR_KEY));
    Assert.assertEquals(APP_CODE, e.getServiceErrorCode().intValue());
    Assert.assertEquals(ERR_MSG, e.getMessage());
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) ErrorResponse(com.linkedin.restli.common.ErrorResponse) Test(org.testng.annotations.Test)

Aggregations

ErrorResponse (com.linkedin.restli.common.ErrorResponse)41 Test (org.testng.annotations.Test)20 DataMap (com.linkedin.data.DataMap)12 HashMap (java.util.HashMap)12 RestResponse (com.linkedin.r2.message.rest.RestResponse)8 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)8 RestException (com.linkedin.r2.message.rest.RestException)6 RestRequest (com.linkedin.r2.message.rest.RestRequest)6 IOException (java.io.IOException)6 CompoundKey (com.linkedin.restli.common.CompoundKey)5 Map (java.util.Map)5 Callback (com.linkedin.common.callback.Callback)4 MultiPartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.MultiPartMIMEFullReaderCallback)4 SinglePartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback)4 RequestContext (com.linkedin.r2.message.RequestContext)4 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)4 StreamException (com.linkedin.r2.message.stream.StreamException)4 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)4 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)4 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)4