Search in sources :

Example 46 with RecordTemplate

use of com.linkedin.data.template.RecordTemplate in project rest.li by linkedin.

the class UpdateArgumentBuilder method extractRequestData.

@Override
public RestLiRequestData extractRequestData(RoutingResult routingResult, RestRequest request) {
    RecordTemplate record = ArgumentBuilder.extractEntity(request, ArgumentUtils.getValueClass(routingResult));
    RestLiRequestDataImpl.Builder builder = new RestLiRequestDataImpl.Builder().entity(record);
    if (ArgumentUtils.hasResourceKey(routingResult)) {
        Object keyValue = ArgumentUtils.getResourceKey(routingResult);
        builder.key(keyValue);
    }
    return builder.build();
}
Also used : RecordTemplate(com.linkedin.data.template.RecordTemplate) RestLiRequestDataImpl(com.linkedin.restli.server.RestLiRequestDataImpl)

Example 47 with RecordTemplate

use of com.linkedin.data.template.RecordTemplate 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 48 with RecordTemplate

use of com.linkedin.data.template.RecordTemplate 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)

Example 49 with RecordTemplate

use of com.linkedin.data.template.RecordTemplate 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<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");
    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<TestRecord>(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, TestRecord>(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 50 with RecordTemplate

use of com.linkedin.data.template.RecordTemplate in project rest.li by linkedin.

the class TestClientBuilders method checkKeyValueRecordCollectionIsReadOnly.

@SuppressWarnings("unchecked")
private <V extends RecordTemplate> void checkKeyValueRecordCollectionIsReadOnly(ComplexResourceKey<TestRecord, TestRecord> id1, ComplexResourceKey<TestRecord, TestRecord> id2, RecordTemplate t1, RecordTemplate t2, Class<V> valueClass, CollectionRequest<KeyValueRecord<?, V>> entries) {
    for (KeyValueRecord<?, V> entry : entries.getElements()) {
        ComplexResourceKey<TestRecord, TestRecord> generatedKey = entry.getComplexKey(TestRecord.class, TestRecord.class);
        if (generatedKey.equals(id1)) {
            checkComplexKeyIsReadOnly(id1, generatedKey);
        } else {
            checkComplexKeyIsReadOnly(id2, generatedKey);
        }
        Assert.assertTrue(generatedKey.isReadOnly());
        RecordTemplate value = entry.getValue(valueClass);
        if (value.equals(t1)) {
            if (t1.data().isMadeReadOnly()) {
                Assert.assertSame(t1.data(), value.data());
            } else {
                Assert.assertNotSame(t1.data(), value.data());
            }
        } else {
            if (t2.data().isReadOnly()) {
                Assert.assertSame(t2.data(), value.data());
            } else {
                Assert.assertNotSame(t2.data(), value.data());
            }
        }
        Assert.assertTrue(value.data().isMadeReadOnly());
    }
}
Also used : RecordTemplate(com.linkedin.data.template.RecordTemplate) DynamicRecordTemplate(com.linkedin.data.template.DynamicRecordTemplate) TestRecord(com.linkedin.restli.client.test.TestRecord)

Aggregations

RecordTemplate (com.linkedin.data.template.RecordTemplate)59 DataMap (com.linkedin.data.DataMap)23 Test (org.testng.annotations.Test)23 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)10 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)9 DataList (com.linkedin.data.DataList)8 DynamicRecordTemplate (com.linkedin.data.template.DynamicRecordTemplate)8 ArrayList (java.util.ArrayList)8 PathSpec (com.linkedin.data.schema.PathSpec)7 TestRecord (com.linkedin.restli.client.test.TestRecord)7 AnyRecord (com.linkedin.restli.internal.server.methods.AnyRecord)7 FilterResponseContext (com.linkedin.restli.server.filter.FilterResponseContext)7 BeforeTest (org.testng.annotations.BeforeTest)7 ByteString (com.linkedin.data.ByteString)6 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)6 EmptyRecord (com.linkedin.restli.common.EmptyRecord)6 RequestExecutionReport (com.linkedin.restli.server.RequestExecutionReport)6 RequestExecutionReportBuilder (com.linkedin.restli.server.RequestExecutionReportBuilder)6 RestLiResponseAttachments (com.linkedin.restli.server.RestLiResponseAttachments)6 RoutingException (com.linkedin.restli.server.RoutingException)6