-
Notifications
You must be signed in to change notification settings - Fork 845
/
JaxRsTestResource.groovy
240 lines (212 loc) · 6.08 KB
/
JaxRsTestResource.groovy
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package test
import io.opentelemetry.instrumentation.test.base.HttpServerTest
import javax.ws.rs.ApplicationPath
import javax.ws.rs.GET
import javax.ws.rs.HeaderParam
import javax.ws.rs.Path
import javax.ws.rs.PathParam
import javax.ws.rs.QueryParam
import javax.ws.rs.container.AsyncResponse
import javax.ws.rs.container.Suspended
import javax.ws.rs.core.Application
import javax.ws.rs.core.Context
import javax.ws.rs.core.Response
import javax.ws.rs.core.UriInfo
import javax.ws.rs.ext.ExceptionMapper
import java.util.concurrent.CompletableFuture
import java.util.concurrent.CompletionStage
import java.util.concurrent.CyclicBarrier
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.CAPTURE_HEADERS
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.ERROR
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.EXCEPTION
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.INDEXED_CHILD
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.PATH_PARAM
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.QUERY_PARAM
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.REDIRECT
import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.SUCCESS
import static java.util.concurrent.TimeUnit.SECONDS
@Path("")
class JaxRsTestResource {
@Path("/success")
@GET
String success() {
HttpServerTest.controller(SUCCESS) {
SUCCESS.body
}
}
@Path("query")
@GET
String query_param(@QueryParam("some") String param) {
HttpServerTest.controller(QUERY_PARAM) {
"some=$param"
}
}
@Path("redirect")
@GET
Response redirect(@Context UriInfo uriInfo) {
HttpServerTest.controller(REDIRECT) {
Response.status(Response.Status.FOUND)
.location(uriInfo.relativize(new URI(REDIRECT.body)))
.build()
}
}
@Path("error-status")
@GET
Response error() {
HttpServerTest.controller(ERROR) {
Response.status(ERROR.status)
.entity(ERROR.body)
.build()
}
}
@Path("exception")
@GET
Object exception() {
HttpServerTest.controller(EXCEPTION) {
throw new Exception(EXCEPTION.body)
}
}
@Path("path/{id}/param")
@GET
String path_param(@PathParam("id") int id) {
HttpServerTest.controller(PATH_PARAM) {
id
}
}
@GET
@Path("captureHeaders")
Response capture_headers(@HeaderParam("X-Test-Request") String header) {
HttpServerTest.controller(CAPTURE_HEADERS) {
Response.status(CAPTURE_HEADERS.status)
.header("X-Test-Response", header)
.entity(CAPTURE_HEADERS.body)
.build()
}
}
@Path("/child")
@GET
void indexed_child(@Context UriInfo uriInfo, @Suspended AsyncResponse response) {
def parameters = uriInfo.queryParameters
CompletableFuture.runAsync({
HttpServerTest.controller(INDEXED_CHILD) {
INDEXED_CHILD.collectSpanAttributes { parameters.getFirst(it) }
response.resume("")
}
})
}
static final BARRIER = new CyclicBarrier(2)
@Path("async")
@GET
void asyncOp(@Suspended AsyncResponse response, @QueryParam("action") String action) {
CompletableFuture.runAsync({
// await for the test method to verify that there are no spans yet
BARRIER.await(1, SECONDS)
switch (action) {
case "succeed":
response.resume("success")
break
case "throw":
response.resume(new Exception("failure"))
break
case "cancel":
response.cancel()
break
default:
response.resume(new AssertionError((Object) ("invalid action value: " + action)))
break
}
})
}
@Path("async-completion-stage")
@GET
CompletionStage<String> jaxRs21Async(@QueryParam("action") String action) {
def result = new CompletableFuture<String>()
CompletableFuture.runAsync({
// await for the test method to verify that there are no spans yet
BARRIER.await(1, SECONDS)
switch (action) {
case "succeed":
result.complete("success")
break
case "throw":
result.completeExceptionally(new Exception("failure"))
break
default:
result.completeExceptionally(new AssertionError((Object) ("invalid action value: " + action)))
break
}
})
result
}
}
@Path("test-resource-super")
class JaxRsSuperClassTestResource extends JaxRsSuperClassTestResourceSuper {
}
class JaxRsSuperClassTestResourceSuper {
@GET
Object call() {
HttpServerTest.controller(SUCCESS) {
SUCCESS.body
}
}
}
class JaxRsInterfaceClassTestResource extends JaxRsInterfaceClassTestResourceSuper implements JaxRsInterface {
}
@Path("test-resource-interface")
interface JaxRsInterface {
@Path("call")
@GET
Object call()
}
class JaxRsInterfaceClassTestResourceSuper {
Object call() {
HttpServerTest.controller(SUCCESS) {
SUCCESS.body
}
}
}
@Path("test-sub-resource-locator")
class JaxRsSubResourceLocatorTestResource {
@Path("call")
Object call() {
HttpServerTest.controller(SUCCESS) {
return new SubResource()
}
}
}
class SubResource {
@Path("sub")
@GET
String call() {
HttpServerTest.controller(SUCCESS) {
SUCCESS.body
}
}
}
class JaxRsTestExceptionMapper implements ExceptionMapper<Exception> {
@Override
Response toResponse(Exception exception) {
return Response.status(500)
.entity(exception.message)
.build()
}
}
class JaxRsTestApplication extends Application {
@Override
Set<Class<?>> getClasses() {
def classes = new HashSet()
classes.add(JaxRsTestResource)
classes.add(JaxRsSuperClassTestResource)
classes.add(JaxRsInterfaceClassTestResource)
classes.add(JaxRsSubResourceLocatorTestResource)
classes.add(JaxRsTestExceptionMapper)
return classes
}
}
@ApplicationPath("/rest-app")
class JaxRsApplicationPathTestApplication extends JaxRsTestApplication {
}