Search in sources :

Example 6 with OutboundSseEvent

use of javax.ws.rs.sse.OutboundSseEvent in project cxf by apache.

the class OutboundSseEventImplTest method testDefaultMediaType.

/**
 * A user should not need to specify a media type when creating an outbound event. The default
 * should be <code>MediaType.SERVER_SENT_EVENTS_TYPE</code>.
 */
@Test
public void testDefaultMediaType() {
    Sse sse = new SseImpl();
    // test newEvent(data)
    OutboundSseEvent event = sse.newEvent("myData");
    assertNull(event.getName());
    assertEquals("myData", event.getData());
    assertEquals(MediaType.TEXT_PLAIN_TYPE, event.getMediaType());
    // test newEvent(name, data)
    event = sse.newEvent("myName", "myData2");
    assertEquals("myName", event.getName());
    assertEquals("myData2", event.getData());
    assertEquals(MediaType.TEXT_PLAIN_TYPE, event.getMediaType());
    // test newEventBuilder()...build()
    event = sse.newEventBuilder().comment("myComment").data("myData3").build();
    assertEquals("myComment", event.getComment());
    assertEquals("myData3", event.getData());
    assertEquals(MediaType.TEXT_PLAIN_TYPE, event.getMediaType());
}
Also used : OutboundSseEvent(javax.ws.rs.sse.OutboundSseEvent) Sse(javax.ws.rs.sse.Sse) Test(org.junit.Test)

Example 7 with OutboundSseEvent

use of javax.ws.rs.sse.OutboundSseEvent in project cxf by apache.

the class OutboundSseEventImplTest method testBuilderAPIs.

/**
 * Test that event built by the builder contains all of the data passed in
 * to it.
 */
@Test
public void testBuilderAPIs() {
    SseImpl sse = new SseImpl();
    OutboundSseEvent.Builder builder = sse.newEventBuilder();
    builder.comment("myComment");
    builder.data(new TestDataImpl("dataNoSpecifiedType"));
    builder.id("id");
    builder.mediaType(MediaType.APPLICATION_JSON_TYPE);
    builder.name("name");
    builder.reconnectDelay(5000);
    OutboundSseEvent event = builder.build();
    assertEquals("myComment", event.getComment());
    assertEquals(TestDataImpl.class, event.getType());
    assertTrue(event.getData() instanceof TestDataImpl);
    assertEquals("dataNoSpecifiedType", ((TestDataImpl) event.getData()).getData().get(0));
    assertEquals("id", event.getId());
    assertEquals(MediaType.APPLICATION_JSON_TYPE, event.getMediaType());
    assertEquals("name", event.getName());
    assertEquals(5000, event.getReconnectDelay());
    // now reuse the builder to build a new event
    builder.comment("myComment2");
    builder.data(TestData.class, new TestDataImpl("data1", "data2"));
    builder.id("id2");
    builder.mediaType(MediaType.TEXT_PLAIN_TYPE);
    builder.name("name2");
    builder.reconnectDelay(9000);
    event = builder.build();
    assertEquals("myComment2", event.getComment());
    assertEquals(new TestDataImpl("data1", "data2"), event.getData());
    assertEquals(TestData.class, event.getType());
    assertEquals("id2", event.getId());
    assertEquals(MediaType.TEXT_PLAIN_TYPE, event.getMediaType());
    assertEquals("name2", event.getName());
    assertEquals(9000, event.getReconnectDelay());
}
Also used : OutboundSseEvent(javax.ws.rs.sse.OutboundSseEvent) Test(org.junit.Test)

Example 8 with OutboundSseEvent

use of javax.ws.rs.sse.OutboundSseEvent in project cxf by apache.

the class OutboundSseEventImplTest method testDefaultClass.

/**
 * A user should not need to specify the type of data being sent in an outbound
 * event. In that case the OutboundSseEvent should use the data object's type. Other
 * types may be specified, but the default (if not specified by the user) should be
 * the return value from the object's <code>getClass()</code> method.
 */
@Test
public void testDefaultClass() {
    Sse sse = new SseImpl();
    // test newEvent(string)
    OutboundSseEvent event = sse.newEvent("myData");
    assertNull(event.getName());
    assertEquals("myData", event.getData());
    assertEquals(String.class, event.getType());
    // test newEvent(name, data)
    event = sse.newEvent("myName", "myData2");
    assertEquals("myName", event.getName());
    assertEquals("myData2", event.getData());
    assertEquals(String.class, event.getType());
    // test newEventBuilder()...build()
    event = sse.newEventBuilder().comment("myComment").data("myData3").build();
    assertEquals("myComment", event.getComment());
    assertEquals("myData3", event.getData());
    assertEquals(String.class, event.getType());
    // test that object's class is re-enabled when calling different signatures of the data method
    OutboundSseEvent.Builder builder = sse.newEventBuilder();
    builder.data(TestData.class, new TestDataImpl("1", "2"));
    event = builder.build();
    assertEquals(TestData.class, event.getType());
    builder.data("myString");
    event = builder.build();
    assertEquals(String.class, event.getType());
    // same thing, but don't build in between calls to data
    event = sse.newEventBuilder().data(TestDataImpl.class, new TestDataImpl("3")).data("anotherString").build();
    assertEquals(String.class, event.getType());
    assertEquals("anotherString", event.getData());
}
Also used : OutboundSseEvent(javax.ws.rs.sse.OutboundSseEvent) Sse(javax.ws.rs.sse.Sse) Test(org.junit.Test)

Aggregations

OutboundSseEvent (javax.ws.rs.sse.OutboundSseEvent)8 Test (org.junit.Test)7 CountDownLatch (java.util.concurrent.CountDownLatch)4 Flow (javax.ws.rs.Flow)4 Sse (javax.ws.rs.sse.Sse)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 OutboundSseEventBodyWriter (org.apache.cxf.jaxrs.sse.OutboundSseEventBodyWriter)1 AtmosphereResource (org.atmosphere.cpr.AtmosphereResource)1 Broadcaster (org.atmosphere.cpr.Broadcaster)1