use of com.linkedin.data.template.FieldDef in project rest.li by linkedin.
the class TestClientBuilders method testBuilderPathKeys1.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "subSubResourceAction1")
public void testBuilderPathKeys1(URIDetails expectedURIDetails) {
List<FieldDef<?>> fieldDefs = new ArrayList<FieldDef<?>>();
fieldDefs.add(new FieldDef<Integer>("key1", Integer.class, DataTemplateUtil.getSchema(Integer.class)));
fieldDefs.add(new FieldDef<Integer>("key2", Integer.class, DataTemplateUtil.getSchema(Integer.class)));
DynamicRecordMetadata requestMetadata = new DynamicRecordMetadata("action", fieldDefs);
Map<String, DynamicRecordMetadata> requestMetadataMap = new HashMap<String, DynamicRecordMetadata>();
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 = getResourceSpecForBuilderPathKeys();
Map<String, Integer> expectedPathKeys = new HashMap<String, Integer>();
expectedPathKeys.put("key1", 1);
expectedPathKeys.put("key2", 2);
Request<TestRecord> request = new ActionRequestBuilder<Void, TestRecord>(SUBRESOURCE_URI, TestRecord.class, resourceSpec, RestliRequestOptions.DEFAULT_OPTIONS).name("action").pathKey("key1", 1).pathKey("key2", 2).build();
URIDetails.testUriGeneration(request, expectedURIDetails);
testPathKeys(request, SUBRESOURCE_URI, expectedPathKeys);
}
use of com.linkedin.data.template.FieldDef in project rest.li by linkedin.
the class TestClientBuilders method testActionRequestNullOptionalParams.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "action")
public void testActionRequestNullOptionalParams(URIDetails expectedURIDetails) {
FieldDef<String> pParam = new FieldDef<String>("p", String.class, DataTemplateUtil.getSchema(String.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, Long.class, TestRecord.class, Collections.<String, CompoundKey.TypeInfo>emptyMap());
ActionRequestBuilder<Long, TestRecord> builder = new ActionRequestBuilder<Long, TestRecord>(TEST_URI, TestRecord.class, resourceSpec, RestliRequestOptions.DEFAULT_OPTIONS);
pParam.getField().setOptional(true);
ActionRequest<TestRecord> requestNullOptionalValue = builder.name("action").setParam(pParam, null).id(2L).build();
Assert.assertEquals(requestNullOptionalValue.getMethod(), ResourceMethod.ACTION);
Assert.assertEquals(requestNullOptionalValue.getHeaders(), Collections.<String, String>emptyMap());
Assert.assertEquals(requestNullOptionalValue.isSafe(), false);
Assert.assertEquals(requestNullOptionalValue.isIdempotent(), false);
Assert.assertEquals(requestNullOptionalValue.getResponseDecoder().getEntityClass(), Void.class);
}
use of com.linkedin.data.template.FieldDef in project rest.li by linkedin.
the class TestClientBuilders method testActionRequestBuilder.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "action")
public void testActionRequestBuilder(URIDetails expectedURIDetails) {
FieldDef<String> pParam = new FieldDef<String>("p", String.class, DataTemplateUtil.getSchema(String.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, Long.class, TestRecord.class, Collections.<String, CompoundKey.TypeInfo>emptyMap());
ActionRequestBuilder<Long, TestRecord> builder = new ActionRequestBuilder<Long, TestRecord>(TEST_URI, TestRecord.class, resourceSpec, RestliRequestOptions.DEFAULT_OPTIONS);
ActionRequest<TestRecord> request = builder.name("action").setParam(pParam, "42").id(1L).appendSingleAttachment(_dataSourceWriterA).appendMultipleAttachments(_dataSourceIterator).appendSingleAttachment(_dataSourceWriterB).build();
DataMap d = new DataMap();
d.put("p", "42");
@SuppressWarnings("unchecked") DynamicRecordTemplate expectedRecordTemplate = new DynamicRecordTemplate(d, DynamicRecordMetadata.buildSchema("action", Arrays.asList(new FieldDef<String>("p", String.class, DataTemplateUtil.getSchema(String.class)))));
URIDetails.testUriGeneration(request, expectedURIDetails);
Assert.assertEquals(request.getMethod(), ResourceMethod.ACTION);
Assert.assertEquals(request.getHeaders(), Collections.<String, String>emptyMap());
Assert.assertEquals(request.getInputRecord(), expectedRecordTemplate);
Assert.assertEquals(request.isSafe(), false);
Assert.assertEquals(request.isIdempotent(), false);
Assert.assertEquals(request.getStreamingAttachments(), _streamingDataSources);
try {
request.getStreamingAttachments().add(new RestLiTestAttachmentDataSource("1", ByteString.empty()));
Assert.fail("Should not be able to add to an immutable list");
} catch (Exception e) {
Assert.assertTrue(e instanceof UnsupportedOperationException);
}
Assert.assertEquals(request.getResponseDecoder().getEntityClass(), Void.class);
}
use of com.linkedin.data.template.FieldDef in project rest.li by linkedin.
the class MockActionResponseFactory method create.
/**
* Create an {@link ActionResponse} with specified value class and data.
*
* @param clazz value class of the response
* @param schema schema of the response
* @param value value data of the response
* @param <T> type of the value class
* @return mocked {@link ActionResponse}
*/
public static <T> ActionResponse<T> create(Class<T> clazz, DataSchema schema, T value) {
final FieldDef<T> fieldDef = new FieldDef<T>(ActionResponse.VALUE_NAME, clazz, schema);
final RecordDataSchema entitySchema = DynamicRecordMetadata.buildSchema(ActionResponse.class.getName(), Collections.<FieldDef<?>>singletonList(fieldDef));
return new ActionResponse<T>(value, fieldDef, entitySchema);
}
use of com.linkedin.data.template.FieldDef in project rest.li by linkedin.
the class TestExamplesGenerator method validateActionResponse.
private <T extends RecordTemplate> ValidationResult validateActionResponse(RestResponse response, Class<T> recordClass, ValidationOptions options) throws IOException {
final DataMap respData = _codec.bytesToMap(response.getEntity().copyBytes());
final FieldDef<T> responseFieldDef = new FieldDef<T>(ActionResponse.VALUE_NAME, recordClass, DataTemplateUtil.getSchema(recordClass));
final RecordDataSchema recordDataSchema = DynamicRecordMetadata.buildSchema(ActionResponse.class.getName(), Collections.<FieldDef<?>>singletonList(responseFieldDef));
final ActionResponse<T> actionResp = new ActionResponse<T>(respData, responseFieldDef, recordDataSchema);
final DataSchema recordSchema = DataTemplateUtil.getSchema(recordClass);
return ValidateDataAgainstSchema.validate(actionResp.getValue().data(), recordSchema, options);
}
Aggregations