Search in sources :

Example 6 with CreateIdEntityStatus

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

the class TestReturnEntityWithCreate method testBatchCreateReturnEntityOnDemand.

/**
 * Ensures that different usages of {@link com.linkedin.restli.client.CreateIdEntityRequestBuilder#returnEntity(boolean)} are handled
 * correctly and that the response appropriately contains the entities or nothing depending on how and if the provided
 * method is used.
 */
@Test(dataProvider = "returnEntityOnDemandData")
public void testBatchCreateReturnEntityOnDemand(Boolean returnEntity, boolean expectReturnEntity) throws RemoteInvocationException {
    Greeting greeting = new Greeting();
    greeting.setMessage("second time!");
    greeting.setTone(Tone.FRIENDLY);
    Greeting greeting2 = new Greeting();
    greeting2.setMessage("first time!");
    greeting2.setTone(Tone.FRIENDLY);
    List<Greeting> greetings = new ArrayList<>();
    greetings.add(greeting);
    greetings.add(greeting2);
    CreateGreetingRequestBuilders builders = new CreateGreetingRequestBuilders();
    CreateGreetingBatchCreateAndGetRequestBuilder builder = builders.batchCreateAndGet().inputs(greetings);
    if (returnEntity != null) {
        builder.returnEntity(returnEntity);
    }
    BatchCreateIdEntityRequest<Long, Greeting> batchCreateIdEntityRequest = builder.build();
    Response<BatchCreateIdEntityResponse<Long, Greeting>> response = getClient().sendRequest(batchCreateIdEntityRequest).getResponse();
    List<CreateIdEntityStatus<Long, Greeting>> createIdEntityStatuses = response.getEntity().getElements();
    Assert.assertEquals(createIdEntityStatuses.size(), greetings.size(), "Expected size of batch response list to match size of input entity list.");
    for (int i = 0; i < createIdEntityStatuses.size(); i++) {
        CreateIdEntityStatus<Long, Greeting> createIdEntityStatus = createIdEntityStatuses.get(i);
        Greeting expectedGreeting = greetings.get(i);
        long id = createIdEntityStatus.getKey();
        Assert.assertEquals((int) createIdEntityStatus.getStatus(), HttpStatus.S_201_CREATED.getCode());
        Assert.assertEquals(createIdEntityStatus.getLocation(), "/" + CreateGreetingRequestBuilders.getPrimaryResource() + "/" + id);
        if (expectReturnEntity) {
            Greeting returnedEntity = createIdEntityStatus.getEntity();
            Assert.assertNotNull(returnedEntity, "RecordTemplate entity in response should not be null.");
            Assert.assertEquals(returnedEntity.getMessage(), expectedGreeting.getMessage(), "Expect returned entity message to match original.");
            Assert.assertEquals(returnedEntity.getTone(), expectedGreeting.getTone(), "Expect returned entity tone to match original.");
        } else {
            Assert.assertNull(createIdEntityStatus.getEntity(), "RecordTemplate entity in response should be null.");
        }
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) CreateIdEntityStatus(com.linkedin.restli.common.CreateIdEntityStatus) ArrayList(java.util.ArrayList) CreateGreetingBatchCreateAndGetRequestBuilder(com.linkedin.restli.examples.greetings.client.CreateGreetingBatchCreateAndGetRequestBuilder) BatchCreateIdEntityResponse(com.linkedin.restli.common.BatchCreateIdEntityResponse) CreateGreetingRequestBuilders(com.linkedin.restli.examples.greetings.client.CreateGreetingRequestBuilders) Test(org.testng.annotations.Test)

Example 7 with CreateIdEntityStatus

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

the class TestParseqBasedFluentClientApi method testBatchCreateReturnEntity.

@Test
public void testBatchCreateReturnEntity() throws Exception {
    CreateGreeting greetings = new CreateGreetingFluentClient(_parSeqRestliClient, _parSeqUnitTestHelper.getEngine());
    String msg1 = Double.toString(Math.random());
    String msg2 = Double.toString(Math.random());
    CompletionStage<List<CreateIdEntityStatus<Long, Greeting>>> result = greetings.batchCreateAndGet(Arrays.asList(getGreeting(msg1), getGreeting(msg2)));
    CompletableFuture<List<CreateIdEntityStatus<Long, Greeting>>> future = result.toCompletableFuture();
    List<CreateIdEntityStatus<Long, Greeting>> entities = future.get(5000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(entities.size(), 2);
    Assert.assertNotNull(entities.get(0).getEntity());
    Assert.assertEquals(msg1, entities.get(0).getEntity().getMessage());
    Assert.assertNotNull(entities.get(1).getEntity());
    Assert.assertEquals(msg2, entities.get(1).getEntity().getMessage());
}
Also used : CreateGreeting(com.linkedin.restli.examples.greetings.client.CreateGreeting) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) PartialUpdateGreeting(com.linkedin.restli.examples.greetings.client.PartialUpdateGreeting) CreateGreetingFluentClient(com.linkedin.restli.examples.greetings.client.CreateGreetingFluentClient) CreateIdEntityStatus(com.linkedin.restli.common.CreateIdEntityStatus) CreateGreeting(com.linkedin.restli.examples.greetings.client.CreateGreeting) CustomLong(com.linkedin.restli.examples.custom.types.CustomLong) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 8 with CreateIdEntityStatus

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

the class TestBatchCreateResponseBuilder method testCreateKVResultBuilder.

@Test(dataProvider = "createKVResultBuilderTestData")
public void testCreateKVResultBuilder(String altKeyName, Map<String, AlternativeKey<?, ?>> alternativeKeyMap, List<CreateIdEntityStatus<?, Foo>> expectedResponses) throws URISyntaxException {
    List<CreateKVResponse<Long, Foo>> createKVResponses = new ArrayList<>(2);
    Foo foo1 = new Foo();
    foo1.setStringField("foo1");
    Foo foo2 = new Foo();
    foo2.setStringField("foo2");
    createKVResponses.add(new CreateKVResponse<>(1L, foo1));
    createKVResponses.add(new CreateKVResponse<>(2L, foo2));
    BatchCreateKVResult<Long, Foo> results = new BatchCreateKVResult<>(createKVResponses);
    Map<String, String> headers = ResponseBuilderUtil.getHeaders();
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(alternativeKeyMap);
    ServerResourceContext mockContext = getMockKVResourceContext(altKeyName);
    RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
    BatchCreateResponseBuilder responseBuilder = new BatchCreateResponseBuilder(null);
    RestRequest request = new RestRequestBuilder(new URI("/foo")).build();
    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.assertTrue(responseData.getResponseEnvelope().isGetAfterCreate());
    List<CreateIdEntityStatus<Object, Foo>> items = new ArrayList<>();
    for (BatchCreateResponseEnvelope.CollectionCreateResponseItem item : responseData.getResponseEnvelope().getCreateResponses()) {
        @SuppressWarnings("unchecked") CreateIdEntityStatus<Object, Foo> record = (CreateIdEntityStatus<Object, Foo>) item.getRecord();
        items.add(record);
    }
    Assert.assertEquals(items, expectedResponses);
    Assert.assertEquals(restResponse.getStatus(), HttpStatus.S_200_OK);
}
Also used : ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ArrayList(java.util.ArrayList) URI(java.net.URI) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) CreateIdEntityStatus(com.linkedin.restli.common.CreateIdEntityStatus) Foo(com.linkedin.pegasus.generator.examples.Foo) RestRequest(com.linkedin.r2.message.rest.RestRequest) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) BatchCreateKVResult(com.linkedin.restli.server.BatchCreateKVResult) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) CreateKVResponse(com.linkedin.restli.server.CreateKVResponse) Test(org.testng.annotations.Test)

