use of com.linkedin.restli.internal.server.model.ResourceMethodDescriptor in project rest.li by linkedin.
the class TestBatchGetResponseBuilder method testProjectionInBuildRestliResponseData.
@Test
public void testProjectionInBuildRestliResponseData() {
MaskTree maskTree = new MaskTree();
maskTree.addOperation(new PathSpec("fruitsField"), MaskOperation.POSITIVE_MASK_OP);
ServerResourceContext mockContext = EasyMock.createMock(ServerResourceContext.class);
EasyMock.expect(mockContext.hasParameter(RestConstants.ALT_KEY_PARAM)).andReturn(false);
EasyMock.expect(mockContext.getProjectionMode()).andReturn(ProjectionMode.AUTOMATIC);
EasyMock.expect(mockContext.getProjectionMask()).andReturn(maskTree);
EasyMock.expect(mockContext.getBatchKeyErrors()).andReturn(Collections.<Object, RestLiServiceException>emptyMap()).once();
EasyMock.replay(mockContext);
ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
Map<Integer, Foo> results = new HashMap<Integer, Foo>();
Foo value = new Foo().setStringField("value").setFruitsField(Fruits.APPLE);
results.put(1, value);
BatchGetResponseBuilder responseBuilder = new BatchGetResponseBuilder(new ErrorResponseBuilder());
RestLiResponseData responseData = responseBuilder.buildRestLiResponseData(null, routingResult, results, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
RecordTemplate record = responseData.getBatchResponseEnvelope().getBatchResponseMap().get(1).getRecord();
Assert.assertEquals(record.data().size(), 1);
Assert.assertEquals(record.data().get("fruitsField"), Fruits.APPLE.toString());
EasyMock.verify(mockContext);
}
use of com.linkedin.restli.internal.server.model.ResourceMethodDescriptor in project rest.li by linkedin.
the class TestActionArgumentBuilder method testArgumentBuilderSuccess.
@Test(dataProvider = "successData")
public void testArgumentBuilderSuccess(String entity, List<Parameter<?>> params, Object[] expectedArgs) {
RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, entity, 3);
ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(null, params, null, null);
ResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(null, null, null, true);
RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 2, context, 1);
RestLiArgumentBuilder argumentBuilder = new ActionArgumentBuilder();
RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, request);
Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
assertEquals(args, expectedArgs);
verify(request, descriptor, routingResult);
}
use of com.linkedin.restli.internal.server.model.ResourceMethodDescriptor in project rest.li by linkedin.
the class TestArgumentBuilder method testBuildArgsHappyPath.
@Test
public void testBuildArgsHappyPath() {
//test integer association key integer
String param1Key = "param1";
Parameter<Integer> param1 = new Parameter<Integer>(param1Key, Integer.class, DataTemplateUtil.getSchema(Integer.class), false, null, Parameter.ParamType.ASSOC_KEY_PARAM, false, AnnotationSet.EMPTY);
Integer param1Value = 123;
//test regular string argument
String param2Key = "param2";
Parameter<String> param2 = new Parameter<String>(param2Key, String.class, DataTemplateUtil.getSchema(String.class), true, null, Parameter.ParamType.QUERY, true, AnnotationSet.EMPTY);
String param2Value = "param2Value";
//test data template argument array with more than element
String param3Key = "param3";
Parameter<StringArray> param3 = new Parameter<StringArray>(param3Key, StringArray.class, DataTemplateUtil.getSchema(StringArray.class), true, null, Parameter.ParamType.QUERY, true, AnnotationSet.EMPTY);
DataList param3Value = new DataList(Arrays.asList("param3a", "param3b"));
StringArray param3Final = new StringArray(param3Value);
//test data template argument array with only one element
String param4Key = "param4";
Parameter<StringArray> param4 = new Parameter<StringArray>(param4Key, StringArray.class, DataTemplateUtil.getSchema(StringArray.class), true, null, Parameter.ParamType.QUERY, true, AnnotationSet.EMPTY);
String param4Value = "param4Value";
StringArray param4Final = new StringArray(new DataList(Collections.singletonList(param4Value)));
// test record template
String param5Key = "param5";
Parameter<TestRecord> param5 = new Parameter<TestRecord>(param5Key, TestRecord.class, DataTemplateUtil.getSchema(TestRecord.class), true, null, Parameter.ParamType.QUERY, true, AnnotationSet.EMPTY);
DataMap param5Value = new DataMap();
param5Value.put("doubleField", "5.5");
param5Value.put("floatField", "5");
param5Value.put("intField", "5");
param5Value.put("longField", "5");
TestRecord param5Final = new TestRecord();
param5Final.setDoubleField(5.5);
param5Final.setFloatField(5F);
param5Final.setIntField(5);
param5Final.setLongField(5);
// test record template array
String param6Key = "param6";
Parameter<TestRecordArray> param6 = new Parameter<TestRecordArray>(param6Key, TestRecordArray.class, DataTemplateUtil.getSchema(TestRecordArray.class), true, null, Parameter.ParamType.QUERY, true, AnnotationSet.EMPTY);
DataList param6Value = new DataList();
DataMap testRecordDataMap1 = new DataMap();
testRecordDataMap1.put("doubleField", "6.6");
testRecordDataMap1.put("floatField", "6");
testRecordDataMap1.put("intField", "6");
testRecordDataMap1.put("longField", "6");
DataMap testRecordDataMap2 = new DataMap();
testRecordDataMap2.put("doubleField", "66.6");
testRecordDataMap2.put("floatField", "66");
testRecordDataMap2.put("intField", "66");
testRecordDataMap2.put("longField", "66");
param6Value.add(testRecordDataMap1);
param6Value.add(testRecordDataMap2);
TestRecordArray param6Final = new TestRecordArray();
TestRecord testRecord1 = new TestRecord();
testRecord1.setDoubleField(6.6);
testRecord1.setFloatField(6);
testRecord1.setIntField(6);
testRecord1.setLongField(6);
TestRecord testRecord2 = new TestRecord();
testRecord2.setDoubleField(66.6);
testRecord2.setFloatField(66);
testRecord2.setIntField(66);
testRecord2.setLongField(66);
param6Final.add(testRecord1);
param6Final.add(testRecord2);
List<Parameter<?>> parameters = new ArrayList<Parameter<?>>();
parameters.add(param1);
parameters.add(param2);
parameters.add(param3);
parameters.add(param4);
parameters.add(param5);
parameters.add(param6);
Object[] positionalArguments = new Object[0];
Capture<String> param1Capture = new Capture<String>();
Capture<String> param2Capture = new Capture<String>();
Capture<String> param3Capture = new Capture<String>();
Capture<String> param4Capture = new Capture<String>();
Capture<String> param5Capture = new Capture<String>();
Capture<String> param6Capture = new Capture<String>();
ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
EasyMock.expect(mockResourceContext.getRequestAttachmentReader()).andReturn(null);
MutablePathKeys mockPathKeys = EasyMock.createMock(MutablePathKeys.class);
ResourceMethodDescriptor mockResourceMethodDescriptor = getMockResourceMethod(parameters);
//easy mock for processing param1
EasyMock.expect(mockPathKeys.get(EasyMock.capture(param1Capture))).andReturn(param1Value);
EasyMock.expect(mockResourceContext.getPathKeys()).andReturn(mockPathKeys);
//easy mock for processing param2
EasyMock.expect(mockResourceContext.getParameter(EasyMock.capture(param2Capture))).andReturn(param2Value);
//easy mock for processing param3
EasyMock.expect(mockResourceContext.getStructuredParameter(EasyMock.capture(param3Capture))).andReturn(param3Value);
//easy mock for processing param4
EasyMock.expect(mockResourceContext.getStructuredParameter(EasyMock.capture(param4Capture))).andReturn(param4Value);
//easy mock for processing param5
EasyMock.expect(mockResourceContext.getStructuredParameter(EasyMock.capture(param5Capture))).andReturn(param5Value);
//easy mock for processing param6
EasyMock.expect(mockResourceContext.getStructuredParameter(EasyMock.capture(param6Capture))).andReturn(param6Value);
EasyMock.replay(mockResourceContext, mockPathKeys);
Object[] results = ArgumentBuilder.buildArgs(positionalArguments, mockResourceMethodDescriptor, mockResourceContext, null);
EasyMock.verify(mockPathKeys, mockResourceContext, mockResourceMethodDescriptor);
Assert.assertEquals(param1Capture.getValue(), param1Key);
Assert.assertEquals(param2Capture.getValue(), param2Key);
Assert.assertEquals(param3Capture.getValue(), param3Key);
Assert.assertEquals(param4Capture.getValue(), param4Key);
Assert.assertEquals(param5Capture.getValue(), param5Key);
Assert.assertEquals(param6Capture.getValue(), param6Key);
Assert.assertEquals(results[0], param1Value);
Assert.assertEquals(results[1], param2Value);
Assert.assertEquals(results[2], param3Final);
Assert.assertEquals(results[3], param4Final);
Assert.assertEquals(results[4], param5Final);
Assert.assertEquals(results[5], param6Final);
}
use of com.linkedin.restli.internal.server.model.ResourceMethodDescriptor in project rest.li by linkedin.
the class TestBatchCreateArgumentBuilder method testArgumentBuilderSuccess.
@Test
public void testArgumentBuilderSuccess() {
RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, "{\"elements\":[{\"b\":123,\"a\":\"abc\"},{\"b\":5678,\"a\":\"xyzw\"}]}", 1);
ResourceModel model = RestLiArgumentBuilderTestHelper.getMockResourceModel(MyComplexKey.class, null, false);
@SuppressWarnings("rawtypes") Parameter<BatchCreateRequest> param = new Parameter<BatchCreateRequest>("", BatchCreateRequest.class, null, false, null, Parameter.ParamType.BATCH, false, new AnnotationSet(new Annotation[] {}));
ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, param);
ResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(null, null, null, true);
RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 2, context, 1);
RestLiArgumentBuilder argumentBuilder = new BatchCreateArgumentBuilder();
RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, request);
Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
assertEquals(args.length, 1);
assertTrue(args[0] instanceof BatchCreateRequest);
@SuppressWarnings("unchecked") List<MyComplexKey> entities = ((BatchCreateRequest<Integer, MyComplexKey>) args[0]).getInput();
assertEquals(entities.size(), 2);
assertEquals(entities.get(0).getA(), "abc");
assertEquals((long) entities.get(0).getB(), 123L);
assertEquals(entities.get(1).getA(), "xyzw");
assertEquals((long) entities.get(1).getB(), 5678L);
verify(request, model, descriptor, context, routingResult);
}
use of com.linkedin.restli.internal.server.model.ResourceMethodDescriptor in project rest.li by linkedin.
the class TestBatchGetArgumentBuilder method testArgumentBuilderSuccess.
@Test(dataProvider = "argumentData")
public void testArgumentBuilderSuccess(Set<Object> batchKeys) {
@SuppressWarnings("rawtypes") Parameter<Set> param = new Parameter<Set>("", Set.class, null, false, null, Parameter.ParamType.BATCH, false, new AnnotationSet(new Annotation[] {}));
ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(null, param);
ResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(null, null, batchKeys, true);
RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 1, context, 2);
RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, null, 0);
RestLiArgumentBuilder argumentBuilder = new BatchGetArgumentBuilder();
RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, request);
Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
Object[] expectedArgs = new Object[] { batchKeys };
assertEquals(args, expectedArgs);
verify(descriptor, context, routingResult, request);
}
Aggregations