Add integration test harness: OpcUaServiceBuilder + OpcUaServerFixture

OpcUaServiceBuilder provides fluent API for constructing OpcUaService
with dependency overrides (IMxProxy, IGalaxyRepository, IMxAccessClient).
WithMxAccessClient skips the STA thread and COM interop entirely.

OpcUaServerFixture wraps the service lifecycle with automatic port
allocation (atomic counter starting at 16000), guaranteed cleanup via
IAsyncLifetime, and factory methods for common test scenarios:
- WithFakes() — FakeMxProxy + FakeGalaxyRepository with standard data
- WithFakeMxAccessClient() — bypasses COM, fastest for most tests

Also adds TestData helper with reusable hierarchy/attributes matching
gr/layout.md, and 5 fixture tests verifying startup, shutdown, port
isolation, and address space building.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Joseph Doherty
2026-03-25 06:22:31 -04:00
parent a0edac81fb
commit 44177acf64
5 changed files with 385 additions and 6 deletions

View File

@@ -0,0 +1,103 @@
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using ZB.MOM.WW.LmxOpcUa.Host;
namespace ZB.MOM.WW.LmxOpcUa.Tests.Helpers
{
/// <summary>
/// xUnit fixture that manages an OpcUaService lifecycle with automatic port allocation.
/// Guarantees no port conflicts between parallel tests.
///
/// Usage (per-test):
/// var fixture = OpcUaServerFixture.WithFakes();
/// await fixture.InitializeAsync();
/// try { ... } finally { await fixture.DisposeAsync(); }
///
/// Usage (skip COM entirely):
/// var fixture = OpcUaServerFixture.WithFakeMxAccessClient();
/// </summary>
internal class OpcUaServerFixture : IAsyncLifetime
{
private static int _nextPort = 16000;
public OpcUaService Service { get; private set; } = null!;
public int OpcUaPort { get; }
public string EndpointUrl => $"opc.tcp://localhost:{OpcUaPort}/LmxOpcUa";
private readonly OpcUaServiceBuilder _builder;
private bool _started;
public OpcUaServerFixture(OpcUaServiceBuilder builder)
{
OpcUaPort = Interlocked.Increment(ref _nextPort);
_builder = builder;
_builder.WithOpcUaPort(OpcUaPort);
_builder.DisableDashboard();
}
/// <summary>
/// Creates fixture with FakeMxProxy + FakeGalaxyRepository (standard test data).
/// The STA thread and COM interop run against FakeMxProxy.
/// </summary>
public static OpcUaServerFixture WithFakes(
FakeMxProxy? proxy = null,
FakeGalaxyRepository? repo = null)
{
var p = proxy ?? new FakeMxProxy();
var r = repo ?? new FakeGalaxyRepository
{
Hierarchy = TestData.CreateStandardHierarchy(),
Attributes = TestData.CreateStandardAttributes()
};
var builder = new OpcUaServiceBuilder()
.WithMxProxy(p)
.WithGalaxyRepository(r)
.WithGalaxyName("TestGalaxy");
return new OpcUaServerFixture(builder);
}
/// <summary>
/// Creates fixture using FakeMxAccessClient directly — skips STA thread + COM entirely.
/// Fastest option for tests that don't need real COM interop.
/// </summary>
public static OpcUaServerFixture WithFakeMxAccessClient(
FakeMxAccessClient? mxClient = null,
FakeGalaxyRepository? repo = null)
{
var client = mxClient ?? new FakeMxAccessClient();
var r = repo ?? new FakeGalaxyRepository
{
Hierarchy = TestData.CreateStandardHierarchy(),
Attributes = TestData.CreateStandardAttributes()
};
var builder = new OpcUaServiceBuilder()
.WithMxAccessClient(client)
.WithGalaxyRepository(r)
.WithGalaxyName("TestGalaxy");
return new OpcUaServerFixture(builder);
}
public Task InitializeAsync()
{
Service = _builder.Build();
Service.Start();
_started = true;
return Task.CompletedTask;
}
public Task DisposeAsync()
{
if (_started)
{
try { Service.Stop(); }
catch { /* swallow cleanup errors */ }
}
return Task.CompletedTask;
}
}
}

View File

