Search in sources :

Example 56 with ErrorResponse

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

the class TestMockBatchCreateIdResponseFactory method testCreate.

@Test(dataProvider = "provideKeys")
public <K> void testCreate(K[] keys) {
    ProtocolVersion version = AllProtocolVersions.BASELINE_PROTOCOL_VERSION;
    List<CreateIdStatus<K>> elements = new ArrayList<>();
    elements.add(new CreateIdStatus<>(HttpStatus.S_201_CREATED.getCode(), keys[0], null, version));
    elements.add(new CreateIdStatus<>(HttpStatus.S_201_CREATED.getCode(), keys[1], null, version));
    ErrorResponse error = new ErrorResponse().setMessage("3");
    elements.add(new CreateIdStatus<>(HttpStatus.S_500_INTERNAL_SERVER_ERROR.getCode(), keys[2], error, version));
    BatchCreateIdResponse<K> batchResp = MockBatchCreateIdResponseFactory.create(elements);
    Assert.assertEquals(batchResp.getElements(), elements);
}
Also used : CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) ArrayList(java.util.ArrayList) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) ErrorResponse(com.linkedin.restli.common.ErrorResponse) Test(org.testng.annotations.Test)

Example 57 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 58 with ErrorResponse

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

the class MockBatchKVResponseFactory method buildDataMap.

private static <K, V extends RecordTemplate> DataMap buildDataMap(Map<K, V> recordTemplates, Map<K, ErrorResponse> errorResponses, ProtocolVersion version) {
    DataMap batchResponseDataMap = new DataMap();
    DataMap rawBatchData = new DataMap();
    for (Map.Entry<K, V> entry : recordTemplates.entrySet()) {
        String stringKey = URIParamUtils.encodeKeyForBody(entry.getKey(), false, version);
        rawBatchData.put(stringKey, entry.getValue().data());
    }
    batchResponseDataMap.put(BatchResponse.RESULTS, rawBatchData);
    DataMap rawErrorData = new DataMap();
    for (Map.Entry<K, ErrorResponse> errorResponse : errorResponses.entrySet()) {
        rawErrorData.put(URIParamUtils.encodeKeyForBody(errorResponse.getKey(), false, version), errorResponse.getValue().data());
    }
    batchResponseDataMap.put(BatchResponse.ERRORS, rawErrorData);
    return batchResponseDataMap;
}
Also used : DataMap(com.linkedin.data.DataMap) Map(java.util.Map) DataMap(com.linkedin.data.DataMap) ErrorResponse(com.linkedin.restli.common.ErrorResponse)

Example 59 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<>();
    Map<ComplexResourceKey<Greeting, Greeting>, ErrorResponse> errorResponses = new HashMap<>();
    Greeting g1 = buildGreeting(1L);
    Greeting g2 = buildGreeting(2L);
    Greeting g3 = buildGreeting(3L);
    recordTemplates.put(new ComplexResourceKey<>(g1, g1), g1);
    recordTemplates.put(new ComplexResourceKey<>(g2, g2), g2);
    errorResponses.put(new ComplexResourceKey<>(g3, g3), new ErrorResponse().setMessage("3"));
    Map<ComplexResourceKey<Greeting, Greeting>, HttpStatus> statuses = new HashMap<>();
    statuses.put(new ComplexResourceKey<>(g1, g1), HttpStatus.S_200_OK);
    statuses.put(new ComplexResourceKey<>(g2, g2), HttpStatus.S_200_OK);
    statuses.put(new ComplexResourceKey<>(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<>();
    expectedRecordTemplates.put(new ComplexResourceKey<>(g1, new Greeting()), recordTemplates.get(new ComplexResourceKey<>(g1, g1)));
    expectedRecordTemplates.put(new ComplexResourceKey<>(g2, new Greeting()), recordTemplates.get(new ComplexResourceKey<>(g2, g2)));
    Map<ComplexResourceKey<Greeting, Greeting>, EntityResponse<Greeting>> expectedResults = new HashMap<>();
    expectedResults.put(new ComplexResourceKey<>(g1, new Greeting()), buildEntityResponse(recordTemplates.get(new ComplexResourceKey<>(g1, g1)), HttpStatus.S_200_OK, null));
    expectedResults.put(new ComplexResourceKey<>(g2, new Greeting()), buildEntityResponse(recordTemplates.get(new ComplexResourceKey<>(g2, g2)), HttpStatus.S_200_OK, null));
    expectedResults.put(new ComplexResourceKey<>(g3, new Greeting()), buildEntityResponse(null, HttpStatus.S_500_INTERNAL_SERVER_ERROR, errorResponses.get(new ComplexResourceKey<>(g3, g3))));
    Map<ComplexResourceKey<Greeting, Greeting>, ErrorResponse> expectedErrors = new HashMap<>();
    expectedErrors.put(new ComplexResourceKey<>(g3, new Greeting()), errorResponses.get(new ComplexResourceKey<>(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 60 with ErrorResponse

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

the class DefaultScatterGatherStrategy method gatherException.

/**
 * Gather an incoming scattered request error and merge it into currently accumulated batch response.
 * @param accumulatedDataMap currently accumulated response data map.
 * @param keys keys which result in the error.
 * @param e error exception.
 * @param version protocol version.
 * @param <K> request key type
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private <K> void gatherException(DataMap accumulatedDataMap, Set<K> keys, Throwable e, ProtocolVersion version) {
    ErrorResponse errorResponse = new ErrorResponse();
    errorResponse.setMessage(e.getMessage());
    errorResponse.setStatus(HttpStatus.S_500_INTERNAL_SERVER_ERROR.getCode());
    errorResponse.setExceptionClass(e.getClass().getName());
    keys.forEach(key -> {
        String keyString = BatchResponse.keyToString(key, version);
        accumulatedDataMap.getDataMap(BatchResponse.ERRORS).put(keyString, errorResponse.data());
    });
}
Also used : 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