Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View HelpDeskDemoQuickstartTest.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 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
/*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
package org.switchyard.test.helpdeskdemo;
 
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.SocketException;
import java.util.List;
import java.util.Locale;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.drools.agent.impl.DoNothingSystemEventListener;
import org.jboss.arquillian.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jbpm.task.Status;
import org.jbpm.task.User;
import org.jbpm.task.query.TaskSummary;
import org.jbpm.task.service.TaskClient;
import org.jbpm.task.service.TaskServer;
import org.jbpm.task.service.TaskService;
import org.jbpm.task.service.TaskServiceSession;
import org.jbpm.task.service.mina.MinaTaskClientConnector;
import org.jbpm.task.service.mina.MinaTaskClientHandler;
import org.jbpm.task.service.mina.MinaTaskServer;
import org.jbpm.task.service.responsehandlers.BlockingTaskOperationResponseHandler;
import org.jbpm.task.service.responsehandlers.BlockingTaskSummaryResponseHandler;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.switchyard.test.ArquillianUtil;
 
/**
*
* @author <a href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
*/
@RunWith(Arquillian.class)
public class HelpDeskDemoQuickstartTest {
 
@Deployment(testable = false)
public static JavaArchive createDeployment() {
return ArquillianUtil.createDemoDeployment("switchyard-quickstart-demo-helpdesk");
}
 
@Test
public void test() throws Exception {
HumanTaskServerHelper ht = new HumanTaskServerHelper();
if (ht.startTaskServer("Developer", "User")) {
try {
HttpHelper http = new HttpHelper();
http.postStringAndTestXML("http://localhost:18001/HelpDeskService", SOAP_REQUEST, EXPECTED_SOAP_RESPONSE);
boolean keepWorking = true;
while (keepWorking) {
keepWorking = ht.completeTasksForUsers("Developer", "User");
}
} finally {
ht.stopTaskServer();
}
}
}
private static final String SOAP_REQUEST = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:helpdesk=\"urn:switchyard-quickstart-demo:helpdesk:1.0\">\n"
+ " <soap:Body>\n"
+ " <helpdesk:openTicket>\n"
+ " <ticket>\n"
+ " <id>TCKT-17761852</id>\n"
+ " </ticket>\n"
+ " </helpdesk:openTicket>\n"
+ " </soap:Body>\n"
+ "</soap:Envelope>\n";
private static final String EXPECTED_SOAP_RESPONSE = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:bpm=\"urn:switchyard-component-bpm:process:1.0\" xmlns:helpdesk=\"urn:switchyard-quickstart-demo:helpdesk:1.0\">\n"
+ " <SOAP-ENV:Header>\n"
+ " <bpm:processInstanceId>1</bpm:processInstanceId>\n"
+ " </SOAP-ENV:Header>\n"
+ " <SOAP-ENV:Body>\n"
+ " <helpdesk:openTicketResponse>\n"
+ " <ticketAck>\n"
+ " <id>TCKT-17761852</id>\n"
+ " <received>true</received>\n"
+ " </ticketAck>\n"
+ " </helpdesk:openTicketResponse>\n"
+ " </SOAP-ENV:Body>\n"
+ "</SOAP-ENV:Envelope>";
 
class HttpHelper {
 
private HttpClient _httpClient;
private String _contentType = "text/xml";
 
/**
* Set the content type.
* <p/>
* Default content type is "text/xml".
*
* @param contentType The content type.
* @return This HTTPMixIn instance.
*/
public HttpHelper() {
initialize();
}
 
public HttpHelper setContentType(String contentType) {
this._contentType = contentType;
return this;
}
 
public void initialize() {
_httpClient = new HttpClient();
}
 
/**
* POST the specified request payload to the specified HTTP endpoint.
* @param endpointURL The HTTP endpoint URL.
* @param request The request payload.
* @return The HTTP response payload.
*/
public String postString(String endpointURL, String request) {
PostMethod postMethod = new PostMethod(endpointURL);
try {
postMethod.setRequestEntity(new StringRequestEntity(request, _contentType, "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
return execute(postMethod);
} finally {
postMethod.releaseConnection();
}
}
 
/**
* POST the specified String request to the specified HTTP endpoint and perform an XML compare
* between the HTTP response and the specified expected response String.
* @param endpointURL The HTTP endpoint URL.
* @param request The classpath resource to be posted to the endpoint.
* @param expectedResponse The String to use to perform the XML test on the response.
* @return The HTTP response payload.
*/
public String postStringAndTestXML(String endpointURL, String request, String expectedResponse) {
String response = postString(endpointURL, request);
System.err.println(response);
return response;
}
 
/**
* POST the specified classpath resource to the specified HTTP endpoint.
* @param endpointURL The HTTP endpoint URL.
* @param requestResource The classpath resource to be posted to the endpoint.
* @return The HTTP response payload.
*/
public String postResource(String endpointURL, String requestResource) {
PostMethod postMethod = new PostMethod(endpointURL);
InputStream requestStream = this.getClass().getResourceAsStream(requestResource);
 
try {
postMethod.setRequestEntity(new InputStreamRequestEntity(requestStream, _contentType + "; charset=utf-8"));
return execute(postMethod);
} finally {
try {
requestStream.close();
} catch (IOException e) {
Assert.fail("Unexpected exception closing HTTP request resource stream.");
} finally {
postMethod.releaseConnection();
}
}
}
 
/**
* POST the specified classpath resource to the specified HTTP endpoint and perform an XML compare
* between the HTTP response and the specified expected classpath response resource.
* @param endpointURL The HTTP endpoint URL.
* @param requestResource The classpath resource to be posted to the endpoint.
* @param expectedResponseResource The classpath resource to use to perform the XML test on the response.
* @return The HTTP response payload.
*/
public String postResourceAndTestXML(String endpointURL, String requestResource, String expectedResponseResource) {
String response = postResource(endpointURL, requestResource);
System.err.println(response);
return response;
}
 
/**
* Execute the supplied HTTP Method.
* <p/>
* Does not release the {@link org.apache.commons.httpclient.HttpMethod#releaseConnection() HttpMethod connection}.
*
* @param method The HTTP Method.
* @return The HTTP Response.
*/
public String execute(HttpMethod method) {
try {
_httpClient.executeMethod(method);
return method.getResponseBodyAsString();
} catch (Exception e) {
e.printStackTrace();
}
 
return null;
}
}
 
/**
* Human Task Server Helper. Based off the BPMMixIn from the jbpm-component.
* Helps with <a href="http://docs.jboss.org/jbpm/v5.0/userguide/ch.Human_Tasks.html">jBPM 5 Human Tasks</a>.
*
* @author Justin Wyer &lt;<a href="mailto:justin@lifeasageek.com">justin@lifeasageek.com</a>&gt; (C) 2011 Red Hat Inc.
*/
class HumanTaskServerHelper {
 
/** The default host. */
public static final String DEFAULT_HOST = "127.0.0.1";
/** The default port. */
public static final int DEFAULT_PORT = 9123;
/** The Administrator user id. */
public static final String ADMINISTRATOR = "Administrator";
private TaskServer _server = null;
private TaskClient _client = null;
private boolean _clientConnected = false;
 
/**
* Starts the task server with the default host and port, and the specified user ids to pre-populate the database with.
* @param userIds the user ids to pre-populate the database with
* @return whether the task server started successfully
*/
public boolean startTaskServer(String... userIds) {
return startTaskServer(DEFAULT_HOST, DEFAULT_PORT, userIds);
}
 
/**
* Starts the task server with the specified host and port, and the specified user ids to pre-populate the database with.
* @param host the task server host
* @param port the task server port
* @param userIds the user ids to pre-populate the database with
* @return whether the task server started successfully
*/
public boolean startTaskServer(String host, int port, String... userIds) {
try {
boolean ready = false;
while (!ready) {
Socket socket = null;
try {
socket = new Socket(host, port);
if (socket.isConnected()) {
Thread.sleep(1000);
} else {
ready = true;
}
} catch (SocketException se) {
ready = true;
} finally {
if (socket != null) {
socket.close();
socket = null;
}
}
}
EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.jbpm.task");
TaskService taskService = new TaskService(emf, new DoNothingSystemEventListener());
TaskServiceSession taskServiceSession = taskService.createSession();
taskServiceSession.addUser(new User(ADMINISTRATOR));
for (String userId : userIds) {
if (!ADMINISTRATOR.equals(userId)) {
taskServiceSession.addUser(new User(userId));
}
}
taskServiceSession.dispose();
_server = new MinaTaskServer(taskService, port, host);
new Thread(_server).start();
//_server.start();
return true;
} catch (Throwable t) {
System.err.println("*** Could not start task server: " + t.getMessage() + " ***");
return false;
}
}
 
/**
* Stops the task server.
*/
public void stopTaskServer() {
if (_server != null) {
try {
_server.stop();
} catch (Throwable t) {
// just to keep checkstyle happy ("Must have at least one statement.")
t.getMessage();
}
_server = null;
}
}
 
/**
* Starts the task client with the default host and port of the task server to connect to.
* @throws Exception if a problem occurred
*/
public void startTaskClient() throws Exception {
startTaskClient(DEFAULT_HOST, DEFAULT_PORT);
}
 
/**
* Starts the task client with the specified host and port of the task server to connect to.
* @param host the task server host
* @param port the task server port
* @throws Exception if a problem occurred
*/
public void startTaskClient(String host, int port) throws Exception {
if (_client == null) {
_client = new TaskClient(new MinaTaskClientConnector(HumanTaskServerHelper.class.getSimpleName(), new MinaTaskClientHandler(new DoNothingSystemEventListener())));
}
if (!_clientConnected) {
_client.connect(host, port);
_clientConnected = true;
}
}
 
/**
* Stops the task client.
*/
public void stopTaskClient() {
if (_client != null) {
if (_clientConnected) {
try {
_client.disconnect();
} catch (Throwable t) {
// just to keep checkstyle happy ("Must have at least one statement.")
t.getMessage();
}
_clientConnected = false;
}
_client = null;
}
}
 
/**
* Completes all tasks for the specified user ids, auto-connecting to the task server with the default host and port.
* @param userIds the user ids to complete the tasks for
* @return if there might be more user tasks to complete
* @throws Exception if a problem occurred
*/
public boolean completeTasksForUsers(String... userIds) throws Exception {
return completeTasksForUsers(DEFAULT_HOST, DEFAULT_PORT, userIds);
}
 
/**
* Completes all tasks for the specified user ids, connecting to the task server with the specified host and port.
* @param host the task server host
* @param port the task server port
* @param userIds the user ids to complete the tasks for
* @return if there might be more user tasks to complete
* @throws Exception if a problem occurred
*/
public boolean completeTasksForUsers(String host, int port, String... userIds) throws Exception {
boolean keepWorking = false;
startTaskClient(host, port);
for (String userId : userIds) {
BlockingTaskSummaryResponseHandler btsrh = new BlockingTaskSummaryResponseHandler();
_client.getTasksOwned(userId, Locale.getDefault().toString().replaceAll("_", "-"), btsrh);
List<TaskSummary> tasks = btsrh.getResults();
for (TaskSummary task : tasks) {
if (Status.Completed.equals(task.getStatus())) {
continue;
}
User user = task.getActualOwner();
BlockingTaskOperationResponseHandler btorh = new BlockingTaskOperationResponseHandler();
_client.start(task.getId(), user.getId(), btorh);
btorh.waitTillDone(10000);
btorh = new BlockingTaskOperationResponseHandler();
_client.complete(task.getId(), user.getId(), null, btorh);
btorh.waitTillDone(10000);
keepWorking = true;
}
}
stopTaskClient();
Thread.sleep(1000);
return keepWorking;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.