Merge "Reduce log noise/warnings format to conventions"
[so.git] / bpmn / MSOInfrastructureBPMN / src / test / java / org / openecomp / mso / bpmn / vcpe / DeleteVcpeResCustServiceTest.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.openecomp.mso.bpmn.vcpe;
21
22 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
23 import static com.github.tomakehurst.wiremock.client.WireMock.get;
24 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
25 import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.openecomp.mso.bpmn.mock.StubResponseAAI.MockDeleteAllottedResource;
30 import static org.openecomp.mso.bpmn.mock.StubResponseAAI.MockGetAllottedResource;
31 import static org.openecomp.mso.bpmn.mock.StubResponseAAI.MockGetServiceInstance;
32 import static org.openecomp.mso.bpmn.mock.StubResponseAAI.MockNodeQueryServiceInstanceById;
33 import static org.openecomp.mso.bpmn.mock.StubResponseAAI.MockPatchAllottedResource;
34 import static org.openecomp.mso.bpmn.mock.StubResponseAAI.MockQueryAllottedResourceById;
35 import static org.openecomp.mso.bpmn.mock.StubResponseDatabase.mockUpdateRequestDB;
36 import static org.openecomp.mso.bpmn.mock.StubResponseSDNCAdapter.mockSDNCAdapter;
37
38 import java.io.IOException;
39 import java.io.UnsupportedEncodingException;
40 import java.util.HashMap;
41 import java.util.Map;
42 import java.util.UUID;
43
44 import org.camunda.bpm.engine.test.Deployment;
45 import org.junit.Test;
46 import org.openecomp.mso.bpmn.common.BPMNUtil;
47 import org.openecomp.mso.bpmn.mock.FileUtil;
48
49 import com.github.tomakehurst.wiremock.stubbing.Scenario;
50
51 public class DeleteVcpeResCustServiceTest extends AbstractTestBase {
52
53     private static final String PROCNAME = "DeleteVcpeResCustService";
54     private static final String Prefix = "DVRCS_";
55     private static final String AR_BRG_ID = "ar-brgB";
56     private static final String AR_TXC_ID = "ar-txcA";
57
58     private final CallbackSet callbacks = new CallbackSet();
59     private final String request;
60
61     public DeleteVcpeResCustServiceTest() throws IOException {
62         callbacks.put("deactivate", FileUtil.readResourceFile("__files/VfModularity/SDNCTopologyDeactivateCallback.xml"));
63         callbacks.put("delete", FileUtil.readResourceFile("__files/VfModularity/SDNCTopologyDeleteCallback.xml"));
64         callbacks.put("unassign", FileUtil.readResourceFile("__files/VfModularity/SDNCTopologyUnassignCallback.xml"));
65
66         request = FileUtil.readResourceFile("__files/VCPE/DeleteVcpeResCustService/request.json");
67     }
68
69     @Test
70     @Deployment(resources = {
71             "process/DeleteVcpeResCustService.bpmn",
72             "subprocess/GenericGetService.bpmn",
73             "subprocess/GenericDeleteService.bpmn",
74             "subprocess/SDNCAdapterV1.bpmn",
75             "subprocess/FalloutHandler.bpmn",
76             "subprocess/DoDeleteServiceInstance.bpmn",
77             "subprocess/DoDeleteAllottedResourceBRG.bpmn",
78             "subprocess/DoDeleteAllottedResourceTXC.bpmn",
79             "subprocess/CompleteMsoProcess.bpmn",
80
81             // stubs
82             "VCPE/stubprocess/DoDeleteVnfAndModules.bpmn"})
83
84     public void testDeleteVcpeResCustService_Success() throws Exception {
85
86         MockNodeQueryServiceInstanceById(INST, "GenericFlows/getSIUrlById.xml");
87
88         // TODO: use INST instead of DEC_INST
89         /*
90                  * Seems to be a bug in GenericDeleteService (or its subflows) as they
91                  * fail to URL-encode the SI id before performing the query so we'll
92                  * add a stub for that case, too.
93                  */
94         MockNodeQueryServiceInstanceById(DEC_INST, "GenericFlows/getSIUrlById.xml");
95                 
96                 /*
97                  * cannot use MockGetServiceInstance(), because we need to return
98                  * different responses as we traverse through the flow
99                  */
100
101         // initially, the SI includes the ARs
102         stubFor(get(urlMatching("/aai/v[0-9]+/business/customers/customer/" + CUST + "/service-subscriptions/service-subscription/" + SVC + "/service-instances/service-instance/" + INST))
103                 .inScenario("SI retrieval")
104                 .whenScenarioStateIs(Scenario.STARTED)
105                 .willReturn(aResponse()
106                         .withStatus(200)
107                         .withHeader("Content-Type", "text/xml")
108                         .withBodyFile("VCPE/DeleteVcpeResCustService/getSI.xml"))
109                 .willSetStateTo("ARs Deleted"));
110
111         // once the ARs have been deleted, the SI should be empty
112         stubFor(get(urlMatching("/aai/v[0-9]+/business/customers/customer/" + CUST + "/service-subscriptions/service-subscription/" + SVC + "/service-instances/service-instance/" + INST))
113                 .inScenario("SI retrieval")
114                 .whenScenarioStateIs("ARs Deleted")
115                 .willReturn(aResponse()
116                         .withStatus(200)
117                         .withHeader("Content-Type", "text/xml")
118                         .withBodyFile("VCPE/DeleteVcpeResCustService/getSIAfterDelArs.xml")));
119
120         // for BRG
121         MockQueryAllottedResourceById(AR_BRG_ID, "VCPE/DeleteVcpeResCustService/getBRGArUrlById.xml");
122         MockGetAllottedResource(CUST, SVC, INST, AR_BRG_ID, "VCPE/DeleteVcpeResCustService/arGetBRGById.xml");
123         MockPatchAllottedResource(CUST, SVC, INST, AR_BRG_ID);
124         MockDeleteAllottedResource(CUST, SVC, INST, AR_BRG_ID, ARVERS);
125
126         // for TXC
127         MockQueryAllottedResourceById(AR_TXC_ID, "VCPE/DeleteVcpeResCustService/getTXCArUrlById.xml");
128         MockGetAllottedResource(CUST, SVC, INST, AR_TXC_ID, "VCPE/DeleteVcpeResCustService/arGetTXCById.xml");
129         MockPatchAllottedResource(CUST, SVC, INST, AR_TXC_ID);
130         MockDeleteAllottedResource(CUST, SVC, INST, AR_TXC_ID, ARVERS);
131
132         mockSDNCAdapter(200);
133         mockUpdateRequestDB(200, "Database/DBUpdateResponse.xml");
134
135         Map<String, Object> variables = setupVariables();
136
137         String businessKey = UUID.randomUUID().toString();
138         invokeAsyncProcess(PROCNAME, "v1", businessKey, request, variables);
139
140         // for BRG
141         injectSDNCCallbacks(callbacks, "deactivate");
142         injectSDNCCallbacks(callbacks, "delete");
143         injectSDNCCallbacks(callbacks, "unassign");
144
145         // for TXC
146         injectSDNCCallbacks(callbacks, "deactivate");
147         injectSDNCCallbacks(callbacks, "delete");
148         injectSDNCCallbacks(callbacks, "unassign");
149
150         // for SI
151         injectSDNCCallbacks(callbacks, "deactivate");
152         injectSDNCCallbacks(callbacks, "delete");
153
154         waitForProcessEnd(businessKey, 10000);
155
156         String workflowException = BPMNUtil.getVariable(processEngineRule, PROCNAME, VAR_WFEX);
157         System.out.println("workflowException:\n" + workflowException);
158
159         String completionReq = BPMNUtil.getVariable(processEngineRule, PROCNAME, Prefix + VAR_COMP_REQ);
160         System.out.println("completionReq:\n" + completionReq);
161
162         assertEquals("true", BPMNUtil.getVariable(processEngineRule, PROCNAME, PROCNAME + VAR_SUCCESS_IND));
163         assertEquals("200", BPMNUtil.getVariable(processEngineRule, PROCNAME, VAR_RESP_CODE));
164         assertEquals(null, workflowException);
165         assertTrue(completionReq.contains("<request-id>testRequestId<"));
166         assertTrue(completionReq.contains("<action>DELETE<"));
167         assertTrue(completionReq.contains("<source>VID<"));
168
169         assertEquals("2", BPMNUtil.getVariable(processEngineRule, PROCNAME, Prefix + "vnfsDeletedCount"));
170
171         BPMNUtil.assertAnyProcessInstanceFinished(processEngineRule, "DoDeleteVnfAndModules");
172     }
173
174     @Test
175     @Deployment(resources = {
176             "process/DeleteVcpeResCustService.bpmn",
177             "subprocess/GenericGetService.bpmn",
178             "subprocess/GenericDeleteService.bpmn",
179             "subprocess/SDNCAdapterV1.bpmn",
180             "subprocess/FalloutHandler.bpmn",
181             "subprocess/DoDeleteServiceInstance.bpmn",
182             "subprocess/DoDeleteAllottedResourceBRG.bpmn",
183             "subprocess/DoDeleteAllottedResourceTXC.bpmn",
184             "subprocess/CompleteMsoProcess.bpmn",
185
186             // stubs
187             "VCPE/stubprocess/DoDeleteVnfAndModules.bpmn"})
188
189     public void testDeleteVcpeResCustService_NoBRG_NoTXC_NoVNF() throws Exception {
190
191         MockNodeQueryServiceInstanceById(INST, "GenericFlows/getSIUrlById.xml");
192
193         // TODO: use INST instead of DEC_INST
194                 /*
195                  * Seems to be a bug in GenericDeleteService (or its subflows) as they
196                  * fail to URL-encode the SI id before performing the query so we'll
197                  * add a stub for that case, too.
198                  */
199         MockNodeQueryServiceInstanceById(DEC_INST, "GenericFlows/getSIUrlById.xml");
200
201         MockGetServiceInstance(CUST, SVC, INST, "VCPE/DeleteVcpeResCustService/getSIAfterDelArs.xml");
202
203         mockSDNCAdapter(200);
204         mockUpdateRequestDB(200, "Database/DBUpdateResponse.xml");
205
206         Map<String, Object> variables = setupVariables();
207
208         String businessKey = UUID.randomUUID().toString();
209         invokeAsyncProcess(PROCNAME, "v1", businessKey, request, variables);
210
211         // for SI
212         injectSDNCCallbacks(callbacks, "deactivate");
213         injectSDNCCallbacks(callbacks, "delete");
214
215         waitForProcessEnd(businessKey, 10000);
216
217         String workflowException = BPMNUtil.getVariable(processEngineRule, PROCNAME, VAR_WFEX);
218         System.out.println("workflowException:\n" + workflowException);
219
220         String completionReq = BPMNUtil.getVariable(processEngineRule, PROCNAME, Prefix + VAR_COMP_REQ);
221         System.out.println("completionReq:\n" + completionReq);
222
223         assertEquals("true", BPMNUtil.getVariable(processEngineRule, PROCNAME, PROCNAME + VAR_SUCCESS_IND));
224         assertEquals("200", BPMNUtil.getVariable(processEngineRule, PROCNAME, VAR_RESP_CODE));
225         assertEquals(null, workflowException);
226         assertTrue(completionReq.contains("<request-id>testRequestId<"));
227         assertTrue(completionReq.contains("<action>DELETE<"));
228         assertTrue(completionReq.contains("<source>VID<"));
229
230         assertEquals("0", BPMNUtil.getVariable(processEngineRule, PROCNAME, Prefix + "vnfsDeletedCount"));
231
232         BPMNUtil.assertNoProcessInstance(processEngineRule, "DoDeleteVnfAndModules");
233     }
234
235     @Test
236     @Deployment(resources = {
237             "process/DeleteVcpeResCustService.bpmn",
238             "subprocess/GenericGetService.bpmn",
239             "subprocess/GenericDeleteService.bpmn",
240             "subprocess/SDNCAdapterV1.bpmn",
241             "subprocess/FalloutHandler.bpmn",
242             "subprocess/DoDeleteServiceInstance.bpmn",
243             "subprocess/DoDeleteAllottedResourceBRG.bpmn",
244             "subprocess/DoDeleteAllottedResourceTXC.bpmn",
245             "subprocess/CompleteMsoProcess.bpmn",
246
247             // stubs
248             "VCPE/stubprocess/DoDeleteVnfAndModules.bpmn"})
249
250     public void testDeleteVcpeResCustService_Fault() throws Exception {
251
252         MockNodeQueryServiceInstanceById(INST, "GenericFlows/getSIUrlById.xml");
253
254         // TODO: use INST instead of DEC_INST
255                 /*
256                  * Seems to be a bug in GenericDeleteService (or its subflows) as they
257                  * fail to URL-encode the SI id before performing the query so we'll
258                  * add a stub for that case, too.
259                  */
260         MockNodeQueryServiceInstanceById(DEC_INST, "GenericFlows/getSIUrlById.xml");
261
262         MockGetServiceInstance(CUST, SVC, INST, "VCPE/DeleteVcpeResCustService/getSIAfterDelArs.xml");
263
264         // generate failure
265         mockSDNCAdapter(404);
266
267         mockUpdateRequestDB(200, "Database/DBUpdateResponse.xml");
268
269         Map<String, Object> variables = setupVariables();
270
271         String businessKey = UUID.randomUUID().toString();
272         invokeAsyncProcess(PROCNAME, "v1", businessKey, request, variables);
273
274         waitForProcessEnd(businessKey, 10000);
275
276         String workflowException = BPMNUtil.getVariable(processEngineRule, PROCNAME, VAR_WFEX);
277         System.out.println("workflowException:\n" + workflowException);
278
279         String completionReq = BPMNUtil.getVariable(processEngineRule, PROCNAME, Prefix + VAR_COMP_REQ);
280         System.out.println("completionReq:\n" + completionReq);
281
282         assertEquals(null, BPMNUtil.getVariable(processEngineRule, PROCNAME, PROCNAME + VAR_SUCCESS_IND));
283         assertEquals(null, BPMNUtil.getVariable(processEngineRule, PROCNAME, VAR_RESP_CODE));
284         assertNotNull(workflowException);
285     }
286
287     private Map<String, Object> setupVariables() throws UnsupportedEncodingException {
288         Map<String, Object> variables = new HashMap<>();
289         variables.put("isDebugLogEnabled", "true");
290         variables.put("requestId", "testRequestId");
291         variables.put("serviceInstanceId", DEC_INST);
292         variables.put("sdncVersion", "1802");
293         variables.put("serviceInstanceName", "some-junk-name");
294         return variables;
295     }
296
297 }