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 @@ -52,6 +52,70 @@ await messagingDomainService.SendEmailMessage(TestData.ConnectionIdentifier,
CancellationToken.None);
}

[Fact]
public async Task MessagingDomainService_SendEmailMessage_EmailSentFailed_APICallFailed_MessageFailed()
{
Mock<IAggregateRepository<EmailAggregate, DomainEvent>> emailAggregateRepository = new Mock<IAggregateRepository<EmailAggregate, DomainEvent>>();
emailAggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.GetEmptyEmailAggregate());
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.FailedAPICallEmailServiceProxyResponse);
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_EmailSentFailed_APIResponseError_MessageFailed()
{
Mock<IAggregateRepository<EmailAggregate, DomainEvent>> emailAggregateRepository = new Mock<IAggregateRepository<EmailAggregate, DomainEvent>>();
emailAggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.GetEmptyEmailAggregate());
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.FailedEmailServiceProxyResponse);
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_ResendEmailMessage_MessageSent()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,14 @@
[ExcludeFromCodeCoverage]
public class EmailServiceProxyResponse
{
/// <summary>
/// Gets or sets the API status code.
/// </summary>
/// <value>
/// The API status code.
/// </value>
public HttpStatusCode ApiStatusCode { get; set; }
public Boolean ApiCallSuccessful { get; set; }

/// <summary>
/// Gets or sets the request identifier.
/// </summary>
/// <value>
/// The request identifier.
/// </value>
public String RequestIdentifier { get; set; }

/// <summary>
/// Gets or sets the email identifier.
/// </summary>
/// <value>
/// The email identifier.
/// </value>
public String EmailIdentifier { get; set; }

/// <summary>
/// Gets or sets the error code.
/// </summary>
/// <value>
/// The error code.
/// </value>
public String ErrorCode { get; set; }

/// <summary>
/// Gets or sets the error.
/// </summary>
/// <value>
/// The error.
/// </value>
public String Error { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public async Task<EmailServiceProxyResponse> SendEmail(Guid messageId,
{
RequestIdentifier = "requestid",
EmailIdentifier = "emailid",
ApiStatusCode = HttpStatusCode.OK,
ApiCallSuccessful = true,
Error = string.Empty,
ErrorCode = string.Empty
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,31 @@ public async Task<EmailServiceProxyResponse> SendEmail(Guid messageId,

StringContent content = new StringContent(requestSerialised, Encoding.UTF8, "application/json");

String requestUri = $"{ConfigurationReader.GetValue("SMTP2GoBaseAddress")}/email/send";
String requestUri = $"{ConfigurationReader.GetValue("SMTP2GoBaseAddress")}email/send";
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri);
requestMessage.Content = content;

HttpResponseMessage httpResponse = await this.HttpClient.SendAsync(requestMessage, cancellationToken);

Smtp2GoSendEmailResponse apiResponse = JsonConvert.DeserializeObject<Smtp2GoSendEmailResponse>(await httpResponse.Content.ReadAsStringAsync());
Smtp2GoSendEmailResponse apiResponse = new Smtp2GoSendEmailResponse(){
Data = new Smtp2GoSendEmailResponseData()
};
if (httpResponse.IsSuccessStatusCode){
apiResponse = JsonConvert.DeserializeObject<Smtp2GoSendEmailResponse>(await httpResponse.Content.ReadAsStringAsync());
}
else{
apiResponse = new Smtp2GoSendEmailResponse();
apiResponse.Data = new Smtp2GoSendEmailResponseData();
apiResponse.Data.Error = httpResponse.StatusCode.ToString();
apiResponse.Data.ErrorCode = ((Int32)httpResponse.StatusCode).ToString();
}

Logger.LogDebug($"Response Message Received from Email Provider [SMTP2Go] {JsonConvert.SerializeObject(apiResponse)}");

// Translate the Response
response = new EmailServiceProxyResponse
{
ApiStatusCode = httpResponse.StatusCode,
ApiCallSuccessful = httpResponse.IsSuccessStatusCode && String.IsNullOrEmpty(apiResponse.Data.ErrorCode),
EmailIdentifier = apiResponse.Data.EmailId,
Error = apiResponse.Data.Error,
ErrorCode = apiResponse.Data.ErrorCode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,13 @@ public async Task SendEmailMessage(Guid connectionIdentifier,
EmailServiceProxyResponse emailResponse =
await this.EmailServiceProxy.SendEmail(messageId, fromAddress, toAddresses, subject, body, isHtml, attachments, cancellationToken);

// response message from provider (record event)
emailAggregate.ReceiveResponseFromProvider(emailResponse.RequestIdentifier, emailResponse.EmailIdentifier);
if (emailResponse.ApiCallSuccessful){
// response message from provider (record event)
emailAggregate.ReceiveResponseFromProvider(emailResponse.RequestIdentifier, emailResponse.EmailIdentifier);
}
else{
emailAggregate.ReceiveBadResponseFromProvider(emailResponse.Error,emailResponse.ErrorCode);
}

// Save Changes to persistance
await this.EmailAggregateRepository.SaveChanges(emailAggregate, cancellationToken);
Expand Down
14 changes: 14 additions & 0 deletions MessagingService.EmailAggregate.Tests/EmailAggregateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ public void EmailAggregate_ReceiveResponseFromProvider_ResponseReceived() {
messageStatus.ShouldBe(MessageStatus.Sent);
}

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

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

emailAggregate.Error.ShouldBe(TestData.EmailError);
emailAggregate.ErrorCode.ShouldBe(TestData.EmailErrorCode);
MessageStatus messageStatus = emailAggregate.GetDeliveryStatus();
messageStatus.ShouldBe(MessageStatus.Failed);
}

[Fact]
public void EmailAggregate_MarkMessageAsDelivered_MessageMarkedAsDelivered() {
EmailAggregate emailAggregate = EmailAggregate.Create(TestData.MessageId);
Expand Down
44 changes: 44 additions & 0 deletions MessagingService.EmailMessage.DomainEvents/DomainEvents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace MessagingService.EmailMessage.DomainEvents;

using System;
using System.Collections.Generic;
using Shared.DomainDrivenDesign.EventSourcing;

public record ResponseReceivedFromEmailProviderEvent(Guid MessageId,
String ProviderRequestReference,
String ProviderEmailReference) : DomainEvent(MessageId, Guid.NewGuid());

public record BadResponseReceivedFromEmailProviderEvent(Guid MessageId,
String ErrorCode,
String Error) : DomainEvent(MessageId, Guid.NewGuid());

public record EmailMessageBouncedEvent(Guid MessageId,
String ProviderStatus,
DateTime BouncedDateTime) : DomainEvent(MessageId, Guid.NewGuid());

public record EmailMessageDeliveredEvent(Guid MessageId,
String ProviderStatus,
DateTime DeliveredDateTime) : DomainEvent(MessageId, Guid.NewGuid());

public record EmailMessageFailedEvent(Guid MessageId,
String ProviderStatus,
DateTime FailedDateTime) : DomainEvent(MessageId, Guid.NewGuid());

public record EmailMessageMarkedAsSpamEvent(Guid MessageId,
String ProviderStatus,
DateTime SpamDateTime) : DomainEvent(MessageId, Guid.NewGuid());

public record EmailMessageRejectedEvent(Guid MessageId,
String ProviderStatus,
DateTime RejectedDateTime) : DomainEvent(MessageId, Guid.NewGuid());

public record RequestResentToEmailProviderEvent(Guid MessageId) : DomainEvent(MessageId, Guid.NewGuid());

public record EmailAttachmentRequestSentToProviderEvent(Guid MessageId, String Filename, String FileData, Int32 FileType) : DomainEvent(MessageId, Guid.NewGuid());

public record RequestSentToEmailProviderEvent(Guid MessageId,
String FromAddress,
List<String> ToAddresses,
String Subject,
String Body,
Boolean IsHtml) : DomainEvent(MessageId, Guid.NewGuid());

This file was deleted.

This file was deleted.

Loading