Search in sources :

Example 21 with BatchUpdateResult

use of com.linkedin.restli.server.BatchUpdateResult in project rest.li by linkedin.

the class ComplexKeysDataProvider method batchUpdate.

public BatchUpdateResult<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchUpdate(BatchUpdateRequest<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> entities) {
    final Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateResponse> results = new HashMap<ComplexResourceKey<TwoPartKey, TwoPartKey>, UpdateResponse>();
    final Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, RestLiServiceException> errors = new HashMap<ComplexResourceKey<TwoPartKey, TwoPartKey>, RestLiServiceException>();
    for (Map.Entry<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> entry : entities.getData().entrySet()) {
        if (_db.containsKey(keyToString(entry.getKey().getKey()))) {
            _db.put(keyToString(entry.getKey().getKey()), entry.getValue());
            results.put(entry.getKey(), new UpdateResponse(HttpStatus.S_200_OK));
        } else {
            errors.put(entry.getKey(), new RestLiServiceException(HttpStatus.S_404_NOT_FOUND));
        }
    }
    return new BatchUpdateResult<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message>(results, errors);
}
Also used : TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) UpdateResponse(com.linkedin.restli.server.UpdateResponse) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) Message(com.linkedin.restli.examples.greetings.api.Message) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) HashMap(java.util.HashMap) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) HashMap(java.util.HashMap) Map(java.util.Map)

Example 22 with BatchUpdateResult

use of com.linkedin.restli.server.BatchUpdateResult in project rest.li by linkedin.

the class ExampleRequestResponseGenerator method batchDelete.

public ExampleRequestResponse batchDelete() {
    checkSupports(ResourceMethod.BATCH_DELETE);
    BatchDeleteRequestBuilder<Object, RecordTemplatePlaceholder> delete = new BatchDeleteRequestBuilder<Object, RecordTemplatePlaceholder>(_uriTemplate, RecordTemplatePlaceholder.class, _resourceSpec, _requestOptions);
    Object id1 = generateKey(0);
    Object id2 = generateKey(1);
    delete.ids(id1, id2);
    addParams(delete, ResourceMethod.BATCH_DELETE);
    addPathKeys(delete);
    BatchDeleteRequest<Object, RecordTemplatePlaceholder> request = delete.build();
    final Map<Object, UpdateResponse> bdResponseData = new HashMap<Object, UpdateResponse>();
    bdResponseData.put(id1, new UpdateResponse(HttpStatus.S_200_OK));
    bdResponseData.put(id2, new UpdateResponse(HttpStatus.S_200_OK));
    BatchUpdateResult<Object, RecordTemplatePlaceholder> result = new BatchUpdateResult<Object, RecordTemplatePlaceholder>(bdResponseData);
    return buildRequestResponse(request, result, buildResourceMethodDescriptorForRestMethod(request));
}
Also used : UpdateResponse(com.linkedin.restli.server.UpdateResponse) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) HashMap(java.util.HashMap) BatchDeleteRequestBuilder(com.linkedin.restli.client.BatchDeleteRequestBuilder)

Example 23 with BatchUpdateResult

