Search in sources :

Example 11 with CreateIdStatus

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

the class TestComplexKeysResource method testBatchCreateMain.

private void testBatchCreateMain(BatchCreateRequestBuilder<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchCreateRequestBuilder, BatchGetRequestBuilder<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> batchGetRequestBuilder) throws RemoteInvocationException {
    final String messageText1 = "firstMessage";
    Message message1 = new Message();
    message1.setMessage(messageText1);
    final String messageText2 = "secondMessage";
    Message message2 = new Message();
    message2.setMessage(messageText2);
    List<Message> messages = new ArrayList<>(2);
    messages.add(message1);
    messages.add(message2);
    ComplexResourceKey<TwoPartKey, TwoPartKey> expectedComplexKey1 = getComplexKey(messageText1, messageText1);
    ComplexResourceKey<TwoPartKey, TwoPartKey> expectedComplexKey2 = getComplexKey(messageText2, messageText2);
    // test build
    Request<CollectionResponse<CreateStatus>> request = batchCreateRequestBuilder.inputs(messages).build();
    ResponseFuture<CollectionResponse<CreateStatus>> future = getClient().sendRequest(request);
    Response<CollectionResponse<CreateStatus>> response = future.getResponse();
    Assert.assertEquals(response.getStatus(), 200);
    Set<ComplexResourceKey<TwoPartKey, TwoPartKey>> expectedComplexKeys = new HashSet<>(2);
    expectedComplexKeys.add(expectedComplexKey1);
    expectedComplexKeys.add(expectedComplexKey2);
    for (CreateStatus createStatus : response.getEntity().getElements()) {
        @SuppressWarnings("unchecked") CreateIdStatus<ComplexResourceKey<TwoPartKey, TwoPartKey>> createIdStatus = (CreateIdStatus<ComplexResourceKey<TwoPartKey, TwoPartKey>>) createStatus;
        Assert.assertEquals(createIdStatus.getStatus(), Integer.valueOf(201));
        Assert.assertTrue(expectedComplexKeys.contains(createIdStatus.getKey()));
        try {
            @SuppressWarnings("deprecation") String id = createIdStatus.getId();
            Assert.fail("buildReadOnlyId should throw an exception on ComplexKeys");
        } catch (UnsupportedOperationException e) {
        // expected
        }
        expectedComplexKeys.remove(createIdStatus.getKey());
    }
    Assert.assertTrue(expectedComplexKeys.isEmpty());
    // attempt to batch get created records
    List<ComplexResourceKey<TwoPartKey, TwoPartKey>> createdKeys = new ArrayList<>(2);
    createdKeys.add(expectedComplexKey1);
    createdKeys.add(expectedComplexKey2);
    BatchGetKVRequest<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> getRequest = batchGetRequestBuilder.ids(createdKeys).buildKV();
    ResponseFuture<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message>> getFuture = getClient().sendRequest(getRequest);
    Response<BatchKVResponse<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message>> getResponse = getFuture.getResponse();
    Map<ComplexResourceKey<TwoPartKey, TwoPartKey>, Message> getResults = getResponse.getEntity().getResults();
    Assert.assertEquals(getResults.get(expectedComplexKey1), message1);
    Assert.assertEquals(getResults.get(expectedComplexKey2), message2);
    Assert.assertEquals(getResults.size(), 2);
}
Also used : TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) CreateStatus(com.linkedin.restli.common.CreateStatus) Message(com.linkedin.restli.examples.greetings.api.Message) CollectionResponse(com.linkedin.restli.common.CollectionResponse) ArrayList(java.util.ArrayList) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) HashSet(java.util.HashSet)

Example 12 with CreateIdStatus

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

the class TestCustomTypesClient method testCollectionBatchCreateId.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider")
public void testCollectionBatchCreateId(RestliRequestOptions options) throws RemoteInvocationException {
    CustomTypes2RequestBuilders builders = new CustomTypes2RequestBuilders(options);
    BatchCreateIdRequest<CustomLong, Greeting> request = builders.batchCreate().input(new Greeting().setId(1)).input(new Greeting().setId(2)).build();
    Response<BatchCreateIdResponse<CustomLong>> response = getClient().sendRequest(request).getResponse();
    List<CreateIdStatus<CustomLong>> results = response.getEntity().getElements();
    Set<CustomLong> expectedKeys = new HashSet<>();
    expectedKeys.add(new CustomLong(1L));
    expectedKeys.add(new CustomLong(2L));
    for (CreateIdStatus<CustomLong> status : results) {
        Assert.assertEquals(status.getStatus().intValue(), HttpStatus.S_204_NO_CONTENT.getCode());
        Assert.assertTrue(expectedKeys.contains(status.getKey()));
        @SuppressWarnings("deprecation") String id = status.getId();
        Assert.assertEquals(BatchResponse.keyToString(status.getKey(), ProtocolVersionUtil.extractProtocolVersion(response.getHeaders())), id);
        expectedKeys.remove(status.getKey());
    }
    Assert.assertTrue(expectedKeys.isEmpty());
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) CustomTypes2RequestBuilders(com.linkedin.restli.examples.greetings.client.CustomTypes2RequestBuilders) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) CustomLong(com.linkedin.restli.examples.custom.types.CustomLong) BatchCreateIdResponse(com.linkedin.restli.common.BatchCreateIdResponse) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 13 with CreateIdStatus

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

