Skip to content

Commit db22a81

Browse files
OleksiiBondarsnicoll
authored andcommitted
Add unit test for cassandra health checker
See gh-15583
1 parent 06be905 commit db22a81

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2012-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.cassandra;
18+
19+
import com.datastax.driver.core.ResultSet;
20+
import com.datastax.driver.core.Row;
21+
import com.datastax.driver.core.querybuilder.Select;
22+
import org.junit.Test;
23+
24+
import org.springframework.boot.actuate.health.Health;
25+
import org.springframework.boot.actuate.health.Status;
26+
import org.springframework.data.cassandra.core.CassandraOperations;
27+
import org.springframework.data.cassandra.core.cql.CqlOperations;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
import static org.mockito.ArgumentMatchers.any;
31+
import static org.mockito.BDDMockito.given;
32+
import static org.mockito.Mockito.mock;
33+
34+
/**
35+
* Tests for {@link CassandraHealthIndicator}.
36+
*
37+
* @author Oleksii Bondar
38+
*/
39+
40+
public class CassandraHealthIndicatorTests {
41+
42+
@Test(expected = IllegalArgumentException.class)
43+
public void throwsExceptionOnNullCassandraOperations() {
44+
new CassandraHealthIndicator(null);
45+
}
46+
47+
@Test
48+
public void verifyHealthStatusWhenExhausted() {
49+
CassandraOperations cassandraOperations = mock(CassandraOperations.class);
50+
CqlOperations cqlOperations = mock(CqlOperations.class);
51+
ResultSet resultSet = mock(ResultSet.class);
52+
CassandraHealthIndicator healthIndicator = new CassandraHealthIndicator(
53+
cassandraOperations);
54+
given(cassandraOperations.getCqlOperations()).willReturn(cqlOperations);
55+
given(cqlOperations.queryForResultSet(any(Select.class))).willReturn(resultSet);
56+
given(resultSet.isExhausted()).willReturn(true);
57+
58+
Health health = healthIndicator.health();
59+
60+
assertThat(health.getStatus()).isEqualTo(Status.UP);
61+
}
62+
63+
@Test
64+
public void verifyHealthStatusWithVersion() {
65+
CassandraOperations cassandraOperations = mock(CassandraOperations.class);
66+
CqlOperations cqlOperations = mock(CqlOperations.class);
67+
ResultSet resultSet = mock(ResultSet.class);
68+
Row row = mock(Row.class);
69+
CassandraHealthIndicator healthIndicator = new CassandraHealthIndicator(
70+
cassandraOperations);
71+
given(cassandraOperations.getCqlOperations()).willReturn(cqlOperations);
72+
given(cqlOperations.queryForResultSet(any(Select.class))).willReturn(resultSet);
73+
given(resultSet.isExhausted()).willReturn(false);
74+
given(resultSet.one()).willReturn(row);
75+
String expectedVersion = "1.0.0";
76+
given(row.getString(0)).willReturn(expectedVersion);
77+
78+
Health health = healthIndicator.health();
79+
80+
assertThat(health.getStatus()).isEqualTo(Status.UP);
81+
assertThat(health.getDetails().get("version")).isEqualTo(expectedVersion);
82+
}
83+
84+
}

0 commit comments

Comments
 (0)