use of com.linkedin.restli.common.CompoundKey in project rest.li by linkedin.
the class TestMockBatchKVResponseFactory method compoundKeyData.
@DataProvider(name = "compoundKey")
public Object[][] compoundKeyData() {
CompoundKey c1 = buildCompoundKey("c1", 1);
CompoundKey c2 = buildCompoundKey("c2", 2);
CompoundKey c3 = buildCompoundKey("c3", 3);
Map<CompoundKey, Greeting> recordTemplates = new HashMap<CompoundKey, Greeting>();
recordTemplates.put(c1, buildGreeting(1L));
recordTemplates.put(c2, buildGreeting(2L));
Map<CompoundKey, ErrorResponse> errorResponses = new HashMap<CompoundKey, ErrorResponse>();
errorResponses.put(c3, new ErrorResponse().setMessage("3"));
Map<CompoundKey, HttpStatus> statuses = new HashMap<CompoundKey, HttpStatus>();
statuses.put(c1, HttpStatus.S_200_OK);
statuses.put(c2, HttpStatus.S_200_OK);
statuses.put(c3, HttpStatus.S_500_INTERNAL_SERVER_ERROR);
Map<String, CompoundKey.TypeInfo> keyParts = new HashMap<String, CompoundKey.TypeInfo>();
keyParts.put("part1", new CompoundKey.TypeInfo(String.class, String.class));
keyParts.put("part2", new CompoundKey.TypeInfo(Integer.class, Integer.class));
Map<CompoundKey, EntityResponse<Greeting>> expectedResults = new HashMap<CompoundKey, EntityResponse<Greeting>>();
expectedResults.put(c1, buildEntityResponse(recordTemplates.get(c1), HttpStatus.S_200_OK, null));
expectedResults.put(c2, buildEntityResponse(recordTemplates.get(c2), HttpStatus.S_200_OK, null));
expectedResults.put(c3, buildEntityResponse(null, HttpStatus.S_500_INTERNAL_SERVER_ERROR, errorResponses.get(c3)));
return new Object[][] { { keyParts, recordTemplates, statuses, errorResponses, expectedResults } };
}
use of com.linkedin.restli.common.CompoundKey in project rest.li by linkedin.
the class TestClientBuilders method testBatchPartialUpdateCompoundKeyRequestBuilder.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "batchCompoundKey")
public void testBatchPartialUpdateCompoundKeyRequestBuilder(URIDetails expectedURIDetails) throws CloneNotSupportedException {
BatchPartialUpdateRequestBuilder<CompoundKey, TestRecord> builder = new BatchPartialUpdateRequestBuilder<CompoundKey, TestRecord>(TEST_URI, TestRecord.class, _ASSOC_SPEC, RestliRequestOptions.DEFAULT_OPTIONS);
Map<CompoundKey, PatchRequest<TestRecord>> inputs = new HashMap<CompoundKey, PatchRequest<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);
TestRecord t3 = new TestRecord().setMessage("3");
PatchRequest<TestRecord> patch1 = PatchGenerator.diff(t1, t2);
PatchRequest<TestRecord> patch2 = PatchGenerator.diff(t2, t3);
inputs.put(key1, patch1);
inputs.put(key2, patch2);
BatchPartialUpdateRequest<CompoundKey, TestRecord> request = builder.inputs(inputs).build();
testBaseUriGeneration(request, expectedURIDetails.getProtocolVersion());
Assert.assertEquals(request.isSafe(), false);
Assert.assertEquals(request.isIdempotent(), false);
@SuppressWarnings({ "unchecked", "rawtypes" }) BatchRequest<PatchRequest<TestRecord>> expectedRequest = new BatchRequest(new DataMap(), PatchRequest.class);
expectedRequest.getEntities().put(toEntityKey(key1, expectedURIDetails.getProtocolVersion()), patch1);
expectedRequest.getEntities().put(toEntityKey(key2, expectedURIDetails.getProtocolVersion()), patch2);
@SuppressWarnings({ "unchecked", "rawtypes" }) KeyValueRecordFactory<CompoundKey, PatchRequest> factory = new KeyValueRecordFactory<CompoundKey, PatchRequest>(CompoundKey.class, null, null, getCompoundKeyFieldTypes(), PatchRequest.class);
@SuppressWarnings({ "unchecked", "rawtypes" }) CollectionRequest<KeyValueRecord> collectionRequest = buildCollectionRequest(factory, new CompoundKey[] { key1, key2 }, new PatchRequest[] { patch1, patch2 });
checkBasicRequest(request, expectedURIDetails, ResourceMethod.BATCH_PARTIAL_UPDATE, collectionRequest, expectedRequest, Collections.<String, String>emptyMap(), null);
}
use of com.linkedin.restli.common.CompoundKey in project rest.li by linkedin.
the class TestCollectionRequestUtil method testCompoundKeyMultipleEntities.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "versions")
public void testCompoundKeyMultipleEntities(ProtocolVersion version) {
String key1 = "key1";
String key2 = "key2";
CompoundKey c1 = new CompoundKey().append(key1, 1L).append(key2, 2L);
CompoundKey c2 = new CompoundKey().append(key1, 3L).append(key2, 4L);
CompoundKey[] keys = { c1, c2 };
Map<String, CompoundKey.TypeInfo> fieldTypes = new HashMap<String, CompoundKey.TypeInfo>();
fieldTypes.put(key1, new CompoundKey.TypeInfo(Long.class, Long.class));
fieldTypes.put(key2, new CompoundKey.TypeInfo(Long.class, Long.class));
@SuppressWarnings("rawtypes") KeyValueRecordFactory<CompoundKey, TestRecord> factory = new KeyValueRecordFactory<CompoundKey, TestRecord>(CompoundKey.class, null, null, fieldTypes, TestRecord.class);
@SuppressWarnings("rawtypes") CollectionRequest<KeyValueRecord> collectionRequest = new CollectionRequest<KeyValueRecord>(KeyValueRecord.class);
Map<CompoundKey, TestRecord> inputs = new HashMap<CompoundKey, TestRecord>();
for (CompoundKey key : keys) {
TestRecord testRecord = buildTestRecord(1L, "message" + key.hashCode());
inputs.put(key, testRecord);
collectionRequest.getElements().add(factory.create(key, testRecord));
}
@SuppressWarnings("unchecked") BatchRequest<TestRecord> batchRequest = CollectionRequestUtil.convertToBatchRequest(collectionRequest, CompoundKey.class, null, null, fieldTypes, TestRecord.class, version);
Map<String, TestRecord> entities = batchRequest.getEntities();
Assert.assertEquals(entities.size(), inputs.size());
for (CompoundKey key : keys) {
Assert.assertEquals(entities.get(BatchResponse.keyToString(key, version)), inputs.get(key));
}
}
use of com.linkedin.restli.common.CompoundKey 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.CompoundKey in project rest.li by linkedin.
the class ArgumentUtils method dataMapToCompoundKey.
public static CompoundKey dataMapToCompoundKey(DataMap dataMap, Collection<Key> keys) throws IllegalArgumentException {
CompoundKey compoundKey = new CompoundKey();
for (Key key : keys) {
String name = key.getName();
// may be a partial compound key
String value = dataMap.getString(name);
if (value != null) {
dataMap.remove(name);
compoundKey.append(name, convertSimpleValue(value, key.getDataSchema(), key.getType()));
}
}
if (!dataMap.isEmpty()) {
StringBuilder errorMessageBuilder = new StringBuilder();
for (String leftOverKey : dataMap.keySet()) {
errorMessageBuilder.append("Unknown key part named '");
errorMessageBuilder.append(leftOverKey);
errorMessageBuilder.append("'");
}
throw new IllegalArgumentException(errorMessageBuilder.toString());
}
return compoundKey;
}
Aggregations