Skip to content

Commit

Permalink
update test classes to use new pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
m-nash committed Apr 30, 2021
1 parent ec173e7 commit 5a9fff2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 36 deletions.
46 changes: 21 additions & 25 deletions sdk/core/Azure.Core/tests/TestClients/ArmOperationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,65 +11,61 @@

namespace Azure.Core.Tests
{
public class ArmOperationTest<T> : ArmOperation<T>
where T : class
public class ArmOperationTest : ArmOperation<TestResource>, IOperationSource<TestResource>
{
private T _value;
private TestResource _value;
private bool _exceptionOnWait;
private OperationOrResponseInternals<TestResource> _operationHelper;

protected ArmOperationTest()
{
}

public ArmOperationTest(T value, bool exceptionOnWait = false)
public ArmOperationTest(TestResource value, bool exceptionOnWait = false)
{
_value = value;
_exceptionOnWait = exceptionOnWait;
_operationHelper = new OperationOrResponseInternals<TestResource>(Response.FromValue(value, null));
}

public override string Id => "testId";

public override T Value => _value;
public override TestResource Value => _operationHelper.Value;

public override bool HasCompleted => true;
public override bool HasCompleted => _operationHelper.HasCompleted;

public override bool HasValue => true;
public override bool HasValue => _operationHelper.HasValue;

public override Response GetRawResponse()
{
return Response.FromValue(_value, null) as Response;
}
public override Response GetRawResponse() => _operationHelper.GetRawResponse();

public override ValueTask<Response<T>> WaitForCompletionAsync(CancellationToken cancellationToken = default)
public override ValueTask<Response<TestResource>> WaitForCompletionAsync(CancellationToken cancellationToken = default)
{
if (_exceptionOnWait)
throw new ArgumentException("FakeArg");

return new ValueTask<Response<T>>(Response.FromValue(_value, null));
return new ValueTask<Response<TestResource>>(Response.FromValue(_value, null));
}

public override ValueTask<Response<T>> WaitForCompletionAsync(TimeSpan pollingInterval, CancellationToken cancellationToken)
public override ValueTask<Response<TestResource>> WaitForCompletionAsync(TimeSpan pollingInterval, CancellationToken cancellationToken)
{
if (_exceptionOnWait)
throw new ArgumentException("FakeArg");

return new ValueTask<Response<T>>(Response.FromValue(_value, null));
return new ValueTask<Response<TestResource>>(Response.FromValue(_value, null));
}

public override T CreateResult(Response response, CancellationToken cancellationToken)
{
if (_exceptionOnWait)
throw new ArgumentException("FakeArg");
public override ValueTask<Response> UpdateStatusAsync(CancellationToken cancellationToken = default) => _operationHelper.UpdateStatusAsync(cancellationToken);

return new ValueTask<Response>(Response.FromValue(_value, null) as Response);
}
public override Response UpdateStatus(CancellationToken cancellationToken = default) => _operationHelper.UpdateStatus(cancellationToken);

public async override ValueTask<T> CreateResultAsync(Response response, CancellationToken cancellationToken)
public TestResource CreateResult(Response response, CancellationToken cancellationToken)
{
if (_exceptionOnWait)
throw new ArgumentException("FakeArg");
return _value;
}

return Response.FromValue(_value, null) as Response;
public ValueTask<TestResource> CreateResultAsync(Response response, CancellationToken cancellationToken)
{
return new ValueTask<TestResource>(_value);
}
}
}
30 changes: 28 additions & 2 deletions sdk/core/Azure.Core/tests/TestClients/PhArmOperationTest.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Threading;
using System.Threading.Tasks;
using Azure.ResourceManager.Core;

namespace Azure.Core.Tests
{
public class PhArmOperationTest<T> : ArmOperationTest<T>
public class PhArmOperationTest<T> : ArmOperation<T>
where T : class
{
private OperationOrResponseInternals<T> _operationHelper;

public override T Value => _operationHelper.Value;

public override bool HasValue => _operationHelper.HasValue;

public override string Id => "MyId";

public override bool HasCompleted => _operationHelper.HasCompleted;

protected PhArmOperationTest()
{
}

public PhArmOperationTest(T value) : base(value)
public PhArmOperationTest(T value)
{
_operationHelper = new OperationOrResponseInternals<T>(Response.FromValue(value, null));
}

public override ValueTask<Response<T>> WaitForCompletionAsync(CancellationToken cancellationToken = default) => _operationHelper.WaitForCompletionAsync(cancellationToken);

public override ValueTask<Response<T>> WaitForCompletionAsync(TimeSpan pollingInterval, CancellationToken cancellationToken) => _operationHelper.WaitForCompletionAsync(pollingInterval, cancellationToken);

public override Response GetRawResponse() => _operationHelper.GetRawResponse();

public override ValueTask<Response> UpdateStatusAsync(CancellationToken cancellationToken = default) => _operationHelper.UpdateStatusAsync(cancellationToken);

public override Response UpdateStatus(CancellationToken cancellationToken = default) => _operationHelper.UpdateStatus(cancellationToken);
}
}
19 changes: 10 additions & 9 deletions sdk/core/Azure.Core/tests/TestClients/TestResourceOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading;
using System.Threading.Tasks;
using Azure.Core.Pipeline;
using Azure.ResourceManager.Core;

