aboutsummaryrefslogtreecommitdiffstats
path: root/status-control/src/test/java/org/openecomp/mso/HealthCheckUtilsTest.java
blob: 712cdb2ce6f81d9d60ea4d510d6fc2188729fd6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*-
 * ============LICENSE_START=======================================================
 * OPENECOMP - MSO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.mso;


import org.openecomp.mso.logger.MsoLogger;
import org.openecomp.mso.properties.MsoJavaProperties;
import org.openecomp.mso.requestsdb.RequestsDatabase;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicStatusLine;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;

/**
 */
public class HealthCheckUtilsTest {

    private static HealthCheckUtils utils;
    private static String port = "8080";
    private static String ip1 = "localhost";
    private static String ip2 = "192.3.2.1";
    private static String iptest = "test/";
    private static String apihUrl1 = "/api/healthcheck";
    private static String apihUrl2 = "/api/healthcheck2";
    private static String bpmnUrl1 = "/bpmn/healthcheck";
    private static String raUrl1 = "/tenants/healthcheck";
    private static String raUrl2 = "/vnf/healthcheck";
    private static String raUrl3 = "/sdnc/healthcheck";
    private static MsoJavaProperties properties;
    private static String sslEnable = "false";
    private static CloseableHttpClient client;
    private static CloseableHttpResponse nokRes, okRes;

