2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.client.appc;
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;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.Optional;
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;
44 import com.fasterxml.jackson.core.JsonProcessingException;
47 public class ApplicationControllerActionTest extends BaseTest{
49 private ApplicationControllerAction appCAction;
52 private ApplicationControllerOrchestrator client;
56 MockitoAnnotations.initMocks(this);
58 appCAction = new ApplicationControllerAction();
59 appCAction.client = client;
63 public void runAppCCommand_ResumeTraffic_Test() throws ApplicationControllerOrchestratorException, JsonProcessingException {
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";
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);
80 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
83 verify(client, times(1)).vnfCommand(action, msoRequestId, vnfId, vserverId, otherPayload, controllerType);
87 public void runAppCCommand_Start_Test() throws ApplicationControllerOrchestratorException, JsonProcessingException {
88 runAppCCommand_StartStop_Test(Action.Start);
92 public void runAppCCommand_Stop_Test() throws ApplicationControllerOrchestratorException, JsonProcessingException {
93 runAppCCommand_StartStop_Test(Action.Stop);
96 private void runAppCCommand_StartStop_Test(Action action) throws ApplicationControllerOrchestratorException, JsonProcessingException {
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";
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);
112 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
115 verify(client, times(1)).vnfCommand(action, msoRequestId, vnfId, vserverId, otherPayload, controllerType);
119 public void runAppCCommand_Unlock_Test() throws ApplicationControllerOrchestratorException {
120 runAppCCommand_LockUnlock_Test(Action.Unlock);
124 public void runAppCCommand_Lock_Test() throws ApplicationControllerOrchestratorException {
125 runAppCCommand_LockUnlock_Test(Action.Lock);
128 private void runAppCCommand_LockUnlock_Test(Action action) throws ApplicationControllerOrchestratorException {
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";
138 Status status = new Status();
139 Optional<String> otherPayload = Optional.empty();
140 doReturn(status).when(client).vnfCommand(action, msoRequestId, vnfId, vserverId, otherPayload, controllerType);
143 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
146 verify(client, times(1)).vnfCommand(action, msoRequestId, vnfId, vserverId, otherPayload, controllerType);
150 public void runAppCCommand_QuiesceTraffic_PayloadPresent_Test() throws ApplicationControllerOrchestratorException, JsonProcessingException {
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";
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);
167 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
170 verify(client, times(1)).vnfCommand(action, msoRequestId, vnfId, vserverId, modifiedPayload, controllerType);
174 public void runAppCCommand_QuiesceTraffic_NoPayload_Test() throws ApplicationControllerOrchestratorException {
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";
184 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
187 verify(client, times(0)).vnfCommand(any(), any(), any(), any(), any(), any());
188 assertEquals("Payload is not present for " + action.toString(), appCAction.getErrorMessage());
192 public void runAppCCommand_HealthCheck_Test() throws ApplicationControllerOrchestratorException, JsonProcessingException {
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";
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);
210 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
213 verify(client, times(1)).vnfCommand(action, msoRequestId, vnfId, vserverId, otherPayload, controllerType);
217 public void runAppCCommand_Snapshot_Test() throws ApplicationControllerOrchestratorException, JsonProcessingException {
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";
228 String vmId2 = "testlink:testVmId2";
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);
241 JSONObject vserverIdListJson = new JSONObject();
242 vserverIdListJson.put("vserverIds", vserverIdList);
243 payloadInfo.put("vserverIdList", vserverIdListJson.toString());
244 String controllerType = "testControllerType";
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);
254 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
257 verify(client, times(1)).vnfCommand(action, msoRequestId, vnfId, vserverIdString1, otherPayloadVm1, controllerType);
258 verify(client, times(1)).vnfCommand(action, msoRequestId, vnfId, vserverIdString2, otherPayloadVm2, controllerType);
262 public void runAppCCommand_ConfigModify__PayloadPresent_Test() throws ApplicationControllerOrchestratorException {
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";
273 Status status = new Status();
274 doReturn(status).when(client).vnfCommand(action, msoRequestId, vnfId, vserverId, payload, controllerType);
277 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
280 verify(client, times(1)).vnfCommand(action, msoRequestId, vnfId, vserverId, payload, controllerType);
284 public void runAppCCommand_ConfigModify__NoPayload_Test() throws ApplicationControllerOrchestratorException {
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";
294 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
297 verify(client, times(0)).vnfCommand(any(), any(), any(), any(), any(), any());
298 assertEquals("Payload is not present for " + action.toString(), appCAction.getErrorMessage());
302 public void runAppCCommand_UpgradePreCheck_PayloadPresent_Test() throws ApplicationControllerOrchestratorException, JsonProcessingException {
303 runAppCCommand_Upgrade_PayloadPresent_Test(Action.UpgradePreCheck);
307 public void runAppCCommand_UpgradePostCheck_PayloadPresent_Test() throws ApplicationControllerOrchestratorException, JsonProcessingException {
308 runAppCCommand_Upgrade_PayloadPresent_Test(Action.UpgradePostCheck);
312 public void runAppCCommand_UpgradeSoftware_PayloadPresent_Test() throws ApplicationControllerOrchestratorException, JsonProcessingException {
313 runAppCCommand_Upgrade_PayloadPresent_Test(Action.UpgradeSoftware);
317 public void runAppCCommand_UpgradeBackup_PayloadPresent_Test() throws ApplicationControllerOrchestratorException, JsonProcessingException {
318 runAppCCommand_Upgrade_PayloadPresent_Test(Action.UpgradeBackup);
321 private void runAppCCommand_Upgrade_PayloadPresent_Test(Action action) throws ApplicationControllerOrchestratorException, JsonProcessingException {
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";
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);
337 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
340 verify(client, times(1)).vnfCommand(action, msoRequestId, vnfId, vserverId, modifiedPayload, controllerType);
344 public void runAppCCommand_UpgradePreCheck_NoPayload_Test() throws ApplicationControllerOrchestratorException {
345 runAppCCommand_Upgrade_NoPayload_Test(Action.UpgradePreCheck);
349 public void runAppCCommand_UpgradePostCheck_NoPayload_Test() throws ApplicationControllerOrchestratorException {
350 runAppCCommand_Upgrade_NoPayload_Test(Action.UpgradePostCheck);
354 public void runAppCCommand_UpgradeSoftware_NoPayload_Test() throws ApplicationControllerOrchestratorException {
355 runAppCCommand_Upgrade_NoPayload_Test(Action.UpgradeSoftware);
359 public void runAppCCommand_UpgradeBackup_NoPayload_Test() throws ApplicationControllerOrchestratorException {
360 runAppCCommand_Upgrade_NoPayload_Test(Action.UpgradeBackup);
363 private void runAppCCommand_Upgrade_NoPayload_Test(Action action) throws ApplicationControllerOrchestratorException {
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";
372 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
375 verify(client, times(0)).vnfCommand(any(), any(), any(), any(), any(), any());
376 assertEquals("Payload is not present for " + action.toString(), appCAction.getErrorMessage());
380 public void runAppCCommand_InvalidAppCAction_Test() throws ApplicationControllerOrchestratorException {
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";
390 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
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
398 public void runAppCCommand_NormalAppCStatusGetErrorCode_Test() throws ApplicationControllerOrchestratorException, JsonProcessingException {
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";
410 Status status = new Status();
412 Optional<String> otherPayload = PayloadClient.startStopFormat(payloadInfo.get("vnfName"));
413 doReturn(status).when(client).vnfCommand(action, msoRequestId, vnfId, vserverId, otherPayload, controllerType);
416 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
419 String expectedErrorCode = "0";
420 assertEquals(expectedErrorCode, appCAction.getErrorCode());
424 public void getErrorCode_Test() {
425 String defaultErrorCode = "1002";
426 //Verify default error code
427 assertEquals(defaultErrorCode, appCAction.getErrorCode());
431 public void getErrorMessage_Test() {
432 String defaultErrorMessage = "Unable to reach App C Servers";
433 //Verify default error message
434 assertEquals(defaultErrorMessage, appCAction.getErrorMessage());
438 public void applicationControllerOrchestratorExceptionCaught_Test() throws ApplicationControllerOrchestratorException, JsonProcessingException {
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";
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);
455 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
458 assertEquals(expectedErrorMessage, appCAction.getErrorMessage());
459 String expectedErrorCode = "1002";
460 assertEquals(expectedErrorCode, appCAction.getErrorCode());