Search in sources :

Example 81 with Greeting

use of com.linkedin.restli.examples.greetings.api.Greeting in project rest.li by linkedin.

the class TestRequestCompression method testUpdate.

@Test(dataProvider = "requestData")
public void testUpdate(CompressionConfig requestCompressionConfig, String supportedEncodings, RestliRequestOptions restliRequestOptions, int messageLength, String testHelpHeader) throws RemoteInvocationException, CloneNotSupportedException, InterruptedException, ExecutionException, TimeoutException {
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("R2 Netty Scheduler"));
    Map<String, CompressionConfig> requestCompressionConfigs = new HashMap<String, CompressionConfig>();
    if (requestCompressionConfig != null) {
        requestCompressionConfigs.put(SERVICE_NAME, requestCompressionConfig);
    }
    HttpClientFactory httpClientFactory = new HttpClientFactory(FilterChains.empty(), new NioEventLoopGroup(), true, executor, true, null, false, AbstractJmxManager.NULL_JMX_MANAGER, // The default compression threshold is between small and large.
    500, requestCompressionConfigs);
    Map<String, String> properties = new HashMap<String, String>();
    properties.put(HttpClientFactory.HTTP_REQUEST_CONTENT_ENCODINGS, supportedEncodings);
    properties.put(HttpClientFactory.HTTP_SERVICE_NAME, SERVICE_NAME);
    TransportClientAdapter clientAdapter1 = new TransportClientAdapter(httpClientFactory.getClient(properties));
    RestClient client = new RestClient(clientAdapter1, FILTERS_URI_PREFIX);
    RootBuilderWrapper<Long, Greeting> builders = new RootBuilderWrapper<Long, Greeting>(new GreetingsRequestBuilders(restliRequestOptions));
    // GET
    Request<Greeting> request = builders.get().id(1L).build();
    ResponseFuture<Greeting> future = client.sendRequest(request);
    Response<Greeting> greetingResponse = future.getResponse();
    String response1 = greetingResponse.getEntity().getMessage();
    Assert.assertNotNull(response1);
    // POST
    Greeting greeting = new Greeting(greetingResponse.getEntity().data().copy());
    char[] As = new char[messageLength];
    Arrays.fill(As, 'A');
    String message = new String(As);
    greeting.setMessage(message);
    Request<EmptyRecord> writeRequest = builders.update().id(1L).input(greeting).setHeader(TEST_HELP_HEADER, testHelpHeader).build();
    client.sendRequest(writeRequest).getResponse();
    // GET again, to verify that our POST worked.
    Request<Greeting> request2 = builders.get().id(1L).build();
    ResponseFuture<Greeting> future2 = client.sendRequest(request2);
    String response2 = future2.getResponse().getEntity().getMessage();
    Assert.assertEquals(response2, message);
    FutureCallback<None> callback1 = new FutureCallback<None>();
    client.shutdown(callback1);
    callback1.get(30, TimeUnit.SECONDS);
    FutureCallback<None> callback2 = new FutureCallback<None>();
    httpClientFactory.shutdown(callback2);
    callback2.get(30, TimeUnit.SECONDS);
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) EmptyRecord(com.linkedin.restli.common.EmptyRecord) HashMap(java.util.HashMap) GreetingsRequestBuilders(com.linkedin.restli.examples.greetings.client.GreetingsRequestBuilders) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) FutureCallback(com.linkedin.common.callback.FutureCallback) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) NamedThreadFactory(com.linkedin.r2.util.NamedThreadFactory) RootBuilderWrapper(com.linkedin.restli.test.util.RootBuilderWrapper) RestClient(com.linkedin.restli.client.RestClient) None(com.linkedin.common.util.None) CompressionConfig(com.linkedin.r2.filter.CompressionConfig) Test(org.testng.annotations.Test)

Example 82 with Greeting

use of com.linkedin.restli.examples.greetings.api.Greeting in project rest.li by linkedin.

the class TestRestLiValidation method createFailures.

