-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerTest.java
152 lines (117 loc) · 5.05 KB
/
ServerTest.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package io.jd.framework.tests;
import io.jd.framework.BeanProvider;
import io.jd.framework.BeanProviderFactory;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublisher;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import static io.jd.framework.webapp.MediaType.APPLICATION_JSON;
import static java.net.http.HttpRequest.BodyPublishers.noBody;
import static java.net.http.HttpResponse.BodyHandlers.ofString;
import static org.eclipse.jetty.http.HttpHeader.ACCEPT;
import static org.eclipse.jetty.http.HttpHeader.CONTENT_TYPE;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
class ServerTest {
private static ServerContainer serverContainer = null;
private final HttpClient client = HttpClient.newHttpClient();
@BeforeAll
static void setupTest() throws Exception {
BeanProvider beanProvider = BeanProviderFactory.getInstance();
serverContainer = beanProvider.provide(ServerContainer.class);
serverContainer.start();
}
@AfterAll
static void cleanupTest() throws Exception {
serverContainer.stop();
}
private static URI path(String endpoint) {
return URI.create("http://localhost:%s".formatted(serverContainer.port()) + endpoint);
}
private static HttpRequest getRequest(String endpointPath) {
return buildWithAcceptHeader(endpointPath)
.GET()
.build();
}
private static HttpRequest postRequest(BodyPublisher publisher, String endpoint) {
return buildWithAcceptHeader(endpoint)
.POST(publisher)
.build();
}
private static HttpRequest putRequest(BodyPublisher publisher, String endpoint) {
return buildWithAcceptHeader(endpoint)
.PUT(publisher)
.build();
}
private static HttpRequest.Builder buildWithAcceptHeader(String endpoint) {
return HttpRequest.newBuilder(path(endpoint))
.header(ACCEPT.toString(), APPLICATION_JSON);
}
private static String getContentTypeValue(HttpResponse<String> response) {
return response.headers().firstValue(CONTENT_TYPE.toString()).get();
}
@Test
void testSlashInt() throws IOException, InterruptedException {
var request = getRequest("/int");
var response = client.send(request, ofString());
assertEquals(200, response.statusCode());
assertDoesNotThrow(() -> Integer.parseInt(response.body()));
assertEquals(
"application/json+framework;charset=utf-8",
getContentTypeValue(response)
);
}
@Test
void testSlashInt2() throws IOException, InterruptedException {
var request = getRequest("/int2");
var response = client.send(request, ofString());
assertEquals(200, response.statusCode());
assertDoesNotThrow(() -> Integer.parseInt(response.body()));
assertEquals("text/plain;charset=utf-8", getContentTypeValue(response));
}
@ParameterizedTest
@ValueSource(ints = {1, 2, 11, 100, 400})
void testSlashInt3(int value) throws IOException, InterruptedException {
var request = postRequest(BodyPublishers.ofString(String.valueOf(value)), "/int3");
var response = client.send(request, ofString());
assertEquals(response.statusCode(), 200);
assertEquals(Integer.parseInt(response.body()), value);
assertEquals(getContentTypeValue(response), APPLICATION_JSON);
}
@Test
void testSlashVoid() throws IOException, InterruptedException {
var request = postRequest(noBody(), "/void");
var response = client.send(request, ofString());
assertEquals(response.statusCode(), 204);
assertEquals(response.body(), "");
}
@Test
void testSlashReturnErrorResponse() throws IOException, InterruptedException {
var request = putRequest(noBody(), "/return-error-response");
var response = client.send(request, ofString());
assertEquals(500, response.statusCode());
assertEquals("", response.body());
}
@Test
void testSlashResource() throws IOException, InterruptedException {
var request = getRequest("/resource");
var response = client.send(request, ofString());
assertEquals(200, response.statusCode());
assertEquals("{\"key\":\"value\"}", response.body());
}
@Test
void testSlashError() throws IOException, InterruptedException {
var request = getRequest("/error");
var response = client.send(request, ofString());
assertEquals(500, response.statusCode());
assertEquals("{\"errorMessage\": \"Expected error message\"}", response.body());
}
}