namespace Azure.Core.Tests
{
Expand All @@ -17,14 +18,14 @@ public virtual TestResourceOperations GetAnotherOperations()
return new TestResource();
}

public virtual ArmOperationTest<TestResource> GetArmOperation(bool exceptionOnWait = false, CancellationToken cancellationToken = default)
public virtual ArmOperationTest GetArmOperation(bool exceptionOnWait = false, CancellationToken cancellationToken = default)
{
using var scope = _diagnostic.CreateScope("TestResourceOperations.GetArmOperation");
scope.Start();

try
{
return new ArmOperationTest<TestResource>(new TestResource(), exceptionOnWait);
return new ArmOperationTest(new TestResource(), exceptionOnWait);
}
catch (Exception e)
{
Expand All @@ -33,14 +34,14 @@ public virtual ArmOperationTest<TestResource> GetArmOperation(bool exceptionOnWa
}
}

public virtual Task<ArmOperationTest<TestResource>> GetArmOperationAsync(bool exceptionOnWait = false, CancellationToken cancellationToken = default)
public virtual Task<ArmOperationTest> GetArmOperationAsync(bool exceptionOnWait = false, CancellationToken cancellationToken = default)
{
using var scope = _diagnostic.CreateScope("TestResourceOperations.GetArmOperation");
scope.Start();

try
{
return Task.FromResult(new ArmOperationTest<TestResource>(new TestResource(), exceptionOnWait));
return Task.FromResult(new ArmOperationTest(new TestResource(), exceptionOnWait));
}
catch (Exception e)
{
Expand Down Expand Up @@ -81,7 +82,7 @@ public virtual Task<ArmResponseTest<TestResource>> GetArmResponseAsync(Cancellat
}
}

public virtual ArmOperationTest<TestResource> GetPhArmOperation(CancellationToken cancellationToken = default)
public virtual ArmOperation<TestResource> GetPhArmOperation(CancellationToken cancellationToken = default)
{
using var scope = _diagnostic.CreateScope("TestResourceOperations.GetPhArmOperation");
scope.Start();
Expand All @@ -97,14 +98,14 @@ public virtual ArmOperationTest<TestResource> GetPhArmOperation(CancellationToke
}
}

public virtual Task<ArmOperationTest<TestResource>> GetPhArmOperationAsync(CancellationToken cancellationToken = default)
public virtual Task<ArmOperation<TestResource>> GetPhArmOperationAsync(CancellationToken cancellationToken = default)
{
using var scope = _diagnostic.CreateScope("TestResourceOperations.GetPhArmOperation");
scope.Start();

try
{
return Task.FromResult<ArmOperationTest<TestResource>>(new PhArmOperationTest<TestResource>(new TestResource()));
return Task.FromResult<ArmOperation<TestResource>>(new PhArmOperationTest<TestResource>(new TestResource()));
}
catch (Exception e)
{
Expand Down Expand Up @@ -177,7 +178,7 @@ public virtual Task<ArmResponseTest<TestResource>> GetArmResponseExceptionAsync(
}
}

public virtual ArmOperationTest<TestResource> GetArmOperationException(CancellationToken cancellationToken = default)
public virtual ArmOperationTest GetArmOperationException(CancellationToken cancellationToken = default)
{
using var scope = _diagnostic.CreateScope("TestResourceOperations.GetArmOperationException");
scope.Start();
Expand All @@ -193,7 +194,7 @@ public virtual ArmOperationTest<TestResource> GetArmOperationException(Cancellat
}
}

public virtual Task<ArmOperationTest<TestResource>> GetArmOperationExceptionAsync(CancellationToken cancellationToken = default)
public virtual Task<ArmOperationTest> GetArmOperationExceptionAsync(CancellationToken cancellationToken = default)
{
using var scope = _diagnostic.CreateScope("TestResourceOperations.GetArmOperationException");
scope.Start();
Expand Down

0 comments on commit 5a9fff2

Please sign in to comment.