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.bpmn.appc.payload.PayloadClient;
42 import org.onap.so.BaseTest;
43 import com.fasterxml.jackson.core.JsonProcessingException;
46 public class ApplicationControllerActionTest extends BaseTest{
48 private ApplicationControllerAction appCAction;
51 private ApplicationControllerOrchestrator client;
55 MockitoAnnotations.initMocks(this);
57 appCAction = new ApplicationControllerAction();
58 appCAction.client = client;
62 public void runAppCCommand_ResumeTraffic_Test() throws ApplicationControllerOrchestratorException, JsonProcessingException {
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";
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);
79 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
82 verify(client, times(1)).vnfCommand(action, msoRequestId, vnfId, vserverId, otherPayload, controllerType);
86 public void runAppCCommand_Start_Test() throws ApplicationControllerOrchestratorException, JsonProcessingException {
87 runAppCCommand_StartStop_Test(Action.Start);
91 public void runAppCCommand_Stop_Test() throws ApplicationControllerOrchestratorException, JsonProcessingException {
92 runAppCCommand_StartStop_Test(Action.Stop);
95 private void runAppCCommand_StartStop_Test(Action action) throws ApplicationControllerOrchestratorException, JsonProcessingException {
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";
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);
111 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
114 verify(client, times(1)).vnfCommand(action, msoRequestId, vnfId, vserverId, otherPayload, controllerType);
118 public void runAppCCommand_Unlock_Test() throws ApplicationControllerOrchestratorException {
119 runAppCCommand_LockUnlock_Test(Action.Unlock);
123 public void runAppCCommand_Lock_Test() throws ApplicationControllerOrchestratorException {
124 runAppCCommand_LockUnlock_Test(Action.Lock);
127 private void runAppCCommand_LockUnlock_Test(Action action) throws ApplicationControllerOrchestratorException {
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";
137 Status status = new Status();
138 Optional<String> otherPayload = Optional.empty();
139 doReturn(status).when(client).vnfCommand(action, msoRequestId, vnfId, vserverId, otherPayload, controllerType);
142 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
145 verify(client, times(1)).vnfCommand(action, msoRequestId, vnfId, vserverId, otherPayload, controllerType);
149 public void runAppCCommand_QuiesceTraffic_PayloadPresent_Test() throws ApplicationControllerOrchestratorException, JsonProcessingException {
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";
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);
166 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
169 verify(client, times(1)).vnfCommand(action, msoRequestId, vnfId, vserverId, modifiedPayload, controllerType);
173 public void runAppCCommand_QuiesceTraffic_NoPayload_Test() throws ApplicationControllerOrchestratorException {
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";
183 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
186 verify(client, times(0)).vnfCommand(any(), any(), any(), any(), any(), any());
187 assertEquals("Payload is not present for " + action.toString(), appCAction.getErrorMessage());
191 public void runAppCCommand_HealthCheck_Test() throws ApplicationControllerOrchestratorException, JsonProcessingException {
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";
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);
209 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
212 verify(client, times(1)).vnfCommand(action, msoRequestId, vnfId, vserverId, otherPayload, controllerType);
216 public void runAppCCommand_Snapshot_Test() throws ApplicationControllerOrchestratorException, JsonProcessingException {
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";
227 String vmId2 = "testlink:testVmId2";
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);
240 JSONObject vserverIdListJson = new JSONObject();
241 vserverIdListJson.put("vserverIds", vserverIdList);
242 payloadInfo.put("vserverIdList", vserverIdListJson.toString());
243 String controllerType = "testControllerType";
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);
253 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
256 verify(client, times(1)).vnfCommand(action, msoRequestId, vnfId, vserverIdString1, otherPayloadVm1, controllerType);
257 verify(client, times(1)).vnfCommand(action, msoRequestId, vnfId, vserverIdString2, otherPayloadVm2, controllerType);
261 public void runAppCCommand_ConfigModify__PayloadPresent_Test() throws ApplicationControllerOrchestratorException {
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";
272 Status status = new Status();
273 doReturn(status).when(client).vnfCommand(action, msoRequestId, vnfId, vserverId, payload, controllerType);
276 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
279 verify(client, times(1)).vnfCommand(action, msoRequestId, vnfId, vserverId, payload, controllerType);
283 public void runAppCCommand_ConfigModify__NoPayload_Test() throws ApplicationControllerOrchestratorException {
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";
293 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
296 verify(client, times(0)).vnfCommand(any(), any(), any(), any(), any(), any());
297 assertEquals("Payload is not present for " + action.toString(), appCAction.getErrorMessage());
301 public void runAppCCommand_UpgradePreCheck_PayloadPresent_Test() throws ApplicationControllerOrchestratorException, JsonProcessingException {
302 runAppCCommand_Upgrade_PayloadPresent_Test(Action.UpgradePreCheck);
306 public void runAppCCommand_UpgradePostCheck_PayloadPresent_Test() throws ApplicationControllerOrchestratorException, JsonProcessingException {
307 runAppCCommand_Upgrade_PayloadPresent_Test(Action.UpgradePostCheck);
311 public void runAppCCommand_UpgradeSoftware_PayloadPresent_Test() throws ApplicationControllerOrchestratorException, JsonProcessingException {
312 runAppCCommand_Upgrade_PayloadPresent_Test(Action.UpgradeSoftware);
316 public void runAppCCommand_UpgradeBackup_PayloadPresent_Test() throws ApplicationControllerOrchestratorException, JsonProcessingException {
317 runAppCCommand_Upgrade_PayloadPresent_Test(Action.UpgradeBackup);
320 private void runAppCCommand_Upgrade_PayloadPresent_Test(Action action) throws ApplicationControllerOrchestratorException, JsonProcessingException {
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";
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);
336 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
339 verify(client, times(1)).vnfCommand(action, msoRequestId, vnfId, vserverId, modifiedPayload, controllerType);
343 public void runAppCCommand_UpgradePreCheck_NoPayload_Test() throws ApplicationControllerOrchestratorException {
344 runAppCCommand_Upgrade_NoPayload_Test(Action.UpgradePreCheck);
348 public void runAppCCommand_UpgradePostCheck_NoPayload_Test() throws ApplicationControllerOrchestratorException {
349 runAppCCommand_Upgrade_NoPayload_Test(Action.UpgradePostCheck);
353 public void runAppCCommand_UpgradeSoftware_NoPayload_Test() throws ApplicationControllerOrchestratorException {
354 runAppCCommand_Upgrade_NoPayload_Test(Action.UpgradeSoftware);
358 public void runAppCCommand_UpgradeBackup_NoPayload_Test() throws ApplicationControllerOrchestratorException {
359 runAppCCommand_Upgrade_NoPayload_Test(Action.UpgradeBackup);
362 private void runAppCCommand_Upgrade_NoPayload_Test(Action action) throws ApplicationControllerOrchestratorException {
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";
371 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
374 verify(client, times(0)).vnfCommand(any(), any(), any(), any(), any(), any());
375 assertEquals("Payload is not present for " + action.toString(), appCAction.getErrorMessage());
379 public void runAppCCommand_InvalidAppCAction_Test() throws ApplicationControllerOrchestratorException {
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";
389 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
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
397 public void runAppCCommand_NormalAppCStatusGetErrorCode_Test() throws ApplicationControllerOrchestratorException, JsonProcessingException {
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";
409 Status status = new Status();
411 Optional<String> otherPayload = PayloadClient.startStopFormat(payloadInfo.get("vnfName"));
412 doReturn(status).when(client).vnfCommand(action, msoRequestId, vnfId, vserverId, otherPayload, controllerType);
415 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
418 String expectedErrorCode = "0";
419 assertEquals(expectedErrorCode, appCAction.getErrorCode());
423 public void getErrorCode_Test() {
424 String defaultErrorCode = "1002";
425 //Verify default error code
426 assertEquals(defaultErrorCode, appCAction.getErrorCode());
430 public void getErrorMessage_Test() {
431 String defaultErrorMessage = "Unable to reach App C Servers";
432 //Verify default error message
433 assertEquals(defaultErrorMessage, appCAction.getErrorMessage());
437 public void applicationControllerOrchestratorExceptionCaught_Test() throws ApplicationControllerOrchestratorException, JsonProcessingException {
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";
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);
454 appCAction.runAppCCommand(action, msoRequestId, vnfId, payload, payloadInfo, controllerType);
457 assertEquals(expectedErrorMessage, appCAction.getErrorMessage());
458 String expectedErrorCode = "1002";
459 assertEquals(expectedErrorCode, appCAction.getErrorCode());