Search in sources :

Example 86 with ComplexResourceKey

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

the class TestComplexArrayResource method testBatchGet.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "versionWithRequestOptionsDataProvider")
public void testBatchGet(ProtocolVersion version, RestliRequestOptions options) throws RemoteInvocationException {
    List<ComplexResourceKey<ComplexArray, ComplexArray>> complexKeys = getBatchCompleKeys();
    ComplexArrayBuilders builders = new ComplexArrayBuilders(options);
    Request<BatchKVResponse<ComplexResourceKey<ComplexArray, ComplexArray>, Greeting>> request = builders.batchGet().ids(complexKeys).buildKV();
    Response<BatchKVResponse<ComplexResourceKey<ComplexArray, ComplexArray>, Greeting>> response = getClient().sendRequest(request).getResponse();
    Greeting greeting1 = response.getEntity().getResults().get(complexKeys.get(0));
    Assert.assertNotNull(greeting1);
    Greeting greeting2 = response.getEntity().getResults().get(complexKeys.get(1));
    Assert.assertNotNull(greeting2);
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) ComplexArrayBuilders(com.linkedin.restli.examples.greetings.client.ComplexArrayBuilders) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) ComplexArray(com.linkedin.restli.examples.greetings.api.ComplexArray) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) Test(org.testng.annotations.Test)

Example 87 with ComplexResourceKey

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

the class TestComplexArrayResource method getBatchCompleKeys.

private static List<ComplexResourceKey<ComplexArray, ComplexArray>> getBatchCompleKeys() {
    LongArray singleton1 = new LongArray(1L);
    ComplexArray next1 = new ComplexArray().setArray(singleton1);
    ComplexArray key1 = new ComplexArray().setArray(singleton1).setNext(next1);
    ComplexArray params1 = new ComplexArray().setArray(singleton1).setNext(next1);
    ComplexResourceKey<ComplexArray, ComplexArray> complexKey1 = new ComplexResourceKey<>(key1, params1);
    LongArray singleton2 = new LongArray(2L);
    ComplexArray next2 = new ComplexArray().setArray(singleton2);
    ComplexArray key2 = new ComplexArray().setArray(singleton2).setNext(next2);
    ComplexArray params2 = new ComplexArray().setArray(singleton2).setNext(next2);
    ComplexResourceKey<ComplexArray, ComplexArray> complexKey2 = new ComplexResourceKey<>(key2, params2);
    List<ComplexResourceKey<ComplexArray, ComplexArray>> complexKeys = new ArrayList<>();
    complexKeys.add(complexKey1);
    complexKeys.add(complexKey2);
    return complexKeys;
}
Also used : LongArray(com.linkedin.data.template.LongArray) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) ArrayList(java.util.ArrayList) ComplexArray(com.linkedin.restli.examples.greetings.api.ComplexArray)

Example 88 with ComplexResourceKey

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

the class TestAltKeyResource method testComplexKeyAltKeyCreate.

@Test
public void testComplexKeyAltKeyCreate() throws Throwable {
    // In ComplexKeyAltKeyResource create method, we predefine a ComplexKey,
    // here we coerce that key to Alternative Key,
    // which will be used as expected key which will be returned by response.
    StringComplexKeyCoercer coercer = new StringComplexKeyCoercer();
    TwoPartKey key = new TwoPartKey();
    key.setMajor("testKey");
    key.setMinor("testKey");
    ComplexResourceKey<TwoPartKey, TwoPartKey> complexKey = new ComplexResourceKey<>(key, new TwoPartKey());
    String altKey = coercer.coerceFromKey(complexKey);
    Message message = new Message().setMessage("message");
    byte[] byteArray = dataTemplateToBytes(message);
    URI altKeyUri = URI.create(URI_PREFIX + RestLiIntTestServer.DEFAULT_PORT + "/complexKeyAltKey?altkey=alt");
    RestRequest altKeyRequest = new RestRequestBuilder(altKeyUri).setHeader(HEADER_RESTLI_PROTOCOL_VERSION, PROTOCOL_VERSION_2).setEntity(byteArray).setMethod(RestMethod.POST).build();
    RestResponse response = client.restRequest(altKeyRequest).get();
    Assert.assertNotNull(response);
    Assert.assertEquals(response.getStatus(), 201);
    Assert.assertEquals(response.getHeader(HEADER_RESTLI_ID), altKey);
}
Also used : TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) RestRequest(com.linkedin.r2.message.rest.RestRequest) Message(com.linkedin.restli.examples.greetings.api.Message) RestResponse(com.linkedin.r2.message.rest.RestResponse) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) StringComplexKeyCoercer(com.linkedin.restli.examples.greetings.server.altkey.StringComplexKeyCoercer) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 89 with ComplexResourceKey

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

