Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
using Moq;
using Shared.DomainDrivenDesign.EventSourcing;
using Shared.EventStore.Aggregate;
using Shouldly;
using SimpleResults;
using SMSMessageAggregate;
using Testing;
using Xunit;
Expand All @@ -25,6 +27,39 @@
{
Mock<IAggregateRepository<EmailAggregate, DomainEvent>> emailAggregateRepository = new Mock<IAggregateRepository<EmailAggregate, DomainEvent>>();
emailAggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.GetEmptyEmailAggregate());
emailAggregateRepository.Setup(a => a.SaveChanges(It.IsAny<EmailAggregate>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success);
Mock<IAggregateRepository<SMSAggregate, DomainEvent>> smsAggregateRepository = new Mock<IAggregateRepository<SMSAggregate, DomainEvent>>();
Mock<IEmailServiceProxy> emailServiceProxy = new Mock<IEmailServiceProxy>();
emailServiceProxy
.Setup(e => e.SendEmail(It.IsAny<Guid>(),
It.IsAny<String>(),
It.IsAny<List<String>>(),
It.IsAny<String>(),
It.IsAny<String>(),
It.IsAny<Boolean>(),
It.IsAny<List<EmailAttachment>>(),
It.IsAny<CancellationToken>())).ReturnsAsync(TestData.SuccessfulEmailServiceProxyResponse);
Mock<ISMSServiceProxy> smsServiceProxy = new Mock<ISMSServiceProxy>();

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<IAggregateRepository<EmailAggregate, DomainEvent>> emailAggregateRepository = new Mock<IAggregateRepository<EmailAggregate, DomainEvent>>();
emailAggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.GetEmptyEmailAggregate());
emailAggregateRepository.Setup(a => a.SaveChanges(It.IsAny<EmailAggregate>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Failure);
Mock<IAggregateRepository<SMSAggregate, DomainEvent>> smsAggregateRepository = new Mock<IAggregateRepository<SMSAggregate, DomainEvent>>();
Mock<IEmailServiceProxy> emailServiceProxy = new Mock<IEmailServiceProxy>();
emailServiceProxy
Expand Down Expand Up @@ -200,6 +235,7 @@
Mock<IAggregateRepository<EmailAggregate, DomainEvent>> emailAggregateRepository = new Mock<IAggregateRepository<EmailAggregate, DomainEvent>>();
Mock<IAggregateRepository<SMSAggregate, DomainEvent>> smsAggregateRepository = new Mock<IAggregateRepository<SMSAggregate, DomainEvent>>();
smsAggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.GetEmptySMSAggregate());
smsAggregateRepository.Setup(a => a.SaveChanges(It.IsAny<SMSAggregate>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success());
Mock<IEmailServiceProxy> emailServiceProxy = new Mock<IEmailServiceProxy>();
Mock<ISMSServiceProxy> smsServiceProxy = new Mock<ISMSServiceProxy>();
smsServiceProxy
Expand All @@ -219,6 +255,32 @@
CancellationToken.None);
}

[Fact]
public async Task MessagingDomainService_SendSMSMessage_SaveFailed_MessageSent()
{
Mock<IAggregateRepository<EmailAggregate, DomainEvent>> emailAggregateRepository = new Mock<IAggregateRepository<EmailAggregate, DomainEvent>>();
Mock<IAggregateRepository<SMSAggregate, DomainEvent>> smsAggregateRepository = new Mock<IAggregateRepository<SMSAggregate, DomainEvent>>();
smsAggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.GetEmptySMSAggregate());
smsAggregateRepository.Setup(a => a.SaveChanges(It.IsAny<SMSAggregate>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Failure);
Mock<IEmailServiceProxy> emailServiceProxy = new Mock<IEmailServiceProxy>();
Mock<ISMSServiceProxy> smsServiceProxy = new Mock<ISMSServiceProxy>();
smsServiceProxy
.Setup(e => e.SendSMS(It.IsAny<Guid>(),
It.IsAny<String>(),
It.IsAny<String>(),
It.IsAny<String>(),
It.IsAny<CancellationToken>())).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()
{
Expand Down Expand Up @@ -286,4 +348,70 @@
}

}

