Replaced all tabs with spaces in java and pom.xml
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / flowspecific / tasks / ConfigurationScaleOutTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.bpmn.infrastructure.flowspecific.tasks;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.eq;
25 import static org.mockito.Mockito.doNothing;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.doThrow;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31 import java.nio.file.Files;
32 import java.nio.file.Paths;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Optional;
38 import java.util.UUID;
39 import org.camunda.bpm.engine.delegate.BpmnError;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.mockito.ArgumentMatchers;
43 import org.mockito.InjectMocks;
44 import org.onap.appc.client.lcm.model.Action;
45 import org.onap.so.bpmn.BaseTaskTest;
46 import org.onap.so.bpmn.common.BuildingBlockExecution;
47 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
48 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
49 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
50 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
51 import org.onap.so.client.exception.BBObjectNotFoundException;
52 import org.onap.so.db.catalog.beans.ControllerSelectionReference;
53
54 public class ConfigurationScaleOutTest extends BaseTaskTest {
55
56     @InjectMocks
57     private ConfigurationScaleOut configurationScaleOut = new ConfigurationScaleOut();
58
59     private GenericVnf genericVnf;
60     private VfModule vfModule;
61     private RequestContext requestContext;
62     private String msoRequestId;
63     private List<Map<String, String>> configurationParameters = new ArrayList<>();
64     private Map<String, String> configParamsMap = new HashMap<>();
65
66
67
68     @Before
69     public void before() throws BBObjectNotFoundException {
70         genericVnf = setGenericVnf();
71         vfModule = setVfModule();
72         msoRequestId = UUID.randomUUID().toString();
73         requestContext = setRequestContext();
74         requestContext.setMsoRequestId(msoRequestId);
75         configParamsMap.put("availability-zone",
76                 "$.vnf-topology.vnf-resource-assignments.availability-zones.availability-zone[0]");
77         configParamsMap.put("vnf-id", "$.vnf-topology.vnf-topology-identifier-structure.vnf-id");
78         configurationParameters.add(configParamsMap);
79         requestContext.setConfigurationParameters(configurationParameters);
80         gBBInput.setRequestContext(requestContext);
81
82         doThrow(new BpmnError("BPMN Error")).when(exceptionUtil)
83                 .buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(Exception.class));
84         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.GENERIC_VNF_ID)))
85                 .thenReturn(genericVnf);
86         when(extractPojosForBB.extractByKey(any(), ArgumentMatchers.eq(ResourceKey.VF_MODULE_ID))).thenReturn(vfModule);
87
88     }
89
90     @Test
91     public void setParamsForConfigurationScaleOutTest() throws Exception {
92         ControllerSelectionReference controllerSelectionReference = new ControllerSelectionReference();
93         controllerSelectionReference.setControllerName("testName");
94         controllerSelectionReference.setActionCategory("testAction");
95         controllerSelectionReference.setVnfType("testVnfType");
96         String sdncResponse =
97                 new String(Files.readAllBytes(Paths.get("src/test/resources/__files/SDNCClientGetResponse.json")));
98         String expectedPayload = "{\"request-parameters\":{\"vnf-host-ip-address\":\"10.222.22.2\","
99                 + "\"vf-module-id\":\"testVfModuleId1\"},\"configuration-parameters\""
100                 + ":{\"vnf-id\":\"66dac89b-2a5b-4cb9-b22e-a7e4488fb3db\",\"availability-zone\":\"AZ-MN02\"}}";
101         execution.setVariable("SDNCQueryResponse_" + vfModule.getVfModuleId(), sdncResponse);
102
103         doReturn(controllerSelectionReference).when(catalogDbClient)
104                 .getControllerSelectionReferenceByVnfTypeAndActionCategory(genericVnf.getVnfType(),
105                         Action.ConfigScaleOut.toString());
106
107         configurationScaleOut.setParamsForConfigurationScaleOut(execution);
108
109         assertEquals(genericVnf.getVnfId(), execution.getVariable("vnfId"));
110         assertEquals(genericVnf.getVnfName(), execution.getVariable("vnfName"));
111         assertEquals("ConfigScaleOut", execution.getVariable("action"));
112         assertEquals(requestContext.getMsoRequestId(), execution.getVariable("msoRequestId"));
113         assertEquals(controllerSelectionReference.getControllerName(), execution.getVariable("controllerType"));
114         assertEquals(vfModule.getVfModuleId(), execution.getVariable("vfModuleId"));
115         assertEquals(expectedPayload, execution.getVariable("payload"));
116     }
117
118     @Test
119     public void callAppcClientTest() throws Exception {
120         Action action = Action.ConfigScaleOut;
121         String vnfId = genericVnf.getVnfId();
122         String controllerType = "testType";
123         String payload = "{\"request-parameters\":{\"vnf-host-ip-address\":\"10.222.22.2\","
124                 + "\"vf-module-id\":\"testVfModuleId1\"},\"configuration-parameters\""
125                 + ":{\"vnf-id\":\"66dac89b-2a5b-4cb9-b22e-a7e4488fb3db\",\"availability-zone\":\"AZ-MN02\"}}";
126         HashMap<String, String> payloadInfo = new HashMap<String, String>();
127         payloadInfo.put("vnfName", "testVnfName");
128         payloadInfo.put("vfModuleId", "testVfModuleId");
129
130         execution.setVariable("action", Action.ConfigScaleOut.toString());
131         execution.setVariable("msoRequestId", msoRequestId);
132         execution.setVariable("controllerType", controllerType);
133         execution.setVariable("vnfId", "testVnfId1");
134         execution.setVariable("vnfName", "testVnfName");
135         execution.setVariable("vfModuleId", "testVfModuleId");
136         execution.setVariable("payload", payload);
137
138         doNothing().when(appCClient).runAppCCommand(action, msoRequestId, vnfId, Optional.of(payload), payloadInfo,
139                 controllerType);
140
141         configurationScaleOut.callAppcClient(execution);
142         verify(appCClient, times(1)).runAppCCommand(action, msoRequestId, vnfId, Optional.of(payload), payloadInfo,
143                 controllerType);
144     }
145
146     @Test
147     public void setParamsForConfigurationScaleOutBadPathTest() throws Exception {
148         ControllerSelectionReference controllerSelectionReference = new ControllerSelectionReference();
149         controllerSelectionReference.setControllerName("testName");
150         controllerSelectionReference.setActionCategory("testAction");
151         controllerSelectionReference.setVnfType("testVnfType");
152         String sdncResponse = new String(
153                 Files.readAllBytes(Paths.get("src/test/resources/__files/SDNCClientResponseIncorrectPath.json")));
154         String expectedPayload = "{\"request-parameters\":{\"vnf-host-ip-address\":\"10.222.22.2\","
155                 + "\"vf-module-id\":\"testVfModuleId1\"},\"configuration-parameters\""
156                 + ":{\"vnf-id\":\"66dac89b-2a5b-4cb9-b22e-a7e4488fb3db\",\"availability-zone\":null}}";
157         execution.setVariable("SDNCQueryResponse_" + vfModule.getVfModuleId(), sdncResponse);
158
159         doReturn(controllerSelectionReference).when(catalogDbClient)
160                 .getControllerSelectionReferenceByVnfTypeAndActionCategory(genericVnf.getVnfType(),
161                         Action.ConfigScaleOut.toString());
162
163         configurationScaleOut.setParamsForConfigurationScaleOut(execution);
164
165         assertEquals(genericVnf.getVnfId(), execution.getVariable("vnfId"));
166         assertEquals(genericVnf.getVnfName(), execution.getVariable("vnfName"));
167         assertEquals("ConfigScaleOut", execution.getVariable("action"));
168         assertEquals(requestContext.getMsoRequestId(), execution.getVariable("msoRequestId"));
169         assertEquals(controllerSelectionReference.getControllerName(), execution.getVariable("controllerType"));
170         assertEquals(vfModule.getVfModuleId(), execution.getVariable("vfModuleId"));
171         assertEquals(expectedPayload, execution.getVariable("payload"));
172     }
173
174     @Test
175     public void callAppcClientExceptionTest() throws Exception {
176         expectedException.expect(BpmnError.class);
177         Action action = Action.ConfigScaleOut;
178         String vnfId = genericVnf.getVnfId();
179         String controllerType = "testType";
180         String payload = "{\"request-parameters\":{\"vnf-host-ip-address\":\"10.222.22.2\","
181                 + "\"vf-module-id\":\"testVfModuleId1\"},\"configuration-parameters\""
182                 + ":{\"vnf-id\":\"66dac89b-2a5b-4cb9-b22e-a7e4488fb3db\",\"availability-zone\":\"AZ-MN02\"}}";
183         HashMap<String, String> payloadInfo = new HashMap<String, String>();
184         payloadInfo.put("vnfName", "testVnfName");
185         payloadInfo.put("vfModuleId", "testVfModuleId");
186
187         execution.setVariable("action", Action.ConfigScaleOut.toString());
188         execution.setVariable("msoRequestId", msoRequestId);
189         execution.setVariable("controllerType", controllerType);
190         execution.setVariable("vnfId", "testVnfId1");
191         execution.setVariable("vnfName", "testVnfName");
192         execution.setVariable("vfModuleId", "testVfModuleId");
193         execution.setVariable("payload", payload);
194
195
196         doThrow(new BpmnError("BPMN Error")).when(exceptionUtil)
197                 .buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1002), eq("APPC Client Failed"));
198         doThrow(new RuntimeException("APPC Client Failed")).when(appCClient).runAppCCommand(action, msoRequestId, vnfId,
199                 Optional.of(payload), payloadInfo, controllerType);
200         configurationScaleOut.callAppcClient(execution);
201         verify(appCClient, times(1)).runAppCCommand(action, msoRequestId, vnfId, Optional.of(payload), payloadInfo,
202                 controllerType);
203     }
204 }