@ -17,13 +17,22 @@
package org.springframework.boot.actuate.couchbase ;
package org.springframework.boot.actuate.couchbase ;
import java.net.InetAddress ;
import java.net.InetAddress ;
import java.net.InetSocketAddress ;
import java.net.UnknownHostException ;
import java.net.UnknownHostException ;
import java.time.Duration ;
import java.time.Duration ;
import java.util.Arrays ;
import java.util.Collections ;
import java.util.Collections ;
import java.util.List ;
import java.util.Map ;
import java.util.concurrent.TimeUnit ;
import java.util.concurrent.TimeUnit ;
import java.util.concurrent.TimeoutException ;
import java.util.concurrent.TimeoutException ;
import com.couchbase.client.core.message.internal.DiagnosticsReport ;
import com.couchbase.client.core.message.internal.EndpointHealth ;
import com.couchbase.client.core.service.ServiceType ;
import com.couchbase.client.core.state.LifecycleState ;
import com.couchbase.client.java.Bucket ;
import com.couchbase.client.java.Bucket ;
import com.couchbase.client.java.Cluster ;
import com.couchbase.client.java.bucket.BucketInfo ;
import com.couchbase.client.java.bucket.BucketInfo ;
import com.couchbase.client.java.bucket.BucketManager ;
import com.couchbase.client.java.bucket.BucketManager ;
import com.couchbase.client.java.cluster.ClusterInfo ;
import com.couchbase.client.java.cluster.ClusterInfo ;
@ -49,7 +58,7 @@ import static org.mockito.Mockito.verify;
public class CouchbaseHealthIndicatorTests {
public class CouchbaseHealthIndicatorTests {
@Test
@Test
public void couchbase IsUp( ) throws UnknownHostException {
public void couchbase Operations IsUp( ) throws UnknownHostException {
BucketInfo bucketInfo = mock ( BucketInfo . class ) ;
BucketInfo bucketInfo = mock ( BucketInfo . class ) ;
given ( bucketInfo . nodeList ( ) ) . willReturn (
given ( bucketInfo . nodeList ( ) ) . willReturn (
Collections . singletonList ( InetAddress . getByName ( "127.0.0.1" ) ) ) ;
Collections . singletonList ( InetAddress . getByName ( "127.0.0.1" ) ) ) ;
@ -63,6 +72,7 @@ public class CouchbaseHealthIndicatorTests {
CouchbaseOperations couchbaseOperations = mock ( CouchbaseOperations . class ) ;
CouchbaseOperations couchbaseOperations = mock ( CouchbaseOperations . class ) ;
given ( couchbaseOperations . getCouchbaseBucket ( ) ) . willReturn ( bucket ) ;
given ( couchbaseOperations . getCouchbaseBucket ( ) ) . willReturn ( bucket ) ;
given ( couchbaseOperations . getCouchbaseClusterInfo ( ) ) . willReturn ( clusterInfo ) ;
given ( couchbaseOperations . getCouchbaseClusterInfo ( ) ) . willReturn ( clusterInfo ) ;
@SuppressWarnings ( "deprecation" )
CouchbaseHealthIndicator healthIndicator = new CouchbaseHealthIndicator (
CouchbaseHealthIndicator healthIndicator = new CouchbaseHealthIndicator (
couchbaseOperations , Duration . ofSeconds ( 2 ) ) ;
couchbaseOperations , Duration . ofSeconds ( 2 ) ) ;
Health health = healthIndicator . health ( ) ;
Health health = healthIndicator . health ( ) ;
@ -74,7 +84,7 @@ public class CouchbaseHealthIndicatorTests {
}
}
@Test
@Test
public void couchbase Timeout( ) {
public void couchbase Operations Timeout( ) {
BucketManager bucketManager = mock ( BucketManager . class ) ;
BucketManager bucketManager = mock ( BucketManager . class ) ;
given ( bucketManager . info ( 1500 , TimeUnit . MILLISECONDS ) ) . willThrow (
given ( bucketManager . info ( 1500 , TimeUnit . MILLISECONDS ) ) . willThrow (
new RuntimeException ( new TimeoutException ( "timeout, expected" ) ) ) ;
new RuntimeException ( new TimeoutException ( "timeout, expected" ) ) ) ;
@ -82,6 +92,7 @@ public class CouchbaseHealthIndicatorTests {
given ( bucket . bucketManager ( ) ) . willReturn ( bucketManager ) ;
given ( bucket . bucketManager ( ) ) . willReturn ( bucketManager ) ;
CouchbaseOperations couchbaseOperations = mock ( CouchbaseOperations . class ) ;
CouchbaseOperations couchbaseOperations = mock ( CouchbaseOperations . class ) ;
given ( couchbaseOperations . getCouchbaseBucket ( ) ) . willReturn ( bucket ) ;
given ( couchbaseOperations . getCouchbaseBucket ( ) ) . willReturn ( bucket ) ;
@SuppressWarnings ( "deprecation" )
CouchbaseHealthIndicator healthIndicator = new CouchbaseHealthIndicator (
CouchbaseHealthIndicator healthIndicator = new CouchbaseHealthIndicator (
couchbaseOperations , Duration . ofMillis ( 1500 ) ) ;
couchbaseOperations , Duration . ofMillis ( 1500 ) ) ;
Health health = healthIndicator . health ( ) ;
Health health = healthIndicator . health ( ) ;
@ -90,10 +101,11 @@ public class CouchbaseHealthIndicatorTests {
}
}
@Test
@Test
public void couchbase IsDown( ) {
public void couchbase Operations IsDown( ) {
CouchbaseOperations couchbaseOperations = mock ( CouchbaseOperations . class ) ;
CouchbaseOperations couchbaseOperations = mock ( CouchbaseOperations . class ) ;
given ( couchbaseOperations . getCouchbaseClusterInfo ( ) )
given ( couchbaseOperations . getCouchbaseClusterInfo ( ) )
. willThrow ( new IllegalStateException ( "test, expected" ) ) ;
. willThrow ( new IllegalStateException ( "test, expected" ) ) ;
@SuppressWarnings ( "deprecation" )
CouchbaseHealthIndicator healthIndicator = new CouchbaseHealthIndicator (
CouchbaseHealthIndicator healthIndicator = new CouchbaseHealthIndicator (
couchbaseOperations , Duration . ofSeconds ( 1 ) ) ;
couchbaseOperations , Duration . ofSeconds ( 1 ) ) ;
Health health = healthIndicator . health ( ) ;
Health health = healthIndicator . health ( ) ;
@ -102,4 +114,48 @@ public class CouchbaseHealthIndicatorTests {
verify ( couchbaseOperations ) . getCouchbaseClusterInfo ( ) ;
verify ( couchbaseOperations ) . getCouchbaseClusterInfo ( ) ;
}
}
@Test
@SuppressWarnings ( "unchecked" )
public void couchbaseClusterIsUp ( ) {
Cluster cluster = mock ( Cluster . class ) ;
CouchbaseHealthIndicator healthIndicator = new CouchbaseHealthIndicator ( cluster ) ;
List < EndpointHealth > endpoints = Arrays . asList ( new EndpointHealth (
ServiceType . BINARY , LifecycleState . CONNECTED , new InetSocketAddress ( 0 ) ,
new InetSocketAddress ( 0 ) , 1234 , "endpoint-1" ) ) ;
DiagnosticsReport diagnostics = new DiagnosticsReport ( endpoints , "test-sdk" ,
"test-id" ) ;
given ( cluster . diagnostics ( ) ) . willReturn ( diagnostics ) ;
Health health = healthIndicator . health ( ) ;
assertThat ( health . getStatus ( ) ) . isEqualTo ( Status . UP ) ;
assertThat ( health . getDetails ( ) ) . containsEntry ( "sdk" , "test-sdk" ) ;
assertThat ( health . getDetails ( ) ) . containsKey ( "endpoints" ) ;
assertThat ( ( List < Map < String , Object > > ) health . getDetails ( ) . get ( "endpoints" ) )
. hasSize ( 1 ) ;
verify ( cluster ) . diagnostics ( ) ;
}
@Test
@SuppressWarnings ( "unchecked" )
public void couchbaseClusterIsDown ( ) {
Cluster cluster = mock ( Cluster . class ) ;
CouchbaseHealthIndicator healthIndicator = new CouchbaseHealthIndicator ( cluster ) ;
List < EndpointHealth > endpoints = Arrays . asList (
new EndpointHealth ( ServiceType . BINARY , LifecycleState . CONNECTED ,
new InetSocketAddress ( 0 ) , new InetSocketAddress ( 0 ) , 1234 ,
"endpoint-1" ) ,
new EndpointHealth ( ServiceType . BINARY , LifecycleState . CONNECTING ,
new InetSocketAddress ( 0 ) , new InetSocketAddress ( 0 ) , 1234 ,
"endpoint-2" ) ) ;
DiagnosticsReport diagnostics = new DiagnosticsReport ( endpoints , "test-sdk" ,
"test-id" ) ;
given ( cluster . diagnostics ( ) ) . willReturn ( diagnostics ) ;
Health health = healthIndicator . health ( ) ;
assertThat ( health . getStatus ( ) ) . isEqualTo ( Status . DOWN ) ;
assertThat ( health . getDetails ( ) ) . containsEntry ( "sdk" , "test-sdk" ) ;
assertThat ( health . getDetails ( ) ) . containsKey ( "endpoints" ) ;
assertThat ( ( List < Map < String , Object > > ) health . getDetails ( ) . get ( "endpoints" ) )
. hasSize ( 2 ) ;
verify ( cluster ) . diagnostics ( ) ;
}
}
}