public void StoreUpdatesWhenExistsAndNewerExists()
        {
            AccessCondition downloadCondition = null;

            byte[]         bytes      = null;
            BlobProperties properties = new BlobProperties();

            var mock = new Mock <ICloudBlob>();

            mock.SetupGet(c => c.Properties).Returns(properties);
            mock.Setup(c => c.DownloadToStreamAsync(
                           It.IsAny <Stream>(),
                           It.IsAny <AccessCondition>(),
                           null,
                           null))
            .Returns(async(Stream target, AccessCondition condition, BlobRequestOptions options, OperationContext context) =>
            {
                var data = GetEnvelopedContent("<Element1 />");
                await target.WriteAsync(data, 0, data.Length);
            })
            .Verifiable();

            mock.Setup(c => c.UploadFromByteArrayAsync(
                           It.IsAny <byte[]>(),
                           It.IsAny <int>(),
                           It.IsAny <int>(),
                           It.Is((AccessCondition cond) => cond.IfNoneMatchETag == "*"),
                           It.IsAny <BlobRequestOptions>(),
                           It.IsAny <OperationContext>()))
            .Throws(new StorageException(new RequestResult {
                HttpStatusCode = 412
            }, null, null))
            .Verifiable();

            mock.Setup(c => c.UploadFromByteArrayAsync(
                           It.IsAny <byte[]>(),
                           It.IsAny <int>(),
                           It.IsAny <int>(),
                           It.Is((AccessCondition cond) => cond.IfNoneMatchETag != "*"),
                           It.IsAny <BlobRequestOptions>(),
                           It.IsAny <OperationContext>()))
            .Returns(async(byte[] buffer, int index, int count, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext) =>
            {
                bytes = buffer.Skip(index).Take(count).ToArray();
                await Task.Yield();
            })
            .Verifiable();

            var repository = new AzureBlobXmlRepository(() => mock.Object);

            repository.StoreElement(new XElement("Element2"), null);

            mock.Verify();
            Assert.Null(downloadCondition);
            Assert.Equal(bytes, GetEnvelopedContent("<Element1 /><Element2 />"));
        }
Exemplo n.º 2
0
        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 CanAddKeysIndependently()
        {
            var blobClient = await GetBlobClient("testblob2");

            await blobClient.DeleteIfExistsAsync();

            var repository = new AzureBlobXmlRepository(blobClient);

            repository.StoreElement(new XElement("XmlElement"), null);
            Assert.AreEqual(1, repository.GetAllElements().Count);

            var repository2 = new AzureBlobXmlRepository(blobClient);

            // Store another element
            repository2.StoreElement(new XElement("XmlElement"), null);

            Assert.AreEqual(2, repository2.GetAllElements().Count);
            Assert.AreEqual(2, repository.GetAllElements().Count);
        }
        public void StoreCreatesBlobWhenNotExist()
        {
            AccessCondition downloadCondition = null;
            AccessCondition uploadCondition   = null;

            byte[]         bytes      = null;
            BlobProperties properties = new BlobProperties();

            var mock = new Mock <ICloudBlob>();

            mock.SetupGet(c => c.Properties).Returns(properties);
            mock.Setup(c => c.UploadFromByteArrayAsync(
                           It.IsAny <byte[]>(),
                           It.IsAny <int>(),
                           It.IsAny <int>(),
                           It.IsAny <AccessCondition>(),
                           It.IsAny <BlobRequestOptions>(),
                           It.IsAny <OperationContext>()))
            .Returns(async(byte[] buffer, int index, int count, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext) =>
            {
                bytes           = buffer.Skip(index).Take(count).ToArray();
                uploadCondition = accessCondition;
                await Task.Yield();
            });

            var repository = new AzureBlobXmlRepository(() => mock.Object);

            repository.StoreElement(new XElement("Element"), null);

            Assert.Null(downloadCondition);
            Assert.Equal("*", uploadCondition.IfNoneMatchETag);
            Assert.Equal("application/xml; charset=utf-8", properties.ContentType);
            var element = "<Element />";

            Assert.Equal(bytes, GetEnvelopedContent(element));
        }
Exemplo n.º 5
0
        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 />"));
        }