use of com.linkedin.restli.common.CompoundKey in project rest.li by linkedin.
the class TestClientBuilders method testGetCompoundKeyRequestBuilder.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "compoundKey")
public void testGetCompoundKeyRequestBuilder(URIDetails expectedURIDetails) {
GetRequestBuilder<CompoundKey, TestRecord> builder = new GetRequestBuilder<CompoundKey, TestRecord>(TEST_URI, TestRecord.class, _ASSOC_SPEC, RestliRequestOptions.DEFAULT_OPTIONS);
CompoundKey key = buildCompoundKey();
GetRequest<TestRecord> request = builder.id(key).build();
testBaseUriGeneration(request, expectedURIDetails.getProtocolVersion());
Assert.assertEquals(request.isSafe(), true);
Assert.assertEquals(request.isIdempotent(), true);
checkBasicRequest(request, expectedURIDetails, ResourceMethod.GET, null, Collections.<String, String>emptyMap(), null);
}
use of com.linkedin.restli.common.CompoundKey in project rest.li by linkedin.
the class TestClientBuilders method testBatchGetCompoundKeyRequestBuilder.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "batchGetWithEncoding")
public void testBatchGetCompoundKeyRequestBuilder(URIDetails expectedURIDetails) {
BatchGetRequestBuilder<CompoundKey, TestRecord> builder = new BatchGetRequestBuilder<CompoundKey, TestRecord>(TEST_URI, TestRecord.class, _ASSOC_SPEC, RestliRequestOptions.DEFAULT_OPTIONS);
CompoundKey key1 = new CompoundKey();
key1.append("equals", "=");
key1.append("ampersand", "&");
CompoundKey key2 = new CompoundKey();
key2.append("equals", "==");
key2.append("ampersand", "&&");
BatchGetKVRequest<CompoundKey, TestRecord> request = builder.ids(key1, key2).fields(TestRecord.fields().id(), TestRecord.fields().message()).buildKV();
testBaseUriGeneration(request, expectedURIDetails.getProtocolVersion());
// Compare key sets. Note that have to convert keys to Strings as the request internally converts them to string
HashSet<CompoundKey> expectedIds = new HashSet<CompoundKey>(Arrays.asList(key1, key2));
Assert.assertEquals(request.getObjectIds(), expectedIds);
Assert.assertEquals(request.getFields(), new HashSet<PathSpec>(Arrays.asList(TestRecord.fields().id(), TestRecord.fields().message())));
Assert.assertEquals(request.isSafe(), true);
Assert.assertEquals(request.isIdempotent(), true);
checkBasicRequest(request, expectedURIDetails, ResourceMethod.BATCH_GET, null, Collections.<String, String>emptyMap(), null);
}
use of com.linkedin.restli.common.CompoundKey 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.restli.common.CompoundKey in project rest.li by linkedin.
the class TestClientBuilders method testPartialUpdateCompoundKeyRequestBuilder.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "compoundKey")
public void testPartialUpdateCompoundKeyRequestBuilder(URIDetails expectedURIDetails) throws CloneNotSupportedException {
PartialUpdateRequestBuilder<CompoundKey, TestRecord> builder = new PartialUpdateRequestBuilder<CompoundKey, TestRecord>(TEST_URI, TestRecord.class, _ASSOC_SPEC, RestliRequestOptions.DEFAULT_OPTIONS);
TestRecord t1 = new TestRecord();
TestRecord t2 = new TestRecord(t1.data().copy());
t2.setId(1L);
t2.setMessage("Foo Bar Baz");
PatchRequest<TestRecord> patch = PatchGenerator.diff(t1, t2);
PartialUpdateRequest<TestRecord> request = builder.id(buildCompoundKey()).input(patch).build();
Assert.assertEquals(request.isSafe(), false);
Assert.assertEquals(request.isIdempotent(), false);
testBaseUriGeneration(request, expectedURIDetails.getProtocolVersion());
checkBasicRequest(request, expectedURIDetails, ResourceMethod.PARTIAL_UPDATE, patch, Collections.<String, String>emptyMap(), null);
}
use of com.linkedin.restli.common.CompoundKey in project rest.li by linkedin.
the class TestClientBuilders method testBatchUpdateCompoundKeyRequestBuilder.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "batchCompoundKey")
public void testBatchUpdateCompoundKeyRequestBuilder(URIDetails expectedURIDetails) {
BatchUpdateRequestBuilder<CompoundKey, TestRecord> builder = new BatchUpdateRequestBuilder<CompoundKey, TestRecord>(TEST_URI, TestRecord.class, _ASSOC_SPEC, RestliRequestOptions.DEFAULT_OPTIONS);
Map<CompoundKey, TestRecord> inputs = new HashMap<CompoundKey, TestRecord>();
CompoundKey key1 = new CompoundKey().append("part1", 1L).append("part2", "2");
CompoundKey key2 = new CompoundKey().append("part1", 11L).append("part2", "22");
TestRecord t1 = new TestRecord().setId(1L).setMessage("1");
TestRecord t2 = new TestRecord().setId(2L);
inputs.put(key1, t1);
inputs.put(key2, t2);
BatchRequest<TestRecord> expectedRequest = new BatchRequest<TestRecord>(new DataMap(), TestRecord.class);
expectedRequest.getEntities().put(toEntityKey(key1, expectedURIDetails.getProtocolVersion()), t1);
expectedRequest.getEntities().put(toEntityKey(key2, expectedURIDetails.getProtocolVersion()), t2);
BatchUpdateRequest<CompoundKey, TestRecord> request = builder.inputs(inputs).build();
testBaseUriGeneration(request, expectedURIDetails.getProtocolVersion());
Assert.assertEquals(request.isSafe(), false);
Assert.assertEquals(request.isIdempotent(), true);
KeyValueRecordFactory<CompoundKey, TestRecord> factory = new KeyValueRecordFactory<CompoundKey, TestRecord>(CompoundKey.class, null, null, getCompoundKeyFieldTypes(), TestRecord.class);
@SuppressWarnings({ "unchecked", "rawtypes" }) CollectionRequest<KeyValueRecord> collectionRequest = buildCollectionRequest(factory, new CompoundKey[] { key1, key2 }, new TestRecord[] { t1, t2 });
checkBasicRequest(request, expectedURIDetails, ResourceMethod.BATCH_UPDATE, collectionRequest, expectedRequest, Collections.<String, String>emptyMap(), null);
}
Aggregations