|
| 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.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
| 31 | +import static org.mockito.ArgumentMatchers.any; |
| 32 | +import static org.mockito.BDDMockito.given; |
| 33 | +import static org.mockito.Mockito.mock; |
| 34 | + |
| 35 | +/** |
| 36 | + * Tests for {@link CassandraHealthIndicator}. |
| 37 | + * |
| 38 | + * @author Oleksii Bondar |
| 39 | + */ |
| 40 | +public class CassandraHealthIndicatorTests { |
| 41 | + |
| 42 | + @Test |
| 43 | + public void createWhenCassandraOperationsIsNullShouldThrowException() { |
| 44 | + assertThatIllegalArgumentException() |
| 45 | + .isThrownBy(() -> new CassandraHealthIndicator(null)); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void verifyHealthStatusWhenExhausted() { |
| 50 | + CassandraOperations cassandraOperations = mock(CassandraOperations.class); |
| 51 | + CqlOperations cqlOperations = mock(CqlOperations.class); |
| 52 | + ResultSet resultSet = mock(ResultSet.class); |
| 53 | + CassandraHealthIndicator healthIndicator = new CassandraHealthIndicator( |
| 54 | + cassandraOperations); |
| 55 | + given(cassandraOperations.getCqlOperations()).willReturn(cqlOperations); |
| 56 | + given(cqlOperations.queryForResultSet(any(Select.class))).willReturn(resultSet); |
| 57 | + given(resultSet.isExhausted()).willReturn(true); |
| 58 | + Health health = healthIndicator.health(); |
| 59 | + assertThat(health.getStatus()).isEqualTo(Status.UP); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void verifyHealthStatusWithVersion() { |
| 64 | + CassandraOperations cassandraOperations = mock(CassandraOperations.class); |
| 65 | + CqlOperations cqlOperations = mock(CqlOperations.class); |
| 66 | + ResultSet resultSet = mock(ResultSet.class); |
| 67 | + Row row = mock(Row.class); |
| 68 | + CassandraHealthIndicator healthIndicator = new CassandraHealthIndicator( |
| 69 | + cassandraOperations); |
| 70 | + given(cassandraOperations.getCqlOperations()).willReturn(cqlOperations); |
| 71 | + given(cqlOperations.queryForResultSet(any(Select.class))).willReturn(resultSet); |
| 72 | + given(resultSet.isExhausted()).willReturn(false); |
| 73 | + given(resultSet.one()).willReturn(row); |
| 74 | + String expectedVersion = "1.0.0"; |
| 75 | + given(row.getString(0)).willReturn(expectedVersion); |
| 76 | + Health health = healthIndicator.health(); |
| 77 | + assertThat(health.getStatus()).isEqualTo(Status.UP); |
| 78 | + assertThat(health.getDetails().get("version")).isEqualTo(expectedVersion); |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments