Search in sources :

Example 11 with RestLiResponse

use of com.linkedin.restli.internal.server.response.RestLiResponse in project rest.li by linkedin.

the class TestRestLiResponseHandler method testBuildResponseWithExceptionObject.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicData")
@SuppressWarnings("deprecation")
void testBuildResponseWithExceptionObject(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
    RestLiResponse response;
    RestLiServiceException ex;
    final RestRequest request = buildRequest(acceptTypeData.acceptHeaders, protocolVersion);
    // #1
    ex = new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "missing fields");
    response = buildPartialRestResponse(request, buildRoutingResult(request, acceptTypeData.acceptHeaders), ex);
    checkResponse(response, 400, 2, true, true, errorResponseHeaderName);
    DataMap dataMap = response.getDataMap();
    assertEquals(dataMap.getInteger("status"), Integer.valueOf(400));
    assertEquals(dataMap.getString("message"), "missing fields");
    // #2
    ex = new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "missing fields").setServiceErrorCode(11);
    response = buildPartialRestResponse(request, buildRoutingResult(request, acceptTypeData.acceptHeaders), ex);
    checkResponse(response, 400, 2, true, true, errorResponseHeaderName);
    dataMap = response.getDataMap();
    assertEquals(dataMap.getInteger("status"), Integer.valueOf(400));
    assertEquals(dataMap.getString("message"), "missing fields");
    assertEquals(dataMap.getInteger("serviceErrorCode"), Integer.valueOf(11));
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 12 with RestLiResponse

use of com.linkedin.restli.internal.server.response.RestLiResponse in project rest.li by linkedin.

the class TestRestLiResponseHandler method testBatchResponses.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicData")
public void testBatchResponses(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
    RestLiResponse response;
    // #4 batch
    Map<Long, Status> map = new HashMap<>();
    map.put(1L, buildStatusRecord());
    map.put(2L, buildStatusRecord());
    map.put(3L, buildStatusRecord());
    response = invokeResponseHandler("/test", map, ResourceMethod.BATCH_GET, acceptTypeData.acceptHeaders, protocolVersion);
    checkResponse(response, 200, 1, true, errorResponseHeaderName);
    Map<Long, UpdateResponse> updateStatusMap = new HashMap<>();
    updateStatusMap.put(1L, new UpdateResponse(HttpStatus.S_204_NO_CONTENT));
    updateStatusMap.put(2L, new UpdateResponse(HttpStatus.S_204_NO_CONTENT));
    updateStatusMap.put(3L, new UpdateResponse(HttpStatus.S_204_NO_CONTENT));
    BatchUpdateResult<Long, Status> batchUpdateResult = new BatchUpdateResult<>(updateStatusMap);
    response = invokeResponseHandler("/test", batchUpdateResult, ResourceMethod.BATCH_UPDATE, acceptTypeData.acceptHeaders, protocolVersion);
    checkResponse(response, 200, 1, true, errorResponseHeaderName);
    response = invokeResponseHandler("/test", batchUpdateResult, ResourceMethod.BATCH_PARTIAL_UPDATE, acceptTypeData.acceptHeaders, protocolVersion);
    checkResponse(response, 200, 1, true, errorResponseHeaderName);
    response = invokeResponseHandler("/test", batchUpdateResult, ResourceMethod.BATCH_DELETE, acceptTypeData.acceptHeaders, protocolVersion);
    checkResponse(response, 200, 1, true, errorResponseHeaderName);
    List<CreateResponse> createResponses = new ArrayList<>();
    createResponses.add(new CreateResponse("42", HttpStatus.S_204_NO_CONTENT));
    createResponses.add(new CreateResponse(HttpStatus.S_400_BAD_REQUEST));
    createResponses.add(new CreateResponse(HttpStatus.S_500_INTERNAL_SERVER_ERROR));
    BatchCreateResult<Long, Status> batchCreateResult = new BatchCreateResult<>(createResponses);
    // here
    response = invokeResponseHandler("/test", batchCreateResult, ResourceMethod.BATCH_CREATE, acceptTypeData.acceptHeaders, protocolVersion);
    checkResponse(response, 200, 1, true, errorResponseHeaderName);
}
Also used : Status(com.linkedin.restli.server.twitter.TwitterTestDataModels.Status) HttpStatus(com.linkedin.restli.common.HttpStatus) HashMap(java.util.HashMap) CreateResponse(com.linkedin.restli.server.CreateResponse) ArrayList(java.util.ArrayList) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) UpdateResponse(com.linkedin.restli.server.UpdateResponse) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) BatchCreateResult(com.linkedin.restli.server.BatchCreateResult) Test(org.testng.annotations.Test)

