Files
UA-.NETStandard/Tests/Opc.Ua.Di.Tests/DiInProcessSessionBridge.cs
Marc Schier 4756e7c245 DI (OPC 10000-100) with samples (Pump, SW Update), fluent API gap closures and model dependency implementation (#3808)
This PR implements the **OPC UA Device Integration (DI, OPC 10000-100)**
companion-spec library trio (`Opc.Ua.Di` / `Opc.Ua.Di.Server` /
`Opc.Ua.Di.Client`) plus a worked example
(`Applications/MinimalPumpServer`) demonstrating the OPC 40223 Pumps
companion specification end-to-end with full simulation. It also closes
11 fluent-API gaps in `Opc.Ua.Server.Fluent`, three source-generator
bugs, integrates the DI layer into the unified `services.AddOpcUa()`
Microsoft.Extensions DI hosting pattern, and adds 250+ new tests.

**Worked example — `Applications/MinimalPumpServer/`**
- AOT-compatible console host using `Host.CreateApplicationBuilder` +
`AddOpcUa()`.
- `PumpNodeManager` inherits from `DiNodeManager`; multi-namespace (DI +
Machinery + Pumps) via runtime NodeSet2 loading and
`ModelLoaderBuilder`.
- Demonstrates declarative second-pump creation via
`.ConfigureDevicesFor<PumpNodeManager>(...)` and software-package
seeding from the same configurator.
- Full simulation: pressure, temperatures, power, flow, efficiency,
level, cavitation/overheat fault injection, alarm setup, NAMUR-style
supervision.

**Documentation**
- `Docs/Opc.Ua.Di/` — README, DeviceBuilder, Hosting, LockService,
SoftwareUpdate, ClientHelpers (6 new developer guides).
- `Docs/Opc.Ua.Di/ComplianceMatrix.md` — every DI declaration mapped to
In-v1 / Generated / Deferred / Excluded.
- `Docs/CompanionSpecLibraries.md` — packaging guide for new
companion-spec libraries.
- `Docs/FluentApiGaps.md` — historical record of G1–G11.
- `Docs/SourceGeneratedNodeManagers.md` updated for richer node
managers.
- `Docs/plans/StateMachineBuilder.md` — deferred state-machine builder
design.
2026-06-04 05:57:57 +02:00

181 lines
7.3 KiB
C#

/* ========================================================================
* Copyright (c) 2005-2026 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Moq;
using Opc.Ua.Client;
using Opc.Ua.Tests;
namespace Opc.Ua.Di.Tests
{
/// <summary>
/// Builds a Moq-backed <see cref="ISession"/> that dispatches
/// <c>TranslateBrowsePathsToNodeIdsAsync</c> and
/// <c>CallAsync</c> calls directly into a <see cref="DiServerFixture"/>'s
/// in-process address space. Lets client-side helpers be exercised
/// end-to-end without standing up a real TCP server.
/// </summary>
internal static class DiInProcessSessionBridge
{
public static Mock<ISession> Build(DiServerFixture fixture)
{
var mock = new Mock<ISession>();
NamespaceTable nsTable = fixture.Manager.Server.NamespaceUris;
mock.SetupGet(s => s.NamespaceUris).Returns(nsTable);
ServiceMessageContext ctx = ServiceMessageContext.Create(NUnitTelemetryContext.Create());
ctx.NamespaceUris = nsTable;
mock.SetupGet(s => s.MessageContext).Returns(ctx);
// ---- TranslateBrowsePathsToNodeIdsAsync ----
mock.Setup(s => s.TranslateBrowsePathsToNodeIdsAsync(
It.IsAny<RequestHeader?>(),
It.IsAny<ArrayOf<BrowsePath>>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync((RequestHeader? _, ArrayOf<BrowsePath> paths, CancellationToken _) =>
{
var results = new BrowsePathResult[paths.Count];
for (int i = 0; i < paths.Count; i++)
{
results[i] = ResolveBrowsePath(fixture, paths[i]);
}
return new TranslateBrowsePathsToNodeIdsResponse
{
ResponseHeader = new ResponseHeader(),
Results = ArrayOf.Wrapped(results),
DiagnosticInfos = default,
};
});
// ---- CallAsync ----
mock.Setup(s => s.CallAsync(
It.IsAny<RequestHeader?>(),
It.IsAny<ArrayOf<CallMethodRequest>>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync((RequestHeader? _, ArrayOf<CallMethodRequest> calls, CancellationToken _) =>
{
var results = new CallMethodResult[calls.Count];
for (int i = 0; i < calls.Count; i++)
{
results[i] = InvokeCall(fixture, calls[i]);
}
return new CallResponse
{
ResponseHeader = new ResponseHeader(),
Results = ArrayOf.Wrapped(results),
DiagnosticInfos = default,
};
});
return mock;
}
private static BrowsePathResult ResolveBrowsePath(
DiServerFixture fixture, BrowsePath path)
{
NodeState? current = fixture.Manager.FindPredefinedNode<NodeState>(
path.StartingNode);
if (current is null)
{
return new BrowsePathResult
{
StatusCode = StatusCodes.BadNodeIdUnknown,
Targets = global::Opc.Ua.ArrayOf.Empty<BrowsePathTarget>(),
};
}
foreach (RelativePathElement element in path.RelativePath.Elements)
{
NodeState? next = current.FindChild(
fixture.Manager.SystemContext, element.TargetName);
if (next is null)
{
return new BrowsePathResult
{
StatusCode = StatusCodes.BadNoMatch,
Targets = global::Opc.Ua.ArrayOf.Empty<BrowsePathTarget>(),
};
}
current = next;
}
return new BrowsePathResult
{
StatusCode = StatusCodes.Good,
Targets = ArrayOf.Wrapped(new[]
{
new BrowsePathTarget
{
TargetId = (ExpandedNodeId)current.NodeId,
RemainingPathIndex = uint.MaxValue,
}
}),
};
}
private static CallMethodResult InvokeCall(
DiServerFixture fixture, CallMethodRequest call)
{
MethodState? method = fixture.Manager.FindPredefinedNode<MethodState>(
call.MethodId);
if (method is null)
{
return new CallMethodResult
{
StatusCode = StatusCodes.BadMethodInvalid,
InputArgumentResults = global::Opc.Ua.ArrayOf.Empty<StatusCode>(),
InputArgumentDiagnosticInfos = default,
OutputArguments = global::Opc.Ua.ArrayOf.Empty<Variant>(),
};
}
var outputs = new List<Variant>();
var argErrors = new List<ServiceResult>();
ServiceResult status = method.Call(
fixture.Manager.SystemContext,
call.ObjectId,
call.InputArguments,
argErrors,
outputs);
var inputResults = new StatusCode[argErrors.Count];
for (int i = 0; i < argErrors.Count; i++)
{
inputResults[i] = argErrors[i].StatusCode;
}
return new CallMethodResult
{
StatusCode = status.StatusCode,
InputArgumentResults = ArrayOf.Wrapped(inputResults),
InputArgumentDiagnosticInfos = default,
OutputArguments = ArrayOf.Wrapped(outputs.ToArray()),
};
}
}
}