Search in sources :

Example 16 with RestLiResponse

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

the class TestRestLiResponseHandler method testBasicResponses.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "statusData")
public void testBasicResponses(AcceptTypeData acceptTypeData, String expectedStatus, ProtocolVersion protocolVersion, String errorResponseHeaderName, String idHeaderName) throws Exception {
    RestLiResponse response;
    // #1 simple record template
    response = invokeResponseHandler("/test", buildStatusRecord(), ResourceMethod.GET, acceptTypeData.acceptHeaders, protocolVersion);
    checkResponse(response, 200, 1, true, errorResponseHeaderName);
    if (acceptTypeData != AcceptTypeData.PSON && acceptTypeData != AcceptTypeData.PROTOBUF) {
        assertEquals(DataMapUtils.mapToByteString(response.getDataMap(), response.getHeaders()).asAvroString(), expectedStatus);
    }
    RestRequest req = buildRequest("/test", acceptTypeData.acceptHeaders, protocolVersion);
    RoutingResult routing = buildRoutingResult(ResourceMethod.GET, req, acceptTypeData.acceptHeaders);
    RestResponse restResponse = ResponseUtils.buildResponse(routing, response);
    assertEquals(restResponse.getEntity().asAvroString(), expectedStatus);
    // #2 create (with id)
    response = invokeResponseHandler("/test", new CreateResponse(1), ResourceMethod.CREATE, acceptTypeData.acceptHeaders, protocolVersion);
    checkResponse(response, 201, 3, false, errorResponseHeaderName);
    assertEquals(response.getHeader(RestConstants.HEADER_LOCATION), "/test/1");
    assertEquals(response.getHeader(idHeaderName), "1");
    // #2.1 create (without id)
    response = invokeResponseHandler("/test", new CreateResponse(HttpStatus.S_201_CREATED), ResourceMethod.CREATE, acceptTypeData.acceptHeaders, protocolVersion);
    checkResponse(response, 201, 1, false, errorResponseHeaderName);
    // #2.2 create (with id and slash at the end of uri)
    response = invokeResponseHandler("/test/", new CreateResponse(1), ResourceMethod.CREATE, acceptTypeData.acceptHeaders, protocolVersion);
    checkResponse(response, 201, 3, false, errorResponseHeaderName);
    assertEquals(response.getHeader(RestConstants.HEADER_LOCATION), "/test/1");
    assertEquals(response.getHeader(idHeaderName), "1");
    // #2.3 create (without id and slash at the end of uri)
    response = invokeResponseHandler("/test/", new CreateResponse(HttpStatus.S_201_CREATED), ResourceMethod.CREATE, acceptTypeData.acceptHeaders, protocolVersion);
    checkResponse(response, 201, 1, false, errorResponseHeaderName);
    // #3 update
    response = invokeResponseHandler("/test", new UpdateResponse(HttpStatus.S_204_NO_CONTENT), ResourceMethod.UPDATE, acceptTypeData.acceptHeaders, protocolVersion);
    checkResponse(response, 204, 1, false, errorResponseHeaderName);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) UpdateResponse(com.linkedin.restli.server.UpdateResponse) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) CreateResponse(com.linkedin.restli.server.CreateResponse) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) Test(org.testng.annotations.Test)

Example 17 with RestLiResponse

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

the class TestRestLiResponseHandler method testCollections.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicData")
public void testCollections(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
    ResourceModel resourceModel = buildResourceModel(StatusCollectionResource.class);
    ResourceMethodDescriptor methodDescriptor = resourceModel.findFinderMethod("search");
    RestLiResponse response;
    // #1 check datamap/entity structure
    ServerResourceContext context = new ResourceContextImpl();
    RestUtils.validateRequestHeadersAndUpdateResourceContext(acceptTypeData.acceptHeaders, Collections.emptySet(), context);
    RoutingResult routingResult = new RoutingResult(context, methodDescriptor);
    response = buildPartialRestResponse(buildRequest(acceptTypeData.acceptHeaders, protocolVersion), routingResult, buildStatusList(3));
    checkResponse(response, 200, 1, true, errorResponseHeaderName);
    String baseUri = "/test?someParam=foo";
    // #1.1 using CollectionResult
    response = invokeResponseHandler(baseUri + "&start=0&count=5", methodDescriptor, new BasicCollectionResult<>(buildStatusList(5)), acceptTypeData.acceptHeaders, protocolVersion);
    checkCollectionResponse(response, 5, 0, 5, 1, null, null, null, acceptTypeData);
    // #1.1 using CollectionResult (with total)
    response = invokeResponseHandler(baseUri + "&start=0&count=5", methodDescriptor, new BasicCollectionResult<>(buildStatusList(5), 10), acceptTypeData.acceptHeaders, protocolVersion);
    checkCollectionResponse(response, 5, 0, 5, 1, 10, null, null, acceptTypeData);
    // using CollectionResult with metadata RecordTemplate
    CollectionMetadata metadata = new CollectionMetadata();
    metadata.setCount(42);
    response = invokeResponseHandler(baseUri + "&start=0&count=5", methodDescriptor, new CollectionResult<>(buildStatusList(5), 10, metadata), acceptTypeData.acceptHeaders, protocolVersion);
    checkCollectionResponse(response, 5, 0, 5, 1, 10, null, null, acceptTypeData);
    DataMap dataMap = response.getDataMap();
    CollectionResponse<Status> collectionResponse = new CollectionResponse<>(dataMap, Status.class);
    assertEquals(new CollectionMetadata(collectionResponse.getMetadataRaw()), metadata);
    // #2 pagination: first page, no next
    response = invokeResponseHandler(baseUri + "&start=0&count=5", methodDescriptor, buildStatusList(3), acceptTypeData.acceptHeaders, protocolVersion);
    checkCollectionResponse(response, 3, 0, 5, 0, null, null, null, acceptTypeData);
    // #3 pagination: first page, has next (boundary case)
    response = invokeResponseHandler(baseUri + "&start=0&count=5", methodDescriptor, buildStatusList(5), acceptTypeData.acceptHeaders, protocolVersion);
    // "/test?count=5&start=5&someParam=foo"
    final Map<String, String> queryParamsMap3next = new HashMap<>();
    queryParamsMap3next.put("count", "5");
    queryParamsMap3next.put("start", "5");
    queryParamsMap3next.put("someParam", "foo");
    final URIDetails expectedURIDetails3next = new URIDetails(protocolVersion, "/test", null, queryParamsMap3next, null);
    checkCollectionResponse(response, 5, 0, 5, 1, null, null, expectedURIDetails3next, acceptTypeData);
    // #4 pagination: second page, has prev/ext
    response = invokeResponseHandler(baseUri + "&start=5&count=5", methodDescriptor, buildStatusList(5), acceptTypeData.acceptHeaders, protocolVersion);
    // "/test?count=5&start=0&someParam=foo", "/test?count=5&start=10&someParam=foo",
    final Map<String, String> queryParamsMap4prev = new HashMap<>();
    queryParamsMap4prev.put("count", "5");
    queryParamsMap4prev.put("start", "0");
    queryParamsMap4prev.put("someParam", "foo");
    final URIDetails expectedURIDetails4prev = new URIDetails(protocolVersion, "/test", null, queryParamsMap4prev, null);
    final Map<String, String> queryParamsMap4next = new HashMap<>();
    queryParamsMap4next.put("count", "5");
    queryParamsMap4next.put("start", "10");
    queryParamsMap4next.put("someParam", "foo");
    final URIDetails expectedURIDetails4next = new URIDetails(protocolVersion, "/test", null, queryParamsMap4next, null);
    checkCollectionResponse(response, 5, 5, 5, 2, null, expectedURIDetails4prev, expectedURIDetails4next, acceptTypeData);
    // #5 pagination:last page, has prev
    response = invokeResponseHandler(baseUri + "&start=10&count=5", methodDescriptor, buildStatusList(4), acceptTypeData.acceptHeaders, protocolVersion);
    // "/test?count=5&start=5&someParam=foo"
    final Map<String, String> queryParamsMap5prev = new HashMap<>();
    queryParamsMap5prev.put("count", "5");
    queryParamsMap5prev.put("start", "5");
    queryParamsMap5prev.put("someParam", "foo");
    final URIDetails expectedURIDetails5prev = new URIDetails(protocolVersion, "/test", null, queryParamsMap5prev, null);
    checkCollectionResponse(response, 4, 10, 5, 1, null, expectedURIDetails5prev, null, acceptTypeData);
    response = invokeResponseHandler(baseUri + "&start=10&count=5", methodDescriptor, new BasicCollectionResult<>(buildStatusList(4), 15), acceptTypeData.acceptHeaders, protocolVersion);
    // "/test?count=5&start=5&someParam=foo", "/test?count=5&start=14&someParam=foo"
    final Map<String, String> queryParamsMap6prev = new HashMap<>();
    queryParamsMap6prev.put("count", "5");
    queryParamsMap6prev.put("start", "5");
    queryParamsMap6prev.put("someParam", "foo");
    final URIDetails expectedURIDetails6prev = new URIDetails(protocolVersion, "/test", null, queryParamsMap6prev, null);
    final Map<String, String> queryParamsMap6next = new HashMap<>();
    queryParamsMap6next.put("count", "5");
    queryParamsMap6next.put("start", "14");
    queryParamsMap6next.put("someParam", "foo");
    final URIDetails expectedURIDetails6next = new URIDetails(protocolVersion, "/test", null, queryParamsMap6next, null);
    checkCollectionResponse(response, 4, 10, 5, 2, 15, expectedURIDetails6prev, expectedURIDetails6next, acceptTypeData);
    response = invokeResponseHandler(baseUri + "&start=10&count=5", methodDescriptor, new BasicCollectionResult<>(buildStatusList(4), 14), acceptTypeData.acceptHeaders, protocolVersion);
    // "/test?count=5&start=5&someParam=foo"
    final Map<String, String> queryParamsMap7prev = new HashMap<>();
    queryParamsMap7prev.put("count", "5");
    queryParamsMap7prev.put("start", "5");
    queryParamsMap7prev.put("someParam", "foo");
    final URIDetails expectedURIDetails7prev = new URIDetails(protocolVersion, "/test", null, queryParamsMap7prev, null);
    checkCollectionResponse(response, 4, 10, 5, 1, 14, expectedURIDetails7prev, null, acceptTypeData);
}
Also used : Status(com.linkedin.restli.server.twitter.TwitterTestDataModels.Status) HttpStatus(com.linkedin.restli.common.HttpStatus) CollectionMetadata(com.linkedin.restli.common.CollectionMetadata) URIDetails(com.linkedin.restli.internal.testutils.URIDetails) HashMap(java.util.HashMap) CollectionResponse(com.linkedin.restli.common.CollectionResponse) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) ByteString(com.linkedin.data.ByteString) DataMap(com.linkedin.data.DataMap) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) CollectionResult(com.linkedin.restli.server.CollectionResult) BasicCollectionResult(com.linkedin.restli.server.BasicCollectionResult) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) BasicCollectionResult(com.linkedin.restli.server.BasicCollectionResult) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiTestHelper.buildResourceModel(com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) Test(org.testng.annotations.Test)

