|
| 1 | +/* |
| 2 | + * This program and the accompanying materials are made available under the terms of the |
| 3 | + * Eclipse Public License v2.0 which accompanies this distribution, and is available at |
| 4 | + * https://www.eclipse.org/legal/epl-v20.html |
| 5 | + * |
| 6 | + * SPDX-License-Identifier: EPL-2.0 |
| 7 | + * |
| 8 | + * Copyright Contributors to the Zowe Project. |
| 9 | + */ |
| 10 | +package org.zowe.apiml.discovery.health; |
| 11 | + |
| 12 | +import org.junit.jupiter.api.Assertions; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.springframework.boot.actuate.health.Health; |
| 15 | +import org.springframework.boot.actuate.health.Status; |
| 16 | +import org.springframework.cloud.client.DefaultServiceInstance; |
| 17 | +import org.springframework.cloud.client.discovery.DiscoveryClient; |
| 18 | +import org.zowe.apiml.product.constants.CoreService; |
| 19 | + |
| 20 | +import java.util.Collections; |
| 21 | + |
| 22 | +import static org.mockito.Mockito.mock; |
| 23 | +import static org.mockito.Mockito.when; |
| 24 | + |
| 25 | +class DiscoveryServiceHealthIndicatorTest { |
| 26 | + |
| 27 | + private final DiscoveryClient discoveryClient = mock(DiscoveryClient.class); |
| 28 | + private final DiscoveryServiceHealthIndicator discoverServiceHealthIndicator = new DiscoveryServiceHealthIndicator(discoveryClient); |
| 29 | + private final Health.Builder builder = new Health.Builder(); |
| 30 | + |
| 31 | + @Test |
| 32 | + void testStatusIsUpWhenGatewayIsAvailable() { |
| 33 | + when(discoveryClient.getInstances(CoreService.GATEWAY.getServiceId())).thenReturn( |
| 34 | + Collections.singletonList( |
| 35 | + new DefaultServiceInstance(null, CoreService.GATEWAY.getServiceId(), "host", 10010, true))); |
| 36 | + |
| 37 | + discoverServiceHealthIndicator.doHealthCheck(builder); |
| 38 | + |
| 39 | + Assertions.assertEquals(Status.UP, builder.build().getStatus()); |
| 40 | + Assertions.assertEquals(Status.UP, builder.build().getDetails().get("gateway")); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void testStatusIsPartialWhenGatewayIsNotAvailable() { |
| 45 | + when(discoveryClient.getInstances(CoreService.GATEWAY.getServiceId())).thenReturn(Collections.emptyList()); |
| 46 | + |
| 47 | + discoverServiceHealthIndicator.doHealthCheck(builder); |
| 48 | + |
| 49 | + Assertions.assertEquals(new Status("PARTIAL"), builder.build().getStatus()); |
| 50 | + Assertions.assertEquals("Authenticated endpoints not available.", builder.build().getStatus().getDescription()); |
| 51 | + Assertions.assertEquals(Status.DOWN, builder.build().getDetails().get("gateway")); |
| 52 | + } |
| 53 | +} |
0 commit comments