use of com.linkedin.data.DataMap 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);
RestResponse response;
// #1 simple record template
response = _responseHandler.buildResponse(request, buildRoutingResultAction(Status.class, request, acceptTypeData.acceptHeaders), buildStatusRecord());
checkResponse(response, 200, 2, acceptTypeData.responseContentType, ActionResponse.class.getName(), Status.class.getName(), true, errorResponseHeaderName);
assertEquals(response.getEntity().asAvroString(), response1);
// #2 DataTemplate response
StringMap map = new StringMap();
map.put("key1", "value1");
map.put("key2", "value2");
response = _responseHandler.buildResponse(request, buildRoutingResultAction(StringMap.class, request, acceptTypeData.acceptHeaders), map);
checkResponse(response, 200, 2, acceptTypeData.responseContentType, ActionResponse.class.getName(), StringMap.class.getName(), true, errorResponseHeaderName);
//Convert both of these back into maps depending on their response type
final DataMap actualMap;
final DataMap expectedMap;
if (acceptTypeData == AcceptTypeData.PSON) {
actualMap = PSON_DATA_CODEC.bytesToMap(response.getEntity().copyBytes());
expectedMap = PSON_DATA_CODEC.bytesToMap(ByteString.copyAvroString(response2, false).copyBytes());
} else {
actualMap = JACKSON_DATA_CODEC.bytesToMap(response.getEntity().copyBytes());
expectedMap = JACKSON_DATA_CODEC.stringToMap(response2);
}
assertEquals(actualMap, expectedMap);
// #3 empty response
response = _responseHandler.buildResponse(request, buildRoutingResultAction(Void.TYPE, request, acceptTypeData.acceptHeaders), null);
checkResponse(response, 200, 1, null, null, null, false, errorResponseHeaderName);
assertEquals(response.getEntity().asAvroString(), "");
}
use of com.linkedin.data.DataMap in project rest.li by linkedin.
the class TestRestLiResponseHandler method buildStatusRecord.
private Status buildStatusRecord() {
DataMap map = new DataMap();
map.put("text", "test status");
Status status = new Status(map);
return status;
}
use of com.linkedin.data.DataMap in project rest.li by linkedin.
the class TestRestLiResponseHandler method buildRoutingResultAction.
/**
* Creates a RoutingResult for an Action with the given returnType.
*
* @param actionReturnType the return type of the action.
* @return a RoutingResult
*/
private final RoutingResult buildRoutingResultAction(Class<?> actionReturnType, RestRequest request, Map<String, String> headers) throws NoSuchMethodException, RestLiSyntaxException, URISyntaxException {
if (actionReturnType == Void.class) {
actionReturnType = Void.TYPE;
}
// actual method passed in is irrelevant, since we are constructing a ResourceMethodDescriptor by hand.
Method method = ProjectionTestFixture.class.getMethod("batchGet", Set.class);
ResourceModel model = RestLiTestHelper.buildResourceModel(StatusCollectionResource.class);
String actionName = "return" + actionReturnType.getSimpleName();
List<Parameter<?>> parameters = Collections.<Parameter<?>>emptyList();
RecordDataSchema actionReturnRecordDataSchema;
FieldDef<?> returnFieldDef;
if (actionReturnType != Void.TYPE) {
@SuppressWarnings({ "unchecked", "rawtypes" }) FieldDef<?> nonVoidFieldDef = new FieldDef(ActionResponse.VALUE_NAME, actionReturnType, DataTemplateUtil.getSchema(actionReturnType));
returnFieldDef = nonVoidFieldDef;
actionReturnRecordDataSchema = DynamicRecordMetadata.buildSchema(actionName, Collections.singleton(returnFieldDef));
} else {
returnFieldDef = null;
actionReturnRecordDataSchema = DynamicRecordMetadata.buildSchema(actionName, Collections.<FieldDef<?>>emptyList());
}
ResourceMethodDescriptor methodDescriptor = ResourceMethodDescriptor.createForAction(method, parameters, actionName, ResourceLevel.COLLECTION, returnFieldDef, actionReturnRecordDataSchema, DynamicRecordMetadata.buildSchema(actionName, parameters), InterfaceType.SYNC, new DataMap());
model.addResourceMethodDescriptor(methodDescriptor);
ServerResourceContext resourceContext = new ResourceContextImpl(new PathKeysImpl(), request, new RequestContext());
RestUtils.validateRequestHeadersAndUpdateResourceContext(headers, resourceContext);
return new RoutingResult(resourceContext, methodDescriptor);
}
use of com.linkedin.data.DataMap 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 {
RestResponse response;
List<Status> statusCollection = buildStatusListResult(10, "f1", "f2", "f3");
RestRequest request = buildRequest("/test?fields=f1,f2", acceptTypeData.acceptHeaders, protocolVersion);
response = _responseHandler.buildResponse(request, buildRoutingResultFinder(request, acceptTypeData.acceptHeaders), statusCollection);
checkResponse(response, 200, 2, acceptTypeData.responseContentType, CollectionResponse.class.getName(), null, true, errorResponseHeaderName);
DataMap dataMap = acceptTypeData.dataCodec.readMap(response.getEntity().asInputStream());
CollectionResponse<Status> collectionResponse = new CollectionResponse<Status>(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.data.DataMap in project rest.li by linkedin.
the class TestRestLiResponseHandler method buildStatusWithFields.
private Status buildStatusWithFields(String... fields) {
DataMap map = new DataMap();
for (String field : fields) {
map.put(field, "value");
}
Status status = new Status(map);
return status;
}
Aggregations