|
| 1 | +/* |
| 2 | + * Copyright 2019-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.cloud.gcp.autoconfigure.pubsub; |
| 18 | + |
| 19 | +import com.google.api.gax.grpc.GrpcStatusCode; |
| 20 | +import com.google.api.gax.rpc.ApiException; |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.runner.RunWith; |
| 23 | +import org.mockito.Mock; |
| 24 | +import org.mockito.junit.MockitoJUnitRunner; |
| 25 | + |
| 26 | +import org.springframework.boot.actuate.health.Status; |
| 27 | +import org.springframework.cloud.gcp.autoconfigure.pubsub.health.PubSubHealthIndicator; |
| 28 | +import org.springframework.cloud.gcp.pubsub.core.PubSubTemplate; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | +import static org.mockito.ArgumentMatchers.anyBoolean; |
| 32 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 33 | +import static org.mockito.ArgumentMatchers.anyString; |
| 34 | +import static org.mockito.Mockito.when; |
| 35 | + |
| 36 | +/** |
| 37 | + * Tests for the PubSub Health Indicator. |
| 38 | + * |
| 39 | + * @author Vinicius Carvalho |
| 40 | + */ |
| 41 | +@RunWith(MockitoJUnitRunner.class) |
| 42 | +public class PubSubHealthIndicatorTests { |
| 43 | + |
| 44 | + @Mock |
| 45 | + private PubSubTemplate pubSubTemplate; |
| 46 | + |
| 47 | + @Test |
| 48 | + public void healthUp() throws Exception { |
| 49 | + when(pubSubTemplate.pull(anyString(), anyInt(), anyBoolean())).thenThrow(new ApiException( |
| 50 | + new IllegalStateException("Illegal State"), GrpcStatusCode.of(io.grpc.Status.Code.NOT_FOUND), false)); |
| 51 | + PubSubHealthIndicator healthIndicator = new PubSubHealthIndicator(pubSubTemplate); |
| 52 | + assertThat(healthIndicator.health().getStatus()).isEqualTo(Status.UP); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void healthDown() { |
| 57 | + when(pubSubTemplate.pull(anyString(), anyInt(), anyBoolean())) |
| 58 | + .thenThrow(new ApiException(new IllegalStateException("Illegal State"), |
| 59 | + GrpcStatusCode.of(io.grpc.Status.Code.INVALID_ARGUMENT), false)); |
| 60 | + PubSubHealthIndicator healthIndicator = new PubSubHealthIndicator(pubSubTemplate); |
| 61 | + assertThat(healthIndicator.health().getStatus()).isEqualTo(Status.DOWN); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void healthDownGenericException() { |
| 66 | + when(pubSubTemplate.pull(anyString(), anyInt(), anyBoolean())) |
| 67 | + .thenThrow(new IllegalStateException("Illegal State")); |
| 68 | + PubSubHealthIndicator healthIndicator = new PubSubHealthIndicator(pubSubTemplate); |
| 69 | + assertThat(healthIndicator.health().getStatus()).isEqualTo(Status.DOWN); |
| 70 | + } |
| 71 | + |
| 72 | +} |
0 commit comments