Search in sources :

Example 61 with ComplexResourceKey

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

the class TestRestLiMethodInvocation method testPromiseBatchGet.

@Test
public void testPromiseBatchGet() throws Exception {
    ResourceModel statusResourceModel = buildResourceModel(PromiseStatusCollectionResource.class);
    ResourceModel followsAssociationResourceModel = buildResourceModel(PromiseFollowsAssociativeResource.class);
    ResourceModel discoveredItemsResourceModel = buildResourceModel(PromiseDiscoveredItemsResource.class);
    ResourceMethodDescriptor methodDescriptor;
    PromiseStatusCollectionResource statusResource;
    PromiseFollowsAssociativeResource followsResource;
    PromiseDiscoveredItemsResource discoveredItemsResource;
    // #1 Batch get on collection resource
    methodDescriptor = statusResourceModel.findMethod(ResourceMethod.BATCH_GET);
    statusResource = getMockResource(PromiseStatusCollectionResource.class);
    EasyMock.expect(statusResource.batchGet((Set<Long>) Matchers.eqCollectionUnordered(Sets.newHashSet(1L, 2L, 3L)))).andReturn(Promises.value(null)).once();
    checkInvocation(statusResource, methodDescriptor, "GET", version, "/promisestatuses?ids=List(1,2,3)", buildBatchPathKeys(1L, 2L, 3L));
    // #2 Batch get on association resource
    methodDescriptor = followsAssociationResourceModel.findMethod(ResourceMethod.BATCH_GET);
    followsResource = getMockResource(PromiseFollowsAssociativeResource.class);
    Set<CompoundKey> expectedKeys = new HashSet<>();
    CompoundKey key1 = new CompoundKey();
    key1.append("followeeID", 1L);
    key1.append("followerID", 1L);
    expectedKeys.add(key1);
    CompoundKey key2 = new CompoundKey();
    key2.append("followeeID", 2L);
    key2.append("followerID", 2L);
    expectedKeys.add(key2);
    EasyMock.expect(followsResource.batchGet((Set<CompoundKey>) Matchers.eqCollectionUnordered(expectedKeys))).andReturn(Promises.value(null)).once();
    checkInvocation(followsResource, methodDescriptor, "GET", version, "/promisefollows?ids=List((followeeID:1,followerID:1),(followeeID:2,followerID:2))", buildBatchPathKeys(key1, key2));
    // #3 Batch get on complex key resource
    methodDescriptor = discoveredItemsResourceModel.findMethod(ResourceMethod.BATCH_GET);
    discoveredItemsResource = getMockResource(PromiseDiscoveredItemsResource.class);
    ComplexResourceKey<DiscoveredItemKey, DiscoveredItemKeyParams> keyA = getDiscoveredItemComplexKey(1L, 2, 3L);
    ComplexResourceKey<DiscoveredItemKey, DiscoveredItemKeyParams> keyB = getDiscoveredItemComplexKey(4L, 5, 6L);
    @SuppressWarnings("unchecked") Set<ComplexResourceKey<DiscoveredItemKey, DiscoveredItemKeyParams>> set = (Set<ComplexResourceKey<DiscoveredItemKey, DiscoveredItemKeyParams>>) Matchers.eqCollectionUnordered(Sets.newHashSet(keyA, keyB));
    EasyMock.expect(discoveredItemsResource.batchGet(set)).andReturn(Promises.value(null)).once();
    checkInvocation(discoveredItemsResource, methodDescriptor, "GET", version, "/promisediscovereditems?ids=List((itemId:1,type:2,userId:3),(itemId:4,type:5,userId:6))", buildBatchPathKeys(keyA, keyB));
}
Also used : PromiseStatusCollectionResource(com.linkedin.restli.server.twitter.PromiseStatusCollectionResource) PromiseFollowsAssociativeResource(com.linkedin.restli.server.twitter.PromiseFollowsAssociativeResource) Set(java.util.Set) HashSet(java.util.HashSet) CompoundKey(com.linkedin.restli.common.CompoundKey) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) PromiseDiscoveredItemsResource(com.linkedin.restli.server.twitter.PromiseDiscoveredItemsResource) DiscoveredItemKeyParams(com.linkedin.restli.server.twitter.TwitterTestDataModels.DiscoveredItemKeyParams) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiTestHelper.buildResourceModel(com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel) DiscoveredItemKey(com.linkedin.restli.server.twitter.TwitterTestDataModels.DiscoveredItemKey) HashSet(java.util.HashSet) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 62 with ComplexResourceKey

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

