AzureBlobXmlRepositoryTests.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Xml.Linq;
  8. using Microsoft.WindowsAzure.Storage;
  9. using Microsoft.WindowsAzure.Storage.Blob;
  10. using Moq;
  11. using Xunit;
  12. namespace Microsoft.AspNetCore.DataProtection.AzureStorage.Test
  13. {
  14. public class AzureBlobXmlRepositoryTests
  15. {
  16. [Fact]
  17. public void StoreCreatesBlobWhenNotExist()
  18. {
  19. AccessCondition downloadCondition = null;
  20. AccessCondition uploadCondition = null;
  21. byte[] bytes = null;
  22. BlobProperties properties = new BlobProperties();
  23. var mock = new Mock<ICloudBlob>();
  24. mock.SetupGet(c => c.Properties).Returns(properties);
  25. mock.Setup(c => c.UploadFromByteArrayAsync(
  26. It.IsAny<byte[]>(),
  27. It.IsAny<int>(),
  28. It.IsAny<int>(),
  29. It.IsAny<AccessCondition>(),
  30. It.IsAny<BlobRequestOptions>(),
  31. It.IsAny<OperationContext>()))
  32. .Returns(async (byte[] buffer, int index, int count, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext) =>
  33. {
  34. bytes = buffer.Skip(index).Take(count).ToArray();
  35. uploadCondition = accessCondition;
  36. await Task.Yield();
  37. });
  38. var repository = new AzureBlobXmlRepository(() => mock.Object);
  39. repository.StoreElement(new XElement("Element"), null);
  40. Assert.Null(downloadCondition);
  41. Assert.Equal("*", uploadCondition.IfNoneMatchETag);
  42. Assert.Equal("application/xml; charset=utf-8", properties.ContentType);
  43. var element = "<Element />";
  44. Assert.Equal(bytes, GetEnvelopedContent(element));
  45. }
  46. [Fact]
  47. public void StoreUpdatesWhenExistsAndNewerExists()
  48. {
  49. AccessCondition downloadCondition = null;
  50. byte[] bytes = null;
  51. BlobProperties properties = new BlobProperties();
  52. var mock = new Mock<ICloudBlob>();
  53. mock.SetupGet(c => c.Properties).Returns(properties);
  54. mock.Setup(c => c.DownloadToStreamAsync(
  55. It.IsAny<Stream>(),
  56. It.IsAny<AccessCondition>(),
  57. null,
  58. null))
  59. .Returns(async (Stream target, AccessCondition condition, BlobRequestOptions options, OperationContext context) =>
  60. {
  61. var data = GetEnvelopedContent("<Element1 />");
  62. await target.WriteAsync(data, 0, data.Length);
  63. })
  64. .Verifiable();
  65. mock.Setup(c => c.UploadFromByteArrayAsync(
  66. It.IsAny<byte[]>(),
  67. It.IsAny<int>(),
  68. It.IsAny<int>(),
  69. It.Is((AccessCondition cond) => cond.IfNoneMatchETag == "*"),
  70. It.IsAny<BlobRequestOptions>(),
  71. It.IsAny<OperationContext>()))
  72. .Throws(new StorageException(new RequestResult { HttpStatusCode = 412 }, null, null))
  73. .Verifiable();
  74. mock.Setup(c => c.UploadFromByteArrayAsync(
  75. It.IsAny<byte[]>(),
  76. It.IsAny<int>(),
  77. It.IsAny<int>(),
  78. It.Is((AccessCondition cond) => cond.IfNoneMatchETag != "*"),
  79. It.IsAny<BlobRequestOptions>(),
  80. It.IsAny<OperationContext>()))
  81. .Returns(async (byte[] buffer, int index, int count, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext) =>
  82. {
  83. bytes = buffer.Skip(index).Take(count).ToArray();
  84. await Task.Yield();
  85. })
  86. .Verifiable();
  87. var repository = new AzureBlobXmlRepository(() => mock.Object);
  88. repository.StoreElement(new XElement("Element2"), null);
  89. mock.Verify();
  90. Assert.Null(downloadCondition);
  91. Assert.Equal(bytes, GetEnvelopedContent("<Element1 /><Element2 />"));
  92. }
  93. private static byte[] GetEnvelopedContent(string element)
  94. {
  95. return Encoding.UTF8.GetBytes($"<?xml version=\"1.0\" encoding=\"utf-8\"?><repository>{element}</repository>");
  96. }
  97. }
  98. }