use of com.linkedin.restli.internal.server.response.RestLiResponse in project rest.li by linkedin.
the class TestRestLiResponseHandler method testFieldProjectionRecordsPALSyntax.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicData")
public void testFieldProjectionRecordsPALSyntax(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
RestLiResponse response;
DataMap data = new DataMap(asMap("f1", "value", "f2", new DataMap(asMap("f3", "value", "f4", "value"))));
// #1 all fields
Status status = new Status(data);
RestRequest request1 = buildRequest("/test?fields=f1,f2:(f3,f4)", acceptTypeData.acceptHeaders, protocolVersion);
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,f2:(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", "f2", "f3" }, new String[] { "f4" });
// #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", "f4" });
assertTrue(status.data().containsKey("f1"));
assertTrue(status.data().containsKey("f2"));
// #4 fields not in schema
RestRequest request4 = buildRequest("/test?fields=f2:(f99)", acceptTypeData.acceptHeaders, protocolVersion);
response = buildPartialRestResponse(request4, buildRoutingResult(request4, acceptTypeData.acceptHeaders), status);
checkResponse(response, 200, 1, true, errorResponseHeaderName);
checkProjectedFields(response, new String[] { "f2" }, new String[] { "f1", "f3", "f99" });
assertTrue(status.data().containsKey("f2"));
}
use of com.linkedin.restli.internal.server.response.RestLiResponse in project rest.li by linkedin.
the class TestRestLiResponseHandler method testActions.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "statusActionData")
public void testActions(AcceptTypeData acceptTypeData, String response1, String response2, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
final RestRequest request = buildRequest(acceptTypeData.acceptHeaders, protocolVersion);
RestLiResponse response;
// #1 simple record template
RoutingResult routing = buildRoutingResultAction(Status.class, request, acceptTypeData.acceptHeaders);
response = buildPartialRestResponse(request, routing, buildStatusRecord());
checkResponse(response, 200, 1, true, errorResponseHeaderName);
if (acceptTypeData != AcceptTypeData.PSON) {
assertEquals(DataMapUtils.mapToByteString(response.getDataMap(), response.getHeaders()).asAvroString(), response1);
}
RestResponse restResponse = ResponseUtils.buildResponse(routing, response);
assertEquals(restResponse.getEntity().asAvroString(), response1);
// #2 DataTemplate response
StringMap map = new StringMap();
map.put("key1", "value1");
map.put("key2", "value2");
response = buildPartialRestResponse(request, buildRoutingResultAction(StringMap.class, request, acceptTypeData.acceptHeaders), map);
checkResponse(response, 200, 1, true, errorResponseHeaderName);
// Convert both of these back into maps depending on their response type
final DataMap actualMap;
final DataMap expectedMap;
if (acceptTypeData == AcceptTypeData.PSON) {
routing = buildRoutingResultAction(StringMap.class, request, acceptTypeData.acceptHeaders);
restResponse = ResponseUtils.buildResponse(routing, response);
actualMap = PSON_DATA_CODEC.bytesToMap(restResponse.getEntity().copyBytes());
expectedMap = PSON_DATA_CODEC.bytesToMap(ByteString.copyAvroString(response2, false).copyBytes());
} else {
actualMap = JACKSON_DATA_CODEC.bytesToMap(DataMapUtils.mapToByteString(response.getDataMap(), response.getHeaders()).copyBytes());
expectedMap = JACKSON_DATA_CODEC.stringToMap(response2);
}
assertEquals(actualMap, expectedMap);
// #3 empty response
response = buildPartialRestResponse(request, buildRoutingResultAction(Void.TYPE, request, acceptTypeData.acceptHeaders), null);
checkResponse(response, 200, 1, false, errorResponseHeaderName);
assertNull(response.getDataMap());
}
use of com.linkedin.restli.internal.server.response.RestLiResponse in project rest.li by linkedin.
the class TestRestLiResponseHandler method testFieldProjection_batch.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicDataWithBatchUri")
public void testFieldProjection_batch(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String uri, String errorResponseHeaderName) throws Exception {
RestLiResponse response;
Map<Integer, Status> statusBatch = buildStatusBatchResponse(10, "f1", "f2", "f3");
RestRequest request = buildRequest(uri, acceptTypeData.acceptHeaders, protocolVersion);
response = buildPartialRestResponse(request, buildRoutingResult(ResourceMethod.BATCH_GET, request, acceptTypeData.acceptHeaders), statusBatch);
checkResponse(response, 200, 1, true, errorResponseHeaderName);
DataMap dataMap = response.getDataMap();
BatchResponse<Status> batchResponse = new BatchResponse<>(dataMap, Status.class);
assertEquals(batchResponse.getResults().size(), 10);
for (Status status : batchResponse.getResults().values()) {
assertTrue(status.data().containsKey("f1"));
assertTrue(status.data().containsKey("f2"));
assertFalse(status.data().containsKey("f3"));
}
// ensure that output map was not modified by rest.li!
Status status1 = statusBatch.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 testFieldProjection_collections_List.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicData")
public void testFieldProjection_collections_List(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
RestLiResponse response;
List<Status> statusCollection = buildStatusListResult(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.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 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);
}
Aggregations