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