Search in sources :

Example 11 with CollectionRequest

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));
    }
}
Also used : CollectionRequest(com.linkedin.restli.common.CollectionRequest) CompoundKey(com.linkedin.restli.common.CompoundKey) HashMap(java.util.HashMap) KeyValueRecordFactory(com.linkedin.restli.common.KeyValueRecordFactory) KeyValueRecord(com.linkedin.restli.common.KeyValueRecord) TestRecord(com.linkedin.restli.client.test.TestRecord) Test(org.testng.annotations.Test)

Example 12 with CollectionRequest

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);
}
Also used : CollectionRequest(com.linkedin.restli.common.CollectionRequest) TestRecord(com.linkedin.restli.client.test.TestRecord) Test(org.testng.annotations.Test)

Example 13 with CollectionRequest

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);
}
Also used : CollectionRequest(com.linkedin.restli.common.CollectionRequest) RestLiTestAttachmentDataSource(com.linkedin.restli.internal.testutils.RestLiTestAttachmentDataSource) TestRecord(com.linkedin.restli.client.test.TestRecord) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 14 with CollectionRequest

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);
}
Also used : CollectionRequest(com.linkedin.restli.common.CollectionRequest) ResourceProperties(com.linkedin.restli.common.ResourceProperties) PathSpec(com.linkedin.data.schema.PathSpec) RecordTemplate(com.linkedin.data.template.RecordTemplate) Collection(java.util.Collection) HashSet(java.util.HashSet)

Example 15 with CollectionRequest

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);
}
Also used : CollectionRequest(com.linkedin.restli.common.CollectionRequest) ResourceProperties(com.linkedin.restli.common.ResourceProperties) PathSpec(com.linkedin.data.schema.PathSpec) RecordTemplate(com.linkedin.data.template.RecordTemplate) Collection(java.util.Collection) HashSet(java.util.HashSet)

Aggregations

CollectionRequest (com.linkedin.restli.common.CollectionRequest)16 DataMap (com.linkedin.data.DataMap)7 TestRecord (com.linkedin.restli.client.test.TestRecord)7 Test (org.testng.annotations.Test)7 KeyValueRecord (com.linkedin.restli.common.KeyValueRecord)6 HashMap (java.util.HashMap)5 KeyValueRecordFactory (com.linkedin.restli.common.KeyValueRecordFactory)4 RecordTemplate (com.linkedin.data.template.RecordTemplate)3 PathSpec (com.linkedin.data.schema.PathSpec)2 CompoundKey (com.linkedin.restli.common.CompoundKey)2 ResourceProperties (com.linkedin.restli.common.ResourceProperties)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)1 EmptyRecord (com.linkedin.restli.common.EmptyRecord)1 PatchRequest (com.linkedin.restli.common.PatchRequest)1 ResourceSpec (com.linkedin.restli.common.ResourceSpec)1 ResourceSpecImpl (com.linkedin.restli.common.ResourceSpecImpl)1 ResourcePropertiesImpl (com.linkedin.restli.internal.common.ResourcePropertiesImpl)1