1c64dd7952c919b1a5a0e3a2972b819346cb2def
[so.git] /
1 /*
2  * © 2014 AT&T Intellectual Property. All rights reserved. Used under license from AT&T Intellectual Property.
3  */
4 /*- 
5  * ============LICENSE_START======================================================= 
6  * ONAP - SO 
7  * ================================================================================ 
8  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. 
9  * ================================================================================ 
10  * Licensed under the Apache License, Version 2.0 (the "License"); 
11  * you may not use this file except in compliance with the License. 
12  * You may obtain a copy of the License at 
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0 
15  * 
16  * Unless required by applicable law or agreed to in writing, software 
17  * distributed under the License is distributed on an "AS IS" BASIS, 
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
19  * See the License for the specific language governing permissions and 
20  * limitations under the License. 
21  * ============LICENSE_END========================================================= 
22  */
23
24 package org.openecomp.mso.bpmn.infrastructure;
25
26 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
27 import static com.github.tomakehurst.wiremock.client.WireMock.get;
28 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
29 import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
30 import static org.junit.Assert.assertEquals;
31 import static org.openecomp.mso.bpmn.common.BPMNUtil.executeWorkFlow;
32 import static org.openecomp.mso.bpmn.common.BPMNUtil.waitForWorkflowToFinish;
33 import static org.openecomp.mso.bpmn.mock.StubResponseAAI.MockGetGenericVnfByName_404;
34 import static org.openecomp.mso.bpmn.mock.StubResponseAAI.MockGetServiceInstance;
35 import static org.openecomp.mso.bpmn.mock.StubResponseAAI.MockNodeQueryServiceInstanceById;
36 import static org.openecomp.mso.bpmn.mock.StubResponseAAI.MockNodeQueryServiceInstanceById_404;
37 import static org.openecomp.mso.bpmn.mock.StubResponseAAI.MockPutGenericVnf;
38 import static org.openecomp.mso.bpmn.mock.StubResponseDatabase.mockUpdateRequestDB;
39 import static org.openecomp.mso.bpmn.mock.StubResponseSDNCAdapter.mockSDNCAdapter;
40
41 import java.io.IOException;
42 import java.util.HashMap;
43 import java.util.Map;
44 import java.util.UUID;
45
46 import org.camunda.bpm.engine.test.Deployment;
47 import org.junit.Ignore;
48 import org.junit.Test;
49 import org.openecomp.mso.bpmn.common.BPMNUtil;
50 import org.openecomp.mso.bpmn.common.WorkflowTest;
51 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowResponse;
52 import org.openecomp.mso.bpmn.mock.FileUtil;
53
54 /**
55  * Unit Test for the CreateVnfInfra Flow
56  */
57 public class CreateVnfInfraTest extends WorkflowTest {
58
59     private String createVnfInfraRequest;
60     private final CallbackSet callbacks = new CallbackSet();
61
62
63     public CreateVnfInfraTest() throws IOException {
64         createVnfInfraRequest = FileUtil.readResourceFile("__files/InfrastructureFlows/CreateVnfInfraRequest.json");
65         callbacks.put("assign", FileUtil.readResourceFile(
66                 "__files/VfModularity/SDNCTopologyAssignCallback.xml"));
67         callbacks.put("activate", FileUtil.readResourceFile(
68                 "__files/VfModularity/SDNCTopologyActivateCallback.xml"));
69     }
70
71     @Test
72     @Deployment(resources = {"subprocess/DoCreateVnf.bpmn",
73             "subprocess/GenericGetService.bpmn",
74             "subprocess/GenericGetVnf.bpmn",
75             "subprocess/GenericPutVnf.bpmn",
76             "subprocess/SDNCAdapterV1.bpmn",
77             "process/CreateVnfInfra.bpmn",
78             "subprocess/FalloutHandler.bpmn",
79             "subprocess/CompleteMsoProcess.bpmn"})
80     public void testCreateVnfInfra_success() throws Exception {
81
82         MockNodeQueryServiceInstanceById("MIS%2F1604%2F0026%2FSW_INTERNET", "GenericFlows/getSIUrlById.xml");
83         MockGetServiceInstance("SDN-ETHERNET-INTERNET", "123456789", "MIS%252F1604%252F0026%252FSW_INTERNET", "GenericFlows/getServiceInstance.xml");
84         MockGetGenericVnfByName_404();
85         MockPutGenericVnf();
86         mockSDNCAdapter("/SDNCAdapter", "vnf-type>STMTN", 200, "VfModularity/StandardSDNCSynchResponse.xml");
87         mockUpdateRequestDB(200, "Database/DBUpdateResponse.xml");
88
89         String businessKey = UUID.randomUUID().toString();
90         Map<String, Object> variables = new HashMap<>();
91         setVariablesSuccess(variables, createVnfInfraRequest, "testRequestId123", "MIS%2F1604%2F0026%2FSW_INTERNET");
92         TestAsyncResponse asyncResponse = invokeAsyncProcess("CreateVnfInfra",
93                 "v1", businessKey, createVnfInfraRequest, variables);
94
95         WorkflowResponse response = receiveResponse(businessKey, asyncResponse, 10000);
96
97         String responseBody = response.getResponse();
98         System.out.println("Workflow (Synch) Response:\n" + responseBody);
99
100         //injectSDNCCallbacks(callbacks, "assign, query");
101         //injectSDNCCallbacks(callbacks, "activate");
102
103         // TODO add appropriate assertions
104
105         waitForProcessEnd(businessKey, 10000);
106         String status = BPMNUtil.getVariable(processEngineRule, "CreateVnfInfra", "CreateVnfInfraStatus");
107         assertEquals("Success", status);
108
109         logEnd();
110
111         //WorkflowResponse workflowResponse = executeWorkFlow(processEngineRule, "CreateVnfInfra", variables);
112         //      injectSDNCCallbacks(callbacks, "assign");
113         //      injectSDNCCallbacks(callbacks, "activate");
114         //waitForProcessEnd(businessKey, 10000);
115         //waitForWorkflowToFinish(processEngineRule, workflowResponse.getProcessInstanceID());
116
117         //assertVariables("true", "true", "false", "true", "Success", null);
118
119     }
120
121     @Test
122     @Deployment(resources = {"subprocess/GenericGetService.bpmn", "subprocess/GenericGetVnf.bpmn", "subprocess/GenericPutVnf.bpmn", "process/CreateVnfInfra.bpmn", "subprocess/FalloutHandler.bpmn", "subprocess/CompleteMsoProcess.bpmn"})
123     public void testCreateVnfInfra_error_badRequest() throws Exception {
124
125         mockUpdateRequestDB(200, "Database/DBUpdateResponse.xml");
126
127         Map<String, String> variables = new HashMap<>();
128         setVariables(variables, null, "testRequestId123", "MIS%2F1604%2F0026%2FSW_INTERNET");
129
130         WorkflowResponse workflowResponse = executeWorkFlow(processEngineRule, "CreateVnfInfra", variables);
131         waitForWorkflowToFinish(processEngineRule, workflowResponse.getProcessInstanceID());
132
133         assertVariables(null, null, null, null, null, "WorkflowException[processKey=CreateVnfInfra,errorCode=2500,errorMessage=Internal Error - WorkflowException Object and/or RequestInfo is null! CreateVnfInfra]");
134
135     }
136
137     @Test
138     @Ignore
139     @Deployment(resources = {"subprocess/DoCreateVnf.bpmn", "subprocess/GenericGetService.bpmn", "subprocess/GenericGetVnf.bpmn", "subprocess/GenericPutVnf.bpmn", "process/CreateVnfInfra.bpmn", "subprocess/FalloutHandler.bpmn", "subprocess/CompleteMsoProcess.bpmn"})
140     public void testCreateVnfInfra_error_siNotFound() throws Exception {
141
142         MockNodeQueryServiceInstanceById_404("MIS%2F1604%2F0026%2FSW_INTERNET");
143         mockUpdateRequestDB(200, "Database/DBUpdateResponse.xml");
144
145         Map<String, String> variables = new HashMap<>();
146         setVariables(variables, createVnfInfraRequest, "testRequestId123", "MIS%2F1604%2F0026%2FSW_INTERNET");
147
148         WorkflowResponse workflowResponse = executeWorkFlow(processEngineRule, "CreateVnfInfra", variables);
149         waitForWorkflowToFinish(processEngineRule, workflowResponse.getProcessInstanceID());
150
151         assertVariables(null, null, null, null, null, "WorkflowException[processKey=DoCreateVnf,errorCode=404,errorMessage=Service Instance Not Found]");
152
153     }
154
155     @Test
156     @Ignore
157     @Deployment(resources = {"subprocess/DoCreateVnf.bpmn",
158             "subprocess/GenericGetService.bpmn",
159             "subprocess/GenericGetVnf.bpmn",
160             "subprocess/GenericPutVnf.bpmn",
161             "process/CreateVnfInfra.bpmn",
162             "subprocess/FalloutHandler.bpmn",
163             "subprocess/CompleteMsoProcess.bpmn"})
164     public void testCreateVnfInfra_error_vnfExist() throws Exception {
165         MockNodeQueryServiceInstanceById("MIS%2F1604%2F0026%2FSW_INTERNET", "GenericFlows/getSIUrlById.xml");
166         MockGetServiceInstance("SDN-ETHERNET-INTERNET", "123456789", "MIS%252F1604%252F0026%252FSW_INTERNET", "GenericFlows/getServiceInstance.xml");
167
168         stubFor(get(urlMatching("/aai/v[0-9]+/network/generic-vnfs/generic-vnf[?]vnf-name=testVnfName123&depth=1"))
169                 .willReturn(aResponse()
170                         .withStatus(200)
171                         .withHeader("Content-Type", "text/xml")
172                         .withBodyFile("GenericFlows/getGenericVnfResponse.xml")));
173
174         MockPutGenericVnf();
175         mockUpdateRequestDB(200, "Database/DBUpdateResponse.xml");
176
177         Map<String, String> variables = new HashMap<>();
178         setVariables(variables, createVnfInfraRequest, "testRequestId123", "MIS%2F1604%2F0026%2FSW_INTERNET");
179
180         WorkflowResponse workflowResponse = executeWorkFlow(processEngineRule, "CreateVnfInfra", variables);
181         waitForWorkflowToFinish(processEngineRule, workflowResponse.getProcessInstanceID());
182
183         assertVariables(null, null, null, null, null, "WorkflowException[processKey=DoCreateVnf,errorCode=5000,errorMessage=Generic Vnf Already Exist.]");
184
185     }
186
187     private void assertVariables(String exSIFound, String exSISucc, String exVnfFound, String exVnfSucc, String exResponse, String exWorkflowException) {
188
189         String siFound = BPMNUtil.getVariable(processEngineRule, "CreateVnfInfra", "GENGS_FoundIndicator");
190         String siSucc = BPMNUtil.getVariable(processEngineRule, "CreateVnfInfra", "GENGS_SuccessIndicator");
191         String vnfFound = BPMNUtil.getVariable(processEngineRule, "CreateVnfInfra", "GENGV_FoundIndicator");
192         String vnfSucc = BPMNUtil.getVariable(processEngineRule, "CreateVnfInfra", "GENGV_SuccessIndicator");
193         String response = BPMNUtil.getVariable(processEngineRule, "CreateVnfInfra", "WorkflowResponse");
194         String workflowException = BPMNUtil.getVariable(processEngineRule, "CreateVnfInfra", "SavedWorkflowException1");
195
196         assertEquals(exSIFound, siFound);
197         assertEquals(exSISucc, siSucc);
198         assertEquals(exVnfFound, vnfFound);
199         assertEquals(exVnfSucc, vnfSucc);
200         assertEquals(exResponse, response);
201         assertEquals(exWorkflowException, workflowException);
202     }
203
204     private void setVariables(Map<String, String> variables, String request, String requestId, String siId) {
205         variables.put("isDebugLogEnabled", "true");
206         variables.put("bpmnRequest", request);
207         variables.put("mso-request-id", requestId);
208         variables.put("serviceInstanceId", siId);
209         variables.put("testVnfId", "testVnfId123");
210         variables.put("vnfType", "STMTN");
211     }
212
213     private void setVariablesSuccess(Map<String, Object> variables, String request, String requestId, String siId) {
214         variables.put("isDebugLogEnabled", "true");
215         //variables.put("bpmnRequest", request);
216         //variables.put("mso-request-id", requestId);
217         variables.put("serviceInstanceId", siId);
218         variables.put("requestId", requestId);
219         variables.put("testVnfId", "testVnfId123");
220         variables.put("vnfType", "STMTN");
221     }
222
223 }