Search in sources :

Example 21 with PatchRequest

use of com.linkedin.restli.common.PatchRequest 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 22 with PatchRequest

use of com.linkedin.restli.common.PatchRequest 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 23 with PatchRequest

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

the class LatencyInstrumentationResource method batchPartialUpdate.

/**
 * This is the "downstream endpoint", queried by {@link #create(InstrumentationControl)} (the "upstream endpoint").
 */
@ReturnEntity
@RestMethod.BatchPartialUpdate
public BatchUpdateEntityResult<Long, InstrumentationControl> batchPartialUpdate(BatchPatchRequest<Long, InstrumentationControl> batchPatchRequest) throws DataProcessingException {
    final Map<Long, UpdateEntityResponse<InstrumentationControl>> results = new HashMap<>();
    final Map<Long, RestLiServiceException> errors = new HashMap<>();
    for (Map.Entry<Long, PatchRequest<InstrumentationControl>> entry : batchPatchRequest.getData().entrySet()) {
        // Render each patch into a normal record so we know whether or not to force a failure
        InstrumentationControl control = new InstrumentationControl();
        PatchApplier.applyPatch(control, entry.getValue());
        if (control.isForceException()) {
            RestLiServiceException error = new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "You wanted me to fail, so I failed.").setCode(DOWNSTREAM_ERROR_CODE);
            errors.put(entry.getKey(), error);
        } else {
            results.put(entry.getKey(), new UpdateEntityResponse<>(HttpStatus.S_200_OK, control));
        }
    }
    return new BatchUpdateEntityResult<>(results, errors);
}
Also used : RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) HashMap(java.util.HashMap) UpdateEntityResponse(com.linkedin.restli.server.UpdateEntityResponse) PatchRequest(com.linkedin.restli.common.PatchRequest) BatchPatchRequest(com.linkedin.restli.server.BatchPatchRequest) HashMap(java.util.HashMap) Map(java.util.Map) BatchUpdateEntityResult(com.linkedin.restli.server.BatchUpdateEntityResult) InstrumentationControl(com.linkedin.restli.examples.instrumentation.api.InstrumentationControl) ReturnEntity(com.linkedin.restli.server.annotations.ReturnEntity)

Example 24 with PatchRequest

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

the class ValidationDemoResource method batchUpdate.

@RestMethod.BatchPartialUpdate
public BatchUpdateResult<Integer, ValidationDemo> batchUpdate(final BatchPatchRequest<Integer, ValidationDemo> entityUpdates, @ValidatorParam RestLiDataValidator validator) {
    Map<Integer, UpdateResponse> results = new HashMap<>();
    Map<Integer, RestLiServiceException> errors = new HashMap<>();
    for (Map.Entry<Integer, PatchRequest<ValidationDemo>> entry : entityUpdates.getData().entrySet()) {
        Integer key = entry.getKey();
        PatchRequest<ValidationDemo> patch = entry.getValue();
        ValidationResult result = validator.validateInput(patch);
        if (result.isValid()) {
            results.put(key, new UpdateResponse(HttpStatus.S_204_NO_CONTENT));
        } else {
            errors.put(key, new RestLiServiceException(HttpStatus.S_422_UNPROCESSABLE_ENTITY, result.getMessages().toString()));
        }
    }
    return new BatchUpdateResult<>(results, errors);
}
Also used : HashMap(java.util.HashMap) PatchRequest(com.linkedin.restli.common.PatchRequest) BatchPatchRequest(com.linkedin.restli.server.BatchPatchRequest) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo) UpdateResponse(com.linkedin.restli.server.UpdateResponse) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) HashMap(java.util.HashMap) Map(java.util.Map)

Example 25 with PatchRequest

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

the class GroupMembershipsResource2 method batchUpdate.

@Override
public BatchUpdateResult<CompoundKey, GroupMembership> batchUpdate(BatchPatchRequest<CompoundKey, GroupMembership> patches) {
    Map<CompoundKey, UpdateResponse> results = new HashMap<>();
    for (Map.Entry<CompoundKey, PatchRequest<GroupMembership>> entry : patches.getData().entrySet()) {
        CompoundKey key = entry.getKey();
        PatchRequest<GroupMembership> patch = entry.getValue();
        GroupMembership groupMembership = _app.getMembershipMgr().get(key);
        if (groupMembership == null) {
            results.put(key, new UpdateResponse(HttpStatus.S_404_NOT_FOUND));
        } else {
            try {
                PatchApplier.applyPatch(groupMembership, patch);
                _app.getMembershipMgr().save(groupMembership);
                results.put(key, new UpdateResponse(HttpStatus.S_204_NO_CONTENT));
            } catch (DataProcessingException e) {
                results.put(key, new UpdateResponse(HttpStatus.S_400_BAD_REQUEST));
            }
        }
    }
    return new BatchUpdateResult<>(results);
}
Also used : UpdateResponse(com.linkedin.restli.server.UpdateResponse) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) CompoundKey(com.linkedin.restli.common.CompoundKey) HashMap(java.util.HashMap) GroupMembership(com.linkedin.restli.examples.groups.api.GroupMembership) PatchRequest(com.linkedin.restli.common.PatchRequest) BatchPatchRequest(com.linkedin.restli.server.BatchPatchRequest) HashMap(java.util.HashMap) Map(java.util.Map) DataProcessingException(com.linkedin.data.transform.DataProcessingException)

Aggregations

PatchRequest (com.linkedin.restli.common.PatchRequest)32 HashMap (java.util.HashMap)26 Test (org.testng.annotations.Test)22 Map (java.util.Map)14 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)11 DataMap (com.linkedin.data.DataMap)9 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)8 UpdateStatus (com.linkedin.restli.common.UpdateStatus)6 BatchPatchRequest (com.linkedin.restli.server.BatchPatchRequest)6 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)5 CompoundKey (com.linkedin.restli.common.CompoundKey)5 CustomLong (com.linkedin.restli.examples.custom.types.CustomLong)5 CreateGreeting (com.linkedin.restli.examples.greetings.client.CreateGreeting)5 PartialUpdateGreeting (com.linkedin.restli.examples.greetings.client.PartialUpdateGreeting)5 PartialUpdateGreetingFluentClient (com.linkedin.restli.examples.greetings.client.PartialUpdateGreetingFluentClient)5 ValidationResult (com.linkedin.data.schema.validation.ValidationResult)4 StringMap (com.linkedin.data.template.StringMap)4 KeyValueRecord (com.linkedin.restli.common.KeyValueRecord)4 BatchUpdateResult (com.linkedin.restli.server.BatchUpdateResult)4 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)4