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));
}
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)));
}
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;
}
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);
}
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());
}
Aggregations