I have a camel route which:
- polls a FTP server for new XML files
- downloads the files locally
- validates the XML files against a XSD
- splits the XML by categories into entities
- transforms the entities into json
- 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?
You need to set the expectations on the mocks before you send data into Camel, eg this code
Should be