128c80adfca397b8bbbaf4b9e831a8ee21119746
[ccsdk/cds.git] /
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 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;
34
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;
40
41 @RunWith(MockitoJUnitRunner.class)
42 public class HealthCheckServiceTest {
43
44   @Mock
45   private BasicAuthRestClientService basicAuthRestClientService;
46
47   @Mock
48   private BasicAuthRestClientProperties restClientProperties;
49
50   @InjectMocks
51   private HealthCheckService healthCheckService = new HealthCheckService();
52
53   @Before
54   public void setup() {
55   }
56
57   @Test
58   public void testSystemIsCompletelyDown() {
59
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);
68
69     });
70
71   }
72
73
74   @Test
75   public void testSystemIsUPAndRunning() {
76
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);
85
86     });
87
88   }
89
90   @Test
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);
100
101     });
102
103   }
104
105
106   @Test
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);
116
117     });
118
119   }
120
121   @Test
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);
131
132     });
133
134   }
135
136 }