1616
1717package  org .springframework .boot .actuate .cassandra ;
1818
19+ import  java .util .ArrayList ;
20+ import  java .util .Collections ;
21+ import  java .util .HashMap ;
22+ import  java .util .List ;
23+ import  java .util .Map ;
24+ import  java .util .UUID ;
25+ 
1926import  com .datastax .oss .driver .api .core .CqlSession ;
2027import  com .datastax .oss .driver .api .core .DriverTimeoutException ;
21- import  com .datastax .oss .driver .api .core .cql .ResultSet ;
22- import  com .datastax .oss .driver .api .core .cql .Row ;
23- import  com .datastax .oss .driver .api .core .cql .SimpleStatement ;
28+ import  com .datastax .oss .driver .api .core .Version ;
29+ import  com .datastax .oss .driver .api .core .metadata .Metadata ;
30+ import  com .datastax .oss .driver .api .core .metadata .Node ;
31+ import  com .datastax .oss .driver .api .core .metadata .NodeState ;
2432import  org .junit .jupiter .api .Test ;
2533
2634import  org .springframework .boot .actuate .health .Health ;
2735import  org .springframework .boot .actuate .health .Status ;
2836
2937import  static  org .assertj .core .api .Assertions .assertThat ;
3038import  static  org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
31- import  static  org .mockito .ArgumentMatchers .any ;
3239import  static  org .mockito .BDDMockito .given ;
3340import  static  org .mockito .Mockito .mock ;
3441
3542/** 
3643 * Tests for {@link CassandraDriverHealthIndicator}. 
3744 * 
3845 * @author Alexandre Dutra 
39-  * @since 2.4.0  
46+  * @author Stephane Nicoll  
4047 */ 
4148class  CassandraDriverHealthIndicatorTests  {
4249
@@ -46,29 +53,114 @@ void createWhenCqlSessionIsNullShouldThrowException() {
4653	}
4754
4855	@ Test 
49- 	void  healthWithCassandraUp () {
56+ 	void  healthWithOneHealthyNodeShouldReturnUp () {
57+ 		CqlSession  session  = mockCqlSessionWithNodeState (NodeState .UP );
58+ 		CassandraDriverHealthIndicator  healthIndicator  = new  CassandraDriverHealthIndicator (session );
59+ 		Health  health  = healthIndicator .health ();
60+ 		assertThat (health .getStatus ()).isEqualTo (Status .UP );
61+ 	}
62+ 
63+ 	@ Test 
64+ 	void  healthWithOneUnhealthyNodeShouldReturnDown () {
65+ 		CqlSession  session  = mockCqlSessionWithNodeState (NodeState .DOWN );
66+ 		CassandraDriverHealthIndicator  healthIndicator  = new  CassandraDriverHealthIndicator (session );
67+ 		Health  health  = healthIndicator .health ();
68+ 		assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
69+ 	}
70+ 
71+ 	@ Test 
72+ 	void  healthWithOneUnknownNodeShouldReturnDown () {
73+ 		CqlSession  session  = mockCqlSessionWithNodeState (NodeState .UNKNOWN );
74+ 		CassandraDriverHealthIndicator  healthIndicator  = new  CassandraDriverHealthIndicator (session );
75+ 		Health  health  = healthIndicator .health ();
76+ 		assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
77+ 	}
78+ 
79+ 	@ Test 
80+ 	void  healthWithOneForcedDownNodeShouldReturnDown () {
81+ 		CqlSession  session  = mockCqlSessionWithNodeState (NodeState .FORCED_DOWN );
82+ 		CassandraDriverHealthIndicator  healthIndicator  = new  CassandraDriverHealthIndicator (session );
83+ 		Health  health  = healthIndicator .health ();
84+ 		assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
85+ 	}
86+ 
87+ 	@ Test 
88+ 	void  healthWithOneHealthyNodeAndOneUnhealthyNodeShouldReturnUp () {
89+ 		CqlSession  session  = mockCqlSessionWithNodeState (NodeState .UP , NodeState .DOWN );
90+ 		CassandraDriverHealthIndicator  healthIndicator  = new  CassandraDriverHealthIndicator (session );
91+ 		Health  health  = healthIndicator .health ();
92+ 		assertThat (health .getStatus ()).isEqualTo (Status .UP );
93+ 	}
94+ 
95+ 	@ Test 
96+ 	void  healthWithOneHealthyNodeAndOneUnknownNodeShouldReturnUp () {
97+ 		CqlSession  session  = mockCqlSessionWithNodeState (NodeState .UP , NodeState .UNKNOWN );
98+ 		CassandraDriverHealthIndicator  healthIndicator  = new  CassandraDriverHealthIndicator (session );
99+ 		Health  health  = healthIndicator .health ();
100+ 		assertThat (health .getStatus ()).isEqualTo (Status .UP );
101+ 	}
102+ 
103+ 	@ Test 
104+ 	void  healthWithOneHealthyNodeAndOneForcedDownNodeShouldReturnUp () {
105+ 		CqlSession  session  = mockCqlSessionWithNodeState (NodeState .UP , NodeState .FORCED_DOWN );
106+ 		CassandraDriverHealthIndicator  healthIndicator  = new  CassandraDriverHealthIndicator (session );
107+ 		Health  health  = healthIndicator .health ();
108+ 		assertThat (health .getStatus ()).isEqualTo (Status .UP );
109+ 	}
110+ 
111+ 	@ Test 
112+ 	void  healthWithNodeVersionShouldAddVersionDetail () {
50113		CqlSession  session  = mock (CqlSession .class );
51- 		ResultSet   resultSet  = mock (ResultSet .class );
52- 		Row   row  =  mock ( Row . class );
53- 		given ( session . execute ( any ( SimpleStatement . class ))). willReturn ( resultSet );
54- 		given (resultSet . one ()).willReturn (row );
55- 		given (row . isNull ( 0 )).willReturn (false );
56- 		given (row . getString ( 0 )).willReturn ("1.0.0" );
114+ 		Metadata   metadata  = mock (Metadata .class );
115+ 		given ( session . getMetadata ()). willReturn ( metadata );
116+ 		Node   node  =  mock ( Node . class );
117+ 		given (node . getState ()).willReturn (NodeState . UP );
118+ 		given (node . getCassandraVersion ( )).willReturn (Version . V4_0_0 );
119+ 		given (metadata . getNodes ( )).willReturn (createNodesWithRandomUUID ( Collections . singletonList ( node )) );
57120		CassandraDriverHealthIndicator  healthIndicator  = new  CassandraDriverHealthIndicator (session );
58121		Health  health  = healthIndicator .health ();
59122		assertThat (health .getStatus ()).isEqualTo (Status .UP );
60- 		assertThat (health .getDetails ().get ("version" )).isEqualTo ("1.0.0" );
123+ 		assertThat (health .getDetails ().get ("version" )).isEqualTo (Version . V4_0_0 );
61124	}
62125
63126	@ Test 
64- 	void  healthWithCassandraDown () {
127+ 	void  healthWithoutNodeVersionShouldNotAddVersionDetail () {
128+ 		CqlSession  session  = mockCqlSessionWithNodeState (NodeState .UP );
129+ 		CassandraDriverHealthIndicator  healthIndicator  = new  CassandraDriverHealthIndicator (session );
130+ 		Health  health  = healthIndicator .health ();
131+ 		assertThat (health .getStatus ()).isEqualTo (Status .UP );
132+ 		assertThat (health .getDetails ().get ("version" )).isNull ();
133+ 	}
134+ 
135+ 	@ Test 
136+ 	void  healthWithcassandraDownShouldReturnDown () {
65137		CqlSession  session  = mock (CqlSession .class );
66- 		given (session .execute ( any ( SimpleStatement . class ) )).willThrow (new  DriverTimeoutException ("Test Exception" ));
138+ 		given (session .getMetadata ( )).willThrow (new  DriverTimeoutException ("Test Exception" ));
67139		CassandraDriverHealthIndicator  healthIndicator  = new  CassandraDriverHealthIndicator (session );
68140		Health  health  = healthIndicator .health ();
69141		assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
70142		assertThat (health .getDetails ().get ("error" ))
71143				.isEqualTo (DriverTimeoutException .class .getName () + ": Test Exception" );
72144	}
73145
146+ 	private  CqlSession  mockCqlSessionWithNodeState (NodeState ... nodeStates ) {
147+ 		CqlSession  session  = mock (CqlSession .class );
148+ 		Metadata  metadata  = mock (Metadata .class );
149+ 		List <Node > nodes  = new  ArrayList <>();
150+ 		for  (NodeState  nodeState  : nodeStates ) {
151+ 			Node  node  = mock (Node .class );
152+ 			given (node .getState ()).willReturn (nodeState );
153+ 			nodes .add (node );
154+ 		}
155+ 		given (session .getMetadata ()).willReturn (metadata );
156+ 		given (metadata .getNodes ()).willReturn (createNodesWithRandomUUID (nodes ));
157+ 		return  session ;
158+ 	}
159+ 
160+ 	private  Map <UUID , Node > createNodesWithRandomUUID (List <Node > nodes ) {
161+ 		Map <UUID , Node > indexedNodes  = new  HashMap <>();
162+ 		nodes .forEach ((node ) -> indexedNodes .put (UUID .randomUUID (), node ));
163+ 		return  indexedNodes ;
164+ 	}
165+ 
74166}
0 commit comments