2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.so.bpmn.infrastructure.appc.tasks;
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;
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;
63 @RunWith(MockitoJUnitRunner.Silent.class)
64 public class AppcRunTasksTest extends TestDataSetup {
66 private final static String JSON_FILE_LOCATION = "src/test/resources/__files/BuildingBlocks/";
68 protected ExtractPojosForBB extractPojosForBB;
70 protected CatalogDbClient catalogDbClient;
72 protected ExceptionBuilder exceptionUtil;
74 protected ApplicationControllerAction appCClient;
76 protected AAIVnfResources aaiVnfResources;
78 private AppcRunTasks appcRunTasks = new AppcRunTasks();
81 public void mapRollbackVariablesTest() {
83 BuildingBlockExecution mock = mock(BuildingBlockExecution.class);
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);
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"));
109 appcRunTasks.runAppcCommand(execution, Action.Lock);
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"));
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());
125 appcRunTasks.runAppcCommand(execution, Action.Lock);
127 verify(exceptionUtil, times(1)).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000),
128 eq("No valid VNF exists"));
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");
142 appcRunTasks.runAppcCommand(execution, Action.Lock);
144 assertEquals(true, execution.getVariable("rollbackVnfLock"));
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");
160 appcRunTasks.runAppcCommand(execution, Action.Lock);
162 assertEquals(true, execution.getVariable("rollbackVnfLock"));
166 public void getVserversForAppcTest() throws Exception {
168 GenericVnf genericVnf = getTestGenericVnf();
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\\\"]\"}";
187 assertEquals(vserverIdList, expectedVserverIdList);
188 assertEquals(vmIdList, expectedVmIdList);
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");
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(" ", ""));
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);
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);
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");