Skip to content

Instantly share code, notes, and snippets.

@tfennelly
Created July 21, 2011 13:08
Show Gist options
  • Save tfennelly/1097144 to your computer and use it in GitHub Desktop.
Save tfennelly/1097144 to your computer and use it in GitHub Desktop.
Camel testing
public class SwitchyardComponentTest extends SwitchYardTestCase {
private String _serviceName = "testServiceName";
private CamelContext _camelContext;
private ProducerTemplate _template;
@Before
public void setup() throws Exception {
_camelContext = new DefaultCamelContext();
_camelContext.addRoutes(createRouteBuilder());
_template = _camelContext.createProducerTemplate();
_camelContext.start();
ServiceReferences.clear();
}
@Test
public void sendToSwitchyardInOut() throws Exception {
final String expectedResponse = "replacedContent";
final String payload = "bajja";
final InOnlyService inService = new InOnlyService("testServiceName");
final ServiceReference serviceReference = getServiceDomain().registerService(new QName(_serviceName), new ResponseService(expectedResponse), inService);
ServiceReferences.add(serviceReference.getName(), serviceReference);
final String response = (String) _template.requestBody("direct:input", payload);
assertThat(response, is(equalTo(expectedResponse)));
}
@Test
public void sendToSwitchyardInOnly() throws Exception {
final String payload = "bajja";
final MockHandler mockService = new MockHandler();
final InOnlyService inService = new InOnlyService("testServiceName");
final ServiceReference serviceReference = getServiceDomain().registerService(new QName(_serviceName), mockService, inService);
ServiceReferences.add(serviceReference.getName(), serviceReference);
_template.sendBody("direct:input", payload);
assertThat(mockService.getMessages().size(), is(1));
final String actualPayload = mockService.getMessages().iterator().next().getMessage().getContent(String.class);
assertThat(actualPayload, is(equalTo(payload)));
}
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder()
{
public void configure() throws Exception {
from("direct:input")
.to("switchyard://" + _serviceName.toString() + "?operationName=" + _serviceName.toString())
.to("mock:result");
}
};
}
private static class ResponseService extends BaseHandler {
private final String _response;
public ResponseService(final String response) {
this._response = response;
}
@Override
public void handleMessage(final Exchange exchange) throws HandlerException {
final Message responseMessage = exchange.createMessage().setContent(_response);
exchange.send(responseMessage);
}
}
}
@RunWith(SwitchYardRunner.class)
public class SwitchyardComponentTest extends CamelTestSupport {
private String _serviceName = "testServiceName";
@Before
public void setup() throws Exception {
ServiceReferences.clear();
}
@Test
public void sendToSwitchyardInOut() throws Exception {
final String expectedResponse = "replacedContent";
final String payload = "bajja";
final InOnlyService inService = new InOnlyService("testServiceName");
final ServiceReference serviceReference = getServiceDomain().registerService(new QName(_serviceName), new ResponseService(expectedResponse), inService);
ServiceReferences.add(serviceReference.getName(), serviceReference);
final String response = (String) _template.requestBody("direct:input", payload);
assertThat(response, is(equalTo(expectedResponse)));
}
@Test
public void sendToSwitchyardInOnly() throws Exception {
final String payload = "bajja";
final MockHandler mockService = new MockHandler();
final InOnlyService inService = new InOnlyService("testServiceName");
final ServiceReference serviceReference = getServiceDomain().registerService(new QName(_serviceName), mockService, inService);
ServiceReferences.add(serviceReference.getName(), serviceReference);
template.sendBody("direct:input", payload);
assertThat(mockService.getMessages().size(), is(1));
final String actualPayload = mockService.getMessages().iterator().next().getMessage().getContent(String.class);
assertThat(actualPayload, is(equalTo(payload)));
}
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder()
{
public void configure() throws Exception {
from("direct:input")
.to("switchyard://" + _serviceName.toString() + "?operationName=" + _serviceName.toString())
.to("mock:result");
}
};
}
private static class ResponseService extends BaseHandler {
private final String _response;
public ResponseService(final String response) {
this._response = response;
}
@Override
public void handleMessage(final Exchange exchange) throws HandlerException {
final Message responseMessage = exchange.createMessage().setContent(_response);
exchange.send(responseMessage);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment