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();
}
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);
}
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");
}
}
});
}
}
}
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);
}
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());
}
}
Aggregations