mercredi 14 septembre 2016

How do I setup Azure's CloudBlockBlob.Exists using Fakes?

I am trying to use Microsoft Fakes to unit test the following C# CloudStorageAccount code.

var containerName = ConfigurationManager.AppSettings["ContainerName"];
var blobName = ConfigurationManager.AppSettings["BlobName"];
var storageConnectionString = ConfigurationManager.AppSettings["StorageConnectionString"];

// Create a Blob Client
var cloudStorageAccount = CloudStorageAccount.Parse(storageConnectionString);
var cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();

// Get Container Reference
var containerReference = cloudBlobClient.GetContainerReference(containerName);
if (containerReference.Exists())
{
    // Get Blob Reference
    var cloudBlockBlobReference = containerReference.GetBlockBlobReference(blobName);
    if (cloudBlockBlobReference.Exists())
    {
        // Acquire a Lease
        var timeSpan = new TimeSpan(0, 0, 0, 30);
        var leaseId = Guid.NewGuid().ToString();
        leaseId = cloudBlockBlobReference.AcquireLease(timeSpan, leaseId);

        // Update the blob
        var leaseCondition = AccessCondition.GenerateLeaseCondition(leaseId);
        cloudBlockBlobReference.UploadText("content", Encoding.UTF8, leaseCondition, null);

        // Release the Lease
        cloudBlockBlobReference.ReleaseLease(leaseCondition);
    }
}

I have created the unit test which uses a Fake for "Microsoft.WindowsAzure.Storage" version 7.2.0.0. I'm using the following code to setup the Shim methods.

// Parse
ShimCloudStorageAccount.ParseString = (connectionString) =>
{
    return new ShimCloudStorageAccount
    {
        // CreateCloudBlobClient
        CreateCloudBlobClient = () => new ShimCloudBlobClient
        {
            // GetContainerReference
            GetContainerReferenceString = (containerName) =>
            {
                return new ShimCloudBlobContainer
                {
                    // Exists
                    ExistsBlobRequestOptionsOperationContext = (blobRequestOptions, operationContext) => true,

                    // GetBlockBlobReference
                    GetBlockBlobReferenceString = (blobname) =>
                    {
                        var shimCloudBlob = new ShimCloudBlob
                        {
                            // Exists
                            ExistsBooleanBlobRequestOptionsOperationContext = (requestOptions, operationContext, x) => true
                        };

                        return new ShimCloudBlockBlob() 
                        {
                             // How to include CloudBlob (above)???
                        };
                    }
                };
            }
        }
    };
};

Q) How to set the ShimCloudBlob base class for ShimCloudBlockBlob?

Q) How do I get rid of the "Cannot convert expression type" warnings for each return type in fake setup? I believe I need to return the Shim version.

Q) Is there some example code which shows how to setup Shim methods for CloudBlockBlob?

Aucun commentaire:

Enregistrer un commentaire