use of com.linkedin.restli.server.BatchUpdateResult 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<Integer, UpdateResponse>();
    Map<Integer, RestLiServiceException> errors = new HashMap<Integer, RestLiServiceException>();
    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<Integer, ValidationDemo>(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 24 with BatchUpdateResult

use of com.linkedin.restli.server.BatchUpdateResult in project rest.li by linkedin.

the class NullGreetingsResourceImpl method batchUpdate.

@RestMethod.BatchUpdate
public BatchUpdateResult<Long, Greeting> batchUpdate(BatchUpdateRequest<Long, Greeting> entities) {
    final Map<Long, UpdateResponse> responseMap = new HashMap<Long, UpdateResponse>();
    responseMap.put(3l, new UpdateResponse(HttpStatus.S_201_CREATED));
    final Map<Long, RestLiServiceException> errorsMap = new HashMap<Long, RestLiServiceException>();
    errorsMap.put(8l, new RestLiServiceException(HttpStatus.S_202_ACCEPTED));
    if (entities.getData().containsKey(1l)) {
        //Return a null BatchUpdateResult
        return null;
    } else if (entities.getData().containsKey(2l)) {
        //Return a BatchUpdateResult with a null results Map
        return new BatchUpdateResult<Long, Greeting>(null);
    } else if (entities.getData().containsKey(3l)) {
        //Return a BatchUpdateResult with a null errors Map
        return new BatchUpdateResult<Long, Greeting>(responseMap, null);
    } else if (entities.getData().containsKey(4l)) {
        //Return a BatchUpdateResult with a errors Map that has a null key in it
        errorsMap.put(null, new RestLiServiceException(HttpStatus.S_202_ACCEPTED));
        return new BatchUpdateResult<Long, Greeting>(responseMap, errorsMap);
    } else if (entities.getData().containsKey(5l)) {
        //Return a BatchUpdateResult with a errors Map that has a null value in it
        errorsMap.put(9l, null);
        return new BatchUpdateResult<Long, Greeting>(responseMap, errorsMap);
    } else if (entities.getData().containsKey(6l)) {
        //Return a BatchUpdateResult with a map that has a null key in it
        responseMap.put(null, new UpdateResponse(HttpStatus.S_201_CREATED));
        return new BatchUpdateResult<Long, Greeting>(responseMap);
    } else {
        /*
       * Return a BatchUpdateResult with java.util.concurrent.ConcurrentHashMap(s).
       * This test is in place because certain map implementations, such as ConcurrentHashMap, can throw an NPE when
       * calling contains(null). We want to verify that checking for the existence of nulls in maps returned by
       * Rest.li resource methods do not cause such NPEs.
       * This is one of the few cases in this file where an error will not be generated by Rest.li.
       */
        final Map<Long, UpdateResponse> concurrentResponseMap = new ConcurrentHashMap<Long, UpdateResponse>(responseMap);
        return new BatchUpdateResult<Long, Greeting>(concurrentResponseMap, new ConcurrentHashMap<Long, RestLiServiceException>());
    }
}
Also used : UpdateResponse(com.linkedin.restli.server.UpdateResponse) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 25 with BatchUpdateResult

use of com.linkedin.restli.server.BatchUpdateResult in project rest.li by linkedin.

the class TestRestLiMethodInvocation method testPromiseBatchUpdateCollection.

@Test
@SuppressWarnings({ "unchecked" })
public void testPromiseBatchUpdateCollection() throws Exception {
    ResourceModel statusResourceModel = buildResourceModel(PromiseStatusCollectionResource.class);
    ResourceMethodDescriptor methodDescriptor = statusResourceModel.findMethod(ResourceMethod.BATCH_UPDATE);
    PromiseStatusCollectionResource statusResource = getMockResource(PromiseStatusCollectionResource.class);
    @SuppressWarnings("rawtypes") BatchUpdateRequest batchUpdateRequest = (BatchUpdateRequest) EasyMock.anyObject();
    EasyMock.expect(statusResource.batchUpdate(batchUpdateRequest)).andReturn(Promises.<BatchUpdateResult<Long, Status>>value(null)).once();
    String body = RestLiTestHelper.doubleQuote("{'entities':{'1':{},'2':{}}}");
    checkInvocation(statusResource, methodDescriptor, "PUT", version, "/promisestatuses?ids=List(1,2)", body, buildBatchPathKeys(1L, 2L));
}
Also used : PromiseStatusCollectionResource(com.linkedin.restli.server.twitter.PromiseStatusCollectionResource) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) BatchUpdateRequest(com.linkedin.restli.server.BatchUpdateRequest) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiTestHelper.buildResourceModel(com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel) ByteString(com.linkedin.data.ByteString) CustomString(com.linkedin.restli.server.custom.types.CustomString) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

BatchUpdateResult (com.linkedin.restli.server.BatchUpdateResult)33 UpdateResponse (com.linkedin.restli.server.UpdateResponse)19 HashMap (java.util.HashMap)19 Test (org.testng.annotations.Test)15 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)14 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)14 RestLiTestHelper.buildResourceModel (com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel)14 AfterTest (org.testng.annotations.AfterTest)14 BeforeTest (org.testng.annotations.BeforeTest)14 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)11 Map (java.util.Map)10 Callback (com.linkedin.common.callback.Callback)9 CompoundKey (com.linkedin.restli.common.CompoundKey)9 RestLiCallback (com.linkedin.restli.internal.server.RestLiCallback)9 FilterChainCallback (com.linkedin.restli.internal.server.filter.FilterChainCallback)9 RequestExecutionCallback (com.linkedin.restli.server.RequestExecutionCallback)9 EasyMock.anyObject (org.easymock.EasyMock.anyObject)9 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)8 BatchPatchRequest (com.linkedin.restli.server.BatchPatchRequest)8 DiscoveredItemKey (com.linkedin.restli.server.twitter.TwitterTestDataModels.DiscoveredItemKey)5