Search in sources :

Example 1 with UpdateEntityStatus

use of com.linkedin.restli.common.UpdateEntityStatus in project rest.li by linkedin.

the class TestRestLiScatterGather method testSendSGPartialUpdateEntityRequests.

// BatchPartialUpdateEntityRequest
private static void testSendSGPartialUpdateEntityRequests(RestClient restClient, Map<Long, PatchRequest<Greeting>> inputs) throws RemoteInvocationException {
    BatchPartialUpdateEntityRequest<Long, Greeting> request = new PartialUpdateGreetingRequestBuilders().batchPartialUpdateAndGet().inputs(inputs).setParam("foo", "bar").returnEntity(true).build();
    BatchKVResponse<Long, UpdateEntityStatus<Greeting>> result = restClient.sendRequest(request).getResponse().getEntity();
    Assert.assertEquals(result.getResults().size(), inputs.size());
    UpdateEntityStatus<Greeting> item = result.getResults().values().iterator().next();
    Assert.assertNotNull(item.getEntity());
    Assert.assertFalse(item.hasError());
    Assert.assertEquals(result.getErrors().size(), 0);
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) UpdateEntityStatus(com.linkedin.restli.common.UpdateEntityStatus) PartialUpdateGreetingRequestBuilders(com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingRequestBuilders)

Example 2 with UpdateEntityStatus

use of com.linkedin.restli.common.UpdateEntityStatus in project rest.li by linkedin.

the class TestReturnEntityWithBatchPartialUpdate method testReturnEntityOnDemand.

/**
 * Ensures that different usages of {@link BatchPartialUpdateEntityRequestBuilder#returnEntity(boolean)} are handled
 * correctly and that the response appropriately contains the entities or nothing depending on how and if the provided
 * method is used.
 * @param returnEntity value of the {@link RestConstants#RETURN_ENTITY_PARAM} parameter of this request
 * @param expectReturnEntities whether or not the patched entities are expected in the response
 */
@Test(dataProvider = "returnEntityOnDemandData")
public void testReturnEntityOnDemand(Boolean returnEntity, boolean expectReturnEntities) throws RemoteInvocationException {
    final long expectedId1 = 8L;
    final long expectedId2 = 9L;
    Map<Long, PatchRequest<Greeting>> patches = new HashMap<>();
    patches.put(expectedId1, PatchRequest.createFromEmptyPatchDocument());
    patches.put(expectedId2, PatchRequest.createFromEmptyPatchDocument());
    Map<Long, Greeting> expectedGreetings = new HashMap<>();
    expectedGreetings.put(expectedId1, new Greeting().setId(expectedId1).setMessage("Message " + expectedId1).setTone(Tone.FRIENDLY));
    expectedGreetings.put(expectedId2, new Greeting().setId(expectedId2).setMessage("Message " + expectedId2).setTone(Tone.FRIENDLY));
    BatchPartialUpdateEntityRequestBuilder<Long, Greeting> requestBuilder = new PartialUpdateGreetingRequestBuilders().batchPartialUpdateAndGet().inputs(patches);
    if (returnEntity != null) {
        requestBuilder.returnEntity(returnEntity);
    }
    BatchPartialUpdateEntityRequest<Long, Greeting> request = requestBuilder.build();
    Response<BatchKVResponse<Long, UpdateEntityStatus<Greeting>>> response = getClient().sendRequest(request).getResponse();
    BatchKVResponse<Long, UpdateEntityStatus<Greeting>> batchKVResponse = response.getEntity();
    Assert.assertNotNull(batchKVResponse);
    Map<Long, UpdateEntityStatus<Greeting>> greetings = batchKVResponse.getResults();
    Assert.assertNotNull(greetings);
    for (Long key : greetings.keySet()) {
        Assert.assertTrue(expectedGreetings.containsKey(key), "Encountered unexpected ID in batch response.");
        UpdateEntityStatus<Greeting> updateEntityStatus = greetings.get(key);
        Assert.assertNotNull(updateEntityStatus);
        Assert.assertEquals(updateEntityStatus.getStatus().intValue(), HttpStatus.S_200_OK.getCode());
        Assert.assertFalse(updateEntityStatus.hasError());
        if (expectReturnEntities) {
            Assert.assertTrue(updateEntityStatus.hasEntity());
            Greeting returnedEntity = updateEntityStatus.getEntity();
            Greeting expectedEntity = expectedGreetings.get(key);
            Assert.assertNotNull(returnedEntity, "RecordTemplate entity in response should not be null.");
            Assert.assertEquals(returnedEntity.getId(), expectedEntity.getId(), "Expected returned entity ID to match original.");
            Assert.assertEquals(returnedEntity.getMessage(), expectedEntity.getMessage(), "Expected returned entity message to match original.");
            Assert.assertEquals(returnedEntity.getTone(), expectedEntity.getTone(), "Expected returned entity tone to match original.");
        } else {
            Assert.assertFalse(updateEntityStatus.hasEntity());
            Assert.assertNull(updateEntityStatus.getEntity());
        }
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) UpdateEntityStatus(com.linkedin.restli.common.UpdateEntityStatus) HashMap(java.util.HashMap) PatchRequest(com.linkedin.restli.common.PatchRequest) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) PartialUpdateGreetingRequestBuilders(com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingRequestBuilders) Test(org.testng.annotations.Test)

