Replaced all tabs with spaces in java and pom.xml
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / onap / so / client / appc / ApplicationControllerActionTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.client.appc;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.doThrow;
27 import static org.mockito.Mockito.times;
28 import static org.mockito.Mockito.verify;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.Optional;
32 import org.json.JSONObject;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.onap.appc.client.lcm.model.Action;
38 import org.onap.appc.client.lcm.model.Status;
39 import org.onap.so.bpmn.appc.payload.PayloadClient;
40 import org.onap.so.BaseTest;
41 import com.fasterxml.jackson.core.JsonProcessingException;
42
43
44 public class ApplicationControllerActionTest extends BaseTest {
45
46     private ApplicationControllerAction appCAction;
47
48     @Mock
49     private ApplicationControllerOrchestrator client;
50
51     @Before
52     public void setup() {
53         MockitoAnnotations.initMocks(this);
54
55         appCAction = new ApplicationControllerAction();
56         appCAction.client = client;
57     }
58
59     @Test
60     public void runAppCCommand_ResumeTraffic_Test()
61             throws ApplicationControllerOrchestratorException, JsonProcessingException {
62         // Prepare method
63         Action action = Action.ResumeTraffic;
64         String msoRequestId = "testMsoRequestId";
65         String vnfId = "testVnfId";
66         Optional<String> payload = Optional.empty();
67         Optional<String> vserverId = Optional.empty();
68         HashMap<String, String> payloadInfo = new HashMap<String, String>();
69         payloadInfo.put("vnfName", "testVnfName");
70         String controllerType = "testControllerType";
71
72         // Prepare mocks
73         Status status = new Status();
74         Optional<String> otherPayload = PayloadClient.resumeTrafficFormat(payloadInfo.get("vnfName"));
75         doReturn(status).when(client).vnfCommand(action, msoRequestId, vnfId, vserverId, otherPayload, controllerType);
76
77         // Run method
78         appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
79
80         // Verify call
81         verify(client, times(1)).vnfCommand(action, msoRequestId, vnfId, vserverId, otherPayload, controllerType);
82     }
83
84     @Test
85     public void runAppCCommand_Start_Test() throws ApplicationControllerOrchestratorException, JsonProcessingException {
86         runAppCCommand_StartStop_Test(Action.Start);
87     }
88
89     @Test
90     public void runAppCCommand_Stop_Test() throws ApplicationControllerOrchestratorException, JsonProcessingException {
91         runAppCCommand_StartStop_Test(Action.Stop);
92     }
93
94     private void runAppCCommand_StartStop_Test(Action action)
95             throws ApplicationControllerOrchestratorException, JsonProcessingException {
96         // Prepare method
97         String msoRequestId = "testMsoRequestId";
98         String vnfId = "testVnfId";
99         Optional<String> payload = Optional.empty();
100         Optional<String> vserverId = Optional.empty();
101         HashMap<String, String> payloadInfo = new HashMap<String, String>();
102         payloadInfo.put("vnfName", "testVnfName");
103         String controllerType = "testControllerType";
104
105         // Prepare mocks
106         Status status = new Status();
107         Optional<String> otherPayload = PayloadClient.startStopFormat(payloadInfo.get("vnfName"));
108         doReturn(status).when(client).vnfCommand(action, msoRequestId, vnfId, vserverId, otherPayload, controllerType);
109
110         // Run method
111         appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
112
113         // Verify call
114         verify(client, times(1)).vnfCommand(action, msoRequestId, vnfId, vserverId, otherPayload, controllerType);
115     }
116
117     @Test
118     public void runAppCCommand_Unlock_Test() throws ApplicationControllerOrchestratorException {
119         runAppCCommand_LockUnlock_Test(Action.Unlock);
120     }
121
122     @Test
123     public void runAppCCommand_Lock_Test() throws ApplicationControllerOrchestratorException {
124         runAppCCommand_LockUnlock_Test(Action.Lock);
125     }
126
127     private void runAppCCommand_LockUnlock_Test(Action action) throws ApplicationControllerOrchestratorException {
128         // Prepare method
129         String msoRequestId = "testMsoRequestId";
130         String vnfId = "testVnfId";
131         Optional<String> payload = Optional.empty();
132         Optional<String> vserverId = Optional.empty();
133         HashMap<String, String> payloadInfo = new HashMap<String, String>();
134         String controllerType = "testControllerType";
135
136         // Prepare mocks
137         Status status = new Status();
138         Optional<String> otherPayload = Optional.empty();
139         doReturn(status).when(client).vnfCommand(action, msoRequestId, vnfId, vserverId, otherPayload, controllerType);
140
141         // Run method
142         appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
143
144         // Verify call
145         verify(client, times(1)).vnfCommand(action, msoRequestId, vnfId, vserverId, otherPayload, controllerType);
146     }
147
148     @Test
149     public void runAppCCommand_QuiesceTraffic_PayloadPresent_Test()
150             throws ApplicationControllerOrchestratorException, JsonProcessingException {
151         // Prepare method
152         Action action = Action.QuiesceTraffic;
153         String msoRequestId = "testMsoRequestId";
154         String vnfId = "testVnfId";
155         Optional<String> payload = Optional.of("testPayload");
156         Optional<String> vserverId = Optional.empty();
157         HashMap<String, String> payloadInfo = new HashMap<String, String>();
158         payloadInfo.put("vnfName", "testVnfName");
159         String controllerType = "testControllerType";
160
161         // Prepare mocks
162         Status status = new Status();
163         Optional<String> modifiedPayload = PayloadClient.quiesceTrafficFormat(payload, payloadInfo.get("vnfName"));
164         doReturn(status).when(client).vnfCommand(action, msoRequestId, vnfId, vserverId, modifiedPayload,
165                 controllerType);
166
167         // Run method
168         appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
169
170         // Verify call
171         verify(client, times(1)).vnfCommand(action, msoRequestId, vnfId, vserverId, modifiedPayload, controllerType);
172     }
173
174     @Test
175     public void runAppCCommand_QuiesceTraffic_NoPayload_Test() throws ApplicationControllerOrchestratorException {
176         // Prepare method
177         Action action = Action.QuiesceTraffic;
178         String msoRequestId = "testMsoRequestId";
179         String vnfId = "testVnfId";
180         Optional<String> payload = Optional.empty();
181         HashMap<String, String> payloadInfo = new HashMap<String, String>();
182         String controllerType = "testControllerType";
183
184         // Run method
185         appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
186
187         // Verify non call
188         verify(client, times(0)).vnfCommand(any(), any(), any(), any(), any(), any());
189         assertEquals("Payload is not present for " + action.toString(), appCAction.getErrorMessage());
190     }
191
192     @Test
193     public void runAppCCommand_HealthCheck_Test()
194             throws ApplicationControllerOrchestratorException, JsonProcessingException {
195         // Prepare method
196         Action action = Action.HealthCheck;
197         String msoRequestId = "testMsoRequestId";
198         String vnfId = "testVnfId";
199         Optional<String> payload = Optional.empty();
200         Optional<String> vserverId = Optional.empty();
201         HashMap<String, String> payloadInfo = new HashMap<String, String>();
202         payloadInfo.put("vnfName", "testVnfName");
203         payloadInfo.put("vnfHostIpAddress", "testVnfHostIpAddress");
204         String controllerType = "testControllerType";
205
206         // Prepare mocks
207         Status status = new Status();
208         Optional<String> otherPayload =
209                 PayloadClient.healthCheckFormat(payloadInfo.get("vnfName"), payloadInfo.get("vnfHostIpAddress"));
210         doReturn(status).when(client).vnfCommand(action, msoRequestId, vnfId, vserverId, otherPayload, controllerType);
211
212         // Run method
213         appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
214
215         // Verify call
216         verify(client, times(1)).vnfCommand(action, msoRequestId, vnfId, vserverId, otherPayload, controllerType);
217     }
218
219     @Test
220     public void runAppCCommand_Snapshot_Test()
221             throws ApplicationControllerOrchestratorException, JsonProcessingException {
222         // Prepare method
223         Action action = Action.Snapshot;
224         String msoRequestId = "testMsoRequestId";
225         String vnfId = "testVnfId";
226         Optional<String> payload = Optional.empty();
227         HashMap<String, String> payloadInfo = new HashMap<String, String>();
228         payloadInfo.put("identityUrl", "testIdentityUrl");
229         ArrayList<String> vmIdList = new ArrayList<String>();
230         String vmId1 = "testlink:testVmId1";
231         vmIdList.add(vmId1);
232         String vmId2 = "testlink:testVmId2";
233         vmIdList.add(vmId2);
234         JSONObject vmIdListJson = new JSONObject();
235         vmIdListJson.put("vmIds", vmIdList);
236         payloadInfo.put("vmIdList", vmIdListJson.toString());
237         ArrayList<String> vserverIdList = new ArrayList<String>();
238         String vserverId1 = "testVserverId1";
239         Optional<String> vserverIdString1 = Optional.of(vserverId1);
240         vserverIdList.add(vserverId1);
241         String vserverId2 = "testVserverId2";
242         Optional<String> vserverIdString2 = Optional.of(vserverId2);
243         vserverIdList.add(vserverId2);
244
245         JSONObject vserverIdListJson = new JSONObject();
246         vserverIdListJson.put("vserverIds", vserverIdList);
247         payloadInfo.put("vserverIdList", vserverIdListJson.toString());
248         String controllerType = "testControllerType";
249
250         // Prepare mocks
251         Status status = new Status();
252         Optional<String> otherPayloadVm1 = PayloadClient.snapshotFormat(vmId1, payloadInfo.get("identityUrl"));
253         Optional<String> otherPayloadVm2 = PayloadClient.snapshotFormat(vmId2, payloadInfo.get("identityUrl"));
254         doReturn(status).when(client).vnfCommand(action, msoRequestId, vnfId, vserverIdString1, otherPayloadVm1,
255                 controllerType);
256         doReturn(status).when(client).vnfCommand(action, msoRequestId, vnfId, vserverIdString2, otherPayloadVm2,
257                 controllerType);
258
259         // Run method
260         appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
261
262         // Verify call
263         verify(client, times(1)).vnfCommand(action, msoRequestId, vnfId, vserverIdString1, otherPayloadVm1,
264                 controllerType);
265         verify(client, times(1)).vnfCommand(action, msoRequestId, vnfId, vserverIdString2, otherPayloadVm2,
266                 controllerType);
267     }
268
269     @Test
270     public void runAppCCommand_ConfigModify__PayloadPresent_Test() throws ApplicationControllerOrchestratorException {
271         // Prepare method
272         Action action = Action.ConfigModify;
273         String msoRequestId = "testMsoRequestId";
274         String vnfId = "testVnfId";
275         Optional<String> payload = Optional.of("testPayload");
276         Optional<String> vserverId = Optional.empty();
277         HashMap<String, String> payloadInfo = new HashMap<String, String>();
278         String controllerType = "testControllerType";
279
280         // Prepare mocks
281         Status status = new Status();
282         doReturn(status).when(client).vnfCommand(action, msoRequestId, vnfId, vserverId, payload, controllerType);
283
284         // Run method
285         appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
286
287         // Verify call
288         verify(client, times(1)).vnfCommand(action, msoRequestId, vnfId, vserverId, payload, controllerType);
289     }
290
291     @Test
292     public void runAppCCommand_ConfigModify__NoPayload_Test() throws ApplicationControllerOrchestratorException {
293         // Prepare method
294         Action action = Action.ConfigModify;
295         String msoRequestId = "testMsoRequestId";
296         String vnfId = "testVnfId";
297         Optional<String> payload = Optional.empty();
298         HashMap<String, String> payloadInfo = new HashMap<String, String>();
299         String controllerType = "testControllerType";
300
301         // Run method
302         appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
303
304         // Verify non call
305         verify(client, times(0)).vnfCommand(any(), any(), any(), any(), any(), any());
306         assertEquals("Payload is not present for " + action.toString(), appCAction.getErrorMessage());
307     }
308
309     @Test
310     public void runAppCCommand_UpgradePreCheck_PayloadPresent_Test()
311             throws ApplicationControllerOrchestratorException, JsonProcessingException {
312         runAppCCommand_Upgrade_PayloadPresent_Test(Action.UpgradePreCheck);
313     }
314
315     @Test
316     public void runAppCCommand_UpgradePostCheck_PayloadPresent_Test()
317             throws ApplicationControllerOrchestratorException, JsonProcessingException {
318         runAppCCommand_Upgrade_PayloadPresent_Test(Action.UpgradePostCheck);
319     }
320
321     @Test
322     public void runAppCCommand_UpgradeSoftware_PayloadPresent_Test()
323             throws ApplicationControllerOrchestratorException, JsonProcessingException {
324         runAppCCommand_Upgrade_PayloadPresent_Test(Action.UpgradeSoftware);
325     }
326
327     @Test
328     public void runAppCCommand_UpgradeBackup_PayloadPresent_Test()
329             throws ApplicationControllerOrchestratorException, JsonProcessingException {
330         runAppCCommand_Upgrade_PayloadPresent_Test(Action.UpgradeBackup);
331     }
332
333     private void runAppCCommand_Upgrade_PayloadPresent_Test(Action action)
334             throws ApplicationControllerOrchestratorException, JsonProcessingException {
335         // Prepare method
336         String msoRequestId = "testMsoRequestId";
337         String vnfId = "testVnfId";
338         Optional<String> payload = Optional.of("testPayload");
339         Optional<String> vserverId = Optional.empty();
340         HashMap<String, String> payloadInfo = new HashMap<String, String>();
341         payloadInfo.put("vnfName", "testVnfName");
342         String controllerType = "testControllerType";
343
344         // Prepare mocks
345         Status status = new Status();
346         Optional<String> modifiedPayload = PayloadClient.upgradeFormat(payload, payloadInfo.get("vnfName"));
347         doReturn(status).when(client).vnfCommand(action, msoRequestId, vnfId, vserverId, modifiedPayload,
348                 controllerType);
349
350         // Run method
351         appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
352
353         // Verify call
354         verify(client, times(1)).vnfCommand(action, msoRequestId, vnfId, vserverId, modifiedPayload, controllerType);
355     }
356
357     @Test
358     public void runAppCCommand_UpgradePreCheck_NoPayload_Test() throws ApplicationControllerOrchestratorException {
359         runAppCCommand_Upgrade_NoPayload_Test(Action.UpgradePreCheck);
360     }
361
362     @Test
363     public void runAppCCommand_UpgradePostCheck_NoPayload_Test() throws ApplicationControllerOrchestratorException {
364         runAppCCommand_Upgrade_NoPayload_Test(Action.UpgradePostCheck);
365     }
366
367     @Test
368     public void runAppCCommand_UpgradeSoftware_NoPayload_Test() throws ApplicationControllerOrchestratorException {
369         runAppCCommand_Upgrade_NoPayload_Test(Action.UpgradeSoftware);
370     }
371
372     @Test
373     public void runAppCCommand_UpgradeBackup_NoPayload_Test() throws ApplicationControllerOrchestratorException {
374         runAppCCommand_Upgrade_NoPayload_Test(Action.UpgradeBackup);
375     }
376
377     private void runAppCCommand_Upgrade_NoPayload_Test(Action action)
378             throws ApplicationControllerOrchestratorException {
379         // Prepare method
380         String msoRequestId = "testMsoRequestId";
381         String vnfId = "testVnfId";
382         Optional<String> payload = Optional.empty();
383         HashMap<String, String> payloadInfo = new HashMap<String, String>();
384         String controllerType = "testControllerType";
385
386         // Run method
387         appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
388
389         // Verify non call
390         verify(client, times(0)).vnfCommand(any(), any(), any(), any(), any(), any());
391         assertEquals("Payload is not present for " + action.toString(), appCAction.getErrorMessage());
392     }
393
394     @Test
395     public void runAppCCommand_InvalidAppCAction_Test() throws ApplicationControllerOrchestratorException {
396         // Prepare method
397         Action action = Action.ActionStatus;
398         String msoRequestId = "testMsoRequestId";
399         String vnfId = "testVnfId";
400         Optional<String> payload = Optional.empty();
401         HashMap<String, String> payloadInfo = new HashMap<String, String>();
402         String controllerType = "testControllerType";
403
404         // Run method
405         appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
406
407         // Verify non call
408         verify(client, times(0)).vnfCommand(any(), any(), any(), any(), any(), any());
409         // TODO For original author/architect: it appears that whoever coded this wanted the error message to be "Unable
410         // to idenify Action request for AppCClient" and this is not the case because of the
411         // ApplicationControllerSupport.getCategoryOf(appCStatus) call with a null appCStatus, so this may be something
412         // worth looking into
413     }
414
415     @Test
416     public void runAppCCommand_NormalAppCStatusGetErrorCode_Test()
417             throws ApplicationControllerOrchestratorException, JsonProcessingException {
418         // Prepare method
419         Action action = Action.Start;
420         String msoRequestId = "testMsoRequestId";
421         String vnfId = "testVnfId";
422         Optional<String> payload = Optional.empty();
423         Optional<String> vserverId = Optional.empty();
424         HashMap<String, String> payloadInfo = new HashMap<String, String>();
425         payloadInfo.put("vnfName", "testVnfName");
426         String controllerType = "testControllerType";
427
428         // Prepare mocks
429         Status status = new Status();
430         status.setCode(100);
431         Optional<String> otherPayload = PayloadClient.startStopFormat(payloadInfo.get("vnfName"));
432         doReturn(status).when(client).vnfCommand(action, msoRequestId, vnfId, vserverId, otherPayload, controllerType);
433
434         // Run method
435         appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
436
437         // Verify call
438         String expectedErrorCode = "0";
439         assertEquals(expectedErrorCode, appCAction.getErrorCode());
440     }
441
442     @Test
443     public void getErrorCode_Test() {
444         String defaultErrorCode = "1002";
445         // Verify default error code
446         assertEquals(defaultErrorCode, appCAction.getErrorCode());
447     }
448
449     @Test
450     public void getErrorMessage_Test() {
451         String defaultErrorMessage = "Unable to reach App C Servers";
452         // Verify default error message
453         assertEquals(defaultErrorMessage, appCAction.getErrorMessage());
454     }
455
456     @Test
457     public void applicationControllerOrchestratorExceptionCaught_Test()
458             throws ApplicationControllerOrchestratorException, JsonProcessingException {
459         // Prepare method
460         Action action = Action.Start;
461         String msoRequestId = "testMsoRequestId";
462         String vnfId = "testVnfId";
463         Optional<String> payload = Optional.empty();
464         Optional<String> vserverId = Optional.empty();
465         HashMap<String, String> payloadInfo = new HashMap<String, String>();
466         payloadInfo.put("vnfName", "testVnfName");
467         String controllerType = "testControllerType";
468
469         // Prepare mocks
470         Optional<String> otherPayload = PayloadClient.startStopFormat(payloadInfo.get("vnfName"));
471         String expectedErrorMessage = "Test appc orchestrator error message";
472         doThrow(new ApplicationControllerOrchestratorException(expectedErrorMessage, 0)).when(client).vnfCommand(action,
473                 msoRequestId, vnfId, vserverId, otherPayload, controllerType);
474
475         // Run method
476         appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
477
478         // Verify error
479         assertEquals(expectedErrorMessage, appCAction.getErrorMessage());
480         String expectedErrorCode = "1002";
481         assertEquals(expectedErrorCode, appCAction.getErrorCode());
482     }
483
484 }