Skip to content

Commit 2e23945

Browse files
Update message status from API
1 parent 8efdea0 commit 2e23945

File tree

16 files changed

+1055
-61
lines changed

16 files changed

+1055
-61
lines changed

MessagingService.BusinessLogic.Tests/DomainEventHanders/DomainEventHandlerResolverTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void DomainEventHandlerResolver_GetDomainEventHandlers_ResponseReceivedFr
4545
String handlerTypeName = "MessagingService.BusinessLogic.EventHandling.EmailDomainEventHandler";
4646
Dictionary<String, String[]> eventHandlerConfiguration = new Dictionary<String, String[]>();
4747

48-
ResponseReceivedFromProviderEvent responseReceivedFromProviderEvent = TestData.ResponseReceivedFromProviderEvent;
48+
ResponseReceivedFromProviderEvent responseReceivedFromProviderEvent = TestData.EmailResponseReceivedFromProviderEvent;
4949

5050
eventHandlerConfiguration.Add(responseReceivedFromProviderEvent.GetType().FullName, new String[] { handlerTypeName });
5151

@@ -67,7 +67,7 @@ public void DomainEventHandlerResolver_GetDomainEventHandlers_ResponseReceivedFr
6767
String handlerTypeName = "MessagingService.BusinessLogic.EventHandling.EmailDomainEventHandler";
6868
Dictionary<String, String[]> eventHandlerConfiguration = new Dictionary<String, String[]>();
6969

70-
ResponseReceivedFromProviderEvent responseReceivedFromProviderEvent = TestData.ResponseReceivedFromProviderEvent;
70+
ResponseReceivedFromProviderEvent responseReceivedFromProviderEvent = TestData.EmailResponseReceivedFromProviderEvent;
7171

7272
eventHandlerConfiguration.Add("RandomEvent", new String[] { handlerTypeName });
7373
Mock<IDomainEventHandler> domainEventHandler = new Mock<IDomainEventHandler>();
@@ -85,7 +85,7 @@ public void DomainEventHandlerResolver_GetDomainEventHandlers_ResponseReceivedFr
8585
{
8686
Dictionary<String, String[]> eventHandlerConfiguration = new Dictionary<String, String[]>();
8787

88-
ResponseReceivedFromProviderEvent responseReceivedFromProviderEvent = TestData.ResponseReceivedFromProviderEvent;
88+
ResponseReceivedFromProviderEvent responseReceivedFromProviderEvent = TestData.EmailResponseReceivedFromProviderEvent;
8989
Mock<IDomainEventHandler> domainEventHandler = new Mock<IDomainEventHandler>();
9090

9191
Func<Type, IDomainEventHandler> createDomainEventHandlerFunc = (type) => { return domainEventHandler.Object; };

MessagingService.BusinessLogic.Tests/DomainEventHanders/EmailDomainEventHandlerTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public async Task EmailDomainEventHandler_Handle_ResponseReceivedFromProviderEve
2727
EmailDomainEventHandler emailDomainEventHandler = new EmailDomainEventHandler(aggregateRepository.Object,
2828
emailServiceProxy.Object);
2929

30-
await emailDomainEventHandler.Handle(TestData.ResponseReceivedFromProviderEvent, CancellationToken.None);
30+
await emailDomainEventHandler.Handle(TestData.EmailResponseReceivedFromProviderEvent, CancellationToken.None);
3131
}
3232

3333
[Fact]
@@ -42,7 +42,7 @@ public async Task EmailDomainEventHandler_Handle_ResponseReceivedFromProviderEve
4242
EmailDomainEventHandler emailDomainEventHandler = new EmailDomainEventHandler(aggregateRepository.Object,
4343
emailServiceProxy.Object);
4444

45-
await emailDomainEventHandler.Handle(TestData.ResponseReceivedFromProviderEvent, CancellationToken.None);
45+
await emailDomainEventHandler.Handle(TestData.EmailResponseReceivedFromProviderEvent, CancellationToken.None);
4646
}
4747

4848
[Fact]
@@ -57,7 +57,7 @@ public async Task EmailDomainEventHandler_Handle_ResponseReceivedFromProviderEve
5757
EmailDomainEventHandler emailDomainEventHandler = new EmailDomainEventHandler(aggregateRepository.Object,
5858
emailServiceProxy.Object);
5959

60-
await emailDomainEventHandler.Handle(TestData.ResponseReceivedFromProviderEvent, CancellationToken.None);
60+
await emailDomainEventHandler.Handle(TestData.EmailResponseReceivedFromProviderEvent, CancellationToken.None);
6161
}
6262

