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