Search in sources :

Example 21 with CreateIdStatus

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

the class TestSimpleResourceHierarchy method testSubCollectionBatchCreate.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestSubBuilderDataProvider")
public void testSubCollectionBatchCreate(RootBuilderWrapper<Long, Greeting> builders) throws RemoteInvocationException {
    Greeting greeting = new Greeting();
    greeting.setMessage("Message1");
    greeting.setTone(Tone.FRIENDLY);
    Greeting greeting2 = new Greeting();
    greeting.setMessage("Message2");
    greeting.setTone(Tone.FRIENDLY);
    ArrayList<Greeting> greetings = new ArrayList<>();
    greetings.add(greeting);
    greetings.add(greeting2);
    // POST
    List<CreateIdStatus<Long>> statuses = BatchCreateHelper.batchCreate(getClient(), builders, greetings, false);
    ArrayList<Long> ids = new ArrayList<>();
    for (CreateIdStatus<Long> status : statuses) {
        @SuppressWarnings("deprecation") String id = status.getId();
        Assert.assertEquals(status.getKey().longValue(), Long.parseLong(id));
        ids.add(status.getKey());
    }
    // GET again to verify that the create has worked.
    final RestliRequestOptions requestOptions = builders.getRequestOptions();
    Request<BatchKVResponse<Long, EntityResponse<Greeting>>> request = new SubgreetingsRequestBuilders(requestOptions).batchGet().ids(ids).build();
    Response<BatchKVResponse<Long, EntityResponse<Greeting>>> response = getClient().sendRequest(request).getResponse();
    BatchKVResponse<Long, EntityResponse<Greeting>> batchResponse = response.getEntity();
    Assert.assertEquals(batchResponse.getResults().size(), ids.size());
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) RestliRequestOptions(com.linkedin.restli.client.RestliRequestOptions) SubgreetingsRequestBuilders(com.linkedin.restli.examples.greetings.client.SubgreetingsRequestBuilders) ArrayList(java.util.ArrayList) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) EntityResponse(com.linkedin.restli.common.EntityResponse) Test(org.testng.annotations.Test)

Example 22 with CreateIdStatus

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

the class TestBatchCreateResponseBuilder method testReturnEntityInBuildRestLiResponseData.

@Test(dataProvider = "returnEntityData")
@SuppressWarnings({ "Duplicates", "unchecked" })
public void testReturnEntityInBuildRestLiResponseData(Object batchCreateResult, List<CreateResponse> createResponses, boolean isReturnEntityRequested, boolean expectEntityReturned) throws URISyntaxException {
    ServerResourceContext mockContext = EasyMock.createMock(ServerResourceContext.class);
    EasyMock.expect(mockContext.hasParameter(RestConstants.ALT_KEY_PARAM)).andReturn(false).atLeastOnce();
    EasyMock.expect(mockContext.isReturnEntityRequested()).andReturn(isReturnEntityRequested);
    EasyMock.expect(mockContext.getProjectionMode()).andReturn(ProjectionMode.AUTOMATIC);
    EasyMock.expect(mockContext.getProjectionMask()).andReturn(null);
    EasyMock.expect(mockContext.getRawRequestContext()).andReturn(new RequestContext()).anyTimes();
    EasyMock.expect(mockContext.getAlwaysProjectedFields()).andReturn(Collections.emptySet()).anyTimes();
    EasyMock.replay(mockContext);
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(null);
    RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
    BatchCreateResponseBuilder responseBuilder = new BatchCreateResponseBuilder(new ErrorResponseBuilder());
    RestRequest request = new RestRequestBuilder(new URI("/foo")).build();
    RestLiResponseData<BatchCreateResponseEnvelope> responseData = responseBuilder.buildRestLiResponseData(request, routingResult, batchCreateResult, Collections.emptyMap(), Collections.emptyList());
    BatchCreateResponseEnvelope responseEnvelope = responseData.getResponseEnvelope();
    Assert.assertEquals(responseEnvelope.isGetAfterCreate(), expectEntityReturned);
    Assert.assertEquals(responseEnvelope.getCreateResponses().size(), createResponses.size());
    for (int i = 0; i < createResponses.size(); i++) {
        CreateIdStatus<Long> createIdStatus = (CreateIdStatus<Long>) responseEnvelope.getCreateResponses().get(i).getRecord();
        CreateResponse createResponse = createResponses.get(i);
        Assert.assertEquals(createIdStatus.getStatus().intValue(), HttpStatus.S_201_CREATED.getCode());
        Assert.assertEquals(createIdStatus.getLocation(), "/foo/" + createResponse.getId());
        if (expectEntityReturned) {
            CreateIdEntityStatus<Long, Foo> createIdEntityStatus = (CreateIdEntityStatus<Long, Foo>) createIdStatus;
            Assert.assertEquals(createIdEntityStatus.getEntity(), ((CreateKVResponse) createResponse).getEntity());
        }
    }
}
Also used : CreateIdEntityStatus(com.linkedin.restli.common.CreateIdEntityStatus) CreateResponse(com.linkedin.restli.server.CreateResponse) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) Foo(com.linkedin.pegasus.generator.examples.Foo) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) URI(java.net.URI) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 23 with CreateIdStatus

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

