Search in sources :

Example 16 with RestLiResponseData

use of com.linkedin.restli.server.RestLiResponseData in project rest.li by linkedin.

the class TestRestLiCallback method testOnSuccessWithUnstructuredDataResponse.

@Test
@SuppressWarnings("unchecked")
public void testOnSuccessWithUnstructuredDataResponse() throws Exception {
    String result = "foo";
    RestLiResponseAttachments restLiResponseAttachments = new RestLiResponseAttachments.Builder().build();
    RestLiResponseData<UpdateResponseEnvelope> responseData = ResponseDataBuilderUtil.buildUpdateResponseData(HttpStatus.S_200_OK);
    RestLiResponse partialResponse = new RestLiResponse.Builder().build();
    // Set up.
    when((RestLiResponseData<UpdateResponseEnvelope>) _responseHandler.buildRestLiResponseData(_restRequest, _routingResult, result)).thenReturn(responseData);
    when(_responseHandler.buildPartialResponse(_routingResult, responseData)).thenReturn(partialResponse);
    // Invoke.
    _noFilterRestLiCallback.onSuccess(result);
    // Verify.
    verify(_responseHandler).buildPartialResponse(_routingResult, responseData);
    verify(_responseHandler).buildRestLiResponseData(_restRequest, _routingResult, result);
    verify(_callback).onSuccess(partialResponse);
    verifyZeroInteractions(_restRequest);
    verifyNoMoreInteractions(_responseHandler, _callback);
}
Also used : RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) RestLiResponseAttachments(com.linkedin.restli.server.RestLiResponseAttachments) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 17 with RestLiResponseData

use of com.linkedin.restli.server.RestLiResponseData in project rest.li by linkedin.

the class TestRestLiCallback method testOnErrorWithFiltersSuccessfullyHandlingAppEx.

@SuppressWarnings("unchecked")
@Test
public void testOnErrorWithFiltersSuccessfullyHandlingAppEx() throws Exception {
    Exception exFromApp = new RuntimeException("Runtime exception from app");
    RestLiServiceException appException = new RestLiServiceException(HttpStatus.S_404_NOT_FOUND);
    final Map<String, String> headersFromApp = Maps.newHashMap();
    headersFromApp.put("Key", "Input");
    final RecordTemplate entityFromFilter = Foo.createFoo("Key", "Two");
    final Map<String, String> headersFromFilter = Maps.newHashMap();
    headersFromFilter.put("Key", "Output");
    RestLiResponseData<CreateResponseEnvelope> responseData = new RestLiResponseDataImpl<>(new CreateResponseEnvelope(appException, false), headersFromApp, Collections.emptyList());
    RestLiResponse partialResponse = new RestLiResponse.Builder().build();
    ArgumentCaptor<RestLiServiceException> exCapture = ArgumentCaptor.forClass(RestLiServiceException.class);
    when(_responseHandler.buildExceptionResponseData(eq(_routingResult), exCapture.capture(), anyMap(), anyList())).thenReturn(responseData);
    when(_responseHandler.buildPartialResponse(_routingResult, responseData)).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();
            Throwable t = (Throwable) args[0];
            FilterRequestContext requestContext = (FilterRequestContext) args[1];
            FilterResponseContext responseContext = (FilterResponseContext) args[2];
            // Verify incoming data.
            RestLiResponseData<CreateResponseEnvelope> responseData = (RestLiResponseData<CreateResponseEnvelope>) responseContext.getResponseData();
            assertEquals(HttpStatus.S_404_NOT_FOUND, responseData.getResponseEnvelope().getStatus());
            assertEquals(headersFromApp, responseData.getHeaders());
            assertNull(responseData.getResponseEnvelope().getRecord());
            // Modify data in filter.
            setStatus(responseContext, HttpStatus.S_400_BAD_REQUEST);
            responseData.getHeaders().clear();
            return CompletableFuture.completedFuture(null);
        }
    }).when(_filter).onError(any(Throwable.class), eq(_filterRequestContext), any(FilterResponseContext.class));
    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.
            RestLiResponseData<CreateResponseEnvelope> responseData = (RestLiResponseData<CreateResponseEnvelope>) responseContext.getResponseData();
            assertEquals(HttpStatus.S_400_BAD_REQUEST, responseData.getResponseEnvelope().getStatus());
            assertTrue(responseData.getHeaders().isEmpty());
            assertNull(responseData.getResponseEnvelope().getRecord());
            // Modify data in filter.
            setStatus(responseContext, HttpStatus.S_403_FORBIDDEN);
            responseData.getResponseEnvelope().setRecord(entityFromFilter, HttpStatus.S_403_FORBIDDEN);
            responseData.getHeaders().putAll(headersFromFilter);
            return CompletableFuture.completedFuture(null);
        }
    }).when(_filter).onResponse(eq(_filterRequestContext), any(FilterResponseContext.class));
    // invoke request filters so cursor is in correct place
    when(_filter.onRequest(any(FilterRequestContext.class))).thenReturn(CompletableFuture.completedFuture(null));
    _twoFilterChain.onRequest(_filterRequestContext, _filterResponseContextFactory);
    // Invoke.
    _twoFilterRestLiCallback.onError(exFromApp);
    // Verify.
    assertNotNull(responseData);
    assertEquals(HttpStatus.S_403_FORBIDDEN, responseData.getResponseEnvelope().getStatus());
    assertEquals(entityFromFilter, responseData.getResponseEnvelope().getRecord());
    assertEquals(headersFromFilter, responseData.getHeaders());
    verify(_responseHandler).buildExceptionResponseData(eq(_routingResult), exCapture.capture(), anyMap(), anyList());
    verify(_responseHandler).buildPartialResponse(_routingResult, responseData);
    verify(_callback).onSuccess(partialResponse);
    verify(_restRequest, times(1)).getHeaders();
    verifyNoMoreInteractions(_restRequest, _responseHandler, _callback);
    RestLiServiceException restliEx = exCapture.getValue();
    assertNotNull(restliEx);
    assertEquals(HttpStatus.S_500_INTERNAL_SERVER_ERROR, restliEx.getStatus());
    assertEquals(exFromApp.getMessage(), restliEx.getMessage());
    assertEquals(exFromApp, restliEx.getCause());
}
Also used : RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RoutingException(com.linkedin.restli.server.RoutingException) RestException(com.linkedin.r2.message.rest.RestException) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) FilterResponseContext(com.linkedin.restli.server.filter.FilterResponseContext) RecordTemplate(com.linkedin.data.template.RecordTemplate) InvocationOnMock(org.mockito.invocation.InvocationOnMock) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 18 with RestLiResponseData

