Search in sources :

Example 11 with RestLiResponseData

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

the class TestRestLiResponseHandler method testBuildRestLiUnstructuredDataResponse.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicData")
@SuppressWarnings("unchecked")
public void testBuildRestLiUnstructuredDataResponse(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
    final RestRequest request = buildRequest(Collections.EMPTY_MAP, protocolVersion);
    RoutingResult routingResult = buildUnstructuredDataRoutingResult(request);
    RestLiResponseData<GetResponseEnvelope> responseData = (RestLiResponseData<GetResponseEnvelope>) _responseHandler.buildRestLiResponseData(request, routingResult, null);
    assertEquals(responseData.getResponseEnvelope().getStatus(), HttpStatus.S_200_OK);
    assertEquals(responseData.getResponseEnvelope().getRecord(), new EmptyRecord());
    RestLiResponse restResponse = buildPartialRestResponse(request, routingResult, null);
    assertNotNull(restResponse);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) EmptyRecord(com.linkedin.restli.common.EmptyRecord) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) GetResponseEnvelope(com.linkedin.restli.internal.server.response.GetResponseEnvelope) Test(org.testng.annotations.Test)

Example 12 with RestLiResponseData

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

the class RestLiFilterResponseContextFactory method fromThrowable.

/**
   * Create a {@link FilterResponseContext} based on the given error.
   *
   * @param throwable Error obtained from the resource method invocation.
   *
   * @return {@link FilterResponseContext} corresponding to the given input.
   */
public FilterResponseContext fromThrowable(Throwable throwable) {
    RestLiServiceException restLiServiceException;
    if (throwable instanceof RestLiServiceException) {
        restLiServiceException = (RestLiServiceException) throwable;
    } else if (throwable instanceof RoutingException) {
        RoutingException routingException = (RoutingException) throwable;
        restLiServiceException = new RestLiServiceException(HttpStatus.fromCode(routingException.getStatus()), routingException.getMessage(), routingException);
    } else {
        restLiServiceException = new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, throwable.getMessage(), throwable);
    }
    Map<String, String> requestHeaders = _request.getHeaders();
    Map<String, String> headers = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
    headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, ProtocolVersionUtil.extractProtocolVersion(requestHeaders).toString());
    headers.put(HeaderUtil.getErrorResponseHeaderName(requestHeaders), RestConstants.HEADER_VALUE_ERROR);
    final RestLiResponseData responseData = _responseHandler.buildExceptionResponseData(_request, _method, restLiServiceException, headers, Collections.<HttpCookie>emptyList());
    return new FilterResponseContext() {

        @Override
        public RestLiResponseData getResponseData() {
            return responseData;
        }
    };
}
Also used : RoutingException(com.linkedin.restli.server.RoutingException) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) FilterResponseContext(com.linkedin.restli.server.filter.FilterResponseContext) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) TreeMap(java.util.TreeMap)

Example 13 with RestLiResponseData

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

the class TestBatchUpdateResponseBuilder method testBuilder.

@Test(dataProvider = "testData")
@SuppressWarnings("unchecked")
public void testBuilder(Object results, ProtocolVersion protocolVersion, String altKeyName, Map<String, AlternativeKey<?, ?>> alternativeKeyMap, Map<String, UpdateStatus> expectedResults, Map<String, ErrorResponse> expectedErrors) {
    ResourceContext mockContext = getMockResourceContext(protocolVersion, altKeyName);
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(alternativeKeyMap);
    RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
    Map<String, String> headers = ResponseBuilderUtil.getHeaders();
    BatchUpdateResponseBuilder batchUpdateResponseBuilder = new BatchUpdateResponseBuilder(new ErrorResponseBuilder());
    RestLiResponseData responseData = batchUpdateResponseBuilder.buildRestLiResponseData(null, routingResult, results, headers, Collections.<HttpCookie>emptyList());
    PartialRestResponse restResponse = batchUpdateResponseBuilder.buildResponse(routingResult, responseData);
    BatchResponse<UpdateStatus> batchResponse = (BatchResponse<UpdateStatus>) restResponse.getEntity();
    EasyMock.verify(mockContext, mockDescriptor);
    ResponseBuilderUtil.validateHeaders(restResponse, headers);
    Assert.assertEquals(batchResponse.getResults(), expectedResults);
    Assert.assertEquals(batchResponse.getErrors().size(), expectedErrors.size());
    for (Map.Entry<String, ErrorResponse> entry : batchResponse.getErrors().entrySet()) {
        String key = entry.getKey();
        ErrorResponse value = entry.getValue();
        Assert.assertEquals(value.getStatus(), expectedErrors.get(key).getStatus());
    }
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) UpdateStatus(com.linkedin.restli.common.UpdateStatus) BatchResponse(com.linkedin.restli.common.BatchResponse) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) ErrorResponse(com.linkedin.restli.common.ErrorResponse) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.testng.annotations.Test)

