FTP Route - Unit Testing

893 views Asked by At

I have a camel route which:

  1. polls a FTP server for new XML files
  2. downloads the files locally
  3. validates the XML files against a XSD
  4. splits the XML by categories into entities
  5. transforms the entities into json
  6. sends the json to a HTTP endpoint

UPDATE: This works now

@Component
public class FTPPoller extends RouteBuilder {
    XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat();

    @Override
    public void configure() throws Exception {

        from("{{endpoint.ftp.server}}")
                .id("ftp-poller")
                .log("Found file ${file:name}.")
                .to("{{endpoint.local.validation}}");


        from("{{endpoint.local.validation}}")
                .id("xml-validator")
                .log("Processing file ${file:name}.")
                .doTry()
                    .to("validator:classpath:schema/fr-masterdata.xsd")
                    .log("File ${file:name} is valid.")
                    .to("{{endpoint.local.processing}}")
                .doCatch(org.apache.camel.ValidationException.class)
                    .log("File ${file:name} is invalid.")
                    .to("{{endpoint.local.error}}")
                .end();

        from("{{endpoint.local.processing}}")
                .id("xml-processor")
                .split(xpath("//flu:entities/category")
                        .namespace("flu", "hxxx://www.xxx.com")
                ).streaming()
                .marshal(xmlJsonFormat)
                .to("direct:category")
                .end();

        from("direct:category")
                .id("requestbin")
                .log("Processing category ${body}")
                .setHeader(Exchange.HTTP_METHOD, constant("POST"))
                .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
                .to("{{endpoint.requestbin}}");

    }
}

Tests :

@RunWith(CamelSpringBootRunner.class)
@SpringBootTest(classes = {HbIntegrationApplication.class},
        properties = { "camel.springboot.java-routes-include-pattern=**/FTPPoller*"})
public class FTPPollerTest {

    @Autowired
    protected ProducerTemplate producerTemplate;

    @EndpointInject(uri = "{{endpoint.requestbin}}")
    protected MockEndpoint requestbinEndpoint;

    @EndpointInject(uri = "{{endpoint.local.error}}")
    protected MockEndpoint localErrorEndpoint;

    @Before
    public void cleanDir() throws Exception {
        deleteDirectory("hb");
    }


    @Test
    @DirtiesContext
    public void testFileUploadSuccess() throws Exception {
        String fileContent = FileUtils.readFileToString(new File("src/test/resources/test-files/category.xml"));

        requestbinEndpoint.expectedMessageCount(2);
        producerTemplate.sendBody("file://hb/incoming", fileContent);

        requestbinEndpoint.assertIsSatisfied();
    }

    @Test
    @DirtiesContext
    public void testFileUploadFailure() throws Exception {

        localErrorEndpoint.expectedMessageCount(1);
        requestbinEndpoint.expectedMessageCount(0);

        producerTemplate.sendBody("file://hb/incoming", "invalidContent");

        localErrorEndpoint.assertIsSatisfied();
        requestbinEndpoint.assertIsSatisfied();
    }
}

application.properties :

endpoint.ftp.server=file://hb/incoming
endpoint.local.validation=file://hb/validation
endpoint.local.processing=file://hb/processing
endpoint.local.error=mock:file://hb/error
endpoint.requestbin=mock:requestbin

Remaining question is:

If I have defined the following property:

endpoint.local.processing=mock:file://hb/processing

my test fails with:

Caused by: java.lang.UnsupportedOperationException: You cannot consume from this endpoint

Is there any way to define which routes should be included in my Unit test?

1

There are 1 answers

6
Claus Ibsen On

You need to set the expectations on the mocks before you send data into Camel, eg this code

    localValidationEndpoint.sendBody(xml);

    requestbinEndpoint.expectedMessageCount(2);
    requestbinEndpoint.assertIsSatisfied();

Should be

    requestbinEndpoint.expectedMessageCount(2);

    localValidationEndpoint.sendBody(xml);

    requestbinEndpoint.assertIsSatisfied();