use of com.linkedin.restli.server.RestLiResponseData in project rest.li by linkedin.

the class TestRestLiCallback method testOnErrorWithFiltersNotHandlingAppEx.

@SuppressWarnings({ "unchecked", "deprecation" })
@Test
public void testOnErrorWithFiltersNotHandlingAppEx() throws Exception {
    Exception exFromApp = new RuntimeException("Runtime exception from app");
    RestLiServiceException appException = new RestLiServiceException(HttpStatus.S_404_NOT_FOUND);
    final Map<String, String> headersFromApp = Maps.newHashMap();
    headersFromApp.put("Key", "Input");
    final Map<String, String> headersFromFilter = Maps.newHashMap();
    headersFromFilter.put("Key", "Output");
    RestLiResponseData<CreateResponseEnvelope> responseData = new RestLiResponseDataImpl<>(new CreateResponseEnvelope(appException, false), headersFromApp, Collections.emptyList());
    RestLiResponse partialResponse = new RestLiResponse.Builder().build();
    when(_responseHandler.buildExceptionResponseData(eq(_routingResult), any(RestLiServiceException.class), anyMap(), anyList())).thenReturn(responseData);
    when(_responseHandler.buildPartialResponse(_routingResult, responseData)).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();
            Throwable t = (Throwable) args[0];
            FilterRequestContext requestContext = (FilterRequestContext) args[1];
            FilterResponseContext responseContext = (FilterResponseContext) args[2];
            // Verify incoming data.
            RestLiResponseData<CreateResponseEnvelope> responseData = (RestLiResponseData<CreateResponseEnvelope>) responseContext.getResponseData();
            assertEquals(HttpStatus.S_404_NOT_FOUND, responseData.getResponseEnvelope().getStatus());
            assertEquals(headersFromApp, responseData.getHeaders());
            assertNull(responseData.getResponseEnvelope().getRecord());
            // Modify data in filter.
            setStatus(responseContext, HttpStatus.S_400_BAD_REQUEST);
            responseData.getHeaders().clear();
            return completedFutureWithError(responseData.getResponseEnvelope().getException());
        }
    }).doAnswer(new Answer<Object>() {

        // Mock the behavior of the second filter.
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            Throwable t = (Throwable) args[0];
            FilterRequestContext requestContext = (FilterRequestContext) args[1];
            FilterResponseContext responseContext = (FilterResponseContext) args[2];
            // Verify incoming data.
            RestLiResponseData<CreateResponseEnvelope> responseData = (RestLiResponseData<CreateResponseEnvelope>) responseContext.getResponseData();
            assertEquals(HttpStatus.S_400_BAD_REQUEST, responseData.getResponseEnvelope().getStatus());
            assertNull(responseData.getResponseEnvelope().getRecord());
            // Modify data in filter.
            setStatus(responseContext, HttpStatus.S_403_FORBIDDEN);
            responseData.getHeaders().putAll(headersFromFilter);
            return completedFutureWithError(responseData.getResponseEnvelope().getException());
        }
    }).when(_filter).onError(any(Throwable.class), eq(_filterRequestContext), any(FilterResponseContext.class));
    // invoke request filters so cursor is in correct place
    when(_filter.onRequest(any(FilterRequestContext.class))).thenReturn(CompletableFuture.completedFuture(null));
    _twoFilterChain.onRequest(_filterRequestContext, _filterResponseContextFactory);
    // Invoke.
    _twoFilterRestLiCallback.onError(exFromApp);
    // Verify.
    assertNotNull(responseData);
    assertEquals(HttpStatus.S_403_FORBIDDEN, responseData.getResponseEnvelope().getStatus());
    assertNull(responseData.getResponseEnvelope().getRecord());
    assertTrue(responseData.getResponseEnvelope().isErrorResponse());
    assertEquals(responseData.getResponseEnvelope().getException().getErrorDetails(), appException.getErrorDetails());
    assertEquals(responseData.getResponseEnvelope().getException().getOverridingFormat(), appException.getOverridingFormat());
    assertEquals(responseData.getResponseEnvelope().getException().getServiceErrorCode(), appException.getServiceErrorCode());
    assertEquals(responseData.getResponseEnvelope().getException().getMessage(), appException.getMessage());
    Map<String, String> expectedHeaders = buildErrorHeaders();
    expectedHeaders.put("Key", "Output");
    assertEquals(expectedHeaders, responseData.getHeaders());
    ArgumentCaptor<RestLiServiceException> exCapture = ArgumentCaptor.forClass(RestLiServiceException.class);
    verify(_responseHandler, times(1)).buildExceptionResponseData(eq(_routingResult), exCapture.capture(), anyMap(), anyList());
    verify(_responseHandler).buildPartialResponse(_routingResult, responseData);
    ArgumentCaptor<RestLiResponseException> partialRestResponseExceptionCaptor = ArgumentCaptor.forClass(RestLiResponseException.class);
    verify(_callback).onError(partialRestResponseExceptionCaptor.capture());
    verify(_restRequest, times(1)).getHeaders();
    verifyNoMoreInteractions(_restRequest, _responseHandler, _callback);
    final RestLiServiceException restliEx1 = exCapture.getAllValues().get(0);
    assertNotNull(restliEx1);
    assertEquals(HttpStatus.S_500_INTERNAL_SERVER_ERROR, restliEx1.getStatus());
    assertEquals(exFromApp.getMessage(), restliEx1.getMessage());
    assertEquals(exFromApp, restliEx1.getCause());
    RestLiResponseException restLiResponseException = partialRestResponseExceptionCaptor.getValue();
    assertEquals(restLiResponseException.getRestLiResponse(), partialResponse);
    assertTrue(restLiResponseException.getCause() instanceof RestLiServiceException);
    RestLiServiceException restliEx2 = (RestLiServiceException) restLiResponseException.getCause();
    assertNotNull(restliEx2);
    assertEquals(HttpStatus.S_403_FORBIDDEN, restliEx2.getStatus());
}
Also used : RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RoutingException(com.linkedin.restli.server.RoutingException) RestException(com.linkedin.r2.message.rest.RestException) Answer(org.mockito.stubbing.Answer) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) FilterResponseContext(com.linkedin.restli.server.filter.FilterResponseContext) InvocationOnMock(org.mockito.invocation.InvocationOnMock) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 19 with RestLiResponseData

