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