Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.NotificationOutbox.Tests/ServiceRegistrationTests.cs
T
Joseph Doherty 7b0b9c7365 refactor: rename ScadaLink → ZB.MOM.WW.ScadaBridge (code + projects + namespaces)
Solution + 23 src projects + 26 test projects renamed; folders, csproj,
namespaces, and ScadaLinkDbContext/ScadaBridgeDbContext class updated.
ActorSystem "scadalink" → "scadabridge", Akka seed-node URLs migrated.
SQL roles/logins, LDAP domains, CLI command name, and CLI config dir
(~/.scadalink → ~/.scadabridge) also renamed.

Build green; 5 Host.Tests fail awaiting SQL login rename in next commit.
Pre-existing StaleTagMonitor timing flakes unchanged.

Rename script committed at tools/rename-to-scadabridge.sh.
2026-05-28 09:37:45 -04:00

114 lines
4.5 KiB
C#

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using NSubstitute;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
using ZB.MOM.WW.ScadaBridge.NotificationOutbox.Delivery;
using ZB.MOM.WW.ScadaBridge.NotificationService;
namespace ZB.MOM.WW.ScadaBridge.NotificationOutbox.Tests;
/// <summary>
/// Task 17: Tests for <see cref="ServiceCollectionExtensions.AddNotificationOutbox"/> — the
/// DI registration extension for the Notification Outbox component. The extension binds
/// <see cref="NotificationOutboxOptions"/> from the <c>ScadaBridge:NotificationOutbox</c>
/// configuration section and registers the channel delivery adapter(s).
///
/// The Host wires both <c>AddNotificationService</c> and <c>AddNotificationOutbox</c> on the
/// central node; these tests do the same so the
/// adapter's SMTP dependencies (<c>Func&lt;ISmtpClientWrapper&gt;</c>, <c>OAuth2TokenService</c>,
/// <c>NotificationOptions</c>) are satisfied. <see cref="INotificationRepository"/> — which the
/// <see cref="EmailNotificationDeliveryAdapter"/> takes directly and is registered scoped by the
/// Configuration Database component — is supplied here by a lightweight stub.
/// </summary>
public class ServiceRegistrationTests
{
private static ServiceProvider BuildProvider()
{
var services = new ServiceCollection();
// Empty configuration: the options binding must still succeed and yield the
// documented NotificationOutboxOptions defaults.
var configuration = new ConfigurationBuilder().Build();
services.AddSingleton<IConfiguration>(configuration);
services.AddLogging();
// INotificationRepository is registered scoped by the Configuration Database
// component in production; a no-op stub stands in for it here.
services.AddScoped<INotificationRepository>(_ => Substitute.For<INotificationRepository>());
services.AddNotificationService();
services.AddNotificationOutbox();
return services.BuildServiceProvider();
}
[Fact]
public void AddNotificationOutbox_RegistersNotificationOutboxOptions_WithDefaults()
{
using var provider = BuildProvider();
var options = provider.GetRequiredService<IOptions<NotificationOutboxOptions>>().Value;
Assert.NotNull(options);
Assert.Equal(TimeSpan.FromSeconds(10), options.DispatchInterval);
Assert.Equal(100, options.DispatchBatchSize);
}
[Fact]
public void AddNotificationOutbox_OptionsSection_IsTheNotificationOutboxConfigPath()
{
Assert.Equal(
"ScadaBridge:NotificationOutbox",
NotificationOutbox.ServiceCollectionExtensions.OptionsSection);
}
[Fact]
public void AddNotificationOutbox_BindsNotificationOutboxOptions_FromConfiguration()
{
var services = new ServiceCollection();
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string?>
{
["ScadaBridge:NotificationOutbox:DispatchBatchSize"] = "250",
})
.Build();
services.AddSingleton<IConfiguration>(configuration);
services.AddLogging();
services.AddScoped<INotificationRepository>(_ => Substitute.For<INotificationRepository>());
services.AddNotificationService();
services.AddNotificationOutbox();
using var provider = services.BuildServiceProvider();
var options = provider.GetRequiredService<IOptions<NotificationOutboxOptions>>().Value;
Assert.Equal(250, options.DispatchBatchSize);
}
[Fact]
public void AddNotificationOutbox_RegistersEmailDeliveryAdapter()
{
using var provider = BuildProvider();
using var scope = provider.CreateScope();
var adapter = scope.ServiceProvider.GetRequiredService<EmailNotificationDeliveryAdapter>();
Assert.NotNull(adapter);
Assert.Equal(NotificationType.Email, adapter.Type);
}
[Fact]
public void AddNotificationOutbox_RegistersEmailAdapter_AsINotificationDeliveryAdapter()
{
using var provider = BuildProvider();
using var scope = provider.CreateScope();
var adapters = scope.ServiceProvider.GetServices<INotificationDeliveryAdapter>().ToList();
var email = Assert.Single(adapters);
Assert.IsType<EmailNotificationDeliveryAdapter>(email);
Assert.Equal(NotificationType.Email, email.Type);
}
}