use of com.linkedin.restli.common.CollectionResponse in project rest.li by linkedin.
the class CollectionResponseBuilder method buildResponse.
@Override
public PartialRestResponse buildResponse(RoutingResult routingResult, RestLiResponseData responseData) {
CollectionResponseEnvelope response = responseData.getCollectionResponseEnvelope();
PartialRestResponse.Builder builder = new PartialRestResponse.Builder();
CollectionResponse<AnyRecord> collectionResponse = new CollectionResponse<AnyRecord>(AnyRecord.class);
collectionResponse.setPaging(response.getCollectionResponsePaging());
DataList elementsMap = (DataList) collectionResponse.data().get(CollectionResponse.ELEMENTS);
for (RecordTemplate entry : response.getCollectionResponse()) {
CheckedUtil.addWithoutChecking(elementsMap, entry.data());
}
if (response.getCollectionResponseCustomMetadata() != null) {
collectionResponse.setMetadataRaw(response.getCollectionResponseCustomMetadata().data());
}
builder.entity(collectionResponse);
return builder.headers(responseData.getHeaders()).cookies(responseData.getCookies()).build();
}
use of com.linkedin.restli.common.CollectionResponse in project rest.li by linkedin.
the class TestCustomTypesRequestBuilders method testFinderCustomLongArray.
// test correct request is built for customLongArray
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "request1BuilderDataProviderCustomLongArrayFinder")
public void testFinderCustomLongArray(RootBuilderWrapper<Long, Greeting> builders, ProtocolVersion version, String expectedUri) throws IOException, RestException {
List<CustomLong> ls = new ArrayList<CustomLong>(2);
ls.add(new CustomLong(2L));
ls.add(new CustomLong(4L));
Request<CollectionResponse<Greeting>> request = builders.findBy("CustomLongArray").setQueryParam("ls", ls).build();
checkRequestBuilder(request, ResourceMethod.FINDER, CollectionResponseDecoder.class, expectedUri, null, version);
}
use of com.linkedin.restli.common.CollectionResponse in project rest.li by linkedin.
the class TestComplexKeysResource method testBatchCreateMain.
private void testBatchCreateMain(BatchCreateRequestBuilder<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchCreateRequestBuilder, BatchGetRequestBuilder<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchGetRequestBuilder) throws RemoteInvocationException {
final String messageText1 = "firstMessage";
Message message1 = new Message();
message1.setMessage(messageText1);
final String messageText2 = "secondMessage";
Message message2 = new Message();
message2.setMessage(messageText2);
List<Message> messages = new ArrayList<Message>(2);
messages.add(message1);
messages.add(message2);
ComplexResourceKey<TwoPartKey, TwoPartKey> expectedComplexKey1 = getComplexKey(messageText1, messageText1);
ComplexResourceKey<TwoPartKey, TwoPartKey> expectedComplexKey2 = getComplexKey(messageText2, messageText2);
// test build
Request<CollectionResponse<CreateStatus>> request = batchCreateRequestBuilder.inputs(messages).build();
ResponseFuture<CollectionResponse<CreateStatus>> future = getClient().sendRequest(request);
Response<CollectionResponse<CreateStatus>> response = future.getResponse();
Assert.assertEquals(response.getStatus(), 200);
Set<ComplexResourceKey<TwoPartKey, TwoPartKey>> expectedComplexKeys = new HashSet<ComplexResourceKey<TwoPartKey, TwoPartKey>>(2);
expectedComplexKeys.add(expectedComplexKey1);
expectedComplexKeys.add(expectedComplexKey2);
for (CreateStatus createStatus : response.getEntity().getElements()) {
@SuppressWarnings("unchecked") CreateIdStatus<ComplexResourceKey<TwoPartKey, TwoPartKey>> createIdStatus = (CreateIdStatus<ComplexResourceKey<TwoPartKey, TwoPartKey>>) createStatus;
Assert.assertEquals(createIdStatus.getStatus(), new Integer(201));
Assert.assertTrue(expectedComplexKeys.contains(createIdStatus.getKey()));
try {
@SuppressWarnings("deprecation") String id = createIdStatus.getId();
Assert.fail("buildReadOnlyId should throw an exception on ComplexKeys");
} catch (UnsupportedOperationException e) {
// expected
}
expectedComplexKeys.remove(createIdStatus.getKey());
}
Assert.assertTrue(expectedComplexKeys.isEmpty());
// attempt to batch get created records
List<ComplexResourceKey<TwoPartKey, TwoPartKey>> createdKeys = new ArrayList<ComplexResourceKey<TwoPartKey, TwoPartKey>>(2);
createdKeys.add(expectedComplexKey1);
createdKeys.add(expectedComplexKey2);
BatchGetKVRequest<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> getRequest = batchGetRequestBuilder.ids(createdKeys).buildKV();
ResponseFuture<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message>> getFuture = getClient().sendRequest(getRequest);
Response<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message>> getResponse = getFuture.getResponse();
Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> getResults = getResponse.getEntity().getResults();
Assert.assertEquals(getResults.get(expectedComplexKey1), message1);
Assert.assertEquals(getResults.get(expectedComplexKey2), message2);
Assert.assertEquals(getResults.size(), 2);
}
use of com.linkedin.restli.common.CollectionResponse in project rest.li by linkedin.
the class TestCompressionServer method testSearch.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "clientsCompressedResponsesBuilderDataProvider")
public void testSearch(RestClient client, String operationsForCompression, RootBuilderWrapper<Long, Greeting> builders, ProtocolVersion protocolVersion) throws RemoteInvocationException {
Request<CollectionResponse<Greeting>> findRequest = builders.findBy("Search").setQueryParam("tone", Tone.FRIENDLY).build();
Response<CollectionResponse<Greeting>> response = client.sendRequest(findRequest).getResponse();
checkHeaderForCompression(response, operationsForCompression, "finder:search");
List<Greeting> greetings = client.sendRequest(findRequest).getResponse().getEntity().getElements();
for (Greeting g : greetings) {
Assert.assertEquals(g.getTone(), Tone.FRIENDLY);
Assert.assertNotNull(g.getMessage());
}
}
use of com.linkedin.restli.common.CollectionResponse in project rest.li by linkedin.
the class TestCustomTypesClient method testCollectionBatchCreate.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider")
public void testCollectionBatchCreate(RestliRequestOptions options) throws RemoteInvocationException {
CustomTypes2Builders builders = new CustomTypes2Builders(options);
BatchCreateRequest<Greeting> request = builders.batchCreate().input(new Greeting().setId(1)).input(new Greeting().setId(2)).build();
Response<CollectionResponse<CreateStatus>> response = getClient().sendRequest(request).getResponse();
List<CreateStatus> results = response.getEntity().getElements();
Set<CustomLong> expectedKeys = new HashSet<CustomLong>();
expectedKeys.add(new CustomLong(1L));
expectedKeys.add(new CustomLong(2L));
for (CreateStatus status : results) {
@SuppressWarnings("unchecked") CreateIdStatus<CustomLong> createIdStatus = (CreateIdStatus<CustomLong>) status;
Assert.assertEquals(createIdStatus.getStatus().intValue(), HttpStatus.S_204_NO_CONTENT.getCode());
Assert.assertTrue(expectedKeys.contains(createIdStatus.getKey()));
@SuppressWarnings("deprecation") String id = createIdStatus.getId();
Assert.assertEquals(BatchResponse.keyToString(createIdStatus.getKey(), ProtocolVersionUtil.extractProtocolVersion(response.getHeaders())), id);
expectedKeys.remove(createIdStatus.getKey());
}
Assert.assertTrue(expectedKeys.isEmpty());
}
Aggregations