|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2019 the original author or authors. |
| 2 | + * Copyright 2012-2020 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.boot.actuate.hazelcast;
|
18 | 18 |
|
19 |
| -import com.hazelcast.core.Endpoint; |
| 19 | +import java.io.IOException; |
| 20 | + |
20 | 21 | import com.hazelcast.core.HazelcastException;
|
21 | 22 | import com.hazelcast.core.HazelcastInstance;
|
22 |
| -import com.hazelcast.transaction.TransactionalTask; |
23 | 23 | import org.junit.jupiter.api.Test;
|
24 | 24 |
|
25 | 25 | import org.springframework.boot.actuate.health.Health;
|
26 | 26 | import org.springframework.boot.actuate.health.Status;
|
| 27 | +import org.springframework.boot.autoconfigure.hazelcast.HazelcastInstanceFactory; |
| 28 | +import org.springframework.core.io.ClassPathResource; |
27 | 29 |
|
28 | 30 | import static org.assertj.core.api.Assertions.assertThat;
|
29 | 31 | import static org.mockito.ArgumentMatchers.any;
|
|
38 | 40 | */
|
39 | 41 | class HazelcastHealthIndicatorTests {
|
40 | 42 |
|
41 |
| - private final HazelcastInstance hazelcast = mock(HazelcastInstance.class); |
42 |
| - |
43 | 43 | @Test
|
44 |
| - void hazelcastUp() { |
45 |
| - Endpoint endpoint = mock(Endpoint.class); |
46 |
| - given(this.hazelcast.getName()).willReturn("hz0-instance"); |
47 |
| - given(this.hazelcast.getLocalEndpoint()).willReturn(endpoint); |
48 |
| - given(endpoint.getUuid()).willReturn("7581bb2f-879f-413f-b574-0071d7519eb0"); |
49 |
| - given(this.hazelcast.executeTransaction(any())).willAnswer((invocation) -> { |
50 |
| - TransactionalTask<?> task = invocation.getArgument(0); |
51 |
| - return task.execute(null); |
52 |
| - }); |
53 |
| - Health health = new HazelcastHealthIndicator(this.hazelcast).health(); |
54 |
| - assertThat(health.getStatus()).isEqualTo(Status.UP); |
55 |
| - assertThat(health.getDetails()).containsOnlyKeys("name", "uuid").containsEntry("name", "hz0-instance") |
56 |
| - .containsEntry("uuid", "7581bb2f-879f-413f-b574-0071d7519eb0"); |
| 44 | + void hazelcastUp() throws IOException { |
| 45 | + HazelcastInstance hazelcast = new HazelcastInstanceFactory(new ClassPathResource("hazelcast.xml")) |
| 46 | + .getHazelcastInstance(); |
| 47 | + try { |
| 48 | + Health health = new HazelcastHealthIndicator(hazelcast).health(); |
| 49 | + assertThat(health.getStatus()).isEqualTo(Status.UP); |
| 50 | + assertThat(health.getDetails()).containsOnlyKeys("name", "uuid").containsEntry("name", |
| 51 | + "actuator-hazelcast"); |
| 52 | + assertThat(health.getDetails().get("uuid")).asString().isNotEmpty(); |
| 53 | + } |
| 54 | + finally { |
| 55 | + hazelcast.shutdown(); |
| 56 | + } |
57 | 57 | }
|
58 | 58 |
|
59 | 59 | @Test
|
60 | 60 | void hazelcastDown() {
|
61 |
| - given(this.hazelcast.executeTransaction(any())).willReturn(new HazelcastException()); |
62 |
| - Health health = new HazelcastHealthIndicator(this.hazelcast).health(); |
| 61 | + HazelcastInstance hazelcast = mock(HazelcastInstance.class); |
| 62 | + given(hazelcast.executeTransaction(any())).willThrow(new HazelcastException()); |
| 63 | + Health health = new HazelcastHealthIndicator(hazelcast).health(); |
63 | 64 | assertThat(health.getStatus()).isEqualTo(Status.DOWN);
|
64 | 65 | }
|
65 | 66 |
|
|
0 commit comments