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 } };
}
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);
}
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);
}
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);
}
}
}
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());
}
Aggregations