jeudi 5 novembre 2015

Sending a SOAP fault from a mocked Spring-WS service in a camel route

In the system there is a camel route passing some input to a external web service.

@Component
public class MyRouteBuilder extends RouteBuilder {


    public void configure() {
        errorHandler(deadLetterChannel("direct:error").disableRedelivery());
        // further routes for business logic omitted here

        from("direct:frontendService")
        .transform()
        .simple("<urn:myPayload>...</urn:myPayload>")
        .to("spring-ws:http://ift.tt/1PpCvu5")
        .end();
    }
}

The requirement is to perform certain operations depending on the result of the external service call, in particular depending on the SOAP fault message (in particular on the custom soap fault detail).

<mydetail>
   <code>ERR-123</code>
   <app-msg>Something wired happend</app-msg>
</mydetail>

Therefore I want to implement some test cases to mimic the fault replies of the external service. My approach is to mock out the call to the actual service and replacing it by a mock response:

@RunWith(CamelSpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = CamelSpringDelegatingTestContextLoader.class, classes = { MyRouteBuilderTest.TestConfig.class,
MyRouteBuilder.class })
@MockEndpointsAndSkip(value = "spring-ws:*")
public class MyRouteBuilderTest {

  @EndpointInject(uri = "mock:error")
  protected MockEndpoint errorEndpoint;

  @EndpointInject(uri = "mock:spring-ws:http://ift.tt/1PpCvu5")
  protected MockEndpoint service;

  @Produce(uri = "direct:frontendService")
  protected ProducerTemplate frontendServiceProducer;

  @Configuration
  @PropertySource("classpath:application.properties")
  public static class TestConfig extends SingleRouteCamelConfiguration {
    @Bean
    @Override
    public RouteBuilder route() {
      return new MyRouteBuilder() {
        public void configure() throws Exception {
          super.configure();
          from("direct:error").to("mock:error");
        };

      };
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer configurer() {
      return new PropertySourcesPlaceholderConfigurer();
    }
  }

  @Test
  public void test() throws InterruptedException {

    errorEndpoint.expectedMessageCount(1);

    service.whenAnyExchangeReceived(new Processor() {

      @Override
      public void process(Exchange arg0) throws Exception {
        // How do I send some fault message here in a way that the
        // route behaves like if it was thrown from the real service
      }
    });

    Object o = frontendServiceProducer.requestBody("<some>payload</some>");


    service.expectedMessageCount(0);

    service.assertIsSatisfied();
    errorEndpoint.assertIsSatisfied();
  } 
}

I know that I could throw some exception inside my custom processor, but this is not the exact same behaviour, especially there is no soap fault detail which is important for further evaluation.

Any suggestions how I can send mocked SOAP faults to as response from my mocked service or is there another way to go?

Thanks.

Aucun commentaire:

Enregistrer un commentaire