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 async Task TestUploadBlobAsync()
        {
            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);

                byte[] blobData = new byte[1] {
                    (byte)1
                };

                string contentType   = "application/pdf";
                string blobName      = "Test_Async_" + new Guid().ToString();
                string containerName = "ds2019";

                var shim = new Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlockBlob
                {
                    UploadFromByteArrayAsyncByteArrayInt32Int32 = (buffer, index, count) =>
                    {
                        CollectionAssert.AreEqual(blobData, buffer);
                        return(Task.FromResult <Object>(null));
                    },
                };
                Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlob.AllInstances.UriGet = (blb) =>
                {
                    return(expectedUri);
                };
                Action <string, string, string> callbackTester = (cntType, bName, contName) =>
                {
                    Assert.AreEqual(contentType, cntType);
                    Assert.AreEqual(blobName, bName);
                };

                iBlobStorageSettings.Setup(x => x.CreateBlockBlob(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()))
                .Returns(shim)
                .Callback(callbackTester);
                Uri blobUploadUri = service.UploadBlob(blobData, contentType, blobName, containerName);
                Assert.AreEqual(expectedUri, blobUploadUri);

                blobUploadUri = await service.UploadBlobAsync(blobData, contentType, blobName, containerName);

                Assert.AreEqual(expectedUri, blobUploadUri);
            }

            //Assert.AreEqual("https://" + settings.StorageName + ".blob.core.windows.net/" + settings.StorageContainer + "/" + blobName, blobUploadUri);
            //Assert.AreEqual(blobUploadUri, service.GetBlobLocation(blobName));
            //Assert.IsTrue(await service.DeleteBlobAsync(blobName));
        }
        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 async Task TestUploadBlobAsync_Stream()
        {
            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);

                Stream blobData      = new System.IO.MemoryStream();
                string contentType   = "application/pdf";
                string blobName      = "Test_Async_" + new Guid().ToString();
                string containerName = "ds2019";

                var shim = new Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlockBlob
                {
                    UploadFromStreamAsyncStream = (blbData) =>
                    {
                        return(Task.FromResult <Object>(null));
                    }
                };
                Microsoft.WindowsAzure.Storage.Blob.Fakes.ShimCloudBlob.AllInstances.UriGet = (blb) =>
                {
                    return(expectedUri);
                };
                Action <string, string, string> callbackTester = (cntType, bName, contName) =>
                {
                    Assert.AreEqual(contentType, cntType);
                    Assert.AreEqual(blobName, bName);
                };

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

                Uri blobUploadUri = service.UploadBlob(blobData, contentType, blobName, containerName);
                Assert.AreEqual(expectedUri, blobUploadUri);

                blobUploadUri = await service.UploadBlobAsync(blobData, contentType, blobName, containerName);

                Assert.AreEqual(expectedUri, blobUploadUri);
            }
        }
示例#7
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);
            }
        }