From 2e9ca0668887403bd5c00963d20a2af29869ee47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Sun, 26 Nov 2017 20:16:43 -0500 Subject: [PATCH 1/2] Add CouchbaseHealthIndicatorTests See gh-11161 --- .../CouchbaseHealthIndicatorTests.java | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/couchbase/CouchbaseHealthIndicatorTests.java diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/couchbase/CouchbaseHealthIndicatorTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/couchbase/CouchbaseHealthIndicatorTests.java new file mode 100644 index 00000000000..3535cb6a79d --- /dev/null +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/couchbase/CouchbaseHealthIndicatorTests.java @@ -0,0 +1,67 @@ +/* + * Copyright 2012-2017 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.couchbase; + +import java.util.Arrays; + +import com.couchbase.client.java.cluster.ClusterInfo; +import com.couchbase.client.java.util.features.Version; +import org.junit.Test; + +import org.springframework.boot.actuate.health.Health; +import org.springframework.boot.actuate.health.Status; +import org.springframework.data.couchbase.core.CouchbaseOperations; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +/** + * Tests for {@link CouchbaseHealthIndicator} + * + * @author EddĂș MelĂ©ndez + */ +public class CouchbaseHealthIndicatorTests { + + @Test + public void couchbaseIsUp() { + CouchbaseOperations couchbaseOperations = mock(CouchbaseOperations.class); + ClusterInfo clusterInfo = mock(ClusterInfo.class); + given(clusterInfo.getAllVersions()).willReturn( + Arrays.asList(new Version(1, 2, 3))); + given(couchbaseOperations.getCouchbaseClusterInfo()).willReturn(clusterInfo); + CouchbaseHealthIndicator healthIndicator = new CouchbaseHealthIndicator( + couchbaseOperations); + Health health = healthIndicator.health(); + assertThat(health.getStatus()).isEqualTo(Status.UP); + assertThat(health.getDetails().get("versions")).isEqualTo("1.2.3"); + verify(clusterInfo).getAllVersions(); + } + + @Test + public void couchbaseIsDown() { + CouchbaseOperations couchbaseOperations = mock(CouchbaseOperations.class); + given(couchbaseOperations.getCouchbaseClusterInfo()).willThrow(new NullPointerException()); + CouchbaseHealthIndicator healthIndicator = new CouchbaseHealthIndicator( + couchbaseOperations); + Health health = healthIndicator.health(); + assertThat(health.getStatus()).isEqualTo(Status.DOWN); + assertThat((String) health.getDetails().get("error")).contains("null"); + verify(couchbaseOperations).getCouchbaseClusterInfo(); + } +} From c4387e1e8a8b22e1ed19980d1e997e1f601e78b3 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 27 Nov 2017 10:30:31 +0100 Subject: [PATCH 2/2] Polish "Add CouchbaseHealthIndicatorTests" Closes gh-11161 --- .../actuate/couchbase/CouchbaseHealthIndicatorTests.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/couchbase/CouchbaseHealthIndicatorTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/couchbase/CouchbaseHealthIndicatorTests.java index 3535cb6a79d..9c3ee8a5ed9 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/couchbase/CouchbaseHealthIndicatorTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/couchbase/CouchbaseHealthIndicatorTests.java @@ -56,12 +56,14 @@ public class CouchbaseHealthIndicatorTests { @Test public void couchbaseIsDown() { CouchbaseOperations couchbaseOperations = mock(CouchbaseOperations.class); - given(couchbaseOperations.getCouchbaseClusterInfo()).willThrow(new NullPointerException()); + given(couchbaseOperations.getCouchbaseClusterInfo()).willThrow( + new IllegalStateException("test, expected")); CouchbaseHealthIndicator healthIndicator = new CouchbaseHealthIndicator( couchbaseOperations); Health health = healthIndicator.health(); assertThat(health.getStatus()).isEqualTo(Status.DOWN); - assertThat((String) health.getDetails().get("error")).contains("null"); + assertThat((String) health.getDetails().get("error")).contains("test, expected"); verify(couchbaseOperations).getCouchbaseClusterInfo(); } + }