abad6c373def36d90b1dc4a5efbb863de0983b52
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / openecomp / mso / bpmn / common / WorkflowAsyncResourceTest.java
1 /*- 
2  * ============LICENSE_START======================================================= 
3  * OPENECOMP - MSO 
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.openecomp.mso.bpmn.common;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.Mockito.mock;
25
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.UUID;
29
30 import org.camunda.bpm.engine.test.Deployment;
31 import org.camunda.bpm.engine.variable.impl.VariableMapImpl;
32 import org.jboss.resteasy.spi.AsynchronousResponse;
33 import org.junit.Test;
34 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowAsyncResource;
35 import org.openecomp.mso.bpmn.common.workflow.service.WorkflowResponse;
36
37 public class WorkflowAsyncResourceTest extends WorkflowTest {
38
39         @Test
40         @Deployment(resources = { "testAsyncResource.bpmn" })
41         public void asyncRequestSuccess() throws InterruptedException {
42                 //it can be any request which asynchronously processed by the workflow
43                 String request = "<aetgt:CreateTenantRequest xmlns:aetgt=\"http://org.openecomp/mso/workflow/schema/v1\" xmlns:sdncadapterworkflow=\"http://org.openecomp/mso/workflow/schema/v1\" xmlns:ns5=\"http://org.openecomp/mso/request/types/v1\">  <msoservtypes:service-information xmlns:msoservtypes=\"http://org.openecomp/mso/request/types/v1\">    <msoservtypes:service-type>SDN-ETHERNET-INTERNET</msoservtypes:service-type>    <msoservtypes:service-instance-id>HI/VLXM/950604//SW_INTERNET</msoservtypes:service-instance-id>    <msoservtypes:subscriber-name>SubName01</msoservtypes:subscriber-name> </msoservtypes:service-information> </aetgt:CreateTenantRequest>";
44
45                 Map<String,String> variables = new HashMap<String,String>();
46                 variables.put("testAsyncRequestMsg", request);
47                 variables.put("mso-request-id", UUID.randomUUID().toString());
48                 variables.put("mso-service-request-timeout", "5");
49                 
50                 WorkflowResponse workflowResponse = BPMNUtil.executeAsyncWorkflow(processEngineRule, "testAsyncProcess", variables);
51                 assertEquals("Received the request, the process is getting executed, request message<aetgt:CreateTenantRequest xmlns:aetgt=\"http://org.openecomp/mso/workflow/schema/v1\" xmlns:sdncadapterworkflow=\"http://org.openecomp/mso/workflow/schema/v1\" xmlns:ns5=\"http://org.openecomp/mso/request/types/v1\">  <msoservtypes:service-information xmlns:msoservtypes=\"http://org.openecomp/mso/request/types/v1\">    <msoservtypes:service-type>SDN-ETHERNET-INTERNET</msoservtypes:service-type>    <msoservtypes:service-instance-id>HI/VLXM/950604//SW_INTERNET</msoservtypes:service-instance-id>    <msoservtypes:subscriber-name>SubName01</msoservtypes:subscriber-name> </msoservtypes:service-information> </aetgt:CreateTenantRequest>", workflowResponse.getResponse());
52                 assertEquals(200, workflowResponse.getMessageCode());
53         }
54
55         private void executeWorkflow(String request, String requestId, AsynchronousResponse asyncResponse, String processKey) {
56                 WorkflowAsyncResource workflowResource = new WorkflowAsyncResource();
57                 VariableMapImpl variableMap = new VariableMapImpl();
58
59                 Map<String, Object> variableValueType = new HashMap<String, Object>();
60
61                 Map<String, Object> requestMsg = new HashMap<String, Object>();
62                 requestMsg.put("value", request);
63                 requestMsg.put("type", "String");
64
65                 Map<String, Object> msorequestId = new HashMap<String, Object>();
66                 msorequestId.put("type", "String");
67                 msorequestId.put("value",requestId);
68
69                 Map<String, Object> timeout = new HashMap<String, Object>();
70                 timeout.put("type", "String");
71                 timeout.put("value","5"); 
72
73                 variableValueType.put("testAsyncRequestMsg", requestMsg);
74                 variableValueType.put("mso-request-id", msorequestId);
75                 variableValueType.put("mso-service-request-timeout", timeout);
76
77                 variableMap.put("variables", variableValueType);
78                 
79                 workflowResource.setProcessEngineServices4junit(processEngineRule);
80                 workflowResource.startProcessInstanceByKey(asyncResponse, processKey, variableMap);
81         }
82
83         class ProcessThread extends Thread {
84                 
85                 public WorkflowResponse workflowResponse;
86                 public String requestId;
87                 public String processKey;
88                 public AsynchronousResponse asyncResponse = mock(AsynchronousResponse.class);
89                 public String request;
90                 public boolean started;
91                 public void run() {
92                         started = true;
93                         executeWorkflow(request, requestId, asyncResponse, processKey);
94                 }
95         }       
96 }