Merge "Fix rollback for ConfigAssignBB"
[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.doReturn;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.times;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
34 import java.nio.file.Files;
35 import java.nio.file.Paths;
36 import java.util.Optional;
37 import org.junit.Test;
38 import org.mockito.InjectMocks;
39 import org.onap.aai.domain.yang.Vserver;
40 import org.onap.appc.client.lcm.model.Action;
41 import org.onap.so.bpmn.BaseTaskTest;
42 import org.onap.so.bpmn.common.BuildingBlockExecution;
43 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
44 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
45 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
46 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
47 import org.onap.so.client.aai.entities.AAIResultWrapper;
48 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
49 import org.onap.so.client.exception.BBObjectNotFoundException;
50 import org.onap.so.db.catalog.beans.ControllerSelectionReference;
51 import com.fasterxml.jackson.databind.ObjectMapper;
52
53 public class AppcRunTasksTest extends BaseTaskTest {
54
55     private final static String JSON_FILE_LOCATION = "src/test/resources/__files/BuildingBlocks/";
56
57     @InjectMocks
58     private AppcRunTasks appcRunTasks = new AppcRunTasks();
59
60     @Test
61     public void mapRollbackVariablesTest() {
62
63         BuildingBlockExecution mock = mock(BuildingBlockExecution.class);
64
65         appcRunTasks.mapRollbackVariables(mock, Action.Lock, "1");
66         verify(mock, times(0)).setVariable(any(String.class), any());
67         appcRunTasks.mapRollbackVariables(mock, Action.Lock, "0");
68         verify(mock, times(1)).setVariable("rollbackVnfLock", true);
69         appcRunTasks.mapRollbackVariables(mock, Action.Unlock, "0");
70         verify(mock, times(1)).setVariable("rollbackVnfLock", false);
71         appcRunTasks.mapRollbackVariables(mock, Action.Start, "0");
72         verify(mock, times(1)).setVariable("rollbackVnfStop", false);
73         appcRunTasks.mapRollbackVariables(mock, Action.Stop, "0");
74         verify(mock, times(1)).setVariable("rollbackVnfStop", true);
75         appcRunTasks.mapRollbackVariables(mock, Action.QuiesceTraffic, "0");
76         verify(mock, times(1)).setVariable("rollbackQuiesceTraffic", true);
77         appcRunTasks.mapRollbackVariables(mock, Action.ResumeTraffic, "0");
78         verify(mock, times(1)).setVariable("rollbackQuiesceTraffic", false);
79     }
80
81     @Test
82     public void runAppcCommandVnfNull() throws BBObjectNotFoundException {
83         execution.getLookupMap().put(ResourceKey.GENERIC_VNF_ID, "NULL-TEST");
84         fillRequiredAppcExecutionFields();
85         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(null);
86         when(catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory(isNull(),
87                 eq(Action.Lock.toString()))).thenThrow(new IllegalArgumentException("name or values is null"));
88
89         appcRunTasks.runAppcCommand(execution, Action.Lock);
90
91         // if vnf = null -> vnfType = null ->
92         // IllegalArgumentException will be thrown in
93         // catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory
94         verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1002),
95                 eq("name or values is null"));
96     }
97
98     @Test
99     public void runAppcCommandBBObjectNotFoundException() throws BBObjectNotFoundException {
100         execution.getLookupMap().put(ResourceKey.GENERIC_VNF_ID, "EXCEPTION-TEST");
101         fillRequiredAppcExecutionFields();
102         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.GENERIC_VNF_ID)))
103                 .thenThrow(new BBObjectNotFoundException());
104
105         appcRunTasks.runAppcCommand(execution, Action.Lock);
106
107         verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000),
108                 eq("No valid VNF exists"));
109     }
110
111     @Test
112     public void runAppcCommandVfModuleNull() throws BBObjectNotFoundException {
113         execution.getLookupMap().put(ResourceKey.GENERIC_VNF_ID, "SUCCESS-TEST");
114         fillRequiredAppcExecutionFields();
115         GenericVnf genericVnf = getTestGenericVnf();
116         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(genericVnf);
117         mockReferenceResponse();
118         execution.getLookupMap().put(ResourceKey.VF_MODULE_ID, "VF-MODULE-ID-TEST");
119         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.VF_MODULE_ID))).thenReturn(null);
120         when(appCClient.getErrorCode()).thenReturn("0");
121
122         appcRunTasks.runAppcCommand(execution, Action.Lock);
123
124         assertEquals(true, execution.getVariable("rollbackVnfLock"));
125     }
126
127     @Test
128     public void runAppcCommand() throws BBObjectNotFoundException {
129         execution.getLookupMap().put(ResourceKey.GENERIC_VNF_ID, "SUCCESS-TEST");
130         fillRequiredAppcExecutionFields();
131         GenericVnf genericVnf = getTestGenericVnf();
132         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.GENERIC_VNF_ID))).thenReturn(genericVnf);
133         mockReferenceResponse();
134         execution.getLookupMap().put(ResourceKey.VF_MODULE_ID, "VF-MODULE-ID-TEST");
135         VfModule vfModule = new VfModule();
136         vfModule.setVfModuleId("VF-MODULE-ID");
137         when(extractPojosForBB.extractByKey(eq(execution), eq(ResourceKey.VF_MODULE_ID))).thenReturn(vfModule);
138         when(appCClient.getErrorCode()).thenReturn("0");
139
140         appcRunTasks.runAppcCommand(execution, Action.Lock);
141
142         assertEquals(true, execution.getVariable("rollbackVnfLock"));
143     }
144
145     @Test
146     public void getVserversForAppcTest() throws Exception {
147
148         GenericVnf genericVnf = getTestGenericVnf();
149
150         final String aaiVnfJson =
151                 new String(Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + "aaiGenericVnfWithVservers.json")));
152         final String aaiVserverJson =
153                 new String(Files.readAllBytes(Paths.get(JSON_FILE_LOCATION + "aaiVserverQueryResponse.json")));
154         AAIResultWrapper aaiResultWrapper = new AAIResultWrapper(aaiVnfJson);
155         ObjectMapper mapper = new ObjectMapper();
156         Vserver vserver = mapper.readValue(aaiVserverJson, Vserver.class);
157         doReturn(aaiResultWrapper).when(aaiVnfResources).queryVnfWrapperById(genericVnf);
158         doReturn(Optional.of(vserver)).when(aaiVnfResources).getVserver(any(AAIResourceUri.class));
159         appcRunTasks.getVserversForAppc(execution, genericVnf);
160         String vserverIdList = execution.getVariable("vserverIdList");
161         String expectedVserverIdList =
162                 "{\"vserverIds\":\"[\\\"1b3f44e5-d96d-4aac-bd9a-310e8cfb0af5\\\",\\\"14551849-1e70-45cd-bc5d-a256d49548a2\\\",\\\"48bd7f11-408f-417c-b834-b41c1b98f7d7\\\"]\"}";
163         String vmIdList = execution.getVariable("vmIdList");
164         String expectedVmIdList =
165                 "{\"vmIds\":\"[\\\"http://VSERVER-link.com\\\",\\\"http://VSERVER-link.com\\\",\\\"http://VSERVER-link.com\\\"]\"}";
166
167         assertEquals(vserverIdList, expectedVserverIdList);
168         assertEquals(vmIdList, expectedVmIdList);
169     }
170
171     private void mockReferenceResponse() {
172         ControllerSelectionReference reference = new ControllerSelectionReference();
173         reference.setControllerName("TEST-CONTROLLER-NAME");
174         when(catalogDbClient.getControllerSelectionReferenceByVnfTypeAndActionCategory(eq("TEST-VNF-TYPE"),
175                 eq(Action.Lock.toString()))).thenReturn(reference);
176     }
177
178     private void fillRequiredAppcExecutionFields() {
179         RequestContext context = new RequestContext();
180         context.setMsoRequestId("TEST-MSO-ID");
181         execution.setVariable("aicIdentity", "AIC-TEST");
182         execution.setVariable("vmIdList", "VM-ID-LIST-TEST");
183         execution.setVariable("vserverIdList", "VSERVER-ID-LIST");
184         execution.setVariable("identityUrl", "IDENTITY-URL-TEST");
185         execution.getGeneralBuildingBlock().setRequestContext(context);
186     }
187
188     private GenericVnf getTestGenericVnf() {
189         GenericVnf genericVnf = new GenericVnf();
190         genericVnf.setVnfId("TEST-VNF-ID");
191         genericVnf.setVnfType("TEST-VNF-TYPE");
192         genericVnf.setVnfName("TEST-VNF-NAME");
193         genericVnf.setIpv4OamAddress("129.0.0.1");
194         return genericVnf;
195     }
196 }