/
MpRestClientAsyncTestEndpoint.java
215 lines (174 loc) · 7.38 KB
/
MpRestClientAsyncTestEndpoint.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
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
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.eclipse.microprofile.telemetry.tracing.tck.async;
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
import static org.testng.Assert.assertEquals;
import java.net.URI;
import java.util.concurrent.CompletionStage;
import org.eclipse.microprofile.rest.client.RestClientBuilder;
import org.eclipse.microprofile.telemetry.tracing.tck.exporter.InMemorySpanExporter;
import org.testng.Assert;
import io.opentelemetry.api.baggage.Baggage;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Scope;
import jakarta.inject.Inject;
import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Application;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;
@Path("MpRestClientAsyncTestEndpoint")
public class MpRestClientAsyncTestEndpoint {
public static final String TEST_PASSED = "Test Passed";
public static final String TEST_VALUE = "TEST_VALUE";
@Inject
private InMemorySpanExporter spanExporter;
@GET
@Path("/mpclient")
public Response requestMpClient(@Context UriInfo uriInfo) {
Assert.assertNotNull(Span.current());
try (Scope s = Baggage.builder().put("foo", "bar").build().makeCurrent()) {
Baggage baggage = Baggage.current();
Assert.assertEquals("bar", baggage.getEntryValue("foo"));
String baseUrl = uriInfo.getAbsolutePath().toString().replace("/mpclient", "");
URI baseUri = null;
try {
baseUri = new URI(baseUrl);
} catch (Exception e) {
throw new RuntimeException(e);
}
Assert.assertNotNull(Span.current());
MpClientTwo mpClientTwo = RestClientBuilder.newBuilder()
.baseUri(baseUri)
.build(MpClientTwo.class);
String result = mpClientTwo.requestMpClient(TEST_VALUE);
Assert.assertEquals(TEST_PASSED, result);
}
return Response.ok(Span.current().getSpanContext().getTraceId()).build();
}
@GET
@Path("/mpclientasync")
public Response requestMpClientAsync(@Context UriInfo uriInfo) {
Assert.assertNotNull(Span.current());
try (Scope s = Baggage.builder().put("foo", "bar").build().makeCurrent()) {
Baggage baggage = Baggage.current();
Assert.assertEquals("bar", baggage.getEntryValue("foo"));
String baseUrl = uriInfo.getAbsolutePath().toString().replace("/mpclientasync", "");
URI baseUri = null;
try {
baseUri = new URI(baseUrl);
} catch (Exception e) {
throw new RuntimeException(e);
}
Assert.assertNotNull(Span.current());
MpClientTwoAsync mpClientTwo = RestClientBuilder.newBuilder()
.baseUri(baseUri)
.build(MpClientTwoAsync.class);
String result = mpClientTwo.requestMpClient(TEST_VALUE).toCompletableFuture().join();
Assert.assertEquals(TEST_PASSED, result);
}
return Response.ok(Span.current().getSpanContext().getTraceId()).build();
}
@GET
@Path("/mpclientasyncerror")
public Response requestMpClientAsyncError(@Context UriInfo uriInfo) {
Assert.assertNotNull(Span.current());
try (Scope s = Baggage.builder().put("foo", "bar").build().makeCurrent()) {
Baggage baggage = Baggage.current();
Assert.assertEquals("bar", baggage.getEntryValue("foo"));
String baseUrl = uriInfo.getAbsolutePath().toString().replace("/mpclientasyncerror", "");
URI baseUri = null;
try {
baseUri = new URI(baseUrl);
} catch (Exception e) {
throw new RuntimeException(e);
}
Assert.assertNotNull(Span.current());
MpClientTwoAsyncError mpClientTwoError = RestClientBuilder.newBuilder()
.baseUri(baseUri)
.build(MpClientTwoAsyncError.class);
String result = mpClientTwoError.requestMpClientError(TEST_VALUE)
.exceptionally(e -> "Exception:" + extractResponseStatus(e))
.toCompletableFuture()
.join();
assertEquals("Exception:400", result);
}
return Response.ok(Span.current().getSpanContext().getTraceId()).build();
}
/**
* Search the cause chain of an exception to find a WebApplicationException and extract the status code
*
* @param originalException
* the exception
* @return the status code
*/
private static int extractResponseStatus(Throwable originalException) {
Throwable t = originalException;
// Look up the cause chain to find a WebApplicationException and extract the response
while (t != null && !(t instanceof WebApplicationException)) {
t = t.getCause();
}
if (t == null) {
throw new RuntimeException("Non WebApplicationException thrown", originalException);
}
WebApplicationException webEx = (WebApplicationException) t;
return webEx.getResponse().getStatus();
}
@GET
@Path("requestMpClient")
public Response requestMpClient(@QueryParam("value") String value) {
Assert.assertNotNull(Span.current());
Assert.assertEquals(TEST_VALUE, value);
Baggage baggage = Baggage.current();
Assert.assertEquals("bar", baggage.getEntryValue("foo"));
return Response.ok(TEST_PASSED).build();
}
@GET
@Path("requestMpClientError")
public Response requestMpClientError(@QueryParam("value") String value) {
Assert.assertNotNull(Span.current());
Assert.assertEquals(TEST_VALUE, value);
Baggage baggage = Baggage.current();
Assert.assertEquals("bar", baggage.getEntryValue("foo"));
return Response.status(HTTP_BAD_REQUEST).build();
}
public interface MpClientTwo {
@GET
@Path("requestMpClient")
public String requestMpClient(@QueryParam("value") String value);
}
public interface MpClientTwoAsync {
@GET
@Path("requestMpClient")
public CompletionStage<String> requestMpClient(@QueryParam("value") String value);
}
public interface MpClientTwoAsyncError {
@GET
@Path("requestMpClientError")
public CompletionStage<String> requestMpClientError(@QueryParam("value") String value);
}
@ApplicationPath("/")
public static class RestApplication extends Application {
}
}