Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FarhadJabiyev committed Nov 26, 2024
1 parent fa62ae6 commit b5e3b86
Show file tree
Hide file tree
Showing 7 changed files with 533 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#nullable disable

namespace Azure.Communication.Sms.Models
{
/// <summary> Model factory for models. </summary>
public static partial class SmsModelFactory
/// <summary>
/// Model factory that enables mocking for the Sms library.
/// </summary>
public static class SmsModelFactory
{
/// <summary> Initializes a new instance of SmsSendResult. </summary>
/// <param name="to"> The recipient&apos;s phone number in E.164 format. </param>
Expand All @@ -15,6 +15,6 @@ public static partial class SmsModelFactory
/// <param name="successful"> Indicates if the message is processed successfully or not. </param>
/// <param name="errorMessage"> Optional error message in case of 4xx/5xx/repeatable errors. </param>
public static SmsSendResult SmsSendResult(string to, string messageId, int httpStatusCode, bool successful, string errorMessage)
=> new SmsSendResult(to, messageId, httpStatusCode, SmsSendResponseItemRepeatabilityResult.Accepted, successful, errorMessage);
=> new(to, messageId, httpStatusCode, SmsSendResponseItemRepeatabilityResult.Accepted, successful, errorMessage);
}
}
19 changes: 12 additions & 7 deletions sdk/communication/Azure.Communication.Sms/src/OptOutsClient.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Azure.Communication.Sms.Models;
using Azure.Core.Pipeline;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Azure.Communication.Sms.Models;
using Azure.Core.Pipeline;

namespace Azure.Communication.Sms
{
Expand All @@ -20,9 +20,14 @@ public class OptOutsClient

internal OptOutsRestClient OptOutsRestClient;

internal OptOutsClient(OptOutsRestClient optOutsRestClient, ClientDiagnostics clientDiagnostics)
internal OptOutsClient(ClientDiagnostics clientDiagnostics, HttpPipeline pipeline, Uri endpoint, string apiVersion = "2024-12-10-preview")
{
OptOutsRestClient = optOutsRestClient;
Argument.CheckNotNull(clientDiagnostics, nameof(clientDiagnostics));
Argument.CheckNotNull(pipeline, nameof(pipeline));
Argument.CheckNotNull(endpoint, nameof(endpoint));
Argument.CheckNotNull(apiVersion, nameof(apiVersion));

OptOutsRestClient = new OptOutsRestClient(clientDiagnostics, pipeline, endpoint, apiVersion);
_clientDiagnostics = clientDiagnostics;
}

Expand Down
2 changes: 1 addition & 1 deletion sdk/communication/Azure.Communication.Sms/src/SmsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private SmsClient(string endpoint, HttpPipeline httpPipeline, SmsClientOptions o
{
_clientDiagnostics = new ClientDiagnostics(options);
RestClient = new SmsRestClient(_clientDiagnostics, httpPipeline, new Uri(endpoint), options.ApiVersion);
OptOuts = new OptOutsClient(new OptOutsRestClient(_clientDiagnostics, httpPipeline, new Uri(endpoint), options.ApiVersion), _clientDiagnostics);
OptOuts = new OptOutsClient(_clientDiagnostics, httpPipeline, new Uri(endpoint), options.ApiVersion);
}

#endregion
Expand Down
198 changes: 198 additions & 0 deletions sdk/communication/Azure.Communication.Sms/tests/OptOutsClientTest.cs
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;
}
}
Loading

0 comments on commit b5e3b86

Please sign in to comment.