Search code examples
javaspring-bootapache-camelazure-blob-storagespring-camel

Unit test cases for camel azure blob container


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();

Solution

  • 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.

    Focus on testing your routing logic not camel components or external services

    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.

    Testing levels

    1. Unit testing
    2. Integration testing
    3. System testing
    4. Acceptance testing

    You can find more information about them from various online articles as well as from wikipedia.

    Testing azure-storage-blob routes

    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.)

    1. Test that azure-storage-blob endpoint receives file
    2. Test that azure-storage-blob endpoint receives valid data.
    3. Test that route logs error or throws proper exception when it does not.
      • If monitoring is in use its possible that the error message needs to be in some specific format for alerts and whatnot to work.
    4. Test that invalid data does not get send to the azure-storage-blob endpoint
    5. Test that connection errors with azure logs error or proper exception.
      • These are very helpful for system and integration testing that can be done later if there are problems with integration configs, firewalls and whatnot.

    Since your route has some dependencies like headers:

    • CamelAzureStorageBlobSourceBlobAccountName
    • CamelAzureStorageBlobContainerName
    • CamelFileName

    You could test that:

    1. azure-storage-blob endpoint receives these headers when given
    2. route logs error or throws proper exception if any of these headers are missing.

    For these tests you can use use adviceWith and weave to replace the azure-storage-blob endpoint using weaveByURI or weaveByID and replace.

    Example

    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.