1
1
package org .springframework .cloud .client .discovery .composite ;
2
2
3
+ import static org .assertj .core .api .Assertions .assertThat ;
4
+ import static org .springframework .cloud .client .discovery .composite .CompositeDiscoveryClientTestsConfig .CUSTOM_SERVICE_ID ;
5
+
3
6
import java .net .URI ;
4
- import java .util .Arrays ;
5
- import java .util .Collections ;
6
- import java .util .List ;
7
7
8
8
import org .junit .Test ;
9
9
import org .junit .runner .RunWith ;
10
+
10
11
import org .springframework .beans .factory .annotation .Autowired ;
11
- import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
12
12
import org .springframework .boot .test .context .SpringBootTest ;
13
- import org .springframework .cloud .client .DefaultServiceInstance ;
14
13
import org .springframework .cloud .client .ServiceInstance ;
15
14
import org .springframework .cloud .client .discovery .DiscoveryClient ;
16
- import org .springframework .context .annotation .Bean ;
17
- import org .springframework .context .annotation .Configuration ;
18
- import org .springframework .core .annotation .Order ;
19
15
import org .springframework .test .context .junit4 .SpringRunner ;
20
16
21
- import static org .assertj .core .api .Assertions .assertThat ;
22
-
23
17
/**
24
18
* Tests for behavior of Composite Discovery Client
25
19
*
32
26
"spring.cloud.discovery.client.simple.instances.service1[0].uri=http://s1-1:8080" ,
33
27
"spring.cloud.discovery.client.simple.instances.service1[1].uri=https://s1-2:8443" ,
34
28
"spring.cloud.discovery.client.simple.instances.service2[0].uri=https://s2-1:8080" ,
35
- "spring.cloud.discovery.client.simple.instances.service2[1].uri=https://s2-2:443" })
29
+ "spring.cloud.discovery.client.simple.instances.service2[1].uri=https://s2-2:443" , }, classes = {
30
+ CompositeDiscoveryClientTestsConfig .class })
36
31
public class CompositeDiscoveryClientTests {
37
32
38
33
@ Autowired
39
34
private DiscoveryClient discoveryClient ;
40
35
41
36
@ Test
42
37
public void getInstancesByServiceIdShouldDelegateCall () {
43
- assertThat (this . discoveryClient ).isInstanceOf (CompositeDiscoveryClient .class );
38
+ assertThat (discoveryClient ).isInstanceOf (CompositeDiscoveryClient .class );
44
39
45
- assertThat (this . discoveryClient .getInstances ("service1" )).hasSize (2 );
40
+ assertThat (discoveryClient .getInstances ("service1" )).hasSize (2 );
46
41
47
- ServiceInstance s1 = this . discoveryClient .getInstances ("service1" ).get (0 );
42
+ ServiceInstance s1 = discoveryClient .getInstances ("service1" ).get (0 );
48
43
assertThat (s1 .getHost ()).isEqualTo ("s1-1" );
49
44
assertThat (s1 .getPort ()).isEqualTo (8080 );
50
45
assertThat (s1 .getUri ()).isEqualTo (URI .create ("http://s1-1:8080" ));
@@ -53,53 +48,23 @@ public void getInstancesByServiceIdShouldDelegateCall() {
53
48
54
49
@ Test
55
50
public void getServicesShouldAggregateAllServiceNames () {
56
- assertThat (this .discoveryClient .getServices ()).containsOnlyOnce ("service1" , "service2" , "custom" );
51
+ assertThat (discoveryClient .getServices ()).containsOnlyOnce ("service1" , "service2" ,
52
+ CUSTOM_SERVICE_ID );
57
53
}
58
54
59
55
@ Test
60
56
public void getDescriptionShouldBeComposite () {
61
- assertThat (this . discoveryClient .description ()).isEqualTo ("Composite Discovery Client" );
57
+ assertThat (discoveryClient .description ()).isEqualTo ("Composite Discovery Client" );
62
58
}
63
59
64
60
@ Test
65
61
public void getInstancesShouldRespectOrder () {
66
- assertThat (this . discoveryClient .getInstances ("custom" )).hasSize (1 );
67
- assertThat (this . discoveryClient .getInstances ("custom" )).hasSize (1 );
62
+ assertThat (discoveryClient .getInstances (CUSTOM_SERVICE_ID )).hasSize (1 );
63
+ assertThat (discoveryClient .getInstances (CUSTOM_SERVICE_ID )).hasSize (1 );
68
64
}
69
65
70
66
@ Test
71
67
public void getInstancesByUnknownServiceIdShouldReturnAnEmptyList () {
72
- assertThat (this .discoveryClient .getInstances ("unknown" )).hasSize (0 );
73
- }
74
-
75
- @ EnableAutoConfiguration
76
- @ Configuration
77
- public static class Config {
78
-
79
- @ Bean
80
- @ Order (1 )
81
- public DiscoveryClient customDiscoveryClient () {
82
- return new DiscoveryClient () {
83
- @ Override
84
- public String description () {
85
- return "A custom discovery client" ;
86
- }
87
-
88
- @ Override
89
- public List <ServiceInstance > getInstances (String serviceId ) {
90
- if (serviceId .equals ("custom" )) {
91
- ServiceInstance s1 = new DefaultServiceInstance ("custom" , "host" ,
92
- 123 , false );
93
- return Arrays .asList (s1 );
94
- }
95
- return Collections .emptyList ();
96
- }
97
-
98
- @ Override
99
- public List <String > getServices () {
100
- return Arrays .asList ("custom" );
101
- }
102
- };
103
- }
68
+ assertThat (discoveryClient .getInstances ("unknown" )).hasSize (0 );
104
69
}
105
70
}
0 commit comments