|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.IO; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Azure.Core; |
| 9 | +using Azure.Core.TestFramework; |
| 10 | +using Azure.Storage.Blobs.Models; |
| 11 | +using Azure.Storage.Blobs.Specialized; |
| 12 | +using Azure.Storage.Test; |
| 13 | +using Azure.Storage.Test.Shared; |
| 14 | +using NUnit.Framework; |
| 15 | + |
| 16 | +namespace Azure.Storage.Blobs.Tests |
| 17 | +{ |
| 18 | + public class AppendBlobClientOpenWriteTests : BlobBaseClientOpenWriteTests<AppendBlobClient> |
| 19 | + { |
| 20 | + public AppendBlobClientOpenWriteTests(bool async, BlobClientOptions.ServiceVersion serviceVersion) |
| 21 | + : base(async, serviceVersion, null /* RecordedTestMode.Record /* to re-record */) |
| 22 | + { |
| 23 | + } |
| 24 | + |
| 25 | + protected override AppendBlobClient GetResourceClient(BlobContainerClient container, string resourceName = null, BlobClientOptions options = null) |
| 26 | + { |
| 27 | + Argument.AssertNotNull(container, nameof(container)); |
| 28 | + |
| 29 | + string blobName = resourceName ?? GetNewResourceName(); |
| 30 | + |
| 31 | + if (options == null) |
| 32 | + { |
| 33 | + return container.GetAppendBlobClient(blobName); |
| 34 | + } |
| 35 | + |
| 36 | + container = InstrumentClient(new BlobContainerClient(container.Uri, Tenants.GetNewSharedKeyCredentials(), options ?? ClientBuilder.GetOptions())); |
| 37 | + return InstrumentClient(container.GetAppendBlobClient(blobName)); |
| 38 | + } |
| 39 | + |
| 40 | + protected override async Task ModifyAsync(AppendBlobClient client, Stream data) |
| 41 | + { |
| 42 | + Argument.AssertNotNull(client, nameof(client)); |
| 43 | + Argument.AssertNotNull(data, nameof(data)); |
| 44 | + |
| 45 | + // open write doesn't support modification, we need to manually edit the blob |
| 46 | + |
| 47 | + await client.AppendBlockAsync(data); |
| 48 | + } |
| 49 | + |
| 50 | + protected override Task<Stream> OpenWriteAsync( |
| 51 | + AppendBlobClient client, |
| 52 | + bool overwrite, |
| 53 | + long? maxDataSize, |
| 54 | + int? bufferSize = null, |
| 55 | + BlobRequestConditions conditions = null, |
| 56 | + Dictionary<string, string> metadata = null, |
| 57 | + HttpHeaderParameters httpHeaders = null, |
| 58 | + IProgress<long> progressHandler = null) |
| 59 | + => OpenWriteAsync(client, overwrite, maxDataSize, tags: default, bufferSize, conditions, metadata, httpHeaders, progressHandler); |
| 60 | + |
| 61 | + protected override async Task<Stream> OpenWriteAsync( |
| 62 | + AppendBlobClient client, |
| 63 | + bool overwrite, |
| 64 | + long? maxDataSize, |
| 65 | + Dictionary<string, string> tags, |
| 66 | + int? bufferSize = default, |
| 67 | + BlobRequestConditions conditions = default, |
| 68 | + Dictionary<string, string> metadata = default, |
| 69 | + HttpHeaderParameters httpHeaders = default, |
| 70 | + IProgress<long> progressHandler = default) |
| 71 | + { |
| 72 | + if (metadata != default) |
| 73 | + { |
| 74 | + TestHelper.AssertInconclusiveRecordingFriendly(Recording.Mode, "AppendBlobClient.OpenWriteAsync() does not support metadata."); |
| 75 | + } |
| 76 | + if (tags != default) |
| 77 | + { |
| 78 | + TestHelper.AssertInconclusiveRecordingFriendly(Recording.Mode, "AppendBlobClient.OpenWriteAsync() does not support tags."); |
| 79 | + } |
| 80 | + if (httpHeaders != default) |
| 81 | + { |
| 82 | + TestHelper.AssertInconclusiveRecordingFriendly(Recording.Mode, "AppendBlobClient.OpenWriteAsync() does not support httpHeaders."); |
| 83 | + } |
| 84 | + |
| 85 | + AppendBlobRequestConditions appendConditions = conditions == default ? default : new AppendBlobRequestConditions |
| 86 | + { |
| 87 | + IfModifiedSince = conditions.IfModifiedSince, |
| 88 | + IfUnmodifiedSince = conditions.IfUnmodifiedSince, |
| 89 | + IfMatch = conditions.IfMatch, |
| 90 | + IfNoneMatch = conditions.IfNoneMatch, |
| 91 | + LeaseId = conditions.LeaseId, |
| 92 | + }; |
| 93 | + return await client.OpenWriteAsync(overwrite, new AppendBlobOpenWriteOptions |
| 94 | + { |
| 95 | + BufferSize = bufferSize, |
| 96 | + OpenConditions = appendConditions, |
| 97 | + ProgressHandler = progressHandler |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + #region Tests |
| 102 | + [RecordedTest] |
| 103 | + public async Task OpenWriteAsync_AppendExistingBlob() |
| 104 | + { |
| 105 | + await using IDisposingContainer<BlobContainerClient> disposingContainer = await GetDisposingContainerAsync(); |
| 106 | + AppendBlobClient blob = GetResourceClient(disposingContainer.Container); |
| 107 | + await blob.CreateAsync(); |
| 108 | + |
| 109 | + byte[] originalData = GetRandomBuffer(Constants.KB); |
| 110 | + using Stream originalStream = new MemoryStream(originalData); |
| 111 | + |
| 112 | + await blob.AppendBlockAsync(content: originalStream); |
| 113 | + |
| 114 | + byte[] newData = GetRandomBuffer(Constants.KB); |
| 115 | + using Stream newStream = new MemoryStream(newData); |
| 116 | + |
| 117 | + // Act |
| 118 | + Stream openWriteStream = await blob.OpenWriteAsync(overwrite: false); |
| 119 | + await newStream.CopyToAsync(openWriteStream); |
| 120 | + await openWriteStream.FlushAsync(); |
| 121 | + |
| 122 | + // Assert |
| 123 | + byte[] expectedData = new byte[2 * Constants.KB]; |
| 124 | + Array.Copy(originalData, 0, expectedData, 0, Constants.KB); |
| 125 | + Array.Copy(newData, 0, expectedData, Constants.KB, Constants.KB); |
| 126 | + |
| 127 | + Response<BlobDownloadInfo> result = await blob.DownloadAsync(new HttpRange(0, 2 * Constants.KB)); |
| 128 | + MemoryStream dataResult = new MemoryStream(); |
| 129 | + await result.Value.Content.CopyToAsync(dataResult); |
| 130 | + Assert.AreEqual(expectedData.Length, dataResult.Length); |
| 131 | + TestHelper.AssertSequenceEqual(expectedData, dataResult.ToArray()); |
| 132 | + } |
| 133 | + #endregion |
| 134 | + } |
| 135 | +} |
0 commit comments