Example 13 with RestLiResponse

use of com.linkedin.restli.internal.server.response.RestLiResponse in project rest.li by linkedin.

the class TestRestLiResponseHandler method testBuildRestException.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicData")
@SuppressWarnings("deprecation")
void testBuildRestException(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
    final RestRequest request = buildRequest(acceptTypeData.acceptHeaders, protocolVersion);
    final RoutingResult routingResult = buildRoutingResult(request, acceptTypeData.acceptHeaders);
    Map<String, String> requestHeaders = new HashMap<>();
    requestHeaders.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString());
    RestLiServiceException ex = new RestLiServiceException(HttpStatus.S_404_NOT_FOUND, "freeway not found").setServiceErrorCode(237);
    RestLiResponseData<?> responseData = _responseHandler.buildExceptionResponseData(routingResult, ex, requestHeaders, Collections.emptyList());
    RestLiResponse response = _responseHandler.buildPartialResponse(routingResult, responseData);
    checkResponse(response, 404, 2, // The response Content-Type should always be 'application/json'
    true, true, errorResponseHeaderName);
    DataMap dataMap = response.getDataMap();
    assertEquals(dataMap.getInteger("status"), Integer.valueOf(404));
    assertEquals(dataMap.getString("message"), "freeway not found");
    assertEquals(dataMap.getInteger("serviceErrorCode"), Integer.valueOf(237));
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) HashMap(java.util.HashMap) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) ByteString(com.linkedin.data.ByteString) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 14 with RestLiResponse

use of com.linkedin.restli.internal.server.response.RestLiResponse in project rest.li by linkedin.

the class TestRestLiResponseHandler method testFieldProjection_records.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicData")
public void testFieldProjection_records(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
    RestLiResponse response;
    // #1 all fields
    RestRequest request1 = buildRequest("/test?fields=f1,f2,f3", acceptTypeData.acceptHeaders, protocolVersion);
    Status status = buildStatusWithFields("f1", "f2", "f3");
    response = buildPartialRestResponse(request1, buildRoutingResult(request1, acceptTypeData.acceptHeaders), status);
    checkResponse(response, 200, 1, true, errorResponseHeaderName);
    checkProjectedFields(response, new String[] { "f1", "f2", "f3" }, new String[0]);
    // #2 some fields
    RestRequest request2 = buildRequest("/test?fields=f1,f3", acceptTypeData.acceptHeaders, protocolVersion);
    response = buildPartialRestResponse(request2, buildRoutingResult(request2, acceptTypeData.acceptHeaders), status);
    assertTrue(status.data().containsKey("f2"));
    checkResponse(response, 200, 1, true, errorResponseHeaderName);
    checkProjectedFields(response, new String[] { "f1", "f3" }, new String[] { "f2" });
    // #3 no fields
    RestRequest request3 = buildRequest("/test?fields=", acceptTypeData.acceptHeaders, protocolVersion);
    response = buildPartialRestResponse(request3, buildRoutingResult(request3, acceptTypeData.acceptHeaders), status);
    checkResponse(response, 200, 1, true, errorResponseHeaderName);
    checkProjectedFields(response, new String[] {}, new String[] { "f1", "f2", "f3" });
    assertTrue(status.data().containsKey("f1"));
    assertTrue(status.data().containsKey("f2"));
    assertTrue(status.data().containsKey("f3"));
    // #4 fields not in schema
    RestRequest request4 = buildRequest("/test?fields=f1,f99", acceptTypeData.acceptHeaders, protocolVersion);
    response = buildPartialRestResponse(request4, buildRoutingResult(request4, acceptTypeData.acceptHeaders), status);
    checkResponse(response, 200, 1, true, errorResponseHeaderName);
    checkProjectedFields(response, new String[] { "f1" }, new String[] { "f2", "f3", "f99" });
    assertTrue(status.data().containsKey("f2"));
    assertTrue(status.data().containsKey("f3"));
}
Also used : Status(com.linkedin.restli.server.twitter.TwitterTestDataModels.Status) HttpStatus(com.linkedin.restli.common.HttpStatus) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) Test(org.testng.annotations.Test)

Example 15 with RestLiResponse

use of com.linkedin.restli.internal.server.response.RestLiResponse in project rest.li by linkedin.

