Skip to content

Instantly share code, notes, and snippets.

@tfennelly
Created June 7, 2011 18:00
Show Gist options
  • Save tfennelly/1012763 to your computer and use it in GitHub Desktop.
Save tfennelly/1012763 to your computer and use it in GitHub Desktop.
public class QuickstartInvoker {
private Map<String, Object> _headers = new HashMap<String, Object>();
private String _endpointURL;
private String _responseURL;
private long _timeout = 10000;
/**
* Create a Synchronous invoker.
* @param endpointURL The target endpoint (URL).
*/
public QuickstartInvoker(String endpointURL) {
this._endpointURL = endpointURL;
}
/**
* Create a Asynchronous invoker.
* @param endpointURL The target endpoint (URL).
* @param responseURL The asynchronous endpoint (URL).
*/
public QuickstartInvoker(String endpointURL, String responseURL) {
this._endpointURL = endpointURL;
this._responseURL = responseURL;
}
/**
* Create a Quickstart endpoint invoker instance to the specified Quickstart URL.
* @param endpointURL The quickstart endpoint URL.
* @return This object.
*/
public static QuickstartInvoker target(String endpointURL) {
return new QuickstartInvoker(endpointURL);
}
/**
* Set the asynchronous response endpoint URL.
* @param responseURL The asynchronous response endpoint URL.
* @return This object.
*/
public QuickstartInvoker asyncResponseFrom(String responseURL) {
this._responseURL = responseURL;
return this;
}
/**
* Set an invocation header.
* @param name The header name.
* @param value The header value.
* @return This object.
*/
public QuickstartInvoker setHeader(String name, Object value) {
_headers.put(name, value);
return this;
}
/**
* Set the response wait timeout.
* @param timeout Timeout in milliseconds.
*/
public void setTimeout(long timeout) {
this._timeout = timeout;
}
/**
* Send a payload to the target endpoint and wait for a response.
* <p/>
* The assumption is that there is always a response, received either synchronously from the
* target endpoint (e.g. HTTP) or asynchronously from the {@link #QuickstartInvoker(String, String) response endpoint}
* (e.g. a file protocol).
*
* @param payload The payload to send to the target endpoint.
* @param returnType The required return type.
* @param <T> Return type generic.
* @return The return value.
*/
public <T> T send(final Object payload, Class<T> returnType) {
CamelContext camelContext = new DefaultCamelContext();
try {
final MockEndpoint resultEndpoint = new MockEndpoint("mock:result");
resultEndpoint.setCamelContext(camelContext);
resultEndpoint.setMinimumExpectedMessageCount(1);
resultEndpoint.setMinimumResultWaitTime(_timeout);
camelContext.addEndpoint(resultEndpoint.getEndpointUri(), resultEndpoint);
camelContext.addRoutes(new RouteBuilder(camelContext) {
@Override
public void configure() throws Exception {
if (_responseURL == null) {
from ("direct:input")
.log("Sending: ${body}")
.to(_endpointURL)
.to(resultEndpoint.getEndpointUri());
} else {
from ("direct:input")
.log("Sending: ${body}")
.to(_endpointURL);
from (_responseURL)
.to(resultEndpoint.getEndpointUri());
}
}
});
camelContext.start();
try {
ProducerTemplate template = camelContext.createProducerTemplate();
Endpoint inputEndpoint = camelContext.getEndpoint("direct:input");
template.send(inputEndpoint, new Processor() {
public void process(Exchange exchange) {
if (payload instanceof Message) {
exchange.setIn((Message) payload);
} else {
Message in = exchange.getIn();
if (!_headers.isEmpty()) {
in.getHeaders().putAll(_headers);
}
in.setBody(payload);
}
}
});
resultEndpoint.await();
List<Exchange> exchanges = resultEndpoint.getExchanges();
Assert.assertNotNull(exchanges);
Assert.assertEquals(1, exchanges.size());
return exchanges.get(0).getIn().getBody(returnType);
} finally {
camelContext.stop();
}
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
return null;
}
}
}
@RunWith(Arquillian.class)
public class OrdersDemoQuickstartTest {
@Deployment(testable = false)
public static JavaArchive createDeployment() {
return ArquillianUtil.createDemoDeployment("switchyard-quickstart-demo-orders");
}
@Test
public void test() throws IOException, SAXException {
String response =
QuickstartInvoker.target("http4://localhost:18001/OrderService")
.setHeader(Exchange.CONTENT_TYPE, "text/xml")
.send(SOAP_REQUEST, String.class);
XMLUnit.setIgnoreWhitespace(true);
XMLUnit.compareXML(EXPECTED_SOAP_RESPONSE, response);
}
private static final String SOAP_REQUEST = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
" <soap:Body>\n" +
" <orders:submitOrder xmlns:orders=\"urn:switchyard-quickstart-demo:orders:1.0\">\n" +
" <order>\n" +
" <orderId>PO-19838-XYZ</orderId>\n" +
" <itemId>BUTTER</itemId>\n" +
" <quantity>200</quantity>\n" +
" </order>\n" +
" </orders:submitOrder>\n" +
" </soap:Body>\n" +
"</soap:Envelope>";
private static final String EXPECTED_SOAP_RESPONSE = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
" <SOAP-ENV:Body>\n" +
" <orders:submitOrderResponse xmlns:orders=\"urn:switchyard-quickstart-demo:orders:1.0\">\n" +
" <orderAck>\n" +
" <orderId>PO-19838-XYZ</orderId>\n" +
" <accepted>true</accepted>\n" +
" <status>Order Accepted</status>\n" +
" </orderAck>\n" +
" </orders:submitOrderResponse>\n" +
" </SOAP-ENV:Body>\n" +
"</SOAP-ENV:Envelope>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment