Skip to content

Instantly share code, notes, and snippets.

@danbev
Created July 21, 2011 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danbev/1097034 to your computer and use it in GitHub Desktop.
Save danbev/1097034 to your computer and use it in GitHub Desktop.
CamelTestSupport example
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)));
}
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