Search in sources :

Example 91 with RoutingResult

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

the class TestActionArgumentBuilder method testExtractRequestDataFailure.

@Test(dataProvider = "failureData")
public void testExtractRequestDataFailure(String entity, List<Parameter<?>> params, String errorRegEx) throws IOException {
    RecordDataSchema dataSchema = DynamicRecordMetadata.buildSchema("testAction", params);
    RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, entity);
    ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(null, null, "testAction", dataSchema);
    RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 1, null, 1);
    RestLiArgumentBuilder argumentBuilder = new ActionArgumentBuilder();
    try {
        argumentBuilder.extractRequestData(routingResult, DataMapUtils.readMapWithExceptions(request));
        fail("Expected RoutingException");
    } catch (RoutingException e) {
        assertTrue(e.getMessage().matches(errorRegEx));
    }
    verify(request, descriptor, routingResult);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RoutingException(com.linkedin.restli.server.RoutingException) RestRequest(com.linkedin.r2.message.rest.RestRequest) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) Test(org.testng.annotations.Test)

Example 92 with RoutingResult

use of com.linkedin.restli.internal.server.RoutingResult 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 93 with RoutingResult

use of com.linkedin.restli.internal.server.RoutingResult 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 94 with RoutingResult

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

the class TestRestLiResponseHandler method invokeResponseHandler.

private RestLiResponse invokeResponseHandler(String path, Object body, ResourceMethod method, Map<String, String> headers, ProtocolVersion protocolVersion) throws Exception {
    RestRequest req = buildRequest(path, headers, protocolVersion);
    RoutingResult routing = buildRoutingResult(method, req, headers);
    return buildPartialRestResponse(req, routing, body);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest)

Example 95 with RoutingResult

use of com.linkedin.restli.internal.server.RoutingResult 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)

Aggregations

RoutingResult (com.linkedin.restli.internal.server.RoutingResult)114 Test (org.testng.annotations.Test)89 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)72 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)71 RestRequest (com.linkedin.r2.message.rest.RestRequest)56 RestLiRequestData (com.linkedin.restli.server.RestLiRequestData)29 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)28 RequestContext (com.linkedin.r2.message.RequestContext)24 ResourceContextImpl (com.linkedin.restli.internal.server.ResourceContextImpl)20 URI (java.net.URI)20 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)19 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)18 RoutingException (com.linkedin.restli.server.RoutingException)18 HashMap (java.util.HashMap)17 DataMap (com.linkedin.data.DataMap)16 Foo (com.linkedin.pegasus.generator.examples.Foo)16 RestLiResponse (com.linkedin.restli.internal.server.response.RestLiResponse)14 Parameter (com.linkedin.restli.internal.server.model.Parameter)12 RestLiTestHelper.buildResourceModel (com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel)11 Callback (com.linkedin.common.callback.Callback)10