Skip to content

Commit bf8a6f6

Browse files
committedSep 12, 2017
PAYARA-1939 : SSE sample test with arquillian
1 parent b77d453 commit bf8a6f6

File tree

6 files changed

+146
-66
lines changed

6 files changed

+146
-66
lines changed
 

‎sse/producer/pom.xml

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,19 @@
1111
<packaging>war</packaging>
1212
<name>Java EE 8 Samples: Server Sent Events [Producer]</name>
1313
<dependencies>
14-
<dependency>
15-
<groupId>javax.ejb</groupId>
16-
<artifactId>javax.ejb-api</artifactId>
17-
<version>3.2</version>
18-
<scope>test</scope>
19-
<type>jar</type>
20-
</dependency>
14+
<!-- JSON-B API -->
15+
<dependency>
16+
<groupId>javax.json.bind</groupId>
17+
<artifactId>javax.json.bind-api</artifactId>
18+
<version>1.0.0-RC2</version>
19+
</dependency>
20+
21+
<!-- Yasson (JSON-B RI) -->
22+
<dependency>
23+
<groupId>org.eclipse</groupId>
24+
<artifactId>yasson</artifactId>
25+
<version>1.0.0-RC1</version>
26+
<scope>runtime</scope>
27+
</dependency>
2128
</dependencies>
2229
</project>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package org.javaee8.sse.data;
7+
8+
import java.util.Date;
9+
import java.util.UUID;
10+
import javax.json.bind.JsonbBuilder;
11+
12+
/**
13+
*
14+
* @author daniel
15+
*/
16+
public class EventData {
17+
18+
private Date time;
19+
private String id;
20+
private String comment;
21+
22+
public EventData() {
23+
super();
24+
}
25+
26+
public EventData(String comment) {
27+
super();
28+
this.setTime(new Date());
29+
this.setId(UUID.randomUUID().toString());
30+
this.setComment(comment);
31+
}
32+
33+
public Date getTime() {
34+
return time;
35+
}
36+
37+
public void setTime(Date time) {
38+
this.time = time;
39+
}
40+
41+
public String getId() {
42+
return id;
43+
}
44+
45+
public void setId(String id) {
46+
this.id = id;
47+
}
48+
49+
public String getComment() {
50+
return comment;
51+
}
52+
53+
public void setComment(String comment) {
54+
this.comment = comment;
55+
}
56+
57+
@Override
58+
public String toString() {
59+
return JsonbBuilder.create().toJson(this);
60+
}
61+
}

‎sse/producer/src/main/java/org/javaee8/sse/producer/SseResource.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import javax.annotation.PostConstruct;
44
import javax.inject.Singleton;
5+
import javax.json.bind.Jsonb;
6+
import javax.json.bind.JsonbBuilder;
57
import javax.ws.rs.GET;
68
import javax.ws.rs.Path;
79
import javax.ws.rs.Produces;
@@ -10,49 +12,48 @@
1012
import javax.ws.rs.sse.Sse;
1113
import javax.ws.rs.sse.SseBroadcaster;
1214
import javax.ws.rs.sse.SseEventSink;
15+
import org.javaee8.sse.data.EventData;
1316

1417
/**
1518
* @author Daniel Contreras
1619
*/
17-
@Path("/")
20+
@Path("sse")
1821
@Singleton
1922
public class SseResource {
20-
23+
2124
@Context
2225
private Sse sse;
2326

2427
private volatile SseBroadcaster sseBroadcaster;
2528

2629
@PostConstruct
2730
public void init() {
28-
System.out.println("1");
2931
this.sseBroadcaster = sse.newBroadcaster();
30-
System.out.println("2");
3132
}
3233

3334
@GET
3435
@Path("register")
3536
@Produces(MediaType.SERVER_SENT_EVENTS)
3637
public void register(@Context SseEventSink eventSink) {
37-
System.out.println("3");
38-
eventSink.send(sse.newEvent("welcome!"));
39-
System.out.println("4");
38+
39+
Jsonb jsonb = JsonbBuilder.create();
40+
41+
eventSink.send(sse.newEvent("INIT",new EventData("event:intialized").toString()));
42+
4043
sseBroadcaster.register(eventSink);
41-
System.out.println("5");
42-
eventSink.send(sse.newEvent("event1"));
43-
eventSink.send(sse.newEvent("event2"));
44-
eventSink.send(sse.newEvent("event3"));
4544

46-
for (int i = 0; i < 20; i++) {
45+
for (int i = 0; i < 5; i++) {
4746

48-
sseBroadcaster.broadcast(sse.newEvent("Repeated" + i));
47+
sseBroadcaster.broadcast(sse.newEvent("EVENT",new EventData("event:"+i).toString()));
4948

5049
try {
51-
Thread.sleep(1000);
50+
Thread.sleep(100);
5251
} catch (InterruptedException e) {
5352
e.printStackTrace();
5453
}
5554
}
55+
56+
eventSink.send(sse.newEvent("FINISH",new EventData("event:finished").toString()));
5657
}
57-
58+
5859
}

‎sse/producer/src/main/java/org/javaee8/sse/rest/RestApplication.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
*
1313
* @author daniel
1414
*/
15-
@ApplicationPath(value = "rest")
16-
public class RestApplication extends Application{
15+
@ApplicationPath("rest")
16+
public class RestApplication extends Application {
1717

18-
1918
}

‎sse/producer/src/main/webapp/index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
To change this license header, choose License Headers in Project Properties.
44
To change this template file, choose Tools | Templates
55
and open the template in the editor.
6+
7+
@author Daniel Contreras
68
-->
79
<html>
810
<head>
@@ -21,7 +23,7 @@
2123

2224
function start() {
2325

24-
var eventSource = new EventSource("rest/register");
26+
var eventSource = new EventSource("rest/sse/register");
2527
console.log(eventSource);
2628

2729
eventSource.onmessage = function (event) {

‎sse/producer/src/test/java/org/javaee8/sse/producer/SseResourceTest.java

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
import java.io.IOException;
66
import java.net.URL;
7+
import java.util.Arrays;
8+
import java.util.Date;
9+
import javax.json.bind.Jsonb;
10+
import javax.json.bind.JsonbBuilder;
711

812
import org.jboss.arquillian.container.test.api.Deployment;
913
import org.jboss.arquillian.container.test.api.RunAsClient;
@@ -18,75 +22,81 @@
1822
import javax.ws.rs.client.Client;
1923
import javax.ws.rs.client.ClientBuilder;
2024
import javax.ws.rs.client.WebTarget;
21-
import javax.ws.rs.core.Context;
2225
import javax.ws.rs.sse.SseEventSource;
26+
import static org.hamcrest.CoreMatchers.instanceOf;
27+
import org.javaee8.sse.data.EventData;
28+
import org.javaee8.sse.rest.RestApplication;
29+
import static org.junit.Assert.assertNotNull;
30+
import static org.junit.Assert.assertThat;
31+
import static org.junit.Assert.assertTrue;
2332

2433
/**
2534
* @author Daniel Contreras
2635
*/
27-
//@RunWith(Arquillian.class)
36+
@RunWith(Arquillian.class)
2837
public class SseResourceTest {
29-
/*
38+
3039
@ArquillianResource
3140
private URL base;
3241

3342
private Client sseClient;
34-
WebTarget target;
43+
private WebTarget target;
44+
45+
SseEventSource eventSource;
3546

36-
@Deployment
47+
@Deployment(testable = false)
3748
public static WebArchive createDeployment() {
3849
return create(WebArchive.class)
39-
.addClass(SseResource.class);
50+
.addClasses(RestApplication.class, SseResource.class, EventData.class, JsonbBuilder.class, Jsonb.class);
4051
}
4152

4253
@Before
4354
public void setup() {
44-
System.out.println("01");
4555
this.sseClient = ClientBuilder.newClient();
46-
System.out.println("02");
56+
this.target = this.sseClient.target(base + "rest/sse/register");
57+
eventSource = SseEventSource.target(target).build();
58+
System.out.println("SSE Event source created........");
4759
}
4860

4961
@After
5062
public void teardown() {
51-
63+
eventSource.close();
64+
System.out.println("Closed SSE Event source..");
65+
sseClient.close();
66+
System.out.println("Closed JAX-RS client..");
5267
}
5368

69+
String[] types = {"INIT", "EVENT", "FINISH"};
70+
5471
@Test
5572
@RunAsClient
56-
public void testClient() throws IOException {
57-
System.out.println("Client");
58-
System.out.println(base + "producer/register");
59-
System.out.println(this.sseClient.getConfiguration().toString());
60-
}
61-
62-
@Test
63-
public void testServer() throws IOException {
64-
System.out.println("Server");
65-
System.out.println(base + "producer/register");
66-
//this.sseClient.target(base + "producer/register");
67-
System.out.println(this.sseClient.getConfiguration().getProperties().toString());
68-
System.out.println(this.sseClient.getConfiguration().getRuntimeType().toString());
69-
System.out.println(this.sseClient.getConfiguration().getInstances().toArray().toString());
70-
//this.sseClient.getHostnameVerifier();
71-
72-
System.out.println(base + "producer/register");
73-
WebTarget target = sseClient.target(base + "producer/register");
74-
System.out.println("1");
75-
try (SseEventSource source = SseEventSource.target(target).build()) {
76-
System.out.println("2");
77-
source.register(System.out::println);
78-
System.out.println("3");
79-
source.open();
80-
System.out.println("5");
81-
Thread.sleep(500); // Consume events for just 500 ms
82-
73+
public void testSSE() throws IOException {
74+
75+
Jsonb jsonb = JsonbBuilder.create();
76+
77+
System.out.println("SSE Client triggered in thread " + Thread.currentThread().getName());
78+
try {
79+
eventSource.register(
80+
(sseEvent)
81+
-> {
82+
assertTrue(Arrays.asList(types).contains(sseEvent.getName()));
83+
assertNotNull(sseEvent.readData());
84+
EventData event = jsonb.fromJson(sseEvent.readData(), EventData.class);
85+
assertThat(event.getTime(), instanceOf(Date.class));
86+
assertNotNull(event.getId());
87+
assertTrue(event.getComment().contains("event:"));
88+
System.out.println("\nSSE Event received :: " + event.toString() +"\n");
89+
90+
},
91+
(e) -> e.printStackTrace());
92+
93+
eventSource.open();
94+
Thread.sleep(1500);
8395
} catch (Exception e) {
84-
85-
System.out.println("error!!!");
96+
System.out.println("Error on SSE Test");
8697
System.out.println(e.getMessage());
87-
8898
}
8999

90100
}
91-
*/
101+
92102
}

0 commit comments

Comments
 (0)