Merge "Add subscription for notifications"
[so.git] / bpmn / mso-infrastructure-bpmn / src / test / java / org / onap / so / bpmn / vcpe / DeleteVcpeResCustServiceIT.java
1 /*
2  * ============LICENSE_START======================================================= ONAP - SO
3  * ================================================================================ Copyright (C) 2017 AT&T Intellectual
4  * Property. All rights reserved. ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
11  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
12  * specific language governing permissions and limitations under the License.
13  * ============LICENSE_END=========================================================
14  */
15
16 package org.onap.so.bpmn.vcpe;
17
18 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
19 import static com.github.tomakehurst.wiremock.client.WireMock.delete;
20 import static com.github.tomakehurst.wiremock.client.WireMock.get;
21 import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.onap.so.bpmn.mock.StubResponseAAI.MockDeleteAllottedResource;
27 import static org.onap.so.bpmn.mock.StubResponseAAI.MockDeleteServiceInstance;
28 import static org.onap.so.bpmn.mock.StubResponseAAI.MockGetAllottedResource;
29 import static org.onap.so.bpmn.mock.StubResponseAAI.MockGetServiceInstance;
30 import static org.onap.so.bpmn.mock.StubResponseAAI.MockNodeQueryServiceInstanceById;
31 import static org.onap.so.bpmn.mock.StubResponseAAI.MockPatchAllottedResource;
32 import static org.onap.so.bpmn.mock.StubResponseAAI.MockQueryAllottedResourceById;
33 import static org.onap.so.bpmn.mock.StubResponseDatabase.mockUpdateRequestDB;
34 import static org.onap.so.bpmn.mock.StubResponseSDNCAdapter.mockSDNCAdapter;
35 import java.io.IOException;
36 import java.io.UnsupportedEncodingException;
37 import java.util.HashMap;
38 import java.util.Map;
39 import java.util.UUID;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.onap.so.bpmn.common.BPMNUtil;
43 import org.onap.so.bpmn.mock.FileUtil;
44 import com.github.tomakehurst.wiremock.stubbing.Scenario;
45
46 public class DeleteVcpeResCustServiceIT extends AbstractTestBase {
47
48     private static final String PROCNAME = "DeleteVcpeResCustService";
49     private static final String Prefix = "DVRCS_";
50     private static final String AR_BRG_ID = "ar-brgB";
51     private static final String AR_TXC_ID = "ar-txcA";
52
53     private final CallbackSet callbacks = new CallbackSet();
54     private final String request;
55
56     public DeleteVcpeResCustServiceIT() throws IOException {
57         callbacks.put("deactivate",
58                 FileUtil.readResourceFile("__files/VfModularity/SDNCTopologyDeactivateCallback.xml"));
59         callbacks.put("delete", FileUtil.readResourceFile("__files/VfModularity/SDNCTopologyDeleteCallback.xml"));
60         callbacks.put("unassign", FileUtil.readResourceFile("__files/VfModularity/SDNCTopologyUnassignCallback.xml"));
61
62         request = FileUtil.readResourceFile("__files/VCPE/DeleteVcpeResCustService/request.json");
63     }
64
65     @Before
66     public void init() {
67         BPMNUtil.cleanHistory(processEngine);
68     }
69
70     @Test
71     public void testDeleteVcpeResCustService_Success() throws Exception {
72         logStart();
73         MockNodeQueryServiceInstanceById(wireMockServer, INST, "GenericFlows/getSIUrlById.xml");
74
75         // TODO: use INST instead of DEC_INST
76         /*
77          * Seems to be a bug as they fail to URL-encode the SI id before performing the query so we'll add a stub for
78          * that case, too.
79          */
80         MockNodeQueryServiceInstanceById(wireMockServer, DEC_INST, "GenericFlows/getSIUrlById.xml");
81
82         /*
83          * cannot use MockGetServiceInstance(wireMockServer, ), because we need to return different responses as we
84          * traverse through the flow
85          */
86
87         // initially, the SI includes the ARs
88         wireMockServer.stubFor(get(urlMatching("/aai/v[0-9]+/business/customers/customer/" + CUST
89                 + "/service-subscriptions/service-subscription/" + SVC + "/service-instances/service-instance/" + INST))
90                         .inScenario("SI retrieval").whenScenarioStateIs(Scenario.STARTED)
91                         .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "text/xml")
92                                 .withBodyFile("VCPE/DeleteVcpeResCustService/getSI.xml"))
93                         .willSetStateTo("ARs Deleted"));
94
95         // once the ARs have been deleted, the SI should be empty
96         wireMockServer.stubFor(get(urlMatching("/aai/v[0-9]+/business/customers/customer/" + CUST
97                 + "/service-subscriptions/service-subscription/" + SVC + "/service-instances/service-instance/" + INST))
98                         .inScenario("SI retrieval").whenScenarioStateIs("ARs Deleted")
99                         .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "text/xml")
100                                 .withBodyFile("VCPE/DeleteVcpeResCustService/getSIAfterDelArs.xml")));
101
102         // for BRG
103         MockQueryAllottedResourceById(wireMockServer, AR_BRG_ID, "VCPE/DeleteVcpeResCustService/getBRGArUrlById.xml");
104         MockGetAllottedResource(wireMockServer, CUST, SVC, INST, AR_BRG_ID,
105                 "VCPE/DeleteVcpeResCustService/arGetBRGById.xml");
106         MockPatchAllottedResource(wireMockServer, CUST, SVC, INST, AR_BRG_ID);
107         MockDeleteAllottedResource(wireMockServer, CUST, SVC, INST, AR_BRG_ID, ARVERS);
108
109         // for TXC
110         MockQueryAllottedResourceById(wireMockServer, AR_TXC_ID, "VCPE/DeleteVcpeResCustService/getTXCArUrlById.xml");
111         MockGetAllottedResource(wireMockServer, CUST, SVC, INST, AR_TXC_ID,
112                 "VCPE/DeleteVcpeResCustService/arGetTXCById.xml");
113         MockPatchAllottedResource(wireMockServer, CUST, SVC, INST, AR_TXC_ID);
114         MockDeleteAllottedResource(wireMockServer, CUST, SVC, INST, AR_TXC_ID, ARVERS);
115
116         // MockGetGenericVnfById("vnfX.*", "GenericFlows/getGenericVnfByNameResponse.xml");
117         wireMockServer.stubFor(get(urlMatching("/aai/v[0-9]+/network/generic-vnfs/generic-vnf/.*"))
118                 .willReturn(aResponse().withStatus(200).withHeader("Content-Type", "text/xml")
119                         .withBodyFile("GenericFlows/getGenericVnfByNameResponse.xml")));
120
121
122         wireMockServer.stubFor(delete(urlMatching("/aai/v[0-9]+/network/generic-vnfs/generic-vnf/.*"))
123                 .willReturn(aResponse().withStatus(204).withHeader("Content-Type", "text/xml")));
124
125         MockDeleteServiceInstance(wireMockServer, CUST, SVC, INST, SVC);
126
127         mockSDNCAdapter(wireMockServer, 200);
128         mockUpdateRequestDB(wireMockServer, 200, "Database/DBUpdateResponse.xml");
129
130         String businessKey = UUID.randomUUID().toString();
131         Map<String, Object> variables = setupVariables(businessKey);
132
133         invokeAsyncProcess(PROCNAME, "v1", businessKey, request, variables);
134
135         // for BRG
136         injectSDNCCallbacks(callbacks, "deactivate");
137         injectSDNCCallbacks(callbacks, "delete");
138         injectSDNCCallbacks(callbacks, "unassign");
139
140         // for VNF1
141         injectSDNCCallbacks(callbacks, "deactivate");
142         injectSDNCCallbacks(callbacks, "unassign");
143
144         // for VNF2
145         injectSDNCCallbacks(callbacks, "deactivate");
146         injectSDNCCallbacks(callbacks, "unassign");
147
148         // for TXC
149         injectSDNCCallbacks(callbacks, "deactivate");
150         injectSDNCCallbacks(callbacks, "delete");
151         injectSDNCCallbacks(callbacks, "unassign");
152
153         // for SI
154         injectSDNCCallbacks(callbacks, "deactivate");
155         injectSDNCCallbacks(callbacks, "delete");
156
157         waitForProcessEnd(businessKey, 70000);
158         assertTrue(isProcessEnded(businessKey));
159
160         String workflowException = BPMNUtil.getVariable(processEngine, PROCNAME, VAR_WFEX);
161         System.out.println("workflowException:\n" + workflowException);
162
163         String completionReq = BPMNUtil.getVariable(processEngine, PROCNAME, Prefix + VAR_COMP_REQ);
164         System.out.println("completionReq:\n" + completionReq);
165
166         assertEquals("true", BPMNUtil.getVariable(processEngine, PROCNAME, PROCNAME + VAR_SUCCESS_IND));
167         assertEquals("200", BPMNUtil.getVariable(processEngine, PROCNAME, VAR_RESP_CODE));
168         assertNull(workflowException);
169         assertTrue(completionReq.contains("<request-id>" + businessKey + "<"));
170         assertTrue(completionReq.contains("<action>DELETE<"));
171         assertTrue(completionReq.contains("<source>VID<"));
172
173         assertEquals("2", BPMNUtil.getVariable(processEngine, PROCNAME, Prefix + "vnfsDeletedCount"));
174
175         BPMNUtil.assertAnyProcessInstanceFinished(processEngine, "DoDeleteVnfAndModules");
176         logEnd();
177     }
178
179     @Test
180     public void testDeleteVcpeResCustService_NoBRG_NoTXC_NoVNF() throws Exception {
181         logStart();
182         MockNodeQueryServiceInstanceById(wireMockServer, INST, "GenericFlows/getSIUrlById.xml");
183
184         // TODO: use INST instead of DEC_INST
185         /*
186          * Seems to be a bug as they fail to URL-encode the SI id before performing the query so we'll add a stub for
187          * that case, too.
188          */
189         MockNodeQueryServiceInstanceById(wireMockServer, DEC_INST, "GenericFlows/getSIUrlById.xml");
190
191         MockGetServiceInstance(wireMockServer, CUST, SVC, INST, "VCPE/DeleteVcpeResCustService/getSIAfterDelArs.xml");
192
193         mockSDNCAdapter(wireMockServer, 200);
194         mockUpdateRequestDB(wireMockServer, 200, "Database/DBUpdateResponse.xml");
195
196         String businessKey = UUID.randomUUID().toString();
197         Map<String, Object> variables = setupVariables(businessKey);
198
199
200         invokeAsyncProcess(PROCNAME, "v1", businessKey, request, variables);
201
202         // for SI
203         injectSDNCCallbacks(callbacks, "deactivate");
204         injectSDNCCallbacks(callbacks, "delete");
205
206         waitForProcessEnd(businessKey, 70000);
207
208         String workflowException = BPMNUtil.getVariable(processEngine, PROCNAME, VAR_WFEX);
209         System.out.println("workflowException:\n" + workflowException);
210
211         String completionReq = BPMNUtil.getVariable(processEngine, PROCNAME, Prefix + VAR_COMP_REQ);
212         System.out.println("completionReq:\n" + completionReq);
213
214         assertEquals("true", BPMNUtil.getVariable(processEngine, PROCNAME, PROCNAME + VAR_SUCCESS_IND));
215         assertEquals("200", BPMNUtil.getVariable(processEngine, PROCNAME, VAR_RESP_CODE));
216         assertNull(workflowException);
217         assertTrue(completionReq.contains("<request-id>" + businessKey + "<"));
218         assertTrue(completionReq.contains("<action>DELETE<"));
219         assertTrue(completionReq.contains("<source>VID<"));
220
221         assertEquals("0", BPMNUtil.getVariable(processEngine, PROCNAME, Prefix + "vnfsDeletedCount"));
222
223         BPMNUtil.assertNoProcessInstance(processEngine, "DoDeleteVnfAndModules");
224         logEnd();
225     }
226
227     @Test
228     public void testDeleteVcpeResCustService_Fault() throws Exception {
229         logStart();
230         MockNodeQueryServiceInstanceById(wireMockServer, INST, "GenericFlows/getSIUrlById.xml");
231
232         // TODO: use INST instead of DEC_INST
233         /*
234          * Seems to be a bug as they fail to URL-encode the SI id before performing the query so we'll add a stub for
235          * that case, too.
236          */
237         MockNodeQueryServiceInstanceById(wireMockServer, DEC_INST, "GenericFlows/getSIUrlById.xml");
238
239         MockGetServiceInstance(wireMockServer, CUST, SVC, INST, "VCPE/DeleteVcpeResCustService/getSIAfterDelArs.xml");
240
241         // generate failure
242         mockSDNCAdapter(wireMockServer, 404);
243
244         mockUpdateRequestDB(wireMockServer, 200, "Database/DBUpdateResponse.xml");
245
246         String businessKey = UUID.randomUUID().toString();
247         Map<String, Object> variables = setupVariables(businessKey);
248
249         invokeAsyncProcess(PROCNAME, "v1", businessKey, request, variables);
250
251         waitForProcessEnd(businessKey, 70000);
252
253         String workflowException = BPMNUtil.getVariable(processEngine, PROCNAME, VAR_WFEX);
254         System.out.println("workflowException:\n" + workflowException);
255
256         String completionReq = BPMNUtil.getVariable(processEngine, PROCNAME, Prefix + VAR_COMP_REQ);
257         System.out.println("completionReq:\n" + completionReq);
258
259         assertEquals(null, BPMNUtil.getVariable(processEngine, PROCNAME, PROCNAME + VAR_SUCCESS_IND));
260         assertEquals(null, BPMNUtil.getVariable(processEngine, PROCNAME, VAR_RESP_CODE));
261         assertNotNull(workflowException);
262         logEnd();
263     }
264
265     private Map<String, Object> setupVariables(String requestId) throws UnsupportedEncodingException {
266         Map<String, Object> variables = new HashMap<>();
267         variables.put("isDebugLogEnabled", "true");
268         variables.put("requestId", requestId);
269         variables.put("serviceInstanceId", DEC_INST);
270         variables.put("sdncVersion", "1802");
271         variables.put("serviceInstanceName", "some-junk-name");
272         return variables;
273     }
274
275 }