Search in sources :

Example 6 with ErrorResponse

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

the class TestBatchKVResponse method testDeserialization.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "batchKVResponseDataProvider")
public <K> void testDeserialization(List<K> keys, Class<K> keyClass, Class<? extends RecordTemplate> keyKeyClass, Class<? extends RecordTemplate> keyParamsClass, ProtocolVersion protocolVersion) {
    final K resultKey = keys.get(0);
    final K errorKey = keys.get(1);
    final String resultKeyString = URIParamUtils.encodeKeyForBody(resultKey, false, protocolVersion);
    final String errorKeyString = URIParamUtils.encodeKeyForBody(errorKey, false, protocolVersion);
    final DataMap inputResults = new DataMap();
    inputResults.put(resultKeyString, _record.data());
    final DataMap inputErrors = new DataMap();
    inputErrors.put(errorKeyString, _error.data());
    final DataMap testData = new DataMap();
    testData.put(BatchKVResponse.RESULTS, inputResults);
    testData.put(BatchKVResponse.ERRORS, inputErrors);
    final BatchKVResponse<K, TestRecord> response = new BatchKVResponse<K, TestRecord>(testData, keyClass, TestRecord.class, Collections.<String, CompoundKey.TypeInfo>emptyMap(), keyKeyClass, keyParamsClass, protocolVersion);
    final Map<K, TestRecord> outputResults = response.getResults();
    final TestRecord outRecord = outputResults.get(resultKey);
    Assert.assertEquals(outRecord, outRecord);
    final Map<K, ErrorResponse> outputErrors = response.getErrors();
    final ErrorResponse outError = outputErrors.get(errorKey);
    Assert.assertEquals(outError, _error);
}
Also used : CompoundKey(com.linkedin.restli.common.CompoundKey) TestRecord(com.linkedin.restli.client.test.TestRecord) DataMap(com.linkedin.data.DataMap) ErrorResponse(com.linkedin.restli.common.ErrorResponse) Test(org.testng.annotations.Test)

Example 7 with ErrorResponse

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

the class TestBatchEntityResponseDecoder method testDecoding.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "batchEntityResponseDataProvider")
public void testDecoding(List<String> keys, ProtocolVersion protocolVersion) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, IOException {
    final String resultKey = keys.get(0);
    final String statusKey = keys.get(1);
    final String errorKey = keys.get(2);
    final DataMap resultData = new DataMap();
    resultData.put(resultKey, _record.data());
    final DataMap statusData = new DataMap();
    statusData.put(statusKey, _status.getCode());
    final DataMap errorData = new DataMap();
    errorData.put(errorKey, _error.data());
    final DataMap data = new DataMap();
    data.put(BatchResponse.RESULTS, resultData);
    data.put(BatchResponse.STATUSES, statusData);
    data.put(BatchResponse.ERRORS, errorData);
    final BatchEntityResponseDecoder<String, TestRecord> decoder = new BatchEntityResponseDecoder<String, TestRecord>(new TypeSpec<TestRecord>(TestRecord.class), new TypeSpec<String>(String.class), Collections.<String, CompoundKey.TypeInfo>emptyMap(), null);
    final BatchKVResponse<String, EntityResponse<TestRecord>> response = decoder.wrapResponse(data, Collections.<String, String>emptyMap(), protocolVersion);
    final Map<String, EntityResponse<TestRecord>> results = response.getResults();
    final Map<String, ErrorResponse> errors = response.getErrors();
    final Collection<String> uniqueKeys = new HashSet<String>(keys);
    Assert.assertEquals(results.size(), uniqueKeys.size());
    Assert.assertEquals(errors.size(), 1);
    Assert.assertEquals(results.get(resultKey).getEntity(), _record);
    Assert.assertEquals(results.get(statusKey).getStatus(), _status);
    Assert.assertEquals(results.get(errorKey).getError(), _error);
    Assert.assertEquals(errors.get(errorKey), _error);
}
Also used : CompoundKey(com.linkedin.restli.common.CompoundKey) DataMap(com.linkedin.data.DataMap) ErrorResponse(com.linkedin.restli.common.ErrorResponse) EntityResponse(com.linkedin.restli.common.EntityResponse) TestRecord(com.linkedin.restli.client.test.TestRecord) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 8 with ErrorResponse

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

the class TestNullGreetingsClient method testBatchUpdateUnsupportedNullKeyMap.

/*
   * This test is one of the few areas in this test suite where we don't expect an exception.
   * The purpose of this test is to make sure Rest.li can handle java.util.concurrent.ConcurrentHashMap(s) sent
   * back by resource methods.
   */
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestBuilderDataProvider")
public void testBatchUpdateUnsupportedNullKeyMap(final RootBuilderWrapper<Long, Greeting> builders) throws RemoteInvocationException {
    Greeting someGreeting = new Greeting().setMessage("Hello").setTone(Tone.INSULTING);
    Request<BatchKVResponse<Long, UpdateStatus>> writeRequest = builders.batchUpdate().input(7l, someGreeting).build();
    Response<BatchKVResponse<Long, UpdateStatus>> response = getClient().sendRequest(writeRequest).getResponse();
    Map<Long, ErrorResponse> actualErrors = response.getEntity().getErrors();
    Assert.assertEquals(actualErrors.size(), 0, "Errors map should be empty");
    Map<Long, UpdateStatus> actualResults = response.getEntity().getResults();
    Map<Long, UpdateStatus> expectedResults = new HashMap<Long, UpdateStatus>();
    UpdateStatus updateStatus = new UpdateStatus().setStatus(201);
    expectedResults.put(3l, updateStatus);
    Assert.assertEquals(actualResults, expectedResults, "The results map should be correct");
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) UpdateStatus(com.linkedin.restli.common.UpdateStatus) HashMap(java.util.HashMap) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) ErrorResponse(com.linkedin.restli.common.ErrorResponse) Test(org.testng.annotations.Test)

Example 9 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) {
    ErrorResponse errorResponse = new ErrorResponse();
    errorResponse.setStatus(status.getCode());
    errorResponse.setMessage(message);
    return errorResponse;
}
Also used : ErrorResponse(com.linkedin.restli.common.ErrorResponse)

Example 10 with ErrorResponse

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

the class ErrorResponseBuilder method buildErrorResponse.

private ErrorResponse buildErrorResponse(RestLiServiceException result, ErrorResponseFormat errorResponseFormat) {
    ErrorResponse er = new ErrorResponse();
    if (errorResponseFormat.showStatusCodeInBody()) {
        er.setStatus(result.getStatus().getCode());
    }
    if (errorResponseFormat.showMessage() && result.getMessage() != null) {
        er.setMessage(result.getMessage());
    }
    if (errorResponseFormat.showServiceErrorCode() && result.hasServiceErrorCode()) {
        er.setServiceErrorCode(result.getServiceErrorCode());
    }
    if (errorResponseFormat.showDetails() && result.hasErrorDetails()) {
        er.setErrorDetails(new ErrorDetails(result.getErrorDetails()));
    }
    if (errorResponseFormat.showStacktrace()) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        result.printStackTrace(pw);
        er.setStackTrace(sw.toString());
        er.setExceptionClass(result.getClass().getName());
    }
    if (errorResponseFormat.showExceptionClass()) {
        er.setExceptionClass(result.getClass().getName());
    }
    return er;
}
Also used : StringWriter(java.io.StringWriter) ErrorDetails(com.linkedin.restli.common.ErrorDetails) ErrorResponse(com.linkedin.restli.common.ErrorResponse) PrintWriter(java.io.PrintWriter)

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