use of com.linkedin.r2.message.Request in project rest.li by linkedin.
the class TestRestLiCallback method testOnSuccessWithExceptionBuildingResponseNoFilters.
@SuppressWarnings("unchecked")
@Test
public void testOnSuccessWithExceptionBuildingResponseNoFilters() throws Exception {
String result = "foo";
RequestExecutionReport executionReport = new RequestExecutionReportBuilder().build();
RestLiResponseAttachments responseAttachments = new RestLiResponseAttachments.Builder().build();
RestLiServiceException ex = new RestLiServiceException(HttpStatus.S_422_UNPROCESSABLE_ENTITY);
Map<String, String> inputHeaders = Maps.newHashMap();
inputHeaders.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString());
RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(ex, new HashMap<String, String>(), Collections.<HttpCookie>emptyList());
responseData.setResponseEnvelope(new GetResponseEnvelope(new EmptyRecord(), responseData));
PartialRestResponse partialResponse = new PartialRestResponse.Builder().build();
RestException restException = new RestException(new RestResponseBuilder().build());
// Set up.
// Throw an exception when we try to build the response data.
when(_requestExecutionReportBuilder.build()).thenReturn(executionReport);
when(_responseHandler.buildRestLiResponseData(_restRequest, _routingResult, result)).thenThrow(ex);
when(_restRequest.getHeaders()).thenReturn(inputHeaders);
when(_responseHandler.buildExceptionResponseData(eq(_restRequest), eq(_routingResult), eq(ex), anyMap(), anyList())).thenReturn(responseData);
when(_responseHandler.buildPartialResponse(_routingResult, responseData)).thenReturn(partialResponse);
when(_responseHandler.buildRestException(ex, partialResponse)).thenReturn(restException);
// invoke request filters so cursor is in correct place
when(_filter.onRequest(any(FilterRequestContext.class))).thenReturn(CompletableFuture.completedFuture(null));
_twoFilterChain.onRequest(_filterRequestContext, _filterResponseContextFactory);
// Invoke.
_noFilterRestLiCallback.onSuccess(result, executionReport, responseAttachments);
// Verify.
verify(_responseHandler).buildRestLiResponseData(_restRequest, _routingResult, result);
verify(_responseHandler).buildRestException(ex, partialResponse);
verify(_responseHandler).buildExceptionResponseData(eq(_restRequest), eq(_routingResult), eq(ex), anyMap(), anyList());
verify(_responseHandler).buildPartialResponse(_routingResult, responseData);
verify(_callback).onError(restException, executionReport, _requestAttachmentReader, responseAttachments);
verify(_restRequest).getHeaders();
verifyZeroInteractions(_routingResult);
verifyNoMoreInteractions(_restRequest, _responseHandler, _callback);
}
use of com.linkedin.r2.message.Request in project rest.li by linkedin.
the class TestRestLiCallback method testOnSuccessWithFiltersSuccessful.
@Test
public void testOnSuccessWithFiltersSuccessful() throws Exception {
String result = "foo";
RequestExecutionReport executionReport = new RequestExecutionReportBuilder().build();
final RestLiResponseAttachments restLiResponseAttachments = new RestLiResponseAttachments.Builder().build();
final RecordTemplate entityFromApp = Foo.createFoo("Key", "One");
final Map<String, String> headersFromApp = Maps.newHashMap();
headersFromApp.put("Key", "Input");
final RecordTemplate entityFromFilter1 = Foo.createFoo("Key", "Two");
final RecordTemplate entityFromFilter2 = Foo.createFoo("Key", "Three");
final Map<String, String> headersFromFilters = Maps.newHashMap();
headersFromFilters.put("Key", "Output");
RestLiResponseDataImpl appResponseData = new RestLiResponseDataImpl(HttpStatus.S_200_OK, headersFromApp, Collections.<HttpCookie>emptyList());
appResponseData.setResponseEnvelope(new CreateResponseEnvelope(entityFromApp, appResponseData));
PartialRestResponse partialResponse = new PartialRestResponse.Builder().build();
// Setup.
when(_requestExecutionReportBuilder.build()).thenReturn(executionReport);
when(_responseHandler.buildRestLiResponseData(_restRequest, _routingResult, result)).thenReturn(appResponseData);
when(_responseHandler.buildPartialResponse(_routingResult, appResponseData)).thenReturn(partialResponse);
// Mock the behavior of the first filter.
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
FilterRequestContext requestContext = (FilterRequestContext) args[0];
FilterResponseContext responseContext = (FilterResponseContext) args[1];
// Verify incoming data.
assertEquals(HttpStatus.S_200_OK, responseContext.getResponseData().getStatus());
assertEquals(headersFromApp, responseContext.getResponseData().getHeaders());
assertEquals(entityFromApp, responseContext.getResponseData().getRecordResponseEnvelope().getRecord());
// Modify data in filter.
setStatus(responseContext, HttpStatus.S_400_BAD_REQUEST);
responseContext.getResponseData().getRecordResponseEnvelope().setRecord(entityFromFilter1, HttpStatus.S_400_BAD_REQUEST);
responseContext.getResponseData().getHeaders().clear();
return CompletableFuture.completedFuture(null);
}
}).doAnswer(new Answer<Object>() {
// Mock the behavior of the second filter.
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
FilterRequestContext requestContext = (FilterRequestContext) args[0];
FilterResponseContext responseContext = (FilterResponseContext) args[1];
// Verify incoming data.
assertEquals(HttpStatus.S_400_BAD_REQUEST, responseContext.getResponseData().getStatus());
assertTrue(responseContext.getResponseData().getHeaders().isEmpty());
assertEquals(responseContext.getResponseData().getRecordResponseEnvelope().getRecord(), entityFromFilter1);
// Modify data in filter.
setStatus(responseContext, HttpStatus.S_403_FORBIDDEN);
responseContext.getResponseData().getRecordResponseEnvelope().setRecord(entityFromFilter2, HttpStatus.S_403_FORBIDDEN);
responseContext.getResponseData().getHeaders().putAll(headersFromFilters);
return CompletableFuture.completedFuture(null);
}
}).when(_filter).onResponse(eq(_filterRequestContext), any(FilterResponseContext.class));
RestResponse restResponse = new RestResponseBuilder().build();
when(_responseHandler.buildResponse(_routingResult, partialResponse)).thenReturn(restResponse);
// invoke request filters so cursor is in correct place
when(_filter.onRequest(any(FilterRequestContext.class))).thenReturn(CompletableFuture.completedFuture(null));
_twoFilterChain.onRequest(_filterRequestContext, _filterResponseContextFactory);
// Invoke with some response attachments.
_twoFilterRestLiCallback.onSuccess(result, executionReport, restLiResponseAttachments);
// Verify.
assertNotNull(appResponseData);
assertEquals(HttpStatus.S_403_FORBIDDEN, appResponseData.getStatus());
assertEquals(entityFromFilter2, appResponseData.getRecordResponseEnvelope().getRecord());
assertEquals(headersFromFilters, appResponseData.getHeaders());
verify(_responseHandler).buildRestLiResponseData(_restRequest, _routingResult, result);
verify(_responseHandler).buildPartialResponse(_routingResult, appResponseData);
verify(_responseHandler).buildResponse(_routingResult, partialResponse);
verify(_callback).onSuccess(restResponse, executionReport, restLiResponseAttachments);
verifyZeroInteractions(_restRequest, _routingResult);
verifyNoMoreInteractions(_responseHandler, _callback);
}
use of com.linkedin.r2.message.Request in project rest.li by linkedin.
the class TestActionArgumentBuilder method testArgumentBuilderSuccess.
@Test(dataProvider = "successData")
public void testArgumentBuilderSuccess(String entity, List<Parameter<?>> params, Object[] expectedArgs) {
RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, entity, 3);
ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(null, params, null, null);
ResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(null, null, null, true);
RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 2, context, 1);
RestLiArgumentBuilder argumentBuilder = new ActionArgumentBuilder();
RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, request);
Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
assertEquals(args, expectedArgs);
verify(request, descriptor, routingResult);
}
use of com.linkedin.r2.message.Request in project rest.li by linkedin.
the class TestBatchCreateArgumentBuilder method testArgumentBuilderSuccess.
@Test
public void testArgumentBuilderSuccess() {
RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, "{\"elements\":[{\"b\":123,\"a\":\"abc\"},{\"b\":5678,\"a\":\"xyzw\"}]}", 1);
ResourceModel model = RestLiArgumentBuilderTestHelper.getMockResourceModel(MyComplexKey.class, null, false);
@SuppressWarnings("rawtypes") Parameter<BatchCreateRequest> param = new Parameter<BatchCreateRequest>("", BatchCreateRequest.class, null, false, null, Parameter.ParamType.BATCH, false, new AnnotationSet(new Annotation[] {}));
ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, param);
ResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(null, null, null, true);
RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 2, context, 1);
RestLiArgumentBuilder argumentBuilder = new BatchCreateArgumentBuilder();
RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, request);
Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
assertEquals(args.length, 1);
assertTrue(args[0] instanceof BatchCreateRequest);
@SuppressWarnings("unchecked") List<MyComplexKey> entities = ((BatchCreateRequest<Integer, MyComplexKey>) args[0]).getInput();
assertEquals(entities.size(), 2);
assertEquals(entities.get(0).getA(), "abc");
assertEquals((long) entities.get(0).getB(), 123L);
assertEquals(entities.get(1).getA(), "xyzw");
assertEquals((long) entities.get(1).getB(), 5678L);
verify(request, model, descriptor, context, routingResult);
}
use of com.linkedin.r2.message.Request in project rest.li by linkedin.
the class TestBatchGetArgumentBuilder method testArgumentBuilderSuccess.
@Test(dataProvider = "argumentData")
public void testArgumentBuilderSuccess(Set<Object> batchKeys) {
@SuppressWarnings("rawtypes") Parameter<Set> param = new Parameter<Set>("", Set.class, null, false, null, Parameter.ParamType.BATCH, false, new AnnotationSet(new Annotation[] {}));
ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(null, param);
ResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(null, null, batchKeys, true);
RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 1, context, 2);
RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, null, 0);
RestLiArgumentBuilder argumentBuilder = new BatchGetArgumentBuilder();
RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, request);
Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
Object[] expectedArgs = new Object[] { batchKeys };
assertEquals(args, expectedArgs);
verify(descriptor, context, routingResult, request);
}
Aggregations