the class TestMockBatchCreateIdResponseFactory method testCreate.

@Test(dataProvider = "provideKeys")
public <K> void testCreate(K[] keys) {
    ProtocolVersion version = AllProtocolVersions.BASELINE_PROTOCOL_VERSION;
    List<CreateIdStatus<K>> elements = new ArrayList<>();
    elements.add(new CreateIdStatus<>(HttpStatus.S_201_CREATED.getCode(), keys[0], null, version));
    elements.add(new CreateIdStatus<>(HttpStatus.S_201_CREATED.getCode(), keys[1], null, version));
    ErrorResponse error = new ErrorResponse().setMessage("3");
    elements.add(new CreateIdStatus<>(HttpStatus.S_500_INTERNAL_SERVER_ERROR.getCode(), keys[2], error, version));
    BatchCreateIdResponse<K> batchResp = MockBatchCreateIdResponseFactory.create(elements);
    Assert.assertEquals(batchResp.getElements(), elements);
}
Also used : CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) ArrayList(java.util.ArrayList) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) ErrorResponse(com.linkedin.restli.common.ErrorResponse) Test(org.testng.annotations.Test)

Example 24 with CreateIdStatus

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

the class TestExceptionsResource method testBatchCreateIdErrors.

@SuppressWarnings("deprecation")
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider")
public void testBatchCreateIdErrors(RestliRequestOptions requestOptions) throws Exception {
    ExceptionsRequestBuilders builders = new ExceptionsRequestBuilders(requestOptions);
    BatchCreateIdRequest<Long, Greeting> batchCreateRequest = builders.batchCreate().input(new Greeting().setId(10L).setMessage("Greetings.").setTone(Tone.SINCERE)).input(new Greeting().setId(11L).setMessage("@#$%@!$%").setTone(Tone.INSULTING)).build();
    Response<BatchCreateIdResponse<Long>> response = getClient().sendRequest(batchCreateRequest).getResponse();
    List<CreateIdStatus<Long>> createStatuses = response.getEntity().getElements();
    Assert.assertEquals(createStatuses.size(), 2);
    @SuppressWarnings("unchecked") CreateIdStatus<Long> status0 = createStatuses.get(0);
    Assert.assertEquals(status0.getStatus().intValue(), HttpStatus.S_201_CREATED.getCode());
    Assert.assertEquals(status0.getKey(), Long.valueOf(10));
    @SuppressWarnings("deprecation") String id = status0.getId();
    Assert.assertEquals(BatchResponse.keyToString(status0.getKey(), ProtocolVersionUtil.extractProtocolVersion(response.getHeaders())), id);
    Assert.assertFalse(status0.hasError());
    CreateIdStatus<Long> status1 = createStatuses.get(1);
    Assert.assertEquals(status1.getStatus().intValue(), HttpStatus.S_406_NOT_ACCEPTABLE.getCode());
    Assert.assertTrue(status1.hasError());
    ErrorResponse error = status1.getError();
    Assert.assertEquals(error.getStatus().intValue(), HttpStatus.S_406_NOT_ACCEPTABLE.getCode());
    Assert.assertEquals(error.getMessage(), "I will not tolerate your insolence!");
    Assert.assertEquals(error.getServiceErrorCode().intValue(), 999);
    Assert.assertEquals(error.getExceptionClass(), "com.linkedin.restli.server.RestLiServiceException");
    Assert.assertEquals(error.getErrorDetails().data().getString("reason"), "insultingGreeting");
    Assert.assertTrue(error.getStackTrace().startsWith("com.linkedin.restli.server.RestLiServiceException [HTTP Status:406, serviceErrorCode:999]: I will not tolerate your insolence!"), "stacktrace mismatch:" + error.getStackTrace());
    Assert.assertFalse(error.hasCode());
    Assert.assertFalse(error.hasDocUrl());
    Assert.assertFalse(error.hasRequestId());
    Assert.assertEquals(error.getErrorDetailType(), EmptyRecord.class.getCanonicalName());
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) EmptyRecord(com.linkedin.restli.common.EmptyRecord) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) ErrorResponse(com.linkedin.restli.common.ErrorResponse) BatchCreateIdResponse(com.linkedin.restli.common.BatchCreateIdResponse) ExceptionsRequestBuilders(com.linkedin.restli.examples.greetings.client.ExceptionsRequestBuilders) Test(org.testng.annotations.Test)

