|
15 | 15 | */
|
16 | 16 | package org.springframework.boot.actuate.couchbase;
|
17 | 17 |
|
18 |
| -import java.net.InetAddress; |
| 18 | +import java.net.InetSocketAddress; |
19 | 19 | import java.util.Arrays;
|
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
20 | 22 |
|
21 |
| -import com.couchbase.client.java.Bucket; |
22 |
| -import com.couchbase.client.java.bucket.AsyncBucketManager; |
23 |
| -import com.couchbase.client.java.bucket.BucketInfo; |
24 |
| -import com.couchbase.client.java.bucket.BucketManager; |
25 |
| -import com.couchbase.client.java.cluster.ClusterInfo; |
26 |
| -import com.couchbase.client.java.error.TranscodingException; |
27 |
| -import com.couchbase.client.java.util.features.Version; |
| 23 | +import com.couchbase.client.core.message.internal.DiagnosticsReport; |
| 24 | +import com.couchbase.client.core.message.internal.EndpointHealth; |
| 25 | +import com.couchbase.client.core.service.ServiceType; |
| 26 | +import com.couchbase.client.core.state.LifecycleState; |
| 27 | +import com.couchbase.client.java.Cluster; |
28 | 28 | import org.junit.Test;
|
29 |
| -import reactor.core.publisher.Mono; |
30 |
| -import reactor.test.StepVerifier; |
31 |
| -import rx.Observable; |
32 | 29 |
|
33 | 30 | import org.springframework.boot.actuate.health.Health;
|
34 | 31 | import org.springframework.boot.actuate.health.Status;
|
35 |
| -import org.springframework.data.couchbase.core.RxJavaCouchbaseOperations; |
36 | 32 |
|
37 | 33 | import static org.assertj.core.api.Assertions.assertThat;
|
38 | 34 | import static org.mockito.BDDMockito.given;
|
39 | 35 | import static org.mockito.Mockito.mock;
|
| 36 | +import static org.mockito.Mockito.verify; |
40 | 37 |
|
41 | 38 | /**
|
42 | 39 | * Tests for {@link CouchbaseReactiveHealthIndicator}.
|
43 | 40 | */
|
44 | 41 | public class CouchbaseReactiveHealthIndicatorTests {
|
45 | 42 |
|
46 | 43 | @Test
|
47 |
| - public void couchbaseIsUp() { |
48 |
| - RxJavaCouchbaseOperations rxJavaCouchbaseOperations = mock( |
49 |
| - RxJavaCouchbaseOperations.class); |
50 |
| - AsyncBucketManager asyncBucketManager = mockAsyncBucketManager( |
51 |
| - rxJavaCouchbaseOperations); |
52 |
| - BucketInfo info = mock(BucketInfo.class); |
53 |
| - InetAddress node1Address = mock(InetAddress.class); |
54 |
| - InetAddress node2Address = mock(InetAddress.class); |
55 |
| - given(info.nodeList()).willReturn(Arrays.asList(node1Address, node2Address)); |
56 |
| - given(node1Address.toString()).willReturn("127.0.0.1"); |
57 |
| - given(node2Address.toString()).willReturn("127.0.0.2"); |
58 |
| - given(asyncBucketManager.info()).willReturn(Observable.just(info)); |
59 |
| - CouchbaseReactiveHealthIndicator couchbaseReactiveHealthIndicator = new CouchbaseReactiveHealthIndicator( |
60 |
| - rxJavaCouchbaseOperations); |
61 |
| - Mono<Health> health = couchbaseReactiveHealthIndicator.health(); |
62 |
| - StepVerifier.create(health).consumeNextWith((h) -> { |
63 |
| - assertThat(h.getStatus()).isEqualTo(Status.UP); |
64 |
| - assertThat(h.getDetails()).containsKeys("versions", "nodes"); |
65 |
| - assertThat(h.getDetails().get("versions")).isEqualTo("5.5.0,6.0.0"); |
66 |
| - assertThat(h.getDetails().get("nodes")).isEqualTo("127.0.0.1,127.0.0.2"); |
67 |
| - }).verifyComplete(); |
| 44 | + @SuppressWarnings("unchecked") |
| 45 | + public void couchbaseClusterIsUp() { |
| 46 | + Cluster cluster = mock(Cluster.class); |
| 47 | + CouchbaseReactiveHealthIndicator healthIndicator = new CouchbaseReactiveHealthIndicator( |
| 48 | + cluster); |
| 49 | + List<EndpointHealth> endpoints = Arrays.asList(new EndpointHealth( |
| 50 | + ServiceType.BINARY, LifecycleState.CONNECTED, new InetSocketAddress(0), |
| 51 | + new InetSocketAddress(0), 1234, "endpoint-1")); |
| 52 | + DiagnosticsReport diagnostics = new DiagnosticsReport(endpoints, "test-sdk", |
| 53 | + "test-id", null); |
| 54 | + given(cluster.diagnostics()).willReturn(diagnostics); |
| 55 | + Health health = healthIndicator.health().block(); |
| 56 | + assertThat(health.getStatus()).isEqualTo(Status.UP); |
| 57 | + assertThat(health.getDetails()).containsEntry("sdk", "test-sdk"); |
| 58 | + assertThat(health.getDetails()).containsKey("endpoints"); |
| 59 | + assertThat((List<Map<String, Object>>) health.getDetails().get("endpoints")) |
| 60 | + .hasSize(1); |
| 61 | + verify(cluster).diagnostics(); |
68 | 62 | }
|
69 | 63 |
|
70 | 64 | @Test
|
71 |
| - public void couchbaseIsDown() { |
72 |
| - RxJavaCouchbaseOperations rxJavaCouchbaseOperations = mock( |
73 |
| - RxJavaCouchbaseOperations.class); |
74 |
| - AsyncBucketManager asyncBucketManager = mockAsyncBucketManager( |
75 |
| - rxJavaCouchbaseOperations); |
76 |
| - given(asyncBucketManager.info()) |
77 |
| - .willReturn(Observable.error(new TranscodingException("Failure"))); |
78 |
| - CouchbaseReactiveHealthIndicator couchbaseReactiveHealthIndicator = new CouchbaseReactiveHealthIndicator( |
79 |
| - rxJavaCouchbaseOperations); |
80 |
| - Mono<Health> health = couchbaseReactiveHealthIndicator.health(); |
81 |
| - StepVerifier.create(health).consumeNextWith((h) -> { |
82 |
| - assertThat(h.getStatus()).isEqualTo(Status.DOWN); |
83 |
| - assertThat(h.getDetails()).containsOnlyKeys("error"); |
84 |
| - assertThat(h.getDetails().get("error")) |
85 |
| - .isEqualTo(TranscodingException.class.getName() + ": Failure"); |
86 |
| - }).verifyComplete(); |
87 |
| - } |
88 |
| - |
89 |
| - private AsyncBucketManager mockAsyncBucketManager( |
90 |
| - RxJavaCouchbaseOperations rxJavaCouchbaseOperations) { |
91 |
| - ClusterInfo clusterInfo = mock(ClusterInfo.class); |
92 |
| - given(rxJavaCouchbaseOperations.getCouchbaseClusterInfo()) |
93 |
| - .willReturn(clusterInfo); |
94 |
| - given(clusterInfo.getAllVersions()) |
95 |
| - .willReturn(Arrays.asList(new Version(5, 5, 0), new Version(6, 0, 0))); |
96 |
| - Bucket bucket = mock(Bucket.class); |
97 |
| - BucketManager bucketManager = mock(BucketManager.class); |
98 |
| - AsyncBucketManager asyncBucketManager = mock(AsyncBucketManager.class); |
99 |
| - given(rxJavaCouchbaseOperations.getCouchbaseBucket()).willReturn(bucket); |
100 |
| - given(bucket.bucketManager()).willReturn(bucketManager); |
101 |
| - given(bucketManager.async()).willReturn(asyncBucketManager); |
102 |
| - return asyncBucketManager; |
| 65 | + @SuppressWarnings("unchecked") |
| 66 | + public void couchbaseClusterIsDown() { |
| 67 | + Cluster cluster = mock(Cluster.class); |
| 68 | + CouchbaseReactiveHealthIndicator healthIndicator = new CouchbaseReactiveHealthIndicator( |
| 69 | + cluster); |
| 70 | + List<EndpointHealth> endpoints = Arrays.asList( |
| 71 | + new EndpointHealth(ServiceType.BINARY, LifecycleState.CONNECTED, |
| 72 | + new InetSocketAddress(0), new InetSocketAddress(0), 1234, |
| 73 | + "endpoint-1"), |
| 74 | + new EndpointHealth(ServiceType.BINARY, LifecycleState.CONNECTING, |
| 75 | + new InetSocketAddress(0), new InetSocketAddress(0), 1234, |
| 76 | + "endpoint-2")); |
| 77 | + DiagnosticsReport diagnostics = new DiagnosticsReport(endpoints, "test-sdk", |
| 78 | + "test-id", null); |
| 79 | + given(cluster.diagnostics()).willReturn(diagnostics); |
| 80 | + Health health = healthIndicator.health().block(); |
| 81 | + assertThat(health.getStatus()).isEqualTo(Status.DOWN); |
| 82 | + assertThat(health.getDetails()).containsEntry("sdk", "test-sdk"); |
| 83 | + assertThat(health.getDetails()).containsKey("endpoints"); |
| 84 | + assertThat((List<Map<String, Object>>) health.getDetails().get("endpoints")) |
| 85 | + .hasSize(2); |
| 86 | + verify(cluster).diagnostics(); |
103 | 87 | }
|
104 | 88 |
|
105 | 89 | }
|
0 commit comments