use of com.launchdarkly.eventsource.EventSource in project syndesis by syndesisio.
the class EventsITCase method sseEventsWithToken.
@Test
public void sseEventsWithToken() throws Exception {
ResponseEntity<EventMessage> r1 = post("/api/v1/event/reservations", null, EventMessage.class);
assertThat(r1.getBody().getEvent().get()).as("event").isEqualTo("uuid");
String uuid = (String) r1.getBody().getData().get();
assertThat(uuid).as("data").isNotNull();
URI uri = resolveURI(EventBusToServerSentEvents.DEFAULT_PATH + "/" + uuid);
// lets setup an event handler that we can inspect events on..
EventHandler handler = recorder(mock(EventHandler.class), EventHandler.class);
List<Recordings.Invocation> invocations = recordedInvocations(handler);
CountDownLatch countDownLatch = resetRecorderLatch(handler, 2);
try (EventSource eventSource = new EventSource.Builder(handler, uri).build()) {
eventSource.start();
assertThat(countDownLatch.await(1000, TimeUnit.SECONDS)).isTrue();
// workaround issues in EventSource
reorderEventSourceInvocations(invocations);
assertThat(invocations.get(0).getMethod().getName()).isEqualTo("onOpen");
// We auto get a message letting us know we connected.
assertThat(invocations.get(1).getMethod().getName()).isEqualTo("onMessage");
assertThat(invocations.get(1).getArgs()[0]).isEqualTo("message");
assertThat(((MessageEvent) invocations.get(1).getArgs()[1]).getData()).isEqualTo("connected");
// ///////////////////////////////////////////////////
// Test that we get notified of created entities
// ///////////////////////////////////////////////////
invocations.clear();
countDownLatch = resetRecorderLatch(handler, 1);
Integration integration = new Integration.Builder().id("1001").name("test").build();
post("/api/v1/integrations", integration, Integration.class);
assertThat(countDownLatch.await(1000, TimeUnit.SECONDS)).isTrue();
assertThat(invocations.get(0).getArgs()[0]).isEqualTo("change-event");
assertThat(((MessageEvent) invocations.get(0).getArgs()[1]).getData()).isEqualTo(ChangeEvent.of("created", "integration", "1001").toJson());
}
}
use of com.launchdarkly.eventsource.EventSource in project syndesis by syndesisio.
the class EventsITCase method sseEventsWithoutToken.
@Test
public void sseEventsWithoutToken() throws Exception {
// TODO: define an entity model for the response message:
// {"timestamp":1490099424012,"status":401,"error":"Unauthorized","message":"Unauthorized","path":"/api/v1/event/reservations"}
ResponseEntity<JsonNode> response = restTemplate().postForEntity("/api/v1/event/reservations", null, JsonNode.class);
assertThat(response.getStatusCode()).as("reservations post status code").isEqualTo(HttpStatus.FORBIDDEN);
// lets setup an event handler that we can inspect events on..
EventHandler handler = recorder(mock(EventHandler.class), EventHandler.class);
List<Recordings.Invocation> invocations = recordedInvocations(handler);
CountDownLatch countDownLatch = resetRecorderLatch(handler, 1);
// Using a random uuid should not work either.
String uuid = UUID.randomUUID().toString();
URI uri = resolveURI(EventBusToServerSentEvents.DEFAULT_PATH + "/" + uuid);
try (EventSource eventSource = new EventSource.Builder(handler, uri).build()) {
eventSource.start();
assertThat(countDownLatch.await(1000, TimeUnit.SECONDS)).isTrue();
assertThat(invocations.get(0).getMethod().getName()).isEqualTo("onError");
assertThat(invocations.get(0).getArgs()[0].toString()).isEqualTo("com.launchdarkly.eventsource.UnsuccessfulResponseException: Unsuccessful response code received from stream: 404");
}
}
Aggregations