public static Object[][] createFailures() {
    ValidationDemo.UnionFieldWithInlineRecord unionField = new ValidationDemo.UnionFieldWithInlineRecord();
    unionField.setMyRecord(new myRecord().setFoo1(111));
    MyItemArray myItems = new MyItemArray();
    myItems.add(new myItem().setBar1("bar1"));
    GreetingMap greetingMap = new GreetingMap();
    greetingMap.put("key1", new Greeting());
    return new Object[][] { // ReadOnly fields should not be specified in a create request, whether they are required or optional
    { new ValidationDemo().setStringA("aaa"), "/stringA :: ReadOnly field present in a create request" }, { new ValidationDemo().setIntA(1234), "/intA :: ReadOnly field present in a create request" }, { new ValidationDemo().setUnionFieldWithInlineRecord(unionField), "/UnionFieldWithInlineRecord/com.linkedin.restli.examples.greetings.api.myRecord/foo1 :: ReadOnly field present in a create request" }, { new ValidationDemo().setArrayWithInlineRecord(myItems), "/ArrayWithInlineRecord/0/bar1 :: ReadOnly field present in a create request" }, { new ValidationDemo().setValidationDemoNext(new ValidationDemo().setStringB("stringB")), "/validationDemoNext/stringB :: ReadOnly field present in a create request" }, { new ValidationDemo().setValidationDemoNext(new ValidationDemo().setUnionFieldWithInlineRecord(unionField)), "/validationDemoNext/UnionFieldWithInlineRecord :: ReadOnly field present in a create request" }, // A field that is CreateOnly and required has to be present in a create request
    { new ValidationDemo(), "/stringB :: field is required but not found and has no default value" }, { new ValidationDemo().setStringB("bbb"), "/UnionFieldWithInlineRecord :: field is required but not found and has no default value" }, // Required fields without Rest.li data annotations should be present in a create request
    { new ValidationDemo().setArrayWithInlineRecord(myItems), "/ArrayWithInlineRecord/0/bar2 :: field is required but not found and has no default value" }, { new ValidationDemo().setMapWithTyperefs(greetingMap), "/MapWithTyperefs/key1/id :: field is required but not found and has no default value" }, { new ValidationDemo().setValidationDemoNext(new ValidationDemo()), "/validationDemoNext/stringA :: field is required but not found and has no default value" }, { new ValidationDemo(), "/UnionFieldWithInlineRecord :: field is required but not found and has no default value" } };
}
Also used : com.linkedin.restli.examples.greetings.api.myRecord(com.linkedin.restli.examples.greetings.api.myRecord) MyItemArray(com.linkedin.restli.examples.greetings.api.MyItemArray) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) com.linkedin.restli.examples.greetings.api.myItem(com.linkedin.restli.examples.greetings.api.myItem) GreetingMap(com.linkedin.restli.examples.greetings.api.GreetingMap) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo)

Example 83 with Greeting

use of com.linkedin.restli.examples.greetings.api.Greeting in project rest.li by linkedin.

the class TestRestLiValidation method updateSuccesses.

public static Object[] updateSuccesses() {
    ValidationDemo.UnionFieldWithInlineRecord unionField = new ValidationDemo.UnionFieldWithInlineRecord();
    unionField.setMyRecord(new myRecord().setFoo1(1));
    ValidationDemo validationDemo1 = new ValidationDemo().setStringA("aaa").setStringB("bbb").setUnionFieldWithInlineRecord(unionField);
    ValidationDemo.UnionFieldWithInlineRecord unionField2 = new ValidationDemo.UnionFieldWithInlineRecord();
    unionField2.setMyEnum(myEnum.BARBAR);
    MyItemArray array = new MyItemArray();
    array.add(new myItem().setBar1("BAR1").setBar2("BAR2"));
    array.add(new myItem().setBar1("BAR11").setBar2("BAR22"));
    GreetingMap map = new GreetingMap();
    map.put("key1", new Greeting().setId(1).setMessage("msg").setTone(Tone.FRIENDLY));
    return new Object[] { // All required fields have to be present, regardless of ReadOnly or CreateOnly annotations
    validationDemo1, new ValidationDemo().setStringA("aaa").setStringB("bbb").setUnionFieldWithInlineRecord(unionField2).setIntA(1234).setIntB(5678).setArrayWithInlineRecord(array).setMapWithTyperefs(map).setValidationDemoNext(validationDemo1) };
}
Also used : com.linkedin.restli.examples.greetings.api.myRecord(com.linkedin.restli.examples.greetings.api.myRecord) MyItemArray(com.linkedin.restli.examples.greetings.api.MyItemArray) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) com.linkedin.restli.examples.greetings.api.myItem(com.linkedin.restli.examples.greetings.api.myItem) GreetingMap(com.linkedin.restli.examples.greetings.api.GreetingMap) ValidationDemo(com.linkedin.restli.examples.greetings.api.ValidationDemo)

Example 84 with Greeting

use of com.linkedin.restli.examples.greetings.api.Greeting in project rest.li by linkedin.

the class TestReturnEntityWithCreate method testCreateIdEntity.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "newBuildersClientDataDataProvider")
public void testCreateIdEntity(RestClient restClient, String expectedContentType, CreateGreetingRequestBuilders builders) throws RemoteInvocationException {
    Greeting greeting = new Greeting();
    greeting.setMessage("second time!");
    greeting.setTone(Tone.FRIENDLY);
    CreateIdEntityRequest<Long, Greeting> createIdEntityRequest = builders.createAndGet().input(greeting).build();
    Response<IdEntityResponse<Long, Greeting>> response = restClient.sendRequest(createIdEntityRequest).getResponse();
    long id = response.getEntity().getId();
    @SuppressWarnings("deprecation") String stringId = response.getId();
    Assert.assertEquals(response.getHeader(RestConstants.HEADER_CONTENT_TYPE), expectedContentType);
    Assert.assertEquals(id, Long.parseLong(stringId));
    Assert.assertEquals("second time!", ((Greeting) response.getEntity().getEntity()).getMessage());
}
Also used : BatchCreateIdEntityResponse(com.linkedin.restli.common.BatchCreateIdEntityResponse) IdEntityResponse(com.linkedin.restli.common.IdEntityResponse) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) Test(org.testng.annotations.Test)

Example 85 with Greeting

use of com.linkedin.restli.examples.greetings.api.Greeting in project rest.li by linkedin.

the class TestReturnEntityWithCreate method testBatchCreateWithEntityWithError.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "newBuildersClientDataDataProvider")
public void testBatchCreateWithEntityWithError(RestClient restClient, String expectedContentType, CreateGreetingRequestBuilders builders) throws RemoteInvocationException {
    Greeting greeting = new Greeting();
    greeting.setMessage("good!");
    greeting.setTone(Tone.FRIENDLY);
    Greeting greeting2 = new Greeting();
    greeting2.setMessage("too much!");
    greeting2.setTone(Tone.FRIENDLY);
    List<Greeting> greetings = new ArrayList<Greeting>(Arrays.asList(greeting, greeting, greeting, greeting2));
    BatchCreateIdEntityRequest<Long, Greeting> batchCreateIdEntityRequest = builders.batchCreateAndGet().inputs(greetings).build();
    Response<BatchCreateIdEntityResponse<Long, Greeting>> response = restClient.sendRequest(batchCreateIdEntityRequest).getResponse();
    BatchCreateIdEntityResponse<Long, Greeting> entityResponses = response.getEntity();
    Assert.assertEquals(response.getHeader(RestConstants.HEADER_CONTENT_TYPE), expectedContentType);
    int numOfElem = 0;
    for (CreateIdEntityStatus<?, ?> singleResponse : entityResponses.getElements()) {
        if (numOfElem > 2) {
            Assert.assertTrue(singleResponse.hasError());
            Assert.assertEquals(singleResponse.getStatus().intValue(), HttpStatus.S_400_BAD_REQUEST.getCode());
            // More than 3 elements were sent, should trigger exception.
            Assert.assertEquals(singleResponse.getError().getMessage(), "exceed quota");
        } else {
            @SuppressWarnings("deprecation") String id = singleResponse.getId();
            Assert.assertNotNull(id);
            Greeting entity = (Greeting) singleResponse.getEntity();
            Assert.assertEquals(Tone.FRIENDLY, entity.getTone());
            Assert.assertEquals(singleResponse.getStatus().intValue(), HttpStatus.S_201_CREATED.getCode());
        }
        numOfElem++;
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) BatchCreateIdEntityResponse(com.linkedin.restli.common.BatchCreateIdEntityResponse) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Aggregations

Greeting (com.linkedin.restli.examples.greetings.api.Greeting)250 Test (org.testng.annotations.Test)195 CollectionResponse (com.linkedin.restli.common.CollectionResponse)59 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)33 EmptyRecord (com.linkedin.restli.common.EmptyRecord)33 RestLiResponseException (com.linkedin.restli.client.RestLiResponseException)20 CustomLong (com.linkedin.restli.examples.custom.types.CustomLong)20 ArrayList (java.util.ArrayList)19 HashMap (java.util.HashMap)18 BatchCreateIdResponse (com.linkedin.restli.common.BatchCreateIdResponse)12 EntityResponse (com.linkedin.restli.common.EntityResponse)12 BatchResponse (com.linkedin.restli.common.BatchResponse)11 RestLiIntegrationTest (com.linkedin.restli.examples.RestLiIntegrationTest)11 IdResponse (com.linkedin.restli.common.IdResponse)10 GreetingsRequestBuilders (com.linkedin.restli.examples.greetings.client.GreetingsRequestBuilders)10 GreetingsBuilders (com.linkedin.restli.examples.greetings.client.GreetingsBuilders)9 ConsistentHashKeyMapper (com.linkedin.d2.balancer.util.hashing.ConsistentHashKeyMapper)8 Response (com.linkedin.restli.client.Response)8 CreateIdStatus (com.linkedin.restli.common.CreateIdStatus)8 ErrorResponse (com.linkedin.restli.common.ErrorResponse)8