the class TestRestLiMethodInvocation method testAsyncBatchDeleteComplexResource.

@Test
public void testAsyncBatchDeleteComplexResource() throws Exception {
    ResourceModel discoveredResourceModel = buildResourceModel(AsyncDiscoveredItemsResource.class);
    RestLiCallback callback = getCallback();
    ResourceMethodDescriptor methodDescriptor;
    AsyncDiscoveredItemsResource discoveredResource;
    methodDescriptor = discoveredResourceModel.findMethod(ResourceMethod.BATCH_DELETE);
    discoveredResource = getMockResource(AsyncDiscoveredItemsResource.class);
    @SuppressWarnings("unchecked") BatchDeleteRequest<ComplexResourceKey<DiscoveredItemKey, DiscoveredItemKeyParams>, DiscoveredItem> mockBatchDeleteReq = EasyMock.anyObject();
    discoveredResource.batchDelete(mockBatchDeleteReq, EasyMock.anyObject());
    EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {

        @Override
        public Object answer() throws Throwable {
            @SuppressWarnings("unchecked") Callback<BatchUpdateResult<ComplexResourceKey<DiscoveredItemKey, DiscoveredItemKeyParams>, DiscoveredItem>> callback = (Callback<BatchUpdateResult<ComplexResourceKey<DiscoveredItemKey, DiscoveredItemKeyParams>, DiscoveredItem>>) EasyMock.getCurrentArguments()[1];
            callback.onSuccess(null);
            return null;
        }
    });
    EasyMock.replay(discoveredResource);
    String uri = "/asyncdiscovereditems?ids=List((itemId:1,type:1,userId:1),(itemId:2,type:2,userId:2),(itemId:3,type:3,userId:3))";
    checkAsyncInvocation(discoveredResource, callback, methodDescriptor, "DELETE", version, uri, buildBatchPathKeys(getDiscoveredItemComplexKey(1, 1, 1), getDiscoveredItemComplexKey(2, 2, 2), getDiscoveredItemComplexKey(3, 3, 3)));
}
Also used : AsyncDiscoveredItemsResource(com.linkedin.restli.server.twitter.AsyncDiscoveredItemsResource) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ByteString(com.linkedin.data.ByteString) CustomString(com.linkedin.restli.server.custom.types.CustomString) Callback(com.linkedin.common.callback.Callback) RestLiCallback(com.linkedin.restli.internal.server.RestLiCallback) FilterChainCallback(com.linkedin.restli.internal.server.filter.FilterChainCallback) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) DiscoveredItemKeyParams(com.linkedin.restli.server.twitter.TwitterTestDataModels.DiscoveredItemKeyParams) RestLiCallback(com.linkedin.restli.internal.server.RestLiCallback) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) DiscoveredItem(com.linkedin.restli.server.twitter.TwitterTestDataModels.DiscoveredItem) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiTestHelper.buildResourceModel(com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel) DiscoveredItemKey(com.linkedin.restli.server.twitter.TwitterTestDataModels.DiscoveredItemKey) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 63 with ComplexResourceKey

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

the class QueryParamsDataMap method fixUpComplexKeySingletonArray.

