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