Skip to content

Commit

Permalink
#14 Migrated to latest SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei15193 committed Dec 6, 2020
1 parent 7fc92eb commit 5a98e79
Show file tree
Hide file tree
Showing 56 changed files with 385 additions and 942 deletions.
99 changes: 4 additions & 95 deletions CloudStub.Tests/AzureStorageUnitTest.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Table;
using System;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Table;

namespace CloudStub.Tests
{
Expand All @@ -18,12 +16,6 @@ namespace CloudStub.Tests
/// </summary>
public abstract class AzureStorageUnitTest : IDisposable
{
private const string _testBlobContainerPrefix = "testblobcontainer";
private static int _blobCount = 0;

private const string _testQueuePrefix = "testqueue";
private static int _queueCount = 0;

private const string _testTablePrefix = "testtable";
private static int _tableCount = 0;

Expand All @@ -37,25 +29,11 @@ public abstract class AzureStorageUnitTest : IDisposable
.DefaultIfEmpty("UseDevelopmentStorage=true;")
.First();

private readonly Lazy<string> _blobContainerName;
private readonly Lazy<string> _testQueueName;
private readonly Lazy<string> _testTableName;

/// <summary>Initializes a new instance of the <see cref="AzureStorageUnitTest"/> class.</summary>
protected AzureStorageUnitTest()
{
_blobContainerName = new Lazy<string>(() => Task.Run(_GetTestBlobContainerNameAsync).Result);
_testQueueName = new Lazy<string>(() => Task.Run(_GetTestQueueNameAsync).Result);
_testTableName = new Lazy<string>(() => Task.Run(_GetTestTableNameAsync).Result);
}

/// <summary>Gets the test blob container name. Unique for each test method.</summary>
protected string TestBlobContainerName
=> _blobContainerName.Value;

/// <summary>Gets the test queue name. Unique for each test method.</summary>
protected string TestQueueName
=> _testQueueName.Value;
=> _testTableName = new Lazy<string>(() => Task.Run(_GetTestTableNameAsync).Result);

/// <summary>Gets the test table name. Unique for each test method.</summary>
protected string TestTableName
Expand All @@ -76,77 +54,8 @@ protected virtual void Dispose(bool disposing)
}

private Task _ClearTestEnvironmentAsync()
=> Task.WhenAll(
_ClearTestBlobContainersAsync(),
_ClearTableAsync(),
_ClearTestQueuesAsync()
);

private static async Task<string> _GetTestBlobContainerNameAsync()
{
var blobContainerName = $"{_testBlobContainerPrefix}{Interlocked.Increment(ref _blobCount)}";
var blobContainer = CloudStorageAccount
.Parse(AzureStorageConnectionString)
.CreateCloudBlobClient()
.GetContainerReference(blobContainerName);
if (await blobContainer.ExistsAsync().ConfigureAwait(false))
await _ClearBlobs(blobContainer).ConfigureAwait(false);
else
await blobContainer.CreateAsync(BlobContainerPublicAccessType.Off, new BlobRequestOptions(), new OperationContext());

return blobContainerName;
}

private static async Task _ClearBlobs(CloudBlobContainer blobContainer)
{
var deletedBlobs = 0;
do
{
var blobsToDelete = (await blobContainer.ListBlobsSegmentedAsync(null).ConfigureAwait(false)).Results.OfType<ICloudBlob>().ToList();

await Task.WhenAll(
from blobToDelete in blobsToDelete
select blobToDelete.DeleteIfExistsAsync()
).ConfigureAwait(false);
deletedBlobs = blobsToDelete.Count;
} while (deletedBlobs > 0);
}
=> _ClearTableAsync();

private async Task _ClearTestBlobContainersAsync()
{
if (_blobContainerName.IsValueCreated)
await CloudStorageAccount
.Parse(AzureStorageConnectionString)
.CreateCloudBlobClient()
.GetContainerReference(_blobContainerName.Value)
.DeleteIfExistsAsync()
.ConfigureAwait(false);
}

private static async Task<string> _GetTestQueueNameAsync()
{
var queueName = $"{_testQueuePrefix}{Interlocked.Increment(ref _queueCount)}";
var queue = CloudStorageAccount
.Parse(AzureStorageConnectionString)
.CreateCloudQueueClient()
.GetQueueReference(queueName);

if (await queue.ExistsAsync().ConfigureAwait(false))
await queue.ClearAsync();

return queueName;
}

private async Task _ClearTestQueuesAsync()
{
if (_testQueueName.IsValueCreated)
await CloudStorageAccount
.Parse(AzureStorageConnectionString)
.CreateCloudQueueClient()
.GetQueueReference(_testQueueName.Value)
.DeleteIfExistsAsync()
.ConfigureAwait(false);
}

private async Task<string> _GetTestTableNameAsync()
{
Expand Down
5 changes: 2 additions & 3 deletions CloudStub.Tests/BaseInMemoryCloudTableTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using System;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Table;

namespace CloudStub.Tests
{
Expand Down
6 changes: 3 additions & 3 deletions CloudStub.Tests/CloudStub.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="Moq" Version="4.13.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="Moq" Version="4.15.2" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
4 changes: 2 additions & 2 deletions CloudStub.Tests/InMemoryCloudTableQueryComparisonTestData.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Microsoft.WindowsAzure.Storage.Table;
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Azure.Cosmos.Table;

namespace CloudStub.Tests
{
Expand Down
2 changes: 1 addition & 1 deletion CloudStub.Tests/InMemoryCloudTableQueryTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Microsoft.WindowsAzure.Storage.Table;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Table;
using Xunit;

namespace CloudStub.Tests
Expand Down
41 changes: 10 additions & 31 deletions CloudStub.Tests/InMemoryCloudTableTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using System;
using System;
using System.Collections;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Table;
using Xunit;

namespace CloudStub.Tests
Expand Down Expand Up @@ -59,7 +58,7 @@ public async Task CreateAsync_WhenTableExists_ThrowsException()
var exception = await Assert.ThrowsAsync<StorageException>(() => CloudTable.CreateAsync(null, null));

Assert.Equal("Conflict", exception.Message);
Assert.Equal("Microsoft.WindowsAzure.Storage", exception.Source);
Assert.Equal("Microsoft.Azure.Cosmos.Table", exception.Source);
Assert.Null(exception.HelpLink);
Assert.Equal(-2146233088, exception.HResult);
Assert.Null(exception.InnerException);
Expand All @@ -70,15 +69,11 @@ public async Task CreateAsync_WhenTableExists_ThrowsException()
Assert.Empty(exception.RequestInformation.ErrorCode);
Assert.Null(exception.RequestInformation.Etag);

Assert.Equal("StorageException", exception.RequestInformation.ExceptionInfo.Type);
Assert.Equal("Conflict", exception.RequestInformation.ExceptionInfo.Message);
Assert.Equal("Microsoft.WindowsAzure.Storage", exception.RequestInformation.ExceptionInfo.Source);
Assert.Equal("TableAlreadyExists", exception.RequestInformation.ExtendedErrorInformation.ErrorCode);
Assert.Matches(
@$"^The table specified already exists.\nRequestId:{exception.RequestInformation.ServiceRequestID}\nTime:\d{{4}}-\d{{2}}-\d{{2}}T\d{{2}}:\d{{2}}:\d{{2}}.\d{{7}}Z$",
exception.RequestInformation.ExtendedErrorInformation.ErrorMessage
);
Assert.Null(exception.RequestInformation.ExceptionInfo.InnerExceptionInfo);

Assert.Same(exception, exception.RequestInformation.Exception);
}
Expand All @@ -92,8 +87,8 @@ public async Task CreateAsync_WhenTableNameIsInvalid_ThrowsException(string tabl

var exception = await Assert.ThrowsAsync<StorageException>(() => cloudTable.CreateAsync(null, null));

Assert.Equal("Bad Request", exception.Message);
Assert.Equal("Microsoft.WindowsAzure.Storage", exception.Source);
Assert.Equal("The remote server returned an error: (400) Bad Request.", exception.Message);
Assert.Equal("Microsoft.Azure.Cosmos.Table", exception.Source);
Assert.Null(exception.HelpLink);
Assert.Equal(-2146233088, exception.HResult);
Assert.Null(exception.InnerException);
Expand All @@ -104,15 +99,11 @@ public async Task CreateAsync_WhenTableNameIsInvalid_ThrowsException(string tabl
Assert.Empty(exception.RequestInformation.ErrorCode);
Assert.Null(exception.RequestInformation.Etag);

Assert.Equal("StorageException", exception.RequestInformation.ExceptionInfo.Type);
Assert.Equal("Bad Request", exception.RequestInformation.ExceptionInfo.Message);
Assert.Equal("Microsoft.WindowsAzure.Storage", exception.RequestInformation.ExceptionInfo.Source);
Assert.Equal("InvalidResourceName", exception.RequestInformation.ExtendedErrorInformation.ErrorCode);
Assert.Matches(
@$"^The specifed resource name contains invalid characters.\nRequestId:{exception.RequestInformation.ServiceRequestID}\nTime:\d{{4}}-\d{{2}}-\d{{2}}T\d{{2}}:\d{{2}}:\d{{2}}.\d{{7}}Z$",
exception.RequestInformation.ExtendedErrorInformation.ErrorMessage
);
Assert.Null(exception.RequestInformation.ExceptionInfo.InnerExceptionInfo);

Assert.Same(exception, exception.RequestInformation.Exception);
}
Expand All @@ -125,8 +116,8 @@ public async Task CreateAsync_WhenTableNameIsReserved_ThrowsException(string tab

var exception = await Assert.ThrowsAsync<StorageException>(() => cloudTable.CreateAsync(null, null));

Assert.Equal("Bad Request", exception.Message);
Assert.Equal("Microsoft.WindowsAzure.Storage", exception.Source);
Assert.Equal("The remote server returned an error: (400) Bad Request.", exception.Message);
Assert.Equal("Microsoft.Azure.Cosmos.Table", exception.Source);
Assert.Null(exception.HelpLink);
Assert.Equal(-2146233088, exception.HResult);
Assert.Null(exception.InnerException);
Expand All @@ -137,15 +128,11 @@ public async Task CreateAsync_WhenTableNameIsReserved_ThrowsException(string tab
Assert.Empty(exception.RequestInformation.ErrorCode);
Assert.Null(exception.RequestInformation.Etag);

Assert.Equal("StorageException", exception.RequestInformation.ExceptionInfo.Type);
Assert.Equal("Bad Request", exception.RequestInformation.ExceptionInfo.Message);
Assert.Equal("Microsoft.WindowsAzure.Storage", exception.RequestInformation.ExceptionInfo.Source);
Assert.Equal("InvalidInput", exception.RequestInformation.ExtendedErrorInformation.ErrorCode);
Assert.Matches(
@$"^One of the request inputs is not valid.\nRequestId:{exception.RequestInformation.ServiceRequestID}\nTime:\d{{4}}-\d{{2}}-\d{{2}}T\d{{2}}:\d{{2}}:\d{{2}}.\d{{7}}Z$",
exception.RequestInformation.ExtendedErrorInformation.ErrorMessage
);
Assert.Null(exception.RequestInformation.ExceptionInfo.InnerExceptionInfo);

Assert.Same(exception, exception.RequestInformation.Exception);
}
Expand All @@ -161,8 +148,8 @@ public async Task CreateAsync_WhenTableNameHasInvalidLength_ThrowsException(stri

var exception = await Assert.ThrowsAsync<StorageException>(() => cloudTable.CreateAsync(null, null));

Assert.Equal("Bad Request", exception.Message);
Assert.Equal("Microsoft.WindowsAzure.Storage", exception.Source);
Assert.Equal("The remote server returned an error: (400) Bad Request.", exception.Message);
Assert.Equal("Microsoft.Azure.Cosmos.Table", exception.Source);
Assert.Null(exception.HelpLink);
Assert.Equal(-2146233088, exception.HResult);
Assert.Null(exception.InnerException);
Expand All @@ -173,15 +160,11 @@ public async Task CreateAsync_WhenTableNameHasInvalidLength_ThrowsException(stri
Assert.Empty(exception.RequestInformation.ErrorCode);
Assert.Null(exception.RequestInformation.Etag);

Assert.Equal("StorageException", exception.RequestInformation.ExceptionInfo.Type);
Assert.Equal("Bad Request", exception.RequestInformation.ExceptionInfo.Message);
Assert.Equal("Microsoft.WindowsAzure.Storage", exception.RequestInformation.ExceptionInfo.Source);
Assert.Equal("OutOfRangeInput", exception.RequestInformation.ExtendedErrorInformation.ErrorCode);
Assert.Matches(
@$"^The specified resource name length is not within the permissible limits.\nRequestId:{exception.RequestInformation.ServiceRequestID}\nTime:\d{{4}}-\d{{2}}-\d{{2}}T\d{{2}}:\d{{2}}:\d{{2}}.\d{{7}}Z$",
exception.RequestInformation.ExtendedErrorInformation.ErrorMessage
);
Assert.Null(exception.RequestInformation.ExceptionInfo.InnerExceptionInfo);

Assert.Same(exception, exception.RequestInformation.Exception);
}
Expand All @@ -208,7 +191,7 @@ public async Task DeleteAsync_WhenTableDoesNotExist_ThrowsException()
var exception = await Assert.ThrowsAsync<StorageException>(() => CloudTable.DeleteAsync(null, null));

Assert.Equal("Not Found", exception.Message);
Assert.Equal("Microsoft.WindowsAzure.Storage", exception.Source);
Assert.Equal("Microsoft.Azure.Cosmos.Table", exception.Source);
Assert.Null(exception.HelpLink);
Assert.Equal(-2146233088, exception.HResult);
Assert.Null(exception.InnerException);
Expand All @@ -219,15 +202,11 @@ public async Task DeleteAsync_WhenTableDoesNotExist_ThrowsException()
Assert.Empty(exception.RequestInformation.ErrorCode);
Assert.Null(exception.RequestInformation.Etag);

Assert.Equal("StorageException", exception.RequestInformation.ExceptionInfo.Type);
Assert.Equal("Not Found", exception.RequestInformation.ExceptionInfo.Message);
Assert.Equal("Microsoft.WindowsAzure.Storage", exception.RequestInformation.ExceptionInfo.Source);
Assert.Equal("ResourceNotFound", exception.RequestInformation.ExtendedErrorInformation.ErrorCode);
Assert.Matches(
@$"^The specified resource does not exist.\nRequestId:{exception.RequestInformation.ServiceRequestID}\nTime:\d{{4}}-\d{{2}}-\d{{2}}T\d{{2}}:\d{{2}}:\d{{2}}.\d{{7}}Z$",
exception.RequestInformation.ExtendedErrorInformation.ErrorMessage
);
Assert.Null(exception.RequestInformation.ExceptionInfo.InnerExceptionInfo);

Assert.Same(exception, exception.RequestInformation.Exception);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using System;
using System;
using System.Collections;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Table;
using Xunit;

namespace CloudStub.Tests.TableBatchOperationTests
Expand Down Expand Up @@ -100,7 +99,7 @@ public async Task ExecuteBatchAsync_WithMultipleOperationsOnSameEntity_ThrowsExc
);

Assert.Equal("Element 1 in the batch returned an unexpected response code.", exception.Message);
Assert.Equal("Microsoft.WindowsAzure.Storage", exception.Source);
Assert.Equal("Microsoft.Azure.Cosmos.Table", exception.Source);
Assert.Null(exception.HelpLink);
Assert.Equal(-2146233088, exception.HResult);
Assert.Null(exception.InnerException);
Expand All @@ -111,15 +110,11 @@ public async Task ExecuteBatchAsync_WithMultipleOperationsOnSameEntity_ThrowsExc
Assert.Null(exception.RequestInformation.ErrorCode);
Assert.Null(exception.RequestInformation.Etag);

Assert.Equal("StorageException", exception.RequestInformation.ExceptionInfo.Type);
Assert.Equal("Element 1 in the batch returned an unexpected response code.", exception.RequestInformation.ExceptionInfo.Message);
Assert.Equal("Microsoft.WindowsAzure.Storage", exception.RequestInformation.ExceptionInfo.Source);
Assert.Equal("InvalidDuplicateRow", exception.RequestInformation.ExtendedErrorInformation.ErrorCode);
Assert.Matches(
@$"^1:The batch request contains multiple changes with same row key. An entity can appear only once in a batch request.\nRequestId:{exception.RequestInformation.ServiceRequestID}\nTime:\d{{4}}-\d{{2}}-\d{{2}}T\d{{2}}:\d{{2}}:\d{{2}}.\d{{7}}Z$",
exception.RequestInformation.ExtendedErrorInformation.ErrorMessage
);
Assert.Null(exception.RequestInformation.ExceptionInfo.InnerExceptionInfo);

Assert.Same(exception, exception.RequestInformation.Exception);
}
Expand Down
Loading

0 comments on commit 5a98e79

Please sign in to comment.