Files
UA-.NETStandard/tests/Opc.Ua.MachineTool.Tests/MachineToolProductionFactoryTests.cs
Marc Schier 0374e19121 Add MachineTool, WMTP, GMS and CranesHoists companions (#4765)
# Description

_Describe the changes here to communicate to the maintainers why they
should accept this pull request. By default - this will become the
Commit message after merging and thus define history._

Add provider-backed MachineTool, bundled WMTP, GMS and CranesHoists
companions using the existing Machinery/MachineVision fluent, DI and
typed-client approach. **The PR is open/non-draft by user choice.
Current head `ecc4a7d6ca3e8004cf28ab54da605da302d026bf` is mergeable and
has 108 successful checks, six intentionally skipped checks, zero
failures and zero pending checks. All five primary workflows completed
successfully at that exact head.**

| Family | Released specification/model | Packaging |
| --- | --- | --- |
| MachineTool | OPC 40501-1, 1.02.0 | Model, Server, Client and MCP;
normal root versions |
| WMTP | OPC 40505, 1.0.0 | Bundled in MachineTool; distinct CLR/wire
namespace and independent WMTP-only hosting |
| GMS | OPC 40210 reference 1.0.1, normative model 1.0.0 | Model,
Server, Client and MCP; normal root versions |
| CranesHoists | OPC 40020-1, 1.00 | Model, Server, Client and MCP; all
four plus Debug variants forced preview |

There are **12 new packable projects**. No separate WMTP
package/profile/guide, root version bump, tag, release or package
publication is included. Mandatory instance structure is the default;
selected optional features require explicit providers. The
implementation reuses Machinery results/jobs/files/ProcessValues and
Robotics topology, including retained MachineTool Production, defined
GMS jobs/parts, modern ISA95/Machinery jobs, shared-owner hosting and
isolated instances alongside MachineVision.

Runtime and integration fixes preserve raw quality/timestamps, coherent
acquisition, namespace ownership, accepted-configuration state,
borrowed-resource lifetime, session/node/file cleanup, terminal-value
publication, event propagation and endpoint-aware capability
publication. Firmware/licence transfer means provider acceptance, not
installation/activation. Cancellation does not silently abort another
caller's operation. The later upstream MCP expansion is integrated
without losing either family set or its default-off privileged/live
execution policy.

### Conformance and scope

Canonical model inputs are pinned to UA-Nodeset revision
`96a08945425d0295edb3607d2a38f6841ef44046`.

**GMS upstream exception:** [Annex
A.1](https://reference.opcfoundation.org/specs/OPC-40210/annex-a) makes
the released NodeSet normative, but §12.3.2.7 requires undefined
`GMS_ProductionStatisticsType`. Inherited statistics and defined
jobs/parts are implemented without inventing a standard type/NodeId; the
complete GMS Production facet is withheld.

The requirement ledger records **102 positively evidenced CU rows, 96
directly negative-tested rows and six explicit negative-evidence
limitations**. All 648 test-class/name/TRX links were independently
resolved to passing cases. The six limitations concern Stacklight,
retained legacy PowerOnDuration, Machinery PowerOnDuration, WorkingUnit,
Obligation and MaintenanceMode. They are not claims that every
invalid-input or hardware-control behavior was tested. The undefined GMS
requirement is outside the 102 defined CUs. No external certification
claim is made.

Retained baseline complete-assembly line coverage, with generated code
retained and no source exclusions:

| Family | Model | Server | Client |
| --- | ---: | ---: | ---: |
| MachineTool/WMTP | 83.80% | 91.98% | 84.74% |
| GMS | 95.51% | 90.63% | 88.38% |
| CranesHoists | 92.76% | 94.45% | 92.24% |

These nine measurements predate the later focused review fixes and
merges; they are not relabelled as new `ecc4a7d6c` percentages. The
current head's separate CI code-coverage check succeeded. No new
exclusions or threshold relaxations were introduced.

## Related Issues

- Fixes
[#4764](https://github.com/OPCFoundation/UA-.NETStandard/issues/4764),
the implementation/design and acceptance tracker.

## Checklist

_Put an `x` in the boxes that apply. You can complete these step by step
after opening the PR._

- [ ] I have signed the
[CLA](https://opcfoundation.org/license/cla/ContributorLicenseAgreementv1.0.pdf)
and read the
[CONTRIBUTING](https://github.com/OPCFoundation/UA-.NETStandard/blob/master/CONTRIBUTING.md)
doc.
- [x] I have added tests that prove my fix is effective or that my
feature works and increased code coverage.
- [x] I have added all necessary documentation.
- [ ] I have verified that my changes do not introduce (new) build or
analyzer warnings.
- [ ] I ran **all** tests locally using the **UA.slnx** solution against
at least .net **framework** and .net **10**, and all passed.
- [ ] I fixed **all** failing and flaky tests in the CI pipelines and
**all** CodeQL warnings.
- [x] I have addressed **all** PR feedback received.
2026-10-15 15:15:46 +02:00

355 lines
18 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.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using Opc.Ua.Client;
using Opc.Ua.Client.TestFramework;
using Opc.Ua.GMS;
using Opc.Ua.MachineTool.Server;
using Opc.Ua.Tests;
namespace Opc.Ua.MachineTool.Tests
{
/// <summary>
/// Verifies production composition on actual generated GMS types and rejects invalid ownership transfers.
/// </summary>
[TestFixture]
[Category("MachineTool")]
[NonParallelizable]
public sealed class MachineToolProductionFactoryTests
{
/// <summary>
/// GMSJob and GMSPart retain their types, added members and resource bindings across static job replacement.
/// </summary>
[Test]
public async Task ActualGmsFactoriesPreserveDerivedNodesAcrossStaticReplacementAsync()
{
await using var fixture = new MachineToolRuntimeFixture();
await fixture.StartAsync().ConfigureAwait(false);
await LoadGmsAsync(fixture).ConfigureAwait(false);
GMSState machine = fixture.Context.Context.CreateInstanceOfGMSType(
fixture.Context.MachinesFolder, new QualifiedName("Gms", fixture.Context.InstanceNamespaceIndex));
GMSJobState? jobNode = null;
GMSPartState? partNode = null;
var jobResource = new Resource();
var partResource = new Resource();
var controller = new MachineToolProductionController(fixture.Context, machine,
new MachineToolProductionOptions
{
Features = MachineToolFeatures.Production | MachineToolFeatures.ProductionParts,
CreateJobAsync = (context, parent, name, _) =>
{
jobNode = context.CreateInstanceOfGMSJobType(parent, name);
jobNode.AddBatchIdentifier(context).AddMeasurementReason(context);
jobNode.BatchIdentifier!.Value = "Batch-preserved";
jobNode.MeasurementReason!.Value = MeasurementReasonEnum.AuditMeasurement;
return new ValueTask<MachineToolProductionNode<ProductionJobState>>(
new MachineToolProductionNode<ProductionJobState>(jobNode, jobResource));
},
CreatePartAsync = (context, parent, name, _) =>
{
partNode = context.CreateInstanceOfGMSPartType(parent, name);
partNode.AddPartDescription(context);
partNode.PartDescription!.Value = "Description-preserved";
return new ValueTask<MachineToolProductionNode<ProductionPartState>>(
new MachineToolProductionNode<ProductionPartState>(partNode, partResource));
}
}, TimeProvider.System);
await using (controller.ConfigureAwait(false))
{
MachineToolSnapshot example = MachineToolProductionTests.Example();
MachineToolProductionJob job = example.Production.Jobs[0];
MachineToolProgram program = job.Programs[0] with
{
Key = "Program",
Phase = MachineToolProductionPhase.Ended
};
MachineToolProductionPartSet set = job.PartSets[0] with
{
PartsCompletedPerRun = 1,
Parts =
[
job.PartSets[0].Parts[0] with
{
Phase = MachineToolProductionPhase.Ended, Quality = PartQuality.Good
}
]
};
job = job with
{
Key = "Slot",
Phase = MachineToolProductionPhase.Ended,
RunsCompleted = 1,
RunsPlanned = 1,
PartsCompleted = 1,
PartsGood = 1,
Programs = [program],
PartSets = [set]
};
var initial = example with
{
ActiveProgram = program,
Production = example.Production with { Jobs = [job], Statistics = null }
};
await controller.InitializeAsync(initial).ConfigureAwait(false);
Assert.That(controller.State, Is.SameAs(machine.Production));
Assert.That(jobNode, Is.TypeOf<GMSJobState>());
Assert.That(partNode, Is.TypeOf<GMSPartState>());
NodeId jobId = jobNode!.NodeId;
NodeId partId = partNode!.NodeId;
Assert.That(jobNode.TypeDefinitionId,
Is.EqualTo(NodeId.Create(1016u, GmsModel.NamespaceUri, fixture.Context.Context.NamespaceUris)));
Assert.That(partNode.TypeDefinitionId,
Is.EqualTo(NodeId.Create(1017u, GmsModel.NamespaceUri, fixture.Context.Context.NamespaceUris)));
program = program with
{
Name = "NEXT.NC",
JobIdentifier = "NextJob",
Phase = MachineToolProductionPhase.Initializing,
TransitionSequence = 1
};
set = set with
{
PartsCompletedPerRun = 0,
Parts =
[
set.Parts[0] with
{
Identifier = "NextPart", Phase = MachineToolProductionPhase.Initializing,
TransitionSequence = 1, Quality = PartQuality.NotYetMeasured
}
]
};
job = job with
{
Identifier = "NextJob",
Phase = MachineToolProductionPhase.Initializing,
TransitionSequence = 1,
RunsCompleted = 0,
RunsPlanned = 2,
PartsCompleted = 0,
PartsGood = 0,
Programs = [program],
PartSets = [set]
};
await controller.ApplyAsync(initial with
{
Revision = 1,
ActiveProgram = program,
Production = initial.Production with { Jobs = [job] }
}).ConfigureAwait(false);
Assert.That(jobNode.NodeId, Is.EqualTo(jobId));
Assert.That(partNode.NodeId, Is.EqualTo(partId));
Assert.That(jobNode.Identifier!.Value, Is.EqualTo("NextJob"));
Assert.That(partNode.Identifier!.Value, Is.EqualTo("NextPart"));
Assert.That(jobNode.BatchIdentifier!.Value, Is.EqualTo("Batch-preserved"));
Assert.That(jobNode.MeasurementReason!.Value, Is.EqualTo(MeasurementReasonEnum.AuditMeasurement));
Assert.That(partNode.PartDescription!.Value, Is.EqualTo("Description-preserved"));
Assert.That(jobNode.State!.LastTransition!.Number!.Value, Is.EqualTo(2));
Assert.That(partNode.State!.LastTransition!.Number!.Value, Is.EqualTo(2));
Assert.That(controller.State.ActiveProgram!.JobNodeId!.Value, Is.EqualTo(jobId));
Assert.That(jobResource.Disposals, Is.Zero);
Assert.That(partResource.Disposals, Is.Zero);
}
Assert.That(jobResource.Disposals, Is.EqualTo(1));
Assert.That(partResource.Disposals, Is.EqualTo(1));
}
/// <summary>
/// Returning an already owned generated node must not dispose or delete the existing job on rollback.
/// </summary>
[Test]
public async Task FactoryCannotStealAnExistingJobOrItsResourceAsync()
{
await using var fixture = new MachineToolRuntimeFixture();
await fixture.StartAsync().ConfigureAwait(false);
MachineToolState machine = fixture.Context.Context.CreateInstanceOfMachineToolType(
fixture.Context.MachinesFolder, new QualifiedName("Ownership", fixture.Context.InstanceNamespaceIndex));
ProductionJobState? owned = null;
var resource = new Resource();
await using var controller = new MachineToolProductionController(fixture.Context, machine,
new MachineToolProductionOptions
{
Features = MachineToolFeatures.Production | MachineToolFeatures.DynamicProductionPlan,
PublishFinalObservationsAsync =
(_, _) => throw new InvalidOperationException("No removal is requested."),
CreateJobAsync = (context, parent, name, _) =>
{
owned ??= context.CreateInstanceOfProductionJobType(parent, name);
return new ValueTask<MachineToolProductionNode<ProductionJobState>>(
new MachineToolProductionNode<ProductionJobState>(owned, resource));
}
}, TimeProvider.System);
var job = new MachineToolProductionJob
{
Identifier = "A",
Programs = [new MachineToolProgram { Name = "Main" }]
};
var initial = new MachineToolSnapshot
{
Production = new MachineToolProductionObservation { Jobs = [job] }
};
await controller.InitializeAsync(initial).ConfigureAwait(false);
Assert.ThrowsAsync<ServiceResultException>(async () => await controller.ApplyAsync(initial with
{
Revision = 1,
Production = initial.Production with { Jobs = [job, job with { Identifier = "B" }] }
}).ConfigureAwait(false));
Assert.That(controller.Observation, Is.SameAs(initial));
Assert.That(controller.State.ProductionPlan!.FindChild(fixture.Context.Context,
new QualifiedName("A", fixture.Context.InstanceNamespaceIndex)), Is.SameAs(owned));
Assert.That(owned!.Identifier!.Value, Is.EqualTo("A"));
Assert.That(resource.Disposals, Is.Zero);
}
/// <summary>
/// The public builder passes real GMS factories into the composed runtime and its live address space.
/// </summary>
[Test]
public async Task PublicBuilderPublishesActualGmsJobAndPartNodesOverTcpAsync()
{
await using var fixture = new MachineToolRuntimeFixture();
await fixture.StartAsync().ConfigureAwait(false);
await LoadGmsAsync(fixture).ConfigureAwait(false);
var provider = new InMemoryMachineToolProvider(MachineToolProductionTests.Example());
GMSJobState? job = null;
GMSPartState? part = null;
await using MachineToolRuntime<GMSState> runtime = await fixture.Context
.AddMachineTool("GmsBuilder",
static (context, parent, name) => context.CreateInstanceOfGMSType(parent, name))
.WithProvider(provider)
.WithProduction(new MachineToolProductionOptions
{
Features = MachineToolFeatures.Production | MachineToolFeatures.ProductionParts |
MachineToolFeatures.ProductionStatistics,
CreateJobAsync = (context, parent, name, _) =>
{
job = context.CreateInstanceOfGMSJobType(parent, name);
job.AddBatchIdentifier(context);
job.BatchIdentifier!.Value = "GMS batch";
return new ValueTask<MachineToolProductionNode<ProductionJobState>>(
new MachineToolProductionNode<ProductionJobState>(job));
},
CreatePartAsync = (context, parent, name, _) =>
{
part = context.CreateInstanceOfGMSPartType(parent, name);
part.AddPartDescription(context);
part.PartDescription!.Value = "GMS part";
return new ValueTask<MachineToolProductionNode<ProductionPartState>>(
new MachineToolProductionNode<ProductionPartState>(part));
}
})
.WithIdentification(identity =>
{
identity.Manufacturer = new LocalizedText("GMS production test");
identity.SerialNumber = "GMS-Production";
identity.ProductInstanceUri = "urn:production-tests:gms";
}).BuildAsync().ConfigureAwait(false);
Assert.That(runtime.State, Is.TypeOf<GMSState>());
Assert.That(fixture.Context.Manager.FindPredefinedNode(job!.NodeId), Is.SameAs(job));
Assert.That(fixture.Context.Manager.FindPredefinedNode(part!.NodeId), Is.SameAs(part));
await using var client = new ClientFixture(NUnitTelemetryContext.Create());
await client.LoadClientConfigurationAsync(fixture.Root, "GmsProductionClient").ConfigureAwait(false);
using ISession session = await client.ConnectAsync(fixture.Endpoint, SecurityPolicies.None)
.ConfigureAwait(false);
DataValue batch = await session.ReadValueAsync(job.BatchIdentifier!.NodeId).ConfigureAwait(false);
DataValue description = await session.ReadValueAsync(part.PartDescription!.NodeId).ConfigureAwait(false);
Assert.That(batch.WrappedValue.TryGetValue(out string? batchName), Is.True);
Assert.That(batchName, Is.EqualTo("GMS batch"));
Assert.That(description.WrappedValue.TryGetValue(out string? partName), Is.True);
Assert.That(partName, Is.EqualTo("GMS part"));
MachineToolSnapshot next = await provider.ReadAsync().ConfigureAwait(false);
MachineToolProgram running = next.ActiveProgram with
{
Phase = MachineToolProductionPhase.Running,
TransitionSequence = 1
};
await provider.SetSnapshotAsync(next with
{
ActiveProgram = running,
Production = next.Production with
{
Jobs =
[
next.Production.Jobs[0] with
{
Phase = MachineToolProductionPhase.Running, TransitionSequence = 1, Programs = [running]
}
]
}
}).ConfigureAwait(false);
await runtime.RefreshAsync().ConfigureAwait(false);
DataValue current = await session.ReadValueAsync(job.State!.CurrentState!.Id!.NodeId).ConfigureAwait(false);
Assert.That(current.WrappedValue.TryGetValue(out NodeId state), Is.True);
Assert.That(state, Is.EqualTo(NodeId.Create(5017u, Namespaces.MachineTool, session.NamespaceUris)));
Assert.That(runtime.State.Production!.ActiveProgram!.JobNodeId!.Value, Is.EqualTo(job.NodeId));
await session.CloseAsync(CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Loads released GMS declarations into the existing namespace owner without creating another runtime.
/// </summary>
private static async Task LoadGmsAsync(MachineToolRuntimeFixture fixture)
{
fixture.Context.Context.EncodeableFactory.Builder.AddGmsTypeSystem().Commit();
var definitions = new NodeStateCollection();
definitions.AddGmsTypeSystem(fixture.Context.Context);
for (int i = 0; i < definitions.Count; i++)
{
NodeState definition = definitions[i];
if (fixture.Context.Manager.FindPredefinedNode(definition.NodeId) == null)
{
await fixture.Context.Manager.AddPredefinedNodeAsync(definition).ConfigureAwait(false);
}
}
}
/// <summary>
/// Counts transfers of a companion-specific resource without inspecting module implementation fields.
/// </summary>
private sealed class Resource : IAsyncDisposable
{
/// <summary>
/// Gets the number of completed disposals.
/// </summary>
public int Disposals { get; private set; }
/// <inheritdoc/>
public ValueTask DisposeAsync()
{
Disposals++;
return default;
}
}
}
}