the class TestRestLiMethodInvocation method testInvokeWithInvalidAcceptMimeType.

@Test
public void testInvokeWithInvalidAcceptMimeType() throws Exception {
    RestRequestBuilder builder = new RestRequestBuilder(new URI("")).addHeaderValue("Accept", "foo").setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, version.toString());
    RestRequest request = builder.build();
    final CountDownLatch latch = new CountDownLatch(1);
    RestLiResponseHandler restLiResponseHandler = new RestLiResponseHandler(_methodAdapterProvider, _errorResponseBuilder);
    Callback<RestLiResponse> executionCallback = new Callback<RestLiResponse>() {

        @Override
        public void onError(Throwable e) {
            latch.countDown();
            Assert.assertTrue(e instanceof RestException);
            RestException ex = (RestException) e;
            Assert.assertEquals(ex.getResponse().getStatus(), HttpStatus.S_400_BAD_REQUEST.getCode());
        }

        @Override
        public void onSuccess(RestLiResponse result) {
        }
    };
    ServerResourceContext context = new ResourceContextImpl();
    try {
        RoutingResult routingResult = new RoutingResult(context, null);
        RestUtils.validateRequestHeadersAndUpdateResourceContext(request.getHeaders(), Collections.emptySet(), routingResult.getContext());
        FilterChainDispatcher filterChainDispatcher = new FilterChainDispatcherImpl(routingResult, _invoker, null);
        FilterChainCallback filterChainCallback = new FilterChainCallbackImpl(null, restLiResponseHandler, executionCallback, _errorResponseBuilder);
        final RestLiCallback callback = new RestLiCallback(null, new RestLiFilterResponseContextFactory(request, null, restLiResponseHandler), new RestLiFilterChain(null, filterChainDispatcher, filterChainCallback));
        _invoker.invoke(null, routingResult, null, callback);
        latch.await();
    } catch (Exception e) {
        // exception is expected
        Assert.assertTrue(e instanceof RestLiServiceException);
    }
    Assert.assertNull(context.getResponseMimeType());
}
Also used : FilterChainDispatcherImpl(com.linkedin.restli.internal.server.filter.FilterChainDispatcherImpl) RestException(com.linkedin.r2.message.rest.RestException) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) RestLiFilterChain(com.linkedin.restli.internal.server.filter.RestLiFilterChain) RestException(com.linkedin.r2.message.rest.RestException) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RoutingException(com.linkedin.restli.server.RoutingException) RestLiSyntaxException(com.linkedin.restli.internal.server.util.RestLiSyntaxException) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) FilterChainCallback(com.linkedin.restli.internal.server.filter.FilterChainCallback) RestLiFilterResponseContextFactory(com.linkedin.restli.internal.server.filter.RestLiFilterResponseContextFactory) FilterChainDispatcher(com.linkedin.restli.internal.server.filter.FilterChainDispatcher) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiResponseHandler(com.linkedin.restli.internal.server.response.RestLiResponseHandler) Callback(com.linkedin.common.callback.Callback) RestLiCallback(com.linkedin.restli.internal.server.RestLiCallback) FilterChainCallback(com.linkedin.restli.internal.server.filter.FilterChainCallback) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) FilterChainCallbackImpl(com.linkedin.restli.internal.server.filter.FilterChainCallbackImpl) RestLiCallback(com.linkedin.restli.internal.server.RestLiCallback) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

RestLiResponse (com.linkedin.restli.internal.server.response.RestLiResponse)27 Test (org.testng.annotations.Test)21 RestRequest (com.linkedin.r2.message.rest.RestRequest)18 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)14 DataMap (com.linkedin.data.DataMap)9 HttpStatus (com.linkedin.restli.common.HttpStatus)9 Status (com.linkedin.restli.server.twitter.TwitterTestDataModels.Status)9 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)8 ResourceContextImpl (com.linkedin.restli.internal.server.ResourceContextImpl)7 Callback (com.linkedin.common.callback.Callback)5 ByteString (com.linkedin.data.ByteString)5 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)5 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)5 URI (java.net.URI)5 RequestContext (com.linkedin.r2.message.RequestContext)4 RestLiResponseException (com.linkedin.restli.internal.server.response.RestLiResponseException)4 RestLiSyntaxException (com.linkedin.restli.internal.server.util.RestLiSyntaxException)4 AfterTest (org.testng.annotations.AfterTest)4 BeforeTest (org.testng.annotations.BeforeTest)4 RestResponse (com.linkedin.r2.message.rest.RestResponse)3