Search in sources :

Example 6 with CreateIdStatus

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

the class BatchCreateHelper method batchCreateOldBuilders.

private static <K, V extends RecordTemplate> List<CreateIdStatus<K>> batchCreateOldBuilders(RestClient restClient, BatchCreateRequestBuilder<K, V> builder, List<V> entities) throws RemoteInvocationException {
    BatchCreateRequest<V> request = builder.inputs(entities).build();
    Response<CollectionResponse<CreateStatus>> response = restClient.sendRequest(request).getResponse();
    List<CreateStatus> elements = response.getEntity().getElements();
    List<CreateIdStatus<K>> result = new ArrayList<>(elements.size());
    for (CreateStatus status : elements) {
        @SuppressWarnings("unchecked") CreateIdStatus<K> createIdStatus = (CreateIdStatus<K>) status;
        result.add(createIdStatus);
    }
    return result;
}
Also used : CreateStatus(com.linkedin.restli.common.CreateStatus) CollectionResponse(com.linkedin.restli.common.CollectionResponse) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) ArrayList(java.util.ArrayList)

Example 7 with CreateIdStatus

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

the class TestParseqBasedFluentClientApi method testBatchCreate.

@Test
public void testBatchCreate() throws Exception {
    Greetings greetings = new GreetingsFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
    CompletionStage<List<CreateIdStatus<Long>>> result = greetings.batchCreate(Arrays.asList(getGreeting(), getGreeting()));
    CompletableFuture<List<CreateIdStatus<Long>>> future = result.toCompletableFuture();
    List<CreateIdStatus<Long>> ids = future.get(5000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(ids.size(), 2);
}
Also used : 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) Test(org.testng.annotations.Test)

Example 8 with CreateIdStatus

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

the class TestReturnEntityWithCreate method testBatchCreateId.

/**
 * Test for backward compatibility of batch create id request.
 * @param restClient
 * @param expectedContentType
 * @param builders
 * @throws RemoteInvocationException
 */
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "newBuildersClientDataDataProvider")
public void testBatchCreateId(RestClient restClient, String expectedContentType, CreateGreetingRequestBuilders builders) throws RemoteInvocationException {
    Greeting greeting = new Greeting();
    greeting.setMessage("first time!");
    greeting.setTone(Tone.FRIENDLY);
    BatchCreateIdRequest<Long, Greeting> batchCreateIdRequest = builders.batchCreate().inputs(Arrays.asList(greeting, greeting)).build();
    Response<BatchCreateIdResponse<Long>> response = restClient.sendRequest(batchCreateIdRequest).getResponse();
    Assert.assertEquals(response.getHeader(RestConstants.HEADER_CONTENT_TYPE), expectedContentType);
    List<CreateIdStatus<Long>> elems = response.getEntity().getElements();
    Assert.assertEquals(elems.get(0).getStatus().intValue(), HttpStatus.S_201_CREATED.getCode());
    Assert.assertEquals(elems.get(0).getLocation(), "/" + builders.getPrimaryResource() + "/" + elems.get(0).getKey());
    Assert.assertEquals(elems.get(1).getStatus().intValue(), HttpStatus.S_201_CREATED.getCode());
    Assert.assertEquals(elems.get(1).getLocation(), "/" + builders.getPrimaryResource() + "/" + elems.get(1).getKey());
}
Also used : BatchCreateIdResponse(com.linkedin.restli.common.BatchCreateIdResponse) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) Test(org.testng.annotations.Test)

Example 9 with CreateIdStatus

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

the class TestRestLiValidation method testBatchCreateManualFailure.

