I have the route, which will just take file and upload that to blob container. Can someone help me with different ways I can write unit -tests for this route ?
from("direct:uploadFileToBlob")
.routeId("uploadFile")
.setHeader("CamelAzureStorageBlobSourceBlobAccountName",constant(storageAccountName))
.log(LoggingLevel.INFO,"Connecting To Container Name: ${header.CamelAzureStorageBlobContainerName}")
.log(LoggingLevel.INFO,"Connecting To Account Name: ${header.CamelAzureStorageBlobSourceBlobAccountName}")
.toD("azure-storage-blob://${header.CamelAzureStorageBlobSourceBlobAccountName}/${header.CamelAzureStorageBlobContainerName}?blobName=${header.CamelFileName}&operation=uploadBlockBlob&serviceClient=#serviceClient")
.log(LoggingLevel.INFO,"${header.CamelFileName} Uploaded to ${header.CamelAzureStorageBlobContainerName} Container Successfully")
.end();
Already provided you with some answers in your other question. Even added some code examples there to help testing routes with files albeit using more generic CamelTestSupport instead of Spring annotations.
Key thing to understand when writing camel unit tests is that you should focus on testing your routing logic not camel components. People maintaining camel have already written bunch of unit tests for camel and most if not all its available components.
To avoid over-complicating unit tests its good to understand different levels of testing along with their scope and purpose.
You can find more information about them from various online articles as well as from wikipedia.
There's usually not much business logic to test when it comes to routes that just send a file to blob storage or something similar. What you should test is that the azure-storage-blob
endpoint actually receives the file. Additionally you may have business requirements requiring you to validate the file before sending (i.e are the contents valid, does the file/blob name follow required naming convention etc.)
Since your route has some dependencies like headers:
You could test that:
For these tests you can use use adviceWith and weave to replace the azure-storage-blob endpoint using weaveByURI or weaveByID and replace
.
context.adviceWith(context.getRouteDefinition("uploadFile"),
new AdviceWithRouteBuilder(){
@Override
public void configure() throws Exception {
weaveByToUri("azure-storage-blob:*")
.replace()
.to("mock:azure-storage-blob");
}
}
);
MockEndpoint azureBlobStorageMockEndpoint = getMockEndpoint("mock:azure-storage-blob");
azureBlobStorageMockEndpoint.expectedMessageCount(1);
azureBlobStorageMockEndpoint.message(0)
.header(Exchange.FILE_NAME).isEqualTo("TestFile.json");
InputStream body = fetchFileFromResourcesFolderAsStream("test-files/TestFile.json");
Map<String, Object> headers = new HashMap<>();
headers.put(Exchange.FILE_NAME, "TestFile.json");
startCamelContext();
template.sendBodyAndHeaders("direct:start", body, headers);
azureBlobStorageMockEndpoint.assertIsSatisfied();
These tests require minimal modifications to your actual route as they simply test that when route results in expected output when provided with specific input.