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