    @BeforeClass
    public static final void prepareMockvalues() {
        utils = Mockito.mock(HealthCheckUtils.class);
        client = Mockito.mock(CloseableHttpClient.class);
        nokRes = Mockito.mock(CloseableHttpResponse.class);
        okRes = Mockito.mock(CloseableHttpResponse.class);
        Mockito.when(nokRes.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_SERVICE_UNAVAILABLE, "FINE!"));;
        Mockito.when(okRes.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "FINE!"));

        properties = new MsoJavaProperties();
        properties.setProperty("server-port", port);
        properties.setProperty("ssl-enable", sslEnable);
        properties.setProperty("apih-load-balancer", ip1);
        properties.setProperty("apih-healthcheck-urn",apihUrl1 + "," + apihUrl2);
        properties.setProperty("camunda-load-balancer",ip1);
        properties.setProperty("camunda-healthcheck-urn",bpmnUrl1);
        properties.setProperty("jra-load-balancer",ip1);
        properties.setProperty("jra-healthcheck-urn",raUrl1 + "," + raUrl2 + "," + raUrl3);
        properties.setProperty("apih-nodehealthcheck-urn", apihUrl1);
        properties.setProperty("camunda-nodehealthcheck-urn", bpmnUrl1);
        properties.setProperty("jra-nodehealthcheck-urn", raUrl1);

        Mockito.when(utils.loadTopologyProperties()).thenReturn(properties);
        Mockito.when(utils.verifyNodeHealthCheck(HealthCheckUtils.NodeType.APIH, null)).thenCallRealMethod();
        Mockito.when(utils.verifyNodeHealthCheck(HealthCheckUtils.NodeType.RA, null)).thenCallRealMethod();
        Mockito.when(utils.verifyGlobalHealthCheck(true, null)).thenCallRealMethod();
        Mockito.when(utils.verifyGlobalHealthCheck(false, null)).thenCallRealMethod();

        Mockito.when(utils.getFinalUrl (ip1, port, raUrl1, sslEnable)).thenCallRealMethod();
        Mockito.when(utils.getFinalUrl (ip1, port, raUrl1, null)).thenCallRealMethod();
        Mockito.when(utils.getFinalUrl (ip1, port, raUrl1, "true")).thenCallRealMethod();
        Mockito.when(utils.getFinalUrl (ip1, port, raUrl1, "otherValue")).thenCallRealMethod();
        Mockito.when(utils.getFinalUrl (ip1, port, raUrl1, "True")).thenCallRealMethod();
        Mockito.when(utils.getFinalUrl (ip1, port, raUrl1, "TRUE")).thenCallRealMethod();
        Mockito.when(utils.getFinalUrl (ip1, null, raUrl1, null)).thenCallRealMethod();
        Mockito.when(utils.getFinalUrl (iptest, null, raUrl1, null)).thenCallRealMethod();

        System.setProperty("jboss.qualified.host.name", ip1);
    }

    @Test
    public final void testVerifyNodeHealthCheck () {
        Mockito.when (utils.verifyLocalHealth(ip1, port, apihUrl1, sslEnable, null)).thenReturn(true);
        Mockito.when (utils.verifyLocalHealth(ip1, port, apihUrl2, sslEnable, null)).thenReturn(true);
        Mockito.when (utils.verifyLocalHealth(ip2, port, apihUrl2, sslEnable, null)).thenReturn(true);
        Mockito.when (utils.verifyLocalHealth(ip2, port, apihUrl1, sslEnable, null)).thenReturn(false);
        Mockito.when (utils.verifyLocalHealth(ip1, port, raUrl1, sslEnable, null)).thenReturn(true);
        Mockito.when (utils.verifyLocalHealth(ip1, port, raUrl2, sslEnable, null)).thenReturn(false);
        Mockito.when (utils.verifyLocalHealth(ip1, port, raUrl3, sslEnable, null)).thenReturn(true);

        assertTrue (utils.verifyNodeHealthCheck (HealthCheckUtils.NodeType.APIH, null));
        assertFalse (utils.verifyNodeHealthCheck (HealthCheckUtils.NodeType.RA, null));

        Mockito.when (utils.verifyLocalHealth(ip1, port, apihUrl1, sslEnable, null)).thenReturn(false);
        Mockito.when (utils.verifyLocalHealth(ip1, port, raUrl2, sslEnable, null)).thenReturn(true);
        assertFalse (utils.verifyNodeHealthCheck (HealthCheckUtils.NodeType.APIH, null));
        assertTrue (utils.verifyNodeHealthCheck (HealthCheckUtils.NodeType.RA, null));

        Mockito.when (utils.verifyLocalHealth(ip2, port, apihUrl1, sslEnable, null)).thenReturn(true);
        assertFalse (utils.verifyNodeHealthCheck (HealthCheckUtils.NodeType.APIH, null));
        assertTrue (utils.verifyNodeHealthCheck (HealthCheckUtils.NodeType.RA, null));

    }

    @Test
    public final void testVerifyGlobalHealthCheckBPMN () {

        // healthcheck of bpmn returns false
        Mockito.when(utils.verifyLocalHealth(ip1, null, bpmnUrl1, null, null)).thenReturn(false);
        Mockito.when(utils.verifyLocalHealth(ip1, null, apihUrl1, null, null)).thenReturn(true);
        Mockito.when(utils.verifyLocalHealth(ip1, null, raUrl1, null, null)).thenReturn(true);

        // verify BPMN healthcheck
        assertFalse(utils.verifyGlobalHealthCheck (true, null));

        // do not verify BPMN healthcheck
        assertTrue(utils.verifyGlobalHealthCheck (false, null));

        Mockito.when(utils.verifyLocalHealth(ip1, null, bpmnUrl1, null, null)).thenReturn(true);
        assertTrue(utils.verifyGlobalHealthCheck (true, null));
    }

    @Test
    public final void testVerifyGlobalHealthCheckAPIH () {

        Mockito.when(utils.verifyLocalHealth(ip1, null, apihUrl1, null, null)).thenReturn(true);
        Mockito.when(utils.verifyLocalHealth(ip1, null, raUrl1, null, null)).thenReturn(true);
        Mockito.when(utils.verifyLocalHealth(ip1, null, bpmnUrl1, null, null)).thenReturn(true);
        assertTrue(utils.verifyGlobalHealthCheck (true, null));

        Mockito.when(utils.verifyLocalHealth(ip1, null, apihUrl1, null, null)).thenReturn(false);
        assertFalse(utils.verifyGlobalHealthCheck (true, null));
    }

    @Test
    public final void testVerifyGlobalHealthCheckRA () {
        // all health check apis returns true
        Mockito.when(utils.verifyLocalHealth(ip1, null, apihUrl1, null, null)).thenReturn(true);
        Mockito.when(utils.verifyLocalHealth(ip1, null, raUrl1, null, null)).thenReturn(true);
        Mockito.when(utils.verifyLocalHealth(ip1, null, bpmnUrl1, null, null)).thenReturn(true);
        assertTrue(utils.verifyGlobalHealthCheck (true, null));


        // 3rd ra api return false; others return true
        Mockito.when(utils.verifyLocalHealth(ip1, null, raUrl1, null, null)).thenReturn(false);
        assertFalse(utils.verifyGlobalHealthCheck (true, null));
    }

    @Test
    public final void testGetFinalUrl () {
        String finalUrl1 = utils.getFinalUrl (ip1, port, raUrl1, sslEnable);
        assertTrue (finalUrl1.equals ("http://" + ip1 + ":" + port + raUrl1));

        String finalUrl2 = utils.getFinalUrl (ip1, port, raUrl1, "true");
        assertTrue (finalUrl2.equals ("https://" + ip1 + ":" + port + raUrl1));

        String finalUrl3 = utils.getFinalUrl (ip1, port, raUrl1, null);
        assertTrue (finalUrl3.equals ("http://" + ip1 + ":" + port + raUrl1));

        String finalUrl4 = utils.getFinalUrl (ip1, port, raUrl1, "otherValue");
        assertTrue (finalUrl4.equals ("http://" + ip1 + ":" + port + raUrl1));

        String finalUrl5 = utils.getFinalUrl (ip1, port, raUrl1, "True");
        assertTrue (finalUrl5.equals ("https://" + ip1 + ":" + port + raUrl1));

        String finalUrl6 = utils.getFinalUrl (ip1, port, raUrl1, "TRUE");
        assertTrue (finalUrl6.equals ("https://" + ip1 + ":" + port + raUrl1));

        String finalUrl7 = utils.getFinalUrl (ip1, null, raUrl1, null);
        assertTrue (finalUrl7.equals (ip1 + raUrl1));

        String finalUrl8 = utils.getFinalUrl (iptest, null, raUrl1, null);
        assertTrue (finalUrl8.equals ("test" + raUrl1));
    }

    @Test
    public final void testVerifyLocalHealth() {
        HealthCheckUtils tempUtil = Mockito.mock(HealthCheckUtils.class);

        Mockito.when(tempUtil.verifyLocalHealth(ip1, port, apihUrl1, sslEnable, null)).thenCallRealMethod ();
        Mockito.when(tempUtil.getFinalUrl (ip1, port, apihUrl1, sslEnable)).thenCallRealMethod ();
        Mockito.when(tempUtil.getHttpClient()).thenReturn (client);

        try {
            Mockito.when (client.execute (any(HttpUriRequest.class))).thenReturn (okRes);
            Boolean res1 = (Boolean)invokeProtectedMethod(tempUtil, "verifyLocalHealth", ip1, port, apihUrl1, sslEnable, null);
            assertTrue(res1);

            Mockito.when (client.execute (any(HttpUriRequest.class))).thenReturn (nokRes);
            Boolean res2 = (Boolean)invokeProtectedMethod(tempUtil, "verifyLocalHealth", ip1, port, apihUrl1, sslEnable, null);
            assertFalse(res2);

        } catch (Exception e) {
            e.printStackTrace ();
        }

    }


    @Test
    public final void NullityCheck () {
        Mockito.when(utils.verifyLocalHealth(ip1, null, bpmnUrl1, null, null)).thenReturn(true);
        Mockito.when(utils.verifyLocalHealth(ip1, null, apihUrl1, null, null)).thenReturn(true);
        Mockito.when(utils.verifyLocalHealth(ip1, null, raUrl1, null, null)).thenReturn(true);

        assertTrue (utils.verifyGlobalHealthCheck (true, null));

        // mising server-port parameter
        MsoJavaProperties newProperties1 = new MsoJavaProperties();
        Mockito.when(utils.loadTopologyProperties()).thenReturn(newProperties1);

        newProperties1.setProperty("apih-load-balancer", ip1);
        newProperties1.setProperty("apih-nodehealthcheck-urn",apihUrl1);
        newProperties1.setProperty("camunda-load-balancer",ip1);
        newProperties1.setProperty("camunda-nodehealthcheck-urn",bpmnUrl1);
        newProperties1.setProperty("jra-load-balancer",ip1);
        newProperties1.setProperty("jra-nodehealthcheck-urn",raUrl1);

        assertFalse (utils.verifyGlobalHealthCheck (true, null));

        // mising apih-server-list parameter
        MsoJavaProperties newProperties2 = new MsoJavaProperties();
        Mockito.when(utils.loadTopologyProperties()).thenReturn(newProperties2);

        newProperties2.setProperty("server-port", port);
        newProperties1.setProperty("apih-nodehealthcheck-urn",apihUrl1);
        newProperties1.setProperty("camunda-load-balancer",ip1);
        newProperties1.setProperty("camunda-nodehealthcheck-urn",bpmnUrl1);
        newProperties1.setProperty("jra-load-balancer",ip1);
        newProperties1.setProperty("jra-nodehealthcheck-urn",raUrl1);

        assertFalse (utils.verifyGlobalHealthCheck (true, null));

        // mising jra-healthcheck-urn parameter
        MsoJavaProperties newProperties3 = new MsoJavaProperties();
        Mockito.when(utils.loadTopologyProperties()).thenReturn(newProperties3);

        newProperties3.setProperty("server-port", port);
        newProperties1.setProperty("apih-load-balancer", ip1);
        newProperties1.setProperty("apih-nodehealthcheck-urn",apihUrl1);
        newProperties1.setProperty("camunda-load-balancer",ip1);
        newProperties1.setProperty("camunda-nodehealthcheck-urn",bpmnUrl1);
        newProperties1.setProperty("jra-load-balancer",ip1);
        newProperties3.setProperty("jra-server-list",ip1);

        assertFalse (utils.verifyGlobalHealthCheck (true, null));

        Mockito.when(utils.loadTopologyProperties()).thenReturn(properties);
    }

    // User reflection to invoke to avoid change the publicity of the method
    private static Object invokeProtectedMethod (HealthCheckUtils tempUtil, String methodName, String arg1, String arg2, String arg3, String arg4, String arg5) {
        Method method;
        try {
            method = HealthCheckUtils.class.getDeclaredMethod(methodName, String.class, String.class, String.class, String.class, String.class);
            method.setAccessible(true);
            return  method.invoke(tempUtil, arg1, arg2, arg3, arg4, arg5);
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}