public class DomainServiceHelperTests
{
[Fact]
public void DomainServiceHelper_HandleGetAggregateResult_SuccessfulGet_ResultHandled()
{
Guid aggregateId = Guid.Parse("0639682D-1D28-4AD8-B29D-4B76619083F1");
Result<TestAggregate> 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<TestAggregate> 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<TestAggregate> 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<TestAggregate> 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)

Check failure on line 406 in MessagingService.BusinessLogic.Tests/Services/MessagingDomainServiceTests.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

MessagingService.BusinessLogic.Tests/Services/MessagingDomainServiceTests.cs#L406

Add a nested comment explaining why this method is empty, throw a 'NotSupportedException' or complete the implementation.
{

}

protected override Object GetMetadata()
{
return new Object();
}
}

}
5 changes: 4 additions & 1 deletion MessagingService.BusinessLogic/Requests/EmailAttachment.cs
Original file line number Diff line number Diff line change
@@ -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; }
Expand Down
2 changes: 2 additions & 0 deletions MessagingService.BusinessLogic/Requests/EmailCommands.cs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 2 additions & 0 deletions MessagingService.BusinessLogic/Requests/SMSCommands.cs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
7 changes: 7 additions & 0 deletions MessagingService.EmailAggregate.Tests/EmailAggregateTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Linq;
using Xunit;

namespace MessagingService.EmailAggregate.Tests
Expand Down Expand Up @@ -35,6 +36,12 @@ public void EmailAggregate_SendRequestToProvider_RequestSent() {
toAddresses.Count.ShouldBe(TestData.ToAddresses.Count);
List<EmailAttachment> 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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<DebugType>None</DebugType>
</PropertyGroup>

<ItemGroup>
Expand Down
5 changes: 5 additions & 0 deletions MessagingService.EmailMessageAggregate/EmailAggregate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,11 @@ public static List<EmailAttachment> GetAttachments(this EmailAggregate aggregate
return aggregate.Attachments;
}

public static List<MessageRecipient> GetRecipients(this EmailAggregate aggregate)
{
return aggregate.Recipients;
}

}

public record EmailAggregate : Aggregate
Expand Down
6 changes: 3 additions & 3 deletions MessagingService.EmailMessageAggregate/MessageRecipient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// <summary>
///
/// </summary>
internal class MessageRecipient
public class MessageRecipient
{
#region Constructors

Expand All @@ -19,7 +19,7 @@ internal class MessageRecipient
/// <value>
/// To address.
/// </value>
internal String ToAddress { get; private set; }
public String ToAddress { get; private set; }

#endregion

Expand All @@ -29,7 +29,7 @@ internal class MessageRecipient
/// Creates the specified to address.
/// </summary>
/// <param name="toAddress">To address.</param>
internal void Create(String toAddress)
public void Create(String toAddress)
{
this.ToAddress = toAddress;
}
Expand Down
1 change: 1 addition & 0 deletions MessagingService.Models/MessagingService.Models.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<DebugType>None</DebugType>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<DebugType>None</DebugType>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion MessagingService.Testing/TestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 3 additions & 25 deletions MessagingService/Bootstrapper/MiddlewareRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,36 +108,14 @@ public MiddlewareRegistry() {
Assembly assembly = this.GetType().GetTypeInfo().Assembly;
this.AddMvcCore().AddApplicationPart(assembly).AddControllersAsServices();

bool logRequests = ConfigurationReaderExtensions.GetValueOrDefault<Boolean>("MiddlewareLogging", "LogRequests", true);
bool logResponses = ConfigurationReaderExtensions.GetValueOrDefault<Boolean>("MiddlewareLogging", "LogResponses", true);
LogLevel middlewareLogLevel = ConfigurationReaderExtensions.GetValueOrDefault<LogLevel>("MiddlewareLogging", "MiddlewareLogLevel", LogLevel.Warning);
bool logRequests = ConfigurationReader.GetValueOrDefault<Boolean>("MiddlewareLogging", "LogRequests", true);
bool logResponses = ConfigurationReader.GetValueOrDefault<Boolean>("MiddlewareLogging", "LogResponses", true);
LogLevel middlewareLogLevel = ConfigurationReader.GetValueOrDefault<LogLevel>("MiddlewareLogging", "MiddlewareLogLevel", LogLevel.Warning);

RequestResponseMiddlewareLoggingConfig config =
new RequestResponseMiddlewareLoggingConfig(middlewareLogLevel, logRequests, logResponses);

this.AddSingleton(config);
}
}

public static class ConfigurationReaderExtensions
{
public static T GetValueOrDefault<T>(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;
}
}
}
}
Loading