Search in sources :

Example 11 with MyComplexKey

use of com.linkedin.restli.common.test.MyComplexKey in project rest.li by linkedin.

the class TestBatchUpdateArgumentBuilder method failureData.

@DataProvider(name = "failureData")
private Object[][] failureData() {
    Object[] compoundKeys = new Object[] { new CompoundKey().append("string1", "XXX").append("string2", "oranges"), new CompoundKey().append("string1", "coffee").append("string2", "tea") };
    Object[] complexResourceKeys = new Object[] { new ComplexResourceKey<>(new MyComplexKey().setA("A1").setB(111L), new EmptyRecord()), new ComplexResourceKey<>(new MyComplexKey().setA("A2").setB(222L), new EmptyRecord()) };
    return new Object[][] { { AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), new Key("integerKey", Integer.class, new IntegerDataSchema()), null, "{\"entities\":{\"10001\":{\"b\":123,\"a\":\"abc\"}," + "\"10002\":{\"b\":456,\"a\":\"XY\"}}}", new Object[] { 10001, 99999 }, ERROR_MESSAGE_BATCH_KEYS_MISMATCH }, { AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), new Key("integerKey", Integer.class, new IntegerDataSchema()), null, "{\"entities\":{\"10001\":{\"b\":123,\"a\":\"abc\"}," + "\"99999\":{\"b\":456,\"a\":\"XY\"}}}", new Object[] { 10001, 10002 }, ERROR_MESSAGE_BATCH_KEYS_MISMATCH }, { AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), new Key("integerKey", Integer.class, new IntegerDataSchema()), null, "{\"entities\":{\"10001\":{\"b\":123,\"a\":\"abc\"}," + "\"10002\":{\"b\":456,\"a\":\"XY\"}}}", new Object[] { 10001, 10002, 10003 }, ERROR_MESSAGE_BATCH_KEYS_MISMATCH }, { AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), new Key("compoundKey", CompoundKey.class, null), new Key[] { new Key("string1", String.class), new Key("string2", String.class) }, "{\"entities\":{\"string1=apples&string2=oranges\":{\"b\":123,\"a\":\"abc\"}," + "\"string1=coffee&string2=tea\":{\"b\":456,\"a\":\"XY\"}}}", compoundKeys, ERROR_MESSAGE_BATCH_KEYS_MISMATCH }, { // Duplicate key in the entities body
    AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), new Key("compoundKey", CompoundKey.class, null), new Key[] { new Key("string1", String.class), new Key("string2", String.class) }, "{\"entities\":{\"string1=coffee&string2=tea\":{\"b\":123,\"a\":\"abc\"}," + "\"string2=tea&string1=coffee\":{\"b\":456,\"a\":\"XY\"}}}", compoundKeys, ERROR_MESSAGE_DUPLICATE_BATCH_KEYS }, { AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion(), new Key("compoundKey", CompoundKey.class, null), new Key[] { new Key("string1", String.class), new Key("string2", String.class) }, "{\"entities\":{\"(string1:coffee,string2:tea)\":{\"b\":456,\"a\":\"XY\"}," + "\"(string1:apples,string2:oranges)\":{\"b\":123,\"a\":\"abc\"}}}", compoundKeys, ERROR_MESSAGE_BATCH_KEYS_MISMATCH }, { // Duplicate key in the entities body
    AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion(), new Key("compoundKey", CompoundKey.class, null), new Key[] { new Key("string1", String.class), new Key("string2", String.class) }, "{\"entities\":{\"(string1:coffee,string2:tea)\":{\"b\":456,\"a\":\"XY\"}," + "\"(string2:tea,string1:coffee)\":{\"b\":123,\"a\":\"abc\"}}}", compoundKeys, ERROR_MESSAGE_DUPLICATE_BATCH_KEYS }, { AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), new Key("complexKey", ComplexResourceKey.class, null), null, "{\"entities\":{\"a=A1&b=999\":{\"b\":123,\"a\":\"abc\"}," + "\"a=A2&b=222\":{\"b\":456,\"a\":\"XY\"}}}", complexResourceKeys, ERROR_MESSAGE_BATCH_KEYS_MISMATCH }, { // Duplicate key in the entities body
    AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), new Key("complexKey", ComplexResourceKey.class, null), null, "{\"entities\":{\"a=A1&b=111\":{\"b\":123,\"a\":\"abc\"}," + "\"b=111&a=A1\":{\"b\":456,\"a\":\"XY\"}}}", complexResourceKeys, ERROR_MESSAGE_DUPLICATE_BATCH_KEYS } };
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) MyComplexKey(com.linkedin.restli.common.test.MyComplexKey) IntegerDataSchema(com.linkedin.data.schema.IntegerDataSchema) CompoundKey(com.linkedin.restli.common.CompoundKey) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) MyComplexKey(com.linkedin.restli.common.test.MyComplexKey) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) Key(com.linkedin.restli.server.Key) CompoundKey(com.linkedin.restli.common.CompoundKey) DataProvider(org.testng.annotations.DataProvider)

