80c1e8c215c887b437d4fd9af12d5bcbf6b8eadb
[so.git] / bpmn / MSOInfrastructureBPMN / src / test / java / org / openecomp / mso / bpmn / infrastructure / CreateVnfInfraTest.java
1 /*
2  * © 2014 AT&T Intellectual Property. All rights reserved. Used under license from AT&T Intellectual Property.
3  */
4 /*- 
5  * ============LICENSE_START======================================================= 
6  * OPENECOMP - MSO 
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.Test;
48 import org.openecomp.mso.bpmn.common.BPMNUtil;
49 import org.openecomp.mso.bpmn.common.WorkflowTest;
50 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowResponse;
51 import org.openecomp.mso.bpmn.mock.FileUtil;
52
53 /**
54  * Unit Test for the CreateVnfInfra Flow
55  */
56 public class CreateVnfInfraTest extends WorkflowTest {
57
58         private String createVnfInfraRequest;
59         private final CallbackSet callbacks = new CallbackSet();
60
61
62         public CreateVnfInfraTest() throws IOException {
63                 createVnfInfraRequest = FileUtil.readResourceFile("__files/InfrastructureFlows/CreateVnfInfraRequest.json");
64                 callbacks.put("assign", FileUtil.readResourceFile(
65                                 "__files/VfModularity/SDNCTopologyAssignCallback.xml"));
66                 callbacks.put("activate", FileUtil.readResourceFile(
67                                 "__files/VfModularity/SDNCTopologyActivateCallback.xml"));
68         }
69
70         @Test
71         @Deployment(resources = {"subprocess/DoCreateVnf.bpmn", 
72                                                         "subprocess/GenericGetService.bpmn", 
73                                                         "subprocess/GenericGetVnf.bpmn", 
74                                                         "subprocess/GenericPutVnf.bpmn", 
75                                                         "subprocess/SDNCAdapterV1.bpmn", 
76                                                         "process/CreateVnfInfra.bpmn", 
77                                                         "subprocess/FalloutHandler.bpmn", 
78                                                         "subprocess/CompleteMsoProcess.bpmn"})
79         public void testCreateVnfInfra_success() throws Exception{
80
81                 MockNodeQueryServiceInstanceById("MIS%2F1604%2F0026%2FSW_INTERNET", "GenericFlows/getSIUrlById.xml");
82                 MockGetServiceInstance("SDN-ETHERNET-INTERNET", "123456789", "MIS%252F1604%252F0026%252FSW_INTERNET", "GenericFlows/getServiceInstance.xml");
83                 MockGetGenericVnfByName_404();
84                 MockPutGenericVnf();
85                 mockSDNCAdapter("/SDNCAdapter", "vnf-type>STMTN", 200, "VfModularity/StandardSDNCSynchResponse.xml");
86                 mockUpdateRequestDB(200, "Database/DBUpdateResponse.xml");
87
88                 String businessKey = UUID.randomUUID().toString();
89                 Map<String, Object> variables = new HashMap<String, Object>();
90                 setVariablesSuccess(variables, createVnfInfraRequest, "testRequestId123", "MIS%2F1604%2F0026%2FSW_INTERNET");
91                 TestAsyncResponse asyncResponse = invokeAsyncProcess("CreateVnfInfra",
92                                 "v1", businessKey, createVnfInfraRequest, variables);
93
94                         WorkflowResponse response = receiveResponse(businessKey, asyncResponse, 10000);
95
96                         String responseBody = response.getResponse();
97                         System.out.println("Workflow (Synch) Response:\n" + responseBody);
98
99                         //injectSDNCCallbacks(callbacks, "assign, query");
100                         //injectSDNCCallbacks(callbacks, "activate");
101
102                         // TODO add appropriate assertions
103
104                         waitForProcessEnd(businessKey, 10000);
105                         String status = BPMNUtil.getVariable(processEngineRule, "CreateVnfInfra", "CreateVnfInfraStatus");
106                         assertEquals("Success", status);
107
108                         logEnd();
109
110                         //WorkflowResponse workflowResponse = executeWorkFlow(processEngineRule, "CreateVnfInfra", variables);
111         //      injectSDNCCallbacks(callbacks, "assign");
112         //      injectSDNCCallbacks(callbacks, "activate");
113                 //waitForProcessEnd(businessKey, 10000);
114                 //waitForWorkflowToFinish(processEngineRule, workflowResponse.getProcessInstanceID());
115
116                 //assertVariables("true", "true", "false", "true", "Success", null);
117
118         }
119
120         @Test
121         @Deployment(resources = {"subprocess/GenericGetService.bpmn", "subprocess/GenericGetVnf.bpmn", "subprocess/GenericPutVnf.bpmn", "process/CreateVnfInfra.bpmn", "subprocess/FalloutHandler.bpmn", "subprocess/CompleteMsoProcess.bpmn"})
122         public void testCreateVnfInfra_error_badRequest() throws Exception{
123
124                 mockUpdateRequestDB(200, "Database/DBUpdateResponse.xml");
125
126                 Map<String, String> variables = new HashMap<String, String>();
127                 setVariables(variables, null, "testRequestId123", "MIS%2F1604%2F0026%2FSW_INTERNET");
128
129                 WorkflowResponse workflowResponse = executeWorkFlow(processEngineRule, "CreateVnfInfra", variables);
130                 waitForWorkflowToFinish(processEngineRule, workflowResponse.getProcessInstanceID());
131
132                 assertVariables(null, null, null, null, null, "WorkflowException[processKey=CreateVnfInfra,errorCode=2500,errorMessage=Internal Error - WorkflowException Object and/or RequestInfo is null! CreateVnfInfra]");
133
134         }
135
136         @Test
137         @Deployment(resources = {"subprocess/DoCreateVnf.bpmn", "subprocess/GenericGetService.bpmn", "subprocess/GenericGetVnf.bpmn", "subprocess/GenericPutVnf.bpmn", "process/CreateVnfInfra.bpmn", "subprocess/FalloutHandler.bpmn", "subprocess/CompleteMsoProcess.bpmn"})
138         public void testCreateVnfInfra_error_siNotFound() throws Exception{
139
140                 MockNodeQueryServiceInstanceById_404("MIS%2F1604%2F0026%2FSW_INTERNET");
141                 mockUpdateRequestDB(200, "Database/DBUpdateResponse.xml");
142
143                 Map<String, String> variables = new HashMap<String, String>();
144                 setVariables(variables, createVnfInfraRequest, "testRequestId123", "MIS%2F1604%2F0026%2FSW_INTERNET");
145
146                 WorkflowResponse workflowResponse = executeWorkFlow(processEngineRule, "CreateVnfInfra", variables);
147                 waitForWorkflowToFinish(processEngineRule, workflowResponse.getProcessInstanceID());
148
149                 assertVariables(null, null, null, null, null, "WorkflowException[processKey=DoCreateVnf,errorCode=404,errorMessage=Service Instance Not Found]");
150
151         }
152
153         @Test
154         @Deployment(resources = {"subprocess/DoCreateVnf.bpmn", 
155                                                         "subprocess/GenericGetService.bpmn", 
156                                                         "subprocess/GenericGetVnf.bpmn", 
157                                                         "subprocess/GenericPutVnf.bpmn", 
158                                                         "process/CreateVnfInfra.bpmn", 
159                                                         "subprocess/FalloutHandler.bpmn", 
160                                                         "subprocess/CompleteMsoProcess.bpmn"})
161         public void testCreateVnfInfra_error_vnfExist() throws Exception{
162                 MockNodeQueryServiceInstanceById("MIS%2F1604%2F0026%2FSW_INTERNET", "GenericFlows/getSIUrlById.xml");
163                 MockGetServiceInstance("SDN-ETHERNET-INTERNET", "123456789", "MIS%252F1604%252F0026%252FSW_INTERNET", "GenericFlows/getServiceInstance.xml");
164                 
165                 stubFor(get(urlMatching("/aai/v[0-9]+/network/generic-vnfs/generic-vnf[?]vnf-name=testVnfName123&depth=1"))
166                                 .willReturn(aResponse()
167                                                 .withStatus(200)
168                                                 .withHeader("Content-Type", "text/xml")
169                                                 .withBodyFile("GenericFlows/getGenericVnfResponse.xml")));
170                 
171                 MockPutGenericVnf();
172                 mockUpdateRequestDB(200, "Database/DBUpdateResponse.xml");
173
174                 Map<String, String> variables = new HashMap<String, String>();
175                 setVariables(variables, createVnfInfraRequest, "testRequestId123", "MIS%2F1604%2F0026%2FSW_INTERNET");
176
177                 WorkflowResponse workflowResponse = executeWorkFlow(processEngineRule, "CreateVnfInfra", variables);
178                 waitForWorkflowToFinish(processEngineRule, workflowResponse.getProcessInstanceID());
179
180                 assertVariables(null, null, null, null, null, "WorkflowException[processKey=DoCreateVnf,errorCode=5000,errorMessage=Generic Vnf Already Exist.]");
181
182         }
183
184         private void assertVariables(String exSIFound, String exSISucc, String exVnfFound, String exVnfSucc, String exResponse, String exWorkflowException) {
185
186                 String siFound = BPMNUtil.getVariable(processEngineRule, "CreateVnfInfra", "GENGS_FoundIndicator");
187                 String siSucc = BPMNUtil.getVariable(processEngineRule, "CreateVnfInfra", "GENGS_SuccessIndicator");
188                 String vnfFound = BPMNUtil.getVariable(processEngineRule, "CreateVnfInfra", "GENGV_FoundIndicator");
189                 String vnfSucc = BPMNUtil.getVariable(processEngineRule, "CreateVnfInfra", "GENGV_SuccessIndicator");
190                 String response = BPMNUtil.getVariable(processEngineRule, "CreateVnfInfra", "WorkflowResponse");
191                 String workflowException = BPMNUtil.getVariable(processEngineRule, "CreateVnfInfra", "SavedWorkflowException1");
192
193                 assertEquals(exSIFound, siFound);
194                 assertEquals(exSISucc, siSucc);
195                 assertEquals(exVnfFound, vnfFound);
196                 assertEquals(exVnfSucc, vnfSucc);
197                 assertEquals(exResponse, response);
198                 assertEquals(exWorkflowException, workflowException);
199         }
200
201         private void setVariables(Map<String, String> variables, String request, String requestId, String siId) {
202                 variables.put("isDebugLogEnabled", "true");
203                 variables.put("bpmnRequest", request);
204                 variables.put("mso-request-id", requestId);
205                 variables.put("serviceInstanceId",siId);
206                 variables.put("testVnfId","testVnfId123");
207                 variables.put("vnfType", "STMTN");
208         }
209
210         private void setVariablesSuccess(Map<String, Object> variables, String request, String requestId, String siId) {
211                 variables.put("isDebugLogEnabled", "true");
212                 //variables.put("bpmnRequest", request);
213                 //variables.put("mso-request-id", requestId);
214                 variables.put("serviceInstanceId",siId);
215                 variables.put("requestId", requestId);
216                 variables.put("testVnfId","testVnfId123");
217                 variables.put("vnfType", "STMTN");
218         }
219
220 }