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.Matchers.any;
29 import static org.mockito.Matchers.anyObject;
30 import static org.mockito.Matchers.anyString;
32 import java.util.Collections;
33 import java.util.List;
35 import org.springframework.test.util.ReflectionTestUtils;
36 import javax.ws.rs.container.ContainerRequestContext;
37 import javax.ws.rs.core.Response;
38 import javax.ws.rs.core.UriBuilder;
40 import org.springframework.core.ParameterizedTypeReference;
41 import org.springframework.http.HttpEntity;
42 import org.springframework.http.HttpMethod;
43 import org.springframework.http.HttpStatus;
44 import org.springframework.http.MediaType;
45 import org.json.JSONException;
46 import org.junit.Rule;
47 import org.junit.Test;
48 import org.mockito.InjectMocks;
49 import org.mockito.Matchers;
50 import org.mockito.Mock;
51 import org.mockito.Mockito;
52 import org.mockito.Spy;
53 import org.mockito.junit.MockitoJUnit;
54 import org.mockito.junit.MockitoRule;
55 import org.springframework.http.ResponseEntity;
56 import org.springframework.web.client.RestTemplate;
59 public class GlobalHealthcheckHandlerTest {
61 RestTemplate restTemplate;
64 ContainerRequestContext requestContext;
68 GlobalHealthcheckHandler globalhealth;
70 @Rule public MockitoRule mockitoRule = MockitoJUnit.rule();
73 public void testQuerySubsystemHealthNullResult(){
74 ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage");
75 ReflectionTestUtils.setField(globalhealth, "endpointBpmn", "http://localhost:8080");
77 Mockito.when(restTemplate.exchange(Matchers.any(URI.class),
78 Matchers.any(HttpMethod.class),
79 Matchers.<HttpEntity<?>> any(),
80 Matchers.<Class<Object>> any())).thenReturn(null);
82 String result = globalhealth.querySubsystemHealth(MsoSubsystems.BPMN);
83 System.out.println(result);
84 assertEquals(HealthcheckStatus.DOWN.toString(),result);
88 public void testQuerySubsystemHealthNotNullResult(){
89 ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage");
90 ReflectionTestUtils.setField(globalhealth, "endpointAsdc", "http://localhost:8080");
92 SubsystemHealthcheckResponse subSystemResponse = new SubsystemHealthcheckResponse();
93 subSystemResponse.setStatus("UP");
94 ResponseEntity<Object> r = new ResponseEntity<>(subSystemResponse,HttpStatus.OK);
96 Mockito.when(restTemplate.exchange(Matchers.any(URI.class),
97 Matchers.any(HttpMethod.class),
98 Matchers.<HttpEntity<?>> any(),
99 Matchers.<Class<Object>> any())).thenReturn(r);
101 String result = globalhealth.querySubsystemHealth(MsoSubsystems.ASDC);
102 System.out.println(result);
103 assertEquals(HealthcheckStatus.UP.toString(),result);
106 private Response globalHealthcheck (String status){
107 ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage");
108 ReflectionTestUtils.setField(globalhealth, "endpointAsdc", "http://localhost:8080");
109 ReflectionTestUtils.setField(globalhealth, "endpointSdnc", "http://localhost:8081");
110 ReflectionTestUtils.setField(globalhealth, "endpointBpmn", "http://localhost:8082");
111 ReflectionTestUtils.setField(globalhealth, "endpointCatalogdb", "http://localhost:8083");
112 ReflectionTestUtils.setField(globalhealth, "endpointOpenstack", "http://localhost:8084");
113 ReflectionTestUtils.setField(globalhealth, "endpointRequestdb", "http://localhost:8085");
114 ReflectionTestUtils.setField(globalhealth, "endpointRequestdbAttsvc", "http://localhost:8086");
116 SubsystemHealthcheckResponse subSystemResponse = new SubsystemHealthcheckResponse();
118 subSystemResponse.setStatus(status);
119 ResponseEntity<Object> r = new ResponseEntity<>(subSystemResponse,HttpStatus.OK);
120 Mockito.when(restTemplate.exchange(Matchers.any(URI.class),
121 Matchers.any(HttpMethod.class),
122 Matchers.<HttpEntity<?>> any(),
123 Matchers.<Class<Object>> any())).thenReturn(r);
125 Mockito.when(requestContext.getProperty(anyString())).thenReturn("1234567890");
126 Response response = globalhealth.globalHealthcheck(true, requestContext);
132 public void globalHealthcheckAllUPTest() throws JSONException {
133 Response response = globalHealthcheck("UP");
134 assertEquals(Response.Status.OK.getStatusCode(),response.getStatus());
135 HealthcheckResponse root;
136 root = (HealthcheckResponse) response.getEntity();
137 String apistatus = root.getApih();
138 assertTrue(apistatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
140 String bpmnstatus = root.getBpmn();
141 assertTrue(bpmnstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
143 String sdncstatus = root.getSdncAdapter();
144 assertTrue(sdncstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
146 String asdcstatus = root.getAsdcController();
147 assertTrue(asdcstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
149 String catastatus = root.getCatalogdbAdapter();
150 assertTrue(catastatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
152 String reqdbstatus = root.getRequestdbAdapter();
153 assertTrue(reqdbstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
155 String openstatus = root.getOpenstackAdapter();
156 assertTrue(openstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
158 String reqdbattstatus = root.getRequestdbAdapterAttsvc();
159 assertTrue(reqdbattstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
163 public void globalHealthcheckAllDOWNTest() throws JSONException {
164 Response response = globalHealthcheck("DOWN");
165 assertEquals(Response.Status.OK.getStatusCode(),response.getStatus());
166 HealthcheckResponse root;
167 root = (HealthcheckResponse) response.getEntity();
168 String apistatus = root.getApih();
169 assertTrue(apistatus.equalsIgnoreCase(HealthcheckStatus.UP.toString()));
171 String bpmnstatus = root.getBpmn();
172 assertTrue(bpmnstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));
174 String sdncstatus = root.getSdncAdapter();
175 assertTrue(sdncstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));
177 String asdcstatus = root.getAsdcController();
178 assertTrue(asdcstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));
180 String catastatus = root.getCatalogdbAdapter();
181 assertTrue(catastatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));
183 String reqdbstatus = root.getRequestdbAdapter();
184 assertTrue(reqdbstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));
186 String openstatus = root.getOpenstackAdapter();
187 assertTrue(openstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));
189 String reqdbattstatus = root.getRequestdbAdapterAttsvc();
190 assertTrue(reqdbattstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString()));
194 public void buildHttpEntityForRequestTest(){
195 HttpEntity<String> he = globalhealth.buildHttpEntityForRequest();
196 assertEquals (MediaType.APPLICATION_JSON,he.getHeaders().getAccept().get(0));
197 assertEquals (MediaType.APPLICATION_JSON,he.getHeaders().getContentType());
201 public void getEndpointUrlForSubsystemEnumTest(){
202 ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage");
203 ReflectionTestUtils.setField(globalhealth, "endpointAsdc", "http://localhost:8080");
204 ReflectionTestUtils.setField(globalhealth, "endpointSdnc", "http://localhost:8081");
205 ReflectionTestUtils.setField(globalhealth, "endpointBpmn", "http://localhost:8082");
206 ReflectionTestUtils.setField(globalhealth, "endpointCatalogdb", "http://localhost:8083");
207 ReflectionTestUtils.setField(globalhealth, "endpointOpenstack", "http://localhost:8084");
208 ReflectionTestUtils.setField(globalhealth, "endpointRequestdb", "http://localhost:8085");
209 ReflectionTestUtils.setField(globalhealth, "endpointRequestdbAttsvc", "http://localhost:8086");
211 String result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.ASDC);
212 assertEquals("http://localhost:8080", result);
213 result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.SDNC);
214 assertEquals("http://localhost:8081", result);
215 result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.BPMN);
216 assertEquals("http://localhost:8082", result);
217 result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.CATALOGDB);
218 assertEquals("http://localhost:8083", result);
219 result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.OPENSTACK);
220 assertEquals("http://localhost:8084", result);
221 result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.REQUESTDB);
222 assertEquals("http://localhost:8085", result);
223 result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.REQUESTDBATT);
224 assertEquals("http://localhost:8086", result);
228 public void processResponseFromSubsystemTest(){
229 SubsystemHealthcheckResponse subSystemResponse = new SubsystemHealthcheckResponse();
230 subSystemResponse.setStatus("UP");
231 ResponseEntity<SubsystemHealthcheckResponse> r = new ResponseEntity<>(subSystemResponse,HttpStatus.OK);
232 String result = globalhealth.processResponseFromSubsystem(r,MsoSubsystems.BPMN);
233 assertEquals("UP",result);