Example 14 with RestLiResponseData

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

the class TestCollectionResponseBuilder method testBuilder.

@SuppressWarnings("unchecked")
@Test(dataProvider = "testData")
public void testBuilder(Object results, DataMap expectedMetadata, List<Foo> expectedElements, CollectionMetadata expectedPaging, MaskTree dataMaskTree, MaskTree metaDataMaskTree, MaskTree pagingMaskTree, ProjectionMode dataProjectionMode, ProjectionMode metadataProjectionMode) throws URISyntaxException {
    for (ResourceMethod resourceMethod : Arrays.asList(ResourceMethod.GET_ALL, ResourceMethod.FINDER)) {
        Map<String, String> headers = ResponseBuilderUtil.getHeaders();
        ResourceContext mockContext = getMockResourceContext(dataMaskTree, metaDataMaskTree, pagingMaskTree, dataProjectionMode, metadataProjectionMode);
        ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(resourceMethod);
        RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
        CollectionResponseBuilder responseBuilder = new CollectionResponseBuilder();
        RestLiResponseData responseData = responseBuilder.buildRestLiResponseData(getRestRequest(), routingResult, results, headers, Collections.<HttpCookie>emptyList());
        PartialRestResponse restResponse = responseBuilder.buildResponse(routingResult, responseData);
        EasyMock.verify(mockContext, mockDescriptor);
        ResponseBuilderUtil.validateHeaders(restResponse, headers);
        CollectionResponse<Foo> actualResults = (CollectionResponse<Foo>) restResponse.getEntity();
        Assert.assertEquals(actualResults.getElements(), expectedElements);
        Assert.assertEquals(actualResults.getMetadataRaw(), expectedMetadata);
        Assert.assertEquals(actualResults.getPaging(), expectedPaging);
        EasyMock.verify(mockContext);
    }
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) ResourceContext(com.linkedin.restli.server.ResourceContext) CollectionResponse(com.linkedin.restli.common.CollectionResponse) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) Foo(com.linkedin.pegasus.generator.examples.Foo) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) ResourceMethod(com.linkedin.restli.common.ResourceMethod) Test(org.testng.annotations.Test)

Example 15 with RestLiResponseData

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

the class TestUpdateResponseBuilder method testBuilder.

@Test
public void testBuilder() {
    HttpStatus status = HttpStatus.S_200_OK;
    UpdateResponse updateResponse = new UpdateResponse(status);
    Map<String, String> headers = ResponseBuilderUtil.getHeaders();
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor();
    RoutingResult routingResult = new RoutingResult(null, mockDescriptor);
    UpdateResponseBuilder updateResponseBuilder = new UpdateResponseBuilder();
    RestLiResponseData responseData = updateResponseBuilder.buildRestLiResponseData(null, routingResult, updateResponse, headers, Collections.<HttpCookie>emptyList());
    PartialRestResponse partialRestResponse = updateResponseBuilder.buildResponse(routingResult, responseData);
    EasyMock.verify(mockDescriptor);
    ResponseBuilderUtil.validateHeaders(partialRestResponse, headers);
    Assert.assertEquals(partialRestResponse.getStatus(), status);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) UpdateResponse(com.linkedin.restli.server.UpdateResponse) HttpStatus(com.linkedin.restli.common.HttpStatus) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) Test(org.testng.annotations.Test)

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