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