1818
1919import com .hazelcast .core .HazelcastException ;
2020import com .hazelcast .core .HazelcastInstance ;
21+ import com .hazelcast .core .LifecycleService ;
2122import org .junit .jupiter .api .Test ;
2223
2324import org .springframework .boot .actuate .health .Health ;
3031import static org .assertj .core .api .Assertions .assertThat ;
3132import static org .mockito .ArgumentMatchers .any ;
3233import static org .mockito .BDDMockito .given ;
34+ import static org .mockito .BDDMockito .then ;
3335import static org .mockito .Mockito .mock ;
3436
3537/**
3638 * Tests for {@link HazelcastHealthIndicator}.
3739 *
3840 * @author Dmytro Nosan
3941 * @author Stephane Nicoll
42+ * @author Tommy Karlsson
4043 */
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+ """ )
4159class HazelcastHealthIndicatorTests {
4260
4361 @ 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- """ )
5962 void hazelcastUp () {
6063 new ApplicationContextRunner ().withConfiguration (AutoConfigurations .of (HazelcastAutoConfiguration .class ))
6164 .withPropertyValues ("spring.hazelcast.config=hazelcast.xml" )
@@ -69,12 +72,44 @@ void hazelcastUp() {
6972 });
7073 }
7174
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+
7296 @ Test
7397 void hazelcastDown () {
74- HazelcastInstance hazelcast = mock ( HazelcastInstance . class );
98+ HazelcastInstance hazelcast = mockHazelcastInstance ( true );
7599 given (hazelcast .executeTransaction (any ())).willThrow (new HazelcastException ());
76100 Health health = new HazelcastHealthIndicator (hazelcast ).health ();
77101 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 ;
78113 }
79114
80115}
0 commit comments