use of com.linkedin.restli.common.EntityResponse in project rest.li by linkedin.
the class TestParseqBasedFluentClientApi method testSubResource_noPathKey.
/**
* Test {@link com.linkedin.restli.examples.greetings.server.CollectionUnderSimpleResource}
* A complete set of request tests were tested in {@link TestSimpleResourceHierarchy}
*/
@Test
public void testSubResource_noPathKey() throws Exception {
SubgreetingsFluentClient subgreetingsFluentClient = new SubgreetingsFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
CompletableFuture<Greeting> future = subgreetingsFluentClient.get(1L).toCompletableFuture();
Greeting greeting = future.get(5000, TimeUnit.MILLISECONDS);
Assert.assertTrue(greeting.hasId());
Assert.assertEquals((Long) 1L, greeting.getId());
List<Long> ids = Arrays.asList(1L, 2L, 3L, 4L);
Map<Long, EntityResponse<Greeting>> response = subgreetingsFluentClient.batchGet(new HashSet<>(ids)).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
Assert.assertEquals(response.size(), ids.size());
// Update
String updatedMessage = "updated";
greeting.setMessage(updatedMessage);
CompletionStage<Void> updateStage = subgreetingsFluentClient.update(1L, greeting).thenRun(() -> {
try {
Assert.assertEquals(subgreetingsFluentClient.get(1L).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS), greeting);
} catch (Exception e) {
Assert.fail("Unexpected error");
}
});
updateStage.toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
Assert.assertFalse(updateStage.toCompletableFuture().isCompletedExceptionally());
// Partial update
Greeting update = greeting.copy();
String partialUpdateMessage = "Partial update message";
update.setMessage(partialUpdateMessage);
CompletionStage<Void> partialUpdateResult = subgreetingsFluentClient.partialUpdate(1L, PatchGenerator.diff(greeting, update));
partialUpdateResult.toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
Assert.assertEquals(subgreetingsFluentClient.get(1L).toCompletableFuture().get(500, TimeUnit.MILLISECONDS).getMessage(), partialUpdateMessage);
Assert.assertFalse(partialUpdateResult.toCompletableFuture().isCompletedExceptionally());
// create
String msg = Double.toString(Math.random());
CompletionStage<Long> result = subgreetingsFluentClient.create(getGreeting(msg));
CompletableFuture<Long> createFuture = result.toCompletableFuture();
Long createdId = createFuture.get(5000, TimeUnit.MILLISECONDS);
Assert.assertTrue(subgreetingsFluentClient.get(createdId).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS).getMessage().equals(msg));
// batch create
String msg1 = Double.toString(Math.random());
String msg2 = Double.toString(Math.random());
CompletionStage<List<CreateIdStatus<Long>>> batchCreateStage = subgreetingsFluentClient.batchCreate(Arrays.asList(getGreeting(msg1), getGreeting(msg2)));
CompletableFuture<List<CreateIdStatus<Long>>> batchCreateFuture = batchCreateStage.toCompletableFuture();
List<CreateIdStatus<Long>> createdList = batchCreateFuture.get(5000, TimeUnit.MILLISECONDS);
CompletionStage<Map<Long, EntityResponse<Greeting>>> batchGetStage = subgreetingsFluentClient.batchGet(createdList.stream().map(CreateIdStatus::getKey).collect(Collectors.toSet()));
Map<Long, EntityResponse<Greeting>> entities = batchGetStage.toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
Assert.assertEquals(entities.size(), 2);
}
use of com.linkedin.restli.common.EntityResponse in project rest.li by linkedin.
the class TestSimpleResourceHierarchy method testSubCollectionBatchCreate.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestSubBuilderDataProvider")
public void testSubCollectionBatchCreate(RootBuilderWrapper<Long, Greeting> builders) throws RemoteInvocationException {
Greeting greeting = new Greeting();
greeting.setMessage("Message1");
greeting.setTone(Tone.FRIENDLY);
Greeting greeting2 = new Greeting();
greeting.setMessage("Message2");
greeting.setTone(Tone.FRIENDLY);
ArrayList<Greeting> greetings = new ArrayList<>();
greetings.add(greeting);
greetings.add(greeting2);
// POST
List<CreateIdStatus<Long>> statuses = BatchCreateHelper.batchCreate(getClient(), builders, greetings, false);
ArrayList<Long> ids = new ArrayList<>();
for (CreateIdStatus<Long> status : statuses) {
@SuppressWarnings("deprecation") String id = status.getId();
Assert.assertEquals(status.getKey().longValue(), Long.parseLong(id));
ids.add(status.getKey());
}
// GET again to verify that the create has worked.
final RestliRequestOptions requestOptions = builders.getRequestOptions();
Request<BatchKVResponse<Long, EntityResponse<Greeting>>> request = new SubgreetingsRequestBuilders(requestOptions).batchGet().ids(ids).build();
Response<BatchKVResponse<Long, EntityResponse<Greeting>>> response = getClient().sendRequest(request).getResponse();
BatchKVResponse<Long, EntityResponse<Greeting>> batchResponse = response.getEntity();
Assert.assertEquals(batchResponse.getResults().size(), ids.size());
}
use of com.linkedin.restli.common.EntityResponse in project rest.li by linkedin.
the class TestTyperefKeysResource method testBatchGetEntity.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider")
public void testBatchGetEntity(RestliRequestOptions requestOptions) throws RemoteInvocationException {
BatchGetEntityRequest<Long, Greeting> req = new TyperefKeysRequestBuilders(requestOptions).batchGet().ids(1L, 2L).build();
Response<BatchKVResponse<Long, EntityResponse<Greeting>>> resp = getClient().sendRequest(req).getResponse();
Map<Long, EntityResponse<Greeting>> results = resp.getEntity().getResults();
Assert.assertEquals(results.get(1L).getEntity().getId(), Long.valueOf(1L));
Assert.assertEquals(results.get(2L).getEntity().getId(), Long.valueOf(2L));
}
use of com.linkedin.restli.common.EntityResponse in project rest.li by linkedin.
the class TestDefaultScatterGatherStrategy method createBatchEntityResponse.
private static Response<BatchKVResponse<Long, EntityResponse<TestRecord>>> createBatchEntityResponse(ProtocolVersion version, Set<Long> resultKeys, Set<Long> errorKeys) {
DataMap resultMap = new DataMap();
for (Long id : resultKeys) {
resultMap.put(id.toString(), new EntityResponse<>(TestRecord.class).setEntity(new TestRecord().setId(id)).setStatus(HttpStatus.S_200_OK).data());
}
DataMap errorMap = new DataMap();
for (Long id : errorKeys) {
errorMap.put(id.toString(), new ErrorResponse().setStatus(HttpStatus.S_404_NOT_FOUND.getCode()).data());
}
DataMap responseMap = new DataMap();
responseMap.put(BatchResponse.RESULTS, resultMap);
responseMap.put(BatchResponse.ERRORS, errorMap);
BatchEntityResponse<Long, TestRecord> response = new BatchEntityResponse<>(responseMap, new TypeSpec<>(Long.class), new TypeSpec<>(TestRecord.class), Collections.emptyMap(), null, version);
return new ResponseImpl<>(HttpStatus.S_200_OK.getCode(), Collections.emptyMap(), Collections.emptyList(), response, null);
}
use of com.linkedin.restli.common.EntityResponse 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<>();
recordTemplates.put(c1, buildGreeting(1L));
recordTemplates.put(c2, buildGreeting(2L));
Map<CompoundKey, ErrorResponse> errorResponses = new HashMap<>();
errorResponses.put(c3, new ErrorResponse().setMessage("3"));
Map<CompoundKey, HttpStatus> statuses = new HashMap<>();
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<>();
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<>();
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 } };
}
Aggregations