2 * Copyright © 2019-2020 Orange.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.healthapi;
19 import org.junit.Assert;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.InjectMocks;
24 import org.mockito.Mock;
25 import org.mockito.Mockito;
26 import org.mockito.junit.MockitoJUnitRunner;
27 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.domain.HealthApiResponse;
28 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.domain.HealthCheckStatus;
29 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.service.HealthCheckService;
30 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties;
31 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BasicAuthRestClientService;
32 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService.WebClientResponse;
33 import org.springframework.http.HttpMethod;
35 import static org.junit.Assert.assertEquals;
36 import static org.junit.Assert.assertNotNull;
37 import static org.mockito.ArgumentMatchers.any;
38 import static org.mockito.ArgumentMatchers.eq;
39 import static org.mockito.Mockito.anyString;
41 @RunWith(MockitoJUnitRunner.class)
42 public class HealthCheckServiceTest {
45 private BasicAuthRestClientService basicAuthRestClientService;
48 private BasicAuthRestClientProperties restClientProperties;
51 private HealthCheckService healthCheckService = new HealthCheckService();
58 public void testSystemIsCompletelyDown() {
60 Mockito.when(basicAuthRestClientService.exchangeResource(anyString(), anyString(), anyString())).
61 thenThrow(new RuntimeException());
62 HealthApiResponse healthApiResponse = healthCheckService.retrieveSystemStatus();
63 assertNotNull(healthApiResponse);
64 Assert.assertEquals(healthApiResponse.getStatus(), HealthCheckStatus.DOWN);
65 healthApiResponse.getChecks().stream().forEach(serviceEndpoint -> {
66 assertNotNull(serviceEndpoint);
67 assertEquals(serviceEndpoint.getStatus(), HealthCheckStatus.DOWN);
75 public void testSystemIsUPAndRunning() {
77 Mockito.when(basicAuthRestClientService.exchangeResource(eq(HttpMethod.GET.name()), anyString(), anyString())).
78 thenReturn(new WebClientResponse<>(200, "Success"));
79 HealthApiResponse healthApiResponse = healthCheckService.retrieveSystemStatus();
80 assertNotNull(healthApiResponse);
81 assertEquals(healthApiResponse.getStatus(), HealthCheckStatus.UP);
82 healthApiResponse.getChecks().stream().forEach(serviceEndpoint -> {
83 assertNotNull(serviceEndpoint);
84 assertEquals(serviceEndpoint.getStatus(), HealthCheckStatus.UP);
91 public void testServiceIsNotFound() {
92 Mockito.when(basicAuthRestClientService.exchangeResource(eq(HttpMethod.GET.name()), any(), anyString())).
93 thenReturn(new WebClientResponse<>(404, "failure"));
94 HealthApiResponse healthApiResponse = healthCheckService.retrieveSystemStatus();
95 assertNotNull(healthApiResponse);
96 assertEquals(healthApiResponse.getStatus(), HealthCheckStatus.DOWN);
97 healthApiResponse.getChecks().stream().forEach(serviceEndpoint -> {
98 assertNotNull(serviceEndpoint);
99 assertEquals(serviceEndpoint.getStatus(), HealthCheckStatus.DOWN);
107 public void testServiceInternalServerError() {
108 Mockito.when(basicAuthRestClientService.exchangeResource(eq(HttpMethod.GET.name()), any(), anyString()))
109 .thenReturn(new WebClientResponse<>(500, "failure"));
110 HealthApiResponse healthApiResponse = healthCheckService.retrieveSystemStatus();
111 assertNotNull(healthApiResponse);
112 assertEquals(healthApiResponse.getStatus(), HealthCheckStatus.DOWN);
113 healthApiResponse.getChecks().stream().forEach(serviceEndpoint -> {
114 assertNotNull(serviceEndpoint);
115 assertEquals(serviceEndpoint.getStatus(), HealthCheckStatus.DOWN);
122 public void testServiceIsRedirected() {
123 Mockito.when(basicAuthRestClientService.exchangeResource(eq(HttpMethod.GET.name()), any(), anyString()))
124 .thenReturn(new WebClientResponse<>(300, "failure"));
125 HealthApiResponse healthApiResponse = healthCheckService.retrieveSystemStatus();
126 assertNotNull(healthApiResponse);
127 assertEquals(healthApiResponse.getStatus(), HealthCheckStatus.DOWN);
128 healthApiResponse.getChecks().stream().forEach(serviceEndpoint -> {
129 assertNotNull(serviceEndpoint);
130 assertEquals(serviceEndpoint.getStatus(), HealthCheckStatus.DOWN);