use of com.linkedin.restli.server.RestLiResponseData in project rest.li by linkedin.

the class TestRestLiCallback method testOnSuccessBuildPartialResponseFailure.

@Test
@SuppressWarnings("unchecked")
public void testOnSuccessBuildPartialResponseFailure() throws Exception {
    String result = "foo";
    RestLiResponseData<UpdateResponseEnvelope> responseData = ResponseDataBuilderUtil.buildUpdateResponseData(HttpStatus.S_200_OK);
    RestResponse restResponse = new RestResponseBuilder().build();
    // Set up.
    when((RestLiResponseData<UpdateResponseEnvelope>) _responseHandler.buildRestLiResponseData(_restRequest, _routingResult, result)).thenReturn(responseData);
    Exception e = new RuntimeException("Error1");
    when(_responseHandler.buildPartialResponse(_routingResult, responseData)).thenThrow(e);
    // Invoke.
    _noFilterRestLiCallback.onSuccess(result);
    // Verify.
    verify(_responseHandler).buildPartialResponse(_routingResult, responseData);
    verify(_responseHandler).buildRestLiResponseData(_restRequest, _routingResult, result);
    ArgumentCaptor<RestLiResponseException> partialRestResponseExceptionCaptor = ArgumentCaptor.forClass(RestLiResponseException.class);
    verify(_callback).onError(partialRestResponseExceptionCaptor.capture());
    verifyZeroInteractions(_restRequest);
    verifyNoMoreInteractions(_responseHandler, _callback);
    RestLiResponse restLiResponse = partialRestResponseExceptionCaptor.getValue().getRestLiResponse();
    assertEquals(restLiResponse.getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR);
    assertEquals(RestConstants.HEADER_VALUE_ERROR, restLiResponse.getHeader(HeaderUtil.getErrorResponseHeaderName(Collections.emptyMap())));
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RoutingException(com.linkedin.restli.server.RoutingException) RestException(com.linkedin.r2.message.rest.RestException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 20 with RestLiResponseData

use of com.linkedin.restli.server.RestLiResponseData in project rest.li by linkedin.

the class TestRestLiCallback method testOnSuccessNoFilters.

@Test
@SuppressWarnings("unchecked")
public void testOnSuccessNoFilters() throws Exception {
    String result = "foo";
    RestLiResponseAttachments restLiResponseAttachments = new RestLiResponseAttachments.Builder().build();
    RestLiResponseData<UpdateResponseEnvelope> responseData = ResponseDataBuilderUtil.buildUpdateResponseData(HttpStatus.S_200_OK);
    RestLiResponse partialResponse = new RestLiResponse.Builder().build();
    // Set up.
    when((RestLiResponseData<UpdateResponseEnvelope>) _responseHandler.buildRestLiResponseData(_restRequest, _routingResult, result)).thenReturn(responseData);
    when(_responseHandler.buildPartialResponse(_routingResult, responseData)).thenReturn(partialResponse);
    // Invoke.
    _noFilterRestLiCallback.onSuccess(result);
    // Verify.
    verify(_responseHandler).buildPartialResponse(_routingResult, responseData);
    verify(_responseHandler).buildRestLiResponseData(_restRequest, _routingResult, result);
    verify(_callback).onSuccess(partialResponse);
    verifyZeroInteractions(_restRequest);
    verifyNoMoreInteractions(_responseHandler, _callback);
}
Also used : RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) RestLiResponseAttachments(com.linkedin.restli.server.RestLiResponseAttachments) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

RestLiResponseData (com.linkedin.restli.server.RestLiResponseData)21 Test (org.testng.annotations.Test)20 BeforeTest (org.testng.annotations.BeforeTest)14 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)11 FilterResponseContext (com.linkedin.restli.server.filter.FilterResponseContext)11 RestException (com.linkedin.r2.message.rest.RestException)10 RoutingException (com.linkedin.restli.server.RoutingException)10 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)10 InvocationOnMock (org.mockito.invocation.InvocationOnMock)8 RecordTemplate (com.linkedin.data.template.RecordTemplate)7 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)7 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)5 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)4 Answer (org.mockito.stubbing.Answer)4 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)3 DataMap (com.linkedin.data.DataMap)2 Foo (com.linkedin.pegasus.generator.examples.Foo)2 RestRequest (com.linkedin.r2.message.rest.RestRequest)2 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)2 BatchResponse (com.linkedin.restli.common.BatchResponse)2