Example 18 with RestLiResponse

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

the class TestRestLiResponseHandler method testCustomAcceptHeaders.

@Test
public void testCustomAcceptHeaders() throws Exception {
    Map<String, String> customAcceptHeaders = Collections.singletonMap("Accept", "application/json+2.0");
    // check response with out codec support (expect 406 error)
    try {
        invokeResponseHandler("/test", buildStatusRecord(), ResourceMethod.GET, customAcceptHeaders, AllProtocolVersions.LATEST_PROTOCOL_VERSION);
        Assert.fail();
    } catch (RestLiServiceException e) {
        Assert.assertEquals(e.getStatus().getCode(), 406);
    }
    // check response without creating a custom codec (expect 406 error)
    try {
        RestRequest req = buildRequest("/test", customAcceptHeaders, AllProtocolVersions.LATEST_PROTOCOL_VERSION);
        RoutingResult routing = buildRoutingResult(ResourceMethod.GET, req, customAcceptHeaders, Sets.newHashSet("application/json+2.0"));
        RestLiResponse restLiResponse = buildPartialRestResponse(req, routing, buildStatusRecord());
        ResponseUtils.buildResponse(routing, restLiResponse);
        Assert.fail();
    } catch (RestLiServiceException e) {
        Assert.assertEquals(e.getStatus().getCode(), 406);
    }
    // Register custom codec
    ContentType.createContentType("application/json+2.0", JACKSON_DATA_CODEC);
    RestRequest req = buildRequest("/test", customAcceptHeaders, AllProtocolVersions.LATEST_PROTOCOL_VERSION);
    RoutingResult routing = buildRoutingResult(ResourceMethod.GET, req, customAcceptHeaders, Sets.newHashSet("application/json+2.0"));
    RestLiResponse response = buildPartialRestResponse(req, routing, buildStatusRecord());
    checkResponse(response, 200, 1, true, RestConstants.HEADER_RESTLI_ERROR_RESPONSE);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) ByteString(com.linkedin.data.ByteString) Test(org.testng.annotations.Test)

