use of com.linkedin.data.template.DynamicRecordMetadata in project rest.li by linkedin.
the class BatchGetRequestBuilderTest method testSimpleBatching.
@Test
public void testSimpleBatching() throws URISyntaxException {
String expectedProtocol1Uri = "/?fields=id,message&ids=1&ids=2&ids=3¶m1=value1¶m2=value2";
String expectedProtocol2Uri = "/?fields=id,message&ids=List(1,2,3)¶m1=value1¶m2=value2";
BatchGetRequestBuilder<Integer, TestRecord> batchRequestBuilder1 = new BatchGetRequestBuilder<>("/", TestRecord.class, new ResourceSpecImpl(Collections.<ResourceMethod>emptySet(), Collections.<String, DynamicRecordMetadata>emptyMap(), Collections.<String, DynamicRecordMetadata>emptyMap(), Integer.class, null, null, null, Collections.<String, Object>emptyMap()), RestliRequestOptions.DEFAULT_OPTIONS);
batchRequestBuilder1.ids(1, 2).fields(FIELDS.id()).setParam("param2", "value2").setParam("param1", "value1");
BatchGetRequestBuilder<Integer, TestRecord> batchRequestBuilder2 = new BatchGetRequestBuilder<>("/", TestRecord.class, new ResourceSpecImpl(Collections.<ResourceMethod>emptySet(), Collections.<String, DynamicRecordMetadata>emptyMap(), Collections.<String, DynamicRecordMetadata>emptyMap(), Integer.class, null, null, null, Collections.<String, Object>emptyMap()), RestliRequestOptions.DEFAULT_OPTIONS);
batchRequestBuilder2.ids(2, 3).fields(FIELDS.id(), FIELDS.message()).setParam("param1", "value1").setParam("param2", "value2");
BatchGetRequest<TestRecord> batchRequest1 = batchRequestBuilder1.build();
BatchGetRequest<TestRecord> batchRequest2 = batchRequestBuilder2.build();
@SuppressWarnings("unchecked") BatchGetRequest<TestRecord> batchingRequest = BatchGetRequestBuilder.batch(Arrays.asList(batchRequest1, batchRequest2));
testUriGeneration(batchingRequest, expectedProtocol1Uri, expectedProtocol2Uri);
Assert.assertEquals(batchingRequest.getBaseUriTemplate(), batchRequest1.getBaseUriTemplate());
Assert.assertEquals(batchingRequest.getPathKeys(), batchRequest1.getPathKeys());
Assert.assertEquals(batchingRequest.getFields(), new HashSet<>(Arrays.asList(FIELDS.id(), FIELDS.message())));
Assert.assertEquals(batchingRequest.getObjectIds(), new HashSet<>(Arrays.asList(1, 2, 3)));
}
use of com.linkedin.data.template.DynamicRecordMetadata in project rest.li by linkedin.
the class BatchGetRequestBuilderTest method testBatchingWithNoFields.
@Test
public void testBatchingWithNoFields() throws URISyntaxException {
String expectedProtocol1Uri = "/?ids=1&ids=2&ids=3";
String expectedProtocol2Uri = "/?ids=List(1,2,3)";
BatchGetRequestBuilder<Integer, TestRecord> batchRequestBuilder1 = new BatchGetRequestBuilder<>("/", TestRecord.class, new ResourceSpecImpl(Collections.<ResourceMethod>emptySet(), Collections.<String, DynamicRecordMetadata>emptyMap(), Collections.<String, DynamicRecordMetadata>emptyMap(), Integer.class, null, null, null, Collections.<String, Object>emptyMap()), RestliRequestOptions.DEFAULT_OPTIONS);
batchRequestBuilder1.ids(1);
batchRequestBuilder1.fields();
BatchGetRequestBuilder<Integer, TestRecord> batchRequestBuilder2 = new BatchGetRequestBuilder<>("/", TestRecord.class, new ResourceSpecImpl(Collections.<ResourceMethod>emptySet(), Collections.<String, DynamicRecordMetadata>emptyMap(), Collections.<String, DynamicRecordMetadata>emptyMap(), Integer.class, null, null, null, Collections.<String, Object>emptyMap()), RestliRequestOptions.DEFAULT_OPTIONS);
batchRequestBuilder2.ids(2, 3);
batchRequestBuilder2.fields();
BatchGetRequest<TestRecord> batchRequest1 = batchRequestBuilder1.build();
@SuppressWarnings("unchecked") BatchGetRequest<TestRecord> batchingRequest = BatchGetRequestBuilder.batch(Arrays.asList(batchRequest1, batchRequestBuilder2.build()));
Assert.assertEquals(batchingRequest.getBaseUriTemplate(), batchRequest1.getBaseUriTemplate());
Assert.assertEquals(batchingRequest.getPathKeys(), batchRequest1.getPathKeys());
Assert.assertEquals(batchingRequest.getFields(), Collections.emptySet());
Assert.assertEquals(batchingRequest.getObjectIds(), new HashSet<>(Arrays.asList(1, 2, 3)));
testUriGeneration(batchingRequest, expectedProtocol1Uri, expectedProtocol2Uri);
}
use of com.linkedin.data.template.DynamicRecordMetadata in project rest.li by linkedin.
the class BatchGetRequestBuilderTest method testNoFieldBatchingFailure.
@Test
public void testNoFieldBatchingFailure() {
BatchGetRequestBuilder<Integer, TestRecord> batchRequestBuilder1 = new BatchGetRequestBuilder<>("/", TestRecord.class, new ResourceSpecImpl(Collections.<ResourceMethod>emptySet(), Collections.<String, DynamicRecordMetadata>emptyMap(), Collections.<String, DynamicRecordMetadata>emptyMap(), Integer.class, null, null, null, Collections.<String, Object>emptyMap()), RestliRequestOptions.DEFAULT_OPTIONS);
batchRequestBuilder1.ids(1);
batchRequestBuilder1.fields(FIELDS.id());
BatchGetRequestBuilder<Integer, TestRecord> batchRequestBuilder2 = new BatchGetRequestBuilder<>("/", TestRecord.class, new ResourceSpecImpl(Collections.<ResourceMethod>emptySet(), Collections.<String, DynamicRecordMetadata>emptyMap(), Collections.<String, DynamicRecordMetadata>emptyMap(), Integer.class, null, null, null, Collections.<String, Object>emptyMap()), RestliRequestOptions.DEFAULT_OPTIONS);
batchRequestBuilder2.ids(2, 3);
batchRequestBuilder2.fields(FIELDS.message());
try {
@SuppressWarnings("unchecked") List<BatchGetRequest<TestRecord>> requests = Arrays.asList(batchRequestBuilder1.build(), batchRequestBuilder2.build());
BatchGetRequestBuilder.batch(requests, false);
Assert.fail("expected IllegalArgumentException");
} catch (IllegalArgumentException ignored) {
// Expected
}
}
use of com.linkedin.data.template.DynamicRecordMetadata in project rest.li by linkedin.
the class ExampleRequestResponseGenerator method action.
public ExampleRequestResponse action(String name, ResourceLevel resourceLevel) {
ActionSchema actionSchema;
switch(resourceLevel) {
case COLLECTION:
actionSchema = _resourceSchema.getAction(name);
break;
case ENTITY:
actionSchema = _resourceSchema.getEntityAction(name);
break;
default:
throw new IllegalArgumentException("Unsupported resourceLevel: " + resourceLevel);
}
if (actionSchema == null) {
throw new IllegalArgumentException("No such action for resource: " + name);
}
DynamicRecordMetadata returnsMetadata = _resourceSpec.getActionResponseMetadata(name);
return buildRequestResponse(buildActionRequest(actionSchema, resourceLevel), buildActionResult(actionSchema), buildResourceMethodDescriptorForAction(name, returnsMetadata, resourceLevel));
}
use of com.linkedin.data.template.DynamicRecordMetadata in project rest.li by linkedin.
the class ExampleRequestResponseGenerator method buildActionRequest.
@SuppressWarnings("unchecked")
private ActionRequest<?> buildActionRequest(ActionSchema action, ResourceLevel resourceLevel) {
DynamicRecordMetadata requestParamsMetadata = _resourceSpec.getRequestMetadata(action.getName());
DynamicRecordMetadata responseMetadata = _resourceSpec.getActionResponseMetadata(action.getName());
TypeSpec<RecordTemplatePlaceholder> responseType = null;
if (responseMetadata != null) {
FieldDef<?> fieldDef = responseMetadata.getFieldDef("value");
if (fieldDef != null && fieldDef.getDataClass() != null) {
responseType = new TypeSpec<>((Class<RecordTemplatePlaceholder>) fieldDef.getDataClass(), responseMetadata.getRecordDataSchema());
}
}
ActionRequestBuilder<Object, RecordTemplatePlaceholder> request = new ActionRequestBuilder<>(_uriTemplate, responseType, _resourceSpec, _requestOptions);
request.name(action.getName());
if (resourceLevel == ResourceLevel.ENTITY && !_resourceSpec.isKeylessResource()) {
request.id(generateKey());
}
if (requestParamsMetadata != null) {
addParams(request, requestParamsMetadata, action.getParameters());
}
addPathKeys(request);
return request.build();
}
Aggregations