Search in sources :

Example 1 with NotificationFilter

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

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 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 HashSet (java.util.HashSet)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