2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.apihandlerinfra;
23 import static org.junit.Assert.assertArrayEquals;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.ArgumentMatchers.anyObject;
29 import static org.mockito.ArgumentMatchers.anyString;
31 import java.util.Collections;
32 import java.util.List;
33 import org.springframework.test.util.ReflectionTestUtils;
34 import javax.ws.rs.container.ContainerRequestContext;
35 import javax.ws.rs.core.Response;
36 import javax.ws.rs.core.UriBuilder;
37 import org.springframework.core.ParameterizedTypeReference;
38 import org.springframework.http.HttpEntity;
39 import org.springframework.http.HttpMethod;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.http.MediaType;
42 import org.json.JSONException;
43 import org.junit.Rule;
44 import org.junit.Test;
45 import org.mockito.InjectMocks;
46 import org.mockito.ArgumentMatchers;
47 import org.mockito.Mock;
48 import org.mockito.Mockito;
49 import org.mockito.Spy;
50 import org.mockito.junit.MockitoJUnit;
51 import org.mockito.junit.MockitoRule;
52 import org.springframework.http.ResponseEntity;
53 import org.springframework.web.client.RestTemplate;
56 public class GlobalHealthcheckHandlerTest {
58 RestTemplate restTemplate;
61 ContainerRequestContext requestContext;
65 GlobalHealthcheckHandler globalhealth;
68 public MockitoRule mockitoRule = MockitoJUnit.rule();
71 public void testQuerySubsystemHealthNullResult() {
72 ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage");
73 ReflectionTestUtils.setField(globalhealth, "endpointBpmn", "http://localhost:8080");
75 Mockito.when(restTemplate.exchange(ArgumentMatchers.any(URI.class), ArgumentMatchers.any(HttpMethod.class),
76 ArgumentMatchers.<HttpEntity<?>>any(), ArgumentMatchers.<Class<Object>>any())).thenReturn(null);
78 String result = globalhealth.querySubsystemHealth(MsoSubsystems.BPMN);
79 System.out.println(result);
80 assertEquals(HealthcheckStatus.DOWN.toString(), result);
84 public void testQuerySubsystemHealthNotNullResult() {
85 ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage");
86 ReflectionTestUtils.setField(globalhealth, "endpointAsdc", "http://localhost:8080");
88 SubsystemHealthcheckResponse subSystemResponse = new SubsystemHealthcheckResponse();
89 subSystemResponse.setStatus("UP");
90 ResponseEntity<Object> r = new ResponseEntity<>(subSystemResponse, HttpStatus.OK);
92 Mockito.when(restTemplate.exchange(ArgumentMatchers.any(URI.class), ArgumentMatchers.any(HttpMethod.class),
93 ArgumentMatchers.<HttpEntity<?>>any(), ArgumentMatchers.<Class<Object>>any())).thenReturn(r);
95 String result = globalhealth.querySubsystemHealth(MsoSubsystems.ASDC);
96 System.out.println(result);
97 assertEquals(HealthcheckStatus.UP.toString(), result);
100 private Response globalHealthcheck(String status) {
101 ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage");
102 ReflectionTestUtils.setField(globalhealth, "endpointAsdc", "http://localhost:8080");
103 ReflectionTestUtils.setField(globalhealth, "endpointSdnc", "http://localhost:8081");
104 ReflectionTestUtils.setField(globalhealth, "endpointBpmn", "http://localhost:8082");
105 ReflectionTestUtils.setField(globalhealth, "endpointCatalogdb", "http://localhost:8083");
106 ReflectionTestUtils.setField(globalhealth, "endpointOpenstack", "http://localhost:8084");
107 ReflectionTestUtils.setField(globalhealth, "endpointRequestdb", "http://localhost:8085");
108 ReflectionTestUtils.setField(globalhealth, "endpointRequestdbAttsvc", "http://localhost:8086");
110 SubsystemHealthcheckResponse subSystemResponse = new SubsystemHealthcheckResponse();
112 subSystemResponse.setStatus(status);
113 ResponseEntity<Object> r = new ResponseEntity<>(subSystemResponse, HttpStatus.OK);
114 Mockito.when(restTemplate.exchange(ArgumentMatchers.any(URI.class), ArgumentMatchers.any(HttpMethod.class),
115 ArgumentMatchers.<HttpEntity<?>>any(), ArgumentMatchers.<Class<Object>>any())).thenReturn(r);
117 Mockito.when(requestContext.getProperty(anyString())).thenReturn("1234567890");
118 Response response = globalhealth.globalHealthcheck(true, requestContext);
124 public void globalHealthcheckAllUPTest() throws JSONException {
125 Response response = globalHealthcheck("UP");
126 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
127 HealthcheckResponse root;
128 root = (HealthcheckResponse) response.getEntity();
129 String apistatus = root.getApih();
130 assertTrue(apistatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
132 String bpmnstatus = root.getBpmn();
133 assertTrue(bpmnstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
135 String sdncstatus = root.getSdncAdapter();
136 assertTrue(sdncstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
138 String asdcstatus = root.getAsdcController();
139 assertTrue(asdcstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
141 String catastatus = root.getCatalogdbAdapter();
142 assertTrue(catastatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
144 String reqdbstatus = root.getRequestdbAdapter();
145 assertTrue(reqdbstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
147 String openstatus = root.getOpenstackAdapter();
148 assertTrue(openstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
150 String reqdbattstatus = root.getRequestdbAdapterAttsvc();
151 assertTrue(reqdbattstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
155 public void globalHealthcheckAllDOWNTest() throws JSONException {
156 Response response = globalHealthcheck("DOWN");
157 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
158 HealthcheckResponse root;
159 root = (HealthcheckResponse) response.getEntity();
160 String apistatus = root.getApih();
161 assertTrue(apistatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
163 String bpmnstatus = root.getBpmn();
164 assertTrue(bpmnstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));
166 String sdncstatus = root.getSdncAdapter();
167 assertTrue(sdncstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));
169 String asdcstatus = root.getAsdcController();
170 assertTrue(asdcstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));
172 String catastatus = root.getCatalogdbAdapter();
173 assertTrue(catastatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));
175 String reqdbstatus = root.getRequestdbAdapter();
176 assertTrue(reqdbstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));
178 String openstatus = root.getOpenstackAdapter();
179 assertTrue(openstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));
181 String reqdbattstatus = root.getRequestdbAdapterAttsvc();
182 assertTrue(reqdbattstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));
186 public void buildHttpEntityForRequestTest() {
187 HttpEntity<String> he = globalhealth.buildHttpEntityForRequest();
188 assertEquals(MediaType.APPLICATION_JSON, he.getHeaders().getAccept().get(0));
189 assertEquals(MediaType.APPLICATION_JSON, he.getHeaders().getContentType());
193 public void getEndpointUrlForSubsystemEnumTest() {
194 ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage");
195 ReflectionTestUtils.setField(globalhealth, "endpointAsdc", "http://localhost:8080");
196 ReflectionTestUtils.setField(globalhealth, "endpointSdnc", "http://localhost:8081");
197 ReflectionTestUtils.setField(globalhealth, "endpointBpmn", "http://localhost:8082");
198 ReflectionTestUtils.setField(globalhealth, "endpointCatalogdb", "http://localhost:8083");
199 ReflectionTestUtils.setField(globalhealth, "endpointOpenstack", "http://localhost:8084");
200 ReflectionTestUtils.setField(globalhealth, "endpointRequestdb", "http://localhost:8085");
201 ReflectionTestUtils.setField(globalhealth, "endpointRequestdbAttsvc", "http://localhost:8086");
203 String result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.ASDC);
204 assertEquals("http://localhost:8080", result);
205 result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.SDNC);
206 assertEquals("http://localhost:8081", result);
207 result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.BPMN);
208 assertEquals("http://localhost:8082", result);
209 result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.CATALOGDB);
210 assertEquals("http://localhost:8083", result);
211 result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.OPENSTACK);
212 assertEquals("http://localhost:8084", result);
213 result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.REQUESTDB);
214 assertEquals("http://localhost:8085", result);
215 result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.REQUESTDBATT);
216 assertEquals("http://localhost:8086", result);
220 public void processResponseFromSubsystemTest() {
221 SubsystemHealthcheckResponse subSystemResponse = new SubsystemHealthcheckResponse();
222 subSystemResponse.setStatus("UP");
223 ResponseEntity<SubsystemHealthcheckResponse> r = new ResponseEntity<>(subSystemResponse, HttpStatus.OK);
224 String result = globalhealth.processResponseFromSubsystem(r, MsoSubsystems.BPMN);
225 assertEquals("UP", result);