aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/MSOInfrastructureBPMN/src/test/java/org/openecomp/mso/bpmn/vcpe/DeleteVcpeResCustServiceTest.java
blob: d026b7a949a771c284969881c5def81becc239ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.openecomp.mso.bpmn.vcpe;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.openecomp.mso.bpmn.mock.StubResponseAAI.MockDeleteAllottedResource;
import static org.openecomp.mso.bpmn.mock.StubResponseAAI.MockGetAllottedResource;
import static org.openecomp.mso.bpmn.mock.StubResponseAAI.MockGetServiceInstance;
import static org.openecomp.mso.bpmn.mock.StubResponseAAI.MockNodeQueryServiceInstanceById;
import static org.openecomp.mso.bpmn.mock.StubResponseAAI.MockPatchAllottedResource;
import static org.openecomp.mso.bpmn.mock.StubResponseAAI.MockQueryAllottedResourceById;
import static org.openecomp.mso.bpmn.mock.StubResponseDatabase.mockUpdateRequestDB;
import static org.openecomp.mso.bpmn.mock.StubResponseSDNCAdapter.mockSDNCAdapter;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.camunda.bpm.engine.test.Deployment;
import org.junit.Test;
import org.openecomp.mso.bpmn.common.BPMNUtil;
import org.openecomp.mso.bpmn.mock.FileUtil;

import com.github.tomakehurst.wiremock.stubbing.Scenario;

public class DeleteVcpeResCustServiceTest extends AbstractTestBase {

	private static final String PROCNAME = "DeleteVcpeResCustService";
	private static final String Prefix = "DVRCS_";
	private static final String AR_BRG_ID = "ar-brgB";
	private static final String AR_TXC_ID = "ar-txcA";
	
	private final CallbackSet callbacks = new CallbackSet();
	private final String request;
	
	public DeleteVcpeResCustServiceTest() throws IOException {
		callbacks.put("deactivate", FileUtil.readResourceFile("__files/VfModularity/SDNCTopologyDeactivateCallback.xml"));
		callbacks.put("delete", FileUtil.readResourceFile("__files/VfModularity/SDNCTopologyDeleteCallback.xml"));
		callbacks.put("unassign", FileUtil.readResourceFile("__files/VfModularity/SDNCTopologyUnassignCallback.xml"));
		
		request = FileUtil.readResourceFile("__files/VCPE/DeleteVcpeResCustService/request.json");
	}
	
	@Test
	@Deployment(resources = {
			"process/DeleteVcpeResCustService.bpmn",
			"subprocess/GenericGetService.bpmn",
			"subprocess/GenericDeleteService.bpmn",
			"subprocess/SDNCAdapterV1.bpmn",
			"subprocess/FalloutHandler.bpmn",
            "subprocess/DoDeleteServiceInstance.bpmn",
			"subprocess/DoDeleteAllottedResourceBRG.bpmn",
			"subprocess/DoDeleteAllottedResourceTXC.bpmn",
            "subprocess/CompleteMsoProcess.bpmn",

            // stubs
            "VCPE/stubprocess/DoDeleteVnfAndModules.bpmn"})
	
