How to create a test double for DataLakeDirectoryClient object of Azure.Storage.Files.DataLake c# library?

  Kiến thức lập trình

My code is accessing or precisely downloading the file from Azure’s DataLake Storage Gen2. That file is placed by an upstream system of our application. The files are organized hierarchy of directories.

A typical hierarchy of the file will be:
sample-container/process_Id_12/task_id_34/year2024/month7/day10/file.csv

The functionality of my Subject Under test (GetFiles method) is navigating to correct directory, identify the correct file by a pattern and finally get it as stream.

using Azure.Storage.Files.DataLake;

public class DataLakeService
{
   //this object is pointing to the directory "sample-container/process_Id_12/"
   private DataLakeDirectoryClient downloadClient;

   private readonly string taskid = "task_id_34";
   public DataLakeService(DataLakeDirectoryClient dataLakeDirectoryClient)
   {
        this.downloadClient = dataLakeDirectoryClient;
   }

   public IAsyncEnumerable<Stream> GetFiles(DateTime processingDate)
   {
       DataLakeDirectoryClient dir = downloadClient.GetSubDirectoryClient(taskid)
                                             .GetSubDirectoryClient($"year{processingDate.Year}")
                                             .GetSubDirectoryClient($"month{processingDate.Month}");

       if(!dir.Exists()){
           return AsyncEnumerable.Empty<Stream>();
       }
       var pathItems = dir.GetPathsAsync(true);
       await foreach(var pathItem in pathItems)
       {
            string absoluteFullPathToFile = pathItem.Name; //e.g., sample-container/process_Id_12/task_id_34/year2024/month7/day10/file.csv
            string fileName = System.IO.Path.GetFileName(absoluteFullPathToFile);
            if(!IsRegexPatternMatch(fileName))
            {
                 continue;
            }
            var continer = downloadClient.GetParentFileSystemClient();
            string pathToDirectory = System.IO.Path.GetDirectoryName(absoluteFullPathToFile);
            var directoryClient = container.GetDirectoryClient(pathToDirectory);
            var fileClient = directoryClient.GetFileClient(fileName);
            yield return await fileClient.OpenReadAsync();
       }


   }
}

My dotnet 6 application is using NUnit 3.12.0 , Moq 4.14.5 frameworks. To test GetFiles method, I have mocked **DataLakeDirectoryClient **dependency using Moq. But it feels like I am deceiving myself with the mock setups for DataLakeDirectoryClient‘s method calls.

The thing I stuck with is to setup a protected method of base class of DataLakeDirectoryClient.

DataLakeDirectoryClient class is derived from DataLakePathClient
That particular protected method of base class is invoked in through a Extenstion method of base class (the extension method GetParentFileSystemClient’s definition)

I tried as below:

using Nunit.Framework;
using Moq;
public class DataLakeService
{
    public async Task GetFiles_WhenSingleFileInStorage_ThenReturnAStream()
    {
        //Arrange
        var mockDirectoryClient = new Mock<DataLakeDirectoryClient>();
        mockDirectoryClient
              .Setup(dc => dc.GetSubDirectoryClient(It.IsAny<string>()))
              .Returns(mockDirectoryClient.Object);
        mockDirectoryClient
              .Setup(dc => dc.Exists(default(CancelationToken)))
              .Returns(Response.FromValue(true, Mock.Of<Response>()));

        mockDirectoryClient
              .Setup(dc => dc.GetPathsAsync(true, false, default))
              .Returns(() => {
                  var pathItem1 = DataLakeModelFactory
                   .PathItem("sample-container/process_Id_12/task_id_34/year2024/month7/day10/file.csv",
                               false, new DateTime(2024, 7, 10), ETag.All, 256, "", "", "");
                  var pathItem2 = DataLakeModelFactory
                   .PathItem("sample-container/process_Id_12/task_id_34/year2024/month7/day10/file.txt",
                               false, new DateTime(2024, 7, 10), ETag.All, 256, "", "", "");
                  var page1 = Page<PathItem>.FromValues(values: new[] { pathItem1, pathItem2 },
                                       continuationToken: null, response: Mock.Of<Response>());

                  return AsyncPageable<PathItem>.FromPages(new[] { page1 });

               });
         var mockFileSystemClient = new Mock<DataLakeFileSystemClient>();
         mockFileSystemClient.Setup(fsc => fsc.GetDirectoryClient(It.IsAny<string>()))
                    .Returns(mockDirectoryClient.Object);

         var mockFileClient = new Mock<DataLakeFileClient>();
         Stream stream = BinaryData.FromString("This is a line.n This is the second linen").ToStream();
         mockFileClient.Setup(fc => fc.OpenReadAsync(0, default, default, default))
                .Returns(Task.FromResult(stream));
         mockDirectoryClient
              .Setup(dc => dc.GetFileClient(It.IsAny<string>()))
              .Returns(mockFileClient.Object);

         // DataLakeDirectoryClient's GetParentFileSystemClient method get invoked in the SUT
         // To set up that method, I have tried both below

         mockDirectoryClient.Protected()
              .Setup<DataLakeFileSystemClient>("GetParentDataLakeFileSystemClientCore")
              .Returns(mockFileSystemClient.Object);

         //Error for above is "No protected method DataLakeDirectoryClient.GetParentDataLakeFileSystemClientCore found whose signature is compatible with the provided arguments ()."

         mockDirectoryClient
              .Setup(dc => dc.GetParentFileSystemClient())
              .Returns(mockFileSystemClient.Object);
         // Error: "Unsupported expression : dc => dc.GetParentFileSystemClient()
         // Extension meethods(here: SpecializedDataLakeExtensions.GetParentFileSystemClient) May not be used in setup / verification expression "
      
            
        //Act
        //Assert
    }
}

So How to set up the call to this extension method or protected member of base class ?

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT