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