/**
 * Because of backwards compatibility concerns, array fields of the key component of a
 * {@link ComplexResourceKey}s in a get request will be represented in the request url in the old
 * style.  That is, if an array field has the name "a", and contains [1,2] the part of the url
 * representing the serialized array will look like  "a=1&a=2".  However, if the array is a
 * singleton it will just be represented by "a=1". Therefore it is not possible to distinguish
 * between a single value itself and an array containing a single value.
 *
 * The purpose of this function is to fix up the singleton array problem by checking to see whether the given
 * ComplexKey's key part has an array component, and, if so and the data for that field is NOT a dataList,
 * placing the data into a dataList.
 *
 * @param complexResourceKey The complex key to be fixed.
 */
public static ComplexResourceKey<?, ?> fixUpComplexKeySingletonArray(ComplexResourceKey<?, ?> complexResourceKey) {
    RecordTemplate key = complexResourceKey.getKey();
    DataMap dataMap = key.data();
    List<RecordDataSchema.Field> fields = key.schema() == null ? Collections.<RecordDataSchema.Field>emptyList() : key.schema().getFields();
    for (RecordDataSchema.Field f : fields) {
        DataSchema.Type type = f.getType().getType();
        String fieldName = f.getName();
        if (type == DataSchema.Type.ARRAY && dataMap.containsKey(fieldName)) {
            Object arrayFieldValue = dataMap.get(fieldName);
            if (!(arrayFieldValue instanceof DataList)) {
                DataList list = new DataList();
                list.add(arrayFieldValue);
                ValidateDataAgainstSchema.validate(list, f.getType(), new ValidationOptions(RequiredMode.CAN_BE_ABSENT_IF_HAS_DEFAULT, CoercionMode.STRING_TO_PRIMITIVE));
                dataMap.put(fieldName, list);
            }
        }
    }
    RecordTemplate wrappedKey = DataTemplateUtil.wrap(dataMap, key.getClass());
    @SuppressWarnings("unchecked") ComplexResourceKey<?, ?> newKey = new ComplexResourceKey<RecordTemplate, RecordTemplate>(wrappedKey, complexResourceKey.getParams());
    return newKey;
}
Also used : ByteString(com.linkedin.data.ByteString) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) DataMap(com.linkedin.data.DataMap) DataSchema(com.linkedin.data.schema.DataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataList(com.linkedin.data.DataList) RecordTemplate(com.linkedin.data.template.RecordTemplate) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey)

Example 64 with ComplexResourceKey

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

the class TestClientBuilders method testActionRequestInputIsReadOnly.