6363
[Fact]
@@ -72,7 +72,7 @@ public async Task EmailDomainEventHandler_Handle_ResponseReceivedFromProviderEve
7272
EmailDomainEventHandler emailDomainEventHandler = new EmailDomainEventHandler(aggregateRepository.Object,
7373
emailServiceProxy.Object);
7474

75-
await emailDomainEventHandler.Handle(TestData.ResponseReceivedFromProviderEvent, CancellationToken.None);
75+
await emailDomainEventHandler.Handle(TestData.EmailResponseReceivedFromProviderEvent, CancellationToken.None);
7676
}
7777

7878
[Fact]
@@ -87,7 +87,7 @@ public async Task EmailDomainEventHandler_Handle_ResponseReceivedFromProviderEve
8787
EmailDomainEventHandler emailDomainEventHandler = new EmailDomainEventHandler(aggregateRepository.Object,
8888
emailServiceProxy.Object);
8989

90-
await emailDomainEventHandler.Handle(TestData.ResponseReceivedFromProviderEvent, CancellationToken.None);
90+
await emailDomainEventHandler.Handle(TestData.EmailResponseReceivedFromProviderEvent, CancellationToken.None);
9191
}
9292

9393
[Fact]
@@ -102,7 +102,7 @@ public async Task EmailDomainEventHandler_Handle_ResponseReceivedFromProviderEve
102102
EmailDomainEventHandler emailDomainEventHandler = new EmailDomainEventHandler(aggregateRepository.Object,
103103
emailServiceProxy.Object);
104104

