Search in sources :

Example 46 with ErrorResponse

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

the class TestDefaultScatterGatherStrategy method testGatherBatchResponse.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "protocol")
public void testGatherBatchResponse(ProtocolVersion version) {
    Map<RequestInfo, Response<BatchResponse<TestRecord>>> successResponses = new HashMap<>();
    successResponses.put(new RequestInfo(createBatchGetRequest(1L, 2L), getTargetHostRequestContext(_host1URI)), createBatchResponse(Collections.singleton(1L), Collections.singleton(2L)));
    Map<RequestInfo, Throwable> failResponses = new HashMap<>();
    failResponses.put(new RequestInfo(createBatchGetRequest(3L), getTargetHostRequestContext(_host2URI)), new RestLiScatterGatherException("Partition host is unavailable!"));
    Callback<Response<BatchResponse<TestRecord>>> testCallback = new Callback<Response<BatchResponse<TestRecord>>>() {

        @Override
        public void onError(Throwable e) {
        }

        @Override
        public void onSuccess(Response<BatchResponse<TestRecord>> result) {
            Assert.assertNotNull(result.getEntity());
            Assert.assertEquals(result.getStatus(), HttpStatus.S_200_OK.getCode());
            Assert.assertTrue(result.getEntity().getResults().size() == 1);
            Assert.assertTrue(result.getEntity().getResults().containsKey("1"));
            Assert.assertTrue(result.getEntity().getErrors().size() == 3);
            ErrorResponse keyError = result.getEntity().getErrors().get("2");
            Assert.assertEquals(keyError.getStatus().intValue(), HttpStatus.S_404_NOT_FOUND.getCode());
            ErrorResponse failError = result.getEntity().getErrors().get("3");
            Assert.assertEquals(failError.getExceptionClass(), RestLiScatterGatherException.class.getName());
            Assert.assertEquals(failError.getMessage(), "Partition host is unavailable!");
            ErrorResponse unmappedError = result.getEntity().getErrors().get("4");
            Assert.assertEquals(unmappedError.getExceptionClass(), RestLiScatterGatherException.class.getName());
            Assert.assertEquals(unmappedError.getMessage(), "Unable to find a host for keys :[4]");
        }
    };
    _sgStrategy.onAllResponsesReceived(_batchGetRequest, version, successResponses, failResponses, _unmappedKeys, testCallback);
}
Also used : BatchEntityResponse(com.linkedin.restli.internal.client.response.BatchEntityResponse) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) BatchUpdateEntityResponse(com.linkedin.restli.internal.client.response.BatchUpdateEntityResponse) EntityResponse(com.linkedin.restli.common.EntityResponse) ErrorResponse(com.linkedin.restli.common.ErrorResponse) BatchResponse(com.linkedin.restli.common.BatchResponse) Callback(com.linkedin.common.callback.Callback) BatchResponse(com.linkedin.restli.common.BatchResponse) TestRecord(com.linkedin.restli.client.test.TestRecord) ErrorResponse(com.linkedin.restli.common.ErrorResponse) Test(org.testng.annotations.Test)

Example 47 with ErrorResponse

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

the class TestDefaultScatterGatherStrategy method createBatchResponse.

private static Response<BatchResponse<TestRecord>> createBatchResponse(Set<Long> resultKeys, Set<Long> errorKeys) {
    DataMap resultMap = new DataMap();
    for (Long id : resultKeys) {
        resultMap.put(id.toString(), new TestRecord().setId(id).data());
    }
    DataMap errorMap = new DataMap();
    for (Long id : errorKeys) {
        errorMap.put(id.toString(), new ErrorResponse().setStatus(HttpStatus.S_404_NOT_FOUND.getCode()).data());
    }
    DataMap responseMap = new DataMap();
    responseMap.put(BatchResponse.RESULTS, resultMap);
    responseMap.put(BatchResponse.ERRORS, errorMap);
    BatchResponse<TestRecord> response = new BatchResponse<>(responseMap, TestRecord.class);
    return new ResponseImpl<>(HttpStatus.S_200_OK.getCode(), Collections.emptyMap(), Collections.emptyList(), response, null);
}
Also used : BatchResponse(com.linkedin.restli.common.BatchResponse) TestRecord(com.linkedin.restli.client.test.TestRecord) ResponseImpl(com.linkedin.restli.internal.client.ResponseImpl) DataMap(com.linkedin.data.DataMap) ErrorResponse(com.linkedin.restli.common.ErrorResponse)

Example 48 with ErrorResponse

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

the class RestClientTest method testRestLiResponseFuture.

