Search in sources :

Example 11 with Key

use of com.linkedin.restli.server.Key in project rest.li by linkedin.

the class TestBatchUpdateArgumentBuilder method argumentData.

@DataProvider(name = "argumentData")
private Object[][] argumentData() {
    Object[] simpleKeys = new Object[] { "simple", "(s:pe%cial)" };
    Object[] compoundKeys = new Object[] { new CompoundKey().append("string1", "apples").append("string2", "oranges"), new CompoundKey().append("string1", "simple").append("string2", "(s:pe%cial)") };
    Object[] complexResourceKeys = new Object[] { new ComplexResourceKey<MyComplexKey, EmptyRecord>(new MyComplexKey().setA("simple").setB(111L), new EmptyRecord()), new ComplexResourceKey<MyComplexKey, EmptyRecord>(new MyComplexKey().setA("(s:pe%cial)").setB(222L), new EmptyRecord()) };
    return new Object[][] { { AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), new Key("stringKey", String.class, new StringDataSchema()), null, "{\"entities\":{\"simple\":{\"b\":123,\"a\":\"abc\"}," + "\"(s:pe%cial)\":{\"b\":456,\"a\":\"XY\"}}}", simpleKeys }, { AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), new Key("stringKey", String.class, new StringDataSchema()), null, "{\"entities\":{\"simple\":{\"b\":123,\"a\":\"abc\"}," + "\"(s:pe%cial)\":{\"b\":456,\"a\":\"XY\"}}}", simpleKeys }, { 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=simple&string2=(s:pe%25cial)\":{\"b\":456,\"a\":\"XY\"}}}", compoundKeys }, { 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:simple,string2:%28s%3Ape%25cial%29)\":{\"b\":456,\"a\":\"XY\"}," + "\"(string1:apples,string2:oranges)\":{\"b\":123,\"a\":\"abc\"}}}", compoundKeys }, { AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion(), new Key("complexKey", ComplexResourceKey.class, null), null, "{\"entities\":{\"a=simple&b=111\":{\"b\":123,\"a\":\"abc\"}," + "\"a=(s:pe%25cial)&b=222\":{\"b\":456,\"a\":\"XY\"}}}", complexResourceKeys }, { AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion(), new Key("complexKey", ComplexResourceKey.class, null), null, "{\"entities\":{\"($params:(),a:%28s%3Ape%25cial%29,b:222)\":{\"b\":456,\"a\":\"XY\"}," + "\"($params:(),a:simple,b:111)\":{\"b\":123,\"a\":\"abc\"}}}", complexResourceKeys } };
}
Also used : StringDataSchema(com.linkedin.data.schema.StringDataSchema) EmptyRecord(com.linkedin.restli.common.EmptyRecord) MyComplexKey(com.linkedin.restli.common.test.MyComplexKey) 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 Key

use of com.linkedin.restli.server.Key in project rest.li by linkedin.

the class TestGetArgumentBuilder method testAsyncArguments.

@Test(dataProvider = "asyncArgumentData")
public void testAsyncArguments(List<Parameter<?>> paramList) {
    String keyName = "myComplexKeyCollectionId";
    Object keyValue = new Integer(123);
    DataSchema keySchema = new IntegerDataSchema();
    Key key = new Key(keyName, keyValue.getClass(), keySchema);
    ResourceModel model = RestLiArgumentBuilderTestHelper.getMockResourceModel(null, key, false);
    ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 2, paramList);
    ResourceContext context = RestLiArgumentBuilderTestHelper.getMockResourceContext(keyName, keyValue, null, true);
    RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 3, context, 2);
    RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, null, 0);
    RestLiArgumentBuilder argumentBuilder = new GetArgumentBuilder();
    RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, request);
    Object[] args = argumentBuilder.buildArguments(requestData, routingResult);
    Object[] expectedArgs = new Object[] { keyValue, null };
    assertEquals(args, expectedArgs);
    verify(model, descriptor, context, routingResult, request);
}
Also used : IntegerDataSchema(com.linkedin.data.schema.IntegerDataSchema) ResourceContext(com.linkedin.restli.server.ResourceContext) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) DataSchema(com.linkedin.data.schema.DataSchema) IntegerDataSchema(com.linkedin.data.schema.IntegerDataSchema) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) 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) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData) Test(org.testng.annotations.Test)

Example 13 with Key

use of com.linkedin.restli.server.Key in project rest.li by linkedin.

the class TestGetArgumentBuilder method testFailure.

@Test(dataProvider = "failureData")
public void testFailure(Parameter<?> param, String errorMessage) {
    String keyName = "myComplexKeyCollectionId";
    Key key = new Key(keyName, Integer.class, new IntegerDataSchema());
    ResourceModel model = RestLiArgumentBuilderTestHelper.getMockResourceModel(null, key, true);
    List<Parameter<?>> paramList = Collections.<Parameter<?>>singletonList(param);
    ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 2, paramList);
    ResourceContext context = EasyMock.createMock(ResourceContext.class);
    PathKeys mockPathKeys = EasyMock.createMock(PathKeys.class);
    EasyMock.expect(mockPathKeys.get(keyName)).andReturn(null).anyTimes();
    EasyMock.expect(context.getPathKeys()).andReturn(mockPathKeys).anyTimes();
    EasyMock.replay(context, mockPathKeys);
    RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 3, context, 2);
    RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, null, 0);
    RestLiArgumentBuilder argumentBuilder = new GetArgumentBuilder();
    RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, request);
    try {
        argumentBuilder.buildArguments(requestData, routingResult);
        fail("Expected RoutingException");
    } catch (RoutingException e) {
        assertEquals(e.getMessage(), errorMessage);
    }
    verify(descriptor, context, routingResult, request);
}
Also used : RoutingException(com.linkedin.restli.server.RoutingException) IntegerDataSchema(com.linkedin.data.schema.IntegerDataSchema) ResourceContext(com.linkedin.restli.server.ResourceContext) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) PathKeys(com.linkedin.restli.server.PathKeys) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) 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) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData) Test(org.testng.annotations.Test)

