use of com.linkedin.restli.internal.server.model.Parameter in project rest.li by linkedin.
the class TestGetArgumentBuilder method testKeyArguments.
@Test(dataProvider = "keyArgumentData")
public void testKeyArguments(Parameter<?> param, String keyName, Object keyValue, final DataSchema keySchema) throws IOException {
ResourceModel model;
if (keyName != null) {
Key key = new Key(keyName, keyValue.getClass(), keySchema);
model = RestLiArgumentBuilderTestHelper.getMockResourceModel(null, key, false);
} else {
model = RestLiArgumentBuilderTestHelper.getMockResourceModel(null, null, true);
}
ResourceMethodDescriptor descriptor;
if (param != null) {
List<Parameter<?>> paramList = new ArrayList<>();
paramList.add(param);
descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 2, paramList);
} else {
descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, null);
}
ServerResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(keyName, keyValue, null, true);
RoutingResult routingResult;
if (param != null) {
routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 3, context, 2);
} else {
routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 2, context, 1);
}
RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, null);
RestLiArgumentBuilder argumentBuilder = new GetArgumentBuilder();
RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, null);
Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
Object[] expectedArgs;
if (keyValue == null) {
expectedArgs = new Object[] {};
} else {
expectedArgs = new Object[] { keyValue };
}
assertEquals(args, expectedArgs);
verify(model, descriptor, context, routingResult, request);
}
use of com.linkedin.restli.internal.server.model.Parameter in project rest.li by linkedin.
the class TestGetArgumentBuilder method testHeaderArgument.
@Test
public void testHeaderArgument() throws IOException {
String keyName = "myComplexKeyCollectionId";
Object keyValue = Integer.valueOf(123);
DataSchema keySchema = new IntegerDataSchema();
Key key = new Key(keyName, keyValue.getClass(), keySchema);
Map<String, String> headers = new HashMap<>();
String headerString = "An extra string.";
headers.put("extra", headerString);
List<Parameter<?>> headerParams = new ArrayList<>();
headerParams.add(getIntegerParam());
HeaderParam annotation = createMock(HeaderParam.class);
expect(annotation.value()).andReturn("extra");
AnnotationSet annotationSet = createMock(AnnotationSet.class);
expect(annotationSet.getAll()).andReturn(new Annotation[] {});
expect(annotationSet.get(HeaderParam.class)).andReturn(annotation);
replay(annotation, annotationSet);
Parameter<String> headerParam = new Parameter<>("", String.class, null, false, null, Parameter.ParamType.HEADER, false, annotationSet);
headerParams.add(headerParam);
ResourceModel model = RestLiArgumentBuilderTestHelper.getMockResourceModel(null, key, false);
ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 2, headerParams);
ServerResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(keyName, keyValue, null, headers, true);
RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 3, context, 2);
RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, null);
RestLiArgumentBuilder argumentBuilder = new GetArgumentBuilder();
RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, null);
Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
Object[] expectedArgs = new Object[] { keyValue, headerString };
assertEquals(args, expectedArgs);
verify(model, descriptor, context, routingResult, request, annotation, annotationSet);
}
use of com.linkedin.restli.internal.server.model.Parameter in project rest.li by linkedin.
the class TestUpdateArgumentBuilder method argumentData.
@DataProvider(name = "argumentData")
private Object[][] argumentData() {
Parameter<?> myComplexKeyParam = new Parameter<>("", MyComplexKey.class, DataTemplateUtil.getSchema(MyComplexKey.class), false, null, Parameter.ParamType.POST, false, new AnnotationSet(new Annotation[] {}));
List<Parameter<?>> collectionResourceParams = new ArrayList<>();
collectionResourceParams.add(new Parameter<>("myComplexKeyCollectionId", Integer.class, new IntegerDataSchema(), false, null, Parameter.ParamType.RESOURCE_KEY, false, new AnnotationSet(new Annotation[] {})));
collectionResourceParams.add(myComplexKeyParam);
List<Parameter<?>> simpleResourceParams = new ArrayList<>();
simpleResourceParams.add(myComplexKeyParam);
List<Parameter<?>> associationResourceParams = new ArrayList<>();
associationResourceParams.add(new Parameter<>("myComplexKeyAssociationId", CompoundKey.class, null, false, null, Parameter.ParamType.RESOURCE_KEY, false, new AnnotationSet(new Annotation[] {})));
associationResourceParams.add(myComplexKeyParam);
List<Parameter<?>> complexResourceKeyParams = new ArrayList<>();
@SuppressWarnings("rawtypes") Parameter<ComplexResourceKey> complexResourceKeyParam = new Parameter<>("complexKeyTestId", ComplexResourceKey.class, null, false, null, Parameter.ParamType.RESOURCE_KEY, false, new AnnotationSet(new Annotation[] {}));
complexResourceKeyParams.add(complexResourceKeyParam);
complexResourceKeyParams.add(myComplexKeyParam);
return new Object[][] { { collectionResourceParams, new Key("myComplexKeyCollectionId", Integer.class, new IntegerDataSchema()), "myComplexKeyCollectionId", 4545 }, { simpleResourceParams, null, null, null }, { associationResourceParams, new Key("myComplexKeyAssociationId", CompoundKey.class, null), "myComplexKeyAssociationId", new CompoundKey().append("string1", "apples").append("string2", "oranges") }, { complexResourceKeyParams, new Key("complexKeyTestId", ComplexResourceKey.class, null), "complexKeyTestId", new ComplexResourceKey<>(new MyComplexKey().setA("keyString").setB(1234L), new EmptyRecord()) } };
}
use of com.linkedin.restli.internal.server.model.Parameter in project rest.li by linkedin.
the class TestArgumentBuilder method testQueryParameterValidation.
@Test(dataProvider = "validateQueryParameter")
public void testQueryParameterValidation(String paramKey, Class<?> dataType, Object paramValue, boolean isValid, String errorMessage) {
Parameter<?> param = new Parameter<>(paramKey, dataType, DataTemplateUtil.getSchema(dataType), false, null, Parameter.ParamType.QUERY, false, AnnotationSet.EMPTY);
ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
EasyMock.expect(mockResourceContext.getRequestAttachmentReader()).andReturn(null).anyTimes();
EasyMock.expect(mockResourceContext.getStructuredParameter(paramKey)).andReturn(paramValue).anyTimes();
EasyMock.replay(mockResourceContext);
List<Parameter<?>> parameters = Collections.singletonList(param);
try {
ArgumentBuilder.buildArgs(new Object[0], getMockResourceMethod(parameters), mockResourceContext, null, getMockResourceMethodConfig(true));
assert (isValid);
} catch (Exception e) {
assert (!isValid);
assert (e.getMessage().equals(errorMessage));
}
}
use of com.linkedin.restli.internal.server.model.Parameter in project rest.li by linkedin.
the class TestArgumentBuilder method testPathKeyParameterType.
@Test
@SuppressWarnings("deprecation")
public void testPathKeyParameterType() {
String testParamKey = "testParam";
String expectedTestParamValue = "testParamValue";
// mock setup
ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
MutablePathKeys mockPathKeys = EasyMock.createMock(MutablePathKeys.class);
EasyMock.expect(mockPathKeys.get(testParamKey)).andReturn(expectedTestParamValue).anyTimes();
EasyMock.expect(mockResourceContext.getPathKeys()).andReturn(mockPathKeys).anyTimes();
EasyMock.expect(mockResourceContext.getRequestAttachmentReader()).andReturn(null);
EasyMock.replay(mockResourceContext, mockPathKeys);
List<Parameter<?>> parameters = new ArrayList<>();
Parameter<String> param1 = new Parameter<>(testParamKey, String.class, null, false, null, Parameter.ParamType.KEY, false, AnnotationSet.EMPTY);
Parameter<String> param2 = new Parameter<>(testParamKey, String.class, null, false, null, Parameter.ParamType.ASSOC_KEY_PARAM, false, AnnotationSet.EMPTY);
Parameter<PathKeys> param3 = new Parameter<>(testParamKey, PathKeys.class, null, false, null, Parameter.ParamType.PATH_KEYS, false, AnnotationSet.EMPTY);
Parameter<PathKeys> param4 = new Parameter<>(testParamKey, PathKeys.class, null, false, null, Parameter.ParamType.PATH_KEYS_PARAM, false, AnnotationSet.EMPTY);
parameters.add(param1);
parameters.add(param2);
parameters.add(param3);
parameters.add(param4);
Object[] results = ArgumentBuilder.buildArgs(new Object[0], getMockResourceMethod(parameters), mockResourceContext, null, getMockResourceMethodConfig(false));
Assert.assertEquals(results[0], expectedTestParamValue);
Assert.assertEquals(results[1], expectedTestParamValue);
Assert.assertEquals(results[2], mockPathKeys);
Assert.assertEquals(results[3], mockPathKeys);
}
Aggregations