1616
1717package  org .springframework .boot .actuate .cassandra ;
1818
19+ import  java .util .HashMap ;
20+ import  java .util .Map ;
21+ import  java .util .UUID ;
22+ 
1923import  com .datastax .oss .driver .api .core .CqlSession ;
2024import  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 ;
25+ import  com .datastax .oss .driver .api .core .Version ;
26+ import  com .datastax .oss .driver .api .core .metadata .Metadata ;
27+ import  com .datastax .oss .driver .api .core .metadata .Node ;
28+ import  com .datastax .oss .driver .api .core .metadata .NodeState ;
2429import  org .junit .jupiter .api .Test ;
2530
2631import  org .springframework .boot .actuate .health .Health ;
2732import  org .springframework .boot .actuate .health .Status ;
2833
2934import  static  org .assertj .core .api .Assertions .assertThat ;
3035import  static  org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
31- import  static  org .mockito .ArgumentMatchers .any ;
3236import  static  org .mockito .BDDMockito .given ;
33- import  static  org .mockito .Mockito .mock ;
37+ import  static  org .mockito .BDDMockito .mock ;
38+ import  static  org .mockito .BDDMockito .when ;
3439
3540/** 
3641 * Tests for {@link CassandraDriverHealthIndicator}. 
3742 * 
3843 * @author Alexandre Dutra 
44+  * @author Tomasz Lelek 
3945 * @since 2.4.0 
4046 */ 
4147class  CassandraDriverHealthIndicatorTests  {
@@ -46,29 +52,150 @@ void createWhenCqlSessionIsNullShouldThrowException() {
4652	}
4753
4854	@ Test 
49- 	void  healthWithCassandraUp () {
55+ 	void  oneHealthyNodeShouldReturnUp () {
5056		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" );
57+ 		Metadata  metadata  = mock (Metadata .class );
58+ 		Node  healthyNode  = mock (Node .class );
59+ 		given (healthyNode .getState ()).willReturn (NodeState .UP );
60+ 		given (session .getMetadata ()).willReturn (metadata );
61+ 		given (metadata .getNodes ()).willReturn (createNodesMap (healthyNode ));
5762		CassandraDriverHealthIndicator  healthIndicator  = new  CassandraDriverHealthIndicator (session );
5863		Health  health  = healthIndicator .health ();
5964		assertThat (health .getStatus ()).isEqualTo (Status .UP );
60- 		assertThat (health .getDetails ().get ("version" )).isEqualTo ("1.0.0" );
65+ 	}
66+ 
67+ 	@ Test 
68+ 	void  oneUnhealthyNodeShouldReturnDown () {
69+ 		CqlSession  session  = mock (CqlSession .class );
70+ 		Metadata  metadata  = mock (Metadata .class );
71+ 		Node  unhealthyNode  = mock (Node .class );
72+ 		given (unhealthyNode .getState ()).willReturn (NodeState .DOWN );
73+ 		given (session .getMetadata ()).willReturn (metadata );
74+ 		given (metadata .getNodes ()).willReturn (createNodesMap (unhealthyNode ));
75+ 		CassandraDriverHealthIndicator  healthIndicator  = new  CassandraDriverHealthIndicator (session );
76+ 		Health  health  = healthIndicator .health ();
77+ 		assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
78+ 	}
79+ 
80+ 	@ Test 
81+ 	void  oneUnknownNodeShouldReturnDown () {
82+ 		CqlSession  session  = mock (CqlSession .class );
83+ 		Metadata  metadata  = mock (Metadata .class );
84+ 		Node  unknownNode  = mock (Node .class );
85+ 		given (unknownNode .getState ()).willReturn (NodeState .UNKNOWN );
86+ 		given (session .getMetadata ()).willReturn (metadata );
87+ 		given (metadata .getNodes ()).willReturn (createNodesMap (unknownNode ));
88+ 		CassandraDriverHealthIndicator  healthIndicator  = new  CassandraDriverHealthIndicator (session );
89+ 		Health  health  = healthIndicator .health ();
90+ 		assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
91+ 	}
92+ 
93+ 	@ Test 
94+ 	void  oneForcedDownNodeShouldReturnDown () {
95+ 		CqlSession  session  = mock (CqlSession .class );
96+ 		Metadata  metadata  = mock (Metadata .class );
97+ 		Node  forcedDownNode  = mock (Node .class );
98+ 		given (forcedDownNode .getState ()).willReturn (NodeState .FORCED_DOWN );
99+ 		given (session .getMetadata ()).willReturn (metadata );
100+ 		given (metadata .getNodes ()).willReturn (createNodesMap (forcedDownNode ));
101+ 		CassandraDriverHealthIndicator  healthIndicator  = new  CassandraDriverHealthIndicator (session );
102+ 		Health  health  = healthIndicator .health ();
103+ 		assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
104+ 	}
105+ 
106+ 	@ Test 
107+ 	void  oneHealthyNodeAndOneUnhealthyNodeShouldReturnUp () {
108+ 		CqlSession  session  = mock (CqlSession .class );
109+ 		Metadata  metadata  = mock (Metadata .class );
110+ 		Node  healthyNode  = mock (Node .class );
111+ 		Node  unhealthyNode  = mock (Node .class );
112+ 		given (healthyNode .getState ()).willReturn (NodeState .UP );
113+ 		given (unhealthyNode .getState ()).willReturn (NodeState .DOWN );
114+ 		given (session .getMetadata ()).willReturn (metadata );
115+ 		given (metadata .getNodes ()).willReturn (createNodesMap (healthyNode , unhealthyNode ));
116+ 		CassandraDriverHealthIndicator  healthIndicator  = new  CassandraDriverHealthIndicator (session );
117+ 		Health  health  = healthIndicator .health ();
118+ 		assertThat (health .getStatus ()).isEqualTo (Status .UP );
119+ 	}
120+ 
121+ 	@ Test 
122+ 	void  oneHealthyNodeAndOneUnknownNodeShouldReturnUp () {
123+ 		CqlSession  session  = mock (CqlSession .class );
124+ 		Metadata  metadata  = mock (Metadata .class );
125+ 		Node  healthyNode  = mock (Node .class );
126+ 		Node  unknownNode  = mock (Node .class );
127+ 		given (healthyNode .getState ()).willReturn (NodeState .UP );
128+ 		given (unknownNode .getState ()).willReturn (NodeState .UNKNOWN );
129+ 		given (session .getMetadata ()).willReturn (metadata );
130+ 		given (metadata .getNodes ()).willReturn (createNodesMap (healthyNode , unknownNode ));
131+ 		CassandraDriverHealthIndicator  healthIndicator  = new  CassandraDriverHealthIndicator (session );
132+ 		Health  health  = healthIndicator .health ();
133+ 		assertThat (health .getStatus ()).isEqualTo (Status .UP );
134+ 	}
135+ 
136+ 	@ Test 
137+ 	void  oneHealthyNodeAndOneForcedDownNodeShouldReturnUp () {
138+ 		CqlSession  session  = mock (CqlSession .class );
139+ 		Metadata  metadata  = mock (Metadata .class );
140+ 		Node  healthyNode  = mock (Node .class );
141+ 		Node  forcedDownNode  = mock (Node .class );
142+ 		given (healthyNode .getState ()).willReturn (NodeState .UP );
143+ 		given (forcedDownNode .getState ()).willReturn (NodeState .FORCED_DOWN );
144+ 		given (session .getMetadata ()).willReturn (metadata );
145+ 		given (metadata .getNodes ()).willReturn (createNodesMap (healthyNode , forcedDownNode ));
146+ 		CassandraDriverHealthIndicator  healthIndicator  = new  CassandraDriverHealthIndicator (session );
147+ 		Health  health  = healthIndicator .health ();
148+ 		assertThat (health .getStatus ()).isEqualTo (Status .UP );
149+ 	}
150+ 
151+ 	@ Test 
152+ 	void  addVersionToDetailsIfReportedNotNull () {
153+ 		CqlSession  session  = mock (CqlSession .class );
154+ 		Metadata  metadata  = mock (Metadata .class );
155+ 		when (session .getMetadata ()).thenReturn (metadata );
156+ 		Node  node  = mock (Node .class );
157+ 		when (node .getState ()).thenReturn (NodeState .UP );
158+ 		when (node .getCassandraVersion ()).thenReturn (Version .V4_0_0 );
159+ 		when (metadata .getNodes ()).thenReturn (createNodesMap (node ));
160+ 
161+ 		CassandraDriverHealthIndicator  healthIndicator  = new  CassandraDriverHealthIndicator (session );
162+ 		Health  health  = healthIndicator .health ();
163+ 		assertThat (health .getStatus ()).isEqualTo (Status .UP );
164+ 		assertThat (health .getDetails ().get ("version" )).isEqualTo (Version .V4_0_0 );
165+ 	}
166+ 
167+ 	@ Test 
168+ 	void  doNotAddVersionToDetailsIfReportedNull () {
169+ 		CqlSession  session  = mock (CqlSession .class );
170+ 		Metadata  metadata  = mock (Metadata .class );
171+ 		when (session .getMetadata ()).thenReturn (metadata );
172+ 		Node  node  = mock (Node .class );
173+ 		when (node .getState ()).thenReturn (NodeState .UP );
174+ 		when (metadata .getNodes ()).thenReturn (createNodesMap (node ));
175+ 
176+ 		CassandraDriverHealthIndicator  healthIndicator  = new  CassandraDriverHealthIndicator (session );
177+ 		Health  health  = healthIndicator .health ();
178+ 		assertThat (health .getStatus ()).isEqualTo (Status .UP );
179+ 		assertThat (health .getDetails ().get ("version" )).isNull ();
61180	}
62181
63182	@ Test 
64183	void  healthWithCassandraDown () {
65184		CqlSession  session  = mock (CqlSession .class );
66- 		given (session .execute ( any ( SimpleStatement . class ) )).willThrow (new  DriverTimeoutException ("Test Exception" ));
185+ 		given (session .getMetadata ( )).willThrow (new  DriverTimeoutException ("Test Exception" ));
67186		CassandraDriverHealthIndicator  healthIndicator  = new  CassandraDriverHealthIndicator (session );
68187		Health  health  = healthIndicator .health ();
69188		assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
70189		assertThat (health .getDetails ().get ("error" ))
71190				.isEqualTo (DriverTimeoutException .class .getName () + ": Test Exception" );
72191	}
73192
193+ 	private  static  Map <UUID , Node > createNodesMap (Node ... nodes ) {
194+ 		Map <UUID , Node > nodesMap  = new  HashMap <>();
195+ 		for  (Node  n  : nodes ) {
196+ 			nodesMap .put (UUID .randomUUID (), n );
197+ 		}
198+ 		return  nodesMap ;
199+ 	}
200+ 
74201}
0 commit comments