Search in sources :

Example 41 with ComplexResourceKey

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

the class TestClientBuilders method testComplexKeyBatchPartialUpdateRequestBuilder.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "batchComplexKey")
public void testComplexKeyBatchPartialUpdateRequestBuilder(URIDetails expectedURIDetails) {
    BatchPartialUpdateRequestBuilder<ComplexResourceKey<TestRecord, TestRecord>, TestRecord> builder = new BatchPartialUpdateRequestBuilder<ComplexResourceKey<TestRecord, TestRecord>, TestRecord>(TEST_URI, TestRecord.class, _COMPLEX_KEY_SPEC, RestliRequestOptions.DEFAULT_OPTIONS);
    Map<ComplexResourceKey<TestRecord, TestRecord>, PatchRequest<TestRecord>> inputs = new HashMap<ComplexResourceKey<TestRecord, TestRecord>, PatchRequest<TestRecord>>();
    ComplexResourceKey<TestRecord, TestRecord> key1 = buildComplexKey(1L, "keyMessage1", 2L, "paramMessage1");
    ComplexResourceKey<TestRecord, TestRecord> key2 = buildComplexKey(3L, "keyMessage2", 4L, "paramMessage2");
    TestRecord t1 = new TestRecord().setId(1L);
    TestRecord t2 = new TestRecord().setId(2L).setMessage("foo");
    TestRecord t3 = new TestRecord().setMessage("bar");
    PatchRequest<TestRecord> patch1 = PatchGenerator.diff(t1, t2);
    PatchRequest<TestRecord> patch2 = PatchGenerator.diff(t2, t3);
    inputs.put(key1, patch1);
    inputs.put(key2, patch2);
    BatchPartialUpdateRequest<ComplexResourceKey<TestRecord, TestRecord>, TestRecord> request = builder.inputs(inputs).build();
    Assert.assertEquals(request.isSafe(), false);
    Assert.assertEquals(request.isIdempotent(), false);
    testBaseUriGeneration(request, expectedURIDetails.getProtocolVersion());
    // using .toStringFull (which is deprecated) because this is only used for checking v1
    @SuppressWarnings({ "unchecked", "rawtypes" }) BatchRequest<PatchRequest<TestRecord>> batchRequest = new BatchRequest(new DataMap(), PatchRequest.class);
    batchRequest.getEntities().put(toEntityKey(key1, expectedURIDetails.getProtocolVersion()), patch1);
    batchRequest.getEntities().put(toEntityKey(key2, expectedURIDetails.getProtocolVersion()), patch2);
    @SuppressWarnings({ "unchecked", "rawtypes" }) KeyValueRecordFactory<ComplexResourceKey, PatchRequest> factory = new KeyValueRecordFactory<ComplexResourceKey, PatchRequest>(ComplexResourceKey.class, TestRecord.class, TestRecord.class, null, PatchRequest.class);
    @SuppressWarnings({ "unchecked", "rawtypes" }) CollectionRequest<KeyValueRecord> collectionRequest = buildCollectionRequest(factory, new ComplexResourceKey[] { key1, key2 }, new PatchRequest[] { patch1, patch2 });
    checkBasicRequest(request, expectedURIDetails, ResourceMethod.BATCH_PARTIAL_UPDATE, collectionRequest, batchRequest, Collections.<String, String>emptyMap(), null);
}
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) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) TestRecord(com.linkedin.restli.client.test.TestRecord) Test(org.testng.annotations.Test)

Example 42 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<TestRecord, TestRecord>(kk1, kp1);
    ComplexResourceKey<TestRecord, TestRecord> key2 = new ComplexResourceKey<TestRecord, TestRecord>(kk2, kp2);
    ComplexResourceKey[] keys = { key1, key2 };
    KeyValueRecordFactory<ComplexResourceKey, TestRecord> factory = new KeyValueRecordFactory<ComplexResourceKey, TestRecord>(ComplexResourceKey.class, TestRecord.class, TestRecord.class, null, TestRecord.class);
    CollectionRequest<KeyValueRecord> collectionRequest = new CollectionRequest<KeyValueRecord>(KeyValueRecord.class);
    Map<ComplexResourceKey<TestRecord, TestRecord>, TestRecord> inputs = new HashMap<ComplexResourceKey<TestRecord, TestRecord>, TestRecord>();
    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 43 with ComplexResourceKey

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

the class ArgumentUtils method parseSimplePathKey.

/**
   * The method parses out and returns the correct simple type of the key out of the Object.
   * It does not handle {@link CompoundKey}s or {@link ComplexResourceKey}s.
   *
   * @param value key value string representation to parse
   * @param resource {@link com.linkedin.restli.internal.server.model.ResourceModel} containing the key type
   * @param version the {@link com.linkedin.restli.common.ProtocolVersion}
   * @return parsed key value in the correct type for the key
   * @throws IllegalArgumentException
   * @throws NumberFormatException
   */
public static Object parseSimplePathKey(final String value, final ResourceModel resource, final ProtocolVersion version) throws IllegalArgumentException {
    Key key = resource.getPrimaryKey();
    String decodedValue;
    if (version.compareTo(AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion()) >= 0) {
        decodedValue = UriComponent.decode(value, UriComponent.Type.PATH_SEGMENT);
    } else {
        decodedValue = URLEscaper.unescape(value, URLEscaper.Escaping.URL_ESCAPING);
    }
    return convertSimpleValue(decodedValue, key.getDataSchema(), key.getType());
}
Also used : ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) AlternativeKey(com.linkedin.restli.server.AlternativeKey) Key(com.linkedin.restli.server.Key) CompoundKey(com.linkedin.restli.common.CompoundKey)

Example 44 with ComplexResourceKey

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

the class TestClientBuilders method testComplexKeyBatchGetRequestBuilder.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "batchComplexKeyAndParam")
public void testComplexKeyBatchGetRequestBuilder(URIDetails expectedURIDetails) throws Exception {
    BatchGetRequestBuilder<ComplexResourceKey<TestRecord, TestRecord>, TestRecord> builder = new BatchGetRequestBuilder<ComplexResourceKey<TestRecord, TestRecord>, TestRecord>(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");
    @SuppressWarnings("unchecked") BatchGetKVRequest<ComplexResourceKey<TestRecord, TestRecord>, TestRecord> request = builder.ids(id1, id2).setParam("testParam", param).buildKV();
    Assert.assertTrue(request.isIdempotent());
    Assert.assertTrue(request.isSafe());
    checkBasicRequest(request, expectedURIDetails, ResourceMethod.BATCH_GET, null, Collections.<String, String>emptyMap(), null);
}
Also used : 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 45 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<Object>();
        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<Object>();
        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) {
                        ((List<Object>) readOnlyTarget).add("abc");
                    }
                }
            });
        }
    }
}
Also used : 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) List(java.util.List) ArrayList(java.util.ArrayList) DataList(com.linkedin.data.DataList)

Aggregations

ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)71 Test (org.testng.annotations.Test)36 TestRecord (com.linkedin.restli.client.test.TestRecord)18 DataMap (com.linkedin.data.DataMap)15 CompoundKey (com.linkedin.restli.common.CompoundKey)15 TwoPartKey (com.linkedin.restli.examples.greetings.api.TwoPartKey)15 HashMap (java.util.HashMap)15 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)12 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)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 AfterTest (org.testng.annotations.AfterTest)11 BeforeTest (org.testng.annotations.BeforeTest)11 Message (com.linkedin.restli.examples.greetings.api.Message)10 ArrayList (java.util.ArrayList)10 RecordTemplate (com.linkedin.data.template.RecordTemplate)9 Key (com.linkedin.restli.server.Key)9 DiscoveredItem (com.linkedin.restli.server.twitter.TwitterTestDataModels.DiscoveredItem)9