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();
}
}
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);
}
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());
}
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());
}
Aggregations