Replaced all tabs with spaces in java and pom.xml
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / appc / tasks / AppcRunTasksTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.infrastructure.appc.tasks;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.ArgumentMatchers.isNull;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33 import org.junit.Test;
34 import org.mockito.InjectMocks;
35 import org.onap.appc.client.lcm.model.Action;
36 import org.onap.so.bpmn.BaseTaskTest;
37 import org.onap.so.bpmn.common.BuildingBlockExecution;
38 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
39 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
40 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
41 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
42 import org.onap.so.client.exception.BBObjectNotFoundException;
43 import org.onap.so.db.catalog.beans.ControllerSelectionReference;
44
45 public class AppcRunTasksTest extends BaseTaskTest {
46
47     @InjectMocks
48     private AppcRunTasks appcRunTasks = new AppcRunTasks();
49
50     @Test
51     public void mapRollbackVariablesTest() {
52
53         BuildingBlockExecution mock = mock(BuildingBlockExecution.class);
54
55         appcRunTasks.mapRollbackVariables(mock, Action.Lock, "1");
56         verify(mock, times(0)).setVariable(any(String.class), any());
57         appcRunTasks.mapRollbackVariables(mock, Action.Lock, "0");
58         verify(mock, times(1)).setVariable("rollbackVnfLock", true);
59         appcRunTasks.mapRollbackVariables(mock, Action.Unlock, "0");
60         verify(mock, times(1)).setVariable("rollbackVnfLock", false);
61         appcRunTasks.mapRollbackVariables(mock, Action.Start, "0");
62         verify(mock, times(1)).setVariable("rollbackVnfStop", false);
63         appcRunTasks.mapRollbackVariables(mock, Action.Stop, "0");
64         verify(mock, times(1)).setVariable("rollbackVnfStop", true);
65         appcRunTasks.mapRollbackVariables(mock, Action.QuiesceTraffic, "0");
66         verify(mock, times(1)).setVariable("rollbackQuiesceTraffic", true);
67         appcRunTasks.mapRollbackVariables(mock, Action.ResumeTraffic, "0");
68         verify(mock, times(1)).setVariable("rollbackQuiesceTraffic", false);
69     }
70
71     @Test
72     public void runAppcCommandVnfNull() throws BBObjectNotFoundException {
73         execution.getLookupMap().put(ResourceKey.GENERIC_VNF_ID, "NULL-TEST");
74         fillRequiredAppcExecutionFields();
75         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(null);
76         when(catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory(isNull(),
77                 eq(Action.Lock.toString()))).thenThrow(new IllegalArgumentException("name or values is null"));
78
79         appcRunTasks.runAppcCommand(execution, Action.Lock);
80
81         // if vnf = null -> vnfType = null ->
82         // IllegalArgumentException will be thrown in
83         // catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory
84         verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1002),
85                 eq("name or values is null"));
86     }
87
88     @Test
89     public void runAppcCommandBBObjectNotFoundException() throws BBObjectNotFoundException {
90         execution.getLookupMap().put(ResourceKey.GENERIC_VNF_ID, "EXCEPTION-TEST");
91         fillRequiredAppcExecutionFields();
92         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.GENERIC_VNF_ID)))
93                 .thenThrow(new BBObjectNotFoundException());
94
95         appcRunTasks.runAppcCommand(execution, Action.Lock);
96
97         verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000),
98                 eq("No valid VNF exists"));
99     }
100
101     @Test
102     public void runAppcCommandVfModuleNull() throws BBObjectNotFoundException {
103         execution.getLookupMap().put(ResourceKey.GENERIC_VNF_ID, "SUCCESS-TEST");
104         fillRequiredAppcExecutionFields();
105         GenericVnf genericVnf = getTestGenericVnf();
106         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(genericVnf);
107         mockReferenceResponse();
108         execution.getLookupMap().put(ResourceKey.VF_MODULE_ID, "VF-MODULE-ID-TEST");
109         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.VF_MODULE_ID))).thenReturn(null);
110         when(appCClient.getErrorCode()).thenReturn("0");
111
112         appcRunTasks.runAppcCommand(execution, Action.Lock);
113
114         assertEquals(true, execution.getVariable("rollbackVnfLock"));
115     }
116
117     @Test
118     public void runAppcCommand() throws BBObjectNotFoundException {
119         execution.getLookupMap().put(ResourceKey.GENERIC_VNF_ID, "SUCCESS-TEST");
120         fillRequiredAppcExecutionFields();
121         GenericVnf genericVnf = getTestGenericVnf();
122         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(genericVnf);
123         mockReferenceResponse();
124         execution.getLookupMap().put(ResourceKey.VF_MODULE_ID, "VF-MODULE-ID-TEST");
125         VfModule vfModule = new VfModule();
126         vfModule.setVfModuleId("VF-MODULE-ID");
127         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.VF_MODULE_ID))).thenReturn(vfModule);
128         when(appCClient.getErrorCode()).thenReturn("0");
129
130         appcRunTasks.runAppcCommand(execution, Action.Lock);
131
132         assertEquals(true, execution.getVariable("rollbackVnfLock"));
133     }
134
135     private void mockReferenceResponse() {
136         ControllerSelectionReference reference = new ControllerSelectionReference();
137         reference.setControllerName("TEST-CONTROLLER-NAME");
138         when(catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory(eq("TEST-VNF-TYPE"),
139                 eq(Action.Lock.toString()))).thenReturn(reference);
140     }
141
142     private void fillRequiredAppcExecutionFields() {
143         RequestContext context = new RequestContext();
144         context.setMsoRequestId("TEST-MSO-ID");
145         execution.setVariable("aicIdentity", "AIC-TEST");
146         execution.setVariable("vmIdList", "VM-ID-LIST-TEST");
147         execution.setVariable("vserverIdList", "VSERVER-ID-LIST");
148         execution.setVariable("identityUrl", "IDENTITY-URL-TEST");
149         execution.getGeneralBuildingBlock().setRequestContext(context);
150     }
151
152     private GenericVnf getTestGenericVnf() {
153         GenericVnf genericVnf = new GenericVnf();
154         genericVnf.setVnfId("TEST-VNF-ID");
155         genericVnf.setVnfType("TEST-VNF-TYPE");
156         genericVnf.setVnfName("TEST-VNF-NAME");
157         genericVnf.setIpv4OamAddress("129.0.0.1");
158         return genericVnf;
159     }
160 }