Search in sources :

Example 1 with BatchUpdateEntityResult

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

the class TestBatchPartialUpdateResponseBuilder method testProjectionInBuildRestLiResponseData.

/**
 * Ensures that the returned entities are properly projected when a projection mask is passed into the response builder.
 */
@Test
@SuppressWarnings("unchecked")
public void testProjectionInBuildRestLiResponseData() {
    final Long id = 1L;
    TestRecord record = new TestRecord().setIntField(2147).setDoubleField(21.47).setFloatField(123F).setLongField(456L);
    BatchUpdateEntityResult<Long, TestRecord> result = new BatchUpdateEntityResult<>(Collections.singletonMap(id, new UpdateEntityResponse<>(HttpStatus.S_200_OK, record)));
    MaskTree maskTree = new MaskTree();
    maskTree.addOperation(new PathSpec("intField"), MaskOperation.POSITIVE_MASK_OP);
    Map<String, String> headers = ResponseBuilderUtil.getHeaders();
    RoutingResult routingResult = getMockRoutingResult(true, maskTree);
    BatchPartialUpdateResponseBuilder batchPartialUpdateResponseBuilder = new BatchPartialUpdateResponseBuilder(new ErrorResponseBuilder());
    RestLiResponseData<BatchPartialUpdateResponseEnvelope> responseData = batchPartialUpdateResponseBuilder.buildRestLiResponseData(null, routingResult, result, headers, Collections.emptyList());
    UpdateEntityStatus<TestRecord> updateEntityStatus = (UpdateEntityStatus<TestRecord>) responseData.getResponseEnvelope().getBatchResponseMap().get(id).getRecord();
    Assert.assertNotNull(updateEntityStatus);
    RecordTemplate returnedRecord = updateEntityStatus.getEntity();
    Assert.assertEquals(returnedRecord.data().size(), 1, "Expected response record to be projected down to one field.");
    Assert.assertEquals((int) returnedRecord.data().get("intField"), 2147, "Expected response record intField to match original.");
}
Also used : UpdateEntityStatus(com.linkedin.restli.common.UpdateEntityStatus) BatchUpdateEntityResult(com.linkedin.restli.server.BatchUpdateEntityResult) PathSpec(com.linkedin.data.schema.PathSpec) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) MaskTree(com.linkedin.data.transform.filter.request.MaskTree) UpdateEntityResponse(com.linkedin.restli.server.UpdateEntityResponse) RecordTemplate(com.linkedin.data.template.RecordTemplate) TestRecord(com.linkedin.restli.server.TestRecord) Test(org.testng.annotations.Test)

Example 2 with BatchUpdateEntityResult

use of com.linkedin.restli.server.BatchUpdateEntityResult 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)

Aggregations

BatchUpdateEntityResult (com.linkedin.restli.server.BatchUpdateEntityResult)2 UpdateEntityResponse (com.linkedin.restli.server.UpdateEntityResponse)2 PathSpec (com.linkedin.data.schema.PathSpec)1 RecordTemplate (com.linkedin.data.template.RecordTemplate)1 MaskTree (com.linkedin.data.transform.filter.request.MaskTree)1 PatchRequest (com.linkedin.restli.common.PatchRequest)1 UpdateEntityStatus (com.linkedin.restli.common.UpdateEntityStatus)1 InstrumentationControl (com.linkedin.restli.examples.instrumentation.api.InstrumentationControl)1 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)1 BatchPatchRequest (com.linkedin.restli.server.BatchPatchRequest)1 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)1 TestRecord (com.linkedin.restli.server.TestRecord)1 ReturnEntity (com.linkedin.restli.server.annotations.ReturnEntity)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.testng.annotations.Test)1