Search in sources :

Example 26 with EntityResponse

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);
}
Also used : CreateGreeting(com.linkedin.restli.examples.greetings.client.CreateGreeting) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) PartialUpdateGreeting(com.linkedin.restli.examples.greetings.client.PartialUpdateGreeting) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) ExecutionException(java.util.concurrent.ExecutionException) EntityResponse(com.linkedin.restli.common.EntityResponse) IdEntityResponse(com.linkedin.restli.common.IdEntityResponse) CustomLong(com.linkedin.restli.examples.custom.types.CustomLong) SubgreetingsFluentClient(com.linkedin.restli.examples.greetings.client.SubgreetingsFluentClient) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) StringMap(com.linkedin.data.template.StringMap) Test(org.testng.annotations.Test)

Example 27 with EntityResponse

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());
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) RestliRequestOptions(com.linkedin.restli.client.RestliRequestOptions) SubgreetingsRequestBuilders(com.linkedin.restli.examples.greetings.client.SubgreetingsRequestBuilders) ArrayList(java.util.ArrayList) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) EntityResponse(com.linkedin.restli.common.EntityResponse) Test(org.testng.annotations.Test)

Example 28 with EntityResponse

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));
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) EntityResponse(com.linkedin.restli.common.EntityResponse) TyperefKeysRequestBuilders(com.linkedin.restli.examples.greetings.client.TyperefKeysRequestBuilders) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) Test(org.testng.annotations.Test)

Example 29 with EntityResponse

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);
}
Also used : BatchEntityResponse(com.linkedin.restli.internal.client.response.BatchEntityResponse) BatchEntityResponse(com.linkedin.restli.internal.client.response.BatchEntityResponse) BatchUpdateEntityResponse(com.linkedin.restli.internal.client.response.BatchUpdateEntityResponse) EntityResponse(com.linkedin.restli.common.EntityResponse) TestRecord(com.linkedin.restli.client.test.TestRecord) ResponseImpl(com.linkedin.restli.internal.client.ResponseImpl) DataMap(com.linkedin.data.DataMap) ErrorResponse(com.linkedin.restli.common.ErrorResponse)

Example 30 with EntityResponse

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 } };
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) CompoundKey(com.linkedin.restli.common.CompoundKey) HashMap(java.util.HashMap) HttpStatus(com.linkedin.restli.common.HttpStatus) MyCustomString(com.linkedin.restli.common.MyCustomString) ErrorResponse(com.linkedin.restli.common.ErrorResponse) EntityResponse(com.linkedin.restli.common.EntityResponse) DataProvider(org.testng.annotations.DataProvider)

Aggregations

EntityResponse (com.linkedin.restli.common.EntityResponse)37 Test (org.testng.annotations.Test)21 HashMap (java.util.HashMap)18 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)17 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)14 Map (java.util.Map)12 DataMap (com.linkedin.data.DataMap)11 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)11 Message (com.linkedin.restli.examples.greetings.api.Message)10 IdEntityResponse (com.linkedin.restli.common.IdEntityResponse)9 ErrorResponse (com.linkedin.restli.common.ErrorResponse)7 TwoPartKey (com.linkedin.restli.examples.greetings.api.TwoPartKey)7 ArrayList (java.util.ArrayList)7 CompoundKey (com.linkedin.restli.common.CompoundKey)6 UpdateStatus (com.linkedin.restli.common.UpdateStatus)6 HashSet (java.util.HashSet)6 HttpStatus (com.linkedin.restli.common.HttpStatus)4 PatchRequest (com.linkedin.restli.common.PatchRequest)4 ProtocolVersion (com.linkedin.restli.common.ProtocolVersion)4 ComplexKeys (com.linkedin.restli.examples.greetings.client.ComplexKeys)4