|
| 1 | +/* |
| 2 | + * Copyright 2012-2019 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 | + * https://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.health; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 21 | +import reactor.core.publisher.Mono; |
| 22 | + |
| 23 | +import org.springframework.boot.actuate.health.Health.Builder; |
| 24 | +import org.springframework.boot.test.system.CapturedOutput; |
| 25 | +import org.springframework.boot.test.system.OutputCaptureExtension; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | + |
| 29 | +/** |
| 30 | + * Tests for {@link AbstractReactiveHealthIndicator}. |
| 31 | + * |
| 32 | + * @author Moritz Halbritter |
| 33 | + */ |
| 34 | +@ExtendWith(OutputCaptureExtension.class) |
| 35 | +class AbstractReactiveHealthIndicatorTests { |
| 36 | + |
| 37 | + @Test |
| 38 | + void healthCheckWhenUpDoesNotLogHealthCheckFailedMessage(CapturedOutput output) { |
| 39 | + Health health = new AbstractReactiveHealthIndicator("Test message") { |
| 40 | + @Override |
| 41 | + protected Mono<Health> doHealthCheck(Builder builder) { |
| 42 | + return Mono.just(builder.up().build()); |
| 43 | + } |
| 44 | + |
| 45 | + }.health().block(); |
| 46 | + assertThat(health).isNotNull(); |
| 47 | + assertThat(health.getStatus()).isEqualTo(Status.UP); |
| 48 | + assertThat(output).doesNotContain("Test message"); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void healthCheckWhenDownWithExceptionThrownDoesNotLogHealthCheckFailedMessage(CapturedOutput output) { |
| 53 | + Health health = new AbstractReactiveHealthIndicator("Test message") { |
| 54 | + @Override |
| 55 | + protected Mono<Health> doHealthCheck(Builder builder) { |
| 56 | + throw new IllegalStateException("Test exception"); |
| 57 | + } |
| 58 | + }.health().block(); |
| 59 | + assertThat(health).isNotNull(); |
| 60 | + assertThat(health.getStatus()).isEqualTo(Status.DOWN); |
| 61 | + assertThat(output).contains("Test message").contains("Test exception"); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void healthCheckWhenDownWithExceptionConfiguredDoesNotLogHealthCheckFailedMessage(CapturedOutput output) { |
| 66 | + Health health = new AbstractReactiveHealthIndicator("Test message") { |
| 67 | + @Override |
| 68 | + protected Mono<Health> doHealthCheck(Builder builder) { |
| 69 | + return Mono.just(builder.down().withException(new IllegalStateException("Test exception")).build()); |
| 70 | + } |
| 71 | + }.health().block(); |
| 72 | + assertThat(health).isNotNull(); |
| 73 | + assertThat(health.getStatus()).isEqualTo(Status.DOWN); |
| 74 | + assertThat(output).contains("Test message").contains("Test exception"); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void healthCheckWhenDownWithExceptionConfiguredDoesNotLogHealthCheckFailedMessageTwice(CapturedOutput output) { |
| 79 | + Health health = new AbstractReactiveHealthIndicator("Test message") { |
| 80 | + @Override |
| 81 | + protected Mono<Health> doHealthCheck(Builder builder) { |
| 82 | + IllegalStateException ex = new IllegalStateException("Test exception"); |
| 83 | + builder.down().withException(ex); |
| 84 | + throw ex; |
| 85 | + } |
| 86 | + }.health().block(); |
| 87 | + assertThat(health).isNotNull(); |
| 88 | + assertThat(health.getStatus()).isEqualTo(Status.DOWN); |
| 89 | + assertThat(output).contains("Test message").containsOnlyOnce("Test exception"); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void healthCheckWhenDownWithExceptionAndNoFailureMessageLogsDefaultMessage(CapturedOutput output) { |
| 94 | + Health health = new AbstractReactiveHealthIndicator() { |
| 95 | + @Override |
| 96 | + protected Mono<Health> doHealthCheck(Builder builder) { |
| 97 | + return Mono.just(builder.down().withException(new IllegalStateException("Test exception")).build()); |
| 98 | + } |
| 99 | + }.health().block(); |
| 100 | + assertThat(health).isNotNull(); |
| 101 | + assertThat(health.getStatus()).isEqualTo(Status.DOWN); |
| 102 | + assertThat(output).contains("Health check failed").contains("Test exception"); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void healthCheckWhenDownWithErrorLogsDefaultMessage(CapturedOutput output) { |
| 107 | + Health health = new AbstractReactiveHealthIndicator("Test Message") { |
| 108 | + @Override |
| 109 | + protected Mono<Health> doHealthCheck(Builder builder) { |
| 110 | + return Mono.just(builder.down().withException(new Error("Test error")).build()); |
| 111 | + } |
| 112 | + }.health().block(); |
| 113 | + assertThat(health).isNotNull(); |
| 114 | + assertThat(health.getStatus()).isEqualTo(Status.DOWN); |
| 115 | + assertThat(output).contains("Health check failed").contains("Test error"); |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments