Search in sources :

Example 6 with GreetingsBuilders

use of com.linkedin.restli.examples.greetings.client.GreetingsBuilders in project rest.li by linkedin.

the class TestResponseCompression method testAcceptEncodingConfiguration.

@Test(dataProvider = "encodingsData")
public void testAcceptEncodingConfiguration(String responseContentEncodings, String expectedAcceptEncoding, String expectedContentEncoding) throws RemoteInvocationException {
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(HttpClientFactory.HTTP_RESPONSE_CONTENT_ENCODINGS, responseContentEncodings);
    properties.put(HttpClientFactory.HTTP_USE_RESPONSE_COMPRESSION, "true");
    Client client = newTransportClient(properties);
    Long[] ids = new Long[100];
    for (int i = 0; i < ids.length; i++) {
        ids[i] = (long) i;
    }
    Request<BatchResponse<Greeting>> request = new GreetingsBuilders().batchGet().ids(Arrays.asList(ids)).setHeader(EXPECTED_ACCEPT_ENCODING, expectedAcceptEncoding).build();
    RestClient restClient = new RestClient(client, FILTERS_URI_PREFIX);
    Response<BatchResponse<Greeting>> response = restClient.sendRequest(request).getResponse();
    Assert.assertEquals(response.getHeader(TestCompressionServer.CONTENT_ENCODING_SAVED), expectedContentEncoding);
}
Also used : HashMap(java.util.HashMap) BatchResponse(com.linkedin.restli.common.BatchResponse) RestClient(com.linkedin.restli.client.RestClient) GreetingsBuilders(com.linkedin.restli.examples.greetings.client.GreetingsBuilders) RestClient(com.linkedin.restli.client.RestClient) Client(com.linkedin.r2.transport.common.Client) Test(org.testng.annotations.Test)

Example 7 with GreetingsBuilders

use of com.linkedin.restli.examples.greetings.client.GreetingsBuilders in project rest.li by linkedin.

the class TestResponseCompression method testResponseCompression.

@Test(dataProvider = "requestData")
public void testResponseCompression(Boolean useResponseCompression, CompressionConfig responseCompressionConfig, RestliRequestOptions restliRequestOptions, int idCount, String expectedAcceptEncoding, String expectedCompressionThreshold, boolean responseShouldBeCompressed) throws RemoteInvocationException, CloneNotSupportedException {
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("R2 Netty Scheduler"));
    Map<String, CompressionConfig> responseCompressionConfigs = new HashMap<String, CompressionConfig>();
    if (responseCompressionConfig != null) {
        responseCompressionConfigs.put(SERVICE_NAME, responseCompressionConfig);
    }
    HttpClientFactory httpClientFactory = new HttpClientFactory(FilterChains.empty(), new NioEventLoopGroup(0, /* use default settings */
    new NamedThreadFactory("R2 Nio Event Loop")), true, executor, true, executor, false, AbstractJmxManager.NULL_JMX_MANAGER, Integer.MAX_VALUE, Collections.<String, CompressionConfig>emptyMap(), responseCompressionConfigs, true);
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(HttpClientFactory.HTTP_SERVICE_NAME, SERVICE_NAME);
    if (useResponseCompression != null) {
        properties.put(HttpClientFactory.HTTP_USE_RESPONSE_COMPRESSION, String.valueOf(useResponseCompression));
    }
    TransportClientAdapter clientAdapter1 = new TransportClientAdapter(httpClientFactory.getClient(properties));
    RestClient client = new RestClient(clientAdapter1, FILTERS_URI_PREFIX);
    Long[] ids = new Long[idCount];
    for (int i = 0; i < ids.length; i++) {
        ids[i] = (long) i;
    }
    BatchGetRequestBuilder<Long, Greeting> builder = new GreetingsBuilders(restliRequestOptions).batchGet().ids(Arrays.asList(ids)).setHeader(EXPECTED_ACCEPT_ENCODING, expectedAcceptEncoding);
    if (expectedCompressionThreshold != null) {
        builder.setHeader(EXPECTED_COMPRESSION_THRESHOLD, expectedCompressionThreshold);
    }
    Request<BatchResponse<Greeting>> request = builder.build();
    Response<BatchResponse<Greeting>> response = client.sendRequest(request).getResponse();
    if (responseShouldBeCompressed) {
        Assert.assertEquals(response.getHeader(TestCompressionServer.CONTENT_ENCODING_SAVED), EncodingType.GZIP.getHttpName());
    } else {
        Assert.assertNull(response.getHeader(TestCompressionServer.CONTENT_ENCODING_SAVED));
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) HashMap(java.util.HashMap) NamedThreadFactory(com.linkedin.r2.util.NamedThreadFactory) BatchResponse(com.linkedin.restli.common.BatchResponse) RestClient(com.linkedin.restli.client.RestClient) GreetingsBuilders(com.linkedin.restli.examples.greetings.client.GreetingsBuilders) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) CompressionConfig(com.linkedin.r2.filter.CompressionConfig) Test(org.testng.annotations.Test)

Example 8 with GreetingsBuilders

use of com.linkedin.restli.examples.greetings.client.GreetingsBuilders in project rest.li by linkedin.

the class TestCompressionServer method clientsCompressedResponsesBuilderDataProvider.

