public async Task UploadBlobCommandHandler_GivenValidParameters_ShouldReturnTrue()
        {
            var filename          = "example.pdf";
            var bytes             = new byte[1000];
            var cancellationToken = new CancellationToken();

            var repository = new Mock <IStorageRepository>();

            var request = new UploadBlobCommand(filename, bytes);
            var handler = new UploadBlobCommandHandler(repository.Object);

            var result = await handler.Handle(request, cancellationToken);

            result.IsSuccessful.ShouldBeTrue();
        }
        static async Task Run(Options options)
        {
            IBlobClientFactory factory = new BlobClientFactory(options.StorageConnectionString);
            ICommand           command = new UploadBlobCommand(factory, options.ContainerName);

            BlobRequestOptions requestOptions = new BlobRequestOptions()
            {
                RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(5), 3)
            };

            IDictionary <string, object> commandParameters = new Dictionary <string, object>
            {
                { "options", requestOptions },
                { "blobPath", Path.GetFileName(options.FilePath) },
                { "filePath", options.FilePath },
                { "contentType", options.ContentType }
            };

            await command.Run(commandParameters);
        }
        public async Task ShouldUploadBlob()
        {
            // Arrange
            string             containerName = "container";
            string             blobPath      = "foo.txt";
            string             filePath      = "c:\\foo.txt";
            string             contentType   = "txt";
            BlobRequestOptions options       = new BlobRequestOptions();

            Mock <IBlockBlob> blobMock = new Mock <IBlockBlob>();

            Mock <IBlobContainer> containerMock = new Mock <IBlobContainer>();

            containerMock.Setup(m => m.GetBlockBlobReference(blobPath)).Returns(blobMock.Object);

            Mock <IBlobClient> clientMock = new Mock <IBlobClient>();

            clientMock.Setup(m => m.GetContainerReference(containerName)).Returns(containerMock.Object);

            Mock <IBlobClientFactory> factoryMock = new Mock <IBlobClientFactory>();

            factoryMock.Setup(m => m.CreateBlobClient()).Returns(clientMock.Object);

            ICommand command = new UploadBlobCommand(factoryMock.Object, containerName);

            IDictionary <string, object> commandParameters = new Dictionary <string, object>
            {
                { "options", options },
                { "blobPath", blobPath },
                { "filePath", filePath },
                { "contentType", contentType }
            };

            // Act
            await command.Run(commandParameters);

            // Assert
            containerMock.Verify(m => m.CreateIfNotExistsAsync(options), Times.Once);
            blobMock.Verify(m => m.UploadFromFileAsync(filePath, contentType), Times.Once);
        }
Beispiel #4
0
        public async Task <IActionResult> Blob([FromForm] UploadBlobCommand request)
        {
            var result = await _mediator.Send(request);

            return(Created("/download/blobs", result));
        }