105-
await emailDomainEventHandler.Handle(TestData.ResponseReceivedFromProviderEvent, CancellationToken.None);
105+
await emailDomainEventHandler.Handle(TestData.EmailResponseReceivedFromProviderEvent, CancellationToken.None);
106106
}
107107
}
108108
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
using System.Text;
3+
4+
namespace MessagingService.BusinessLogic.Tests.DomainEventHanders
5+
{
6+
using System.Threading;
7+
using BusinessLogic.Services.EmailServices;
8+
using EmailMessageAggregate;
9+
using EventHandling;
10+
using Moq;
11+
using Shared.EventStore.EventStore;
12+
using System.Threading.Tasks;
13+
using BusinessLogic.Services.SMSServices;
14+
using SMSMessageAggregate;
15+
using Testing;
16+
using Xunit;
17+
18+
public class SMSDomainEventHandlerTests
19+
{
20+
[Fact]
21+
public async Task SMSDomainEventHandler_Handle_ResponseReceivedFromProviderEvent_Delivered_EventIsHandled()
22+
{
23+
Mock<IAggregateRepository<SMSAggregate>> aggregateRepository = new Mock<IAggregateRepository<SMSAggregate>>();
24+
aggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.GetSentSMSAggregate);
25+
Mock<ISMSServiceProxy> smsServiceProxy = new Mock<ISMSServiceProxy>();
26+
smsServiceProxy.Setup(e => e.GetMessageStatus(It.IsAny<String>(), It.IsAny<CancellationToken>()))
27+
.ReturnsAsync(TestData.SMSMessageStatusResponseDelivered);
28+
29+
SMSDomainEventHandler smsDomainEventHandler = new SMSDomainEventHandler(aggregateRepository.Object,
30+
smsServiceProxy.Object);
31+
32+
await smsDomainEventHandler.Handle(TestData.SMSResponseReceivedFromProviderEvent, CancellationToken.None);
33+
}
34+
35+
[Fact]
36+
public async Task SMSDomainEventHandler_Handle_ResponseReceivedFromProviderEvent_Expired_EventIsHandled()
37+
{
38+
Mock<IAggregateRepository<SMSAggregate>> aggregateRepository = new Mock<IAggregateRepository<SMSAggregate>>();
39+
aggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.GetSentSMSAggregate);
40+
Mock<ISMSServiceProxy> smsServiceProxy = new Mock<ISMSServiceProxy>();
41+
smsServiceProxy.Setup(e => e.GetMessageStatus(It.IsAny<String>(), It.IsAny<CancellationToken>()))
42+
.ReturnsAsync(TestData.SMSMessageStatusResponseExpired);
43+
44+
SMSDomainEventHandler smsDomainEventHandler = new SMSDomainEventHandler(aggregateRepository.Object,
45+
smsServiceProxy.Object);
46+
47+
await smsDomainEventHandler.Handle(TestData.SMSResponseReceivedFromProviderEvent, CancellationToken.None);
48+
}
49+
50+
[Fact]
51+
public async Task SMSDomainEventHandler_Handle_ResponseReceivedFromProviderEvent_Rejected_EventIsHandled()
52+
{
53+
Mock<IAggregateRepository<SMSAggregate>> aggregateRepository = new Mock<IAggregateRepository<SMSAggregate>>();
54+
aggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.GetSentSMSAggregate);
55+
Mock<ISMSServiceProxy> smsServiceProxy = new Mock<ISMSServiceProxy>();
56+
smsServiceProxy.Setup(e => e.GetMessageStatus(It.IsAny<String>(), It.IsAny<CancellationToken>()))
57+
.ReturnsAsync(TestData.SMSMessageStatusResponseRejected);
58+
59+
SMSDomainEventHandler smsDomainEventHandler = new SMSDomainEventHandler(aggregateRepository.Object,
60+
smsServiceProxy.Object);
61+
62+
await smsDomainEventHandler.Handle(TestData.SMSResponseReceivedFromProviderEvent, CancellationToken.None);
63+
}
64+
65+
[Fact]
66+
public async Task SMSDomainEventHandler_Handle_ResponseReceivedFromProviderEvent_Undelivered_EventIsHandled()
67+
{
68+
Mock<IAggregateRepository<SMSAggregate>> aggregateRepository = new Mock<IAggregateRepository<SMSAggregate>>();
69+
aggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.GetSentSMSAggregate);
70+
Mock<ISMSServiceProxy> smsServiceProxy = new Mock<ISMSServiceProxy>();
71+
smsServiceProxy.Setup(e => e.GetMessageStatus(It.IsAny<String>(), It.IsAny<CancellationToken>()))
72+
.ReturnsAsync(TestData.SMSMessageStatusResponseUndelivered);
73+
74+
SMSDomainEventHandler smsDomainEventHandler = new SMSDomainEventHandler(aggregateRepository.Object,
75+
smsServiceProxy.Object);
76+
77+
await smsDomainEventHandler.Handle(TestData.SMSResponseReceivedFromProviderEvent, CancellationToken.None);
78+
}
79+
}
80+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
namespace MessagingService.BusinessLogic.EventHandling
2+
{
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using Services.SMSServices;
6+
using Shared.DomainDrivenDesign.EventSourcing;
7+
using Shared.EventStore.EventStore;
8+
using SMSMessage.DomainEvents;
9+
using SMSMessageAggregate;
10+
using MessageStatus = Services.SMSServices.MessageStatus;
11+
12+
public class SMSDomainEventHandler : IDomainEventHandler
13+
{
14+
#region Fields
15+
16+
/// <summary>
17+
/// The aggregate repository
18+
/// </summary>
19+
private readonly IAggregateRepository<SMSAggregate> AggregateRepository;
20+
21+
/// <summary>
22+
/// The email service proxy
23+
/// </summary>
24+
private readonly ISMSServiceProxy SMSServiceProxy;
25+
26+
#endregion
27+
28+
#region Constructors
29+
30+
/// <summary>
31+
/// Initializes a new instance of the <see cref="EmailDomainEventHandler" /> class.
32+
/// </summary>
33+
/// <param name="aggregateRepository">The aggregate repository.</param>
34+
/// <param name="smsServiceProxy">The SMS service proxy.</param>
35+
public SMSDomainEventHandler(IAggregateRepository<SMSAggregate> aggregateRepository,
36+
ISMSServiceProxy smsServiceProxy)
37+
{
38+
this.AggregateRepository = aggregateRepository;
39+
this.SMSServiceProxy = smsServiceProxy;
40+
}
41+
42+
#endregion
43+
44+
#region Methods
45+
46+
/// <summary>
47+
/// Handles the specified domain event.
48+
/// </summary>
49+
/// <param name="domainEvent">The domain event.</param>
50+
/// <param name="cancellationToken">The cancellation token.</param>
51+
public async Task Handle(DomainEvent domainEvent,
52+
CancellationToken cancellationToken)
53+
{
54+
await this.HandleSpecificDomainEvent((dynamic)domainEvent, cancellationToken);
55+
}
56+
57+
/// <summary>
58+
/// Handles the specific domain event.
59+
/// </summary>
60+
/// <param name="domainEvent">The domain event.</param>
61+
/// <param name="cancellationToken">The cancellation token.</param>
62+
private async Task HandleSpecificDomainEvent(ResponseReceivedFromProviderEvent domainEvent,
63+
CancellationToken cancellationToken)
64+
{
65+
SMSAggregate smsAggregate = await this.AggregateRepository.GetLatestVersion(domainEvent.MessageId, cancellationToken);
66+
67+
// Update the aggregate with the status request information
68+
69+
// Get the message status from the provider
70+
MessageStatusResponse messageStatus = await this.SMSServiceProxy.GetMessageStatus(domainEvent.ProviderSMSReference,
71+
cancellationToken);
72+
73+
// Update the aggregate with the response
74+
switch (messageStatus.MessageStatus)
75+
{
76+
case MessageStatus.Expired:
77+
smsAggregate.MarkMessageAsExpired(messageStatus.ProviderStatusDescription, messageStatus.Timestamp);
78+
break;
79+
case MessageStatus.Rejected:
80+
smsAggregate.MarkMessageAsRejected(messageStatus.ProviderStatusDescription, messageStatus.Timestamp);
81+
break;
82+
case MessageStatus.Undeliverable:
83+
smsAggregate.MarkMessageAsUndeliverable(messageStatus.ProviderStatusDescription, messageStatus.Timestamp);
84+
break;
85+
case MessageStatus.Delivered:
86+
smsAggregate.MarkMessageAsDelivered(messageStatus.ProviderStatusDescription, messageStatus.Timestamp);
87+
break;
88+
}
89+
90+
// Save the changes
91+
await this.AggregateRepository.SaveChanges(smsAggregate, cancellationToken);
92+
}
93+
94+
#endregion
95+
}
96+
}

