Skip to content

Commit 88b7b78

Browse files
committed
Make sure that cassandra health check reports version
Closes gh-20719
1 parent c9e32aa commit 88b7b78

File tree

2 files changed

+12
-25
lines changed

2 files changed

+12
-25
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cassandra/CassandraHealthIndicator.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.boot.actuate.cassandra;
1818

1919
import com.datastax.driver.core.ConsistencyLevel;
20-
import com.datastax.driver.core.ResultSet;
2120
import com.datastax.driver.core.SimpleStatement;
2221
import com.datastax.driver.core.Statement;
2322

@@ -58,12 +57,7 @@ public CassandraHealthIndicator(CassandraOperations cassandraOperations) {
5857

5958
@Override
6059
protected void doHealthCheck(Health.Builder builder) throws Exception {
61-
ResultSet results = this.cassandraOperations.getCqlOperations().queryForResultSet(SELECT);
62-
if (results.isFullyFetched()) {
63-
builder.up();
64-
return;
65-
}
66-
String version = results.one().getString(0);
60+
String version = this.cassandraOperations.getCqlOperations().queryForObject(SELECT, String.class);
6761
builder.up().withDetail("version", version);
6862
}
6963

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/cassandra/CassandraHealthIndicatorTests.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,27 @@
1616

1717
package org.springframework.boot.actuate.cassandra;
1818

19-
import com.datastax.driver.core.ResultSet;
20-
import com.datastax.driver.core.Row;
2119
import com.datastax.driver.core.Statement;
2220
import org.junit.Test;
2321

2422
import org.springframework.boot.actuate.health.Health;
2523
import org.springframework.boot.actuate.health.Status;
24+
import org.springframework.data.cassandra.CassandraInternalException;
2625
import org.springframework.data.cassandra.core.CassandraOperations;
2726
import org.springframework.data.cassandra.core.cql.CqlOperations;
2827

2928
import static org.assertj.core.api.Assertions.assertThat;
3029
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
3130
import static org.mockito.ArgumentMatchers.any;
31+
import static org.mockito.ArgumentMatchers.eq;
3232
import static org.mockito.BDDMockito.given;
3333
import static org.mockito.Mockito.mock;
3434

3535
/**
3636
* Tests for {@link CassandraHealthIndicator}.
3737
*
3838
* @author Oleksii Bondar
39+
* @author Stephane Nicoll
3940
*/
4041
public class CassandraHealthIndicatorTests {
4142

@@ -45,34 +46,26 @@ public void createWhenCassandraOperationsIsNullShouldThrowException() {
4546
}
4647

4748
@Test
48-
public void verifyHealthStatusWhenExhausted() {
49+
public void healthWithCassandraUp() {
4950
CassandraOperations cassandraOperations = mock(CassandraOperations.class);
5051
CqlOperations cqlOperations = mock(CqlOperations.class);
51-
ResultSet resultSet = mock(ResultSet.class);
5252
CassandraHealthIndicator healthIndicator = new CassandraHealthIndicator(cassandraOperations);
5353
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");
5655
Health health = healthIndicator.health();
5756
assertThat(health.getStatus()).isEqualTo(Status.UP);
57+
assertThat(health.getDetails().get("version")).isEqualTo("1.0.0");
5858
}
5959

6060
@Test
61-
public void verifyHealthStatusWithVersion() {
61+
public void healthWithCassandraDown() {
6262
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"));
6664
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);
7365
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");
7669
}
7770

7871
}

0 commit comments

Comments
 (0)