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.openecomp.mso;
24 import org.openecomp.mso.logger.MsoLogger;
25 import org.openecomp.mso.properties.MsoJavaProperties;
26 import org.openecomp.mso.requestsdb.RequestsDatabase;
27 import org.apache.http.HttpStatus;
28 import org.apache.http.HttpVersion;
29 import org.apache.http.client.methods.CloseableHttpResponse;
30 import org.apache.http.client.methods.HttpGet;
31 import org.apache.http.client.methods.HttpUriRequest;
32 import org.apache.http.impl.client.CloseableHttpClient;
33 import org.apache.http.message.BasicStatusLine;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.mockito.Mockito;
38 import java.lang.reflect.InvocationTargetException;
39 import java.lang.reflect.Method;
41 import static org.junit.Assert.assertFalse;
42 import static org.junit.Assert.assertTrue;
43 import static org.mockito.Matchers.any;
47 public class HealthCheckUtilsTest {
49 private static HealthCheckUtils utils;
50 private static String port = "8080";
51 private static String ip1 = "localhost";
52 private static String ip2 = "192.3.2.1";
53 private static String iptest = "test/";
54 private static String apihUrl1 = "/api/healthcheck";
55 private static String apihUrl2 = "/api/healthcheck2";
56 private static String bpmnUrl1 = "/bpmn/healthcheck";
57 private static String raUrl1 = "/tenants/healthcheck";
58 private static String raUrl2 = "/vnf/healthcheck";
59 private static String raUrl3 = "/sdnc/healthcheck";
60 private static MsoJavaProperties properties;
61 private static String sslEnable = "false";
62 private static CloseableHttpClient client;
63 private static CloseableHttpResponse nokRes, okRes;
66 public static final void prepareMockvalues() {
67 utils = Mockito.mock(HealthCheckUtils.class);
68 client = Mockito.mock(CloseableHttpClient.class);
69 nokRes = Mockito.mock(CloseableHttpResponse.class);
70 okRes = Mockito.mock(CloseableHttpResponse.class);
71 Mockito.when(nokRes.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_SERVICE_UNAVAILABLE, "FINE!"));;
72 Mockito.when(okRes.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "FINE!"));
74 properties = new MsoJavaProperties();
75 properties.setProperty("server-port", port);
76 properties.setProperty("ssl-enable", sslEnable);
77 properties.setProperty("apih-load-balancer", ip1);
78 properties.setProperty("apih-healthcheck-urn",apihUrl1 + "," + apihUrl2);
79 properties.setProperty("camunda-load-balancer",ip1);
80 properties.setProperty("camunda-healthcheck-urn",bpmnUrl1);
81 properties.setProperty("jra-load-balancer",ip1);
82 properties.setProperty("jra-healthcheck-urn",raUrl1 + "," + raUrl2 + "," + raUrl3);
83 properties.setProperty("apih-nodehealthcheck-urn", apihUrl1);
84 properties.setProperty("camunda-nodehealthcheck-urn", bpmnUrl1);
85 properties.setProperty("jra-nodehealthcheck-urn", raUrl1);
87 Mockito.when(utils.loadTopologyProperties()).thenReturn(properties);
88 Mockito.when(utils.verifyNodeHealthCheck(HealthCheckUtils.NodeType.APIH, null)).thenCallRealMethod();
89 Mockito.when(utils.verifyNodeHealthCheck(HealthCheckUtils.NodeType.RA, null)).thenCallRealMethod();
90 Mockito.when(utils.verifyGlobalHealthCheck(true, null)).thenCallRealMethod();
91 Mockito.when(utils.verifyGlobalHealthCheck(false, null)).thenCallRealMethod();
93 Mockito.when(utils.getFinalUrl (ip1, port, raUrl1, sslEnable)).thenCallRealMethod();
94 Mockito.when(utils.getFinalUrl (ip1, port, raUrl1, null)).thenCallRealMethod();
95 Mockito.when(utils.getFinalUrl (ip1, port, raUrl1, "true")).thenCallRealMethod();
96 Mockito.when(utils.getFinalUrl (ip1, port, raUrl1, "otherValue")).thenCallRealMethod();
97 Mockito.when(utils.getFinalUrl (ip1, port, raUrl1, "True")).thenCallRealMethod();
98 Mockito.when(utils.getFinalUrl (ip1, port, raUrl1, "TRUE")).thenCallRealMethod();
99 Mockito.when(utils.getFinalUrl (ip1, null, raUrl1, null)).thenCallRealMethod();
100 Mockito.when(utils.getFinalUrl (iptest, null, raUrl1, null)).thenCallRealMethod();
102 System.setProperty("jboss.qualified.host.name", ip1);
106 public final void testVerifyNodeHealthCheck () {
107 Mockito.when (utils.verifyLocalHealth(ip1, port, apihUrl1, sslEnable, null)).thenReturn(true);
108 Mockito.when (utils.verifyLocalHealth(ip1, port, apihUrl2, sslEnable, null)).thenReturn(true);
109 Mockito.when (utils.verifyLocalHealth(ip2, port, apihUrl2, sslEnable, null)).thenReturn(true);
110 Mockito.when (utils.verifyLocalHealth(ip2, port, apihUrl1, sslEnable, null)).thenReturn(false);
111 Mockito.when (utils.verifyLocalHealth(ip1, port, raUrl1, sslEnable, null)).thenReturn(true);
112 Mockito.when (utils.verifyLocalHealth(ip1, port, raUrl2, sslEnable, null)).thenReturn(false);
113 Mockito.when (utils.verifyLocalHealth(ip1, port, raUrl3, sslEnable, null)).thenReturn(true);
115 assertTrue (utils.verifyNodeHealthCheck (HealthCheckUtils.NodeType.APIH, null));
116 assertFalse (utils.verifyNodeHealthCheck (HealthCheckUtils.NodeType.RA, null));
118 Mockito.when (utils.verifyLocalHealth(ip1, port, apihUrl1, sslEnable, null)).thenReturn(false);
119 Mockito.when (utils.verifyLocalHealth(ip1, port, raUrl2, sslEnable, null)).thenReturn(true);
120 assertFalse (utils.verifyNodeHealthCheck (HealthCheckUtils.NodeType.APIH, null));
121 assertTrue (utils.verifyNodeHealthCheck (HealthCheckUtils.NodeType.RA, null));
123 Mockito.when (utils.verifyLocalHealth(ip2, port, apihUrl1, sslEnable, null)).thenReturn(true);
124 assertFalse (utils.verifyNodeHealthCheck (HealthCheckUtils.NodeType.APIH, null));
125 assertTrue (utils.verifyNodeHealthCheck (HealthCheckUtils.NodeType.RA, null));
130 public final void testVerifyGlobalHealthCheckBPMN () {
132 // healthcheck of bpmn returns false
133 Mockito.when(utils.verifyLocalHealth(ip1, null, bpmnUrl1, null, null)).thenReturn(false);
134 Mockito.when(utils.verifyLocalHealth(ip1, null, apihUrl1, null, null)).thenReturn(true);
135 Mockito.when(utils.verifyLocalHealth(ip1, null, raUrl1, null, null)).thenReturn(true);
137 // verify BPMN healthcheck
138 assertFalse(utils.verifyGlobalHealthCheck (true, null));
140 // do not verify BPMN healthcheck
141 assertTrue(utils.verifyGlobalHealthCheck (false, null));
143 Mockito.when(utils.verifyLocalHealth(ip1, null, bpmnUrl1, null, null)).thenReturn(true);
144 assertTrue(utils.verifyGlobalHealthCheck (true, null));
148 public final void testVerifyGlobalHealthCheckAPIH () {
150 Mockito.when(utils.verifyLocalHealth(ip1, null, apihUrl1, null, null)).thenReturn(true);
151 Mockito.when(utils.verifyLocalHealth(ip1, null, raUrl1, null, null)).thenReturn(true);
152 Mockito.when(utils.verifyLocalHealth(ip1, null, bpmnUrl1, null, null)).thenReturn(true);
153 assertTrue(utils.verifyGlobalHealthCheck (true, null));
155 Mockito.when(utils.verifyLocalHealth(ip1, null, apihUrl1, null, null)).thenReturn(false);
156 assertFalse(utils.verifyGlobalHealthCheck (true, null));
160 public final void testVerifyGlobalHealthCheckRA () {
161 // all health check apis returns true
162 Mockito.when(utils.verifyLocalHealth(ip1, null, apihUrl1, null, null)).thenReturn(true);
163 Mockito.when(utils.verifyLocalHealth(ip1, null, raUrl1, null, null)).thenReturn(true);
164 Mockito.when(utils.verifyLocalHealth(ip1, null, bpmnUrl1, null, null)).thenReturn(true);
165 assertTrue(utils.verifyGlobalHealthCheck (true, null));
168 // 3rd ra api return false; others return true
169 Mockito.when(utils.verifyLocalHealth(ip1, null, raUrl1, null, null)).thenReturn(false);
170 assertFalse(utils.verifyGlobalHealthCheck (true, null));
174 public final void testGetFinalUrl () {
175 String finalUrl1 = utils.getFinalUrl (ip1, port, raUrl1, sslEnable);
176 assertTrue (finalUrl1.equals ("http://" + ip1 + ":" + port + raUrl1));
178 String finalUrl2 = utils.getFinalUrl (ip1, port, raUrl1, "true");
179 assertTrue (finalUrl2.equals ("https://" + ip1 + ":" + port + raUrl1));
181 String finalUrl3 = utils.getFinalUrl (ip1, port, raUrl1, null);
182 assertTrue (finalUrl3.equals ("http://" + ip1 + ":" + port + raUrl1));
184 String finalUrl4 = utils.getFinalUrl (ip1, port, raUrl1, "otherValue");
185 assertTrue (finalUrl4.equals ("http://" + ip1 + ":" + port + raUrl1));
187 String finalUrl5 = utils.getFinalUrl (ip1, port, raUrl1, "True");
188 assertTrue (finalUrl5.equals ("https://" + ip1 + ":" + port + raUrl1));
190 String finalUrl6 = utils.getFinalUrl (ip1, port, raUrl1, "TRUE");
191 assertTrue (finalUrl6.equals ("https://" + ip1 + ":" + port + raUrl1));
193 String finalUrl7 = utils.getFinalUrl (ip1, null, raUrl1, null);
194 assertTrue (finalUrl7.equals (ip1 + raUrl1));
196 String finalUrl8 = utils.getFinalUrl (iptest, null, raUrl1, null);
197 assertTrue (finalUrl8.equals ("test" + raUrl1));
201 public final void testVerifyLocalHealth() {
202 HealthCheckUtils tempUtil = Mockito.mock(HealthCheckUtils.class);
204 Mockito.when(tempUtil.verifyLocalHealth(ip1, port, apihUrl1, sslEnable, null)).thenCallRealMethod ();
205 Mockito.when(tempUtil.getFinalUrl (ip1, port, apihUrl1, sslEnable)).thenCallRealMethod ();
206 Mockito.when(tempUtil.getHttpClient()).thenReturn (client);
209 Mockito.when (client.execute (any(HttpUriRequest.class))).thenReturn (okRes);
210 Boolean res1 = (Boolean)invokeProtectedMethod(tempUtil, "verifyLocalHealth", ip1, port, apihUrl1, sslEnable, null);
213 Mockito.when (client.execute (any(HttpUriRequest.class))).thenReturn (nokRes);
214 Boolean res2 = (Boolean)invokeProtectedMethod(tempUtil, "verifyLocalHealth", ip1, port, apihUrl1, sslEnable, null);
217 } catch (Exception e) {
218 e.printStackTrace ();
225 public final void NullityCheck () {
226 Mockito.when(utils.verifyLocalHealth(ip1, null, bpmnUrl1, null, null)).thenReturn(true);
227 Mockito.when(utils.verifyLocalHealth(ip1, null, apihUrl1, null, null)).thenReturn(true);
228 Mockito.when(utils.verifyLocalHealth(ip1, null, raUrl1, null, null)).thenReturn(true);
230 assertTrue (utils.verifyGlobalHealthCheck (true, null));
232 // mising server-camunda parameter
233 MsoJavaProperties newProperties1 = new MsoJavaProperties();
234 Mockito.when(utils.loadTopologyProperties()).thenReturn(newProperties1);
236 newProperties1.setProperty("apih-load-balancer", ip1);
237 newProperties1.setProperty("apih-nodehealthcheck-urn",apihUrl1);
238 newProperties1.setProperty("jra-load-balancer",ip1);
239 newProperties1.setProperty("jra-nodehealthcheck-urn",raUrl1);
241 assertFalse (utils.verifyGlobalHealthCheck (true, null));
243 // mising apih-server-list parameter
244 MsoJavaProperties newProperties2 = new MsoJavaProperties();
245 Mockito.when(utils.loadTopologyProperties()).thenReturn(newProperties2);
247 newProperties2.setProperty("server-port", port);
248 newProperties2.setProperty("apih-nodehealthcheck-urn",apihUrl1);
249 newProperties2.setProperty("camunda-load-balancer",ip1);
250 newProperties2.setProperty("camunda-nodehealthcheck-urn",bpmnUrl1);
251 newProperties2.setProperty("jra-load-balancer",ip1);
252 newProperties2.setProperty("jra-nodehealthcheck-urn",raUrl1);
254 assertFalse (utils.verifyGlobalHealthCheck (true, null));
256 // mising jra-healthcheck-urn parameter
257 MsoJavaProperties newProperties3 = new MsoJavaProperties();
258 Mockito.when(utils.loadTopologyProperties()).thenReturn(newProperties3);
260 newProperties3.setProperty("server-port", port);
261 newProperties3.setProperty("apih-load-balancer", ip1);
262 newProperties3.setProperty("apih-nodehealthcheck-urn",apihUrl1);
263 newProperties3.setProperty("camunda-load-balancer",ip1);
264 newProperties3.setProperty("camunda-nodehealthcheck-urn",bpmnUrl1);
265 newProperties3.setProperty("jra-load-balancer",ip1);
266 newProperties3.setProperty("jra-server-list",ip1);
268 assertFalse (utils.verifyGlobalHealthCheck (true, null));
270 Mockito.when(utils.loadTopologyProperties()).thenReturn(properties);
273 // User reflection to invoke to avoid change the publicity of the method
274 private static Object invokeProtectedMethod (HealthCheckUtils tempUtil, String methodName, String arg1, String arg2, String arg3, String arg4, String arg5) {
277 method = HealthCheckUtils.class.getDeclaredMethod(methodName, String.class, String.class, String.class, String.class, String.class);
278 method.setAccessible(true);
279 return method.invoke(tempUtil, arg1, arg2, arg3, arg4, arg5);
280 } catch (NoSuchMethodException e) {
281 // TODO Auto-generated catch block
283 } catch (SecurityException e) {
284 // TODO Auto-generated catch block
286 } catch (IllegalAccessException e) {
287 // TODO Auto-generated catch block
289 } catch (IllegalArgumentException e) {
290 // TODO Auto-generated catch block
292 } catch (InvocationTargetException e) {
293 // TODO Auto-generated catch block