-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fa62ae6
commit b5e3b86
Showing
7 changed files
with
533 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
198 changes: 198 additions & 0 deletions
198
sdk/communication/Azure.Communication.Sms/tests/OptOutsClientTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Azure.Communication.Sms.Models; | ||
using Azure.Core.Pipeline; | ||
using Moq; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.Communication.Sms.Tests | ||
{ | ||
public class OptOutsClientTest | ||
{ | ||
[Test] | ||
public void OptOutsClient_ThrowsWithNullClientDiagnostics() | ||
{ | ||
var httpPipeline = HttpPipelineBuilder.Build(new SmsClientOptions()); | ||
var uri = new Uri("http://localhost"); | ||
|
||
Assert.Throws<ArgumentNullException>(() => new OptOutsClient(null, httpPipeline, uri)); | ||
} | ||
|
||
[Test] | ||
public void OptOutsClient_ThrowsWithNullPipeline() | ||
{ | ||
var clientDiagnostics = new ClientDiagnostics(new SmsClientOptions()); | ||
var uri = new Uri("http://localhost"); | ||
|
||
Assert.Throws<ArgumentNullException>(() => new OptOutsClient(clientDiagnostics, null, uri)); | ||
} | ||
|
||
[Test] | ||
public void OptOutsClient_ThrowsWithNullEndpoint() | ||
{ | ||
var clientDiagnostics = new ClientDiagnostics(new SmsClientOptions()); | ||
var httpPipeline = HttpPipelineBuilder.Build(new SmsClientOptions()); | ||
|
||
Assert.Throws<ArgumentNullException>(() => new OptOutsClient(clientDiagnostics, httpPipeline, null)); | ||
} | ||
|
||
[TestCaseSource(nameof(TestData))] | ||
public async Task CheckAsyncOverload_PassesToGeneratedOne(string expectedFrom, IEnumerable<string> expectedTo) | ||
{ | ||
Mock<OptOutsClient> mockClient = new Mock<OptOutsClient>() { CallBase = true }; | ||
Response<OptOutResponse>? expectedResponse = default; | ||
CancellationToken cancellationToken = new CancellationTokenSource().Token; | ||
var callExpression = BuildExpression(x => x.CheckAsync(It.IsAny<string>(), It.IsAny<IEnumerable<string>>(), It.IsAny<CancellationToken>())); | ||
|
||
mockClient | ||
.Setup(callExpression) | ||
.ReturnsAsync((string from, IEnumerable<string> to, CancellationToken token) => | ||
{ | ||
Assert.AreEqual(expectedFrom, from); | ||
Assert.AreEqual(expectedTo, to); | ||
Assert.AreEqual(cancellationToken, token); | ||
return expectedResponse = new Mock<Response<OptOutResponse>>().Object; | ||
}); | ||
|
||
Response<OptOutResponse> actualResponse = await mockClient.Object.CheckAsync(expectedFrom, expectedTo, cancellationToken); | ||
|
||
mockClient.Verify(callExpression, Times.Once()); | ||
Assert.AreEqual(expectedResponse, actualResponse); | ||
} | ||
|
||
[TestCaseSource(nameof(TestData))] | ||
public void CheckOverload_PassesToGeneratedOne(string expectedFrom, IEnumerable<string> expectedTo) | ||
{ | ||
Mock<OptOutsClient> mockClient = new Mock<OptOutsClient>() { CallBase = true }; | ||
Response<OptOutResponse>? expectedResponse = default; | ||
CancellationToken cancellationToken = new CancellationTokenSource().Token; | ||
var callExpression = BuildExpression(x => x.Check(It.IsAny<string>(), It.IsAny<IEnumerable<string>>(), It.IsAny<CancellationToken>())); | ||
|
||
mockClient | ||
.Setup(callExpression) | ||
.Returns((string from, IEnumerable<string> to, CancellationToken token) => | ||
{ | ||
Assert.AreEqual(expectedFrom, from); | ||
Assert.AreEqual(expectedTo, to); | ||
Assert.AreEqual(cancellationToken, token); | ||
return expectedResponse = new Mock<Response<OptOutResponse>>().Object; | ||
}); | ||
|
||
Response<OptOutResponse> actualResponse = mockClient.Object.Check(expectedFrom, expectedTo, cancellationToken); | ||
|
||
mockClient.Verify(callExpression, Times.Once()); | ||
Assert.AreEqual(expectedResponse, actualResponse); | ||
} | ||
|
||
[TestCaseSource(nameof(TestData))] | ||
public async Task AddAsyncOverload_PassesToGeneratedOne(string expectedFrom, IEnumerable<string> expectedTo) | ||
{ | ||
Mock<OptOutsClient> mockClient = new Mock<OptOutsClient>() { CallBase = true }; | ||
Response<OptOutChangeResponse>? expectedResponse = default; | ||
CancellationToken cancellationToken = new CancellationTokenSource().Token; | ||
var callExpression = BuildExpression(x => x.AddAsync(It.IsAny<string>(), It.IsAny<IEnumerable<string>>(), It.IsAny<CancellationToken>())); | ||
|
||
mockClient | ||
.Setup(callExpression) | ||
.ReturnsAsync((string from, IEnumerable<string> to, CancellationToken token) => | ||
{ | ||
Assert.AreEqual(expectedFrom, from); | ||
Assert.AreEqual(expectedTo, to); | ||
Assert.AreEqual(cancellationToken, token); | ||
return expectedResponse = new Mock<Response<OptOutChangeResponse>>().Object; | ||
}); | ||
|
||
Response<OptOutChangeResponse> actualResponse = await mockClient.Object.AddAsync(expectedFrom, expectedTo, cancellationToken); | ||
|
||
mockClient.Verify(callExpression, Times.Once()); | ||
Assert.AreEqual(expectedResponse, actualResponse); | ||
} | ||
|
||
[TestCaseSource(nameof(TestData))] | ||
public void AddOverload_PassesToGeneratedOne(string expectedFrom, IEnumerable<string> expectedTo) | ||
{ | ||
Mock<OptOutsClient> mockClient = new Mock<OptOutsClient>() { CallBase = true }; | ||
Response<OptOutChangeResponse>? expectedResponse = default; | ||
CancellationToken cancellationToken = new CancellationTokenSource().Token; | ||
var callExpression = BuildExpression(x => x.Add(It.IsAny<string>(), It.IsAny<IEnumerable<string>>(), It.IsAny<CancellationToken>())); | ||
|
||
mockClient | ||
.Setup(callExpression) | ||
.Returns((string from, IEnumerable<string> to, CancellationToken token) => | ||
{ | ||
Assert.AreEqual(expectedFrom, from); | ||
Assert.AreEqual(expectedTo, to); | ||
Assert.AreEqual(cancellationToken, token); | ||
return expectedResponse = new Mock<Response<OptOutChangeResponse>>().Object; | ||
}); | ||
|
||
Response<OptOutChangeResponse> actualResponse = mockClient.Object.Add(expectedFrom, expectedTo, cancellationToken); | ||
|
||
mockClient.Verify(callExpression, Times.Once()); | ||
Assert.AreEqual(expectedResponse, actualResponse); | ||
} | ||
|
||
[TestCaseSource(nameof(TestData))] | ||
public async Task RemoveAsyncOverload_PassesToGeneratedOne(string expectedFrom, IEnumerable<string> expectedTo) | ||
{ | ||
Mock<OptOutsClient> mockClient = new Mock<OptOutsClient>() { CallBase = true }; | ||
Response<OptOutChangeResponse>? expectedResponse = default; | ||
CancellationToken cancellationToken = new CancellationTokenSource().Token; | ||
var callExpression = BuildExpression(x => x.RemoveAsync(It.IsAny<string>(), It.IsAny<IEnumerable<string>>(), It.IsAny<CancellationToken>())); | ||
|
||
mockClient | ||
.Setup(callExpression) | ||
.ReturnsAsync((string from, IEnumerable<string> to, CancellationToken token) => | ||
{ | ||
Assert.AreEqual(expectedFrom, from); | ||
Assert.AreEqual(expectedTo, to); | ||
Assert.AreEqual(cancellationToken, token); | ||
return expectedResponse = new Mock<Response<OptOutChangeResponse>>().Object; | ||
}); | ||
|
||
Response<OptOutChangeResponse> actualResponse = await mockClient.Object.RemoveAsync(expectedFrom, expectedTo, cancellationToken); | ||
|
||
mockClient.Verify(callExpression, Times.Once()); | ||
Assert.AreEqual(expectedResponse, actualResponse); | ||
} | ||
|
||
[TestCaseSource(nameof(TestData))] | ||
public void RemoveOverload_PassesToGeneratedOne(string expectedFrom, IEnumerable<string> expectedTo) | ||
{ | ||
Mock<OptOutsClient> mockClient = new Mock<OptOutsClient>() { CallBase = true }; | ||
Response<OptOutChangeResponse>? expectedResponse = default; | ||
CancellationToken cancellationToken = new CancellationTokenSource().Token; | ||
var callExpression = BuildExpression(x => x.Remove(It.IsAny<string>(), It.IsAny<IEnumerable<string>>(), It.IsAny<CancellationToken>())); | ||
|
||
mockClient | ||
.Setup(callExpression) | ||
.Returns((string from, IEnumerable<string> to, CancellationToken token) => | ||
{ | ||
Assert.AreEqual(expectedFrom, from); | ||
Assert.AreEqual(expectedTo, to); | ||
Assert.AreEqual(cancellationToken, token); | ||
return expectedResponse = new Mock<Response<OptOutChangeResponse>>().Object; | ||
}); | ||
|
||
Response<OptOutChangeResponse> actualResponse = mockClient.Object.Remove(expectedFrom, expectedTo, cancellationToken); | ||
|
||
mockClient.Verify(callExpression, Times.Once()); | ||
Assert.AreEqual(expectedResponse, actualResponse); | ||
} | ||
|
||
private static IEnumerable<object> TestData() | ||
{ | ||
yield return new TestCaseData("+14255550123", new List<string> { "+14255550234" }); | ||
} | ||
|
||
private static Expression<Func<OptOutsClient, TResult>> BuildExpression<TResult>(Expression<Func<OptOutsClient, TResult>> expression) | ||
=> expression; | ||
} | ||
} |
Oops, something went wrong.