@SuppressWarnings("deprecation")
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "sendRequestAndGetResponseOptions")
public void testRestLiResponseFuture(SendRequestOption sendRequestOption, GetResponseOption getResponseOption, TimeoutOption timeoutOption, ProtocolVersionOption versionOption, ProtocolVersion protocolVersion, String errorResponseHeaderName, ContentType contentType) 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;
    final String CODE = "INVALID_INPUT";
    final String DOC_URL = "https://example.com/errors/invalid-input";
    final String REQUEST_ID = "abc123";
    RestClient client = mockClient(ERR_KEY, ERR_VALUE, ERR_MSG, HTTP_CODE, APP_CODE, CODE, DOC_URL, REQUEST_ID, protocolVersion, errorResponseHeaderName);
    Request<ErrorResponse> request = mockRequest(ErrorResponse.class, versionOption, contentType);
    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());
    Assert.assertEquals(CODE, e.getCode());
    Assert.assertEquals(DOC_URL, e.getDocUrl());
    Assert.assertEquals(REQUEST_ID, e.getRequestId());
    Assert.assertEquals(EmptyRecord.class.getCanonicalName(), e.getErrorDetailType());
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) RestRequest(com.linkedin.r2.message.rest.RestRequest) ErrorResponse(com.linkedin.restli.common.ErrorResponse) Test(org.testng.annotations.Test)

Example 49 with ErrorResponse

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

the class RestClientTest method mockClient.

@SuppressWarnings("deprecation")
private RestClient mockClient(String errKey, String errValue, String errMsg, int httpCode, int appCode, String code, String docUrl, String requestId, ProtocolVersion protocolVersion, String errorResponseHeaderName) {
    ErrorResponse er = new ErrorResponse();
    DataMap errMap = new DataMap();
    errMap.put(errKey, errValue);
    er.setErrorDetails(new ErrorDetails(errMap));
    er.setErrorDetailType(EmptyRecord.class.getCanonicalName());
    er.setStatus(httpCode);
    er.setMessage(errMsg);
    er.setServiceErrorCode(appCode);
    er.setCode(code);
    er.setDocUrl(docUrl);
    er.setRequestId(requestId);
    Map<String, String> headers = new HashMap<>();
    headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString());
    headers.put(errorResponseHeaderName, RestConstants.HEADER_VALUE_ERROR);
    byte[] mapBytes;
    try {
        mapBytes = DataMapConverter.getContentType(headers).getCodec().mapToBytes(er.data());
    } catch (IOException | MimeTypeParseException e) {
        throw new RuntimeException(e);
    }
    return new RestClient(new MyMockClient(httpCode, headers, mapBytes), "http://localhost");
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) MimeTypeParseException(javax.activation.MimeTypeParseException) HashMap(java.util.HashMap) ErrorDetails(com.linkedin.restli.common.ErrorDetails) IOException(java.io.IOException) ErrorResponse(com.linkedin.restli.common.ErrorResponse) DataMap(com.linkedin.data.DataMap)

Example 50 with ErrorResponse

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

the class IndividualResponseException method createErrorResponse.

private static ErrorResponse createErrorResponse(HttpStatus status, String message, ErrorResponseBuilder errorResponseBuilder) {
    ErrorResponse errorResponse = new ErrorResponse();
    ErrorResponseFormat errorResponseFormat = errorResponseBuilder.getErrorResponseFormat();
    if (errorResponseFormat.showStatusCodeInBody()) {
        errorResponse.setStatus(status.getCode());
    }
    if (errorResponseFormat.showMessage()) {
        errorResponse.setMessage(message);
    }
    return errorResponse;
}
Also used : ErrorResponseFormat(com.linkedin.restli.server.ErrorResponseFormat) ErrorResponse(com.linkedin.restli.common.ErrorResponse)

Aggregations

ErrorResponse (com.linkedin.restli.common.ErrorResponse)67 Test (org.testng.annotations.Test)35 DataMap (com.linkedin.data.DataMap)19 HashMap (java.util.HashMap)15 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)14 RestResponse (com.linkedin.r2.message.rest.RestResponse)9 RestRequest (com.linkedin.r2.message.rest.RestRequest)8 EntityResponse (com.linkedin.restli.common.EntityResponse)8 ResponseImpl (com.linkedin.restli.internal.client.ResponseImpl)8 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)8 RestException (com.linkedin.r2.message.rest.RestException)7 TestRecord (com.linkedin.restli.client.test.TestRecord)7 Callback (com.linkedin.common.callback.Callback)6 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)6 BatchResponse (com.linkedin.restli.common.BatchResponse)6 EmptyRecord (com.linkedin.restli.common.EmptyRecord)6 IOException (java.io.IOException)6 RequestContext (com.linkedin.r2.message.RequestContext)5 BatchCollectionResponse (com.linkedin.restli.common.BatchCollectionResponse)5 BatchFinderCriteriaResult (com.linkedin.restli.common.BatchFinderCriteriaResult)5