private static async Task <Response <BlobContentInfo> > WriteStreamToTestDataFolder(string filepath, Stream fileStream) { string dataPath = GetDataOutputBlobPath() + filepath; if (!Directory.Exists(Path.GetDirectoryName(dataPath))) { Directory.CreateDirectory(Path.GetDirectoryName(dataPath)); } int filesize; using (Stream streamToWriteTo = File.Open(dataPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { await fileStream.CopyToAsync(streamToWriteTo); streamToWriteTo.Flush(); filesize = (int)streamToWriteTo.Length; } BlobContentInfo mockedBlobInfo = BlobsModelFactory.BlobContentInfo(new ETag("ETagSuccess"), DateTime.Now, new byte[1], DateTime.Now.ToUniversalTime().ToString(), "encryptionKeySha256", "encryptionScope", 1); Mock <Response <BlobContentInfo> > mockResponse = new Mock <Response <BlobContentInfo> >(); mockResponse.SetupGet(r => r.Value).Returns(mockedBlobInfo); Mock <Response> responseMock = new Mock <Response>(); responseMock.SetupGet(r => r.Status).Returns((int)HttpStatusCode.Created); mockResponse.Setup(r => r.GetRawResponse()).Returns(responseMock.Object); return(mockResponse.Object); }
public async Task UploadToAzureStorageAsync_SuccessfullyUploads_ReturnsFilename() { using var context = new DataContext(ContextOptions); var invoiceService = FactoryService.CreateInvoiceService(context); Mock <Stream> mockMemoryStream = new Mock <Stream>(); Mock <BlobContainerClient> mockBlobContainerClient = new Mock <BlobContainerClient>(); var blobContentInfo = BlobsModelFactory.BlobContentInfo(It.IsAny <ETag>(), It.IsAny <DateTimeOffset>(), It.IsAny <byte[]>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <long>()); Mock <Task <Response <BlobContentInfo> > > mockResponse = new Mock <Task <Response <BlobContentInfo> > >(blobContentInfo); string html = "<html><body></body></html>"; mockBlobContainerClient.Setup(c => c.UploadBlobAsync(It.IsAny <string>(), mockMemoryStream.Object, CancellationToken.None)).Returns(mockResponse.Object); var result = await invoiceService.UploadToAzureStorageAsync(html, It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>()); Assert.True(true); }
public override Task <Response <BlobContentInfo> > UploadAsync(System.IO.Stream content, BlobHttpHeaders httpHeaders = null, IDictionary <string, string> metadata = null, BlobRequestConditions conditions = null, IProgress <long> progressHandler = null, AccessTier?accessTier = null, Storage.StorageTransferOptions transferOptions = default, CancellationToken cancellationToken = default) { if (BlobClientUploadBlobException != null) { throw BlobClientUploadBlobException; } if (BlobInfo != null) { throw new RequestFailedException(409, BlobErrorCode.BlobAlreadyExists.ToString(), BlobErrorCode.BlobAlreadyExists.ToString(), default); } return(Task.FromResult( Response.FromValue( BlobsModelFactory.BlobContentInfo(new ETag("etag"), DateTime.UtcNow, new byte[] { }, string.Empty, 0L), Mock.Of <Response>()))); }
public void StoreCreatesBlobWhenNotExist() { BlobRequestConditions uploadConditions = null; byte[] bytes = null; string contentType = null; var mock = new Mock <BlobClient>(); mock.Setup(c => c.UploadAsync( It.IsAny <Stream>(), It.IsAny <BlobHttpHeaders>(), It.IsAny <IDictionary <string, string> >(), It.IsAny <BlobRequestConditions>(), It.IsAny <IProgress <long> >(), It.IsAny <AccessTier?>(), It.IsAny <StorageTransferOptions>(), It.IsAny <CancellationToken>())) .Returns(async(Stream strm, BlobHttpHeaders headers, IDictionary <string, string> metaData, BlobRequestConditions conditions, IProgress <long> progress, AccessTier? access, StorageTransferOptions transfer, CancellationToken token) => { using var memoryStream = new MemoryStream(); strm.CopyTo(memoryStream); bytes = memoryStream.ToArray(); uploadConditions = conditions; contentType = headers?.ContentType; await Task.Yield(); var mockResponse = new Mock <Response <BlobContentInfo> >(); var blobContentInfo = BlobsModelFactory.BlobContentInfo(ETag.All, DateTimeOffset.Now.AddDays(-1), Array.Empty <byte>(), "", 1); mockResponse.Setup(c => c.Value).Returns(blobContentInfo); return(mockResponse.Object); }); var repository = new AzureBlobXmlRepository(mock.Object); repository.StoreElement(new XElement("Element"), null); Assert.AreEqual("*", uploadConditions.IfNoneMatch.ToString()); Assert.AreEqual("application/xml; charset=utf-8", contentType); var element = "<Element />"; Assert.AreEqual(bytes, GetEnvelopedContent(element)); }
public async Task ShouldReturnTrueWhenUploaded() { var azResponse = new Mock <Response <BlobContentInfo> >(); azResponse.SetupGet(c => c.Value).Returns( BlobsModelFactory.BlobContentInfo(new ETag("file"), DateTimeOffset.MinValue, null, null, null, 0)); var blobClient = new Mock <BlobContainerClient>(); blobClient.Setup(c => c.UploadBlobAsync("file1", It.IsAny <Stream>(), It.IsAny <CancellationToken>())) .ReturnsAsync(azResponse.Object); var subject = new FileRepository(blobClient.Object); using (var ms = new MemoryStream()) using (var token = new CancellationTokenSource()) { var response = await subject.Save("file", ms, token.Token); response.Should().BeTrue(); } }
public void StoreUpdatesWhenExistsAndNewerExists() { byte[] bytes = null; var mock = new Mock <BlobClient>(); mock.Setup(c => c.DownloadToAsync( It.IsAny <Stream>(), It.IsAny <BlobRequestConditions>(), It.IsAny <StorageTransferOptions>(), It.IsAny <CancellationToken>())) .Returns(async(Stream target, BlobRequestConditions conditions, StorageTransferOptions options, CancellationToken token) => { var data = GetEnvelopedContent("<Element1 />"); await target.WriteAsync(data, 0, data.Length); var response = new MockResponse(200); response.AddHeader(new HttpHeader("ETag", "*")); return(response); }) .Verifiable(); mock.Setup(c => c.UploadAsync( It.IsAny <Stream>(), It.IsAny <BlobHttpHeaders>(), It.IsAny <IDictionary <string, string> >(), It.Is((BlobRequestConditions conditions) => conditions.IfNoneMatch == ETag.All), It.IsAny <IProgress <long> >(), It.IsAny <AccessTier?>(), It.IsAny <StorageTransferOptions>(), It.IsAny <CancellationToken>())) .Throws(new RequestFailedException(status: 412, message: "")) .Verifiable(); mock.Setup(c => c.UploadAsync( It.IsAny <Stream>(), It.IsAny <BlobHttpHeaders>(), It.IsAny <IDictionary <string, string> >(), It.Is((BlobRequestConditions conditions) => conditions.IfNoneMatch != ETag.All), It.IsAny <IProgress <long> >(), It.IsAny <AccessTier?>(), It.IsAny <StorageTransferOptions>(), It.IsAny <CancellationToken>())) .Returns(async(Stream strm, BlobHttpHeaders headers, IDictionary <string, string> metaData, BlobRequestConditions conditions, IProgress <long> progress, AccessTier? access, StorageTransferOptions transfer, CancellationToken token) => { using var memoryStream = new MemoryStream(); strm.CopyTo(memoryStream); bytes = memoryStream.ToArray(); await Task.Yield(); var mockResponse = new Mock <Response <BlobContentInfo> >(); var blobContentInfo = BlobsModelFactory.BlobContentInfo(ETag.All, DateTimeOffset.Now.AddDays(-1), Array.Empty <byte>(), "", 1); mockResponse.Setup(c => c.Value).Returns(blobContentInfo); return(mockResponse.Object); }) .Verifiable(); var repository = new AzureBlobXmlRepository(mock.Object); repository.StoreElement(new XElement("Element2"), null); mock.Verify(); Assert.AreEqual(bytes, GetEnvelopedContent("<Element1 /><Element2 />")); }