diff --git a/sdk/communication/Azure.Communication.Calling.Server/tests/ConversationClients/ConversationClientBaseTests.cs b/sdk/communication/Azure.Communication.Calling.Server/tests/ConversationClients/ConversationClientBaseTests.cs new file mode 100644 index 0000000000000..5726b52331a42 --- /dev/null +++ b/sdk/communication/Azure.Communication.Calling.Server/tests/ConversationClients/ConversationClientBaseTests.cs @@ -0,0 +1,44 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using Azure.Core; +using Azure.Core.TestFramework; + +namespace Azure.Communication.Calling.Server.Tests.ConversationClients +{ + public class ConversationClientBaseTests + { + private const string dummyAccessKey = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9+eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ+SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV+adQssw5c="; + public ConversationClient CreateMockConversationClient(int statusCode, object? content = null, HttpHeader[]? httpHeaders = null) + { + var uri = new Uri("https://acs.dummyresource.com"); + var communicationTokenCredential = + new AzureKeyCredential(dummyAccessKey); + var mockResponse = new MockResponse(statusCode); + if (content != null) + { + if (content.GetType() == typeof(string)) + mockResponse.SetContent((string)content); + else if (content.GetType() == typeof(byte[])) + mockResponse.SetContent((byte[])content); + } + + if (httpHeaders != null) + { + for (int i = 0; i < httpHeaders.Length; i++) + { + mockResponse.AddHeader(httpHeaders[i]); + } + } + + var callClientOptions = new CallClientOptions + { + Transport = new MockTransport(mockResponse) + }; + + var convClient = new ConversationClient(uri, communicationTokenCredential, callClientOptions); + return convClient; + } + } +} diff --git a/sdk/communication/Azure.Communication.Calling.Server/tests/ConversationClients/ConversationClientsTests.cs b/sdk/communication/Azure.Communication.Calling.Server/tests/ConversationClients/ConversationClientsTests.cs index 3a48a1fbdcf99..b93ae0ffa643d 100644 --- a/sdk/communication/Azure.Communication.Calling.Server/tests/ConversationClients/ConversationClientsTests.cs +++ b/sdk/communication/Azure.Communication.Calling.Server/tests/ConversationClients/ConversationClientsTests.cs @@ -4,156 +4,139 @@ using System; using System.Collections.Generic; using System.Linq.Expressions; -using System.Threading; +using System.Net; using System.Threading.Tasks; -using Moq; +using Azure.Communication.Calling.Server.Tests.ConversationClients; +using Azure.Core; +using Azure.Core.TestFramework; using NUnit.Framework; namespace Azure.Communication.Calling.Server.Tests { - public class ConversationClientsTests + public class ConversationClientsTests : ConversationClientBaseTests { + private const string DummyStartRecordingResponse = "{" + + "\"recordingId\": \"dummyRecordingId\"" + + "}"; + private const string DummyRecordingStateResponse = "{" + + "\"recordingState\": \"active\"" + + "}"; + + private const string DummyPlayAudioResponse = "{" + + "\"id\": \"dummyId\"," + + "\"status\": \"running\"," + + "\"operationContext\": \"dummyOperationContext\"," + + "\"resultInfo\": {" + + "\"code\": 200," + + "\"subcode\": 200," + + "\"message\": \"dummyMessage\"" + + "}" + + "}"; + [TestCaseSource(nameof(TestData_StartRecording))] - public async Task StartRecording_Passes(string expectedConversationId, Uri expectedCallbackUri) + public void StartRecording_Returns200Ok(string sampleConversationId, Uri sampleCallBackUri) { - Mock mockConversationClient = new Mock(); - Response? expectedResponse = default; - CancellationToken cancellationToken = new CancellationTokenSource().Token; - var callExpression = BuildExpression(x => x.StartRecordingAsync(It.IsAny(), It.IsAny(), It.IsAny())); - - mockConversationClient - .Setup(callExpression) - .ReturnsAsync((string conversationId, Uri callBackUri, CancellationToken token) => - { - Assert.AreEqual(expectedConversationId, conversationId); - Assert.AreEqual(expectedCallbackUri, callBackUri); - Assert.AreEqual(cancellationToken, token); - return expectedResponse = new Mock>().Object; - }); - - Response actualResponse = await mockConversationClient.Object.StartRecordingAsync(expectedConversationId, expectedCallbackUri, cancellationToken); + ConversationClient _convClient = CreateMockConversationClient(200, DummyStartRecordingResponse); + StartCallRecordingResponse response = _convClient.StartRecording(sampleConversationId, sampleCallBackUri); + Assert.AreEqual("dummyRecordingId", response.RecordingId); + } - mockConversationClient.Verify(callExpression, Times.Once()); - Assert.AreEqual(expectedResponse, actualResponse); + [TestCaseSource(nameof(TestData_StartRecording))] + public async Task StartRecordingAsync_Returns200Ok(string sampleConversationId, Uri sampleCallBackUri) + { + ConversationClient _convClient = CreateMockConversationClient(200, DummyStartRecordingResponse); + Response response = await _convClient.StartRecordingAsync(sampleConversationId, sampleCallBackUri); + Assert.AreEqual("dummyRecordingId", response.Value.RecordingId); } [TestCaseSource(nameof(TestData_StopRecording))] - public async Task StopRecording_Passes(string expectedConversationId, string expectedRecordingId) + public void StopRecording_Return200Ok(string sampleConversationId, string sampleRecordingId) { - Mock mockConversationClient = new Mock(); - Response? expectedResponse = default; - CancellationToken cancellationToken = new CancellationTokenSource().Token; - var callExpression = BuildExpression(x => x.StopRecordingAsync(It.IsAny(), It.IsAny(), It.IsAny())); - - mockConversationClient - .Setup(callExpression) - .ReturnsAsync((string conversationId, string recordingId, CancellationToken token) => - { - Assert.AreEqual(expectedConversationId, conversationId); - Assert.AreEqual(expectedRecordingId, recordingId); - return expectedResponse = new Mock().Object; - }); - - Response actualResponse = await mockConversationClient.Object.StopRecordingAsync(expectedConversationId, expectedRecordingId, cancellationToken); + ConversationClient _convClient = CreateMockConversationClient(200); + Response response = _convClient.StopRecording(sampleConversationId, sampleRecordingId); + var temp = response.Status; + Assert.AreEqual((int)HttpStatusCode.OK, response.Status); + } - mockConversationClient.Verify(callExpression, Times.Once()); - Assert.AreEqual(expectedResponse, actualResponse); + [TestCaseSource(nameof(TestData_StopRecording))] + public async Task StopRecordingAsync_Return200Ok(string sampleConversationId, string sampleRecordingId) + { + ConversationClient _convClient = CreateMockConversationClient(200); + Response response = await _convClient.StopRecordingAsync(sampleConversationId, sampleRecordingId); + Assert.AreEqual((int)HttpStatusCode.OK, response.Status); } [TestCaseSource(nameof(TestData_PauseRecording))] - public async Task PauseRecording_Passes(string expectedConversationId, string expectedRecordingId) + public void PauseRecording_Return200Ok(string sampleConversationId, string sampleRecordingId) { - Mock mockConversationClient = new Mock(); - Response? expectedResponse = default; - CancellationToken cancellationToken = new CancellationTokenSource().Token; - var callExpression = BuildExpression(x => x.PauseRecordingAsync(It.IsAny(), It.IsAny(), It.IsAny())); - - mockConversationClient - .Setup(callExpression) - .ReturnsAsync((string conversationId, string recordingId, CancellationToken token) => - { - Assert.AreEqual(expectedConversationId, conversationId); - Assert.AreEqual(expectedRecordingId, recordingId); - return expectedResponse = new Mock().Object; - }); - - Response actualResponse = await mockConversationClient.Object.PauseRecordingAsync(expectedConversationId, expectedRecordingId, cancellationToken); + ConversationClient _convClient = CreateMockConversationClient(200); + Response response = _convClient.PauseRecording(sampleConversationId, sampleRecordingId); + Assert.AreEqual((int)HttpStatusCode.OK, response.Status); + } - mockConversationClient.Verify(callExpression, Times.Once()); - Assert.AreEqual(expectedResponse, actualResponse); + [TestCaseSource(nameof(TestData_PauseRecording))] + public async Task PauseRecordingAsync_Return200Ok(string sampleConversationId, string sampleRecordingId) + { + ConversationClient _convClient = CreateMockConversationClient(200); + Response response = await _convClient.PauseRecordingAsync(sampleConversationId, sampleRecordingId); + Assert.AreEqual((int)HttpStatusCode.OK, response.Status); } [TestCaseSource(nameof(TestData_ResumeRecording))] - public async Task ResumeRecording_Passes(string expectedConversationId, string expectedRecordingId) + public void ResumeRecording_Return200Ok(string sampleConversationId, string sampleRecordingId) { - Mock mockConversationClient = new Mock(); - Response? expectedResponse = default; - CancellationToken cancellationToken = new CancellationTokenSource().Token; - var callExpression = BuildExpression(x => x.ResumeRecordingAsync(It.IsAny(), It.IsAny(), It.IsAny())); - - mockConversationClient - .Setup(callExpression) - .ReturnsAsync((string conversationId, string recordingId, CancellationToken token) => - { - Assert.AreEqual(expectedConversationId, conversationId); - Assert.AreEqual(expectedRecordingId, recordingId); - return expectedResponse = new Mock().Object; - }); - - Response actualResponse = await mockConversationClient.Object.ResumeRecordingAsync(expectedConversationId, expectedRecordingId, cancellationToken); + ConversationClient _convClient = CreateMockConversationClient(200); + Response response = _convClient.ResumeRecording(sampleConversationId, sampleRecordingId); + Assert.AreEqual((int)HttpStatusCode.OK, response.Status); + } - mockConversationClient.Verify(callExpression, Times.Once()); - Assert.AreEqual(expectedResponse, actualResponse); + [TestCaseSource(nameof(TestData_ResumeRecording))] + public async Task ResumeRecordingAsync_Return200Ok(string sampleConversationId, string sampleRecordingId) + { + ConversationClient _convClient = CreateMockConversationClient(200); + Response response = await _convClient.ResumeRecordingAsync(sampleConversationId, sampleRecordingId); + Assert.AreEqual((int)HttpStatusCode.OK, response.Status); } [TestCaseSource(nameof(TestData_GetRecordingState))] - public async Task GetRecordingState_Passes(string expectedConversationId, string expectedRecordingId) + public void GetRecordingState_Return200Ok(string sampleConversationId, string sampleRecordingId) { - Mock mockConversationClient = new Mock(); - Response? expectedResponse = default; - CancellationToken cancellationToken = new CancellationTokenSource().Token; - var callExpression = BuildExpression(x => x.GetRecordingStateAsync(It.IsAny(), It.IsAny(), It.IsAny())); - - mockConversationClient - .Setup(callExpression) - .ReturnsAsync((string conversationId, string recordingId, CancellationToken token) => - { - Assert.AreEqual(expectedConversationId, conversationId); - Assert.AreEqual(expectedRecordingId, recordingId); - return expectedResponse = new Mock>().Object; - }); - - Response actualResponse = await mockConversationClient.Object.GetRecordingStateAsync(expectedConversationId, expectedRecordingId, cancellationToken); - - mockConversationClient.Verify(callExpression, Times.Once()); - Assert.AreEqual(expectedResponse, actualResponse); + ConversationClient _convClient = CreateMockConversationClient(200, DummyRecordingStateResponse); + GetCallRecordingStateResponse response = _convClient.GetRecordingState(sampleConversationId, sampleRecordingId); + Assert.AreEqual(CallRecordingState.Active, response.RecordingState); } - [TestCaseSource(nameof(TestData_PlayAudioWithoutRequest))] - public async Task PlayAudioAsync_Passes(string expectedConversationId, Uri expectedAudioFileUri, string expectedAudioFileId, Uri expectedCallbackUri, string expectedOperationContext) + [TestCaseSource(nameof(TestData_GetRecordingState))] + public async Task GetRecordingStateAsync_Return200Ok(string sampleConversationId, string sampleRecordingId) { - Mock mockConversationClient = new Mock(); - Response? expectedResponse = default; - CancellationToken cancellationToken = new CancellationTokenSource().Token; - var callExpression = BuildExpression(x => x.PlayAudioAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())); + ConversationClient _convClient = CreateMockConversationClient(200, DummyRecordingStateResponse); + Response response = await _convClient.GetRecordingStateAsync(sampleConversationId, sampleRecordingId); + Assert.AreEqual(CallRecordingState.Active, response.Value.RecordingState); + } - mockConversationClient - .Setup(callExpression) - .ReturnsAsync((string conversationId, Uri audioFileUri, string audioFileId, Uri callbackUri, string operationContext, CancellationToken token) => - { - Assert.AreEqual(expectedConversationId, conversationId); - Assert.AreEqual(expectedAudioFileUri, audioFileUri); - Assert.AreEqual(expectedAudioFileId, audioFileId); - Assert.AreEqual(expectedCallbackUri, callbackUri); - Assert.AreEqual(expectedOperationContext, operationContext); - Assert.AreEqual(cancellationToken, token); - return expectedResponse = new Mock>().Object; - }); + [TestCaseSource(nameof(TestData_PlayAudio))] + public void PlayAudio_Return202Accepted(string sampleConversationId, Uri sampleAudioFileUri, string sampleAudioFileId, Uri sampleCallbackUri, string sampleOperationContext) + { + ConversationClient _convClient = CreateMockConversationClient(202, DummyPlayAudioResponse); + PlayAudioResponse response = _convClient.PlayAudio(sampleConversationId, sampleAudioFileUri, sampleAudioFileId, sampleCallbackUri, sampleOperationContext); + VerifyPlayAudioResponse(response); + } - Response actualResponse = await mockConversationClient.Object.PlayAudioAsync(expectedConversationId, expectedAudioFileUri, expectedAudioFileId, expectedCallbackUri, expectedOperationContext, cancellationToken); + [TestCaseSource(nameof(TestData_PlayAudio))] + public async Task PlayAudioAsync_Return202Accepted(string sampleConversationId, Uri sampleAudioFileUri, string sampleAudioFileId, Uri sampleCallbackUri, string sampleOperationContext) + { + ConversationClient _convClient = CreateMockConversationClient(202, DummyPlayAudioResponse); + Response response = await _convClient.PlayAudioAsync(sampleConversationId, sampleAudioFileUri, sampleAudioFileId, sampleCallbackUri, sampleOperationContext); + VerifyPlayAudioResponse(response); + } - mockConversationClient.Verify(callExpression, Times.Once()); - Assert.AreEqual(expectedResponse, actualResponse); + private void VerifyPlayAudioResponse(PlayAudioResponse response) + { + Assert.AreEqual("dummyId", response.Id); + Assert.AreEqual(OperationStatus.Running, response.Status); + Assert.AreEqual("dummyOperationContext", response.OperationContext); + Assert.AreEqual(200, response.ResultInfo.Code); + Assert.AreEqual("dummyMessage", response.ResultInfo.Message); } private static IEnumerable TestData_StartRecording() @@ -206,7 +189,7 @@ public async Task PlayAudioAsync_Passes(string expectedConversationId, Uri expec }; } - private static IEnumerable TestData_PlayAudioWithoutRequest() + private static IEnumerable TestData_PlayAudio() { return new List(){ new object?[] {