forked from camunda/camunda-bpm-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BankCustomerProcessTest.java
312 lines (249 loc) · 10.8 KB
/
BankCustomerProcessTest.java
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
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. Camunda licenses this file to you under the Apache License,
* Version 2.0; 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.
*/
package com.camunda.bpm.example.spring.soap;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;
import javax.xml.namespace.QName;
import javax.xml.transform.dom.DOMResult;
import javax.xml.ws.Endpoint;
import org.apache.cxf.binding.soap.SoapFault;
import org.apache.cxf.jaxws.EndpointImpl;
import org.camunda.bpm.engine.HistoryService;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.history.HistoricVariableInstance;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.variable.value.ObjectValue;
import org.camunda.bpm.engine.variable.value.StringValue;
import org.camunda.bpm.engine.variable.value.TypedValue;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.w3c.dom.Element;
import com.camunda.bpm.example.spring.soap.v1.BankCustomerServicePortType;
import com.camunda.bpm.example.spring.soap.v1.BankException;
import com.camunda.bpm.example.spring.soap.v1.BankException_Exception;
import com.camunda.bpm.example.spring.soap.v1.BankRequestHeader;
import com.camunda.bpm.example.spring.soap.v1.GetAccountsRequest;
import com.camunda.bpm.example.spring.soap.v1.GetAccountsResponse;
/**
* This class tests three async processes.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/spring/engine.xml", "classpath:/spring/beans.xml" })
@ActiveProfiles("webservice")
public class BankCustomerProcessTest {
@Autowired
protected RuntimeService runtimeService;
@Autowired
protected TaskService taskService;
@Autowired
protected HistoryService historyService;
@Autowired
protected ProcessEngine processEngine;
@Mock
protected BankCustomerServicePortType serviceMock;
@Value("${bankcustomer.service}")
protected String bankCustomerServiceAddress;
protected EndpointImpl endpoint;
@Before
public void initMocks() throws Exception {
MockitoAnnotations.initMocks(this);
// wrap the evaluator mock in proxy
BankCustomerServicePortType serviceInterface = ServiceProxy.newInstance(serviceMock);
// publish the mock
endpoint = (EndpointImpl) Endpoint.create(serviceInterface);
// we have to use the following CXF dependent code, to specify qname, so that it resource locator finds it
QName serviceName = new QName("http://soap.spring.example.bpm.camunda.com/v1", "BankCustomerServicePortType");
endpoint.setServiceName(serviceName);
endpoint.publish(bankCustomerServiceAddress);
}
@After
public void closeMocks() {
reset(serviceMock);
if (endpoint != null) {
endpoint.stop();
}
}
@Test
public void process_inputJaxbObjectInProcess_javaObjectPassedToService() throws Exception {
// add mock response
GetAccountsResponse mockResponse = new GetAccountsResponse();
List<String> accountList = mockResponse.getAccount();
accountList.add("1234");
accountList.add("5678");
when(serviceMock.getAccounts(any(GetAccountsRequest.class), any(BankRequestHeader.class))).thenReturn(mockResponse);
String customerNumber = "123456789";
String secret = "abc";
// invoke process (async behaviour)
Map<String, Object> variables = new HashMap<>();
variables.put("customerNumber", customerNumber);
variables.put("secret", secret);
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("process", variables);
// wait until all jobs complete
assertTrue(waitUntilNoActiveJobs(processEngine, 1000));
// validate that mock called
verifyRequest(customerNumber, secret);
// verify that all serialization types are application/xml (kind of important)
verifySerializationFormat(processInstance, "application/xml");
// verify that we got our response
GetAccountsResponse response = (GetAccountsResponse) historyService.createHistoricVariableInstanceQuery()
.processInstanceId(processInstance.getProcessInstanceId())
.variableName("accounts")
.singleResult()
.getValue();
assertThat(response.getAccount().size(), is(2));
}
@Test
public void call_serviceWithSoapFault_soapFaultException() throws Exception {
// add mock response
GetAccountsResponse mockResponse = new GetAccountsResponse();
List<String> accountList = mockResponse.getAccount();
accountList.add("1234");
accountList.add("5678");
String errorCode = "myErrorCode";
String errorMessage = "myErrorMessage";
SoapFault fault = createFault(errorCode, errorMessage);
when(serviceMock.getAccounts(any(GetAccountsRequest.class), any(BankRequestHeader.class))).thenThrow(fault);
String customerNumber = "123456789";
String secret = "abc";
// invoke process (async behaviour)
Map<String, Object> variables = new HashMap<>();
variables.put("customerNumber", customerNumber);
variables.put("secret", secret);
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("process", variables);
// wait until all jobs complete
assertTrue(waitUntilNoActiveJobs(processEngine, 1000));
// validate that mock called
verifyRequest(customerNumber, secret);
Assert.assertNull(historyService.createHistoricVariableInstanceQuery()
.processInstanceId(processInstance.getProcessInstanceId())
.variableName("accounts")
.singleResult());
}
/**
* construct soap fault
*/
protected SoapFault createFault(String errorCode, String errorMessage) throws JAXBException, PropertyException {
BankException exception = new BankException();
exception.setCode(errorCode);
exception.setMessage(errorMessage);
QName qName = SoapFault.FAULT_CODE_SERVER;
SoapFault fault = new SoapFault("message", qName);
Element detail = fault.getOrCreateDetail();
JAXBContext context = JAXBContext.newInstance(BankException.class);
DOMResult result = new DOMResult();
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.marshal(
new JAXBElement<BankException>(new QName("http://soap.spring.example.bpm.camunda.com/v1", "bankException"),
BankException.class, exception), result);
detail.appendChild(detail.getOwnerDocument().importNode(result.getNode().getFirstChild(), true));
return fault;
}
protected void verifySerializationFormat(ProcessInstance processInstance, String format) {
List<HistoricVariableInstance> list = historyService.createHistoricVariableInstanceQuery()
.processInstanceId(processInstance.getProcessInstanceId())
.list();
for (HistoricVariableInstance item : list) {
if (item.getName().equals("request") || item.getName().equals("accounts")) {
TypedValue typedValue = item.getTypedValue();
if (typedValue instanceof ObjectValue) {
ObjectValue objectValue = (ObjectValue) typedValue;
assertThat(item.getName(), objectValue.getSerializationDataFormat(), is(format));
} else if (!(typedValue instanceof StringValue)) {
Assert.fail();
}
}
}
}
protected void verifyRequest(String customerNumber, String secret) throws Exception {
ArgumentCaptor<GetAccountsRequest> argument1 = ArgumentCaptor.forClass(GetAccountsRequest.class);
ArgumentCaptor<BankRequestHeader> argument2 = ArgumentCaptor.forClass(BankRequestHeader.class);
verify(serviceMock, times(1)).getAccounts(argument1.capture(), argument2.capture());
GetAccountsRequest request = argument1.getValue();
assertThat(request.getCustomerNumber(), is(customerNumber));
BankRequestHeader header = argument2.getValue();
assertThat(header.getSecret(), is(secret));
}
public boolean waitUntilNoActiveJobs(ProcessEngine processEngine, long wait) throws InterruptedException {
long timeout = System.currentTimeMillis() + wait;
while (System.currentTimeMillis() < timeout) {
long jobs = processEngine.getManagementService().createJobQuery().active().count();
if (jobs == 0) {
return true;
}
System.out.println("Waiting for " + jobs + " jobs");
Thread.sleep(50);
}
return false;
}
/**
* Utility class to wrap the webservice implementation in a mock.
*/
public static class ServiceProxy implements InvocationHandler {
protected Object obj;
public static <T> T newInstance(T obj) {
ServiceProxy proxy = new ServiceProxy(obj);
Class<?> clazz = obj.getClass();
T res = (T) Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), proxy);
return res;
}
ServiceProxy(Object obj) {
this.obj = obj;
}
@Override
public Object invoke(Object proxy, Method m, Object[] args) throws Exception {
try {
return m.invoke(obj, args);
} catch (Exception e) {
if (e.getCause() instanceof org.apache.cxf.binding.soap.SoapFault) {
throw (Exception) e.getCause();
}
throw new RuntimeException(e.getMessage(), e);
}
}
}
}