在camel路由中从模拟的Spring-WS服务发送SOAP错误

时间:2015-11-05 14:43:27

标签: java unit-testing soap apache-camel spring-ws

在系统中有一个将一些输入传递给外部Web服务的驼峰路由。

@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://external-service.com")
        .end();
    }
}

要求是根据外部服务调用的结果执行某些操作,特别是取决于SOAP错误消息(特别是关于自定义soap故障详细信息)。

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

因此,我想实现一些测试用例来模仿外部服务的错误回复。我的方法是模拟对实际服务的调用,并用模拟响应替换它:

@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://external-service.com")
  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();
  } 
}

我知道我可以在我的自定义处理器中抛出一些异常,但这不是完全相同的行为,特别是没有肥皂故障细节对于进一步评估很重要。

任何建议我如何将模拟的SOAP错误发送到我的模拟服务的响应,还是有另一种方法可以去?

0 个答案:

没有答案