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 @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<DebugType>None</DebugType>
<DebugType>Full</DebugType>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,41 @@ public async Task MessagingDomainService_SendEmailMessage_MessageSent()
result.Data.ShouldBe(TestData.MessageId);
}

[Fact]
public async Task MessagingDomainService_SendEmailMessage_SecondSend_MessageNotSent()
{
Mock<IAggregateRepository<EmailAggregate, DomainEvent>> emailAggregateRepository = new Mock<IAggregateRepository<EmailAggregate, DomainEvent>>();
emailAggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.GetSentEmailAggregate());
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);

var result = await messagingDomainService.SendEmailMessage(TestData.ConnectionIdentifier,
TestData.MessageId,
TestData.FromAddress,
TestData.ToAddresses,
TestData.Subject,
TestData.Body,
TestData.IsHtmlTrue,
TestData.EmailAttachmentModels,
CancellationToken.None);
result.IsSuccess.ShouldBeTrue();
result.Data.ShouldBe(TestData.MessageId);
}

[Fact] public async Task MessagingDomainService_SendEmailMessage_SaveFailed_MessageSent()
{
Mock<IAggregateRepository<EmailAggregate, DomainEvent>> emailAggregateRepository = new Mock<IAggregateRepository<EmailAggregate, DomainEvent>>();
Expand Down Expand Up @@ -260,6 +295,35 @@ public async Task MessagingDomainService_SendSMSMessage_MessageSent()

}

[Fact]
public async Task MessagingDomainService_SendSMSMessage_SecondTime_MessageNotSent()
{
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.GetSentSMSAggregate());
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
.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);

var result = await messagingDomainService.SendSMSMessage(TestData.ConnectionIdentifier,
TestData.MessageId,
TestData.Sender,
TestData.Destination,
TestData.Message,
CancellationToken.None);
result.IsSuccess.ShouldBeTrue();
result.Data.ShouldBe(TestData.MessageId);

}

[Fact]
public async Task MessagingDomainService_SendSMSMessage_SaveFailed_MessageSent()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,13 @@ public async Task<Result<Guid>> SendEmailMessage(Guid connectionIdentifier,
List<EmailAttachment> attachments,
CancellationToken cancellationToken) {
Result result = await ApplyEmailUpdates(async (EmailAggregate emailAggregate) => {
// Check if this message has been sent before
if (emailAggregate.GetMessageStatus() != EmailMessageAggregate.MessageStatus.NotSet) {
return Result.Success();
}

// send message to provider (record event)
emailAggregate.SendRequestToProvider(fromAddress, toAddresses, subject, body, isHtml, attachments);
emailAggregate.SendRequestToProvider(fromAddress, toAddresses, subject, body, isHtml, attachments);

// Make call to Email provider here
EmailServiceProxyResponse emailResponse = await this.EmailServiceProxy.SendEmail(messageId, fromAddress,
Expand Down Expand Up @@ -152,6 +157,12 @@ public async Task<Result<Guid>> SendSMSMessage(Guid connectionIdentifier,
CancellationToken cancellationToken)
{
Result result = await ApplySMSUpdates(async (SMSAggregate smsAggregate) => {
// Check if this message has been sent before
if (smsAggregate.GetMessageStatus() != SMSMessageAggregate.MessageStatus.NotSet)
{
return Result.Success();
}

// send message to provider (record event)
smsAggregate.SendRequestToProvider(sender, destination, message);

Expand Down
15 changes: 0 additions & 15 deletions MessagingService.EmailAggregate.Tests/EmailAggregateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,6 @@ public void EmailAggregate_SendRequestToProvider_RequestSent() {
}
}

[Fact]
public void EmailAggregate_SendRequestToProvider_RequestAlreadySent_NoErrorThrown() {
EmailAggregate emailAggregate = EmailAggregate.Create(TestData.MessageId);

emailAggregate.SendRequestToProvider(TestData.FromAddress, TestData.ToAddresses, TestData.Subject, TestData.Body, TestData.IsHtmlTrue, TestData.EmailAttachmentModels);

Should.NotThrow(() => {
emailAggregate.SendRequestToProvider(TestData.FromAddress,
TestData.ToAddresses,
TestData.Subject,
TestData.Body,
TestData.IsHtmlTrue, TestData.EmailAttachmentModels);
});
}

[Fact]
public void EmailAggregate_ReceiveResponseFromProvider_ResponseReceived() {
EmailAggregate emailAggregate = EmailAggregate.Create(TestData.MessageId);
Expand Down
9 changes: 5 additions & 4 deletions MessagingService.EmailMessageAggregate/EmailAggregate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,18 @@ public static void ReceiveBadResponseFromProvider(this EmailAggregate aggregate,
aggregate.ApplyAndAppend(badResponseReceivedFromProviderEvent);
}

public static MessageStatus GetMessageStatus(this EmailAggregate aggregate)
{
return aggregate.DeliveryStatusList[aggregate.ResendCount];
}

public static void SendRequestToProvider(this EmailAggregate aggregate, String fromAddress,
List<String> toAddresses,
String subject,
String body,
Boolean isHtml,
List<EmailAttachment> attachments)
{
if (aggregate.DeliveryStatusList[aggregate.ResendCount] != MessageStatus.NotSet) {
return;
}

RequestSentToEmailProviderEvent requestSentToProviderEvent = new RequestSentToEmailProviderEvent(aggregate.AggregateId, fromAddress, toAddresses, subject, body, isHtml);

aggregate.ApplyAndAppend(requestSentToProviderEvent);
Expand Down
13 changes: 0 additions & 13 deletions MessagingService.SMSAggregate.Tests/SMSAggregateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,6 @@ public void SMSAggregate_SendRequestToProvider_RequestSent()
smsAggregate.GetDeliveryStatus().ShouldBe(MessageStatus.InProgress);
}

[Fact]
public void SMSAggregate_SendRequestToProvider_RequestAlreadySent_NoErrorThrown()
{
SMSAggregate smsAggregate = SMSAggregate.Create(TestData.MessageId);

smsAggregate.SendRequestToProvider(TestData.Sender, TestData.Destination, TestData.Message);

Should.NotThrow(() =>
{
smsAggregate.SendRequestToProvider(TestData.Sender, TestData.Destination, TestData.Message);
});
}

[Fact]
public void SMSAggregate_ReceiveResponseFromProvider_ResponseReceived()
{
Expand Down
8 changes: 5 additions & 3 deletions MessagingService.SMSMessageAggregate/SMSAggregate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ public static void SendRequestToProvider(this SMSAggregate aggregate,
String sender,
String destination,
String message){
if (aggregate.DeliveryStatusList[aggregate.ResendCount] != MessageStatus.NotSet){
return;
}


RequestSentToSMSProviderEvent requestSentToProviderEvent = new RequestSentToSMSProviderEvent(aggregate.AggregateId, sender, destination, message);

Expand Down Expand Up @@ -146,6 +144,10 @@ private static void CheckMessageCanBeSetToUndeliverable(this SMSAggregate aggreg
}
}

public static MessageStatus GetMessageStatus(this SMSAggregate aggregate) {
return aggregate.DeliveryStatusList[aggregate.ResendCount];
}

#endregion
}

Expand Down
Loading