aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/MSOCommonBPMN/src/test/java/org/openecomp/mso/bpmn/common/SDNCAdapterV1Test.java
blob: 846a14f45ff8ca3974fcdab3a263457c31fc1ddc (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*- 
 * ============LICENSE_START======================================================= 
 * OPENECOMP - MSO 
 * ================================================================================ 
 * 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.common;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.openecomp.mso.bpmn.mock.StubResponseDatabase.mockUpdateRequestDB;
import static org.openecomp.mso.bpmn.mock.StubResponseSDNCAdapter.mockSDNCAdapter;

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

import javax.ws.rs.core.Response;

import org.camunda.bpm.engine.MismatchingMessageCorrelationException;
import org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.camunda.bpm.engine.runtime.Job;
import org.camunda.bpm.engine.test.Deployment;
import org.camunda.bpm.engine.variable.impl.VariableMapImpl;
import org.junit.Assert;
import org.junit.Test;
import org.openecomp.mso.bpmn.common.adapter.sdnc.CallbackHeader;
import org.openecomp.mso.bpmn.common.adapter.sdnc.SDNCAdapterCallbackRequest;
import org.openecomp.mso.bpmn.common.adapter.sdnc.SDNCAdapterResponse;
import org.openecomp.mso.bpmn.common.workflow.service.SDNCAdapterCallbackServiceImpl;
import org.openecomp.mso.bpmn.common.workflow.service.SDNCAdapterCallbackServiceImpl.SDNCAdapterExceptionResponse;
import org.openecomp.mso.bpmn.common.workflow.service.WorkflowResource;
import org.openecomp.mso.bpmn.common.workflow.service.WorkflowResponse;
import org.openecomp.mso.bpmn.mock.FileUtil;

/**
 * Unit test cases for SDNCAdapterV1.bpmn
 */
public class SDNCAdapterV1Test extends WorkflowTest {

	private String sdncAdapterWorkflowRequest;
	private String sdncAdapterWorkflowRequestAct;
	private String sdncAdapterCallbackRequestData;
	private String sdncAdapterCallbackRequestDataNonfinal;

	public SDNCAdapterV1Test() throws IOException {
		sdncAdapterWorkflowRequest = FileUtil.readResourceFile("sdncadapterworkflowrequest.xml");
		sdncAdapterWorkflowRequestAct = FileUtil.readResourceFile("sdncadapterworkflowrequest-act.xml");
		sdncAdapterCallbackRequestData = FileUtil.readResourceFile("sdncadaptercallbackrequestdata.text");
		sdncAdapterCallbackRequestDataNonfinal = FileUtil.readResourceFile("sdncadaptercallbackrequestdata-nonfinal.text");
	}

	/**
	 * End-to-End flow - Unit test for SDNCAdapterV1.bpmn
	 *  - String input & String response
	 */

	private WorkflowResponse invokeFlow(String workflowRequest) {

		Map<String, Object>valueMap = new HashMap<String, Object>();
		valueMap.put("value", workflowRequest);
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("sdncAdapterWorkflowRequest", valueMap);

		Map<String, Object> valueMap2 = new HashMap<String, Object>();
		valueMap2.put("value", "true");
		variables.put("isDebugLogEnabled", valueMap2);

		VariableMapImpl varMap = new VariableMapImpl();
		varMap.put("variables", variables);

		//System.out.println("Invoking the flow");

		WorkflowResource workflowResource = new WorkflowResource();
		workflowResource.setProcessEngineServices4junit(processEngineRule);

		Response response = workflowResource.startProcessInstanceByKey("sdncAdapter", varMap);
		WorkflowResponse workflowResponse = (WorkflowResponse) response.getEntity();

		//String pid = workflowResponse.getProcessInstanceID();
		//System.out.println("Back from executing process instance with pid=" + pid);
		return workflowResponse;
	}

	@Test
	@Deployment(resources = {"subprocess/SDNCAdapterV1.bpmn",
			"subprocess/GenericNotificationService.bpmn"
			})
	public void sunnyDay() throws InterruptedException {

		mockSDNCAdapter(200);

		//System.out.println("SDNCAdapter sunny day flow Started!");

		ProcessExecutionThread thread = new ProcessExecutionThread(sdncAdapterWorkflowRequest);
		thread.start();
		waitForExecutionToStart("sdncAdapter", 3);
		String pid = getPid();

		assertProcessInstanceNotFinished(pid);

		System.out.println("Injecting SDNC Adapter asynchronous callback message to continue processing");
		String generatedRequestId = (String) processEngineRule.getRuntimeService().getVariable(pid, "SDNCA_requestId");
		CallbackHeader callbackHeader = new CallbackHeader();
		callbackHeader.setRequestId(generatedRequestId);
		callbackHeader.setResponseCode("200");
		callbackHeader.setResponseMessage("OK");
		SDNCAdapterCallbackRequest sdncAdapterCallbackRequest = new SDNCAdapterCallbackRequest();
		sdncAdapterCallbackRequest.setCallbackHeader(callbackHeader);
		sdncAdapterCallbackRequest.setRequestData(sdncAdapterCallbackRequestData);
		SDNCAdapterCallbackServiceImpl callbackService = new SDNCAdapterCallbackServiceImpl();
		callbackService.setProcessEngineServices4junit(processEngineRule);
		SDNCAdapterResponse sdncAdapterResponse = callbackService.sdncAdapterCallback(sdncAdapterCallbackRequest);
		//System.out.println("Back from executing process again");

		assertFalse(sdncAdapterResponse instanceof SDNCAdapterExceptionResponse);
		assertProcessInstanceFinished(pid);

		//System.out.println("SDNCAdapter sunny day flow Completed!");
	}

	@Test
	@Deployment(resources = {"subprocess/SDNCAdapterV1.bpmn",
			"subprocess/GenericNotificationService.bpmn"
			})
	public void nonFinalWithTimeout() throws InterruptedException {

		mockSDNCAdapter(200);
		mockUpdateRequestDB(200, "Database/DBAdapter.xml");

		//System.out.println("SDNCAdapter interim status processing flow Started!");

		ProcessExecutionThread thread = new ProcessExecutionThread(sdncAdapterWorkflowRequestAct);
		thread.start();
		waitForExecutionToStart("sdncAdapter", 3);
		String pid = getPid();

		assertProcessInstanceNotFinished(pid);

		//System.out.println("Injecting SDNC Adapter asynchronous callback message to continue processing");
		String generatedRequestId = (String) processEngineRule.getRuntimeService().getVariable(pid, "SDNCA_requestId");
		CallbackHeader callbackHeader = new CallbackHeader();
		callbackHeader.setRequestId(generatedRequestId);
		callbackHeader.setResponseCode("200");
		callbackHeader.setResponseMessage("OK");
		SDNCAdapterCallbackRequest sdncAdapterCallbackRequest = new SDNCAdapterCallbackRequest();
		sdncAdapterCallbackRequest.setCallbackHeader(callbackHeader);
		sdncAdapterCallbackRequest.setRequestData(sdncAdapterCallbackRequestDataNonfinal);
		SDNCAdapterCallbackServiceImpl callbackService = new SDNCAdapterCallbackServiceImpl();
		callbackService.setProcessEngineServices4junit(processEngineRule);
		SDNCAdapterResponse sdncAdapterResponse = callbackService.sdncAdapterCallback(sdncAdapterCallbackRequest);
		//System.out.println("Back from executing process again");

		assertFalse(sdncAdapterResponse instanceof SDNCAdapterExceptionResponse);
		assertProcessInstanceNotFinished(pid);

		checkForTimeout(pid);

		assertEquals(true, (Boolean) (getVariable(pid, "continueListening")));


		//System.out.println("SDNCAdapter interim status processing flow Completed!");
	}

	@Test
	@Deployment(resources = {"subprocess/SDNCAdapterV1.bpmn",
			"subprocess/GenericNotificationService.bpmn"
			})
	public void nonFinalThenFinal() throws InterruptedException {

		mockSDNCAdapter(200);
		mockUpdateRequestDB(200, "Database/DBAdapter.xml");

		//System.out.println("SDNCAdapter non-final then final processing flow Started!");

		// Start the flow
		ProcessExecutionThread thread = new ProcessExecutionThread(sdncAdapterWorkflowRequestAct);
		thread.start();
		waitForExecutionToStart("sdncAdapter", 3);
		String pid = getPid();

		assertProcessInstanceNotFinished(pid);

		// Inject a "non-final" SDNC Adapter asynchronous callback message
		//System.out.println("Injecting SDNC Adapter asynchronous callback message to continue processing");
		String generatedRequestId = (String) processEngineRule.getRuntimeService().getVariable(pid, "SDNCA_requestId");
		CallbackHeader callbackHeader = new CallbackHeader();
		callbackHeader.setRequestId(generatedRequestId);
		callbackHeader.setResponseCode("200");
		callbackHeader.setResponseMessage("OK");
		SDNCAdapterCallbackRequest sdncAdapterCallbackRequest = new SDNCAdapterCallbackRequest();
		sdncAdapterCallbackRequest.setCallbackHeader(callbackHeader);
		sdncAdapterCallbackRequest.setRequestData(sdncAdapterCallbackRequestDataNonfinal);
		SDNCAdapterCallbackServiceImpl callbackService = new SDNCAdapterCallbackServiceImpl();
		callbackService.setProcessEngineServices4junit(processEngineRule);
		SDNCAdapterResponse sdncAdapterResponse = callbackService.sdncAdapterCallback(sdncAdapterCallbackRequest);
		//System.out.println("Back from executing process again");

		assertFalse(sdncAdapterResponse instanceof SDNCAdapterExceptionResponse);
		assertProcessInstanceNotFinished(pid);
		assertEquals(true, (Boolean) (getVariable(pid, "continueListening")));

		// Inject a "final" SDNC Adapter asynchronous callback message
		sdncAdapterCallbackRequest.setRequestData(sdncAdapterCallbackRequestData);
		sdncAdapterResponse = callbackService.sdncAdapterCallback(sdncAdapterCallbackRequest);
		//System.out.println("Back from executing process again");

		assertFalse(sdncAdapterResponse instanceof SDNCAdapterExceptionResponse);
		assertProcessInstanceFinished(pid);
		assertEquals(false, (Boolean) (getVariable(pid, "continueListening")));

		//System.out.println("SDNCAdapter non-final then final processing flow Completed!");
	}

	

	private void waitForExecutionToStart(String processDefintion, int count) throws InterruptedException {
		//System.out.println(processEngineRule.getRuntimeService().createExecutionQuery().processDefinitionKey(processDefintion).count());
		while (processEngineRule.getRuntimeService().createExecutionQuery().processDefinitionKey(processDefintion).count() != count) {
			Thread.sleep(200);
		}
	}

	@Test
	@Deployment(resources = {"subprocess/SDNCAdapterV1.bpmn",
			"subprocess/GenericNotificationService.bpmn"
			})
	public void badCorrelationIdTest() throws InterruptedException {

		mockSDNCAdapter(200);

		//System.out.println("SDNCAdapter bad RequestId test Started!");

		ProcessExecutionThread thread = new ProcessExecutionThread(sdncAdapterWorkflowRequest);
		thread.start();
		waitForExecutionToStart("sdncAdapter", 3);
		String pid = getPid();
		assertProcessInstanceNotFinished(pid);

		//System.out.println("Injecting SDNC Adapter asynchronous callback message to continue processing");
		String badRequestId = "This is not the RequestId that was used";
		CallbackHeader callbackHeader = new CallbackHeader();
		callbackHeader.setRequestId(badRequestId);
		callbackHeader.setResponseCode("200");
		callbackHeader.setResponseMessage("OK");
		SDNCAdapterCallbackRequest sdncAdapterCallbackRequest = new SDNCAdapterCallbackRequest();
		sdncAdapterCallbackRequest.setCallbackHeader(callbackHeader);
		sdncAdapterCallbackRequest.setRequestData(sdncAdapterCallbackRequestData);
		SDNCAdapterCallbackServiceImpl callbackService = new SDNCAdapterCallbackServiceImpl();
		callbackService.setProcessEngineServices4junit(processEngineRule);
		SDNCAdapterResponse sdncAdapterResponse = callbackService.sdncAdapterCallback(sdncAdapterCallbackRequest);
		//System.out.println("Back from executing process again");

		assertTrue(sdncAdapterResponse instanceof SDNCAdapterExceptionResponse);
		assertTrue(((SDNCAdapterExceptionResponse) sdncAdapterResponse).getException() instanceof IllegalStateException);
		assertProcessInstanceNotFinished(pid);

		//System.out.println("SDNCAdapter bad RequestId test Completed!");
	}

	@Test
	@Deployment(resources = {"subprocess/SDNCAdapterV1.bpmn",
			"subprocess/GenericNotificationService.bpmn"
			})
	public void badSynchronousResponse() throws IOException, InterruptedException {

		mockSDNCAdapter(404);

		//System.out.println("SDNCAdapter bad synchronous response flow Started!");

		ProcessExecutionThread thread = new ProcessExecutionThread(sdncAdapterWorkflowRequest);
		thread.start();
		while (thread.isAlive()) {
			Thread.sleep(200);
		}
		WorkflowResponse response = thread.workflowResponse;
		Assert.assertNotNull(response);
		Assert.assertEquals("404 error", response.getMessageCode(),7000);
//		assertProcessInstanceFinished(response.getProcessInstanceID());
		//System.out.println("SDNCAdapter bad synchronous response flow Completed!");
	}

	@Test
	@Deployment(resources = {"subprocess/SDNCAdapterV1.bpmn",
			"subprocess/GenericNotificationService.bpmn"
			})
	public void sdncNotFound() throws IOException, InterruptedException {
		mockSDNCAdapter(200);
		mockSDNCAdapter("/sdncAdapterMock/404", 400, "sdncCallbackErrorResponse.xml");

		ProcessExecutionThread thread = new ProcessExecutionThread(sdncAdapterWorkflowRequest);
		thread.start();
		waitForExecutionToStart("sdncAdapter", 3);
		String pid = getPid();

		//System.out.println("Injecting SDNC Adapter asynchronous callback message to continue processing");
		String generatedRequestId = (String) processEngineRule.getRuntimeService().getVariable(pid, "SDNCA_requestId");
		CallbackHeader callbackHeader = new CallbackHeader();
		callbackHeader.setRequestId(generatedRequestId);
		callbackHeader.setResponseCode("404");
		callbackHeader.setResponseMessage("Error processing request to SDNC. Not Found. https://sdncodl.it.us.aic.cip.com:8443/restconf/config/L3SDN-API:services/layer3-service-list/AS%2FVLXM%2F000199%2F%2FSB_INTERNET. SDNC Returned-[error-type:application, error-tag:data-missing, error-message:Request could not be completed because the relevant data model content does not exist.]");
		SDNCAdapterCallbackRequest sdncAdapterCallbackRequest = new SDNCAdapterCallbackRequest();
		sdncAdapterCallbackRequest.setCallbackHeader(callbackHeader);
		SDNCAdapterCallbackServiceImpl callbackService = new SDNCAdapterCallbackServiceImpl();
		callbackService.setProcessEngineServices4junit(processEngineRule);
		SDNCAdapterResponse sdncAdapterResponse = callbackService.sdncAdapterCallback(sdncAdapterCallbackRequest);
		//System.out.println("Back from executing process again");

		assertProcessInstanceFinished(pid);
		assertNotNull(sdncAdapterResponse);
		//TODO query history to see SDNCA_ResponseCode, SDNCA_ErrorResponse
		//System.out.println("SDNCAdapter SDNC Notfound test Completed!");
	}

	@Test
	@Deployment(resources = {"subprocess/SDNCAdapterV1.bpmn",
			"subprocess/GenericNotificationService.bpmn"
			})
	public void asynchronousMessageTimeout() throws IOException, InterruptedException {
		mockSDNCAdapter(200);
		//System.out.println("SDNCAdapter asynchronous message timeout flow Started!");
		ProcessExecutionThread thread = new ProcessExecutionThread(sdncAdapterWorkflowRequest);
		thread.start();
		waitForExecutionToStart("sdncAdapter", 3);
		checkForTimeout(getPid());
	}

	private void checkForTimeout(String pid) throws InterruptedException {

		assertProcessInstanceNotFinished(pid);

		ProcessEngineConfigurationImpl processEngineConfiguration =
			(ProcessEngineConfigurationImpl) processEngineRule.getProcessEngine().getProcessEngineConfiguration();
		assertTrue(processEngineConfiguration.getJobExecutor().isActive());

	    Job timerJob = processEngineRule.getManagementService().createJobQuery().processInstanceId(pid).singleResult();
	    assertNotNull(timerJob);

	    processEngineRule.getManagementService().executeJob(timerJob.getId());

	    assertProcessInstanceFinished(pid);

		//System.out.println("SDNCAdapter asynchronous message timeout flow Completed!");
	}

	class ProcessExecutionThread extends Thread {

		private String workflowRequest;
		private WorkflowResponse workflowResponse;

		public ProcessExecutionThread(String workflowRequest) {
			this.workflowRequest = workflowRequest;
		}

		public void run() {
			workflowResponse = invokeFlow(workflowRequest);
			workflowResponse.getProcessInstanceID();
		}
	}

	private String getPid() {
		return processEngineRule.getHistoryService().createHistoricProcessInstanceQuery().list().get(0).getId();
	}

	private Object getVariable(String pid, String variableName) {
		try {
			return
				processEngineRule
					.getHistoryService()
					.createHistoricVariableInstanceQuery()
					.processInstanceId(pid).variableName(variableName)
					.singleResult()
					.getValue();
		} catch(Exception ex) {
			return null;
		}
	}

	private void assertProcessInstanceFinished(String pid) {
	    assertEquals(1, processEngineRule.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pid).finished().count());
	}

	private void assertProcessInstanceNotFinished(String pid) {
	    assertEquals(0, processEngineRule.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(pid).finished().count());
	}

}