Search in sources :

Example 1 with BatchRequest

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

the class CollectionRequestUtil method convertToBatchRequest.

/**
 * Converts the new way of representing {@link com.linkedin.restli.client.BatchUpdateRequest}s and
 * {@link com.linkedin.restli.client.BatchPartialUpdateRequest}s bodies into the old way
 * @param elementList new style encoding
 * @param keyType
 * @param complexKeyType
 * @param keyParts
 * @param valueType
 * @param version protocol version to use for encoding
 * @param <V>
 * @return a data map with one key, "entities". "entities" maps to another data map (as in the old body encoding)
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <V extends RecordTemplate> BatchRequest<V> convertToBatchRequest(CollectionRequest<KeyValueRecord> elementList, TypeSpec<?> keyType, ComplexKeySpec<?, ?> complexKeyType, Map<String, CompoundKey.TypeInfo> keyParts, TypeSpec<V> valueType, ProtocolVersion version) {
    BatchRequest<V> batchRequest = new BatchRequest<>(new DataMap(), valueType);
    for (KeyValueRecord keyValueRecord : elementList.getElements()) {
        V value = (V) keyValueRecord.getValue(valueType);
        Object key = null;
        if (keyType != null) {
            if (keyType.getType().equals(ComplexResourceKey.class)) {
                // complex keys
                key = keyValueRecord.getComplexKey(complexKeyType);
            } else if (CompoundKey.class.isAssignableFrom(keyType.getType())) {
                key = keyValueRecord.getCompoundKey(keyParts);
            } else {
                // primitive keys
                key = keyValueRecord.getPrimitiveKey(keyType);
            }
        }
        batchRequest.getEntities().put(URIParamUtils.encodeKeyForBody(key, true, version), value);
    }
    return batchRequest;
}
Also used : KeyValueRecord(com.linkedin.restli.common.KeyValueRecord) BatchRequest(com.linkedin.restli.common.BatchRequest) CompoundKey(com.linkedin.restli.common.CompoundKey) DataMap(com.linkedin.data.DataMap)

Example 2 with BatchRequest

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

the class TestClientBuilders method testBatchUpdateRequestBuilder.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "batch")
public void testBatchUpdateRequestBuilder(URIDetails expectedURIDetails) {
    BatchUpdateRequestBuilder<Long, TestRecord> builder = new BatchUpdateRequestBuilder<>(TEST_URI, TestRecord.class, _COLL_SPEC, RestliRequestOptions.DEFAULT_OPTIONS);
    Map<Long, TestRecord> updates = new HashMap<>();
    updates.put(1L, new TestRecord());
    updates.put(2L, new TestRecord());
    updates.put(3L, new TestRecord());
    BatchUpdateRequest<Long, TestRecord> request = builder.inputs(updates).appendSingleAttachment(_dataSourceWriterA).appendMultipleAttachments(_dataSourceIterator).appendSingleAttachment(_dataSourceWriterB).build();
    testBaseUriGeneration(request, expectedURIDetails.getProtocolVersion());
    Assert.assertEquals(request.getObjectIds(), new HashSet<>(Arrays.asList(1L, 2L, 3L)));
    Assert.assertEquals(request.isSafe(), false);
    Assert.assertEquals(request.isIdempotent(), true);
    BatchRequest<TestRecord> expectedRequest = new BatchRequest<>(new DataMap(), TestRecord.class);
    expectedRequest.getEntities().put("1", new TestRecord());
    expectedRequest.getEntities().put("2", new TestRecord());
    expectedRequest.getEntities().put("3", new TestRecord());
    @SuppressWarnings({ "unchecked", "rawtypes" }) KeyValueRecordFactory<Long, TestRecord> factory = new KeyValueRecordFactory<>(Long.class, null, null, null, TestRecord.class);
    @SuppressWarnings({ "unchecked", "rawtypes" }) CollectionRequest<KeyValueRecord> collectionRequest = buildCollectionRequest(factory, new Long[] { 1L, 2L, 3L }, new TestRecord[] { new TestRecord(), new TestRecord(), new TestRecord() });
    checkBasicRequest(request, expectedURIDetails, ResourceMethod.BATCH_UPDATE, collectionRequest, expectedRequest, Collections.<String, String>emptyMap(), _streamingDataSources);
}
Also used : BatchRequest(com.linkedin.restli.common.BatchRequest) HashMap(java.util.HashMap) KeyValueRecordFactory(com.linkedin.restli.common.KeyValueRecordFactory) DataMap(com.linkedin.data.DataMap) KeyValueRecord(com.linkedin.restli.common.KeyValueRecord) TestRecord(com.linkedin.restli.client.test.TestRecord) Test(org.testng.annotations.Test)

Example 3 with BatchRequest

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

the class TestClientBuilders method testBatchPartialUpdateRequestBuilder.

// need suppress on the method because the more specific suppress isn't being obeyed.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "batch")
public void testBatchPartialUpdateRequestBuilder(URIDetails expectedURIDetails) {
    BatchPartialUpdateRequestBuilder<Long, TestRecord> builder = new BatchPartialUpdateRequestBuilder<>(TEST_URI, TestRecord.class, _COLL_SPEC, RestliRequestOptions.DEFAULT_OPTIONS);
    builder.input(1L, new PatchRequest<>());
    builder.input(2L, new PatchRequest<>());
    builder.input(3L, new PatchRequest<>());
    BatchPartialUpdateRequest<Long, TestRecord> request = builder.appendSingleAttachment(_dataSourceWriterA).appendMultipleAttachments(_dataSourceIterator).appendSingleAttachment(_dataSourceWriterB).build();
    testBaseUriGeneration(request, expectedURIDetails.getProtocolVersion());
    Assert.assertEquals(request.getObjectIds(), new HashSet<>(Arrays.asList(1L, 2L, 3L)));
    Assert.assertEquals(request.isSafe(), false);
    Assert.assertEquals(request.isIdempotent(), false);
    // verify partialUpdateInputMap
    Map<Long, PatchRequest<TestRecord>> expectedPartialUpdateMap = new HashMap<>();
    expectedPartialUpdateMap.put(1L, new PatchRequest<>());
    expectedPartialUpdateMap.put(2L, new PatchRequest<>());
    expectedPartialUpdateMap.put(3L, new PatchRequest<>());
    Assert.assertNotNull(request.getPartialUpdateInputMap());
    Assert.assertEquals(request.getPartialUpdateInputMap(), expectedPartialUpdateMap);
    @SuppressWarnings({ "unchecked", "rawtypes" }) BatchRequest<PatchRequest<TestRecord>> expectedRequest = new BatchRequest(new DataMap(), PatchRequest.class);
    expectedRequest.getEntities().put("1", new PatchRequest<>());
    expectedRequest.getEntities().put("2", new PatchRequest<>());
    expectedRequest.getEntities().put("3", new PatchRequest<>());
    KeyValueRecordFactory<Long, PatchRequest> factory = new KeyValueRecordFactory<>(Long.class, null, null, null, PatchRequest.class);
    CollectionRequest<KeyValueRecord> collectionRequest = buildCollectionRequest(factory, new Long[] { 1L, 2L, 3L }, new PatchRequest[] { new PatchRequest(), new PatchRequest(), new PatchRequest() });
    checkBasicRequest(request, expectedURIDetails, ResourceMethod.BATCH_PARTIAL_UPDATE, collectionRequest, expectedRequest, Collections.<String, String>emptyMap(), _streamingDataSources);
}
Also used : BatchRequest(com.linkedin.restli.common.BatchRequest) HashMap(java.util.HashMap) KeyValueRecordFactory(com.linkedin.restli.common.KeyValueRecordFactory) PatchRequest(com.linkedin.restli.common.PatchRequest) DataMap(com.linkedin.data.DataMap) KeyValueRecord(com.linkedin.restli.common.KeyValueRecord) TestRecord(com.linkedin.restli.client.test.TestRecord) Test(org.testng.annotations.Test)

Example 4 with BatchRequest

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

the class TestClientBuilders method testBatchUpdateCompoundKeyRequestBuilder.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "batchCompoundKey")
public void testBatchUpdateCompoundKeyRequestBuilder(URIDetails expectedURIDetails) {
    BatchUpdateRequestBuilder<CompoundKey, TestRecord> builder = new BatchUpdateRequestBuilder<>(TEST_URI, TestRecord.class, _ASSOC_SPEC, RestliRequestOptions.DEFAULT_OPTIONS);
    Map<CompoundKey, TestRecord> inputs = new HashMap<>();
    CompoundKey key1 = new CompoundKey().append("part1", 1L).append("part2", "2");
    CompoundKey key2 = new CompoundKey().append("part1", 11L).append("part2", "22");
    TestRecord t1 = new TestRecord().setId(1L).setMessage("1");
    TestRecord t2 = new TestRecord().setId(2L);
    inputs.put(key1, t1);
    inputs.put(key2, t2);
    BatchRequest<TestRecord> expectedRequest = new BatchRequest<>(new DataMap(), TestRecord.class);
    expectedRequest.getEntities().put(toEntityKey(key1, expectedURIDetails.getProtocolVersion()), t1);
    expectedRequest.getEntities().put(toEntityKey(key2, expectedURIDetails.getProtocolVersion()), t2);
    BatchUpdateRequest<CompoundKey, TestRecord> request = builder.inputs(inputs).build();
    testBaseUriGeneration(request, expectedURIDetails.getProtocolVersion());
    Assert.assertEquals(request.isSafe(), false);
    Assert.assertEquals(request.isIdempotent(), true);
    KeyValueRecordFactory<CompoundKey, TestRecord> factory = new KeyValueRecordFactory<>(CompoundKey.class, null, null, getCompoundKeyFieldTypes(), TestRecord.class);
    @SuppressWarnings({ "unchecked", "rawtypes" }) CollectionRequest<KeyValueRecord> collectionRequest = buildCollectionRequest(factory, new CompoundKey[] { key1, key2 }, new TestRecord[] { t1, t2 });
    checkBasicRequest(request, expectedURIDetails, ResourceMethod.BATCH_UPDATE, collectionRequest, expectedRequest, Collections.<String, String>emptyMap(), null);
}
Also used : BatchRequest(com.linkedin.restli.common.BatchRequest) CompoundKey(com.linkedin.restli.common.CompoundKey) HashMap(java.util.HashMap) KeyValueRecordFactory(com.linkedin.restli.common.KeyValueRecordFactory) DataMap(com.linkedin.data.DataMap) KeyValueRecord(com.linkedin.restli.common.KeyValueRecord) TestRecord(com.linkedin.restli.client.test.TestRecord) Test(org.testng.annotations.Test)

Example 5 with BatchRequest

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

the class TestClientBuilders method testBatchPartialUpdateCompoundKeyRequestBuilder.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "batchCompoundKey")
public void testBatchPartialUpdateCompoundKeyRequestBuilder(URIDetails expectedURIDetails) throws CloneNotSupportedException {
    BatchPartialUpdateRequestBuilder<CompoundKey, TestRecord> builder = new BatchPartialUpdateRequestBuilder<>(TEST_URI, TestRecord.class, _ASSOC_SPEC, RestliRequestOptions.DEFAULT_OPTIONS);
    Map<CompoundKey, PatchRequest<TestRecord>> inputs = new HashMap<>();
    CompoundKey key1 = new CompoundKey().append("part1", 1L).append("part2", "2");
    CompoundKey key2 = new CompoundKey().append("part1", 11L).append("part2", "22");
    TestRecord t1 = new TestRecord().setId(1L).setMessage("1");
    TestRecord t2 = new TestRecord().setId(2L);
    TestRecord t3 = new TestRecord().setMessage("3");
    PatchRequest<TestRecord> patch1 = PatchGenerator.diff(t1, t2);
    PatchRequest<TestRecord> patch2 = PatchGenerator.diff(t2, t3);
    inputs.put(key1, patch1);
    inputs.put(key2, patch2);
    BatchPartialUpdateRequest<CompoundKey, TestRecord> request = builder.inputs(inputs).build();
    testBaseUriGeneration(request, expectedURIDetails.getProtocolVersion());
    Assert.assertEquals(request.isSafe(), false);
    Assert.assertEquals(request.isIdempotent(), false);
    Assert.assertNotNull(request.getPartialUpdateInputMap());
    Assert.assertEquals(request.getPartialUpdateInputMap(), inputs);
    @SuppressWarnings({ "unchecked", "rawtypes" }) BatchRequest<PatchRequest<TestRecord>> expectedRequest = new BatchRequest(new DataMap(), PatchRequest.class);
    expectedRequest.getEntities().put(toEntityKey(key1, expectedURIDetails.getProtocolVersion()), patch1);
    expectedRequest.getEntities().put(toEntityKey(key2, expectedURIDetails.getProtocolVersion()), patch2);
    @SuppressWarnings({ "unchecked", "rawtypes" }) KeyValueRecordFactory<CompoundKey, PatchRequest> factory = new KeyValueRecordFactory<>(CompoundKey.class, null, null, getCompoundKeyFieldTypes(), PatchRequest.class);
    @SuppressWarnings({ "unchecked", "rawtypes" }) CollectionRequest<KeyValueRecord> collectionRequest = buildCollectionRequest(factory, new CompoundKey[] { key1, key2 }, new PatchRequest[] { patch1, patch2 });
    checkBasicRequest(request, expectedURIDetails, ResourceMethod.BATCH_PARTIAL_UPDATE, collectionRequest, expectedRequest, Collections.<String, String>emptyMap(), null);
}
Also used : BatchRequest(com.linkedin.restli.common.BatchRequest) CompoundKey(com.linkedin.restli.common.CompoundKey) HashMap(java.util.HashMap) KeyValueRecordFactory(com.linkedin.restli.common.KeyValueRecordFactory) PatchRequest(com.linkedin.restli.common.PatchRequest) DataMap(com.linkedin.data.DataMap) KeyValueRecord(com.linkedin.restli.common.KeyValueRecord) TestRecord(com.linkedin.restli.client.test.TestRecord) Test(org.testng.annotations.Test)

Aggregations

DataMap (com.linkedin.data.DataMap)7 BatchRequest (com.linkedin.restli.common.BatchRequest)7 KeyValueRecord (com.linkedin.restli.common.KeyValueRecord)7 TestRecord (com.linkedin.restli.client.test.TestRecord)6 KeyValueRecordFactory (com.linkedin.restli.common.KeyValueRecordFactory)6 Test (org.testng.annotations.Test)6 HashMap (java.util.HashMap)5 CompoundKey (com.linkedin.restli.common.CompoundKey)3 PatchRequest (com.linkedin.restli.common.PatchRequest)3 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)2 DynamicRecordTemplate (com.linkedin.data.template.DynamicRecordTemplate)1 RecordTemplate (com.linkedin.data.template.RecordTemplate)1