@Test
@SuppressWarnings("unchecked")
public void testActionRequestInputIsReadOnly() {
    FieldDef<TestRecord> pParam = new FieldDef<>("p", TestRecord.class, DataTemplateUtil.getSchema(TestRecord.class));
    Map<String, DynamicRecordMetadata> requestMetadataMap = new HashMap<>();
    DynamicRecordMetadata requestMetadata = new DynamicRecordMetadata("action", Collections.<FieldDef<?>>singleton(pParam));
    requestMetadataMap.put("action", requestMetadata);
    DynamicRecordMetadata responseMetadata = new DynamicRecordMetadata("action", Collections.<FieldDef<?>>emptyList());
    Map<String, DynamicRecordMetadata> responseMetadataMap = new HashMap<>();
    responseMetadataMap.put("action", responseMetadata);
    ResourceSpec resourceSpec = new ResourceSpecImpl(Collections.<ResourceMethod>emptySet(), requestMetadataMap, responseMetadataMap, ComplexResourceKey.class, TestRecord.class, TestRecord.class, TestRecord.class, Collections.<String, CompoundKey.TypeInfo>emptyMap());
    ActionRequestBuilder<ComplexResourceKey<TestRecord, TestRecord>, TestRecord> builder = new ActionRequestBuilder<>(TEST_URI, TestRecord.class, resourceSpec, RestliRequestOptions.DEFAULT_OPTIONS);
    TestRecord testRecord1 = new TestRecord();
    TestRecord testRecord2 = new TestRecord();
    ComplexResourceKey<TestRecord, TestRecord> key = new ComplexResourceKey<>(testRecord1, testRecord2);
    ActionRequest<TestRecord> request = builder.name("action").setParam(pParam, testRecord1).id(key).build();
    DynamicRecordTemplate inputParams = (DynamicRecordTemplate) request.getInputRecord();
    Assert.assertNotSame(inputParams.getValue(pParam).data(), testRecord1.data());
    Assert.assertTrue(inputParams.data().isReadOnly());
    Assert.assertTrue(inputParams.getValue(pParam).data().isMadeReadOnly());
    Assert.assertNotSame(request.getId(), key);
    Assert.assertTrue(((ComplexResourceKey<TestRecord, TestRecord>) request.getId()).isReadOnly());
    testRecord1.data().makeReadOnly();
    testRecord2.data().makeReadOnly();
    request = builder.build();
    inputParams = (DynamicRecordTemplate) request.getInputRecord();
    Assert.assertSame(inputParams.getValue(pParam).data(), testRecord1.data());
    Assert.assertTrue(inputParams.data().isReadOnly());
    Assert.assertSame(request.getId(), key);
}
Also used : HashMap(java.util.HashMap) CompoundKey(com.linkedin.restli.common.CompoundKey) ResourceSpec(com.linkedin.restli.common.ResourceSpec) ByteString(com.linkedin.data.ByteString) FieldDef(com.linkedin.data.template.FieldDef) DynamicRecordMetadata(com.linkedin.data.template.DynamicRecordMetadata) DynamicRecordTemplate(com.linkedin.data.template.DynamicRecordTemplate) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) TestRecord(com.linkedin.restli.client.test.TestRecord) ResourceSpecImpl(com.linkedin.restli.common.ResourceSpecImpl) Test(org.testng.annotations.Test)

Example 65 with ComplexResourceKey

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

the class TestClientBuilders method testBatchUpdateRequestInputIsReadOnly.

@Test
@SuppressWarnings("unchecked")
public void testBatchUpdateRequestInputIsReadOnly() {
    BatchUpdateRequestBuilder<ComplexResourceKey<TestRecord, TestRecord>, TestRecord> builder = new BatchUpdateRequestBuilder<>(TEST_URI, TestRecord.class, _COMPLEX_KEY_SPEC, RestliRequestOptions.DEFAULT_OPTIONS);
    ComplexResourceKey<TestRecord, TestRecord> id1 = buildComplexKey(1L, "KeyMessage1", 10L, "ParamMessage1");
    ComplexResourceKey<TestRecord, TestRecord> id2 = buildComplexKey(2L, "KeyMessage2", 20L, "ParamMessage2");
    TestRecord t1 = new TestRecord().setMessage("foo");
    TestRecord t2 = new TestRecord().setMessage("bar");
    BatchUpdateRequest<ComplexResourceKey<TestRecord, TestRecord>, TestRecord> request = builder.input(id1, t1).input(id2, t2).build();
    checkKeyValueRecordCollectionIsReadOnly(id1, id2, t1, t2, TestRecord.class, (CollectionRequest<KeyValueRecord<?, TestRecord>>) request.getInputRecord());
    checkKeyValueMapIsReadOnly(id1, id2, t1, t2, TestRecord.class, request.getUpdateInputMap());
    id1.makeReadOnly();
    t2.data().makeReadOnly();
    request = builder.input(id1, t1).input(id2, t2).build();
    checkKeyValueRecordCollectionIsReadOnly(id1, id2, t1, t2, TestRecord.class, (CollectionRequest<KeyValueRecord<?, TestRecord>>) request.getInputRecord());
    checkKeyValueMapIsReadOnly(id1, id2, t1, t2, TestRecord.class, request.getUpdateInputMap());
}
Also used : KeyValueRecord(com.linkedin.restli.common.KeyValueRecord) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) TestRecord(com.linkedin.restli.client.test.TestRecord) Test(org.testng.annotations.Test)

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