	public void testDeleteVcpeResCustService_Success() throws Exception {

		MockNodeQueryServiceInstanceById(INST, "GenericFlows/getSIUrlById.xml");

		// TODO: use INST instead of DEC_INST
		/*
		 * Seems to be a bug in GenericDeleteService (or its subflows) as they
		 * fail to URL-encode the SI id before performing the query so we'll
		 * add a stub for that case, too.
		 */
		MockNodeQueryServiceInstanceById(DEC_INST, "GenericFlows/getSIUrlById.xml");
		
		/*
		 * cannot use MockGetServiceInstance(), because we need to return
		 * different responses as we traverse through the flow
		 */ 

		// initially, the SI includes the ARs
		stubFor(get(urlMatching("/aai/v[0-9]+/business/customers/customer/" + CUST + "/service-subscriptions/service-subscription/" + SVC + "/service-instances/service-instance/" + INST))
				.inScenario("SI retrieval")
				.whenScenarioStateIs(Scenario.STARTED)
				.willReturn(aResponse()
						.withStatus(200)
						.withHeader("Content-Type", "text/xml")
						.withBodyFile("VCPE/DeleteVcpeResCustService/getSI.xml"))
				.willSetStateTo("ARs Deleted"));
		
		// once the ARs have been deleted, the SI should be empty
		stubFor(get(urlMatching("/aai/v[0-9]+/business/customers/customer/" + CUST + "/service-subscriptions/service-subscription/" + SVC + "/service-instances/service-instance/" + INST))
				.inScenario("SI retrieval")
				.whenScenarioStateIs("ARs Deleted")
				.willReturn(aResponse()
						.withStatus(200)
						.withHeader("Content-Type", "text/xml")
						.withBodyFile("VCPE/DeleteVcpeResCustService/getSIAfterDelArs.xml")));

		// for BRG
		MockQueryAllottedResourceById(AR_BRG_ID, "VCPE/DeleteVcpeResCustService/getBRGArUrlById.xml");
		MockGetAllottedResource(CUST, SVC, INST, AR_BRG_ID, "VCPE/DeleteVcpeResCustService/arGetBRGById.xml");
		MockPatchAllottedResource(CUST, SVC, INST, AR_BRG_ID);
		MockDeleteAllottedResource(CUST, SVC, INST, AR_BRG_ID, ARVERS);

		// for TXC
		MockQueryAllottedResourceById(AR_TXC_ID, "VCPE/DeleteVcpeResCustService/getTXCArUrlById.xml");
		MockGetAllottedResource(CUST, SVC, INST, AR_TXC_ID, "VCPE/DeleteVcpeResCustService/arGetTXCById.xml");
		MockPatchAllottedResource(CUST, SVC, INST, AR_TXC_ID);
		MockDeleteAllottedResource(CUST, SVC, INST, AR_TXC_ID, ARVERS);
		
		mockSDNCAdapter(200);
		mockUpdateRequestDB(200, "Database/DBUpdateResponse.xml");
		
		Map<String, Object> variables = setupVariables();

		String businessKey = UUID.randomUUID().toString();
		invokeAsyncProcess(PROCNAME, "v1", businessKey, request, variables);
		
		// for BRG
		injectSDNCCallbacks(callbacks, "deactivate");
		injectSDNCCallbacks(callbacks, "delete");
		injectSDNCCallbacks(callbacks, "unassign");

		// for TXC
		injectSDNCCallbacks(callbacks, "deactivate");
		injectSDNCCallbacks(callbacks, "delete");
		injectSDNCCallbacks(callbacks, "unassign");
		
		// for SI
		injectSDNCCallbacks(callbacks, "deactivate");
		injectSDNCCallbacks(callbacks, "delete");
		
		waitForProcessEnd(businessKey, 10000);

		String workflowException = BPMNUtil.getVariable(processEngineRule, PROCNAME, VAR_WFEX);
		System.out.println("workflowException:\n" + workflowException);

		String completionReq = BPMNUtil.getVariable(processEngineRule, PROCNAME, Prefix+VAR_COMP_REQ);
		System.out.println("completionReq:\n" + completionReq);
		
		assertEquals("true", BPMNUtil.getVariable(processEngineRule, PROCNAME, PROCNAME+VAR_SUCCESS_IND));
		assertEquals("200", BPMNUtil.getVariable(processEngineRule, PROCNAME, VAR_RESP_CODE));
		assertEquals(null, workflowException);
		assertTrue(completionReq.contains("<request-id>testRequestId<"));
		assertTrue(completionReq.contains("<action>DELETE<"));
		assertTrue(completionReq.contains("<source>VID<"));

		assertEquals("2", BPMNUtil.getVariable(processEngineRule, PROCNAME, Prefix+"vnfsDeletedCount"));

		BPMNUtil.assertAnyProcessInstanceFinished(processEngineRule, "DoDeleteVnfAndModules");
	}
	
	@Test
	@Deployment(resources = {
			"process/DeleteVcpeResCustService.bpmn",
			"subprocess/GenericGetService.bpmn",
			"subprocess/GenericDeleteService.bpmn",
			"subprocess/SDNCAdapterV1.bpmn",
			"subprocess/FalloutHandler.bpmn",
            "subprocess/DoDeleteServiceInstance.bpmn",
			"subprocess/DoDeleteAllottedResourceBRG.bpmn",
			"subprocess/DoDeleteAllottedResourceTXC.bpmn",
            "subprocess/CompleteMsoProcess.bpmn",

            // stubs
            "VCPE/stubprocess/DoDeleteVnfAndModules.bpmn"})
	
