Search in sources :

Example 6 with StreamQueryDescriptor

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

the class StreamConsumeTaskTest method testRun.

@Test
public void testRun() throws Exception {
    final AtomicReference<Consumer<String>> hook = hookStream();
    StreamQueryDescriptor descriptor = descriptor();
    BlockingQueue<String> queue = new LinkedBlockingQueue<>();
    task = task(descriptor, queue);
    final List<TestEvent> events = events(10);
    final CountDownLatch readDone = new CountDownLatch(1);
    final CountDownLatch assertionDone = new CountDownLatch(1);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            consume(hook.get(), events);
            readDone.countDown();
            assertionDone.await();
            return null;
        }
    }).doNothing().when(stream).read(Matchers.<Optional<StartPosition>>any());
    readThread.submit(task);
    try {
        assertTrue(readDone.await(10, TimeUnit.SECONDS));
        assertEquals(reduce(events), ImmutableList.copyOf(queue));
        verify(supplier).get(eq(descriptor), Matchers.<AsyncHttpClient>any(), Matchers.<Consumer<String>>any());
        verify(stream).read(Optional.<StartPosition>absent());
    } finally {
        assertionDone.countDown();
    }
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Consumer(com.urbanairship.connect.java8.Consumer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) StreamQueryDescriptor(com.urbanairship.connect.client.model.StreamQueryDescriptor) StartPosition(com.urbanairship.connect.client.model.request.StartPosition) Test(org.junit.Test)

Example 7 with StreamQueryDescriptor

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

the class StreamConnectionTest method testAuth.

@Test
public void testAuth() throws Exception {
    final AtomicReference<String> authorization = new AtomicReference<>();
    final AtomicReference<String> appKeyHeader = 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];
            authorization.set(exchange.getRequestHeaders().getFirst(HttpHeaders.AUTHORIZATION));
            appKeyHeader.set(exchange.getRequestHeaders().getFirst("X-UA-Appkey"));
            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.<StartPosition>absent());
    assertTrue(received.await(10, TimeUnit.SECONDS));
    assertTrue(authorization.get().toLowerCase().startsWith("bearer"));
    String token = authorization.get().substring("bearer ".length());
    assertEquals(descriptor.getCreds().getAppKey(), appKeyHeader.get());
    assertEquals(descriptor.getCreds().getToken(), token);
}
Also used : Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HttpExchange(com.sun.net.httpserver.HttpExchange) 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 8 with StreamQueryDescriptor

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

the class StreamConnectionTest method testRequestBodyWithOffset.

@Test
public void testRequestBodyWithOffset() 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());
    String offset = RandomStringUtils.randomAlphanumeric(32);
    StreamQueryDescriptor descriptor = descriptor();
    stream = new StreamConnection(descriptor, http, connectionRetryStrategy, consumer, url);
    read(stream, Optional.of(StartPosition.offset(offset)));
    assertTrue(received.await(10, TimeUnit.SECONDS));
    JsonObject bodyObj = parser.parse(body.get()).getAsJsonObject();
    assertEquals(offset, bodyObj.get("resume_offset").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 9 with StreamQueryDescriptor

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

the class StreamConnectionTest method testRequestWithRelativeOffsetEarliest.

@Test
public void testRequestWithRelativeOffsetEarliest() 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.EARLIEST)));
    assertTrue(received.await(10, TimeUnit.SECONDS));
    JsonObject bodyObj = parser.parse(body.get()).getAsJsonObject();
    assertEquals("EARLIEST", 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)

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