Skip to content

Instantly share code, notes, and snippets.

@tfennelly
Created July 22, 2011 11:20
Show Gist options
  • Save tfennelly/1099267 to your computer and use it in GitHub Desktop.
Save tfennelly/1099267 to your computer and use it in GitHub Desktop.
OutboundHandlerTest
@RunWith(SwitchYardRunner.class)
public class OutboundHandlerTest extends CamelTestSupport {
@EndpointInject(uri = "mock:result")
private static MockEndpoint camelEndpoint;
private ServiceDomain _serviceDomain;
@ServiceOperation("TargetService")
private Invoker _targetService;
private ServiceReference _service;
/**
* Exception rule for verifying exceptions.
*/
@Rule
public ExpectedException exception = ExpectedException.none();
@Before
public void setupSwitchyardService() {
final CamelBindingModel bindingModel = mock(CamelBindingModel.class);
when(bindingModel.getComponentURI()).thenReturn(URI.create("direct:to"));
_serviceDomain.registerService(_targetService.getServiceName(), new OutboundHandler(bindingModel.getComponentURI().toString(), camelContext));
_service = _serviceDomain.getService(_targetService.getServiceName());
}
@Test
public void routeInOnlyToCamel() throws Exception {
final String payload = "inOnly test string";
_targetService.contract(new BaseExchangeContract(new InOnlyOperation(_targetService.getServiceName().toString())));
_targetService.sendInOnly(payload);
assertThat(camelEndpoint.getReceivedCounter(), is(1));
final String received = (String) camelEndpoint.getReceivedExchanges().get(0).getIn().getBody();
assertThat(received, is(equalTo(payload)));
}
@Test
public void verifyThatEsbPropetiesArePassedToCamel() throws Exception {
final String propertyKey = "testProp";
final String propertyValue = "dummyValue";
final ExchangeContract contract = new BaseExchangeContract(new InOnlyOperation(_targetService.getServiceName().toString()));
final Exchange exchange = _serviceDomain.createExchange(_service, contract);
exchange.getContext().setProperty(propertyKey, propertyValue);
exchange.send(exchange.createMessage());
assertThat(camelEndpoint.getReceivedCounter(), is(1));
final String actualPropertyValue = (String) camelEndpoint.getReceivedExchanges().get(0).getProperty(propertyKey);
assertThat(actualPropertyValue, is(propertyValue));
}
private Message createMessageWithBody(final Exchange exchange, final Object body) {
final Message message = exchange.createMessage();
message.setContent(body);
return message;
}
@Test
public void routeInOutToCamel() throws Exception {
final String body = "inOut test string";
final ExchangeContract contract = new BaseExchangeContract(new InOnlyOperation(_targetService.getServiceName().toString()));
final Exchange exchange = _serviceDomain.createExchange(_service, contract);
final Message message = createMessageWithBody(exchange, body);
exchange.send(message);
final String payload = (String) exchange.getMessage().getContent();
assertThat(payload, is(equalTo(body)));
}
@Test
public void throwsIllegalArgumentExceptionIfBindingModelIsNull() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("uri argument must not be null");
new OutboundHandler(null, mock(CamelContext.class));
}
@Test
public void throwsIllegalArgumentExceptionIfCamelContextIsNull() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("camelContext argument must not be null");
new OutboundHandler("mockuri", null);
}
protected static RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder()
{
public void configure() {
from("direct:to")
.convertBodyTo(String.class)
.log("Before Routing to mock:result body: ${body}")
.to("mock:result");
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment