16
16
17
17
package org .springframework .boot .actuate .cassandra ;
18
18
19
- import com .datastax .driver .core .ResultSet ;
20
- import com .datastax .driver .core .Row ;
21
19
import com .datastax .driver .core .Statement ;
22
20
import org .junit .Test ;
23
21
24
22
import org .springframework .boot .actuate .health .Health ;
25
23
import org .springframework .boot .actuate .health .Status ;
24
+ import org .springframework .data .cassandra .CassandraInternalException ;
26
25
import org .springframework .data .cassandra .core .CassandraOperations ;
27
26
import org .springframework .data .cassandra .core .cql .CqlOperations ;
28
27
29
28
import static org .assertj .core .api .Assertions .assertThat ;
30
29
import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
31
30
import static org .mockito .ArgumentMatchers .any ;
31
+ import static org .mockito .ArgumentMatchers .eq ;
32
32
import static org .mockito .BDDMockito .given ;
33
33
import static org .mockito .Mockito .mock ;
34
34
35
35
/**
36
36
* Tests for {@link CassandraHealthIndicator}.
37
37
*
38
38
* @author Oleksii Bondar
39
+ * @author Stephane Nicoll
39
40
*/
40
41
public class CassandraHealthIndicatorTests {
41
42
@@ -45,34 +46,26 @@ public void createWhenCassandraOperationsIsNullShouldThrowException() {
45
46
}
46
47
47
48
@ Test
48
- public void verifyHealthStatusWhenExhausted () {
49
+ public void healthWithCassandraUp () {
49
50
CassandraOperations cassandraOperations = mock (CassandraOperations .class );
50
51
CqlOperations cqlOperations = mock (CqlOperations .class );
51
- ResultSet resultSet = mock (ResultSet .class );
52
52
CassandraHealthIndicator healthIndicator = new CassandraHealthIndicator (cassandraOperations );
53
53
given (cassandraOperations .getCqlOperations ()).willReturn (cqlOperations );
54
- given (cqlOperations .queryForResultSet (any (Statement .class ))).willReturn (resultSet );
55
- given (resultSet .isFullyFetched ()).willReturn (true );
54
+ given (cqlOperations .queryForObject (any (Statement .class ), eq (String .class ))).willReturn ("1.0.0" );
56
55
Health health = healthIndicator .health ();
57
56
assertThat (health .getStatus ()).isEqualTo (Status .UP );
57
+ assertThat (health .getDetails ().get ("version" )).isEqualTo ("1.0.0" );
58
58
}
59
59
60
60
@ Test
61
- public void verifyHealthStatusWithVersion () {
61
+ public void healthWithCassandraDown () {
62
62
CassandraOperations cassandraOperations = mock (CassandraOperations .class );
63
- CqlOperations cqlOperations = mock (CqlOperations .class );
64
- ResultSet resultSet = mock (ResultSet .class );
65
- Row row = mock (Row .class );
63
+ given (cassandraOperations .getCqlOperations ()).willThrow (new CassandraInternalException ("Connection failed" ));
66
64
CassandraHealthIndicator healthIndicator = new CassandraHealthIndicator (cassandraOperations );
67
- given (cassandraOperations .getCqlOperations ()).willReturn (cqlOperations );
68
- given (cqlOperations .queryForResultSet (any (Statement .class ))).willReturn (resultSet );
69
- given (resultSet .isFullyFetched ()).willReturn (false );
70
- given (resultSet .one ()).willReturn (row );
71
- String expectedVersion = "1.0.0" ;
72
- given (row .getString (0 )).willReturn (expectedVersion );
73
65
Health health = healthIndicator .health ();
74
- assertThat (health .getStatus ()).isEqualTo (Status .UP );
75
- assertThat (health .getDetails ().get ("version" )).isEqualTo (expectedVersion );
66
+ assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
67
+ assertThat (health .getDetails ().get ("error" ))
68
+ .isEqualTo (CassandraInternalException .class .getName () + ": Connection failed" );
76
69
}
77
70
78
71
}
0 commit comments