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