@Test(dataProvider = "batchCreateFailureData")
public void testBatchCreateManualFailure(List<ValidationDemo> validationDemos, List<String> errorMessages) throws RemoteInvocationException {
    Response<CollectionResponse<CreateStatus>> response = _restClientManual.sendRequest(new RootBuilderWrapper<Integer, ValidationDemo>(new ValidationDemosBuilders()).batchCreate().inputs(validationDemos).build()).getResponse();
    List<CreateStatus> results = response.getEntity().getElements();
    int i = 0;
    for (CreateStatus result : results) {
        Assert.assertEquals((int) result.getStatus(), HttpStatus.S_422_UNPROCESSABLE_ENTITY.getCode());
        Assert.assertTrue(result.getError().getMessage().contains(errorMessages.get(i++)));
    }
    response = _restClientManual.sendRequest(new RootBuilderWrapper<Integer, ValidationDemo>(new ValidationDemosRequestBuilders()).batchCreate().inputs(validationDemos).build()).getResponse();
    @SuppressWarnings("unchecked") List<CreateIdStatus<Integer>> results2 = ((BatchCreateIdResponse<Integer>) (Object) response.getEntity()).getElements();
    i = 0;
    for (CreateIdStatus<Integer> result : results2) {
        Assert.assertEquals((int) result.getStatus(), HttpStatus.S_422_UNPROCESSABLE_ENTITY.getCode());
        Assert.assertTrue(result.getError().getMessage().contains(errorMessages.get(i++)));
    }
}
Also used : CreateStatus(com.linkedin.restli.common.CreateStatus) CollectionResponse(com.linkedin.restli.common.CollectionResponse) BatchCollectionResponse(com.linkedin.restli.common.BatchCollectionResponse) RootBuilderWrapper(com.linkedin.restli.test.util.RootBuilderWrapper) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo) BatchCreateIdResponse(com.linkedin.restli.common.BatchCreateIdResponse) ValidationDemosBuilders(com.linkedin.restli.examples.greetings.client.ValidationDemosBuilders) AutoValidationDemosBuilders(com.linkedin.restli.examples.greetings.client.AutoValidationDemosBuilders) AutoValidationDemosRequestBuilders(com.linkedin.restli.examples.greetings.client.AutoValidationDemosRequestBuilders) ValidationDemosRequestBuilders(com.linkedin.restli.examples.greetings.client.ValidationDemosRequestBuilders) Test(org.testng.annotations.Test)

Example 10 with CreateIdStatus

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

the class TestBatchCreateResponseBuilder method testCreateResultBuilder.

@Test(dataProvider = "createResultBuilderTestData")
@SuppressWarnings("unchecked")
public void testCreateResultBuilder(String uriString, String altKeyName, Map<String, AlternativeKey<?, ?>> alternativeKeyMap, List<CreateIdStatus<Object>> expectedStatuses) throws URISyntaxException {
    List<CreateResponse> createResponses = Arrays.asList(new CreateResponse(1L), new CreateResponse(2L));
    BatchCreateResult<Long, Foo> results = new BatchCreateResult<>(createResponses);
    Map<String, String> headers = ResponseBuilderUtil.getHeaders();
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(alternativeKeyMap);
    ServerResourceContext mockContext = getMockResourceContext(altKeyName);
    RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
    RestRequest request = new RestRequestBuilder(new URI(uriString)).build();
    BatchCreateResponseBuilder responseBuilder = new BatchCreateResponseBuilder(null);
    RestLiResponseData<BatchCreateResponseEnvelope> responseData = responseBuilder.buildRestLiResponseData(request, routingResult, results, headers, Collections.emptyList());
    RestLiResponse restResponse = responseBuilder.buildResponse(routingResult, responseData);
    EasyMock.verify(mockDescriptor);
    ResponseBuilderUtil.validateHeaders(restResponse, headers);
    Assert.assertFalse(responseData.getResponseEnvelope().isGetAfterCreate());
    List<com.linkedin.restli.common.CreateIdStatus<Object>> items = new ArrayList<>();
    for (BatchCreateResponseEnvelope.CollectionCreateResponseItem item : responseData.getResponseEnvelope().getCreateResponses()) {
        items.add((CreateIdStatus<Object>) item.getRecord());
    }
    Assert.assertEquals(restResponse.getEntity(), new BatchCreateIdResponse<>(items));
    Assert.assertEquals(expectedStatuses, items);
    Assert.assertEquals(restResponse.getStatus(), HttpStatus.S_200_OK);
}
Also used : CreateResponse(com.linkedin.restli.server.CreateResponse) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ArrayList(java.util.ArrayList) URI(java.net.URI) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) Foo(com.linkedin.pegasus.generator.examples.Foo) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) RestRequest(com.linkedin.r2.message.rest.RestRequest) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) BatchCreateResult(com.linkedin.restli.server.BatchCreateResult) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) 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