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
@@ -0,0 +1,101 @@
namespace MessagingService.BusinessLogic.Tests.DomainEventHanders
{
using System;
using System.Collections.Generic;
using System.Linq;
using EmailMessage.DomainEvents;
using EventHandling;
using Moq;
using Shouldly;
using Testing;
using Xunit;

public class DomainEventHandlerResolverTests
{
[Fact]
public void DomainEventHandlerResolver_CanBeCreated_IsCreated()
{
Dictionary<String, String[]> eventHandlerConfiguration = new Dictionary<String, String[]>();

eventHandlerConfiguration.Add("TestEventType1", new String[] { "MessagingService.BusinessLogic.EventHandling.EmailDomainEventHandler" });

Mock<IDomainEventHandler> domainEventHandler = new Mock<IDomainEventHandler>();
Func<Type, IDomainEventHandler> createDomainEventHandlerFunc = (type) => { return domainEventHandler.Object; };
DomainEventHandlerResolver resolver = new DomainEventHandlerResolver(eventHandlerConfiguration, createDomainEventHandlerFunc);

resolver.ShouldNotBeNull();
}

[Fact]
public void DomainEventHandlerResolver_CanBeCreated_InvalidEventHandlerType_ErrorThrown()
{
Dictionary<String, String[]> eventHandlerConfiguration = new Dictionary<String, String[]>();

eventHandlerConfiguration.Add("TestEventType1", new String[] { "MessagingService.BusinessLogic.EventHandling.NonExistantDomainEventHandler" });

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

Should.Throw<NotSupportedException>(() => new DomainEventHandlerResolver(eventHandlerConfiguration, createDomainEventHandlerFunc));
}

[Fact]
public void DomainEventHandlerResolver_GetDomainEventHandlers_ResponseReceivedFromProviderEvent_EventHandlersReturned()
{
String handlerTypeName = "MessagingService.BusinessLogic.EventHandling.EmailDomainEventHandler";
Dictionary<String, String[]> eventHandlerConfiguration = new Dictionary<String, String[]>();

ResponseReceivedFromProviderEvent responseReceivedFromProviderEvent = TestData.ResponseReceivedFromProviderEvent;

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

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

DomainEventHandlerResolver resolver = new DomainEventHandlerResolver(eventHandlerConfiguration, createDomainEventHandlerFunc);

List<IDomainEventHandler> handlers = resolver.GetDomainEventHandlers(responseReceivedFromProviderEvent);

handlers.ShouldNotBeNull();
handlers.Any().ShouldBeTrue();
handlers.Count.ShouldBe(1);
}

[Fact]
public void DomainEventHandlerResolver_GetDomainEventHandlers_ResponseReceivedFromProviderEvent_EventNotConfigured_EventHandlersReturned()
{
String handlerTypeName = "MessagingService.BusinessLogic.EventHandling.EmailDomainEventHandler";
Dictionary<String, String[]> eventHandlerConfiguration = new Dictionary<String, String[]>();

ResponseReceivedFromProviderEvent responseReceivedFromProviderEvent = TestData.ResponseReceivedFromProviderEvent;

eventHandlerConfiguration.Add("RandomEvent", new String[] { handlerTypeName });
Mock<IDomainEventHandler> domainEventHandler = new Mock<IDomainEventHandler>();
Func<Type, IDomainEventHandler> createDomainEventHandlerFunc = (type) => { return domainEventHandler.Object; };

DomainEventHandlerResolver resolver = new DomainEventHandlerResolver(eventHandlerConfiguration, createDomainEventHandlerFunc);

List<IDomainEventHandler> handlers = resolver.GetDomainEventHandlers(responseReceivedFromProviderEvent);

handlers.ShouldBeNull();
}

[Fact]
public void DomainEventHandlerResolver_GetDomainEventHandlers_ResponseReceivedFromProviderEvent_NoHandlersConfigured_EventHandlersReturned()
{
Dictionary<String, String[]> eventHandlerConfiguration = new Dictionary<String, String[]>();

ResponseReceivedFromProviderEvent responseReceivedFromProviderEvent = TestData.ResponseReceivedFromProviderEvent;
Mock<IDomainEventHandler> domainEventHandler = new Mock<IDomainEventHandler>();

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

DomainEventHandlerResolver resolver = new DomainEventHandlerResolver(eventHandlerConfiguration, createDomainEventHandlerFunc);

List<IDomainEventHandler> handlers = resolver.GetDomainEventHandlers(responseReceivedFromProviderEvent);

handlers.ShouldBeNull();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;
using System.Text;

namespace MessagingService.BusinessLogic.Tests.DomainEventHanders
{
using System.Threading;
using BusinessLogic.Services.EmailServices;
using EmailMessageAggregate;
using EventHandling;
using Moq;
using Shared.EventStore.EventStore;
using System.Threading.Tasks;
using Testing;
using Xunit;

public class EmailDomainEventHandlerTests
{
[Fact]
public async Task EmailDomainEventHandler_Handle_ResponseReceivedFromProviderEvent_Delivered_EventIsHandled()
{
Mock<IAggregateRepository<EmailAggregate>> aggregateRepository = new Mock<IAggregateRepository<EmailAggregate>>();
aggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.GetSentEmailAggregate);
Mock<IEmailServiceProxy> emailServiceProxy = new Mock<IEmailServiceProxy>();
emailServiceProxy.Setup(e => e.GetMessageStatus(It.IsAny<String>(), It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(TestData.MessageStatusResponseDelivered);

EmailDomainEventHandler emailDomainEventHandler = new EmailDomainEventHandler(aggregateRepository.Object,
emailServiceProxy.Object);

await emailDomainEventHandler.Handle(TestData.ResponseReceivedFromProviderEvent, CancellationToken.None);
}

[Fact]
public async Task EmailDomainEventHandler_Handle_ResponseReceivedFromProviderEvent_Failed_EventIsHandled()
{
Mock<IAggregateRepository<EmailAggregate>> aggregateRepository = new Mock<IAggregateRepository<EmailAggregate>>();
aggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.GetSentEmailAggregate);
Mock<IEmailServiceProxy> emailServiceProxy = new Mock<IEmailServiceProxy>();
emailServiceProxy.Setup(e => e.GetMessageStatus(It.IsAny<String>(), It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(TestData.MessageStatusResponseFailed);

EmailDomainEventHandler emailDomainEventHandler = new EmailDomainEventHandler(aggregateRepository.Object,
emailServiceProxy.Object);

await emailDomainEventHandler.Handle(TestData.ResponseReceivedFromProviderEvent, CancellationToken.None);
}

[Fact]
public async Task EmailDomainEventHandler_Handle_ResponseReceivedFromProviderEvent_Rejected_EventIsHandled()
{
Mock<IAggregateRepository<EmailAggregate>> aggregateRepository = new Mock<IAggregateRepository<EmailAggregate>>();
aggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.GetSentEmailAggregate);
Mock<IEmailServiceProxy> emailServiceProxy = new Mock<IEmailServiceProxy>();
emailServiceProxy.Setup(e => e.GetMessageStatus(It.IsAny<String>(), It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(TestData.MessageStatusResponseRejected);

EmailDomainEventHandler emailDomainEventHandler = new EmailDomainEventHandler(aggregateRepository.Object,
emailServiceProxy.Object);

await emailDomainEventHandler.Handle(TestData.ResponseReceivedFromProviderEvent, CancellationToken.None);
}

[Fact]
public async Task EmailDomainEventHandler_Handle_ResponseReceivedFromProviderEvent_Bounced_EventIsHandled()
{
Mock<IAggregateRepository<EmailAggregate>> aggregateRepository = new Mock<IAggregateRepository<EmailAggregate>>();
aggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.GetSentEmailAggregate);
Mock<IEmailServiceProxy> emailServiceProxy = new Mock<IEmailServiceProxy>();
emailServiceProxy.Setup(e => e.GetMessageStatus(It.IsAny<String>(), It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(TestData.MessageStatusResponseBounced);

EmailDomainEventHandler emailDomainEventHandler = new EmailDomainEventHandler(aggregateRepository.Object,
emailServiceProxy.Object);

await emailDomainEventHandler.Handle(TestData.ResponseReceivedFromProviderEvent, CancellationToken.None);
}

[Fact]
public async Task EmailDomainEventHandler_Handle_ResponseReceivedFromProviderEvent_Spam_EventIsHandled()
{
Mock<IAggregateRepository<EmailAggregate>> aggregateRepository = new Mock<IAggregateRepository<EmailAggregate>>();
aggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.GetSentEmailAggregate);
Mock<IEmailServiceProxy> emailServiceProxy = new Mock<IEmailServiceProxy>();
emailServiceProxy.Setup(e => e.GetMessageStatus(It.IsAny<String>(), It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(TestData.MessageStatusResponseSpam);

EmailDomainEventHandler emailDomainEventHandler = new EmailDomainEventHandler(aggregateRepository.Object,
emailServiceProxy.Object);

await emailDomainEventHandler.Handle(TestData.ResponseReceivedFromProviderEvent, CancellationToken.None);
}

[Fact]
public async Task EmailDomainEventHandler_Handle_ResponseReceivedFromProviderEvent_Unknown_EventIsHandled()
{
Mock<IAggregateRepository<EmailAggregate>> aggregateRepository = new Mock<IAggregateRepository<EmailAggregate>>();
aggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.GetSentEmailAggregate);
Mock<IEmailServiceProxy> emailServiceProxy = new Mock<IEmailServiceProxy>();
emailServiceProxy.Setup(e => e.GetMessageStatus(It.IsAny<String>(), It.IsAny<DateTime>(), It.IsAny<DateTime>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(TestData.MessageStatusResponseUnknown);

EmailDomainEventHandler emailDomainEventHandler = new EmailDomainEventHandler(aggregateRepository.Object,
emailServiceProxy.Object);

await emailDomainEventHandler.Handle(TestData.ResponseReceivedFromProviderEvent, CancellationToken.None);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace MessagingService.BusinessLogic.Tests.Services
public class EmailDomainServiceTests
{
[Fact]
public async Task TransactionDomainService_ProcessLogonTransaction_TransactionIsProcessed()
public async Task EmailDomainService_SendEmailMessage_MessageSent()
{
Mock<IAggregateRepository<EmailAggregate>> aggregateRepository = new Mock<IAggregateRepository<EmailAggregate>>();
aggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny<Guid>(), It.IsAny<CancellationToken>())).ReturnsAsync(TestData.GetEmptyEmailAggregate());
Expand All @@ -30,11 +30,7 @@ public async Task TransactionDomainService_ProcessLogonTransaction_TransactionIs
It.IsAny<String>(),
It.IsAny<Boolean>(),
It.IsAny<CancellationToken>())).ReturnsAsync(TestData.SuccessfulEmailServiceProxyResponse);
//IConfigurationRoot configurationRoot = new ConfigurationBuilder().AddInMemoryCollection(TestData.DefaultAppSettings).Build();
//ConfigurationReader.Initialise(configurationRoot);

//Logger.Initialise(NullLogger.Instance);


EmailDomainService emailDomainService =
new EmailDomainService(aggregateRepository.Object, emailServiceProxy.Object);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
namespace MessagingService.BusinessLogic.EventHandling
{
using System;
using System.Collections.Generic;
using System.Linq;
using Shared.DomainDrivenDesign.EventSourcing;

public class DomainEventHandlerResolver : IDomainEventHandlerResolver
{
#region Fields

/// <summary>
/// The domain event handlers
/// </summary>
private readonly Dictionary<String, IDomainEventHandler> DomainEventHandlers;

/// <summary>
/// The event handler configuration
/// </summary>
private readonly Dictionary<String, String[]> EventHandlerConfiguration;

#endregion

#region Constructors

/// <summary>
/// Initializes a new instance of the <see cref="DomainEventHandlerResolver" /> class.
/// </summary>
/// <param name="eventHandlerConfiguration">The event handler configuration.</param>
public DomainEventHandlerResolver(Dictionary<String, String[]> eventHandlerConfiguration, Func<Type, IDomainEventHandler> createEventHandlerResolver)
{
this.EventHandlerConfiguration = eventHandlerConfiguration;

this.DomainEventHandlers = new Dictionary<String, IDomainEventHandler>();

List<String> handlers = new List<String>();

// Precreate the Event Handlers here
foreach (KeyValuePair<String, String[]> handlerConfig in eventHandlerConfiguration)
{
handlers.AddRange(handlerConfig.Value);
}

IEnumerable<String> distinctHandlers = handlers.Distinct();

foreach (String handlerTypeString in distinctHandlers)
{
Type handlerType = Type.GetType(handlerTypeString);

if (handlerType == null)
{
throw new NotSupportedException("Event handler configuration is not for a valid type");
}

IDomainEventHandler eventHandler = createEventHandlerResolver(handlerType);
this.DomainEventHandlers.Add(handlerTypeString, eventHandler);
}
}

#endregion

#region Methods

/// <summary>
/// Gets the domain event handlers.
/// </summary>
/// <param name="domainEvent">The domain event.</param>
/// <returns></returns>
public List<IDomainEventHandler> GetDomainEventHandlers(DomainEvent domainEvent)
{
// Get the type of the event passed in
String typeString = domainEvent.GetType().FullName;

// Lookup the list
Boolean eventIsConfigured = this.EventHandlerConfiguration.ContainsKey(typeString);

if (!eventIsConfigured)
{
// No handlers setup, return null and let the caller decide what to do next
return null;
}

String[] handlers = this.EventHandlerConfiguration[typeString];

List<IDomainEventHandler> handlersToReturn = new List<IDomainEventHandler>();

foreach (String handler in handlers)
{
List<KeyValuePair<String, IDomainEventHandler>> foundHandlers = this.DomainEventHandlers.Where(h => h.Key == handler).ToList();

handlersToReturn.AddRange(foundHandlers.Select(x => x.Value));
}

return handlersToReturn;
}

#endregion
}
}
Loading