From e5218d60b08a8fc1020238fc833adca349d07cec Mon Sep 17 00:00:00 2001 From: tr00d Date: Tue, 3 Sep 2024 09:07:47 +0200 Subject: [PATCH] refactor: clean messages tests --- Vonage.Test/Messages/Rcs/RcsMessagesTest.cs | 3 ++- .../Messages/WhatsApp/WhatsAppMessagesTest.cs | 3 ++- Vonage.Test/TestBase.cs | 21 +++++++++++-------- Vonage.Test/VoiceTests.cs | 11 ++++++---- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/Vonage.Test/Messages/Rcs/RcsMessagesTest.cs b/Vonage.Test/Messages/Rcs/RcsMessagesTest.cs index 68c31b355..5c23e5c7a 100644 --- a/Vonage.Test/Messages/Rcs/RcsMessagesTest.cs +++ b/Vonage.Test/Messages/Rcs/RcsMessagesTest.cs @@ -1,6 +1,7 @@ #region using System; using System.Threading.Tasks; +using Vonage.Common.Monads; using Vonage.Messages; using Vonage.Messages.Rcs; using Vonage.Request; @@ -107,7 +108,7 @@ public async Task SendRcsVideoAsyncReturnsOk() [Fact] public async Task UpdateAsyncReturnsOk() { - this.Setup(this.expectedUri, "", this.helper.GetRequestJson()); + this.Setup(this.expectedUri, Maybe.None, this.helper.GetRequestJson()); await this.client.MessagesClient.UpdateAsync(RcsUpdateMessageRequest.Build("ID-123")); } diff --git a/Vonage.Test/Messages/WhatsApp/WhatsAppMessagesTest.cs b/Vonage.Test/Messages/WhatsApp/WhatsAppMessagesTest.cs index 44bbfa52b..76ad7c414 100644 --- a/Vonage.Test/Messages/WhatsApp/WhatsAppMessagesTest.cs +++ b/Vonage.Test/Messages/WhatsApp/WhatsAppMessagesTest.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using FluentAssertions; +using Vonage.Common.Monads; using Vonage.Messages; using Vonage.Messages.WhatsApp; using Vonage.Messages.WhatsApp.ProductMessages.MultipleItems; @@ -515,7 +516,7 @@ public async Task SendWhatsAppVideoAsyncReturnsOkWithContext() [Fact] public async Task UpdateAsyncReturnsOk() { - this.Setup(this.expectedUri, "", this.helper.GetRequestJson()); + this.Setup(this.expectedUri, Maybe.None, this.helper.GetRequestJson()); await this.BuildVonageClient(Credentials.FromAppIdAndPrivateKey(this.AppId, this.PrivateKey)) .MessagesClient.UpdateAsync(WhatsAppUpdateMessageRequest.Build("ID-123")); } diff --git a/Vonage.Test/TestBase.cs b/Vonage.Test/TestBase.cs index c89158ebf..1beab8983 100644 --- a/Vonage.Test/TestBase.cs +++ b/Vonage.Test/TestBase.cs @@ -1,4 +1,5 @@ -using System; +#region +using System; using System.Collections.Generic; using System.IO; using System.Net; @@ -12,10 +13,12 @@ using Moq; using Moq.Protected; using Vonage.Common; +using Vonage.Common.Monads; using Vonage.Request; using Vonage.Test.Common.TestHelpers; using Xunit; using TimeProvider = Vonage.Common.TimeProvider; +#endregion namespace Vonage.Test { @@ -73,17 +76,21 @@ private static string GetAssemblyDirectory() return Path.GetDirectoryName(path); } - protected void Setup(string uri, string responseContent, string requestContent = null, + protected void Setup(string uri, Maybe responseContent, string requestContent = null, HttpStatusCode expectedCode = HttpStatusCode.OK) => - this.Setup(uri, new StringContent(responseContent, Encoding.UTF8, "application/json"), expectedCode, + this.Setup(uri, + responseContent.Map(content => + new StringContent(content, Encoding.UTF8, "application/json")), expectedCode, requestContent); protected void Setup(string uri, byte[] responseContent, HttpStatusCode expectedCode = HttpStatusCode.OK) => this.Setup(uri, new StreamContent(new MemoryStream(responseContent)), expectedCode); - private void Setup(string uri, HttpContent httpContent, HttpStatusCode expectedCode, + private void Setup(string uri, Maybe httpContent, HttpStatusCode expectedCode, string requestContent = null) { + var expectedResponse = new HttpResponseMessage(expectedCode); + httpContent.IfSome(some => expectedResponse.Content = some); var mockHandler = new Mock(MockBehavior.Strict); mockHandler .Protected() @@ -99,11 +106,7 @@ private void Setup(string uri, HttpContent httpContent, HttpStatusCode expectedC var actualContent = actualHttpRequestMessage.Content.ReadAsStringAsync().Result; Assert.Equal(requestContent, actualContent, StringComparer.OrdinalIgnoreCase); }) - .ReturnsAsync(new HttpResponseMessage - { - StatusCode = expectedCode, - Content = httpContent, - }) + .ReturnsAsync(expectedResponse) .Verifiable(); this.configuration.ClientHandler = mockHandler.Object; } diff --git a/Vonage.Test/VoiceTests.cs b/Vonage.Test/VoiceTests.cs index fe288fd29..439ec9d6d 100644 --- a/Vonage.Test/VoiceTests.cs +++ b/Vonage.Test/VoiceTests.cs @@ -1,14 +1,17 @@ -using System; +#region +using System; using System.Globalization; using System.Net; using System.Threading.Tasks; using AutoFixture; using FluentAssertions; using Vonage.Common.Exceptions; +using Vonage.Common.Monads; using Vonage.Voice; using Vonage.Voice.Nccos; using Vonage.Voice.Nccos.Endpoints; using Xunit; +#endregion namespace Vonage.Test; @@ -456,7 +459,7 @@ public async Task UpdateCallAsync() { var uuid = this.fixture.Create().ToString(); var request = new CallEditCommand {Action = CallEditCommand.ActionType.earmuff}; - this.Setup($"{BaseUri}/{uuid}", string.Empty, this.GetRequestJson()); + this.Setup($"{BaseUri}/{uuid}", Maybe.None, this.GetRequestJson()); var response = await this.client.VoiceClient.UpdateCallAsync(uuid, request); Assert.True(response); } @@ -470,7 +473,7 @@ public async Task UpdateCallWithCredentials() Destination = new Destination {Type = "ncco", Url = new[] {"https://example.com/ncco.json"}}, Action = CallEditCommand.ActionType.transfer, }; - this.Setup($"{BaseUri}/{uuid}", string.Empty, this.GetRequestJson()); + this.Setup($"{BaseUri}/{uuid}", Maybe.None, this.GetRequestJson()); var response = await this.client.VoiceClient.UpdateCallAsync(uuid, request, this.BuildCredentialsForBearerAuthentication()); @@ -486,7 +489,7 @@ public async Task UpdateCallWithInlineNcco() Destination = new Destination {Type = "ncco", Ncco = new Ncco(new TalkAction {Text = "hello world"})}, Action = CallEditCommand.ActionType.transfer, }; - this.Setup($"{BaseUri}/{uuid}", string.Empty, this.GetRequestJson()); + this.Setup($"{BaseUri}/{uuid}", Maybe.None, this.GetRequestJson()); Assert.True(await this.client.VoiceClient.UpdateCallAsync(uuid, request)); }