11/7: merge casablanca to master
[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
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Optional;
39 import java.util.UUID;
40
41 import org.camunda.bpm.engine.delegate.BpmnError;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.mockito.ArgumentMatchers;
45 import org.mockito.InjectMocks;
46 import org.onap.appc.client.lcm.model.Action;
47 import org.onap.so.bpmn.BaseTaskTest;
48 import org.onap.so.bpmn.common.BuildingBlockExecution;
49 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
50 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
51 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
52 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
53 import org.onap.so.client.exception.BBObjectNotFoundException;
54 import org.onap.so.db.catalog.beans.ControllerSelectionReference;
55
56 public class ConfigurationScaleOutTest extends BaseTaskTest {
57         
58         @InjectMocks
59         private ConfigurationScaleOut configurationScaleOut = new ConfigurationScaleOut();
60         
61         private GenericVnf genericVnf;
62         private VfModule vfModule;
63         private RequestContext requestContext;
64         private String msoRequestId;
65         private List<Map<String, String>> configurationParameters = new ArrayList<>();
66         private Map<String, String> configParamsMap = new HashMap<>();
67         
68
69
70         @Before
71         public void before() throws BBObjectNotFoundException {
72                 genericVnf = setGenericVnf();
73                 vfModule = setVfModule();
74                 msoRequestId = UUID.randomUUID().toString();
75                 requestContext = setRequestContext();
76                 requestContext.setMsoRequestId(msoRequestId);
77                 configParamsMap.put("availability-zone", "$.vnf-topology.vnf-resource-assignments.availability-zones.availability-zone[0]");
78                 configParamsMap.put("vnf-id", "$.vnf-topology.vnf-topology-identifier-structure.vnf-id");
79                 configurationParameters.add(configParamsMap);
80                 requestContext.setConfigurationParameters(configurationParameters);
81                 gBBInput.setRequestContext(requestContext);
82                 
83                 doThrow(new BpmnError("BPMN Error")).when(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(Exception.class));
84                 when(extractPojosForBB.extractByKey(any(),ArgumentMatchers.eq(ResourceKey.GENERIC_VNF_ID), any())).thenReturn(genericVnf);
85                 when(extractPojosForBB.extractByKey(any(),ArgumentMatchers.eq(ResourceKey.VF_MODULE_ID), any())).thenReturn(vfModule);
86
87         }
88         
89         @Test
90         public void setParamsForConfigurationScaleOutTest() throws Exception {
91                 ControllerSelectionReference controllerSelectionReference = new ControllerSelectionReference();
92                 controllerSelectionReference.setControllerName("testName");
93                 controllerSelectionReference.setActionCategory("testAction");
94                 controllerSelectionReference.setVnfType("testVnfType");
95                 String sdncResponse =  new String(Files.readAllBytes(Paths.get("src/test/resources/__files/SDNCClientGetResponse.json")));
96                 String expectedPayload = "{\"request-parameters\":{\"vnf-host-ip-address\":\"10.222.22.2\","
97                                 + "\"vf-module-id\":\"testVfModuleId1\"},\"configuration-parameters\""
98                                 + ":{\"vnf-id\":\"66dac89b-2a5b-4cb9-b22e-a7e4488fb3db\",\"availability-zone\":\"AZ-MN02\"}}";
99                 execution.setVariable("SDNCQueryResponse_" + vfModule.getVfModuleId(), sdncResponse);
100                 
101                 doReturn(controllerSelectionReference).when(catalogDbClient).getControllerSelectionReferenceByVnfTypeAndActionCategory(genericVnf.getVnfType(), Action.ConfigScaleOut.toString());
102                 
103                 configurationScaleOut.setParamsForConfigurationScaleOut(execution);
104                 
105                 assertEquals(genericVnf.getVnfId(), execution.getVariable("vnfId"));
106                 assertEquals(genericVnf.getVnfName(), execution.getVariable("vnfName"));
107                 assertEquals("ConfigScaleOut", execution.getVariable("action"));
108                 assertEquals(requestContext.getMsoRequestId(), execution.getVariable("msoRequestId"));
109                 assertEquals(controllerSelectionReference.getControllerName(), execution.getVariable("controllerType"));
110                 assertEquals(vfModule.getVfModuleId(), execution.getVariable("vfModuleId"));
111                 assertEquals(expectedPayload, execution.getVariable("payload"));
112         }
113         @Test
114         public void callAppcClientTest() throws Exception { 
115                 Action action = Action.ConfigScaleOut;
116                 String vnfId = genericVnf.getVnfId();
117                 String controllerType = "testType";
118                 String payload = "{\"request-parameters\":{\"vnf-host-ip-address\":\"10.222.22.2\","
119                                 + "\"vf-module-id\":\"testVfModuleId1\"},\"configuration-parameters\""
120                                 + ":{\"vnf-id\":\"66dac89b-2a5b-4cb9-b22e-a7e4488fb3db\",\"availability-zone\":\"AZ-MN02\"}}";
121                 HashMap<String, String> payloadInfo = new HashMap<String, String>();
122                 payloadInfo.put("vnfName", "testVnfName");
123                 payloadInfo.put("vfModuleId", "testVfModuleId");
124         
125                 execution.setVariable("action", Action.ConfigScaleOut.toString());
126                 execution.setVariable("msoRequestId", msoRequestId);
127                 execution.setVariable("controllerType", controllerType);
128                 execution.setVariable("vnfId", "testVnfId1");
129                 execution.setVariable("vnfName", "testVnfName");
130                 execution.setVariable("vfModuleId", "testVfModuleId");
131                 execution.setVariable("payload", payload);
132                 
133                 doNothing().when(appCClient).runAppCCommand(action, msoRequestId, vnfId, Optional.of(payload), payloadInfo, controllerType);
134                 
135                 configurationScaleOut.callAppcClient(execution);
136                 verify(appCClient, times(1)).runAppCCommand(action, msoRequestId, vnfId, Optional.of(payload), payloadInfo, controllerType);
137         }
138         @Test
139         public void setParamsForConfigurationScaleOutBadPathTest() throws Exception {
140                 ControllerSelectionReference controllerSelectionReference = new ControllerSelectionReference();
141                 controllerSelectionReference.setControllerName("testName");
142                 controllerSelectionReference.setActionCategory("testAction");
143                 controllerSelectionReference.setVnfType("testVnfType");
144                 String sdncResponse =  new String(Files.readAllBytes(Paths.get("src/test/resources/__files/SDNCClientResponseIncorrectPath.json")));
145                 String expectedPayload = "{\"request-parameters\":{\"vnf-host-ip-address\":\"10.222.22.2\","
146                                 + "\"vf-module-id\":\"testVfModuleId1\"},\"configuration-parameters\""
147                                 + ":{\"vnf-id\":\"66dac89b-2a5b-4cb9-b22e-a7e4488fb3db\",\"availability-zone\":null}}";
148                 execution.setVariable("SDNCQueryResponse_" + vfModule.getVfModuleId(), sdncResponse);
149                 
150                 doReturn(controllerSelectionReference).when(catalogDbClient).getControllerSelectionReferenceByVnfTypeAndActionCategory(genericVnf.getVnfType(), Action.ConfigScaleOut.toString());
151                 
152                 configurationScaleOut.setParamsForConfigurationScaleOut(execution);
153                 
154                 assertEquals(genericVnf.getVnfId(), execution.getVariable("vnfId"));
155                 assertEquals(genericVnf.getVnfName(), execution.getVariable("vnfName"));
156                 assertEquals("ConfigScaleOut", execution.getVariable("action"));
157                 assertEquals(requestContext.getMsoRequestId(), execution.getVariable("msoRequestId"));
158                 assertEquals(controllerSelectionReference.getControllerName(), execution.getVariable("controllerType"));
159                 assertEquals(vfModule.getVfModuleId(), execution.getVariable("vfModuleId"));
160                 assertEquals(expectedPayload, execution.getVariable("payload"));
161         }
162
163         @Test
164         public void callAppcClientExceptionTest() throws Exception {
165                 expectedException.expect(BpmnError.class);
166                 Action action = Action.ConfigScaleOut;
167                 String vnfId = genericVnf.getVnfId();
168                 String controllerType = "testType";
169                 String payload = "{\"request-parameters\":{\"vnf-host-ip-address\":\"10.222.22.2\","
170                                 + "\"vf-module-id\":\"testVfModuleId1\"},\"configuration-parameters\""
171                                 + ":{\"vnf-id\":\"66dac89b-2a5b-4cb9-b22e-a7e4488fb3db\",\"availability-zone\":\"AZ-MN02\"}}";
172                 HashMap<String, String> payloadInfo = new HashMap<String, String>();
173                 payloadInfo.put("vnfName", "testVnfName");
174                 payloadInfo.put("vfModuleId", "testVfModuleId");
175         
176                 execution.setVariable("action", Action.ConfigScaleOut.toString());
177                 execution.setVariable("msoRequestId", msoRequestId);
178                 execution.setVariable("controllerType", controllerType);
179                 execution.setVariable("vnfId", "testVnfId1");
180                 execution.setVariable("vnfName", "testVnfName");
181                 execution.setVariable("vfModuleId", "testVfModuleId");
182                 execution.setVariable("payload", payload);
183                 
184                 
185                 doThrow(new BpmnError("BPMN Error")).when(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(1002), eq("APPC Client Failed")); 
186                 doThrow(new RuntimeException("APPC Client Failed")).when(appCClient).runAppCCommand(action, msoRequestId, vnfId, Optional.of(payload), payloadInfo, controllerType);
187                 configurationScaleOut.callAppcClient(execution);
188                 verify(appCClient, times(1)).runAppCCommand(action, msoRequestId, vnfId, Optional.of(payload), payloadInfo, controllerType);
189         }
190 }