Example 9 with CreateIdEntityStatus

use of com.linkedin.restli.common.CreateIdEntityStatus 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 10 with CreateIdEntityStatus

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

the class TestBatchCreateResponseBuilder method createKVResultBuilderTestData.

@DataProvider(name = "createKVResultBuilderTestData")
public Object[][] createKVResultBuilderTestData() {
    Map<String, AlternativeKey<?, ?>> alternativeKeyMap = new HashMap<>();
    alternativeKeyMap.put("alt", new AlternativeKey<>(new TestKeyCoercer(), String.class, new StringDataSchema()));
    Foo foo1 = new Foo();
    foo1.setStringField("foo1");
    Foo foo2 = new Foo();
    foo2.setStringField("foo2");
    List<CreateIdEntityStatus<Long, Foo>> expectedResponses = new ArrayList<>(2);
    expectedResponses.add(new CreateIdEntityStatus<>(201, 1L, foo1, "/foo/1", null, AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion()));
    expectedResponses.add(new CreateIdEntityStatus<>(201, 2L, foo2, "/foo/2", null, AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion()));
    List<CreateIdEntityStatus<String, Foo>> expectedAltResponses = new ArrayList<>(2);
    expectedAltResponses.add(new CreateIdEntityStatus<>(201, "Alt1", foo1, "/foo/Alt1?altkey=alt", null, AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion()));
    expectedAltResponses.add(new CreateIdEntityStatus<>(201, "Alt2", foo2, "/foo/Alt2?altkey=alt", null, AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion()));
    return new Object[][] { { null, null, expectedResponses }, { "alt", alternativeKeyMap, expectedAltResponses } };
}
Also used : CreateIdEntityStatus(com.linkedin.restli.common.CreateIdEntityStatus) HashMap(java.util.HashMap) Foo(com.linkedin.pegasus.generator.examples.Foo) ArrayList(java.util.ArrayList) StringDataSchema(com.linkedin.data.schema.StringDataSchema) AlternativeKey(com.linkedin.restli.server.AlternativeKey) DataProvider(org.testng.annotations.DataProvider)

Aggregations

CreateIdEntityStatus (com.linkedin.restli.common.CreateIdEntityStatus)10 ArrayList (java.util.ArrayList)7 Test (org.testng.annotations.Test)6 DataMap (com.linkedin.data.DataMap)4 Foo (com.linkedin.pegasus.generator.examples.Foo)4 CreateIdStatus (com.linkedin.restli.common.CreateIdStatus)4 RestRequest (com.linkedin.r2.message.rest.RestRequest)3 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)3 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)3 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)3 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)3 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)3 BatchCreateKVResult (com.linkedin.restli.server.BatchCreateKVResult)3 URI (java.net.URI)3 RecordTemplate (com.linkedin.data.template.RecordTemplate)2 RequestContext (com.linkedin.r2.message.RequestContext)2 CreateGreeting (com.linkedin.restli.examples.greetings.client.CreateGreeting)2 CreateGreetingFluentClient (com.linkedin.restli.examples.greetings.client.CreateGreetingFluentClient)2 PartialUpdateGreeting (com.linkedin.restli.examples.greetings.client.PartialUpdateGreeting)2 AnyRecord (com.linkedin.restli.internal.server.methods.AnyRecord)2