Example 25 with CreateIdStatus

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

the class TestExceptionsResource method testBatchCreateErrors.

@SuppressWarnings("deprecation")
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider")
public void testBatchCreateErrors(RestliRequestOptions requestOptions) throws Exception {
    ExceptionsBuilders builders = new ExceptionsBuilders(requestOptions);
    Request<CollectionResponse<CreateStatus>> batchCreateRequest = builders.batchCreate().input(new Greeting().setId(10L).setMessage("Greetings.").setTone(Tone.SINCERE)).input(new Greeting().setId(11L).setMessage("@#$%@!$%").setTone(Tone.INSULTING)).build();
    Response<CollectionResponse<CreateStatus>> response = getClient().sendRequest(batchCreateRequest).getResponse();
    List<CreateStatus> createStatuses = response.getEntity().getElements();
    Assert.assertEquals(createStatuses.size(), 2);
    @SuppressWarnings("unchecked") CreateIdStatus<Long> status0 = (CreateIdStatus<Long>) createStatuses.get(0);
    Assert.assertEquals(status0.getStatus().intValue(), HttpStatus.S_201_CREATED.getCode());
    Assert.assertEquals(status0.getKey(), Long.valueOf(10));
    @SuppressWarnings("deprecation") String id = status0.getId();
    Assert.assertEquals(BatchResponse.keyToString(status0.getKey(), ProtocolVersionUtil.extractProtocolVersion(response.getHeaders())), id);
    Assert.assertFalse(status0.hasError());
    CreateStatus status1 = createStatuses.get(1);
    Assert.assertEquals(status1.getStatus().intValue(), HttpStatus.S_406_NOT_ACCEPTABLE.getCode());
    Assert.assertTrue(status1.hasError());
    ErrorResponse error = status1.getError();
    Assert.assertEquals(error.getStatus().intValue(), HttpStatus.S_406_NOT_ACCEPTABLE.getCode());
    Assert.assertEquals(error.getMessage(), "I will not tolerate your insolence!");
    Assert.assertEquals(error.getServiceErrorCode().intValue(), 999);
    Assert.assertEquals(error.getExceptionClass(), "com.linkedin.restli.server.RestLiServiceException");
    Assert.assertEquals(error.getErrorDetails().data().getString("reason"), "insultingGreeting");
    Assert.assertTrue(error.getStackTrace().startsWith("com.linkedin.restli.server.RestLiServiceException [HTTP Status:406, serviceErrorCode:999]: I will not tolerate your insolence!"), "stacktrace mismatch:" + error.getStackTrace());
    Assert.assertFalse(error.hasCode());
    Assert.assertFalse(error.hasDocUrl());
    Assert.assertFalse(error.hasRequestId());
    Assert.assertEquals(error.getErrorDetailType(), EmptyRecord.class.getCanonicalName());
}
Also used : CreateStatus(com.linkedin.restli.common.CreateStatus) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) EmptyRecord(com.linkedin.restli.common.EmptyRecord) ExceptionsBuilders(com.linkedin.restli.examples.greetings.client.ExceptionsBuilders) CollectionResponse(com.linkedin.restli.common.CollectionResponse) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) ErrorResponse(com.linkedin.restli.common.ErrorResponse) 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