Example 12 with MyComplexKey

use of com.linkedin.restli.common.test.MyComplexKey in project rest.li by linkedin.

the class TestCreateArgumentBuilder method testArgumentBuilderSuccess.

@Test
public void testArgumentBuilderSuccess() throws IOException, NoSuchMethodException {
    RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, "{\"a\":\"xyz\",\"b\":123}");
    ResourceModel model = RestLiArgumentBuilderTestHelper.getMockResourceModel(MyComplexKey.class, null, false);
    Parameter<MyComplexKey> param = new Parameter<>("", MyComplexKey.class, DataTemplateUtil.getSchema(MyComplexKey.class), false, null, Parameter.ParamType.POST, false, new AnnotationSet(new Annotation[] {}));
    ResourceMethodDescriptor descriptor = getMockResourceMethodDescriptor(model, param, CollectionResourceAsyncTemplate.class.getMethod("create", RecordTemplate.class, Callback.class));
    ServerResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(null, null, null, true);
    RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 3, context, 1);
    RestLiArgumentBuilder argumentBuilder = new CreateArgumentBuilder();
    RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, DataMapUtils.readMapWithExceptions(request));
    Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
    assertEquals(args.length, 1);
    assertTrue(args[0] instanceof MyComplexKey);
    assertEquals(((MyComplexKey) args[0]).getA(), "xyz");
    assertEquals((long) ((MyComplexKey) args[0]).getB(), 123L);
    verify(request, model, descriptor, context, routingResult);
}
Also used : MyComplexKey(com.linkedin.restli.common.test.MyComplexKey) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor) CollectionResourceAsyncTemplate(com.linkedin.restli.server.resources.CollectionResourceAsyncTemplate) AnnotationSet(com.linkedin.restli.internal.server.model.AnnotationSet) Annotation(java.lang.annotation.Annotation) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) Callback(com.linkedin.common.callback.Callback) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RecordTemplate(com.linkedin.data.template.RecordTemplate) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) Parameter(com.linkedin.restli.internal.server.model.Parameter) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData) Test(org.testng.annotations.Test)

Example 13 with MyComplexKey

use of com.linkedin.restli.common.test.MyComplexKey in project rest.li by linkedin.

the class TestPatchArgumentBuilder method testArgumentBuilderSuccess.