Example 3 with UpdateEntityStatus

use of com.linkedin.restli.common.UpdateEntityStatus in project rest.li by linkedin.

the class TestReturnEntityWithBatchPartialUpdate method testBatchPartialUpdateErrorMap.

/**
 * Ensures that individual errors are handled correctly and sent back to the client in the batch response.
 * This test coerces the server resource to return 404 errors after trying to patch nonexistent entities.
 */
@Test
public void testBatchPartialUpdateErrorMap() throws RemoteInvocationException {
    Map<Long, PatchRequest<Greeting>> patches = new HashMap<>();
    patches.put(2147L, PatchRequest.createFromEmptyPatchDocument());
    patches.put(2148L, PatchRequest.createFromEmptyPatchDocument());
    BatchPartialUpdateEntityRequest<Long, Greeting> request = new PartialUpdateGreetingRequestBuilders().batchPartialUpdateAndGet().inputs(patches).returnEntity(true).build();
    Response<BatchKVResponse<Long, UpdateEntityStatus<Greeting>>> response = getClient().sendRequest(request).getResponse();
    Assert.assertNotNull(response);
    BatchKVResponse<Long, UpdateEntityStatus<Greeting>> batchKVResponse = response.getEntity();
    Assert.assertNotNull(batchKVResponse);
    Map<Long, UpdateEntityStatus<Greeting>> greetings = batchKVResponse.getResults();
    Assert.assertNotNull(greetings);
    for (UpdateEntityStatus<Greeting> updateEntityStatus : batchKVResponse.getResults().values()) {
        Assert.assertFalse(updateEntityStatus.hasEntity());
        Assert.assertEquals(updateEntityStatus.getStatus().intValue(), HttpStatus.S_404_NOT_FOUND.getCode());
        Assert.assertTrue(updateEntityStatus.hasError());
        ErrorResponse error = updateEntityStatus.getError();
        Assert.assertNotNull(error);
        Assert.assertEquals(updateEntityStatus.getError().getStatus().intValue(), HttpStatus.S_404_NOT_FOUND.getCode());
    }
    Map<Long, ErrorResponse> errors = batchKVResponse.getErrors();
    Assert.assertNotNull(errors);
    for (ErrorResponse error : errors.values()) {
        Assert.assertEquals(error.getStatus().intValue(), HttpStatus.S_404_NOT_FOUND.getCode());
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) UpdateEntityStatus(com.linkedin.restli.common.UpdateEntityStatus) HashMap(java.util.HashMap) PatchRequest(com.linkedin.restli.common.PatchRequest) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) ErrorResponse(com.linkedin.restli.common.ErrorResponse) PartialUpdateGreetingRequestBuilders(com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingRequestBuilders) Test(org.testng.annotations.Test)

Example 4 with UpdateEntityStatus

use of com.linkedin.restli.common.UpdateEntityStatus in project rest.li by linkedin.

the class RestLiValidationFilter method validateBatchResponse.