//Returns a combination of all possible request/response compression combinations
@DataProvider(name = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "clientsCompressedResponsesBuilderDataProvider")
public Object[][] clientsCompressedResponsesBuilderDataProvider() {
    // sample compression operation config
    String[] compressionOperations = { "*", "action:*", "finder:*", "finder:search", "get, batch_get, get_all", "get, batch_get, get_all, batch_create, batch_update, batch_partial_update" };
    int entries = compressionOperations.length;
    Object[][] result = new Object[entries * 4][];
    int index = entries * 4 - 1;
    for (String operation : compressionOperations) {
        Map<String, String> clientProperties = new HashMap<String, String>();
        clientProperties.put(HttpClientFactory.HTTP_RESPONSE_COMPRESSION_OPERATIONS, operation);
        RestClient client = new RestClient(newTransportClient(clientProperties), URI_PREFIX);
        result[index--] = new Object[] { client, operation, new RootBuilderWrapper<Long, Greeting>(new GreetingsBuilders()), AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion() };
        result[index--] = new Object[] { client, operation, new RootBuilderWrapper<Long, Greeting>(new GreetingsBuilders(TestConstants.FORCE_USE_NEXT_OPTIONS)), AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion() };
        result[index--] = new Object[] { client, operation, new RootBuilderWrapper<Long, Greeting>(new GreetingsRequestBuilders()), AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion() };
        result[index--] = new Object[] { client, operation, new RootBuilderWrapper<Long, Greeting>(new GreetingsRequestBuilders(TestConstants.FORCE_USE_NEXT_OPTIONS)), AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion() };
    }
    return result;
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) HashMap(java.util.HashMap) RestClient(com.linkedin.restli.client.RestClient) GreetingsRequestBuilders(com.linkedin.restli.examples.greetings.client.GreetingsRequestBuilders) GreetingsBuilders(com.linkedin.restli.examples.greetings.client.GreetingsBuilders) DataProvider(org.testng.annotations.DataProvider)

Example 9 with GreetingsBuilders

use of com.linkedin.restli.examples.greetings.client.GreetingsBuilders in project rest.li by linkedin.

the class TestCompressionServer method getOldCookbookBatchGetResult.

private Greeting getOldCookbookBatchGetResult(RestClient client, RestliRequestOptions requestOptions) throws RemoteInvocationException {
    Request<BatchResponse<Greeting>> request = new GreetingsBuilders(requestOptions).batchGet().ids(1L).build();
    ResponseFuture<BatchResponse<Greeting>> future = client.sendRequest(request);
    Response<BatchResponse<Greeting>> greetingResponse = future.getResponse();
    checkContentEncodingHeaderIsAbsent(greetingResponse);
    return greetingResponse.getEntity().getResults().get("1");
}
Also used : GreetingsBuilders(com.linkedin.restli.examples.greetings.client.GreetingsBuilders) BatchResponse(com.linkedin.restli.common.BatchResponse)

Example 10 with GreetingsBuilders

use of com.linkedin.restli.examples.greetings.client.GreetingsBuilders in project rest.li by linkedin.

the class TestGreetingClientContentTypes method testCreate.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "buildersClientDataDataProvider")
public void testCreate(RestClient restClient, RestliRequestOptions requestOptions) throws RemoteInvocationException {
    Greeting greeting = new Greeting();
    greeting.setMessage("Hello there!");
    greeting.setTone(Tone.FRIENDLY);
    final GreetingsBuilders builders = new GreetingsBuilders(requestOptions);
    Request<EmptyRecord> createRequest = builders.create().input(greeting).build();
    Response<EmptyRecord> response = restClient.sendRequest(createRequest).getResponse();
    Assert.assertNull(response.getHeader(RestConstants.HEADER_CONTENT_TYPE));
    @SuppressWarnings("unchecked") CreateResponse<Long> createResponse = (CreateResponse<Long>) response.getEntity();
    long id = createResponse.getId();
    @SuppressWarnings("deprecation") String stringId = response.getId();
    Assert.assertEquals(id, Long.parseLong(stringId));
    Request<Greeting> getRequest = builders.get().id(id).build();
    Response<Greeting> getResponse = restClient.sendRequest(getRequest).getResponse();
    Greeting responseGreeting = getResponse.getEntity();
    Assert.assertEquals(responseGreeting.getMessage(), greeting.getMessage());
    Assert.assertEquals(responseGreeting.getTone(), greeting.getTone());
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) EmptyRecord(com.linkedin.restli.common.EmptyRecord) CreateResponse(com.linkedin.restli.client.response.CreateResponse) GreetingsBuilders(com.linkedin.restli.examples.greetings.client.GreetingsBuilders) Test(org.testng.annotations.Test)

Aggregations

GreetingsBuilders (com.linkedin.restli.examples.greetings.client.GreetingsBuilders)11 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)9 Test (org.testng.annotations.Test)9 BatchResponse (com.linkedin.restli.common.BatchResponse)5 RestClient (com.linkedin.restli.client.RestClient)3 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)3 GreetingsRequestBuilders (com.linkedin.restli.examples.greetings.client.GreetingsRequestBuilders)3 HashMap (java.util.HashMap)3 CollectionResponse (com.linkedin.restli.common.CollectionResponse)2 CreateStatus (com.linkedin.restli.common.CreateStatus)2 CompressionConfig (com.linkedin.r2.filter.CompressionConfig)1 Client (com.linkedin.r2.transport.common.Client)1 TransportClientAdapter (com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter)1 HttpClientFactory (com.linkedin.r2.transport.http.client.HttpClientFactory)1 NamedThreadFactory (com.linkedin.r2.util.NamedThreadFactory)1 CreateResponse (com.linkedin.restli.client.response.CreateResponse)1 EmptyRecord (com.linkedin.restli.common.EmptyRecord)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 DataProvider (org.testng.annotations.DataProvider)1