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