Hello,
I would like to use Camel Infrastructure for my Spring Boot Camel End-to-End
tests.
II have spent quite some time investigating how it works. Unfortunately the
documentation is very limited and I was not able to find any examples either.
I started with a very simple scenario -> one route from FTP to file. After
couple of hours I was able to come to following test that works.
@SpringBootTest
@Import({ FtpServiceTestConfig.class, Process1TestRoutes.class})
@TestPropertySource("classpath:application-e2e.yaml")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ActiveProfiles("e2e")
public class Process1E2ETest {
@Autowired
CamelContext camelContext;
@RegisterExtension
static FtpService ftpService = FtpServiceFactory.createEmbeddedService();
@Test
void shouldTransferFileFromInputToOutput() throws Exception {
// Create the route after FTP server is initialized
camelContext.addRoutes(new RouteBuilder() {
@Override
public void configure() {
from("ftp://" + ftpService.hostname() + ":" + ftpService.port()
+ "/" + ftpService.directoryName()
+ "?username=" + ftpService.username()
+ "&password=" + ftpService.password()
+ "&delete=true&delay=1000")
.to("file:target/out")
.log("Received: ${file:name}");
}
});
// Ensure target directory exists
Files.createDirectories(Path.of("target/out"));
// Create input file in the correct FTP directory
Path ftpRootDir = ftpService.getFtpRootDir();
Path ftpInputDir = ftpRootDir.resolve(ftpService.directoryName());
Files.createDirectories(ftpInputDir);
Path inputFile = ftpInputDir.resolve("test.txt");
Files.writeString(inputFile, "Hello from test",
StandardOpenOption.CREATE);
System.out.println("Created file at: " + inputFile.toAbsolutePath());
// Wait longer for Camel route to process the file
Thread.sleep(5000);
Path outputFile = Path.of("target/out/test.txt");
assertTrue(Files.exists(outputFile), "Output file should exist at " +
outputFile.toAbsolutePath());
String content = Files.readString(outputFile);
assertEquals("Hello from test", content);
}
}
I had to copy FtpService and FtpServiceFactory with their dependencies from
Camel Github Repository
(https://github.com/apache/camel/tree/camel-4.12.x/test-infra/camel-test-infra-ftp/src/test/java/org/apache/camel/test/infra/ftp/services)
as they are not included in maven dependency jar. I am using Apache Camel
4.12.0
I do not like the solution much as it creates the route only inside the test
that is kind of ugly. If I try to create it in @TestConfiguration or even
@BeforeAll the troube is that the FTP server is not initalized yet and
ftpService.port() is 0 at that time.
Can anyone explain me how it is meant to be used and/or provide some example?
Regards
Jiří