|
| 1 | +/* |
| 2 | + * Copyright 2012-2017 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.health; |
| 18 | + |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import org.junit.Before; |
| 23 | +import org.junit.Rule; |
| 24 | +import org.junit.Test; |
| 25 | +import org.junit.rules.ExpectedException; |
| 26 | +import org.junit.runner.RunWith; |
| 27 | +import org.mockito.Mock; |
| 28 | +import org.mockito.junit.MockitoJUnitRunner; |
| 29 | + |
| 30 | +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
| 31 | + |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
| 33 | +import static org.mockito.BDDMockito.given; |
| 34 | + |
| 35 | +/** |
| 36 | + * Tests for {@link AsynchronousHealthIndicatorRunner}. |
| 37 | + * |
| 38 | + * @author Vedran Pavic |
| 39 | + */ |
| 40 | +@RunWith(MockitoJUnitRunner.class) |
| 41 | +public class AsyncHealthIndicatorRunnerTests { |
| 42 | + |
| 43 | + @Rule |
| 44 | + public ExpectedException thrown = ExpectedException.none(); |
| 45 | + |
| 46 | + @Mock |
| 47 | + private HealthIndicator one; |
| 48 | + |
| 49 | + @Mock |
| 50 | + private HealthIndicator two; |
| 51 | + |
| 52 | + @Before |
| 53 | + public void setUp() { |
| 54 | + given(this.one.health()).willReturn(new Health.Builder().up().build()); |
| 55 | + given(this.two.health()).willReturn(new Health.Builder().unknown().build()); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void createAndRun() { |
| 60 | + Map<String, HealthIndicator> indicators = new HashMap<>(); |
| 61 | + indicators.put("one", this.one); |
| 62 | + indicators.put("two", this.two); |
| 63 | + ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); |
| 64 | + taskExecutor.setMaxPoolSize(2); |
| 65 | + taskExecutor.afterPropertiesSet(); |
| 66 | + HealthIndicatorRunner healthIndicatorRunner = new AsynchronousHealthIndicatorRunner( |
| 67 | + taskExecutor); |
| 68 | + Map<String, Health> healths = healthIndicatorRunner.run(indicators); |
| 69 | + assertThat(healths.size()).isEqualTo(2); |
| 70 | + assertThat(healths.get("one").getStatus()).isEqualTo(Status.UP); |
| 71 | + assertThat(healths.get("two").getStatus()).isEqualTo(Status.UNKNOWN); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void createWithNullTaskExecutor() { |
| 76 | + this.thrown.expect(IllegalArgumentException.class); |
| 77 | + this.thrown.expectMessage("TaskExecutor must not be null"); |
| 78 | + new AsynchronousHealthIndicatorRunner(null); |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments