public async Task TestDeleteBlobAsync()
        {
            using (ShimsContext.Create())
            {
                connectionStrings.Add(new ConnectionStringSettings(AppSettings.SEVIS_DS2019_STORAGE_CONNECTION_STRING_KEY, "connection string"));
                string connectionString = settings.DS2019FileStorageConnectionString.ConnectionString;

                bool deleted = false;

                string blobName      = "TestBlob";
                string containerName = "ds2019";

                var blobShim = new Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlockBlob
                {
                };

                iBlobStorageSettings.Setup(x => x.CreateBlockBlob(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()))
                .Returns(blobShim);

                var shimContainer = new Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlobContainer
                {
                    GetBlockBlobReferenceString = (blbnm) =>
                    {
                        return(blobShim);
                    }
                };

                iBlobStorageSettings.Setup(x => x.BlobContainer("ds2019"))
                .Returns(shimContainer);

                Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlob.AllInstances.DeleteDeleteSnapshotsOptionAccessConditionBlobRequestOptionsOperationContext = (cBlob, snapOption, accCond, reqOpts, opsCont) =>
                {
                    deleted = true;
                };

                Assert.IsFalse(deleted);
                service.DeleteBlob(blobName, containerName);
                Assert.IsTrue(deleted);

                deleted = false;

                Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlob.AllInstances.DeleteAsync = (cBlob) =>
                {
                    deleted = true;
                    return(Task.FromResult <Object>(null));
                };

                Assert.IsFalse(deleted);
                await service.DeleteBlobAsync(blobName, containerName);

                Assert.IsTrue(deleted);
            }
        }
        public void TestGetBlobLocationAsync_InvalidName()
        {
            using (ShimsContext.Create())
            {
                appSettings.Add(AppSettings.SEVIS_DS2019_STORAGE_CONTAINER, "Storage Container");
                string containerString = settings.DS2019FileStorageContainer;

                connectionStrings.Add(new ConnectionStringSettings(AppSettings.SEVIS_DS2019_STORAGE_CONNECTION_STRING_KEY, "connection string"));
                string connectionString = settings.DS2019FileStorageConnectionString.ConnectionString;

                string blobName      = "Null Blob";
                string containerName = "ds2019";

                string blobUriString = "http://www.google.com";
                var    blobUri       = new Uri(blobUriString);

                var expectedBlobShim = new Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlockBlob
                {
                };

                var containerShim = new Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlobContainer
                {
                    GetBlockBlobReferenceString = (name) =>
                    {
                        return(null);
                    }
                };

                var existsShim = new Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlob
                {
                    ExistsBlobRequestOptionsOperationContext = (exBlob, options) =>
                    {
                        return(false);
                    }
                };

                iBlobStorageSettings.Setup(x => x.BlobContainer("ds2019")).Returns(containerShim);

                Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlobContainer.AllInstances.GetBlobReferenceString = (container, blbnam) =>
                {
                    return(expectedBlobShim);
                };

                Uri blobLocation = service.GetBlobLocation(blobName, containerName);
                Assert.AreNotEqual(blobUriString, blobLocation);

                //Uri asyncBlobLocation = await service.GetBlobLocationAsync(blobName, containerName);
                //Assert.AreNotEqual(blobUriString, asyncBlobLocation);
            }

            //Assert.AreEqual("Invalid Blob Name", service.GetBlobLocation("This blob doesn't exist"));
        }
        public void TestGetBlobLocationAsync()
        {
            using (ShimsContext.Create())
            {
                connectionStrings.Add(new ConnectionStringSettings(AppSettings.SEVIS_DS2019_STORAGE_CONNECTION_STRING_KEY, "connection string"));
                string connectionString = settings.DS2019FileStorageConnectionString.ConnectionString;

                string blobName      = "TestBlob";
                string containerName = "ds2019";

                string blobUriString = "http://www.google.com";

                var blobShim = new Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlockBlob
                {
                };

                iBlobStorageSettings.Setup(x => x.CreateBlockBlob(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()))
                .Returns(blobShim);

                var shimContainer = new Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlobContainer
                {
                    GetBlockBlobReferenceString = (blbnm) =>
                    {
                        return(blobShim);
                    }
                };

                iBlobStorageSettings.Setup(x => x.BlobContainer("ds2019"))
                .Returns(shimContainer);

                Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlob.AllInstances.ExistsBlobRequestOptionsOperationContext = (blob, opts, contxt) =>
                {
                    return(true);
                };

                Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlob.AllInstances.UriGet = (blb) =>
                {
                    return(new Uri(blobUriString));
                };

                var testInstance = service.GetBlobLocation(blobName, containerName);

                Assert.IsNotNull(testInstance);

                var testAsyncInstance = service.GetBlobAsync(blobName, containerName);

                Assert.IsNotNull(testAsyncInstance);
            }
        }
        public void TestGetBlobAsync_InvalidName()
        {
            using (ShimsContext.Create())
            {
                connectionStrings.Add(new ConnectionStringSettings(AppSettings.SEVIS_DS2019_STORAGE_CONNECTION_STRING_KEY, "connection string"));
                string connectionString = settings.DS2019FileStorageConnectionString.ConnectionString;

                string blobName      = "This Blob Doesn't Exist";
                string containerName = "ds2019";

                string blobUriString = "http://www.google.com"; //Blob location
                var    blobUri       = new Uri(blobUriString);

                var blobShim = new Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlockBlob
                {
                };

                iBlobStorageSettings.Setup(x => x.CreateBlockBlob(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()))
                .Returns(blobShim);

                var shimContainer = new Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlobContainer
                {
                    GetBlockBlobReferenceString = (blbnm) =>
                    {
                        return(blobShim);
                    }
                };

                iBlobStorageSettings.Setup(x => x.BlobContainer("ds2019"))
                .Returns(shimContainer);

                Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlob.AllInstances.ExistsBlobRequestOptionsOperationContext = (blb, opts, contxt) =>
                {
                    return(false);
                };

                CloudBlob blob = service.GetBlob(blobName, containerName);
                Assert.IsNull(blob);

                //CloudBlob asyncBlob = await service.GetBlobAsync(blobName, containerName);
                //Assert.IsNull(asyncBlob);
            }
            //Assert.IsNull(service.GetBlob("This blob doesn't exist"));
        }
        public void TestBlobContainer()
        {
            using (ShimsContext.Create())
            {
                connectionStrings.Add(new ConnectionStringSettings(AppSettings.SEVIS_DS2019_STORAGE_CONNECTION_STRING_KEY, "connection string"));
                string connectionString = settings.DS2019FileStorageConnectionString.ConnectionString;

                var expectedUriString = "http://wwww.google.com";
                var expectedUri       = new Uri(expectedUriString);

                string containerString = "ds2019";

                var containerShim = new Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlobContainer
                {
                };

                var blobClientShim = new Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlobClient
                {
                    GetContainerReferenceString = (cntnrName) =>
                    {
                        Assert.AreEqual(containerString, cntnrName);
                        return(containerShim);
                    }
                };

                var storageAccountShim = new Microsoft.WindowsAzure.Storage.Fakes.ShimCloudStorageAccount
                {
                    CreateCloudBlobClient = () =>
                    {
                        return(blobClientShim);
                    }
                };

                var serviceShim = Microsoft.WindowsAzure.Storage.Fakes.ShimCloudStorageAccount.ParseString = (conString) =>
                {
                    return(storageAccountShim);
                };

                CloudBlobContainer actualContainer = blobStorageSettings.BlobContainer("ds2019");

                Assert.IsTrue(object.ReferenceEquals(containerShim.Instance, actualContainer));
            }
        }
Example #6
0
        public async Task TestGetFileAsync()
        {
            using (ShimsContext.Create())
            {
                var byteArray = new byte[1] {
                    (byte)1
                };

                var blobShim = new Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlockBlob
                {
                };


                var shimContainer = new Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlobContainer
                {
                    GetBlockBlobReferenceString = (blbnm) =>
                    {
                        return(blobShim);
                    }
                };


                Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlob.AllInstances.NameGet = (blob) =>
                {
                    return("name");
                };

                Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlob.AllInstances.ExistsBlobRequestOptionsOperationContext = (blob, opts, contxt) =>
                {
                    return(true);
                };

                Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlob.AllInstances.OpenReadAsync = (bShim) =>
                {
                    return(Task.FromResult <Stream>(new MemoryStream(byteArray)));
                };

                var propertiesShim = new Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimBlobProperties
                {
                    LengthGet = () =>
                    {
                        return(byteArray.Length);
                    },
                    ContentTypeGet = () =>
                    {
                        return("application/pdf");
                    }
                };

                Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlob.AllInstances.PropertiesGet = (bShim) =>
                {
                    return(propertiesShim);
                };

                fileStorageService.Setup(x => x.GetBlobAsync(It.IsAny <string>(), It.IsAny <string>())).ReturnsAsync(blobShim);
                var response = await fileStorageHandler.GetFileAsync("test", "test");

                fileStorageService.Verify(x => x.GetBlobAsync(It.IsAny <string>(), It.IsAny <string>()), Times.Once());

                Assert.AreEqual(System.Net.HttpStatusCode.OK, response.StatusCode);
                CollectionAssert.AreEqual(byteArray, await response.Content.ReadAsByteArrayAsync());
                Assert.AreEqual(byteArray.Length, response.Content.Headers.ContentLength);
                Assert.AreEqual(new MediaTypeHeaderValue("application/pdf"), response.Content.Headers.ContentType);
                Assert.AreEqual(new ContentDispositionHeaderValue("attachment")
                {
                    FileName = "name"
                }, response.Content.Headers.ContentDisposition);
            }
        }