a8518d90166c9323014c4b3088125d2b8bbfcba9
[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
34 import org.junit.Test;
35 import org.mockito.InjectMocks;
36 import org.onap.appc.client.lcm.model.Action;
37 import org.onap.so.bpmn.BaseTaskTest;
38 import org.onap.so.bpmn.common.BuildingBlockExecution;
39 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
40 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
41 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
42 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
43 import org.onap.so.client.exception.BBObjectNotFoundException;
44 import org.onap.so.db.catalog.beans.ControllerSelectionReference;
45
46 public class AppcRunTasksTest extends BaseTaskTest {
47
48         @InjectMocks
49         private AppcRunTasks appcRunTasks = new AppcRunTasks();
50
51         @Test
52         public void mapRollbackVariablesTest() {
53                 
54                 BuildingBlockExecution mock = mock(BuildingBlockExecution.class);
55                 
56                 appcRunTasks.mapRollbackVariables(mock, Action.Lock, "1");
57                 verify(mock, times(0)).setVariable(any(String.class), any());
58                 appcRunTasks.mapRollbackVariables(mock, Action.Lock, "0");
59                 verify(mock, times(1)).setVariable("rollbackVnfLock", true);
60                 appcRunTasks.mapRollbackVariables(mock, Action.Unlock, "0");
61                 verify(mock, times(1)).setVariable("rollbackVnfLock", false);
62                 appcRunTasks.mapRollbackVariables(mock, Action.Start, "0");
63                 verify(mock, times(1)).setVariable("rollbackVnfStop", false);
64                 appcRunTasks.mapRollbackVariables(mock, Action.Stop, "0");
65                 verify(mock, times(1)).setVariable("rollbackVnfStop", true);
66                 appcRunTasks.mapRollbackVariables(mock, Action.QuiesceTraffic, "0");
67                 verify(mock, times(1)).setVariable("rollbackQuiesceTraffic", true);
68                 appcRunTasks.mapRollbackVariables(mock, Action.ResumeTraffic, "0");
69                 verify(mock, times(1)).setVariable("rollbackQuiesceTraffic", false);
70         }
71
72     @Test
73     public void runAppcCommandVnfNull() throws BBObjectNotFoundException {
74         execution.getLookupMap().put(ResourceKey.GENERIC_VNF_ID, "NULL-TEST");
75         fillRequiredAppcExecutionFields();
76         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.GENERIC_VNF_ID)))
77             .thenReturn(null);
78         when(catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory(
79             isNull(), eq(Action.Lock.toString()))).
80             thenThrow(new IllegalArgumentException("name or values is null"));
81
82         appcRunTasks.runAppcCommand(execution, Action.Lock);
83
84         // if vnf = null -> vnfType = null ->
85         // IllegalArgumentException will be thrown in catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory
86         verify(exceptionUtil, times(1)).
87             buildAndThrowWorkflowException(
88                 any(BuildingBlockExecution.class), eq(1002), eq("name or values is null"));
89     }
90
91     @Test
92     public void runAppcCommandBBObjectNotFoundException() throws BBObjectNotFoundException {
93         execution.getLookupMap().put(ResourceKey.GENERIC_VNF_ID, "EXCEPTION-TEST");
94         fillRequiredAppcExecutionFields();
95         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.GENERIC_VNF_ID)))
96             .thenThrow(new BBObjectNotFoundException());
97
98         appcRunTasks.runAppcCommand(execution, Action.Lock);
99
100         verify(exceptionUtil, times(1)).
101             buildAndThrowWorkflowException(
102                 any(BuildingBlockExecution.class), eq(7000), eq("No valid VNF exists"));
103     }
104
105     @Test
106     public void runAppcCommandVfModuleNull() throws BBObjectNotFoundException {
107         execution.getLookupMap().put(ResourceKey.GENERIC_VNF_ID, "SUCCESS-TEST");
108         fillRequiredAppcExecutionFields();
109         GenericVnf genericVnf = getTestGenericVnf();
110         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.GENERIC_VNF_ID)))
111             .thenReturn(genericVnf);
112         mockReferenceResponse();
113         execution.getLookupMap().put(ResourceKey.VF_MODULE_ID, "VF-MODULE-ID-TEST");
114         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.VF_MODULE_ID)))
115             .thenReturn(null);
116         when(appCClient.getErrorCode()).thenReturn("0");
117
118         appcRunTasks.runAppcCommand(execution, Action.Lock);
119
120         assertEquals(true, execution.getVariable("rollbackVnfLock"));
121     }
122
123     @Test
124     public void runAppcCommand() throws BBObjectNotFoundException {
125         execution.getLookupMap().put(ResourceKey.GENERIC_VNF_ID, "SUCCESS-TEST");
126         fillRequiredAppcExecutionFields();
127         GenericVnf genericVnf = getTestGenericVnf();
128         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.GENERIC_VNF_ID)))
129             .thenReturn(genericVnf);
130         mockReferenceResponse();
131         execution.getLookupMap().put(ResourceKey.VF_MODULE_ID, "VF-MODULE-ID-TEST");
132         VfModule vfModule = new VfModule();
133         vfModule.setVfModuleId("VF-MODULE-ID");
134         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.VF_MODULE_ID)))
135             .thenReturn(vfModule);
136         when(appCClient.getErrorCode()).thenReturn("0");
137
138         appcRunTasks.runAppcCommand(execution, Action.Lock);
139
140         assertEquals(true, execution.getVariable("rollbackVnfLock"));
141     }
142
143     private void mockReferenceResponse() {
144         ControllerSelectionReference reference = new ControllerSelectionReference();
145         reference.setControllerName("TEST-CONTROLLER-NAME");
146         when(catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory(
147             eq("TEST-VNF-TYPE"), eq(Action.Lock.toString()))).thenReturn(reference);
148     }
149
150     private void fillRequiredAppcExecutionFields() {
151         RequestContext context = new RequestContext();
152         context.setMsoRequestId("TEST-MSO-ID");
153         execution.setVariable("aicIdentity", "AIC-TEST");
154         execution.setVariable("vmIdList", "VM-ID-LIST-TEST");
155         execution.setVariable("vserverIdList", "VSERVER-ID-LIST");
156         execution.setVariable("identityUrl", "IDENTITY-URL-TEST");
157         execution.getGeneralBuildingBlock().setRequestContext(context);
158     }
159
160     private GenericVnf getTestGenericVnf() {
161         GenericVnf genericVnf = new GenericVnf();
162         genericVnf.setVnfId("TEST-VNF-ID");
163         genericVnf.setVnfType("TEST-VNF-TYPE");
164         genericVnf.setVnfName("TEST-VNF-NAME");
165         genericVnf.setIpv4OamAddress("129.0.0.1");
166         return genericVnf;
167     }
168 }