the class TestCustomTypesClient method testCollectionBatchCreate.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider")
public void testCollectionBatchCreate(RestliRequestOptions options) throws RemoteInvocationException {
    CustomTypes2Builders builders = new CustomTypes2Builders(options);
    BatchCreateRequest<Greeting> request = builders.batchCreate().input(new Greeting().setId(1)).input(new Greeting().setId(2)).build();
    Response<CollectionResponse<CreateStatus>> response = getClient().sendRequest(request).getResponse();
    List<CreateStatus> results = response.getEntity().getElements();
    Set<CustomLong> expectedKeys = new HashSet<>();
    expectedKeys.add(new CustomLong(1L));
    expectedKeys.add(new CustomLong(2L));
    for (CreateStatus status : results) {
        @SuppressWarnings("unchecked") CreateIdStatus<CustomLong> createIdStatus = (CreateIdStatus<CustomLong>) status;
        Assert.assertEquals(createIdStatus.getStatus().intValue(), HttpStatus.S_204_NO_CONTENT.getCode());
        Assert.assertTrue(expectedKeys.contains(createIdStatus.getKey()));
        @SuppressWarnings("deprecation") String id = createIdStatus.getId();
        Assert.assertEquals(BatchResponse.keyToString(createIdStatus.getKey(), ProtocolVersionUtil.extractProtocolVersion(response.getHeaders())), id);
        expectedKeys.remove(createIdStatus.getKey());
    }
    Assert.assertTrue(expectedKeys.isEmpty());
}
Also used : CreateStatus(com.linkedin.restli.common.CreateStatus) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) CollectionResponse(com.linkedin.restli.common.CollectionResponse) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) CustomLong(com.linkedin.restli.examples.custom.types.CustomLong) CustomTypes2Builders(com.linkedin.restli.examples.greetings.client.CustomTypes2Builders) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 14 with CreateIdStatus

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

the class BatchCreateResponseBuilder method buildRestLiResponseData.

/**
 * {@inheritDoc}
 *
 * @param result The result for a Rest.li BATCH_CREATE method. It's an instance of {@link BatchCreateResult}, if the
 *               BATCH_CREATE method doesn't return the entity; or an instance of {@link BatchCreateKVResult}, if it
 *               does.
 */
