Search in sources :

Example 66 with ComplexResourceKey

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

the class TestClientBuilders method testComplexKeyBatchUpdateRequestBuilder.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "batchComplexKeyAndParam")
public void testComplexKeyBatchUpdateRequestBuilder(URIDetails expectedURIDetails) throws Exception {
    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");
    RecordTemplate param = buildComplexParam(123, "ParamMessage");
    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).setParam("testParam", param).build();
    // using toStringFull (which is deprecated) because this is only used to check v1
    BatchRequest<TestRecord> expectedRequest = new BatchRequest<>(new DataMap(), TestRecord.class);
    expectedRequest.getEntities().put(toEntityKey(id1, expectedURIDetails.getProtocolVersion()), t1);
    expectedRequest.getEntities().put(toEntityKey(id2, expectedURIDetails.getProtocolVersion()), t2);
    testBaseUriGeneration(request, expectedURIDetails.getProtocolVersion());
    Assert.assertTrue(request.isIdempotent());
    Assert.assertFalse(request.isSafe());
    @SuppressWarnings({ "unchecked", "rawtypes" }) KeyValueRecordFactory<ComplexResourceKey, TestRecord> factory = new KeyValueRecordFactory<>(ComplexResourceKey.class, TestRecord.class, TestRecord.class, null, TestRecord.class);
    @SuppressWarnings({ "unchecked", "rawtypes" }) CollectionRequest<KeyValueRecord> collectionRequest = buildCollectionRequest(factory, new ComplexResourceKey[] { id1, id2 }, 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) KeyValueRecordFactory(com.linkedin.restli.common.KeyValueRecordFactory) DataMap(com.linkedin.data.DataMap) KeyValueRecord(com.linkedin.restli.common.KeyValueRecord) RecordTemplate(com.linkedin.data.template.RecordTemplate) DynamicRecordTemplate(com.linkedin.data.template.DynamicRecordTemplate) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) TestRecord(com.linkedin.restli.client.test.TestRecord) Test(org.testng.annotations.Test)

Example 67 with ComplexResourceKey

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

the class TestClientBuilders method testComplexKeyCreateRequestBuilder.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "noEntity")
public void testComplexKeyCreateRequestBuilder(URIDetails expectedURIDetails) {
    CreateRequestBuilder<ComplexResourceKey<TestRecord, TestRecord>, TestRecord> builder = new CreateRequestBuilder<>(TEST_URI, TestRecord.class, _COMPLEX_KEY_SPEC, RestliRequestOptions.DEFAULT_OPTIONS);
    CreateRequest<TestRecord> request = builder.input(new TestRecord()).build();
    testBaseUriGeneration(request, expectedURIDetails.getProtocolVersion());
    Assert.assertEquals(request.isSafe(), false);
    Assert.assertEquals(request.isIdempotent(), false);
    checkBasicRequest(request, expectedURIDetails, ResourceMethod.CREATE, new TestRecord(), Collections.<String, String>emptyMap(), null);
}
Also used : ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) TestRecord(com.linkedin.restli.client.test.TestRecord) Test(org.testng.annotations.Test)

Example 68 with ComplexResourceKey

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

the class TestAbstractRequestBuilder method testPathKeysAreReadOnly.

@Test
@SuppressWarnings("unchecked")
public void testPathKeysAreReadOnly() {
    final AbstractRequestBuilder<Object, ?, ?> builder = new DummyAbstractRequestBuilder();
    TestRecord testRecord = new TestRecord();
    TestRecord testRecord2 = new TestRecord();
    ComplexResourceKey<TestRecord, TestRecord> originalKey = new ComplexResourceKey<>(testRecord, testRecord2);
    builder.pathKey("abc", originalKey);
    Map<String, Object> pathKeys = builder.buildReadOnlyPathKeys();
    Object key = pathKeys.get("abc");
    Assert.assertNotSame(key, originalKey);
    Assert.assertTrue(((ComplexResourceKey<TestRecord, TestRecord>) key).isReadOnly());
    try {
        pathKeys.put("xyz", "abc");
        Assert.fail("The generated path keys should be read-only.");
    } catch (Exception e) {
    }
    originalKey.makeReadOnly();
    pathKeys = builder.buildReadOnlyPathKeys();
    key = pathKeys.get("abc");
    Assert.assertSame(key, originalKey);
}
Also used : ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) TestRecord(com.linkedin.restli.client.test.TestRecord) Test(org.testng.annotations.Test)

Example 69 with ComplexResourceKey

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

