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
71 changes: 71 additions & 0 deletions FileProcessor.Tests/ControllerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using FileProcessor.Controllers;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Moq;
using Newtonsoft.Json;
using Shared.EventStore.EventHandling;
using Shouldly;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace FileProcessor.Tests
{
using System.Threading;
using File.DomainEvents;
using Shared.General;
using Shared.Logger;

public class ControllerTests
{
public ControllerTests()
{
Logger.Initialise(new NullLogger());
}
[Fact]
public async Task DomainEventController_EventIdNotPresentInJson_ErrorThrown()
{
Mock<IDomainEventHandlerResolver> resolver = new Mock<IDomainEventHandlerResolver>();
TypeMap.AddType<FileLineAddedEvent>("FileLineAddedEvent");
DefaultHttpContext httpContext = new DefaultHttpContext();
httpContext.Request.Headers["eventType"] = "FileLineAddedEvent";
DomainEventController controller = new DomainEventController(resolver.Object)
{
ControllerContext = new ControllerContext()
{
HttpContext = httpContext
}
};
String json = "{\r\n \"estateId\": \"435613ac-a468-47a3-ac4f-649d89764c22\",\r\n \"fileId\": \"17ee2309-ec79-dd25-0af9-f557e565feaa\",\r\n \"fileLine\": \"\",\r\n \"lineNumber\": 16\r\n}\t";
Object request = JsonConvert.DeserializeObject(json);
ArgumentException ex = Should.Throw<ArgumentException>(async () => {
await controller.PostEventAsync(request, CancellationToken.None);
});
ex.Message.ShouldBe("Domain Event must contain an Event Id");
}

[Fact]
public async Task DomainEventController_EventIdPresentInJson_NoErrorThrown()
{
Mock<IDomainEventHandlerResolver> resolver = new Mock<IDomainEventHandlerResolver>();
TypeMap.AddType<FileLineAddedEvent>("FileLineAddedEvent");
DefaultHttpContext httpContext = new DefaultHttpContext();
httpContext.Request.Headers["eventType"] = "FileLineAddedEvent";
DomainEventController controller = new DomainEventController(resolver.Object)
{
ControllerContext = new ControllerContext()
{
HttpContext = httpContext
}
};
String json = "{\r\n \"estateId\": \"435613ac-a468-47a3-ac4f-649d89764c22\",\r\n \"fileId\": \"17ee2309-ec79-dd25-0af9-f557e565feaa\",\r\n \"fileLine\": \"\",\r\n \"lineNumber\": 16,\r\n \"eventId\": \"123456ac-a468-47a3-ac4f-649d89764b44\"\r\n}\t";
Object request = JsonConvert.DeserializeObject(json);
Should.NotThrow(async () => {
await controller.PostEventAsync(request, CancellationToken.None);
});
}
}
}
55 changes: 42 additions & 13 deletions FileProcessor/Controllers/DomainEventController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace FileProcessor.Controllers
using System.Threading;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Shared.DomainDrivenDesign.EventSourcing;
using Shared.EventStore.Aggregate;
using Shared.EventStore.EventHandling;
Expand Down Expand Up @@ -60,12 +61,12 @@ public async Task<IActionResult> PostEventAsync([FromBody] Object request,

cancellationToken.Register(() => this.Callback(cancellationToken, domainEvent.EventId));

List<IDomainEventHandler> eventHandlers = this.GetDomainEventHandlers(domainEvent);

try
{
Logger.LogInformation($"Processing event - ID [{domainEvent.EventId}], Type[{domainEvent.GetType().Name}]");

List<IDomainEventHandler> eventHandlers = this.DomainEventHandlerResolver.GetDomainEventHandlers(domainEvent);

if (eventHandlers == null || eventHandlers.Any() == false)
{
// Log a warning out
Expand Down Expand Up @@ -109,22 +110,37 @@ private void Callback(CancellationToken cancellationToken,
}
}

/// <summary>
/// Gets the domain event.
/// </summary>
/// <param name="domainEvent">The domain event.</param>
/// <returns></returns>
private List<IDomainEventHandler> GetDomainEventHandlers(IDomainEvent domainEvent)
{

if (this.Request.Headers.ContainsKey("EventHandler"))
{
var eventHandler = this.Request.Headers["EventHandler"];
var eventHandlerType = this.Request.Headers["EventHandlerType"];
var resolver = Startup.Container.GetInstance<IDomainEventHandlerResolver>(eventHandlerType);
// We are being told by the caller to use a specific handler
var allhandlers = resolver.GetDomainEventHandlers(domainEvent);
var handlers = allhandlers.Where(h => h.GetType().Name.Contains(eventHandler));

return handlers.ToList();

}

List<IDomainEventHandler> eventHandlers = this.DomainEventHandlerResolver.GetDomainEventHandlers(domainEvent);
return eventHandlers;
}

private async Task<IDomainEvent> GetDomainEvent(Object domainEvent)
{
String eventType = this.Request.Query["eventType"].ToString();
String eventType = this.Request.Headers["eventType"].ToString();

var type = TypeMap.GetType(eventType);
Type type = TypeMap.GetType(eventType);

if (type == null)
throw new Exception($"Failed to find a domain event with type {eventType}");

JsonIgnoreAttributeIgnorerContractResolver jsonIgnoreAttributeIgnorerContractResolver = new JsonIgnoreAttributeIgnorerContractResolver();
var jsonSerialiserSettings = new JsonSerializerSettings
JsonSerializerSettings jsonSerialiserSettings = new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
TypeNameHandling = TypeNameHandling.All,
Expand All @@ -135,15 +151,28 @@ private async Task<IDomainEvent> GetDomainEvent(Object domainEvent)

if (type.IsSubclassOf(typeof(DomainEvent)))
{
var json = JsonConvert.SerializeObject(domainEvent, jsonSerialiserSettings);
DomainEventFactory domainEventFactory = new();
String json = JsonConvert.SerializeObject(domainEvent, jsonSerialiserSettings);

return domainEventFactory.CreateDomainEvent(json, type);
DomainEventFactory domainEventFactory = new();
String validatedJson = this.ValidateEvent(json);
return domainEventFactory.CreateDomainEvent(validatedJson, type);
}

return null;
}

private String ValidateEvent(String domainEventJson)
{
JObject domainEvent = JObject.Parse(domainEventJson);

if (domainEvent.ContainsKey("eventId") == false || domainEvent["eventId"].ToObject<Guid>() == Guid.Empty)
{
throw new ArgumentException("Domain Event must contain an Event Id");
}

return domainEventJson;
}

#endregion

#region Others
Expand Down