8fb5e58fc433ba4f904b8adf54a5f0fac3432617
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / health-api / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / healthapi / HealthCheckServiceTest.java
1 /*
2  * Copyright © 2019-2020 Orange.
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.onap.ccsdk.cds.blueprintsprocessor.healthapi;
18
19
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;
25
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;
41
42 @RunWith(MockitoJUnitRunner.class)
43 public class HealthCheckServiceTest {
44
45   @Mock
46   private BasicAuthRestClientService basicAuthRestClientService;
47
48   @Mock
49   private BasicAuthRestClientProperties restClientProperties;
50
51   @InjectMocks
52   private HealthCheckService healthCheckService = new HealthCheckService();
53
54   @Before
55   public void setup() {
56   }
57
58   @Test
59   public void testSystemIsCompletelyDown() {
60
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);
69
70     });
71
72   }
73
74
75   @Test
76   public void testSystemIsUPAndRunning() {
77
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);
86
87     });
88
89   }
90
91   @Test
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);
101
102     });
103
104   }
105
106
107   @Test
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);
117
118     });
119
120   }
121
122   @Test
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);
132
133     });
134
135   }
136
137 }