use of com.linkedin.restli.common.ComplexResourceKey in project rest.li by linkedin.
the class TestMockBatchCreateIdResponseFactory method provideKeys.
@DataProvider
public Object[][] provideKeys() {
Greeting g1 = buildGreeting(1L);
Greeting g2 = buildGreeting(2L);
Greeting g3 = buildGreeting(3L);
return new Object[][] { new Object[] { new Long[] { 1L, 2L, 3L } }, new Object[] { new MyCustomString[] { new MyCustomString("1"), new MyCustomString("2"), new MyCustomString("3") } }, new Object[] { new CompoundKey[] { buildCompoundKey("c1", 1), buildCompoundKey("c2", 2), buildCompoundKey("c3", 3) } }, new Object[] { new ComplexResourceKey<?, ?>[] { new ComplexResourceKey<Greeting, Greeting>(g1, g1), new ComplexResourceKey<Greeting, Greeting>(g2, g2), new ComplexResourceKey<Greeting, Greeting>(g3, g3) } } };
}
use of com.linkedin.restli.common.ComplexResourceKey in project rest.li by linkedin.
the class TestMockBatchKVResponseFactory method complexKeyData.
@DataProvider(name = "complexKey")
public Object[][] complexKeyData() {
Map<ComplexResourceKey<Greeting, Greeting>, Greeting> recordTemplates = new HashMap<ComplexResourceKey<Greeting, Greeting>, Greeting>();
Map<ComplexResourceKey<Greeting, Greeting>, ErrorResponse> errorResponses = new HashMap<ComplexResourceKey<Greeting, Greeting>, ErrorResponse>();
Greeting g1 = buildGreeting(1L);
Greeting g2 = buildGreeting(2L);
Greeting g3 = buildGreeting(3L);
recordTemplates.put(new ComplexResourceKey<Greeting, Greeting>(g1, g1), g1);
recordTemplates.put(new ComplexResourceKey<Greeting, Greeting>(g2, g2), g2);
errorResponses.put(new ComplexResourceKey<Greeting, Greeting>(g3, g3), new ErrorResponse().setMessage("3"));
Map<ComplexResourceKey<Greeting, Greeting>, HttpStatus> statuses = new HashMap<ComplexResourceKey<Greeting, Greeting>, HttpStatus>();
statuses.put(new ComplexResourceKey<Greeting, Greeting>(g1, g1), HttpStatus.S_200_OK);
statuses.put(new ComplexResourceKey<Greeting, Greeting>(g2, g2), HttpStatus.S_200_OK);
statuses.put(new ComplexResourceKey<Greeting, Greeting>(g3, g3), HttpStatus.S_500_INTERNAL_SERVER_ERROR);
// Strip the parameters from complex keys in expected results and expected errors.
Map<ComplexResourceKey<Greeting, Greeting>, Greeting> expectedRecordTemplates = new HashMap<ComplexResourceKey<Greeting, Greeting>, Greeting>();
expectedRecordTemplates.put(new ComplexResourceKey<Greeting, Greeting>(g1, new Greeting()), recordTemplates.get(new ComplexResourceKey<Greeting, Greeting>(g1, g1)));
expectedRecordTemplates.put(new ComplexResourceKey<Greeting, Greeting>(g2, new Greeting()), recordTemplates.get(new ComplexResourceKey<Greeting, Greeting>(g2, g2)));
Map<ComplexResourceKey<Greeting, Greeting>, EntityResponse<Greeting>> expectedResults = new HashMap<ComplexResourceKey<Greeting, Greeting>, EntityResponse<Greeting>>();
expectedResults.put(new ComplexResourceKey<Greeting, Greeting>(g1, new Greeting()), buildEntityResponse(recordTemplates.get(new ComplexResourceKey<Greeting, Greeting>(g1, g1)), HttpStatus.S_200_OK, null));
expectedResults.put(new ComplexResourceKey<Greeting, Greeting>(g2, new Greeting()), buildEntityResponse(recordTemplates.get(new ComplexResourceKey<Greeting, Greeting>(g2, g2)), HttpStatus.S_200_OK, null));
expectedResults.put(new ComplexResourceKey<Greeting, Greeting>(g3, new Greeting()), buildEntityResponse(null, HttpStatus.S_500_INTERNAL_SERVER_ERROR, errorResponses.get(new ComplexResourceKey<Greeting, Greeting>(g3, g3))));
Map<ComplexResourceKey<Greeting, Greeting>, ErrorResponse> expectedErrors = new HashMap<ComplexResourceKey<Greeting, Greeting>, ErrorResponse>();
expectedErrors.put(new ComplexResourceKey<Greeting, Greeting>(g3, new Greeting()), errorResponses.get(new ComplexResourceKey<Greeting, Greeting>(g3, g3)));
return new Object[][] { { recordTemplates, statuses, errorResponses, expectedRecordTemplates, expectedResults, expectedErrors } };
}
use of com.linkedin.restli.common.ComplexResourceKey in project rest.li by linkedin.
the class QueryParamsDataMap method fixUpComplexKeySingletonArray.
/**
* Because of backwards compatibility concerns, array fields of the key component of a
* {@link ComplexResourceKey}s in a get request will be represented in the request url in the old
* style. That is, if an array field has the name "a", and contains [1,2] the part of the url
* representing the serialized array will look like "a=1&a=2". However, if the array is a
* singleton it will just be represented by "a=1". Therefore it is not possible to distinguish
* between a single value itself and an array containing a single value.
*
* The purpose of this function is to fix up the singleton array problem by checking to see whether the given
* ComplexKey's key part has an array component, and, if so and the data for that field is NOT a dataList,
* placing the data into a dataList.
*
* @param complexResourceKey The complex key to be fixed.
*/
public static ComplexResourceKey<?, ?> fixUpComplexKeySingletonArray(ComplexResourceKey<?, ?> complexResourceKey) {
RecordTemplate key = complexResourceKey.getKey();
DataMap dataMap = key.data();
List<RecordDataSchema.Field> fields = key.schema() == null ? Collections.<RecordDataSchema.Field>emptyList() : key.schema().getFields();
for (RecordDataSchema.Field f : fields) {
DataSchema.Type type = f.getType().getType();
String fieldName = f.getName();
if (type == DataSchema.Type.ARRAY && dataMap.containsKey(fieldName)) {
Object arrayFieldValue = dataMap.get(fieldName);
if (!(arrayFieldValue instanceof DataList)) {
DataList list = new DataList();
list.add(arrayFieldValue);
ValidateDataAgainstSchema.validate(list, f.getType(), new ValidationOptions(RequiredMode.CAN_BE_ABSENT_IF_HAS_DEFAULT, CoercionMode.STRING_TO_PRIMITIVE));
dataMap.put(fieldName, list);
}
}
}
RecordTemplate wrappedKey = DataTemplateUtil.wrap(dataMap, key.getClass());
@SuppressWarnings("unchecked") ComplexResourceKey<?, ?> newKey = new ComplexResourceKey<RecordTemplate, RecordTemplate>(wrappedKey, complexResourceKey.getParams());
return newKey;
}
use of com.linkedin.restli.common.ComplexResourceKey in project rest.li by linkedin.
the class TestClientBuilders method testActionRequestInputIsReadOnly.
@Test
@SuppressWarnings("unchecked")
public void testActionRequestInputIsReadOnly() {
FieldDef<TestRecord> pParam = new FieldDef<TestRecord>("p", TestRecord.class, DataTemplateUtil.getSchema(TestRecord.class));
Map<String, DynamicRecordMetadata> requestMetadataMap = new HashMap<String, DynamicRecordMetadata>();
DynamicRecordMetadata requestMetadata = new DynamicRecordMetadata("action", Collections.<FieldDef<?>>singleton(pParam));
requestMetadataMap.put("action", requestMetadata);
DynamicRecordMetadata responseMetadata = new DynamicRecordMetadata("action", Collections.<FieldDef<?>>emptyList());
Map<String, DynamicRecordMetadata> responseMetadataMap = new HashMap<String, DynamicRecordMetadata>();
responseMetadataMap.put("action", responseMetadata);
ResourceSpec resourceSpec = new ResourceSpecImpl(Collections.<ResourceMethod>emptySet(), requestMetadataMap, responseMetadataMap, ComplexResourceKey.class, TestRecord.class, TestRecord.class, TestRecord.class, Collections.<String, CompoundKey.TypeInfo>emptyMap());
ActionRequestBuilder<ComplexResourceKey<TestRecord, TestRecord>, TestRecord> builder = new ActionRequestBuilder<ComplexResourceKey<TestRecord, TestRecord>, TestRecord>(TEST_URI, TestRecord.class, resourceSpec, RestliRequestOptions.DEFAULT_OPTIONS);
TestRecord testRecord1 = new TestRecord();
TestRecord testRecord2 = new TestRecord();
ComplexResourceKey<TestRecord, TestRecord> key = new ComplexResourceKey<TestRecord, TestRecord>(testRecord1, testRecord2);
ActionRequest<TestRecord> request = builder.name("action").setParam(pParam, testRecord1).id(key).build();
DynamicRecordTemplate inputParams = (DynamicRecordTemplate) request.getInputRecord();
Assert.assertNotSame(inputParams.getValue(pParam).data(), testRecord1.data());
Assert.assertTrue(inputParams.data().isReadOnly());
Assert.assertTrue(inputParams.getValue(pParam).data().isMadeReadOnly());
Assert.assertNotSame(request.getId(), key);
Assert.assertTrue(((ComplexResourceKey<TestRecord, TestRecord>) request.getId()).isReadOnly());
testRecord1.data().makeReadOnly();
testRecord2.data().makeReadOnly();
request = builder.build();
inputParams = (DynamicRecordTemplate) request.getInputRecord();
Assert.assertSame(inputParams.getValue(pParam).data(), testRecord1.data());
Assert.assertTrue(inputParams.data().isReadOnly());
Assert.assertSame(request.getId(), key);
}
use of com.linkedin.restli.common.ComplexResourceKey in project rest.li by linkedin.
the class TestClientBuilders method testComplexKeyUpdateRequestBuilder.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "complexKey")
public void testComplexKeyUpdateRequestBuilder(URIDetails expectedURIDetails) {
UpdateRequestBuilder<ComplexResourceKey<TestRecord, TestRecord>, TestRecord> builder = new UpdateRequestBuilder<ComplexResourceKey<TestRecord, TestRecord>, TestRecord>(TEST_URI, TestRecord.class, _COMPLEX_KEY_SPEC, RestliRequestOptions.DEFAULT_OPTIONS);
ComplexResourceKey<TestRecord, TestRecord> key = buildComplexKey(1L, "keyMessage", 2L, "paramMessage");
UpdateRequest<TestRecord> request = builder.id(key).input(new TestRecord()).build();
testBaseUriGeneration(request, expectedURIDetails.getProtocolVersion());
Assert.assertEquals(request.isSafe(), false);
Assert.assertEquals(request.isIdempotent(), true);
checkBasicRequest(request, expectedURIDetails, ResourceMethod.UPDATE, new TestRecord(), Collections.<String, String>emptyMap(), null);
}
Aggregations