the class ExampleRequestResponseGenerator method generateKey.

private Object generateKey(ResourceSpec resourceSpec, ResourceSchema resourceSchema, Integer batchIdx) {
    if (resourceSpec.getKeyType() == null) {
        throw new IllegalArgumentException("Cannot generate key for keyless resource.");
    }
    switch(toResourceKeyType(resourceSpec.getKeyType().getType())) {
        case COMPLEX:
            RecordDataSchema keySchema = (RecordDataSchema) resourceSpec.getComplexKeyType().getKeyType().getSchema();
            DataMap keyData = (DataMap) _dataGenerator.buildData(postfixBatchIdx(keySchema.getName() + "Key", batchIdx), keySchema);
            RecordDataSchema paramsSchema = (RecordDataSchema) resourceSpec.getComplexKeyType().getParamsType().getSchema();
            DataMap paramsData = (DataMap) _dataGenerator.buildData(postfixBatchIdx(keySchema.getName() + "Params", batchIdx), paramsSchema);
            return new ComplexResourceKey<>(new RecordTemplatePlaceholder(keyData, keySchema), new RecordTemplatePlaceholder(paramsData, paramsSchema));
        case COMPOUND:
            CompoundKey compoundKey = new CompoundKey();
            for (Map.Entry<String, CompoundKey.TypeInfo> keyPart : resourceSpec.getKeyParts().entrySet()) {
                String key = keyPart.getKey();
                CompoundKey.TypeInfo typeInfo = keyPart.getValue();
                compoundKey.append(key, _dataGenerator.buildData(postfixBatchIdx(key, batchIdx), typeInfo.getBinding().getSchema()), typeInfo);
            }
            return compoundKey;
        case PRIMITIVE:
            String keyName = resourceSchema.getCollection().getIdentifier().getName();
            return _dataGenerator.buildData(postfixBatchIdx(keyName, batchIdx), resourceSpec.getKeyType().getSchema());
        case NONE:
            return null;
        default:
            throw new IllegalStateException("Unknown enum value: " + resourceSpec.getKeyType().getType());
    }
}
Also used : CompoundKey(com.linkedin.restli.common.CompoundKey) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) Map(java.util.Map) DataMap(com.linkedin.data.DataMap) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap)

Aggregations

ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)89 Test (org.testng.annotations.Test)43 TwoPartKey (com.linkedin.restli.examples.greetings.api.TwoPartKey)19 TestRecord (com.linkedin.restli.client.test.TestRecord)18 CompoundKey (com.linkedin.restli.common.CompoundKey)18 HashMap (java.util.HashMap)17 EmptyRecord (com.linkedin.restli.common.EmptyRecord)16 DataMap (com.linkedin.data.DataMap)15 Message (com.linkedin.restli.examples.greetings.api.Message)14 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)13 Key (com.linkedin.restli.server.Key)13 RecordTemplate (com.linkedin.data.template.RecordTemplate)11 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)11 EntityResponse (com.linkedin.restli.common.EntityResponse)11 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)11 RestLiTestHelper.buildResourceModel (com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel)11 DiscoveredItemKey (com.linkedin.restli.server.twitter.TwitterTestDataModels.DiscoveredItemKey)11 DiscoveredItemKeyParams (com.linkedin.restli.server.twitter.TwitterTestDataModels.DiscoveredItemKeyParams)11 HashSet (java.util.HashSet)11 AfterTest (org.testng.annotations.AfterTest)11