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);
}
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);
}
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);
}
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"));
}
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);
}
Aggregations