@@ -0,0 +1,86 @@
using System.Diagnostics;
using System.Threading.Tasks;
using Shouldly;
using Xunit;
using ZB.MOM.WW.LmxOpcUa.Host.Domain;
namespace ZB.MOM.WW.LmxOpcUa.Tests.Helpers
{
public class OpcUaServerFixtureTests
{
[Fact]
public async Task WithFakes_StartsAndStops()
{
var fixture = OpcUaServerFixture.WithFakes();
await fixture.InitializeAsync();
fixture.Service.ShouldNotBeNull();
fixture.Service.MxClient.ShouldNotBeNull();
fixture.Service.MxClient!.State.ShouldBe(ConnectionState.Connected);
fixture.Service.GalaxyStatsInstance.ShouldNotBeNull();
fixture.Service.GalaxyStatsInstance!.GalaxyName.ShouldBe("TestGalaxy");
fixture.OpcUaPort.ShouldBeGreaterThan(16000);
fixture.EndpointUrl.ShouldContain(fixture.OpcUaPort.ToString());
await fixture.DisposeAsync();
}
[Fact]
public async Task WithFakeMxAccessClient_SkipsCom()
{
var mxClient = new FakeMxAccessClient();
var fixture = OpcUaServerFixture.WithFakeMxAccessClient(mxClient);
await fixture.InitializeAsync();
fixture.Service.MxClient.ShouldBe(mxClient);
mxClient.State.ShouldBe(ConnectionState.Connected);
await fixture.DisposeAsync();
}
[Fact]
public async Task MultipleFixtures_GetUniquePortsAutomatically()
{
var fixture1 = OpcUaServerFixture.WithFakeMxAccessClient();
var fixture2 = OpcUaServerFixture.WithFakeMxAccessClient();
fixture1.OpcUaPort.ShouldNotBe(fixture2.OpcUaPort);
// Both can start without port conflicts
await fixture1.InitializeAsync();
await fixture2.InitializeAsync();
fixture1.Service.ShouldNotBeNull();
fixture2.Service.ShouldNotBeNull();
await fixture1.DisposeAsync();
await fixture2.DisposeAsync();
}
[Fact]
public async Task Shutdown_CompletesWithin30Seconds()
{
var fixture = OpcUaServerFixture.WithFakes();
await fixture.InitializeAsync();
var sw = Stopwatch.StartNew();
await fixture.DisposeAsync();
sw.Stop();
sw.Elapsed.TotalSeconds.ShouldBeLessThan(30);
}
[Fact]
public async Task WithFakes_BuildsAddressSpace()
{
var fixture = OpcUaServerFixture.WithFakes();
await fixture.InitializeAsync();
fixture.Service.GalaxyStatsInstance!.ObjectCount.ShouldBe(5);
fixture.Service.GalaxyStatsInstance.AttributeCount.ShouldBe(6);
fixture.Service.GalaxyStatsInstance.DbConnected.ShouldBe(true);
await fixture.DisposeAsync();
}
}
}

View File

@@ -0,0 +1,52 @@
using System.Collections.Generic;
using ZB.MOM.WW.LmxOpcUa.Host.Domain;
namespace ZB.MOM.WW.LmxOpcUa.Tests.Helpers
{
/// <summary>
/// Reusable test data matching the Galaxy hierarchy from gr/layout.md.
/// </summary>
public static class TestData
{
public static List<GalaxyObjectInfo> CreateStandardHierarchy()
{
return new List<GalaxyObjectInfo>
{
new GalaxyObjectInfo { GobjectId = 1, TagName = "DEV", ContainedName = "DEV", BrowseName = "DEV", ParentGobjectId = 0, IsArea = true },
new GalaxyObjectInfo { GobjectId = 2, TagName = "TestArea", ContainedName = "TestArea", BrowseName = "TestArea", ParentGobjectId = 1, IsArea = true },
new GalaxyObjectInfo { GobjectId = 3, TagName = "TestMachine_001", ContainedName = "TestMachine_001", BrowseName = "TestMachine_001", ParentGobjectId = 2, IsArea = false },
new GalaxyObjectInfo { GobjectId = 4, TagName = "DelmiaReceiver_001", ContainedName = "DelmiaReceiver", BrowseName = "DelmiaReceiver", ParentGobjectId = 3, IsArea = false },
new GalaxyObjectInfo { GobjectId = 5, TagName = "MESReceiver_001", ContainedName = "MESReceiver", BrowseName = "MESReceiver", ParentGobjectId = 3, IsArea = false },
};
}
public static List<GalaxyAttributeInfo> CreateStandardAttributes()
{
return new List<GalaxyAttributeInfo>
{
new GalaxyAttributeInfo { GobjectId = 3, TagName = "TestMachine_001", AttributeName = "MachineID", FullTagReference = "TestMachine_001.MachineID", MxDataType = 5, IsArray = false },
new GalaxyAttributeInfo { GobjectId = 3, TagName = "TestMachine_001", AttributeName = "MachineCode", FullTagReference = "TestMachine_001.MachineCode", MxDataType = 5, IsArray = false },
new GalaxyAttributeInfo { GobjectId = 4, TagName = "DelmiaReceiver_001", AttributeName = "DownloadPath", FullTagReference = "DelmiaReceiver_001.DownloadPath", MxDataType = 5, IsArray = false },
new GalaxyAttributeInfo { GobjectId = 4, TagName = "DelmiaReceiver_001", AttributeName = "JobStepNumber", FullTagReference = "DelmiaReceiver_001.JobStepNumber", MxDataType = 2, IsArray = false },
new GalaxyAttributeInfo { GobjectId = 5, TagName = "MESReceiver_001", AttributeName = "MoveInBatchID", FullTagReference = "MESReceiver_001.MoveInBatchID", MxDataType = 5, IsArray = false },
new GalaxyAttributeInfo { GobjectId = 5, TagName = "MESReceiver_001", AttributeName = "MoveInPartNumbers", FullTagReference = "MESReceiver_001.MoveInPartNumbers[]", MxDataType = 5, IsArray = true, ArrayDimension = 50 },
};
}
public static List<GalaxyObjectInfo> CreateMinimalHierarchy()
{
return new List<GalaxyObjectInfo>
{
new GalaxyObjectInfo { GobjectId = 1, TagName = "TestObj", BrowseName = "TestObj", ParentGobjectId = 0, IsArea = false }
};
}
public static List<GalaxyAttributeInfo> CreateMinimalAttributes()
{
return new List<GalaxyAttributeInfo>
{
new GalaxyAttributeInfo { GobjectId = 1, TagName = "TestObj", AttributeName = "TestAttr", FullTagReference = "TestObj.TestAttr", MxDataType = 5, IsArray = false }
};
}
}
}