Search in sources :

Example 1 with StreamQueryDescriptor

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

the class StreamConnectionTest method testRequestWithRelativeOffsetLatest.

@Test
public void testRequestWithRelativeOffsetLatest() 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());
    StreamQueryDescriptor descriptor = descriptor();
    stream = new StreamConnection(descriptor, http, connectionRetryStrategy, consumer, url);
    read(stream, Optional.of(StartPosition.relative(StartPosition.RelativePosition.LATEST)));
    assertTrue(received.await(10, TimeUnit.SECONDS));
    JsonObject bodyObj = parser.parse(body.get()).getAsJsonObject();
    assertEquals("LATEST", bodyObj.get("start").getAsString());
}
Also used : Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpExchange(com.sun.net.httpserver.HttpExchange) 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) Test(org.junit.Test)

Example 2 with StreamQueryDescriptor

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

the class StreamConnectionTest method testRequestBodyWithOffsetUpdates.

@Test
public void testRequestBodyWithOffsetUpdates() 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());
    StreamQueryDescriptor descriptor = StreamQueryDescriptor.newBuilder().setCreds(Creds.newBuilder().setAppKey(randomAlphabetic(22)).setToken(randomAlphabetic(5)).build()).enableOffsetUpdates().build();
    stream = new StreamConnection(descriptor, http, connectionRetryStrategy, consumer, url);
    read(stream, Optional.<StartPosition>absent());
    assertTrue(received.await(10, TimeUnit.SECONDS));
    JsonObject bodyObj = parser.parse(body.get()).getAsJsonObject();
    assertEquals(true, bodyObj.get("enable_offset_updates").getAsBoolean());
}
Also used : Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpExchange(com.sun.net.httpserver.HttpExchange) 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) Test(org.junit.Test)

Example 3 with StreamQueryDescriptor

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

the class StreamConnectionTest method testRequestBodyWithFilter.

@Test
public void testRequestBodyWithFilter() 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());
    DeviceFilter device1 = new DeviceFilter(DeviceFilterType.ANDROID_CHANNEL, "c8044c8a-d5fa-4e58-91d4-54d0f70b7409");
    DeviceFilter device2 = new DeviceFilter(DeviceFilterType.IOS_CHANNEL, "3d970087-600e-4bb6-8474-5857d438faaa");
    DeviceFilter device3 = new DeviceFilter(DeviceFilterType.NAMED_USER_ID, "cool user");
    NotificationFilter notification = new NotificationFilter(NotificationFilter.Type.GROUP_ID, "a30abf06-7878-4096-9535-b50ac0ad6e8e");
    Filter filter1 = Filter.newBuilder().setLatency(20000000).addDevices(device1, device2, device3).addDeviceTypes(DeviceType.ANDROID).addNotifications(notification).addEventTypes("OPEN").build();
    Filter filter2 = Filter.newBuilder().setLatency(400).addDeviceTypes(DeviceType.IOS).addEventTypes("TAG_CHANGE").build();
    StreamQueryDescriptor descriptor = filterDescriptor(filter1, filter2);
    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(new HashSet<>(Arrays.asList(filter1, filter2))), gson.toJson(bodyObj.get("filters")));
}
Also used : DeviceFilter(com.urbanairship.connect.client.model.request.filters.DeviceFilter) 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) DeviceFilter(com.urbanairship.connect.client.model.request.filters.DeviceFilter) Filter(com.urbanairship.connect.client.model.request.filters.Filter) NotificationFilter(com.urbanairship.connect.client.model.request.filters.NotificationFilter) InvocationOnMock(org.mockito.invocation.InvocationOnMock) NotificationFilter(com.urbanairship.connect.client.model.request.filters.NotificationFilter) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with StreamQueryDescriptor

use of com.urbanairship.connect.client.model.StreamQueryDescriptor 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)

Example 5 with StreamQueryDescriptor

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

the class StreamConnectionTest method testStream.

@Test
public void testStream() throws Exception {
    StreamQueryDescriptor descriptor = descriptor();
    final String line = randomAlphabetic(15);
    final AtomicReference<String> body = new AtomicReference<>();
    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);
            exchange.getResponseBody().write(line.getBytes(UTF_8));
            exchange.getResponseBody().write("\n".getBytes(UTF_8));
            exchange.close();
            return null;
        }
    };
    doAnswer(httpAnswer).when(serverHandler).handle(Matchers.<HttpExchange>any());
    final List<String> received = new ArrayList<>();
    Answer consumerAnswer = new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            String l = (String) invocation.getArguments()[0];
            received.add(l);
            return null;
        }
    };
    doAnswer(consumerAnswer).when(consumer).accept(anyString());
    stream = new StreamConnection(descriptor, http, connectionRetryStrategy, consumer, url);
    stream.read(Optional.<StartPosition>absent());
    assertEquals(1, received.size());
    assertEquals(line, received.get(0));
    JsonObject bodyObj = parser.parse(body.get()).getAsJsonObject();
    assertFalse(bodyObj.has("start"));
    assertFalse(bodyObj.has("resume_offset"));
}
Also used : HttpExchange(com.sun.net.httpserver.HttpExchange) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.anyString(org.mockito.Matchers.anyString) 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

StreamQueryDescriptor (com.urbanairship.connect.client.model.StreamQueryDescriptor)9 Test (org.junit.Test)9 Mockito.doAnswer (org.mockito.Mockito.doAnswer)9 InvocationOnMock (org.mockito.invocation.InvocationOnMock)9 Answer (org.mockito.stubbing.Answer)9 HttpExchange (com.sun.net.httpserver.HttpExchange)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 Matchers.anyString (org.mockito.Matchers.anyString)8 JsonObject (com.google.gson.JsonObject)7 Gson (com.google.gson.Gson)2 StartPosition (com.urbanairship.connect.client.model.request.StartPosition)1 Subset (com.urbanairship.connect.client.model.request.Subset)1 DeviceFilter (com.urbanairship.connect.client.model.request.filters.DeviceFilter)1 Filter (com.urbanairship.connect.client.model.request.filters.Filter)1 NotificationFilter (com.urbanairship.connect.client.model.request.filters.NotificationFilter)1 Consumer (com.urbanairship.connect.java8.Consumer)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1