16
16
17
17
package org .springframework .boot .actuate .cassandra ;
18
18
19
+ import java .util .HashMap ;
20
+ import java .util .Map ;
21
+ import java .util .UUID ;
22
+
19
23
import com .datastax .oss .driver .api .core .CqlSession ;
20
24
import 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 ;
24
29
import org .junit .jupiter .api .Test ;
25
30
26
31
import org .springframework .boot .actuate .health .Health ;
27
32
import org .springframework .boot .actuate .health .Status ;
28
33
29
34
import static org .assertj .core .api .Assertions .assertThat ;
30
35
import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
31
- import static org .mockito .ArgumentMatchers .any ;
32
36
import 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 ;
34
39
35
40
/**
36
41
* Tests for {@link CassandraDriverHealthIndicator}.
37
42
*
38
43
* @author Alexandre Dutra
44
+ * @author Tomasz Lelek
39
45
* @since 2.4.0
40
46
*/
41
47
class CassandraDriverHealthIndicatorTests {
@@ -46,29 +52,150 @@ void createWhenCqlSessionIsNullShouldThrowException() {
46
52
}
47
53
48
54
@ Test
49
- void healthWithCassandraUp () {
55
+ void oneHealthyNodeShouldReturnUp () {
50
56
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 ));
57
62
CassandraDriverHealthIndicator healthIndicator = new CassandraDriverHealthIndicator (session );
58
63
Health health = healthIndicator .health ();
59
64
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 ();
61
180
}
62
181
63
182
@ Test
64
183
void healthWithCassandraDown () {
65
184
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" ));
67
186
CassandraDriverHealthIndicator healthIndicator = new CassandraDriverHealthIndicator (session );
68
187
Health health = healthIndicator .health ();
69
188
assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
70
189
assertThat (health .getDetails ().get ("error" ))
71
190
.isEqualTo (DriverTimeoutException .class .getName () + ": Test Exception" );
72
191
}
73
192
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
+
74
201
}
0 commit comments