18
18
19
19
import java .net .URI ;
20
20
21
- import javax .ws .rs .ApplicationPath ;
22
21
import javax .ws .rs .GET ;
23
22
import javax .ws .rs .Path ;
24
23
import javax .ws .rs .PathParam ;
25
24
26
25
import io .micrometer .core .instrument .MeterRegistry ;
26
+ import io .micrometer .core .instrument .Tag ;
27
27
import io .micrometer .core .instrument .Timer ;
28
+ import io .micrometer .jersey2 .server .DefaultJerseyTagsProvider ;
29
+ import io .micrometer .jersey2 .server .JerseyTagsProvider ;
28
30
import io .micrometer .jersey2 .server .MetricsApplicationEventListener ;
29
31
import org .glassfish .jersey .server .ResourceConfig ;
32
+ import org .glassfish .jersey .server .monitoring .RequestEvent ;
30
33
import org .junit .Test ;
31
34
32
35
import org .springframework .boot .actuate .autoconfigure .metrics .MetricsAutoConfiguration ;
33
36
import org .springframework .boot .actuate .autoconfigure .metrics .export .simple .SimpleMetricsExportAutoConfiguration ;
37
+ import org .springframework .boot .actuate .autoconfigure .metrics .test .MetricsRun ;
34
38
import org .springframework .boot .autoconfigure .AutoConfigurations ;
35
39
import org .springframework .boot .autoconfigure .jersey .JerseyAutoConfiguration ;
36
40
import org .springframework .boot .autoconfigure .jersey .ResourceConfigCustomizer ;
37
41
import org .springframework .boot .autoconfigure .web .servlet .ServletWebServerFactoryAutoConfiguration ;
38
42
import org .springframework .boot .test .context .FilteredClassLoader ;
39
43
import org .springframework .boot .test .context .assertj .AssertableWebApplicationContext ;
44
+ import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
40
45
import org .springframework .boot .test .context .runner .WebApplicationContextRunner ;
41
46
import org .springframework .boot .web .servlet .context .AnnotationConfigServletWebServerApplicationContext ;
42
47
import org .springframework .context .annotation .Bean ;
43
- import org .springframework .context .annotation .Configuration ;
44
48
import org .springframework .web .client .RestTemplate ;
45
49
46
50
import static org .assertj .core .api .Assertions .assertThat ;
49
53
* Tests for {@link JerseyServerMetricsAutoConfiguration}.
50
54
*
51
55
* @author Michael Weirauch
56
+ * @author Michael Simons
52
57
*/
53
58
public class JerseyServerMetricsAutoConfigurationTests {
54
59
55
- private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner (
60
+ private final ApplicationContextRunner contextRunner = new ApplicationContextRunner ()
61
+ .with (MetricsRun .simple ()).withConfiguration (
62
+ AutoConfigurations .of (JerseyServerMetricsAutoConfiguration .class ));
63
+
64
+ private final WebApplicationContextRunner webContextRunner = new WebApplicationContextRunner (
56
65
AnnotationConfigServletWebServerApplicationContext ::new )
57
66
.withConfiguration (
58
67
AutoConfigurations .of (JerseyAutoConfiguration .class ,
@@ -63,9 +72,30 @@ public class JerseyServerMetricsAutoConfigurationTests {
63
72
.withUserConfiguration (ResourceConfiguration .class )
64
73
.withPropertyValues ("server.port:0" );
65
74
75
+ @ Test
76
+ public void shouldOnlyBeActiveInWebApplicationContext () {
77
+ this .contextRunner .run ((context ) -> assertThat (context )
78
+ .doesNotHaveBean (ResourceConfigCustomizer .class ));
79
+ }
80
+
81
+ @ Test
82
+ public void shouldProvideAllNecessaryBeans () {
83
+ this .webContextRunner .run ((context ) -> assertThat (context )
84
+ .hasSingleBean (DefaultJerseyTagsProvider .class )
85
+ .hasSingleBean (ResourceConfigCustomizer .class ));
86
+ }
87
+
88
+ @ Test
89
+ public void shouldHonorExistingTagProvider () {
90
+ this .webContextRunner
91
+ .withUserConfiguration (CustomJerseyTagsProviderConfiguration .class )
92
+ .run ((context ) -> assertThat (context )
93
+ .hasSingleBean (CustomJerseyTagsProvider .class ));
94
+ }
95
+
66
96
@ Test
67
97
public void httpRequestsAreTimed () {
68
- this .contextRunner .run ((context ) -> {
98
+ this .webContextRunner .run ((context ) -> {
69
99
doRequest (context );
70
100
71
101
MeterRegistry registry = context .getBean (MeterRegistry .class );
@@ -77,7 +107,7 @@ public void httpRequestsAreTimed() {
77
107
78
108
@ Test
79
109
public void noHttpRequestsTimedWhenJerseyInstrumentationMissingFromClasspath () {
80
- this .contextRunner
110
+ this .webContextRunner
81
111
.withClassLoader (
82
112
new FilteredClassLoader (MetricsApplicationEventListener .class ))
83
113
.run ((context ) -> {
@@ -98,18 +128,11 @@ private static void doRequest(AssertableWebApplicationContext context) {
98
128
String .class );
99
129
}
100
130
101
- @ Configuration
102
- @ ApplicationPath ("/" )
103
131
static class ResourceConfiguration {
104
132
105
133
@ Bean
106
134
ResourceConfig resourceConfig () {
107
- return new ResourceConfig ();
108
- }
109
-
110
- @ Bean
111
- ResourceConfigCustomizer resourceConfigCustomizer () {
112
- return (config ) -> config .register (new TestResource ());
135
+ return new ResourceConfig ().register (new TestResource ());
113
136
}
114
137
115
138
@ Path ("/users" )
@@ -125,4 +148,27 @@ public String getUser(@PathParam("id") String id) {
125
148
126
149
}
127
150
151
+ static class CustomJerseyTagsProviderConfiguration {
152
+
153
+ @ Bean
154
+ JerseyTagsProvider customJerseyTagsProvider () {
155
+ return new CustomJerseyTagsProvider ();
156
+ }
157
+
158
+ }
159
+
160
+ static class CustomJerseyTagsProvider implements JerseyTagsProvider {
161
+
162
+ @ Override
163
+ public Iterable <Tag > httpRequestTags (RequestEvent event ) {
164
+ return null ;
165
+ }
166
+
167
+ @ Override
168
+ public Iterable <Tag > httpLongRequestTags (RequestEvent event ) {
169
+ return null ;
170
+ }
171
+
172
+ }
173
+
128
174
}
0 commit comments