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