the class TestCollectionRequestUtil method testComplexKeyMultipleEntities.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "versions")
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testComplexKeyMultipleEntities(ProtocolVersion version) {
    // TestRecord is not keyed by a ComplexResourceKey, but for this test we pretend that it is.
    TestRecord kk1 = buildTestRecord(1, "key key 1");
    TestRecord kp1 = buildTestRecord(2, "key params 1");
    TestRecord kk2 = buildTestRecord(3, "key key 2");
    TestRecord kp2 = buildTestRecord(4, "key params 2");
    ComplexResourceKey<TestRecord, TestRecord> key1 = new ComplexResourceKey<>(kk1, kp1);
    ComplexResourceKey<TestRecord, TestRecord> key2 = new ComplexResourceKey<>(kk2, kp2);
    ComplexResourceKey[] keys = { key1, key2 };
    KeyValueRecordFactory<ComplexResourceKey, TestRecord> factory = new KeyValueRecordFactory<>(ComplexResourceKey.class, TestRecord.class, TestRecord.class, null, TestRecord.class);
    CollectionRequest<KeyValueRecord> collectionRequest = new CollectionRequest<>(KeyValueRecord.class);
    Map<ComplexResourceKey<TestRecord, TestRecord>, TestRecord> inputs = new HashMap<>();
    for (ComplexResourceKey key : keys) {
        TestRecord testRecord = buildTestRecord(1L, "foo");
        inputs.put(key, testRecord);
        collectionRequest.getElements().add(factory.create(key, testRecord));
    }
    @SuppressWarnings({ "unchecked", "rawtypes" }) BatchRequest<TestRecord> batchRequest = CollectionRequestUtil.convertToBatchRequest(collectionRequest, ComplexResourceKey.class, TestRecord.class, TestRecord.class, null, TestRecord.class, version);
    Map<String, TestRecord> entities = batchRequest.getEntities();
    Assert.assertEquals(entities.size(), inputs.size());
    for (ComplexResourceKey key : keys) {
        Assert.assertEquals(entities.get(URIParamUtils.keyToString(key, URLEscaper.Escaping.NO_ESCAPING, null, true, version)), inputs.get(key));
    }
}
Also used : CollectionRequest(com.linkedin.restli.common.CollectionRequest) HashMap(java.util.HashMap) KeyValueRecordFactory(com.linkedin.restli.common.KeyValueRecordFactory) KeyValueRecord(com.linkedin.restli.common.KeyValueRecord) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) TestRecord(com.linkedin.restli.client.test.TestRecord) Test(org.testng.annotations.Test)

Example 70 with ComplexResourceKey

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

the class TestClientBuilders method checkRequestIsReadOnly.

@SuppressWarnings("unchecked")
private void checkRequestIsReadOnly(final Request<?> request) {
    final Set<PathSpec> fields = request.getFields();
    if (fields != null) {
        checkReadOnlyOperation(new Runnable() {

            @Override
            public void run() {
                fields.add(new PathSpec("abc"));
            }
        });
    }
    checkReadOnlyOperation(new Runnable() {

        @Override
        public void run() {
            request.getHeaders().put("abc", "abc");
        }
    });
    final RecordTemplate input = request.getInputRecord();
    if (input != null) {
        checkReadOnlyOperation(new Runnable() {

            @Override
            public void run() {
                input.data().put("abc", "abc");
            }
        });
    }
    final Map<String, Object> pathKeys = request.getPathKeys();
    if (pathKeys != null) {
        checkReadOnlyOperation(new Runnable() {

            @Override
            public void run() {
                pathKeys.put("abc", "abc");
            }
        });
        final List<Object> keysToEdit = new ArrayList<>();
        for (Object key : pathKeys.values()) {
            if (key instanceof CompoundKey || key instanceof ComplexResourceKey) {
                keysToEdit.add(key);
            } else {
                Assert.assertTrue(isPrimitiveOrEnum(key));
            }
        }
        for (final Object keytoEdit : keysToEdit) {
            checkReadOnlyOperation(new Runnable() {

                @Override
                public void run() {
                    if (keytoEdit instanceof ComplexResourceKey) {
                        ((ComplexResourceKey) keytoEdit).getKey().data().put("abc", "abc");
                    } else if (keytoEdit instanceof CompoundKey) {
                        ((CompoundKey) keytoEdit).append("abc", "abc");
                    }
                }
            });
        }
        Collection<Object> queryParamObjects = request.getQueryParamsObjects().values();
        List<Object> readOnlyTargets = new ArrayList<>();
        for (Object queryParamObject : queryParamObjects) {
            collectReadOnlyQueryParamObjectTargets(queryParamObject, readOnlyTargets);
        }
        for (final Object readOnlyTarget : readOnlyTargets) {
            checkReadOnlyOperation(new Runnable() {

                @Override
                public void run() {
                    if (readOnlyTarget instanceof DataTemplate) {
                        Object data = ((DataTemplate) readOnlyTarget).data();
                        if (data instanceof DataMap) {
                            ((DataMap) data).put("abc", "abc");
                        } else if (data instanceof DataList) {
                            ((DataList) data).add("abc");
                        }
                    } else if (readOnlyTarget instanceof CompoundKey) {
                        ((CompoundKey) readOnlyTarget).append("abc", "abc");
                    } else if (readOnlyTarget instanceof ComplexResourceKey) {
                        ((ComplexResourceKey) readOnlyTarget).getKey().data().put("abc", "abc");
                    } else if (readOnlyTarget instanceof List || readOnlyTarget instanceof Set) {
                        ((Collection<Object>) readOnlyTarget).add("abc");
                    }
                }
            });
        }
    }
}
Also used : EnumSet(java.util.EnumSet) Set(java.util.Set) HashSet(java.util.HashSet) CompoundKey(com.linkedin.restli.common.CompoundKey) DataTemplate(com.linkedin.data.template.DataTemplate) ArrayList(java.util.ArrayList) ByteString(com.linkedin.data.ByteString) PathSpec(com.linkedin.data.schema.PathSpec) DataMap(com.linkedin.data.DataMap) DataList(com.linkedin.data.DataList) RecordTemplate(com.linkedin.data.template.RecordTemplate) DynamicRecordTemplate(com.linkedin.data.template.DynamicRecordTemplate) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) DataList(com.linkedin.data.DataList)

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