Example 19 with RestLiResponse

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

the class TestRestLiResponseHandler method testFieldProjection_collections_CollectionResult.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicData")
public void testFieldProjection_collections_CollectionResult(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
    RestLiResponse response;
    BasicCollectionResult<Status> statusCollection = buildStatusCollectionResult(10, "f1", "f2", "f3");
    RestRequest request = buildRequest("/test?fields=f1,f2", acceptTypeData.acceptHeaders, protocolVersion);
    response = buildPartialRestResponse(request, buildRoutingResultFinder(request, acceptTypeData.acceptHeaders), statusCollection);
    checkResponse(response, 200, 1, true, errorResponseHeaderName);
    DataMap dataMap = response.getDataMap();
    CollectionResponse<Status> collectionResponse = new CollectionResponse<>(dataMap, Status.class);
    assertEquals(collectionResponse.getElements().size(), 10);
    for (Status status : collectionResponse.getElements()) {
        assertTrue(status.data().containsKey("f1"));
        assertTrue(status.data().containsKey("f2"));
        assertFalse(status.data().containsKey("f3"));
    }
    // ensure that output status objects were not modified by rest.li!
    Status status1 = statusCollection.getElements().get(1);
    assertNotNull(status1);
    assertTrue(status1.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) CollectionResponse(com.linkedin.restli.common.CollectionResponse) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 20 with RestLiResponse

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

the class TestRestLiResponseHandler method testWrapperResults.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "statusesData")
public void testWrapperResults(AcceptTypeData acceptTypeData, String expectedStatus, String expectedActionStatus, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
    RestLiResponse response;
    final Status status = buildStatusRecord();
    final GetResult<Status> getResult = new GetResult<>(status, HttpStatus.S_500_INTERNAL_SERVER_ERROR);
    response = invokeResponseHandler("/test", getResult, ResourceMethod.GET, acceptTypeData.acceptHeaders, protocolVersion);
    checkResponse(response, HttpStatus.S_500_INTERNAL_SERVER_ERROR.getCode(), 1, true, errorResponseHeaderName);
    if (acceptTypeData != AcceptTypeData.PSON) {
        assertEquals(DataMapUtils.mapToByteString(response.getDataMap(), response.getHeaders()).asAvroString(), expectedStatus);
    }
    RestRequest req = buildRequest("/test", acceptTypeData.acceptHeaders, protocolVersion);
    RoutingResult routing = buildRoutingResult(ResourceMethod.GET, req, acceptTypeData.acceptHeaders);
    RestResponse restResponse = ResponseUtils.buildResponse(routing, response);
    assertEquals(restResponse.getEntity().asAvroString(), expectedStatus);
    final RestRequest request = buildRequest(acceptTypeData.acceptHeaders, protocolVersion);
    final ActionResult<Status> actionResult = new ActionResult<>(status, HttpStatus.S_500_INTERNAL_SERVER_ERROR);
    routing = buildRoutingResultAction(Status.class, request, acceptTypeData.acceptHeaders);
    response = buildPartialRestResponse(request, routing, actionResult);
    checkResponse(response, HttpStatus.S_500_INTERNAL_SERVER_ERROR.getCode(), 1, true, errorResponseHeaderName);
    if (acceptTypeData != AcceptTypeData.PSON) {
        assertEquals(DataMapUtils.mapToByteString(response.getDataMap(), response.getHeaders()).asAvroString(), expectedActionStatus);
    }
    restResponse = ResponseUtils.buildResponse(routing, response);
    assertEquals(restResponse.getEntity().asAvroString(), expectedActionStatus);
}
Also used : Status(com.linkedin.restli.server.twitter.TwitterTestDataModels.Status) HttpStatus(com.linkedin.restli.common.HttpStatus) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) GetResult(com.linkedin.restli.server.GetResult) ActionResult(com.linkedin.restli.server.ActionResult) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) Test(org.testng.annotations.Test)

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