Files
lmxopcua/src/ZB.MOM.WW.LmxOpcUa.Host/OpcUaServiceBuilder.cs
Joseph Doherty 55173665b1 Add configurable transport security profiles and bind address
Adds Security section to appsettings.json with configurable OPC UA
transport profiles (None, Basic256Sha256-Sign, Basic256Sha256-SignAndEncrypt),
certificate policy settings, and a configurable BindAddress for the
OPC UA endpoint. Defaults preserve backward compatibility.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-27 15:59:43 -04:00

214 lines
9.5 KiB
C#

using System.Collections.Generic;
using ZB.MOM.WW.LmxOpcUa.Host.Configuration;
using ZB.MOM.WW.LmxOpcUa.Host.Domain;
namespace ZB.MOM.WW.LmxOpcUa.Host
{
/// <summary>
/// Fluent builder for constructing OpcUaService with dependency overrides.
/// Used by integration tests to substitute fakes for COM/DB components.
/// </summary>
internal class OpcUaServiceBuilder
{
private AppConfiguration _config = new AppConfiguration();
private IMxProxy? _mxProxy;
private IGalaxyRepository? _galaxyRepository;
private IMxAccessClient? _mxAccessClient;
private bool _mxProxySet;
private bool _galaxyRepositorySet;
private bool _mxAccessClientSet;
/// <summary>
/// Replaces the default service configuration used by the test host.
/// </summary>
/// <param name="config">The full configuration snapshot to inject into the service under test.</param>
/// <returns>The current builder so additional overrides can be chained.</returns>
public OpcUaServiceBuilder WithConfig(AppConfiguration config)
{
_config = config;
return this;
}
/// <summary>
/// Sets the OPC UA port used by the test host so multiple integration runs can coexist.
/// </summary>
/// <param name="port">The TCP port to expose for the test server.</param>
/// <returns>The current builder so additional overrides can be chained.</returns>
public OpcUaServiceBuilder WithOpcUaPort(int port)
{
_config.OpcUa.Port = port;
return this;
}
/// <summary>
/// Sets the Galaxy name represented by the test address space.
/// </summary>
/// <param name="name">The Galaxy name to expose through OPC UA and diagnostics.</param>
/// <returns>The current builder so additional overrides can be chained.</returns>
public OpcUaServiceBuilder WithGalaxyName(string name)
{
_config.OpcUa.GalaxyName = name;
return this;
}
/// <summary>
/// Injects an MXAccess proxy substitute for tests that exercise the proxy-driven runtime path.
/// </summary>
/// <param name="proxy">The proxy fake or stub to supply to the service.</param>
/// <returns>The current builder so additional overrides can be chained.</returns>
public OpcUaServiceBuilder WithMxProxy(IMxProxy? proxy)
{
_mxProxy = proxy;
_mxProxySet = true;
return this;
}
/// <summary>
/// Injects a repository substitute for tests that control Galaxy hierarchy and deploy metadata.
/// </summary>
/// <param name="repository">The repository fake or stub to supply to the service.</param>
/// <returns>The current builder so additional overrides can be chained.</returns>
public OpcUaServiceBuilder WithGalaxyRepository(IGalaxyRepository? repository)
{
_galaxyRepository = repository;
_galaxyRepositorySet = true;
return this;
}
/// <summary>
/// Override the MxAccessClient directly, skipping STA thread and COM interop entirely.
/// When set, the service will use this client instead of creating one from IMxProxy.
/// </summary>
/// <param name="client">The direct MXAccess client substitute to inject into the service.</param>
/// <returns>The current builder so additional overrides can be chained.</returns>
public OpcUaServiceBuilder WithMxAccessClient(IMxAccessClient? client)
{
_mxAccessClient = client;
_mxAccessClientSet = true;
return this;
}
/// <summary>
/// Seeds a convenience fake repository with Galaxy hierarchy and attribute rows for address-space tests.
/// </summary>
/// <param name="hierarchy">The object hierarchy to expose through the test OPC UA namespace.</param>
/// <param name="attributes">The attribute rows to attach to the hierarchy.</param>
/// <returns>The current builder so additional overrides can be chained.</returns>
public OpcUaServiceBuilder WithHierarchy(List<GalaxyObjectInfo> hierarchy, List<GalaxyAttributeInfo> attributes)
{
if (!_galaxyRepositorySet)
{
var fake = new FakeBuilderGalaxyRepository();
_galaxyRepository = fake;
_galaxyRepositorySet = true;
}
if (_galaxyRepository is FakeBuilderGalaxyRepository fakeRepo)
{
fakeRepo.Hierarchy = hierarchy;
fakeRepo.Attributes = attributes;
}
return this;
}
/// <summary>
/// Disables the embedded dashboard so tests can focus on the runtime bridge without binding the HTTP listener.
/// </summary>
/// <returns>The current builder so additional overrides can be chained.</returns>
public OpcUaServiceBuilder DisableDashboard()
{
_config.Dashboard.Enabled = false;
return this;
}
/// <summary>
/// Sets the security profile configuration for the test host.
/// </summary>
/// <param name="security">The security profile configuration to inject.</param>
/// <returns>The current builder so additional overrides can be chained.</returns>
public OpcUaServiceBuilder WithSecurity(SecurityProfileConfiguration security)
{
_config.Security = security;
return this;
}
/// <summary>
/// Effectively disables Galaxy change detection by pushing the polling interval beyond realistic test durations.
/// </summary>
/// <returns>The current builder so additional overrides can be chained.</returns>
public OpcUaServiceBuilder DisableChangeDetection()
{
_config.GalaxyRepository.ChangeDetectionIntervalSeconds = int.MaxValue;
return this;
}
/// <summary>
/// Creates an <see cref="OpcUaService"/> using the accumulated test doubles and configuration overrides.
/// </summary>
/// <returns>A service instance ready for integration-style testing.</returns>
public OpcUaService Build()
{
return new OpcUaService(
_config,
_mxProxySet ? _mxProxy : null,
_galaxyRepositorySet ? _galaxyRepository : null,
_mxAccessClientSet ? _mxAccessClient : null,
_mxAccessClientSet);
}
/// <summary>
/// Internal fake repository used by WithHierarchy for convenience.
/// </summary>
private class FakeBuilderGalaxyRepository : IGalaxyRepository
{
/// <summary>
/// Occurs when the fake repository wants to simulate a Galaxy deploy change.
/// </summary>
public event System.Action? OnGalaxyChanged;
/// <summary>
/// Gets or sets the hierarchy rows that the fake repository returns to the service.
/// </summary>
public List<GalaxyObjectInfo> Hierarchy { get; set; } = new();
/// <summary>
/// Gets or sets the attribute rows that the fake repository returns to the service.
/// </summary>
public List<GalaxyAttributeInfo> Attributes { get; set; } = new();
/// <summary>
/// Returns the seeded hierarchy rows for address-space construction.
/// </summary>
/// <param name="ct">A cancellation token that is ignored by the in-memory fake.</param>
/// <returns>The configured hierarchy rows.</returns>
public System.Threading.Tasks.Task<List<GalaxyObjectInfo>> GetHierarchyAsync(System.Threading.CancellationToken ct = default)
=> System.Threading.Tasks.Task.FromResult(Hierarchy);
/// <summary>
/// Returns the seeded attribute rows for address-space construction.
/// </summary>
/// <param name="ct">A cancellation token that is ignored by the in-memory fake.</param>
/// <returns>The configured attribute rows.</returns>
public System.Threading.Tasks.Task<List<GalaxyAttributeInfo>> GetAttributesAsync(System.Threading.CancellationToken ct = default)
=> System.Threading.Tasks.Task.FromResult(Attributes);
/// <summary>
/// Returns the current UTC time so change-detection tests have a deploy timestamp to compare against.
/// </summary>
/// <param name="ct">A cancellation token that is ignored by the in-memory fake.</param>
/// <returns>The current UTC time.</returns>
public System.Threading.Tasks.Task<System.DateTime?> GetLastDeployTimeAsync(System.Threading.CancellationToken ct = default)
=> System.Threading.Tasks.Task.FromResult<System.DateTime?>(System.DateTime.UtcNow);
/// <summary>
/// Reports a healthy repository connection for builder-based test setups.
/// </summary>
/// <param name="ct">A cancellation token that is ignored by the in-memory fake.</param>
/// <returns>A completed task returning <see langword="true"/>.</returns>
public System.Threading.Tasks.Task<bool> TestConnectionAsync(System.Threading.CancellationToken ct = default)
=> System.Threading.Tasks.Task.FromResult(true);
}
}
}