3

I have a camel route which polls the files from a ftp server and send files to s3. I have some processors in the route which calculates/manipulates the headers based on file name. I need to test this route, How can i inject my processor and use the file language inside my processor?

@RunWith(MockitoJUnitRunner.class)
public class CamelS3HeadersProcessorTest extends CamelTestSupport {
  private String filePath = "src/test/resources/sample.txt";

 // @Autowired
//  private CamelS3HeadersProcessor camelS3HeadersProcessor;

  @Test
  public void shouldSetS3HeadersProperly() throws Exception {
    File file = new File(filePath);
    template.sendBody("direct:start", file);
    getMockEndpoint("mock:result").expectedMessageCount(1);
    getMockEndpoint("mock:result").expectedHeaderReceived(S3Constants.KEY, file.getName());
    assertMockEndpointsSatisfied();
  }

  @Override
  protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start").process(new CamelS3HeadersProcessor()).to("mock:result");
        }
    };
  }
}

Processor:

@Component
public class CamelS3HeadersProcessor implements Processor {
  @Override
  public void process(Exchange exchange) throws Exception {
    SimpleBuilder simpleBuilder = new SimpleBuilder("${file:name}");
    String fileName = simpleBuilder.evaluate(exchange, String.class);
    //do some logic and set headers
  }
}

I don't want to mock my processor. I want to mock my endpoints and test my processor.

Problems:

  1. Cannot Autowire/Inject my processor.

  2. File name was evaluated as null. How to use FileConsumer/FTPConsumer instead of ProducerTemplate?

Kenster
  • 23,465
  • 21
  • 80
  • 106
Gangaraju
  • 4,406
  • 9
  • 45
  • 77

2 Answers2

4

I think in createRouteBuilder you will only have the CamelContext and not the ApplicationContext. You can build the route within the test or in the @Before method.

It worked for me like this:

@SpringBootTest
@EnableAutoConfiguration
@RunWith(CamelSpringRunner.class)
public class MyProcessorTest extends AbstractJUnit4SpringContextTests {

  @Autowired
  private CamelContext camelContext;

  @Autowired
  private MyProcessor myProcessor;

  @EndpointInject(uri = "mock:result")
  private MockEndpoint resultEndpoint;

  @Produce(uri = "direct:start")
  private ProducerTemplate template;

  @Before
  public void setUp() throws Exception {
    camelContext.addRoutes(new RouteBuilder() {
      @Override
      public void configure() throws Exception {
        from("direct:start").process(myProcessor).to("mock:result");
      }
    });
    camelContext.start();
  }

  @DirtiesContext
  @Test
  public void test1() throws Exception {
    resultEndpoint.expectedMessageCount(1);
    String myProcessorInput = "test"; // or Object
    template.sendBody(myProcessorInput);

    resultEndpoint.assertIsSatisfied();

    // or other result Type
    String body = (String)resultEndpoint.getExchanges().get(0).getIn().getBody(String.class);

    assertNotNull(body);
    // tests
  }

  @DirtiesContext
  @Test
  public void test2() throws Exception {
  }
}
Dominik Kunicki
  • 1,037
  • 1
  • 12
  • 35
0

In production code, your RouteBuilder will be created outside of the test, so can inject your producer of choice, e.g.

@Component
class MyRouteBuilder extends RouteBuilder {

    private Processor processor;

    @Autowired
    public MyRouteBuilder(Processor pProcessor) {
        processor = pProcessor;
    }

    @Override
    public void configure() {
        from("direct:start").process(processor);
    }
}

Then in your test, you can provide whichever processor (mocked or not) you want:

public class MyTest extends CamelTestSupport {

    private Processor processor;

    @Override
    protected RoutesBuilder createRouteBuilder() {
        return new MyRouteBuilder(processor);
    }

}

(Note also that I've removed the mock endpoint from the route: see this issue How do I test a camel route without changing the production code? for how to test without it.)

Community
  • 1
  • 1
Richard
  • 143
  • 1
  • 10