private void validateBatchResponse(RestLiDataValidator validator, Map<?, BatchResponseEnvelope.BatchResponseEntry> batchResponseMap) {
    StringBuilder sb = new StringBuilder();
    for (Map.Entry<?, ? extends BatchResponseEnvelope.BatchResponseEntry> entry : batchResponseMap.entrySet()) {
        if (entry.getValue().hasException()) {
            continue;
        }
        // The "entity" in the results map may be the raw record entity, or a wrapper containing the record entity
        final RecordTemplate entity = entry.getValue().getRecord();
        ValidationResult result;
        if (entity instanceof UpdateEntityStatus) {
            result = validator.validateOutput(((UpdateEntityStatus<? extends RecordTemplate>) entity).getEntity());
        } else {
            result = validator.validateOutput(entity);
        }
        if (!result.isValid()) {
            sb.append("Key: ").append(entry.getKey()).append(", ").append(result.getMessages().toString());
        }
    }
    if (sb.length() > 0) {
        throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, sb.toString());
    }
}
Also used : UpdateEntityStatus(com.linkedin.restli.common.UpdateEntityStatus) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RecordTemplate(com.linkedin.data.template.RecordTemplate) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) BatchResponseEnvelope(com.linkedin.restli.internal.server.response.BatchResponseEnvelope)

Example 5 with UpdateEntityStatus

use of com.linkedin.restli.common.UpdateEntityStatus in project rest.li by linkedin.

the class TestDefaultScatterGatherStrategy method createBatchUpdateEntityResponse.

private static Response<BatchKVResponse<Long, UpdateEntityStatus<TestRecord>>> createBatchUpdateEntityResponse(ProtocolVersion version, Set<Long> resultKeys, Set<Long> errorKeys) {
    DataMap resultMap = new DataMap();
    DataMap errorMap = new DataMap();
    for (Long id : resultKeys) {
        resultMap.put(id.toString(), new UpdateEntityStatus<>(HttpStatus.S_200_OK.getCode(), new TestRecord().setId(id)).data());
    }
    for (Long id : errorKeys) {
        ErrorResponse err = new ErrorResponse().setStatus(HttpStatus.S_404_NOT_FOUND.getCode());
        errorMap.put(id.toString(), err.data());
    }
    DataMap responseMap = new DataMap();
    responseMap.put(BatchResponse.RESULTS, resultMap);
    responseMap.put(BatchResponse.ERRORS, errorMap);
    DataMap mergedMap = ResponseDecoderUtil.mergeUpdateStatusResponseData(responseMap);
    BatchUpdateEntityResponse<Long, TestRecord> response = new BatchUpdateEntityResponse<>(mergedMap, 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 : UpdateEntityStatus(com.linkedin.restli.common.UpdateEntityStatus) TestRecord(com.linkedin.restli.client.test.TestRecord) BatchUpdateEntityResponse(com.linkedin.restli.internal.client.response.BatchUpdateEntityResponse) ResponseImpl(com.linkedin.restli.internal.client.ResponseImpl) DataMap(com.linkedin.data.DataMap) ErrorResponse(com.linkedin.restli.common.ErrorResponse)

Aggregations

UpdateEntityStatus (com.linkedin.restli.common.UpdateEntityStatus)13 Test (org.testng.annotations.Test)7 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)6 PartialUpdateGreetingRequestBuilders (com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingRequestBuilders)5 DataMap (com.linkedin.data.DataMap)4 RecordTemplate (com.linkedin.data.template.RecordTemplate)4 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)4 HashMap (java.util.HashMap)4 ErrorResponse (com.linkedin.restli.common.ErrorResponse)3 PatchRequest (com.linkedin.restli.common.PatchRequest)3 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)2 TestRecord (com.linkedin.restli.server.TestRecord)2 Map (java.util.Map)2 PathSpec (com.linkedin.data.schema.PathSpec)1 ValidationResult (com.linkedin.data.schema.validation.ValidationResult)1 MaskTree (com.linkedin.data.transform.filter.request.MaskTree)1 ParSeqUnitTestHelper (com.linkedin.parseq.ParSeqUnitTestHelper)1 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)1 RequestContext (com.linkedin.r2.message.RequestContext)1 TimingKey (com.linkedin.r2.message.timing.TimingKey)1