Example 14 with Key

use of com.linkedin.restli.server.Key in project rest.li by linkedin.

the class TestRestLiMethodInvocation method testHeuristicKeySyntaxDetection.

@Test
public void testHeuristicKeySyntaxDetection() throws PathSegment.PathSegmentSyntaxException {
    Set<Key> keys = new HashSet<Key>(2);
    keys.add(new Key("foo", Integer.class));
    keys.add(new Key("bar", String.class));
    // heuristic key syntax detection only occurs in Protocol Version 1.0.0
    ProtocolVersion v1 = AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion();
    Set<String> expectedKeys = new HashSet<String>(Arrays.asList("foo", "bar"));
    Assert.assertEquals(expectedKeys, ArgumentUtils.parseCompoundKey("foo:42;bar:abcd", keys, v1).getPartKeys());
    Assert.assertEquals(expectedKeys, ArgumentUtils.parseCompoundKey("foo:42;bar:abcd=1&efg=2", keys, v1).getPartKeys());
    Assert.assertEquals(expectedKeys, ArgumentUtils.parseCompoundKey("foo=42&bar=abcd", keys, v1).getPartKeys());
    Assert.assertEquals(expectedKeys, ArgumentUtils.parseCompoundKey("foo=42&bar=abcd:1;2", keys, v1).getPartKeys());
    Assert.assertEquals(expectedKeys, ArgumentUtils.parseCompoundKey("foo=42&bar=foo:42", keys, v1).getPartKeys());
    Assert.assertEquals(expectedKeys, ArgumentUtils.parseCompoundKey("foo=42&bar=foo:42;bar:abcd", keys, v1).getPartKeys());
    Assert.assertEquals(expectedKeys, ArgumentUtils.parseCompoundKey("foo:42;bar:foo=42&bar=abcd", keys, v1).getPartKeys());
    Assert.assertEquals(expectedKeys, ArgumentUtils.parseCompoundKey("foo:42;bar:foo=42", keys, v1).getPartKeys());
}
Also used : ByteString(com.linkedin.data.ByteString) CustomString(com.linkedin.restli.server.custom.types.CustomString) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) CompoundKey(com.linkedin.restli.common.CompoundKey) DiscoveredItemKey(com.linkedin.restli.server.twitter.TwitterTestDataModels.DiscoveredItemKey) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) Key(com.linkedin.restli.server.Key) HashSet(java.util.HashSet) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 15 with Key

use of com.linkedin.restli.server.Key in project rest.li by linkedin.

the class ArgumentUtils method parseSimplePathKey.

/**
   * The method parses out and returns the correct simple type of the key out of the Object.
   * It does not handle {@link CompoundKey}s or {@link ComplexResourceKey}s.
   *
   * @param value key value string representation to parse
   * @param resource {@link com.linkedin.restli.internal.server.model.ResourceModel} containing the key type
   * @param version the {@link com.linkedin.restli.common.ProtocolVersion}
   * @return parsed key value in the correct type for the key
   * @throws IllegalArgumentException
   * @throws NumberFormatException
   */
public static Object parseSimplePathKey(final String value, final ResourceModel resource, final ProtocolVersion version) throws IllegalArgumentException {
    Key key = resource.getPrimaryKey();
    String decodedValue;
    if (version.compareTo(AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion()) >= 0) {
        decodedValue = UriComponent.decode(value, UriComponent.Type.PATH_SEGMENT);
    } else {
        decodedValue = URLEscaper.unescape(value, URLEscaper.Escaping.URL_ESCAPING);
    }
    return convertSimpleValue(decodedValue, key.getDataSchema(), key.getType());
}
Also used : ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) AlternativeKey(com.linkedin.restli.server.AlternativeKey) Key(com.linkedin.restli.server.Key) CompoundKey(com.linkedin.restli.common.CompoundKey)

Aggregations

ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)22 Key (com.linkedin.restli.server.Key)22 CompoundKey (com.linkedin.restli.common.CompoundKey)18 MyComplexKey (com.linkedin.restli.common.test.MyComplexKey)10 IntegerDataSchema (com.linkedin.data.schema.IntegerDataSchema)7 DataProvider (org.testng.annotations.DataProvider)7 EmptyRecord (com.linkedin.restli.common.EmptyRecord)6 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)6 Parameter (com.linkedin.restli.internal.server.model.Parameter)5 AlternativeKey (com.linkedin.restli.server.AlternativeKey)5 ResourceContext (com.linkedin.restli.server.ResourceContext)5 HashSet (java.util.HashSet)5 Test (org.testng.annotations.Test)5 RestRequest (com.linkedin.r2.message.rest.RestRequest)4 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)4 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)4 RestLiRequestData (com.linkedin.restli.server.RestLiRequestData)4 ArrayList (java.util.ArrayList)4 DataMap (com.linkedin.data.DataMap)3 AnnotationSet (com.linkedin.restli.internal.server.model.AnnotationSet)3