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;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertNotNull;
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.ArgumentMatchers.eq;
24 import static org.mockito.Mockito.anyString;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.InjectMocks;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.mockito.junit.MockitoJUnitRunner;
34 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.domain.HealthApiResponse;
35 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.domain.HealthCheckStatus;
36 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.service.HealthCheckService;
37 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties;
38 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BasicAuthRestClientService;
39 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService.WebClientResponse;
40 import org.springframework.http.HttpMethod;
42 @RunWith(MockitoJUnitRunner.class)
43 public class HealthCheckServiceTest {
46 private BasicAuthRestClientService basicAuthRestClientService;
49 private BasicAuthRestClientProperties restClientProperties;
52 private HealthCheckService healthCheckService = new HealthCheckService();
59 public void testSystemIsCompletelyDown() {
61 Mockito.when(basicAuthRestClientService.exchangeResource(anyString(), anyString(), anyString())).
62 thenThrow(new RuntimeException());
63 HealthApiResponse healthApiResponse = healthCheckService.retrieveSystemStatus();
64 assertNotNull(healthApiResponse);
65 Assert.assertEquals(healthApiResponse.getStatus(), HealthCheckStatus.DOWN);
66 healthApiResponse.getChecks().stream().forEach(serviceEndpoint -> {
67 assertNotNull(serviceEndpoint);
68 assertEquals(serviceEndpoint.getStatus(), HealthCheckStatus.DOWN);
76 public void testSystemIsUPAndRunning() {
78 Mockito.when(basicAuthRestClientService.exchangeResource(eq(HttpMethod.GET.name()), anyString(), anyString())).
79 thenReturn(new WebClientResponse<>(200, "Success"));
80 HealthApiResponse healthApiResponse = healthCheckService.retrieveSystemStatus();
81 assertNotNull(healthApiResponse);
82 assertEquals(healthApiResponse.getStatus(), HealthCheckStatus.UP);
83 healthApiResponse.getChecks().stream().forEach(serviceEndpoint -> {
84 assertNotNull(serviceEndpoint);
85 assertEquals(serviceEndpoint.getStatus(), HealthCheckStatus.UP);
92 public void testServiceIsNotFound() {
93 Mockito.when(basicAuthRestClientService.exchangeResource(eq(HttpMethod.GET.name()), any(), anyString())).
94 thenReturn(new WebClientResponse<>(404, "failure"));
95 HealthApiResponse healthApiResponse = healthCheckService.retrieveSystemStatus();
96 assertNotNull(healthApiResponse);
97 assertEquals(healthApiResponse.getStatus(), HealthCheckStatus.DOWN);
98 healthApiResponse.getChecks().stream().forEach(serviceEndpoint -> {
99 assertNotNull(serviceEndpoint);
100 assertEquals(serviceEndpoint.getStatus(), HealthCheckStatus.DOWN);
108 public void testServiceInternalServerError() {
109 Mockito.when(basicAuthRestClientService.exchangeResource(eq(HttpMethod.GET.name()), any(), anyString()))
110 .thenReturn(new WebClientResponse<>(500, "failure"));
111 HealthApiResponse healthApiResponse = healthCheckService.retrieveSystemStatus();
112 assertNotNull(healthApiResponse);
113 assertEquals(healthApiResponse.getStatus(), HealthCheckStatus.DOWN);
114 healthApiResponse.getChecks().stream().forEach(serviceEndpoint -> {
115 assertNotNull(serviceEndpoint);
116 assertEquals(serviceEndpoint.getStatus(), HealthCheckStatus.DOWN);
123 public void testServiceIsRedirected() {
124 Mockito.when(basicAuthRestClientService.exchangeResource(eq(HttpMethod.GET.name()), any(), anyString()))
125 .thenReturn(new WebClientResponse<>(300, "failure"));
126 HealthApiResponse healthApiResponse = healthCheckService.retrieveSystemStatus();
127 assertNotNull(healthApiResponse);
128 assertEquals(healthApiResponse.getStatus(), HealthCheckStatus.DOWN);
129 healthApiResponse.getChecks().stream().forEach(serviceEndpoint -> {
130 assertNotNull(serviceEndpoint);
131 assertEquals(serviceEndpoint.getStatus(), HealthCheckStatus.DOWN);