Search in sources :

Example 1 with Subset

use of com.urbanairship.connect.client.model.request.Subset in project connect-java-library by urbanairship.

the class StreamConnectionTest method testRequestBodyWithSubset.

@Test
public void testRequestBodyWithSubset() throws Exception {
    final AtomicReference<String> body = new AtomicReference<>();
    final CountDownLatch received = new CountDownLatch(1);
    Answer httpAnswer = new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            HttpExchange exchange = (HttpExchange) invocation.getArguments()[0];
            int length = Integer.parseInt(exchange.getRequestHeaders().getFirst(HttpHeaders.CONTENT_LENGTH));
            byte[] bytes = new byte[length];
            exchange.getRequestBody().read(bytes);
            body.set(new String(bytes, UTF_8));
            exchange.sendResponseHeaders(200, 0L);
            received.countDown();
            return null;
        }
    };
    doAnswer(httpAnswer).when(serverHandler).handle(Matchers.<HttpExchange>any());
    Subset subset = Subset.createPartitionSubset().setCount(10).setSelection(0).build();
    StreamQueryDescriptor descriptor = subsetDescriptor(subset);
    stream = new StreamConnection(descriptor, http, connectionRetryStrategy, consumer, url);
    read(stream, Optional.<StartPosition>absent());
    assertTrue(received.await(10, TimeUnit.SECONDS));
    Gson gson = GsonUtil.getGson();
    JsonObject bodyObj = parser.parse(body.get()).getAsJsonObject();
    assertEquals(gson.toJson(subset), gson.toJson(bodyObj.get("subset")));
}
Also used : Subset(com.urbanairship.connect.client.model.request.Subset) HttpExchange(com.sun.net.httpserver.HttpExchange) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.anyString(org.mockito.Matchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) StreamQueryDescriptor(com.urbanairship.connect.client.model.StreamQueryDescriptor) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Aggregations

Gson (com.google.gson.Gson)1 JsonObject (com.google.gson.JsonObject)1 HttpExchange (com.sun.net.httpserver.HttpExchange)1 StreamQueryDescriptor (com.urbanairship.connect.client.model.StreamQueryDescriptor)1 Subset (com.urbanairship.connect.client.model.request.Subset)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1 Mockito.doAnswer (org.mockito.Mockito.doAnswer)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1