MessagingService.BusinessLogic/Services/SMSServices/ISMSServiceProxy.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,14 @@ Task<SMSServiceProxyResponse> SendSMS(Guid messageId,
2424
String destination,
2525
String message,
2626
CancellationToken cancellationToken);
27+
28+
/// <summary>
29+
/// Gets the message status.
30+
/// </summary>
31+
/// <param name="providerReference">The provider reference.</param>
32+
/// <param name="cancellationToken">The cancellation token.</param>
33+
/// <returns></returns>
34+
Task<MessageStatusResponse> GetMessageStatus(String providerReference,
35+
CancellationToken cancellationToken);
2736
}
2837
}
Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,48 @@
11
namespace MessagingService.Service.Services.SMSServices.IntegrationTest
22
{
33
using System;
4-
using System.Collections.Generic;
54
using System.Diagnostics.CodeAnalysis;
65
using System.Net;
76
using System.Threading;
87
using System.Threading.Tasks;
98
using BusinessLogic.Services.SMSServices;
9+
using MessageStatus = BusinessLogic.Services.SMSServices.MessageStatus;
1010

1111
/// <summary>
1212
///
1313
/// </summary>
14+
/// <seealso cref="MessagingService.BusinessLogic.Services.SMSServices.ISMSServiceProxy" />
1415
/// <seealso cref="MessagingService.BusinessLogic.Services.EmailServices.IEmailServiceProxy" />
1516
[ExcludeFromCodeCoverage]
1617
public class IntegrationTestSMSServiceProxy : ISMSServiceProxy
1718
{
1819
#region Methods
1920

20-
///// <summary>
21-
///// Gets the message status.
22-
///// </summary>
23-
///// <param name="providerReference">The provider reference.</param>
24-
///// <param name="startDate">The start date.</param>
25-
///// <param name="endDate">The end date.</param>
26-
///// <param name="cancellationToken">The cancellation token.</param>
27-
///// <returns></returns>
28-
//public async Task<MessageStatusResponse> GetMessageStatus(String providerReference,
29-
// DateTime startDate,
30-
// DateTime endDate,
31-
// CancellationToken cancellationToken)
32-
//{
33-
// return new MessageStatusResponse
34-
// {
35-
// MessageStatus = MessageStatus.Delivered,
36-
// ProviderStatusDescription = "delivered"
37-
// };
38-
//}
39-
40-
#endregion
21+
/// <summary>
22+
/// Gets the message status.
23+
/// </summary>
24+
/// <param name="providerReference">The provider reference.</param>
25+
/// <param name="cancellationToken">The cancellation token.</param>
26+
/// <returns></returns>
27+
public async Task<MessageStatusResponse> GetMessageStatus(String providerReference,
28+
CancellationToken cancellationToken)
29+
{
30+
return new MessageStatusResponse
31+
{
32+
MessageStatus = MessageStatus.Delivered,
33+
ProviderStatusDescription = "delivered"
34+
};
35+
}
4136

37+
/// <summary>
38+
/// Sends the SMS.
39+
/// </summary>
40+
/// <param name="messageId">The message identifier.</param>
41+
/// <param name="sender">The sender.</param>
42+
/// <param name="destination">The destination.</param>
43+
/// <param name="message">The message.</param>
44+
/// <param name="cancellationToken">The cancellation token.</param>
45+
/// <returns></returns>
4246
public async Task<SMSServiceProxyResponse> SendSMS(Guid messageId,
4347
String sender,
4448
String destination,
@@ -53,5 +57,7 @@ public async Task<SMSServiceProxyResponse> SendSMS(Guid messageId,
5357
SMSIdentifier = "smsid"
5458
};
5559
}
60+
61+
#endregion
5662
}
5763
}

0 commit comments

Comments
 (0)