Search in sources :

Example 31 with UpdateStatus

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

the class TestParseqBasedFluentClientApi method testCreateAndThenBatchDeleteWithFailures.

@Test
public void testCreateAndThenBatchDeleteWithFailures() throws Exception {
    Greetings greetings = new GreetingsFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
    // Create entities first so we don't delete those used by other tests.
    CompletionStage<List<CreateIdStatus<Long>>> createResult = greetings.batchCreate(Arrays.asList(getGreeting(), getGreeting()));
    CompletionStage<Map<Long, UpdateStatus>> result = createResult.thenCompose(ids -> {
        Set<Long> deleteIds = Sets.newHashSet(ids.stream().map(CreateIdStatus::getKey).collect(Collectors.toList()));
        deleteIds.add(-1L);
        return greetings.batchDelete(deleteIds);
    });
    CompletableFuture<Map<Long, UpdateStatus>> future = result.toCompletableFuture();
    Map<Long, UpdateStatus> ids = future.get(5000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(ids.size(), 3);
    Assert.assertEquals(ids.remove(-1L).getStatus().intValue(), 404);
    for (UpdateStatus status : ids.values()) {
        Assert.assertEquals(status.getStatus().intValue(), 204);
    }
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) CustomLong(com.linkedin.restli.examples.custom.types.CustomLong) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) Greetings(com.linkedin.restli.examples.greetings.client.Greetings) GreetingsFluentClient(com.linkedin.restli.examples.greetings.client.GreetingsFluentClient) 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 32 with UpdateStatus

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

the class TestParseqBasedFluentClientApi method testComplexKey_partialUpdate.

@Test
public void testComplexKey_partialUpdate() throws Exception {
    ComplexKeys complexKeyClient = new ComplexKeysFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
    Message message = new Message();
    message.setTone(Tone.FRIENDLY);
    PatchRequest<Message> patch = PatchGenerator.diffEmpty(message);
    final Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, PatchRequest<Message>> inputs = new HashMap<>();
    ComplexResourceKey<TwoPartKey, TwoPartKey> key1 = getComplexKey(StringTestKeys.SIMPLEKEY, StringTestKeys.SIMPLEKEY2);
    ComplexResourceKey<TwoPartKey, TwoPartKey> key2 = getComplexKey(StringTestKeys.URL, StringTestKeys.URL2);
    inputs.put(key1, patch);
    inputs.put(key2, patch);
    Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateStatus> result = complexKeyClient.batchPartialUpdate(inputs).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
    // Update return valid result
    Assert.assertEquals(result.get(key1).getStatus().intValue(), 204);
    Assert.assertEquals(result.get(key2).getStatus().intValue(), 204);
    Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, EntityResponse<Message>> getResult = complexKeyClient.batchGet(new HashSet<>(Arrays.asList(key1, key2))).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(getResult.get(key1).getEntity().getTone(), Tone.FRIENDLY);
    Assert.assertEquals(getResult.get(key2).getEntity().getTone(), Tone.FRIENDLY);
}
Also used : TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) UpdateStatus(com.linkedin.restli.common.UpdateStatus) Message(com.linkedin.restli.examples.greetings.api.Message) HashMap(java.util.HashMap) PatchRequest(com.linkedin.restli.common.PatchRequest) EntityResponse(com.linkedin.restli.common.EntityResponse) IdEntityResponse(com.linkedin.restli.common.IdEntityResponse) ComplexKeys(com.linkedin.restli.examples.greetings.client.ComplexKeys) ComplexKeysFluentClient(com.linkedin.restli.examples.greetings.client.ComplexKeysFluentClient) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) Test(org.testng.annotations.Test)

Example 33 with UpdateStatus

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

the class TestParseqBasedFluentClientApi method testAssociateResourceBatchPartialUpdate.

@Test
public void testAssociateResourceBatchPartialUpdate() throws Exception {
    Associations associations = new AssociationsFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
    Map<CompoundKey, PatchRequest<Message>> patches = new HashMap<>();
    patches.put(getAssociateResourceUrlKey(associations), new PatchRequest<>());
    patches.put(getAssociateResourceSimpleKey(associations), new PatchRequest<>());
    Map<CompoundKey, UpdateStatus> ids = associations.batchPartialUpdate(patches).toCompletableFuture().get(5000, TimeUnit.MILLISECONDS);
    for (CompoundKey id : ids.keySet()) {
        Assert.assertEquals(ids.get(id).getStatus().intValue(), 204);
    }
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) CompoundKey(com.linkedin.restli.common.CompoundKey) HashMap(java.util.HashMap) Associations(com.linkedin.restli.examples.greetings.client.Associations) PatchRequest(com.linkedin.restli.common.PatchRequest) AssociationsAssociationsFluentClient(com.linkedin.restli.examples.greetings.client.AssociationsAssociationsFluentClient) AssociationsFluentClient(com.linkedin.restli.examples.greetings.client.AssociationsFluentClient) Test(org.testng.annotations.Test)

Example 34 with UpdateStatus

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

the class BatchResponseBuilder method buildRestLiResponseData.

/**
 * {@inheritDoc}
 *
 * @param result The result of a Rest.li BATCH_UPDATE, BATCH_PARTIAL_UPDATE, or BATCH_DELETE method. It is a
 *               {@link BatchUpdateResult} object.
 */