	public void testDeleteVcpeResCustService_NoBRG_NoTXC_NoVNF() throws Exception {

		MockNodeQueryServiceInstanceById(INST, "GenericFlows/getSIUrlById.xml");

		// TODO: use INST instead of DEC_INST
		/*
		 * Seems to be a bug in GenericDeleteService (or its subflows) as they
		 * fail to URL-encode the SI id before performing the query so we'll
		 * add a stub for that case, too.
		 */
		MockNodeQueryServiceInstanceById(DEC_INST, "GenericFlows/getSIUrlById.xml");
		
		MockGetServiceInstance(CUST, SVC, INST, "VCPE/DeleteVcpeResCustService/getSIAfterDelArs.xml");
		
		mockSDNCAdapter(200);
		mockUpdateRequestDB(200, "Database/DBUpdateResponse.xml");
		
		Map<String, Object> variables = setupVariables();

		String businessKey = UUID.randomUUID().toString();
		invokeAsyncProcess(PROCNAME, "v1", businessKey, request, variables);
		
		// for SI
		injectSDNCCallbacks(callbacks, "deactivate");
		injectSDNCCallbacks(callbacks, "delete");
		
		waitForProcessEnd(businessKey, 10000);

		String workflowException = BPMNUtil.getVariable(processEngineRule, PROCNAME, VAR_WFEX);
		System.out.println("workflowException:\n" + workflowException);

		String completionReq = BPMNUtil.getVariable(processEngineRule, PROCNAME, Prefix+VAR_COMP_REQ);
		System.out.println("completionReq:\n" + completionReq);
		
		assertEquals("true", BPMNUtil.getVariable(processEngineRule, PROCNAME, PROCNAME+VAR_SUCCESS_IND));
		assertEquals("200", BPMNUtil.getVariable(processEngineRule, PROCNAME, VAR_RESP_CODE));
		assertEquals(null, workflowException);
		assertTrue(completionReq.contains("<request-id>testRequestId<"));
		assertTrue(completionReq.contains("<action>DELETE<"));
		assertTrue(completionReq.contains("<source>VID<"));

		assertEquals("0", BPMNUtil.getVariable(processEngineRule, PROCNAME, Prefix+"vnfsDeletedCount"));
		
		BPMNUtil.assertNoProcessInstance(processEngineRule, "DoDeleteVnfAndModules");
	}
	
	@Test
	@Deployment(resources = {
			"process/DeleteVcpeResCustService.bpmn",
			"subprocess/GenericGetService.bpmn",
			"subprocess/GenericDeleteService.bpmn",
			"subprocess/SDNCAdapterV1.bpmn",
			"subprocess/FalloutHandler.bpmn",
            "subprocess/DoDeleteServiceInstance.bpmn",
			"subprocess/DoDeleteAllottedResourceBRG.bpmn",
			"subprocess/DoDeleteAllottedResourceTXC.bpmn",
            "subprocess/CompleteMsoProcess.bpmn",

            // stubs
            "VCPE/stubprocess/DoDeleteVnfAndModules.bpmn"})
	
	public void testDeleteVcpeResCustService_Fault() throws Exception {

		MockNodeQueryServiceInstanceById(INST, "GenericFlows/getSIUrlById.xml");

		// TODO: use INST instead of DEC_INST
		/*
		 * Seems to be a bug in GenericDeleteService (or its subflows) as they
		 * fail to URL-encode the SI id before performing the query so we'll
		 * add a stub for that case, too.
		 */
		MockNodeQueryServiceInstanceById(DEC_INST, "GenericFlows/getSIUrlById.xml");
		
		MockGetServiceInstance(CUST, SVC, INST, "VCPE/DeleteVcpeResCustService/getSIAfterDelArs.xml");
		
		// generate failure
		mockSDNCAdapter(404);
		
		mockUpdateRequestDB(200, "Database/DBUpdateResponse.xml");
		
		Map<String, Object> variables = setupVariables();

		String businessKey = UUID.randomUUID().toString();
		invokeAsyncProcess(PROCNAME, "v1", businessKey, request, variables);
		
		waitForProcessEnd(businessKey, 10000);

		String workflowException = BPMNUtil.getVariable(processEngineRule, PROCNAME, VAR_WFEX);
		System.out.println("workflowException:\n" + workflowException);

		String completionReq = BPMNUtil.getVariable(processEngineRule, PROCNAME, Prefix+VAR_COMP_REQ);
		System.out.println("completionReq:\n" + completionReq);
		
		assertEquals(null, BPMNUtil.getVariable(processEngineRule, PROCNAME, PROCNAME+VAR_SUCCESS_IND));
		assertEquals(null, BPMNUtil.getVariable(processEngineRule, PROCNAME, VAR_RESP_CODE));
		assertNotNull(workflowException);
	}
	
	private Map<String, Object> setupVariables() throws UnsupportedEncodingException {
		Map<String, Object> variables = new HashMap<>();
		variables.put("isDebugLogEnabled", "true");
		variables.put("requestId", "testRequestId");
		variables.put("serviceInstanceId", DEC_INST);
		variables.put("sdncVersion", "1802");
		variables.put("serviceInstanceName", "some-junk-name");
		return variables;
	}
	
}