Skip to content

Commit

Permalink
IEventHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
martinothamar committed Oct 30, 2024
1 parent fd21ab9 commit 44563ec
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/Altinn.App.Core/Features/IEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Altinn.App.Core.Features;
/// <summary>
/// Class handling a dedicated type of event from the Event system ie. an external event.
/// </summary>
[ImplementableByApps]
public interface IEventHandler
{
/// <summary>
Expand Down
10 changes: 6 additions & 4 deletions src/Altinn.App.Core/Internal/Events/EventHandlerResolver.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
using Altinn.App.Core.Features;
using Microsoft.Extensions.DependencyInjection;

namespace Altinn.App.Core.Internal.Events;

/// <inheritdoc/>
public class EventHandlerResolver : IEventHandlerResolver
{
private readonly IEnumerable<IEventHandler> _eventHandlers;
private readonly AppImplementationFactory _appImplementationFactory;

/// <inheritdoc/>
public EventHandlerResolver(IEnumerable<IEventHandler> eventHandlers)
public EventHandlerResolver(IServiceProvider serviceProvider)
{
_eventHandlers = eventHandlers;
_appImplementationFactory = serviceProvider.GetRequiredService<AppImplementationFactory>();
}

/// <inheritdoc/>
Expand All @@ -21,7 +22,8 @@ public IEventHandler ResolveEventHandler(string eventType)
return new UnhandledEventHandler();
}

foreach (var handler in _eventHandlers)
var handlers = _appImplementationFactory.GetAll<IEventHandler>();
foreach (var handler in handlers)
{
if (!handler.EventType.Equals(eventType, StringComparison.OrdinalIgnoreCase))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,42 @@
#nullable disable
using Altinn.App.Core.Features;
using Altinn.App.Core.Internal.Events;
using Altinn.App.Core.Models;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;

namespace Altinn.App.PlatformServices.Tests.Internal.Events;

public class EventHandlerResolverTests
{
private sealed record Fixture(IServiceProvider ServiceProvider) : IDisposable
{
public IEventHandlerResolver Factory => ServiceProvider.GetRequiredService<IEventHandlerResolver>();

public static Fixture Create(IEnumerable<IEventHandler> eventHandlers)
{
var services = new ServiceCollection();
services.AddTestAppImplementationFactory();

services.AddTransient<IEventHandlerResolver, EventHandlerResolver>();

foreach (var eventHandler in eventHandlers)
services.AddTransient(_ => eventHandler);

return new Fixture(
services.BuildServiceProvider(
new ServiceProviderOptions { ValidateOnBuild = true, ValidateScopes = true }
)
);
}

public void Dispose() => (ServiceProvider as IDisposable)?.Dispose();
}

[Fact]
public async Task ResolveEventHandler_SubscriptionValidationHandler_ShouldReturnSubscriptionValidationHandler()
{
var factory = new EventHandlerResolver(new List<IEventHandler>() { new SubscriptionValidationHandler() });
using var fixture = Fixture.Create([new SubscriptionValidationHandler()]);
var factory = fixture.Factory;

IEventHandler eventHandler = factory.ResolveEventHandler("platform.events.validatesubscription");

Expand All @@ -24,7 +49,8 @@ public async Task ResolveEventHandler_SubscriptionValidationHandler_ShouldReturn
[Fact]
public void ResolveEventHandler_InvalidEventType_ShouldReturnUnhandledEventHandler()
{
var factory = new EventHandlerResolver(new List<IEventHandler>());
using var fixture = Fixture.Create([]);
var factory = fixture.Factory;

IEventHandler eventHandler = factory.ResolveEventHandler("this.event.should.not.exists");
Action action = () => eventHandler.ProcessEvent(new CloudEvent());
Expand All @@ -37,9 +63,10 @@ public void ResolveEventHandler_InvalidEventType_ShouldReturnUnhandledEventHandl
[Fact]
public void ResolveEventHandler_Null_ShouldReturnUnhandledEventHandler()
{
var factory = new EventHandlerResolver(new List<IEventHandler>());
using var fixture = Fixture.Create([]);
var factory = fixture.Factory;

IEventHandler eventHandler = factory.ResolveEventHandler(null);
IEventHandler eventHandler = factory.ResolveEventHandler(null!);

eventHandler.Should().BeOfType<UnhandledEventHandler>();
}
Expand Down

0 comments on commit 44563ec

Please sign in to comment.