1
1
package org .springframework .cloud .sleuth .instrument .web ;
2
2
3
+ import static com .github .tomakehurst .wiremock .client .WireMock .aResponse ;
4
+ import static com .github .tomakehurst .wiremock .client .WireMock .get ;
5
+ import static com .github .tomakehurst .wiremock .client .WireMock .getRequestedFor ;
6
+ import static com .github .tomakehurst .wiremock .client .WireMock .matching ;
7
+ import static com .github .tomakehurst .wiremock .client .WireMock .urlMatching ;
8
+ import static java .util .concurrent .TimeUnit .SECONDS ;
9
+ import static junitparams .JUnitParamsRunner .$ ;
10
+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .asyncDispatch ;
11
+ import static org .springframework .test .web .servlet .result .MockMvcResultHandlers .print ;
12
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .request ;
13
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
14
+
3
15
import java .util .concurrent .Callable ;
4
16
import java .util .concurrent .ExecutionException ;
5
17
14
26
import org .springframework .cloud .sleuth .instrument .DefaultTestAutoConfiguration ;
15
27
import org .springframework .cloud .sleuth .instrument .web .common .AbstractMvcWiremockIntegrationTest ;
16
28
import org .springframework .cloud .sleuth .instrument .web .common .HttpMockServer ;
29
+ import org .springframework .context .annotation .Bean ;
17
30
import org .springframework .context .annotation .Import ;
18
31
import org .springframework .http .MediaType ;
19
32
import org .springframework .scheduling .annotation .EnableAsync ;
31
44
import junitparams .JUnitParamsRunner ;
32
45
import junitparams .Parameters ;
33
46
34
- import static com .github .tomakehurst .wiremock .client .WireMock .aResponse ;
35
- import static com .github .tomakehurst .wiremock .client .WireMock .get ;
36
- import static com .github .tomakehurst .wiremock .client .WireMock .getRequestedFor ;
37
- import static com .github .tomakehurst .wiremock .client .WireMock .matching ;
38
- import static com .github .tomakehurst .wiremock .client .WireMock .urlMatching ;
39
- import static java .util .concurrent .TimeUnit .SECONDS ;
40
- import static junitparams .JUnitParamsRunner .$ ;
41
- import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .asyncDispatch ;
42
- import static org .springframework .test .web .servlet .result .MockMvcResultHandlers .print ;
43
- import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .request ;
44
- import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
45
-
46
- @ SpringApplicationConfiguration (classes = {RestTemplateTraceAspectIntegrationTests .CorrelationIdAspectTestConfiguration .class })
47
+ @ SpringApplicationConfiguration (classes = {
48
+ RestTemplateTraceAspectIntegrationTests .CorrelationIdAspectTestConfiguration .class })
47
49
@ RunWith (JUnitParamsRunner .class )
48
- public class RestTemplateTraceAspectIntegrationTests extends AbstractMvcWiremockIntegrationTest {
50
+ public class RestTemplateTraceAspectIntegrationTests
51
+ extends AbstractMvcWiremockIntegrationTest {
49
52
50
- @ ClassRule public static final SpringClassRule SCR = new SpringClassRule ();
51
- @ Rule public final SpringMethodRule springMethodRule = new SpringMethodRule ();
53
+ @ ClassRule
54
+ public static final SpringClassRule SCR = new SpringClassRule ();
55
+ @ Rule
56
+ public final SpringMethodRule springMethodRule = new SpringMethodRule ();
52
57
53
- @ Before public void setupDefaultWireMockStubbing () {
58
+ @ Before
59
+ public void setupDefaultWireMockStubbing () {
54
60
stubInteraction (get (urlMatching (".*" )), aResponse ().withStatus (200 ));
55
61
}
56
62
57
63
@ Test
58
- public void should_set_span_data_on_headers_via_aspect_in_synchronous_call () throws Exception {
64
+ public void should_set_span_data_on_headers_via_aspect_in_synchronous_call ()
65
+ throws Exception {
59
66
whenARequestIsSentToASyncEndpoint ();
60
67
61
68
thenTraceIdHasBeenSetOnARequestHeader ();
62
69
}
63
70
64
71
@ Test
65
- public void should_set_span_data_on_headers_when_sending_a_request_via_async_rest_template () throws Exception {
72
+ public void should_set_span_data_on_headers_when_sending_a_request_via_async_rest_template ()
73
+ throws Exception {
66
74
whenARequestIsSentToAAsyncRestTemplateEndpoint ();
67
75
68
76
thenTraceIdHasBeenSetOnARequestHeader ();
69
77
}
70
78
71
79
@ Test
72
80
@ Parameters
73
- public void should_set_span_data_on_headers_via_aspect_in_asynchronous_call (String url ) throws Exception {
81
+ public void should_set_span_data_on_headers_via_aspect_in_asynchronous_call (
82
+ String url ) throws Exception {
74
83
whenARequestIsSentToAnAsyncEndpoint (url );
75
84
76
85
thenTraceIdHasBeenSetOnARequestHeader ();
@@ -81,39 +90,49 @@ public Object[] parametersForShould_set_span_data_on_headers_via_aspect_in_async
81
90
}
82
91
83
92
private void whenARequestIsSentToAAsyncRestTemplateEndpoint () throws Exception {
84
- this .mockMvc .perform (MockMvcRequestBuilders .get ("/asyncRestTemplate" ).accept (MediaType .TEXT_PLAIN )).andReturn ();
93
+ this .mockMvc .perform (MockMvcRequestBuilders .get ("/asyncRestTemplate" )
94
+ .accept (MediaType .TEXT_PLAIN )).andReturn ();
85
95
}
86
96
87
97
private void whenARequestIsSentToASyncEndpoint () throws Exception {
88
- this .mockMvc .perform (MockMvcRequestBuilders .get ("/syncPing" ).accept (MediaType .TEXT_PLAIN )).andReturn ();
98
+ this .mockMvc .perform (
99
+ MockMvcRequestBuilders .get ("/syncPing" ).accept (MediaType .TEXT_PLAIN ))
100
+ .andReturn ();
89
101
}
90
102
91
103
private void thenTraceIdHasBeenSetOnARequestHeader () {
92
- this .wireMock .verifyThat (getRequestedFor (urlMatching (".*" )).withHeader (Span .TRACE_ID_NAME , matching ("^(?!\\ s*$).+" )));
104
+ this .wireMock .verifyThat (getRequestedFor (urlMatching (".*" ))
105
+ .withHeader (Span .TRACE_ID_NAME , matching ("^(?!\\ s*$).+" )));
93
106
}
94
107
95
108
private void whenARequestIsSentToAnAsyncEndpoint (String url ) throws Exception {
96
- MvcResult mvcResult = this .mockMvc . perform ( MockMvcRequestBuilders . get ( url ). accept ( MediaType . TEXT_PLAIN ))
97
- .andExpect ( request (). asyncStarted ( ))
98
- .andReturn ();
109
+ MvcResult mvcResult = this .mockMvc
110
+ .perform ( MockMvcRequestBuilders . get ( url ). accept ( MediaType . TEXT_PLAIN ))
111
+ .andExpect ( request (). asyncStarted ()). andReturn ();
99
112
mvcResult .getAsyncResult (SECONDS .toMillis (2 ));
100
- this .mockMvc .perform (asyncDispatch (mvcResult )).
101
- andDo (print ()).
102
- andExpect (status ().isOk ());
113
+ this .mockMvc .perform (asyncDispatch (mvcResult )).andDo (print ())
114
+ .andExpect (status ().isOk ());
103
115
}
104
116
105
117
@ EnableAsync
106
118
@ DefaultTestAutoConfiguration
107
119
@ Import (AspectTestingController .class )
108
120
public static class CorrelationIdAspectTestConfiguration {
121
+ @ Bean
122
+ public RestTemplate restTemplate () {
123
+ return new RestTemplate ();
124
+ }
109
125
}
110
126
111
127
@ RestController
112
128
public static class AspectTestingController {
113
129
114
- @ Autowired HttpMockServer httpMockServer ;
115
- @ Autowired RestTemplate restTemplate ;
116
- @ Autowired AsyncRestTemplate asyncRestTemplate ;
130
+ @ Autowired
131
+ HttpMockServer httpMockServer ;
132
+ @ Autowired
133
+ RestTemplate restTemplate ;
134
+ @ Autowired
135
+ AsyncRestTemplate asyncRestTemplate ;
117
136
118
137
@ RequestMapping (value = "/asyncRestTemplate" , method = RequestMethod .GET , produces = MediaType .TEXT_PLAIN_VALUE )
119
138
public String asyncRestTemplate ()
@@ -147,13 +166,15 @@ public String call() throws Exception {
147
166
};
148
167
149
168
private String callWiremockAndReturnOk () {
150
- this .restTemplate .getForObject ("http://localhost:" + this .httpMockServer .port (), String .class );
169
+ this .restTemplate .getForObject (
170
+ "http://localhost:" + this .httpMockServer .port (), String .class );
151
171
return "OK" ;
152
172
}
153
173
154
174
private String callWiremockViaAsyncRestTemplateAndReturnOk ()
155
175
throws ExecutionException , InterruptedException {
156
- this .asyncRestTemplate .getForEntity ("http://localhost:" + this .httpMockServer .port (), String .class ).get ();
176
+ this .asyncRestTemplate .getForEntity (
177
+ "http://localhost:" + this .httpMockServer .port (), String .class ).get ();
157
178
return "OK" ;
158
179
}
159
180
}
0 commit comments