18
18
19
19
import com .hazelcast .core .HazelcastException ;
20
20
import com .hazelcast .core .HazelcastInstance ;
21
+ import com .hazelcast .core .LifecycleService ;
21
22
import org .junit .jupiter .api .Test ;
22
23
23
24
import org .springframework .boot .actuate .health .Health ;
30
31
import static org .assertj .core .api .Assertions .assertThat ;
31
32
import static org .mockito .ArgumentMatchers .any ;
32
33
import static org .mockito .BDDMockito .given ;
34
+ import static org .mockito .BDDMockito .then ;
33
35
import static org .mockito .Mockito .mock ;
34
36
35
37
/**
36
38
* Tests for {@link HazelcastHealthIndicator}.
37
39
*
38
40
* @author Dmytro Nosan
39
41
* @author Stephane Nicoll
42
+ * @author Tommy Karlsson
40
43
*/
44
+ @ WithResource (name = "hazelcast.xml" , content = """
45
+ <hazelcast xmlns="http://www.hazelcast.com/schema/config"
46
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
47
+ xsi:schemaLocation="http://www.hazelcast.com/schema/config
48
+ http://www.hazelcast.com/schema/config/hazelcast-config-5.0.xsd">
49
+ <instance-name>actuator-hazelcast</instance-name>
50
+ <map name="defaultCache" />
51
+ <network>
52
+ <join>
53
+ <auto-detection enabled="false"/>
54
+ <multicast enabled="false"/>
55
+ </join>
56
+ </network>
57
+ </hazelcast>
58
+ """ )
41
59
class HazelcastHealthIndicatorTests {
42
60
43
61
@ Test
44
- @ WithResource (name = "hazelcast.xml" , content = """
45
- <hazelcast xmlns="http://www.hazelcast.com/schema/config"
46
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
47
- xsi:schemaLocation="http://www.hazelcast.com/schema/config
48
- http://www.hazelcast.com/schema/config/hazelcast-config-5.0.xsd">
49
- <instance-name>actuator-hazelcast</instance-name>
50
- <map name="defaultCache" />
51
- <network>
52
- <join>
53
- <auto-detection enabled="false"/>
54
- <multicast enabled="false"/>
55
- </join>
56
- </network>
57
- </hazelcast>
58
- """ )
59
62
void hazelcastUp () {
60
63
new ApplicationContextRunner ().withConfiguration (AutoConfigurations .of (HazelcastAutoConfiguration .class ))
61
64
.withPropertyValues ("spring.hazelcast.config=hazelcast.xml" )
@@ -69,12 +72,44 @@ void hazelcastUp() {
69
72
});
70
73
}
71
74
75
+ @ Test
76
+ void hazelcastShutdown () {
77
+ new ApplicationContextRunner ().withConfiguration (AutoConfigurations .of (HazelcastAutoConfiguration .class ))
78
+ .withPropertyValues ("spring.hazelcast.config=hazelcast.xml" )
79
+ .run ((context ) -> {
80
+ HazelcastInstance hazelcast = context .getBean (HazelcastInstance .class );
81
+ hazelcast .shutdown ();
82
+ Health health = new HazelcastHealthIndicator (hazelcast ).health ();
83
+ assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
84
+ });
85
+ }
86
+
87
+ @ Test
88
+ void hazelcastLifecycleNotRunning () {
89
+ HazelcastInstance hazelcast = mockHazelcastInstance (false );
90
+ Health health = new HazelcastHealthIndicator (hazelcast ).health ();
91
+ assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
92
+ then (hazelcast ).should ().getLifecycleService ();
93
+ then (hazelcast ).shouldHaveNoMoreInteractions ();
94
+ }
95
+
72
96
@ Test
73
97
void hazelcastDown () {
74
- HazelcastInstance hazelcast = mock ( HazelcastInstance . class );
98
+ HazelcastInstance hazelcast = mockHazelcastInstance ( true );
75
99
given (hazelcast .executeTransaction (any ())).willThrow (new HazelcastException ());
76
100
Health health = new HazelcastHealthIndicator (hazelcast ).health ();
77
101
assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
102
+ then (hazelcast ).should ().getLifecycleService ();
103
+ then (hazelcast ).should ().executeTransaction (any ());
104
+ then (hazelcast ).shouldHaveNoMoreInteractions ();
105
+ }
106
+
107
+ private static HazelcastInstance mockHazelcastInstance (boolean isRunning ) {
108
+ LifecycleService lifecycleService = mock (LifecycleService .class );
109
+ given (lifecycleService .isRunning ()).willReturn (isRunning );
110
+ HazelcastInstance hazelcastInstance = mock (HazelcastInstance .class );
111
+ given (hazelcastInstance .getLifecycleService ()).willReturn (lifecycleService );
112
+ return hazelcastInstance ;
78
113
}
79
114
80
115
}
0 commit comments