@SuppressWarnings("unchecked")
@Override
public D buildRestLiResponseData(Request request, RoutingResult routingResult, Object result, Map<String, String> headers, List<HttpCookie> cookies) {
    @SuppressWarnings({ "unchecked" }) final BatchUpdateResult<Object, ?> /* constrained by signature of {@link com.linkedin.restli.server.resources.CollectionResource#batchUpdate(java.util.Map)} */
    updateResult = (BatchUpdateResult<Object, ?>) result;
    final Map<Object, UpdateResponse> results = updateResult.getResults();
    // Verify the map is not null. If so, this is a developer error.
    if (results == null) {
        throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null Map found inside of the BatchUpdateResult returned by the resource method: " + routingResult.getResourceMethod());
    }
    final Map<Object, RestLiServiceException> serviceErrors = updateResult.getErrors();
    if (serviceErrors == null) {
        throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null errors Map found inside of the BatchUpdateResult returned by the resource method: " + routingResult.getResourceMethod());
    }
    TimingContextUtil.beginTiming(routingResult.getContext().getRawRequestContext(), FrameworkTimingKeys.SERVER_RESPONSE_RESTLI_PROJECTION_APPLY.key());
    Map<Object, BatchResponseEntry> batchResponseMap = new HashMap<>();
    for (Map.Entry<Object, UpdateResponse> entry : results.entrySet()) {
        if (entry.getKey() == null) {
            throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null key inside of the Map returned inside of the BatchUpdateResult returned by the resource method: " + routingResult.getResourceMethod());
        }
        if (!serviceErrors.containsKey(entry.getKey())) {
            Object finalKey = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(entry.getKey(), routingResult);
            UpdateStatus updateStatus = buildUpdateStatus(routingResult.getContext(), entry.getValue());
            batchResponseMap.put(finalKey, new BatchResponseEntry(entry.getValue().getStatus(), updateStatus));
        }
    }
    TimingContextUtil.endTiming(routingResult.getContext().getRawRequestContext(), FrameworkTimingKeys.SERVER_RESPONSE_RESTLI_PROJECTION_APPLY.key());
    for (Map.Entry<Object, RestLiServiceException> entry : serviceErrors.entrySet()) {
        if (entry.getKey() == null || entry.getValue() == null) {
            throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null key or value inside of the Map returned inside of the BatchUpdateResult returned by the resource method: " + routingResult.getResourceMethod());
        }
        Object finalKey = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(entry.getKey(), routingResult);
        batchResponseMap.put(finalKey, new BatchResponseEntry(entry.getValue().getStatus(), entry.getValue()));
    }
    for (Map.Entry<Object, RestLiServiceException> entry : routingResult.getContext().getBatchKeyErrors().entrySet()) {
        Object finalKey = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(entry.getKey(), routingResult);
        batchResponseMap.put(finalKey, new BatchResponseEntry(entry.getValue().getStatus(), entry.getValue()));
    }
    return buildResponseData(HttpStatus.S_200_OK, batchResponseMap, headers, cookies);
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) HashMap(java.util.HashMap) UpdateResponse(com.linkedin.restli.server.UpdateResponse) BatchResponseEntry(com.linkedin.restli.internal.server.response.BatchResponseEnvelope.BatchResponseEntry) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map)

Example 35 with UpdateStatus

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

the class BatchResponseBuilder method toBatchResponse.

/**
 * Helper method for {@link #buildResponse} that splits the merged {@link UpdateStatus} map into a "statuses" map and
 * an "errors" map, uses this to construct a data map that properly matches the over-the-wire format, and wraps it
 * in a {@link BatchResponse}.
 * @param statuses map of {@link UpdateStatus} objects
 * @param protocolVersion
 * @param <K> key type
 * @return batch response
 */
private static <K> BatchResponse<AnyRecord> toBatchResponse(Map<K, UpdateStatus> statuses, ProtocolVersion protocolVersion) {
    final DataMap splitStatuses = new DataMap();
    final DataMap splitErrors = new DataMap();
    for (Map.Entry<K, UpdateStatus> statusEntry : statuses.entrySet()) {
        final DataMap statusData = statusEntry.getValue().data();
        final String stringKey = URIParamUtils.encodeKeyForBody(statusEntry.getKey(), false, protocolVersion);
        final DataMap error = statusData.getDataMap("error");
        if (error == null) {
            // status and error should be mutually exclusive for now
            CheckedUtil.putWithoutChecking(splitStatuses, stringKey, statusData);
        } else {
            CheckedUtil.putWithoutChecking(splitErrors, stringKey, error);
        }
    }
    final DataMap splitResponseData = new DataMap();
    CheckedUtil.putWithoutChecking(splitResponseData, BatchResponse.RESULTS, splitStatuses);
    CheckedUtil.putWithoutChecking(splitResponseData, BatchResponse.ERRORS, splitErrors);
    return new BatchResponse<>(splitResponseData, AnyRecord.class);
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) BatchResponse(com.linkedin.restli.common.BatchResponse) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) DataMap(com.linkedin.data.DataMap)

Aggregations

UpdateStatus (com.linkedin.restli.common.UpdateStatus)42 HashMap (java.util.HashMap)27 Test (org.testng.annotations.Test)22 Map (java.util.Map)18 DataMap (com.linkedin.data.DataMap)16 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)15 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)12 ArrayList (java.util.ArrayList)11 CustomLong (com.linkedin.restli.examples.custom.types.CustomLong)8 BatchResponse (com.linkedin.restli.common.BatchResponse)7 CompoundKey (com.linkedin.restli.common.CompoundKey)7 EntityResponse (com.linkedin.restli.common.EntityResponse)7 PatchRequest (com.linkedin.restli.common.PatchRequest)6 BatchResponseEntry (com.linkedin.restli.internal.server.response.BatchResponseEnvelope.BatchResponseEntry)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)5 ErrorResponse (com.linkedin.restli.common.ErrorResponse)5 Message (com.linkedin.restli.examples.greetings.api.Message)5 TwoPartKey (com.linkedin.restli.examples.greetings.api.TwoPartKey)5 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)5