@Test(dataProvider = "argumentData")
public void testArgumentBuilderSuccess(List<Parameter<?>> params, Key key, String keyName, Object keyValue) throws Exception {
    RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, "{\"patch\":{\"$set\":{\"a\":\"someString\"}}}");
    ResourceModel model = RestLiArgumentBuilderTestHelper.getMockResourceModel(null, key, true);
    ResourceMethodDescriptor descriptor;
    if (key != null) {
        descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 2, params, CollectionResourceAsyncTemplate.class.getMethod("update", Object.class, PatchRequest.class, Callback.class));
    } else {
        descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 1, params, CollectionResourceAsyncTemplate.class.getMethod("update", Object.class, PatchRequest.class, Callback.class));
    }
    ServerResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(keyName, keyValue, null, true);
    RoutingResult routingResult;
    if (key != null) {
        routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 4, context, 2);
    } else {
        routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 3, context, 1);
    }
    RestLiArgumentBuilder argumentBuilder = new PatchArgumentBuilder();
    RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, DataMapUtils.readMapWithExceptions(request));
    Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
    if (keyValue != null) {
        Assert.assertEquals(args.length, 2);
        Assert.assertEquals(args[0], keyValue);
    }
    Assert.assertTrue(args[args.length - 1] instanceof PatchRequest);
    Map<String, Object> aMap = new HashMap<>();
    aMap.put("a", "someString");
    Map<String, Object> setMap = new HashMap<>();
    setMap.put("$set", new DataMap(aMap));
    Map<String, Object> data = new HashMap<>();
    data.put("patch", new DataMap(setMap));
    PatchRequest<MyComplexKey> patch = new PatchRequest<>(new DataMap(data));
    Assert.assertEquals(args[args.length - 1], patch);
    verify(request, model, descriptor, context, routingResult);
}
Also used : MyComplexKey(com.linkedin.restli.common.test.MyComplexKey) HashMap(java.util.HashMap) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) PatchRequest(com.linkedin.restli.common.PatchRequest) DataMap(com.linkedin.data.DataMap) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData) Test(org.testng.annotations.Test)

Example 14 with MyComplexKey

use of com.linkedin.restli.common.test.MyComplexKey in project rest.li by linkedin.

the class TestPatchArgumentBuilder method argumentData.

@DataProvider(name = "argumentData")
private Object[][] argumentData() {
    @SuppressWarnings("rawtypes") Parameter<?> patchParam = new Parameter<>("", PatchRequest.class, null, 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(patchParam);
    List<Parameter<?>> simpleResourceParams = new ArrayList<>();
    simpleResourceParams.add(patchParam);
    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(patchParam);
    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(patchParam);
    return new Object[][] { { collectionResourceParams, new Key("myComplexKeyCollectionId", Integer.class, new IntegerDataSchema()), "myComplexKeyCollectionId", 1234 }, { 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()) } };
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) IntegerDataSchema(com.linkedin.data.schema.IntegerDataSchema) MyComplexKey(com.linkedin.restli.common.test.MyComplexKey) CompoundKey(com.linkedin.restli.common.CompoundKey) ArrayList(java.util.ArrayList) AnnotationSet(com.linkedin.restli.internal.server.model.AnnotationSet) Annotation(java.lang.annotation.Annotation) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) Parameter(com.linkedin.restli.internal.server.model.Parameter) MyComplexKey(com.linkedin.restli.common.test.MyComplexKey) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) Key(com.linkedin.restli.server.Key) CompoundKey(com.linkedin.restli.common.CompoundKey) DataProvider(org.testng.annotations.DataProvider)

Aggregations

MyComplexKey (com.linkedin.restli.common.test.MyComplexKey)14 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)7 DataProvider (org.testng.annotations.DataProvider)7 Test (org.testng.annotations.Test)7 CompoundKey (com.linkedin.restli.common.CompoundKey)6 EmptyRecord (com.linkedin.restli.common.EmptyRecord)6 AnnotationSet (com.linkedin.restli.internal.server.model.AnnotationSet)6 Key (com.linkedin.restli.server.Key)6 RestRequest (com.linkedin.r2.message.rest.RestRequest)5 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)5 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)5 Parameter (com.linkedin.restli.internal.server.model.Parameter)5 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)5 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)5 RestLiRequestData (com.linkedin.restli.server.RestLiRequestData)5 Annotation (java.lang.annotation.Annotation)5 IntegerDataSchema (com.linkedin.data.schema.IntegerDataSchema)4 Callback (com.linkedin.common.callback.Callback)3 DataMap (com.linkedin.data.DataMap)3 CollectionResourceAsyncTemplate (com.linkedin.restli.server.resources.CollectionResourceAsyncTemplate)3