From 44563ec94de659ae5012aaa6d981d6a930321850 Mon Sep 17 00:00:00 2001 From: Martin Othamar Date: Wed, 16 Oct 2024 09:10:33 +0200 Subject: [PATCH] IEventHandler --- src/Altinn.App.Core/Features/IEventHandler.cs | 1 + .../Internal/Events/EventHandlerResolver.cs | 10 +++-- .../Events/EventHandlerResolverTests.cs | 37 ++++++++++++++++--- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/Altinn.App.Core/Features/IEventHandler.cs b/src/Altinn.App.Core/Features/IEventHandler.cs index e1be51030..914e554ce 100644 --- a/src/Altinn.App.Core/Features/IEventHandler.cs +++ b/src/Altinn.App.Core/Features/IEventHandler.cs @@ -5,6 +5,7 @@ namespace Altinn.App.Core.Features; /// /// Class handling a dedicated type of event from the Event system ie. an external event. /// +[ImplementableByApps] public interface IEventHandler { /// diff --git a/src/Altinn.App.Core/Internal/Events/EventHandlerResolver.cs b/src/Altinn.App.Core/Internal/Events/EventHandlerResolver.cs index 08bb56e58..ad3232859 100644 --- a/src/Altinn.App.Core/Internal/Events/EventHandlerResolver.cs +++ b/src/Altinn.App.Core/Internal/Events/EventHandlerResolver.cs @@ -1,16 +1,17 @@ using Altinn.App.Core.Features; +using Microsoft.Extensions.DependencyInjection; namespace Altinn.App.Core.Internal.Events; /// public class EventHandlerResolver : IEventHandlerResolver { - private readonly IEnumerable _eventHandlers; + private readonly AppImplementationFactory _appImplementationFactory; /// - public EventHandlerResolver(IEnumerable eventHandlers) + public EventHandlerResolver(IServiceProvider serviceProvider) { - _eventHandlers = eventHandlers; + _appImplementationFactory = serviceProvider.GetRequiredService(); } /// @@ -21,7 +22,8 @@ public IEventHandler ResolveEventHandler(string eventType) return new UnhandledEventHandler(); } - foreach (var handler in _eventHandlers) + var handlers = _appImplementationFactory.GetAll(); + foreach (var handler in handlers) { if (!handler.EventType.Equals(eventType, StringComparison.OrdinalIgnoreCase)) { diff --git a/test/Altinn.App.Core.Tests/Internal/Events/EventHandlerResolverTests.cs b/test/Altinn.App.Core.Tests/Internal/Events/EventHandlerResolverTests.cs index ce1cdefa5..9d1301fbc 100644 --- a/test/Altinn.App.Core.Tests/Internal/Events/EventHandlerResolverTests.cs +++ b/test/Altinn.App.Core.Tests/Internal/Events/EventHandlerResolverTests.cs @@ -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(); + + public static Fixture Create(IEnumerable eventHandlers) + { + var services = new ServiceCollection(); + services.AddTestAppImplementationFactory(); + + services.AddTransient(); + + 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() { new SubscriptionValidationHandler() }); + using var fixture = Fixture.Create([new SubscriptionValidationHandler()]); + var factory = fixture.Factory; IEventHandler eventHandler = factory.ResolveEventHandler("platform.events.validatesubscription"); @@ -24,7 +49,8 @@ public async Task ResolveEventHandler_SubscriptionValidationHandler_ShouldReturn [Fact] public void ResolveEventHandler_InvalidEventType_ShouldReturnUnhandledEventHandler() { - var factory = new EventHandlerResolver(new List()); + using var fixture = Fixture.Create([]); + var factory = fixture.Factory; IEventHandler eventHandler = factory.ResolveEventHandler("this.event.should.not.exists"); Action action = () => eventHandler.ProcessEvent(new CloudEvent()); @@ -37,9 +63,10 @@ public void ResolveEventHandler_InvalidEventType_ShouldReturnUnhandledEventHandl [Fact] public void ResolveEventHandler_Null_ShouldReturnUnhandledEventHandler() { - var factory = new EventHandlerResolver(new List()); + using var fixture = Fixture.Create([]); + var factory = fixture.Factory; - IEventHandler eventHandler = factory.ResolveEventHandler(null); + IEventHandler eventHandler = factory.ResolveEventHandler(null!); eventHandler.Should().BeOfType(); }