use of com.linkedin.restli.common.CollectionRequest in project rest.li by linkedin.
the class TestCollectionRequestUtil method testCompoundKeyMultipleEntities.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "versions")
public void testCompoundKeyMultipleEntities(ProtocolVersion version) {
String key1 = "key1";
String key2 = "key2";
CompoundKey c1 = new CompoundKey().append(key1, 1L).append(key2, 2L);
CompoundKey c2 = new CompoundKey().append(key1, 3L).append(key2, 4L);
CompoundKey[] keys = { c1, c2 };
Map<String, CompoundKey.TypeInfo> fieldTypes = new HashMap<String, CompoundKey.TypeInfo>();
fieldTypes.put(key1, new CompoundKey.TypeInfo(Long.class, Long.class));
fieldTypes.put(key2, new CompoundKey.TypeInfo(Long.class, Long.class));
@SuppressWarnings("rawtypes") KeyValueRecordFactory<CompoundKey, TestRecord> factory = new KeyValueRecordFactory<CompoundKey, TestRecord>(CompoundKey.class, null, null, fieldTypes, TestRecord.class);
@SuppressWarnings("rawtypes") CollectionRequest<KeyValueRecord> collectionRequest = new CollectionRequest<KeyValueRecord>(KeyValueRecord.class);
Map<CompoundKey, TestRecord> inputs = new HashMap<CompoundKey, TestRecord>();
for (CompoundKey key : keys) {
TestRecord testRecord = buildTestRecord(1L, "message" + key.hashCode());
inputs.put(key, testRecord);
collectionRequest.getElements().add(factory.create(key, testRecord));
}
@SuppressWarnings("unchecked") BatchRequest<TestRecord> batchRequest = CollectionRequestUtil.convertToBatchRequest(collectionRequest, CompoundKey.class, null, null, fieldTypes, TestRecord.class, version);
Map<String, TestRecord> entities = batchRequest.getEntities();
Assert.assertEquals(entities.size(), inputs.size());
for (CompoundKey key : keys) {
Assert.assertEquals(entities.get(BatchResponse.keyToString(key, version)), inputs.get(key));
}
}
use of com.linkedin.restli.common.CollectionRequest in project rest.li by linkedin.
the class TestClientBuilders method testBatchCreateIdRequestInputIsReadOnly.
@Test
@SuppressWarnings("unchecked")
public void testBatchCreateIdRequestInputIsReadOnly() {
BatchCreateIdRequestBuilder<Long, TestRecord> builder = new BatchCreateIdRequestBuilder<Long, TestRecord>(TEST_URI, TestRecord.class, _COLL_SPEC, RestliRequestOptions.DEFAULT_OPTIONS);
TestRecord testRecord = new TestRecord();
List<TestRecord> newRecords = Arrays.asList(testRecord);
BatchCreateIdRequest<Long, TestRecord> request = builder.inputs(newRecords).build();
CollectionRequest<TestRecord> createInput = (CollectionRequest<TestRecord>) request.getInputRecord();
Assert.assertNotSame(createInput.getElements().get(0), testRecord);
Assert.assertTrue(createInput.getElements().get(0).data().isMadeReadOnly());
testRecord.data().makeReadOnly();
request = builder.build();
createInput = (CollectionRequest<TestRecord>) request.getInputRecord();
Assert.assertSame(createInput.getElements().get(0), testRecord);
}
use of com.linkedin.restli.common.CollectionRequest in project rest.li by linkedin.
the class TestClientBuilders method testBatchCreateRequestBuilder.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "noEntity")
public void testBatchCreateRequestBuilder(URIDetails expectedURIDetails) {
BatchCreateRequestBuilder<Long, TestRecord> builder = new BatchCreateRequestBuilder<Long, TestRecord>(TEST_URI, TestRecord.class, _COLL_SPEC, RestliRequestOptions.DEFAULT_OPTIONS);
List<TestRecord> newRecords = Arrays.asList(new TestRecord(), new TestRecord(), new TestRecord());
BatchCreateRequest<TestRecord> request = builder.inputs(newRecords).appendSingleAttachment(_dataSourceWriterA).appendMultipleAttachments(_dataSourceIterator).appendSingleAttachment(_dataSourceWriterB).build();
testBaseUriGeneration(request, expectedURIDetails.getProtocolVersion());
Assert.assertEquals(request.isSafe(), false);
Assert.assertEquals(request.isIdempotent(), false);
Assert.assertEquals(request.getStreamingAttachments(), _streamingDataSources);
try {
request.getStreamingAttachments().add(new RestLiTestAttachmentDataSource("1", ByteString.empty()));
Assert.fail("Should not be able to add to an immutable list");
} catch (Exception e) {
Assert.assertTrue(e instanceof UnsupportedOperationException);
}
CollectionRequest<TestRecord> expectedRequest = new CollectionRequest<TestRecord>(new DataMap(), TestRecord.class);
expectedRequest.getElements().addAll(newRecords);
checkBasicRequest(request, expectedURIDetails, ResourceMethod.BATCH_CREATE, expectedRequest, Collections.<String, String>emptyMap(), _streamingDataSources);
}
use of com.linkedin.restli.common.CollectionRequest in project rest.li by linkedin.
the class TestScatterGather method testRequest.
@SuppressWarnings({ "unchecked", "rawtypes" })
private static void testRequest(BatchRequest<?> request, Set<String> expectedParams, Set<PathSpec> expectedFields, Map<Long, Greeting> expectedInput, Set<Set<String>> requestIdSets, Set<Long> requestIds) {
Assert.assertEquals(request.getQueryParamsObjects().keySet(), expectedParams);
if (expectedFields != null) {
Collection<PathSpec> actualFields = (Collection<PathSpec>) request.getQueryParamsObjects().get(RestConstants.FIELDS_PARAM);
for (PathSpec field : actualFields) {
Assert.assertTrue(expectedFields.contains(field));
}
}
Set<String> uriIds = new HashSet<String>();
for (Long id : (Collection<Long>) request.getQueryParamsObjects().get(RestConstants.QUERY_BATCH_IDS_PARAM)) {
uriIds.add(id.toString());
}
if (expectedInput != null) {
RecordTemplate inputRecordTemplate;
if (request instanceof BatchUpdateRequest) {
ResourceProperties resourceProperties = request.getResourceProperties();
CollectionRequest inputRecord = (CollectionRequest) request.getInputRecord();
inputRecordTemplate = CollectionRequestUtil.convertToBatchRequest(inputRecord, resourceProperties.getKeyType(), resourceProperties.getComplexKeyType(), resourceProperties.getKeyParts(), resourceProperties.getValueType());
} else {
inputRecordTemplate = request.getInputRecord();
}
checkInput(inputRecordTemplate.data().getDataMap(com.linkedin.restli.common.BatchRequest.ENTITIES), expectedInput, uriIds);
}
Set<Object> idObjects = request.getObjectIds();
Set<String> theseIds = new HashSet<String>(idObjects.size());
for (Object o : idObjects) {
theseIds.add(o.toString());
}
Assert.assertEquals(uriIds, theseIds);
//no duplicate requests
Assert.assertFalse(requestIdSets.contains(theseIds));
for (String id : theseIds) {
//no duplicate ids
Assert.assertFalse(requestIds.contains(Long.parseLong(id)));
requestIds.add(Long.parseLong(id));
}
requestIdSets.add(theseIds);
}
use of com.linkedin.restli.common.CollectionRequest in project rest.li by linkedin.
the class TestScatterGather method testGetEntityRequest.
@SuppressWarnings({ "unchecked", "rawtypes" })
private static void testGetEntityRequest(BatchRequest<BatchKVResponse<Long, EntityResponse<Greeting>>> request, Set<String> expectedParams, Set<PathSpec> expectedFields, Map<Long, Greeting> expectedInput, Set<Set<String>> requestIdSets, Set<Long> requestIds) {
Assert.assertEquals(request.getQueryParamsObjects().keySet(), expectedParams);
if (expectedFields != null) {
Collection<PathSpec> actualFields = (Collection<PathSpec>) request.getQueryParamsObjects().get(RestConstants.FIELDS_PARAM);
for (PathSpec field : actualFields) {
Assert.assertTrue(expectedFields.contains(field));
}
}
Set<String> uriIds = new HashSet<String>();
for (Long id : (Collection<Long>) request.getQueryParamsObjects().get(RestConstants.QUERY_BATCH_IDS_PARAM)) {
uriIds.add(id.toString());
}
if (expectedInput != null) {
RecordTemplate inputRecordTemplate;
if (request instanceof BatchUpdateRequest) {
ResourceProperties resourceProperties = request.getResourceProperties();
CollectionRequest inputRecord = (CollectionRequest) request.getInputRecord();
inputRecordTemplate = CollectionRequestUtil.convertToBatchRequest(inputRecord, resourceProperties.getKeyType(), resourceProperties.getComplexKeyType(), resourceProperties.getKeyParts(), resourceProperties.getValueType());
} else {
inputRecordTemplate = request.getInputRecord();
}
checkInput(inputRecordTemplate.data().getDataMap(com.linkedin.restli.common.BatchRequest.ENTITIES), expectedInput, uriIds);
}
Set<Object> idObjects = request.getObjectIds();
Set<String> theseIds = new HashSet<String>(idObjects.size());
for (Object o : idObjects) {
theseIds.add(o.toString());
}
Assert.assertEquals(uriIds, theseIds);
//no duplicate requests
Assert.assertFalse(requestIdSets.contains(theseIds));
for (String id : theseIds) {
//no duplicate ids
Assert.assertFalse(requestIds.contains(Long.parseLong(id)));
requestIds.add(Long.parseLong(id));
}
requestIdSets.add(theseIds);
}
Aggregations