@Override
public RestLiResponseData<BatchCreateResponseEnvelope> buildRestLiResponseData(Request request, RoutingResult routingResult, Object result, Map<String, String> headers, List<HttpCookie> cookies) {
    Object altKey = null;
    if (routingResult.getContext().hasParameter(RestConstants.ALT_KEY_PARAM)) {
        altKey = routingResult.getContext().getParameter(RestConstants.ALT_KEY_PARAM);
    }
    final ProtocolVersion protocolVersion = ProtocolVersionUtil.extractProtocolVersion(headers);
    final ResourceContext resourceContext = routingResult.getContext();
    if (result instanceof BatchCreateKVResult && resourceContext.isReturnEntityRequested()) {
        BatchCreateKVResult<?, ?> list = (BatchCreateKVResult<?, ?>) result;
        if (list.getResults() == null) {
            throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null List inside of a BatchCreateKVResult returned by the resource method: " + routingResult.getResourceMethod());
        }
        List<BatchCreateResponseEnvelope.CollectionCreateResponseItem> collectionCreateList = new ArrayList<>(list.getResults().size());
        TimingContextUtil.beginTiming(routingResult.getContext().getRawRequestContext(), FrameworkTimingKeys.SERVER_RESPONSE_RESTLI_PROJECTION_APPLY.key());
        for (CreateKVResponse<?, ?> createKVResponse : list.getResults()) {
            if (createKVResponse == null) {
                throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null element inside of List inside of a BatchCreateKVResult returned by the resource method: " + routingResult.getResourceMethod());
            } else {
                Object id = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(createKVResponse.getId(), routingResult);
                if (createKVResponse.getError() == null) {
                    DataMap entityData = createKVResponse.getEntity() != null ? createKVResponse.getEntity().data() : null;
                    final DataMap data = RestUtils.projectFields(entityData, resourceContext);
                    CreateIdEntityStatus<Object, RecordTemplate> entry = new CreateIdEntityStatus<>(createKVResponse.getStatus().getCode(), id, new AnyRecord(data), // location uri
                    getLocationUri(request, id, altKey, protocolVersion), null, protocolVersion);
                    collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(entry));
                } else {
                    collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(createKVResponse.getError()));
                }
            }
        }
        TimingContextUtil.endTiming(routingResult.getContext().getRawRequestContext(), FrameworkTimingKeys.SERVER_RESPONSE_RESTLI_PROJECTION_APPLY.key());
        return new RestLiResponseDataImpl<>(new BatchCreateResponseEnvelope(HttpStatus.S_200_OK, collectionCreateList, true), headers, cookies);
    } else {
        List<? extends CreateResponse> createResponses = extractCreateResponseList(result);
        // Verify that a null list was not passed into the BatchCreateResult. If so, this is a developer error.
        if (createResponses == null) {
            throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null List inside of a BatchCreateResult returned by the resource method: " + routingResult.getResourceMethod());
        }
        List<BatchCreateResponseEnvelope.CollectionCreateResponseItem> collectionCreateList = new ArrayList<>(createResponses.size());
        for (CreateResponse createResponse : createResponses) {
            // Verify that a null element was not passed into the BatchCreateResult list. If so, this is a developer error.
            if (createResponse == null) {
                throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null element inside of List inside of a BatchCreateResult returned by the resource method: " + routingResult.getResourceMethod());
            } else {
                Object id = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(createResponse.getId(), routingResult);
                if (createResponse.getError() == null) {
                    CreateIdStatus<Object> entry = new CreateIdStatus<>(createResponse.getStatus().getCode(), id, // location uri
                    getLocationUri(request, id, altKey, protocolVersion), null, protocolVersion);
                    collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(entry));
                } else {
                    collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(createResponse.getError()));
                }
            }
        }
        return new RestLiResponseDataImpl<>(new BatchCreateResponseEnvelope(HttpStatus.S_200_OK, collectionCreateList, false), headers, cookies);
    }
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) AnyRecord(com.linkedin.restli.internal.server.methods.AnyRecord) CreateIdEntityStatus(com.linkedin.restli.common.CreateIdEntityStatus) CreateResponse(com.linkedin.restli.server.CreateResponse) ArrayList(java.util.ArrayList) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) DataMap(com.linkedin.data.DataMap) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RecordTemplate(com.linkedin.data.template.RecordTemplate) BatchCreateKVResult(com.linkedin.restli.server.BatchCreateKVResult)

Example 15 with CreateIdStatus

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

the class TestRestLiResponseData method testCreateCollectionResponseEnvelopeUpdates.

@Test(dataProvider = "createCollectionResponseEnvelopesProvider")
public void testCreateCollectionResponseEnvelopeUpdates(RestLiResponseDataImpl responseData) {
    BatchCreateResponseEnvelope responseEnvelope = responseData.getBatchCreateResponseEnvelope();
    Assert.assertNull(responseData.getServiceException());
    Assert.assertEquals(responseEnvelope.getCreateResponses(), Collections.emptyList());
    Assert.assertFalse(responseData.isErrorResponse());
    responseData.setException(exception500);
    Assert.assertNull(responseEnvelope.getCreateResponses());
    responseEnvelope.setCreateResponse(new ArrayList<BatchCreateResponseEnvelope.CollectionCreateResponseItem>(), HttpStatus.S_200_OK);
    Assert.assertNull(responseData.getServiceException());
    Assert.assertEquals(responseEnvelope.getCreateResponses().size(), 0);
    responseEnvelope.getCreateResponses().add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(new CreateIdStatus<Object>(new DataMap(), new Object())));
    responseEnvelope.getCreateResponses().add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(exception500, 2));
    Assert.assertEquals(responseEnvelope.getCreateResponses().size(), 2);
}
Also used : CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Aggregations

CreateIdStatus (com.linkedin.restli.common.CreateIdStatus)27 Test (org.testng.annotations.Test)19 ArrayList (java.util.ArrayList)15 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)11 BatchCreateIdResponse (com.linkedin.restli.common.BatchCreateIdResponse)9 CustomLong (com.linkedin.restli.examples.custom.types.CustomLong)7 CollectionResponse (com.linkedin.restli.common.CollectionResponse)6 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)5 HashMap (java.util.HashMap)5 List (java.util.List)5 DataMap (com.linkedin.data.DataMap)4 CreateIdEntityStatus (com.linkedin.restli.common.CreateIdEntityStatus)4 CreateStatus (com.linkedin.restli.common.CreateStatus)4 Greetings (com.linkedin.restli.examples.greetings.client.Greetings)4 GreetingsFluentClient (com.linkedin.restli.examples.greetings.client.GreetingsFluentClient)4 Map (java.util.Map)4 RecordTemplate (com.linkedin.data.template.RecordTemplate)3 StringMap (com.linkedin.data.template.StringMap)3 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)3 EmptyRecord (com.linkedin.restli.common.EmptyRecord)3