From d9bc6cbf1ae3f1ed34d9fe3691dbe6c30f417ab1 Mon Sep 17 00:00:00 2001 From: Stuart Ferguson Date: Wed, 11 Dec 2024 14:17:23 +0000 Subject: [PATCH] added better code coverage --- .../Mediator/MediatorTests.cs | 1 + .../Services/MessagingDomainServiceTests.cs | 128 ++++++++++++++++++ .../Requests/EmailAttachment.cs | 5 +- .../Requests/EmailCommands.cs | 2 + .../Requests/SMSCommands.cs | 2 + .../EmailAggregateTests.cs | 7 + ...ngService.EmailMessage.DomainEvents.csproj | 1 + .../EmailAggregate.cs | 5 + .../MessageRecipient.cs | 6 +- .../MessagingService.Models.csproj | 1 + ...gingService.SMSMessage.DomainEvents.csproj | 1 + MessagingService.Testing/TestData.cs | 2 +- .../Bootstrapper/MiddlewareRegistry.cs | 28 +--- 13 files changed, 159 insertions(+), 30 deletions(-) diff --git a/MessagingService.BusinessLogic.Tests/Mediator/MediatorTests.cs b/MessagingService.BusinessLogic.Tests/Mediator/MediatorTests.cs index bce0a6b..cc49192 100644 --- a/MessagingService.BusinessLogic.Tests/Mediator/MediatorTests.cs +++ b/MessagingService.BusinessLogic.Tests/Mediator/MediatorTests.cs @@ -27,6 +27,7 @@ public MediatorTests() { this.Requests.Add(TestData.SendEmailCommand); this.Requests.Add(TestData.SendSMSCommand); this.Requests.Add(TestData.ResendSMSCommand); + this.Requests.Add(TestData.SendEmailCommandWithAttachments); } [Fact] diff --git a/MessagingService.BusinessLogic.Tests/Services/MessagingDomainServiceTests.cs b/MessagingService.BusinessLogic.Tests/Services/MessagingDomainServiceTests.cs index b27a74e..f743bd4 100644 --- a/MessagingService.BusinessLogic.Tests/Services/MessagingDomainServiceTests.cs +++ b/MessagingService.BusinessLogic.Tests/Services/MessagingDomainServiceTests.cs @@ -14,6 +14,8 @@ namespace MessagingService.BusinessLogic.Tests.Services using Moq; using Shared.DomainDrivenDesign.EventSourcing; using Shared.EventStore.Aggregate; + using Shouldly; + using SimpleResults; using SMSMessageAggregate; using Testing; using Xunit; @@ -25,6 +27,39 @@ public async Task MessagingDomainService_SendEmailMessage_MessageSent() { Mock> emailAggregateRepository = new Mock>(); emailAggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.GetEmptyEmailAggregate()); + emailAggregateRepository.Setup(a => a.SaveChanges(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success); + Mock> smsAggregateRepository = new Mock>(); + Mock emailServiceProxy = new Mock(); + emailServiceProxy + .Setup(e => e.SendEmail(It.IsAny(), + It.IsAny(), + It.IsAny>(), + It.IsAny(), + It.IsAny(), + It.IsAny(), + It.IsAny>(), + It.IsAny())).ReturnsAsync(TestData.SuccessfulEmailServiceProxyResponse); + Mock smsServiceProxy = new Mock(); + + MessagingDomainService messagingDomainService = + new MessagingDomainService(emailAggregateRepository.Object, smsAggregateRepository.Object, emailServiceProxy.Object, smsServiceProxy.Object); + + await messagingDomainService.SendEmailMessage(TestData.ConnectionIdentifier, + TestData.MessageId, + TestData.FromAddress, + TestData.ToAddresses, + TestData.Subject, + TestData.Body, + TestData.IsHtmlTrue, + TestData.EmailAttachmentModels, + CancellationToken.None); + } + + [Fact] public async Task MessagingDomainService_SendEmailMessage_SaveFailed_MessageSent() + { + Mock> emailAggregateRepository = new Mock>(); + emailAggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.GetEmptyEmailAggregate()); + emailAggregateRepository.Setup(a => a.SaveChanges(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure); Mock> smsAggregateRepository = new Mock>(); Mock emailServiceProxy = new Mock(); emailServiceProxy @@ -200,6 +235,7 @@ public async Task MessagingDomainService_SendSMSMessage_MessageSent() Mock> emailAggregateRepository = new Mock>(); Mock> smsAggregateRepository = new Mock>(); smsAggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.GetEmptySMSAggregate()); + smsAggregateRepository.Setup(a => a.SaveChanges(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success()); Mock emailServiceProxy = new Mock(); Mock smsServiceProxy = new Mock(); smsServiceProxy @@ -219,6 +255,32 @@ await messagingDomainService.SendSMSMessage(TestData.ConnectionIdentifier, CancellationToken.None); } + [Fact] + public async Task MessagingDomainService_SendSMSMessage_SaveFailed_MessageSent() + { + Mock> emailAggregateRepository = new Mock>(); + Mock> smsAggregateRepository = new Mock>(); + smsAggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny(), It.IsAny())).ReturnsAsync(TestData.GetEmptySMSAggregate()); + smsAggregateRepository.Setup(a => a.SaveChanges(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure); + Mock emailServiceProxy = new Mock(); + Mock smsServiceProxy = new Mock(); + smsServiceProxy + .Setup(e => e.SendSMS(It.IsAny(), + It.IsAny(), + It.IsAny(), + It.IsAny(), + It.IsAny())).ReturnsAsync(TestData.SuccessfulSMSServiceProxyResponse); + MessagingDomainService messagingDomainService = + new MessagingDomainService(emailAggregateRepository.Object, smsAggregateRepository.Object, emailServiceProxy.Object, smsServiceProxy.Object); + + await messagingDomainService.SendSMSMessage(TestData.ConnectionIdentifier, + TestData.MessageId, + TestData.Sender, + TestData.Destination, + TestData.Message, + CancellationToken.None); + } + [Fact] public async Task MessagingDomainService_ReSendSMSMessage_MessageSent() { @@ -286,4 +348,70 @@ await messagingDomainService.ResendSMSMessage(TestData.ConnectionIdentifier, } } + + public class DomainServiceHelperTests + { + [Fact] + public void DomainServiceHelper_HandleGetAggregateResult_SuccessfulGet_ResultHandled() + { + Guid aggregateId = Guid.Parse("0639682D-1D28-4AD8-B29D-4B76619083F1"); + Result result = Result.Success(new TestAggregate + { + AggregateId = aggregateId + }); + + var handleResult = DomainServiceHelper.HandleGetAggregateResult(result, aggregateId, true); + handleResult.IsSuccess.ShouldBeTrue(); + handleResult.Data.ShouldBeOfType(typeof(TestAggregate)); + handleResult.Data.AggregateId.ShouldBe(aggregateId); + } + + [Fact] + public void DomainServiceHelper_HandleGetAggregateResult_FailedGet_ResultHandled() + { + Guid aggregateId = Guid.Parse("0639682D-1D28-4AD8-B29D-4B76619083F1"); + Result result = Result.Failure("Failed Get"); + + var handleResult = DomainServiceHelper.HandleGetAggregateResult(result, aggregateId, true); + handleResult.IsFailed.ShouldBeTrue(); + handleResult.Message.ShouldBe("Failed Get"); + } + + [Fact] + public void DomainServiceHelper_HandleGetAggregateResult_FailedGet_NotFoundButIsError_ResultHandled() + { + Guid aggregateId = Guid.Parse("0639682D-1D28-4AD8-B29D-4B76619083F1"); + Result result = Result.NotFound("Failed Get"); + + var handleResult = DomainServiceHelper.HandleGetAggregateResult(result, aggregateId, true); + handleResult.IsFailed.ShouldBeTrue(); + handleResult.Message.ShouldBe("Failed Get"); + } + + [Fact] + public void DomainServiceHelper_HandleGetAggregateResult_FailedGet_NotFoundButIsNotError_ResultHandled() + { + Guid aggregateId = Guid.Parse("0639682D-1D28-4AD8-B29D-4B76619083F1"); + Result result = Result.NotFound("Failed Get"); + + var handleResult = DomainServiceHelper.HandleGetAggregateResult(result, aggregateId, false); + handleResult.IsSuccess.ShouldBeTrue(); + handleResult.Data.ShouldBeOfType(typeof(TestAggregate)); + handleResult.Data.AggregateId.ShouldBe(aggregateId); + } + } + + public record TestAggregate : Aggregate + { + public override void PlayEvent(IDomainEvent domainEvent) + { + + } + + protected override Object GetMetadata() + { + return new Object(); + } + } + } diff --git a/MessagingService.BusinessLogic/Requests/EmailAttachment.cs b/MessagingService.BusinessLogic/Requests/EmailAttachment.cs index 30281e4..167cdd8 100644 --- a/MessagingService.BusinessLogic/Requests/EmailAttachment.cs +++ b/MessagingService.BusinessLogic/Requests/EmailAttachment.cs @@ -1,7 +1,10 @@ -namespace MessagingService.BusinessLogic.Requests; +using System.Diagnostics.CodeAnalysis; + +namespace MessagingService.BusinessLogic.Requests; using System; +[ExcludeFromCodeCoverage] public class EmailAttachment { public String Filename { get; set; } diff --git a/MessagingService.BusinessLogic/Requests/EmailCommands.cs b/MessagingService.BusinessLogic/Requests/EmailCommands.cs index 9924415..11607e5 100644 --- a/MessagingService.BusinessLogic/Requests/EmailCommands.cs +++ b/MessagingService.BusinessLogic/Requests/EmailCommands.cs @@ -1,11 +1,13 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using MediatR; using MessagingService.BusinessLogic.Services.EmailServices; using SimpleResults; namespace MessagingService.BusinessLogic.Requests; +[ExcludeFromCodeCoverage] public record EmailCommands { public record SendEmailCommand(Guid ConnectionIdentifier, Guid MessageId, diff --git a/MessagingService.BusinessLogic/Requests/SMSCommands.cs b/MessagingService.BusinessLogic/Requests/SMSCommands.cs index 86a8130..ac7eef0 100644 --- a/MessagingService.BusinessLogic/Requests/SMSCommands.cs +++ b/MessagingService.BusinessLogic/Requests/SMSCommands.cs @@ -1,10 +1,12 @@ using System; +using System.Diagnostics.CodeAnalysis; using MediatR; using MessagingService.BusinessLogic.Services.SMSServices; using SimpleResults; namespace MessagingService.BusinessLogic.Requests; +[ExcludeFromCodeCoverage] public record SMSCommands { public record SendSMSCommand(Guid ConnectionIdentifier, Guid MessageId, diff --git a/MessagingService.EmailAggregate.Tests/EmailAggregateTests.cs b/MessagingService.EmailAggregate.Tests/EmailAggregateTests.cs index 5f7709f..241e3a6 100644 --- a/MessagingService.EmailAggregate.Tests/EmailAggregateTests.cs +++ b/MessagingService.EmailAggregate.Tests/EmailAggregateTests.cs @@ -1,3 +1,4 @@ +using System.Linq; using Xunit; namespace MessagingService.EmailAggregate.Tests @@ -35,6 +36,12 @@ public void EmailAggregate_SendRequestToProvider_RequestSent() { toAddresses.Count.ShouldBe(TestData.ToAddresses.Count); List attachments = emailAggregate.GetAttachments(); attachments.Count.ShouldBe(TestData.EmailAttachmentModels.Count); + var recipients = emailAggregate.GetRecipients(); + recipients.Count.ShouldBe(TestData.ToAddresses.Count); + foreach (var toAddress in TestData.ToAddresses) { + var r = recipients.SingleOrDefault(r => r.ToAddress == toAddress); + r.ShouldNotBeNull(); + } } [Fact] diff --git a/MessagingService.EmailMessage.DomainEvents/MessagingService.EmailMessage.DomainEvents.csproj b/MessagingService.EmailMessage.DomainEvents/MessagingService.EmailMessage.DomainEvents.csproj index 5412d72..a940328 100644 --- a/MessagingService.EmailMessage.DomainEvents/MessagingService.EmailMessage.DomainEvents.csproj +++ b/MessagingService.EmailMessage.DomainEvents/MessagingService.EmailMessage.DomainEvents.csproj @@ -2,6 +2,7 @@ net8.0 + None diff --git a/MessagingService.EmailMessageAggregate/EmailAggregate.cs b/MessagingService.EmailMessageAggregate/EmailAggregate.cs index 61ec596..b395352 100644 --- a/MessagingService.EmailMessageAggregate/EmailAggregate.cs +++ b/MessagingService.EmailMessageAggregate/EmailAggregate.cs @@ -252,6 +252,11 @@ public static List GetAttachments(this EmailAggregate aggregate return aggregate.Attachments; } + public static List GetRecipients(this EmailAggregate aggregate) + { + return aggregate.Recipients; + } + } public record EmailAggregate : Aggregate diff --git a/MessagingService.EmailMessageAggregate/MessageRecipient.cs b/MessagingService.EmailMessageAggregate/MessageRecipient.cs index 4bb0a98..82e590c 100644 --- a/MessagingService.EmailMessageAggregate/MessageRecipient.cs +++ b/MessagingService.EmailMessageAggregate/MessageRecipient.cs @@ -5,7 +5,7 @@ /// /// /// - internal class MessageRecipient + public class MessageRecipient { #region Constructors @@ -19,7 +19,7 @@ internal class MessageRecipient /// /// To address. /// - internal String ToAddress { get; private set; } + public String ToAddress { get; private set; } #endregion @@ -29,7 +29,7 @@ internal class MessageRecipient /// Creates the specified to address. /// /// To address. - internal void Create(String toAddress) + public void Create(String toAddress) { this.ToAddress = toAddress; } diff --git a/MessagingService.Models/MessagingService.Models.csproj b/MessagingService.Models/MessagingService.Models.csproj index 30402ac..bf8ca4e 100644 --- a/MessagingService.Models/MessagingService.Models.csproj +++ b/MessagingService.Models/MessagingService.Models.csproj @@ -4,6 +4,7 @@ net8.0 enable enable + None diff --git a/MessagingService.SMSMessage.DomainEvents/MessagingService.SMSMessage.DomainEvents.csproj b/MessagingService.SMSMessage.DomainEvents/MessagingService.SMSMessage.DomainEvents.csproj index c718e5b..245f748 100644 --- a/MessagingService.SMSMessage.DomainEvents/MessagingService.SMSMessage.DomainEvents.csproj +++ b/MessagingService.SMSMessage.DomainEvents/MessagingService.SMSMessage.DomainEvents.csproj @@ -2,6 +2,7 @@ net8.0 + None diff --git a/MessagingService.Testing/TestData.cs b/MessagingService.Testing/TestData.cs index c1cecc8..3962507 100644 --- a/MessagingService.Testing/TestData.cs +++ b/MessagingService.Testing/TestData.cs @@ -255,7 +255,7 @@ public class TestData TestData.Body, TestData.IsHtmlTrue, TestData.EmptyEmailAttachments); - + public static EmailCommands.SendEmailCommand SendEmailCommandWithAttachments => new SendEmailCommand(TestData.ConnectionIdentifier, TestData.MessageId, TestData.FromAddress, diff --git a/MessagingService/Bootstrapper/MiddlewareRegistry.cs b/MessagingService/Bootstrapper/MiddlewareRegistry.cs index c53a164..f57f955 100644 --- a/MessagingService/Bootstrapper/MiddlewareRegistry.cs +++ b/MessagingService/Bootstrapper/MiddlewareRegistry.cs @@ -108,9 +108,9 @@ public MiddlewareRegistry() { Assembly assembly = this.GetType().GetTypeInfo().Assembly; this.AddMvcCore().AddApplicationPart(assembly).AddControllersAsServices(); - bool logRequests = ConfigurationReaderExtensions.GetValueOrDefault("MiddlewareLogging", "LogRequests", true); - bool logResponses = ConfigurationReaderExtensions.GetValueOrDefault("MiddlewareLogging", "LogResponses", true); - LogLevel middlewareLogLevel = ConfigurationReaderExtensions.GetValueOrDefault("MiddlewareLogging", "MiddlewareLogLevel", LogLevel.Warning); + bool logRequests = ConfigurationReader.GetValueOrDefault("MiddlewareLogging", "LogRequests", true); + bool logResponses = ConfigurationReader.GetValueOrDefault("MiddlewareLogging", "LogResponses", true); + LogLevel middlewareLogLevel = ConfigurationReader.GetValueOrDefault("MiddlewareLogging", "MiddlewareLogLevel", LogLevel.Warning); RequestResponseMiddlewareLoggingConfig config = new RequestResponseMiddlewareLoggingConfig(middlewareLogLevel, logRequests, logResponses); @@ -118,26 +118,4 @@ public MiddlewareRegistry() { this.AddSingleton(config); } } - - public static class ConfigurationReaderExtensions - { - public static T GetValueOrDefault(String sectionName, String keyName, T defaultValue) - { - try - { - var value = ConfigurationReader.GetValue(sectionName, keyName); - - if (String.IsNullOrEmpty(value)) - { - return defaultValue; - } - - return (T)Convert.ChangeType(value, typeof(T)); - } - catch (KeyNotFoundException kex) - { - return defaultValue; - } - } - } }