Search in sources :

Example 1 with EventMessage

use of io.syndesis.common.model.EventMessage in project syndesis by syndesisio.

the class EventsITCase method wsEventsWithToken.

@Test
public void wsEventsWithToken() throws Exception {
    OkHttpClient client = new OkHttpClient();
    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();
    String uriTemplate = EventBusToWebSocket.DEFAULT_PATH + "/" + uuid;
    String url = resolveURI(uriTemplate).toString().replaceFirst("^http", "ws");
    Request request = new Request.Builder().url(url).addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + tokenRule.validToken()).build();
    // lets setup an event handler that we can inspect events on..
    WebSocketListener listener = recorder(mock(WebSocketListener.class), WebSocketListener.class);
    List<Recordings.Invocation> invocations = recordedInvocations(listener);
    CountDownLatch countDownLatch = resetRecorderLatch(listener, 2);
    WebSocket ws = client.newWebSocket(request, listener);
    // We auto get a message letting us know we connected.
    assertThat(countDownLatch.await(1000, TimeUnit.SECONDS)).isTrue();
    assertThat(invocations.get(0).getMethod().getName()).isEqualTo("onOpen");
    assertThat(invocations.get(1).getMethod().getName()).isEqualTo("onMessage");
    assertThat(invocations.get(1).getArgs()[1]).isEqualTo(EventMessage.of("message", "connected").toJson());
    // ///////////////////////////////////////////////////
    // Test that we get notified of created entities
    // ///////////////////////////////////////////////////
    invocations.clear();
    countDownLatch = resetRecorderLatch(listener, 1);
    Integration integration = new Integration.Builder().id("1002").name("test").build();
    post("/api/v1/integrations", integration, Integration.class);
    assertThat(countDownLatch.await(1000, TimeUnit.SECONDS)).isTrue();
    assertThat(invocations.get(0).getMethod().getName()).isEqualTo("onMessage");
    assertThat(invocations.get(0).getArgs()[1]).isEqualTo(EventMessage.of("change-event", ChangeEvent.of("created", "integration", "1002").toJson()).toJson());
    ws.close(1000, "closing");
}
Also used : WebSocketListener(okhttp3.WebSocketListener) Integration(io.syndesis.common.model.integration.Integration) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) CountDownLatch(java.util.concurrent.CountDownLatch) WebSocket(okhttp3.WebSocket) EventMessage(io.syndesis.common.model.EventMessage) Test(org.junit.Test)

Example 2 with EventMessage

use of io.syndesis.common.model.EventMessage 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());
    }
}
Also used : Integration(io.syndesis.common.model.integration.Integration) MessageEvent(com.launchdarkly.eventsource.MessageEvent) EventHandler(com.launchdarkly.eventsource.EventHandler) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) EventSource(com.launchdarkly.eventsource.EventSource) EventMessage(io.syndesis.common.model.EventMessage) Test(org.junit.Test)

Aggregations

EventMessage (io.syndesis.common.model.EventMessage)2 Integration (io.syndesis.common.model.integration.Integration)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Test (org.junit.Test)2 EventHandler (com.launchdarkly.eventsource.EventHandler)1 EventSource (com.launchdarkly.eventsource.EventSource)1 MessageEvent (com.launchdarkly.eventsource.MessageEvent)1 URI (java.net.URI)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 WebSocket (okhttp3.WebSocket)1 WebSocketListener (okhttp3.WebSocketListener)1