From db22a817425b19917a36c663625b3ac35303f3dd Mon Sep 17 00:00:00 2001 From: Oleksii Bondar Date: Sat, 29 Dec 2018 10:29:44 -0800 Subject: [PATCH 1/2] Add unit test for cassandra health checker See gh-15583 --- .../CassandraHealthIndicatorTests.java | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/cassandra/CassandraHealthIndicatorTests.java diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/cassandra/CassandraHealthIndicatorTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/cassandra/CassandraHealthIndicatorTests.java new file mode 100644 index 0000000000..7b9ffa19ea --- /dev/null +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/cassandra/CassandraHealthIndicatorTests.java @@ -0,0 +1,84 @@ +/* + * Copyright 2012-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.actuate.cassandra; + +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Row; +import com.datastax.driver.core.querybuilder.Select; +import org.junit.Test; + +import org.springframework.boot.actuate.health.Health; +import org.springframework.boot.actuate.health.Status; +import org.springframework.data.cassandra.core.CassandraOperations; +import org.springframework.data.cassandra.core.cql.CqlOperations; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; + +/** + * Tests for {@link CassandraHealthIndicator}. + * + * @author Oleksii Bondar + */ + +public class CassandraHealthIndicatorTests { + + @Test(expected = IllegalArgumentException.class) + public void throwsExceptionOnNullCassandraOperations() { + new CassandraHealthIndicator(null); + } + + @Test + public void verifyHealthStatusWhenExhausted() { + CassandraOperations cassandraOperations = mock(CassandraOperations.class); + CqlOperations cqlOperations = mock(CqlOperations.class); + ResultSet resultSet = mock(ResultSet.class); + CassandraHealthIndicator healthIndicator = new CassandraHealthIndicator( + cassandraOperations); + given(cassandraOperations.getCqlOperations()).willReturn(cqlOperations); + given(cqlOperations.queryForResultSet(any(Select.class))).willReturn(resultSet); + given(resultSet.isExhausted()).willReturn(true); + + Health health = healthIndicator.health(); + + assertThat(health.getStatus()).isEqualTo(Status.UP); + } + + @Test + public void verifyHealthStatusWithVersion() { + CassandraOperations cassandraOperations = mock(CassandraOperations.class); + CqlOperations cqlOperations = mock(CqlOperations.class); + ResultSet resultSet = mock(ResultSet.class); + Row row = mock(Row.class); + CassandraHealthIndicator healthIndicator = new CassandraHealthIndicator( + cassandraOperations); + given(cassandraOperations.getCqlOperations()).willReturn(cqlOperations); + given(cqlOperations.queryForResultSet(any(Select.class))).willReturn(resultSet); + given(resultSet.isExhausted()).willReturn(false); + given(resultSet.one()).willReturn(row); + String expectedVersion = "1.0.0"; + given(row.getString(0)).willReturn(expectedVersion); + + Health health = healthIndicator.health(); + + assertThat(health.getStatus()).isEqualTo(Status.UP); + assertThat(health.getDetails().get("version")).isEqualTo(expectedVersion); + } + +} From 4509164789b1b0b7db1a9bc5cde9fdc6f15e6036 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Sun, 30 Dec 2018 10:01:12 +0100 Subject: [PATCH 2/2] Polish "Add unit test for cassandra health checker" Closes gh-15583 --- .../cassandra/CassandraHealthIndicatorTests.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/cassandra/CassandraHealthIndicatorTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/cassandra/CassandraHealthIndicatorTests.java index 7b9ffa19ea..aacc4e71a7 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/cassandra/CassandraHealthIndicatorTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/cassandra/CassandraHealthIndicatorTests.java @@ -27,6 +27,7 @@ import org.springframework.data.cassandra.core.CassandraOperations; import org.springframework.data.cassandra.core.cql.CqlOperations; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -36,12 +37,12 @@ import static org.mockito.Mockito.mock; * * @author Oleksii Bondar */ - public class CassandraHealthIndicatorTests { - @Test(expected = IllegalArgumentException.class) - public void throwsExceptionOnNullCassandraOperations() { - new CassandraHealthIndicator(null); + @Test + public void createWhenCassandraOperationsIsNullShouldThrowException() { + assertThatIllegalArgumentException() + .isThrownBy(() -> new CassandraHealthIndicator(null)); } @Test @@ -54,9 +55,7 @@ public class CassandraHealthIndicatorTests { given(cassandraOperations.getCqlOperations()).willReturn(cqlOperations); given(cqlOperations.queryForResultSet(any(Select.class))).willReturn(resultSet); given(resultSet.isExhausted()).willReturn(true); - Health health = healthIndicator.health(); - assertThat(health.getStatus()).isEqualTo(Status.UP); } @@ -74,9 +73,7 @@ public class CassandraHealthIndicatorTests { given(resultSet.one()).willReturn(row); String expectedVersion = "1.0.0"; given(row.getString(0)).willReturn(expectedVersion); - Health health = healthIndicator.health(); - assertThat(health.getStatus()).isEqualTo(Status.UP); assertThat(health.getDetails().get("version")).isEqualTo(expectedVersion); }