forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 3
/
MutinyTest.java
134 lines (117 loc) · 4.13 KB
/
MutinyTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package io.quarkus.it.resteasy.mutiny;
import static io.restassured.RestAssured.get;
import static org.hamcrest.CoreMatchers.is;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.sse.SseEventSource;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.subscription.UniEmitter;
@QuarkusTest
public class MutinyTest {
@Test
public void testHello() {
get("/mutiny/hello")
.then()
.body(is("hello"))
.statusCode(200);
}
@Test
public void testFail() {
get("/mutiny/fail")
.then()
.statusCode(500);
}
@Test
public void testResponse() {
get("/mutiny/response")
.then()
.body(is("hello"))
.statusCode(202);
}
@Test
public void testHelloAsMulti() {
get("/mutiny/hello/stream")
.then()
.contentType("application/json")
.body("[0]", is("he"))
.body("[1]", is("ll"))
.body("[2]", is("o"))
.statusCode(200);
}
@Test
public void testSerialization() {
get("/mutiny/pet")
.then()
.contentType("application/json")
.body("name", is("neo"))
.body("kind", is("rabbit"))
.statusCode(200);
}
@Test
public void testMultiWithSerialization() {
get("/mutiny/pet/stream")
.then()
.contentType("application/json")
.body("[0].name", is("neo"))
.body("[0].kind", is("rabbit"))
.body("[1].name", is("indy"))
.body("[1].kind", is("dog"))
.statusCode(200);
}
@Test
public void testSSE() {
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:" + RestAssured.port + "/mutiny/pets");
try (SseEventSource eventSource = SseEventSource.target(target).build()) {
Uni<List<Pet>> petList = Uni.createFrom().emitter(new Consumer<UniEmitter<? super List<Pet>>>() {
@Override
public void accept(UniEmitter<? super List<Pet>> uniEmitter) {
List<Pet> pets = new CopyOnWriteArrayList<>();
eventSource.register(event -> {
Pet pet = event.readData(Pet.class, MediaType.APPLICATION_JSON_TYPE);
pets.add(pet);
if (pets.size() == 5) {
uniEmitter.complete(pets);
}
}, ex -> {
uniEmitter.fail(new IllegalStateException("SSE failure", ex));
});
eventSource.open();
}
});
List<Pet> pets = petList.await().atMost(Duration.ofMinutes(1));
Assertions.assertEquals(5, pets.size());
Assertions.assertEquals("neo", pets.get(0).getName());
Assertions.assertEquals("indy", pets.get(1).getName());
Assertions.assertEquals("plume", pets.get(2).getName());
Assertions.assertEquals("titi", pets.get(3).getName());
Assertions.assertEquals("rex", pets.get(4).getName());
}
}
@Test
public void testClientReturningUni() {
get("/mutiny/client")
.then()
.body(is("hello"))
.statusCode(200);
}
@Test
public void testClientReturningUniOfPet() {
get("/mutiny/client/pet")
.then()
.contentType("application/json")
.body("name", is("neo"))
.body("kind", is("rabbit"))
.statusCode(200);
}
}