2222import java .util .Map ;
2323import java .util .Objects ;
2424
25+ import reactor .netty .http .client .HttpClient ;
26+ import reactor .util .retry .Retry ;
27+ import reactor .util .retry .RetryBackoffSpec ;
28+
2529import org .springframework .boot .test .json .BasicJsonTester ;
2630import org .springframework .boot .test .system .CapturedOutput ;
2731import org .springframework .cloud .client .ServiceInstance ;
2832import org .springframework .cloud .client .discovery .DiscoveryClient ;
33+ import org .springframework .cloud .client .discovery .ReactiveDiscoveryClient ;
2934import org .springframework .cloud .kubernetes .commons .discovery .DefaultKubernetesServiceInstance ;
30- import org .springframework .core .ParameterizedTypeReference ;
3135import org .springframework .http .HttpMethod ;
3236import org .springframework .http .client .reactive .ReactorClientHttpConnector ;
3337import org .springframework .web .reactive .function .client .WebClient ;
34- import reactor .netty .http .client .HttpClient ;
35- import reactor .util .retry .Retry ;
36- import reactor .util .retry .RetryBackoffSpec ;
3738
38- import static org .testcontainers .shaded .org .awaitility .Awaitility .await ;
3939import static java .util .AbstractMap .SimpleEntry ;
4040import static java .util .Map .Entry ;
4141import static org .assertj .core .api .Assertions .assertThat ;
42+ import static org .testcontainers .shaded .org .awaitility .Awaitility .await ;
4243
4344/**
4445 * @author wind57
@@ -66,17 +67,16 @@ static void assertPodMetadata(DiscoveryClient discoveryClient) {
6667 .toList ()
6768 .get (0 );
6869 List <Entry <String , String >> podMetadataLabels = withCustomLabel .podMetadata ()
69- .get ("labels" )
70- .entrySet ()
71- .stream ()
72- .toList ();
70+ .get ("labels" )
71+ .entrySet ()
72+ .stream ()
73+ .toList ();
7374
7475 assertThat (withCustomLabel .getServiceId ()).isEqualTo ("busybox-service" );
7576 assertThat (withCustomLabel .getInstanceId ()).isNotNull ();
7677 assertThat (withCustomLabel .getHost ()).isNotNull ();
77- assertThat (withCustomLabel .getMetadata ()).isEqualTo (
78- Map .of ("k8s_namespace" , "default" , "type" , "ClusterIP" , "port.busybox-port" , "80" )
79- );
78+ assertThat (withCustomLabel .getMetadata ())
79+ .isEqualTo (Map .of ("k8s_namespace" , "default" , "type" , "ClusterIP" , "port.busybox-port" , "80" ));
8080 assertThat (podMetadataLabels ).contains (new SimpleEntry <>("my-label" , "my-value" ));
8181
8282 // if annotation are present, we got the one with annotations here
@@ -94,9 +94,8 @@ static void assertPodMetadata(DiscoveryClient discoveryClient) {
9494 assertThat (withCustomLabel .getServiceId ()).isEqualTo ("busybox-service" );
9595 assertThat (withCustomLabel .getInstanceId ()).isNotNull ();
9696 assertThat (withCustomLabel .getHost ()).isNotNull ();
97- assertThat (withCustomLabel .getMetadata ()).isEqualTo (
98- Map .of ("k8s_namespace" , "default" , "type" , "ClusterIP" , "port.busybox-port" , "80" )
99- );
97+ assertThat (withCustomLabel .getMetadata ())
98+ .isEqualTo (Map .of ("k8s_namespace" , "default" , "type" , "ClusterIP" , "port.busybox-port" , "80" ));
10099 assertThat (podMetadataAnnotations ).contains (new SimpleEntry <>("my-annotation" , "my-value" ));
101100 }
102101
@@ -111,8 +110,8 @@ static void assertAllServices(DiscoveryClient discoveryClient) {
111110 assertThat (externalNameInstance .getInstanceId ()).isNotNull ();
112111 assertThat (externalNameInstance .getHost ()).isEqualTo ("spring.io" );
113112 assertThat (externalNameInstance .getPort ()).isEqualTo (-1 );
114- assertThat (externalNameInstance .getMetadata ()). isEqualTo (
115- Map .of ("k8s_namespace" , "default" , "type" , "ExternalName" ));
113+ assertThat (externalNameInstance .getMetadata ())
114+ . isEqualTo ( Map .of ("k8s_namespace" , "default" , "type" , "ExternalName" ));
116115 assertThat (externalNameInstance .isSecure ()).isFalse ();
117116 assertThat (externalNameInstance .getUri ().toASCIIString ()).isEqualTo ("spring.io" );
118117 assertThat (externalNameInstance .getScheme ()).isEqualTo ("http" );
@@ -132,7 +131,6 @@ static void assertBlockingConfiguration(CapturedOutput output, int port) {
132131 waitForLogStatement (output , "publishing InstanceRegisteredEvent" );
133132 waitForLogStatement (output , "Discovery Client has been initialized" );
134133
135-
136134 WebClient healthClient = builder ().baseUrl ("http://localhost:" + port + "/actuator/health" ).build ();
137135
138136 String healthResult = healthClient .method (HttpMethod .GET )
@@ -145,9 +143,7 @@ static void assertBlockingConfiguration(CapturedOutput output, int port) {
145143 .extractingJsonPathStringValue ("$.components.discoveryComposite.status" )
146144 .isEqualTo ("UP" );
147145
148- assertThat (BASIC_JSON_TESTER .from (healthResult ))
149- .extractingJsonPathStringValue (BLOCKING_STATUS )
150- .isEqualTo ("UP" );
146+ assertThat (BASIC_JSON_TESTER .from (healthResult )).extractingJsonPathStringValue (BLOCKING_STATUS ).isEqualTo ("UP" );
151147
152148 assertThat (BASIC_JSON_TESTER .from (healthResult ))
153149 .extractingJsonPathArrayValue ("$.components.discoveryComposite.components.discoveryClient.details.services" )
@@ -191,13 +187,11 @@ static void testDefaultConfiguration(CapturedOutput output, int port) {
191187 .extractingJsonPathStringValue ("$.components.reactiveDiscoveryClients.status" )
192188 .isEqualTo ("UP" );
193189
194- assertThat (BASIC_JSON_TESTER .from (healthResult ))
195- .extractingJsonPathStringValue (
190+ assertThat (BASIC_JSON_TESTER .from (healthResult )).extractingJsonPathStringValue (
196191 "$.components.reactiveDiscoveryClients.components.['Fabric8 Kubernetes Reactive Discovery Client'].status" )
197192 .isEqualTo ("UP" );
198193
199- assertThat (BASIC_JSON_TESTER .from (healthResult ))
200- .extractingJsonPathArrayValue (
194+ assertThat (BASIC_JSON_TESTER .from (healthResult )).extractingJsonPathArrayValue (
201195 "$.components.reactiveDiscoveryClients.components.['Fabric8 Kubernetes Reactive Discovery Client'].details.services" )
202196 .containsExactlyInAnyOrder ("kubernetes" , "busybox-service" );
203197 }
@@ -210,7 +204,7 @@ static void testDefaultConfiguration(CapturedOutput output, int port) {
210204 * We assert for logs and call '/health' endpoint to see that reactive discovery
211205 * client was initialized.
212206 */
213- static void testReactiveConfiguration (CapturedOutput output , int port ) {
207+ static void testReactiveConfiguration (ReactiveDiscoveryClient discoveryClient , CapturedOutput output , int port ) {
214208
215209 waitForLogStatement (output , "Will publish InstanceRegisteredEvent from reactive implementation" );
216210 waitForLogStatement (output , "publishing InstanceRegisteredEvent" );
@@ -228,29 +222,18 @@ static void testReactiveConfiguration(CapturedOutput output, int port) {
228222 .extractingJsonPathStringValue ("$.components.reactiveDiscoveryClients.status" )
229223 .isEqualTo ("UP" );
230224
231- assertThat (BASIC_JSON_TESTER .from (healthResult ))
232- .extractingJsonPathStringValue (REACTIVE_STATUS )
233- .isEqualTo ("UP" );
225+ assertThat (BASIC_JSON_TESTER .from (healthResult )).extractingJsonPathStringValue (REACTIVE_STATUS ).isEqualTo ("UP" );
234226
235- assertThat (BASIC_JSON_TESTER .from (healthResult ))
236- .extractingJsonPathArrayValue (
227+ assertThat (BASIC_JSON_TESTER .from (healthResult )).extractingJsonPathArrayValue (
237228 "$.components.reactiveDiscoveryClients.components.['Fabric8 Kubernetes Reactive Discovery Client'].details.services" )
238229 .containsExactlyInAnyOrder ("kubernetes" , "busybox-service" );
239230
240231 assertThat (BASIC_JSON_TESTER .from (healthResult )).doesNotHaveJsonPath (BLOCKING_STATUS );
241232
242- // test for services also:
243- WebClient servicesClient = builder ().baseUrl ("http://localhost:" + port + "/reactive/services" ).build ();
233+ List <String > services = discoveryClient .getServices ().toStream ().toList ();
244234
245- List <String > servicesResult = servicesClient .method (HttpMethod .GET )
246- .retrieve ()
247- .bodyToMono (new ParameterizedTypeReference <List <String >>() {
248- })
249- .retryWhen (retrySpec ())
250- .block ();
251-
252- assertThat (servicesResult ).contains ("busybox-service" );
253- assertThat (servicesResult ).contains ("kubernetes" );
235+ assertThat (services ).contains ("busybox-service" );
236+ assertThat (services ).contains ("kubernetes" );
254237
255238 }
256239
@@ -287,15 +270,15 @@ static void filterMatchesBothNamespacesViaThePredicate(DiscoveryClient discovery
287270 assertThat (first .getPort ()).isEqualTo (8080 );
288271 assertThat (first .getNamespace ()).isEqualTo ("a-uat" );
289272 assertThat (first .getMetadata ()).isEqualTo (
290- Map .of ("app" , "service-wiremock" , "port.http" , "8080" , "k8s_namespace" , "a-uat" , "type" , "ClusterIP" ));
273+ Map .of ("app" , "service-wiremock" , "port.http" , "8080" , "k8s_namespace" , "a-uat" , "type" , "ClusterIP" ));
291274
292275 DefaultKubernetesServiceInstance second = sorted .get (1 );
293276 assertThat (second .getServiceId ()).isEqualTo ("service-wiremock" );
294277 assertThat (second .getInstanceId ()).isNotNull ();
295278 assertThat (second .getPort ()).isEqualTo (8080 );
296279 assertThat (second .getNamespace ()).isEqualTo ("b-uat" );
297280 assertThat (second .getMetadata ()).isEqualTo (
298- Map .of ("app" , "service-wiremock" , "port.http" , "8080" , "k8s_namespace" , "b-uat" , "type" , "ClusterIP" ));
281+ Map .of ("app" , "service-wiremock" , "port.http" , "8080" , "k8s_namespace" , "b-uat" , "type" , "ClusterIP" ));
299282
300283 }
301284
@@ -316,9 +299,9 @@ static void filterMatchesOneNamespaceViaThePredicate(DiscoveryClient discoveryCl
316299 assertThat (services ).contains ("service-wiremock" );
317300
318301 List <DefaultKubernetesServiceInstance > serviceInstances = discoveryClient .getInstances ("service-wiremock" )
319- .stream ()
320- .map (x -> (DefaultKubernetesServiceInstance ) x )
321- .toList ();
302+ .stream ()
303+ .map (x -> (DefaultKubernetesServiceInstance ) x )
304+ .toList ();
322305
323306 assertThat (serviceInstances .size ()).isEqualTo (1 );
324307
@@ -328,7 +311,7 @@ static void filterMatchesOneNamespaceViaThePredicate(DiscoveryClient discoveryCl
328311 assertThat (first .getPort ()).isEqualTo (8080 );
329312 assertThat (first .getNamespace ()).isEqualTo ("a-uat" );
330313 assertThat (first .getMetadata ()).isEqualTo (
331- Map .of ("app" , "service-wiremock" , "port.http" , "8080" , "k8s_namespace" , "a-uat" , "type" , "ClusterIP" ));
314+ Map .of ("app" , "service-wiremock" , "port.http" , "8080" , "k8s_namespace" , "a-uat" , "type" , "ClusterIP" ));
332315
333316 }
334317
0 commit comments