Repository navigation
Expand file tree
/
Copy pathMachineToolProductionValidationTests.cs
More file actions
413 lines (400 loc) · 21 KB
/
Copy pathMachineToolProductionValidationTests.cs
File metadata and controls
413 lines (400 loc) · 21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// SPDX-FileCopyrightText: Copyright (c) 2005-2026, OPC Federation AISBL
// SPDX-License-Identifier: MIT
using System;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using Opc.Ua.MachineTool.Server;
namespace Opc.Ua.MachineTool.Tests
{
/// <summary>
/// Verifies rejection atomicity and canonical production transitions at the public controller seam.
/// </summary>
[TestFixture]
[Category("MachineTool")]
[NonParallelizable]
public sealed class MachineToolProductionValidationTests
{
/// <summary>
/// Starts a shared model owner; each case still owns a distinct generated machine and production module.
/// </summary>
[OneTimeSetUp]
public async Task StartAsync()
{
m_fixture = new MachineToolRuntimeFixture();
await m_fixture.StartAsync().ConfigureAwait(false);
}
/// <summary>
/// Releases the isolated model owner and certificate stores.
/// </summary>
[OneTimeTearDown]
public async Task StopAsync()
{
await m_fixture.DisposeAsync().ConfigureAwait(false);
}
/// <summary>
/// Each malformed full observation must leave the previous revision, values and transition state intact.
/// </summary>
[TestCase("NullProduction")]
[TestCase("NullActiveProgram")]
[TestCase("ActiveProgramUnknownPhase")]
[TestCase("ActiveProgramMissingTime")]
[TestCase("ActiveProgramUnknownJob")]
[TestCase("ActiveProgramWrongName")]
[TestCase("ActiveProgramWrongState")]
[TestCase("ActiveProgramWrongSequence")]
[TestCase("NoJobs")]
[TestCase("NullJob")]
[TestCase("DuplicateJobs")]
[TestCase("DuplicateJobIdentifiers")]
[TestCase("UnknownJobPhase")]
[TestCase("JobMissingTime")]
[TestCase("JobEmptyIdentifier")]
[TestCase("JobTooLongIdentifier")]
[TestCase("JobTooManyPrograms")]
[TestCase("NoPrograms")]
[TestCase("NullProgram")]
[TestCase("DuplicatePrograms")]
[TestCase("ProgramWrongJob")]
[TestCase("UnknownProgramPhase")]
[TestCase("ProgramMissingTime")]
[TestCase("JobExceedsPlannedRuns")]
[TestCase("UnknownPlanEnded")]
[TestCase("EndedBeforePlannedRuns")]
[TestCase("RunsIncrementWithoutTransition")]
[TestCase("MissingJobTransition")]
[TestCase("SkippedJobTransition")]
[TestCase("InvalidJobTransition")]
[TestCase("ChangedJobIdentifier")]
[TestCase("GoodExceedsCompleted")]
[TestCase("BadHistoricalCounters")]
[TestCase("ChangedHistoricalCounters")]
[TestCase("NullPartSet")]
[TestCase("DuplicatePartSets")]
[TestCase("MismatchedPlannedParts")]
[TestCase("MismatchedCompletedParts")]
[TestCase("NullPart")]
[TestCase("DuplicatePartKeys")]
[TestCase("DuplicatePartIdentifiers")]
[TestCase("UnknownPartQuality")]
[TestCase("UnknownProcessIrregularity")]
[TestCase("UnknownPartPhase")]
[TestCase("WrongJobPartCounters")]
[TestCase("StatisticsMissing")]
[TestCase("StatisticsDecreased")]
[TestCase("StatisticsDisagreesWithModernCounter")]
[TestCase("NullInterruption")]
[TestCase("BadConditionClass")]
[TestCase("InvalidSeverity")]
[TestCase("StaleRevision")]
public async Task InvalidObservationPreservesPublishedRevisionAsync(string violation)
{
MachineToolState machine = CreateMachine(violation);
await using var controller = new MachineToolProductionController(m_fixture.Context, machine,
new MachineToolProductionOptions
{
Features = MachineToolFeatures.Production | MachineToolFeatures.ProductionParts |
MachineToolFeatures.ProductionStatistics | MachineToolFeatures.ProductionInterruptions,
MaximumProgramsPerJob = 2,
MaximumTextLength = 64
}, TimeProvider.System);
MachineToolSnapshot initial = MachineToolProductionTests.Example();
await controller.InitializeAsync(initial).ConfigureAwait(false);
NodeId before = machine.Production!.ActiveProgram!.State!.CurrentState!.Id!.Value;
MachineToolSnapshot bad = InvalidExample(initial, violation);
ServiceResultException rejected = (await Assert.ThrowsAsync<ServiceResultException>(async () =>
await controller.ApplyAsync(bad).ConfigureAwait(false)))!;
Assert.That(rejected.StatusCode, Is.EqualTo(StatusCodes.BadInvalidState), violation);
Assert.That(controller.Observation, Is.SameAs(initial), violation);
Assert.That(machine.Production.ActiveProgram.Name!.Value, Is.EqualTo("MAIN.NC"));
Assert.That(machine.Production.ActiveProgram.State!.CurrentState!.Id!.Value, Is.EqualTo(before));
Assert.That(machine.Production.ActiveProgram.State.LastTransition!.Id!.Value.IsNull, Is.True);
}
/// <summary>
/// Every transition in Tables 47 and 53 uses its specified number and the provider's timestamp.
/// </summary>
[TestCase(MachineToolProductionPhase.Initializing, MachineToolProductionPhase.Running, 0u)]
[TestCase(MachineToolProductionPhase.Running, MachineToolProductionPhase.Ended, 1u)]
[TestCase(MachineToolProductionPhase.Ended, MachineToolProductionPhase.Initializing, 2u)]
[TestCase(MachineToolProductionPhase.Running, MachineToolProductionPhase.Running, 3u)]
[TestCase(MachineToolProductionPhase.Running, MachineToolProductionPhase.Interrupted, 4u)]
[TestCase(MachineToolProductionPhase.Interrupted, MachineToolProductionPhase.Running, 5u)]
[TestCase(MachineToolProductionPhase.Running, MachineToolProductionPhase.Aborted, 6u)]
[TestCase(MachineToolProductionPhase.Interrupted, MachineToolProductionPhase.Aborted, 7u)]
[TestCase(MachineToolProductionPhase.Aborted, MachineToolProductionPhase.Initializing, 8u)]
[TestCase(MachineToolProductionPhase.Initializing, MachineToolProductionPhase.Aborted, 9u)]
public async Task StandardTransitionNumbersAreNotEnumOrdinalsAsync(
MachineToolProductionPhase from, MachineToolProductionPhase to, uint number)
{
MachineToolState machine = CreateMachine("Transition" + number);
await using var controller = new MachineToolProductionController(m_fixture.Context, machine,
new MachineToolProductionOptions(), TimeProvider.System);
var program = new MachineToolProgram
{
Name = "MAIN.NC",
Phase = from,
Sample = new MachineToolSampleInfo(
new DateTime(2026, 5, 6, 7, 8, 9, DateTimeKind.Utc), StatusCodes.UncertainLastUsableValue)
};
var initial = new MachineToolSnapshot { ActiveProgram = program };
await controller.InitializeAsync(initial).ConfigureAwait(false);
var next = initial with
{
Revision = 1, ActiveProgram = program with { Phase = to, TransitionSequence = 1 }
};
await controller.ApplyAsync(next).ConfigureAwait(false);
FiniteTransitionVariableState transition = machine.Production!.ActiveProgram!.State!.LastTransition!;
Assert.That(transition.Number!.Value, Is.EqualTo(number));
Assert.That(transition.Value.Text, Is.EqualTo(from + "To" + to));
Assert.That(transition.TransitionTime!.Value, Is.EqualTo(program.Sample.SourceTimestamp));
Assert.That(transition.StatusCode, Is.EqualTo(program.Sample.StatusCode));
Assert.That(transition.Timestamp, Is.EqualTo(program.Sample.SourceTimestamp));
await controller.ApplyAsync(next).ConfigureAwait(false);
Assert.That(transition.Number.Value, Is.EqualTo(number),
"Re-reading the same snapshot creates no transition.");
}
/// <summary>
/// Setup guards reject impossible limits and features without a delivery capability.
/// </summary>
[TestCase(0, 1)]
[TestCase(65537, 1)]
[TestCase(1, 0)]
[TestCase(1, 65537)]
public void OrderedInventoryLimitsAreExplicit(int jobs, int programs)
{
Assert.That(() => new MachineToolProductionController(m_fixture.Context, CreateMachine("Limits"),
new MachineToolProductionOptions { MaximumJobs = jobs, MaximumProgramsPerJob = programs },
TimeProvider.System), Throws.TypeOf<ArgumentOutOfRangeException>());
}
/// <summary>
/// Dynamic jobs cannot advertise successful deletion without an actual final-observation publisher.
/// </summary>
[Test]
public void DynamicPlanRequiresProductionAndFinalDeliveryCapability()
{
Assert.That(() => new MachineToolProductionController(m_fixture.Context, CreateMachine("MissingProduction"),
new MachineToolProductionOptions { Features = MachineToolFeatures.DynamicProductionPlan },
TimeProvider.System), Throws.TypeOf<ServiceResultException>());
Assert.That(() => new MachineToolProductionController(m_fixture.Context, CreateMachine("MissingDelivery"),
new MachineToolProductionOptions
{
Features = MachineToolFeatures.Production | MachineToolFeatures.DynamicProductionPlan
}, TimeProvider.System), Throws.TypeOf<ServiceResultException>());
}
/// <summary>
/// Cancellation before commit and lifetime misuse leave the initial program unpublished.
/// </summary>
[Test]
public async Task CancellationAndLifetimeGuardsDoNotPublishValuesAsync()
{
MachineToolState machine = CreateMachine("Cancellation");
var controller = new MachineToolProductionController(m_fixture.Context, machine,
new MachineToolProductionOptions(), TimeProvider.System);
using var canceled = new CancellationTokenSource();
canceled.Cancel();
await Assert.ThrowsAsync<OperationCanceledException>(async () =>
await controller.InitializeAsync(new MachineToolSnapshot(), canceled.Token).ConfigureAwait(false));
Assert.That(controller.Observation, Is.Null);
await Assert.ThrowsAsync<ServiceResultException>(async () =>
await controller.ApplyAsync(new MachineToolSnapshot()).ConfigureAwait(false));
await Assert.ThrowsAsync<ArgumentNullException>(async () =>
await controller.InitializeAsync(null!).ConfigureAwait(false));
await controller.InitializeAsync(new MachineToolSnapshot()).ConfigureAwait(false);
await Assert.ThrowsAsync<ServiceResultException>(async () =>
await controller.InitializeAsync(new MachineToolSnapshot()).ConfigureAwait(false));
await controller.DisposeAsync().ConfigureAwait(false);
await controller.DisposeAsync().ConfigureAwait(false);
await Assert.ThrowsAsync<ObjectDisposedException>(async () =>
await controller.ApplyAsync(new MachineToolSnapshot()).ConfigureAwait(false));
}
/// <summary>
/// Creates an unpublished generated machine at the public model factory seam.
/// </summary>
private MachineToolState CreateMachine(string name) =>
m_fixture.Context.Context.CreateInstanceOfMachineToolType(
m_fixture.Context.MachinesFolder,
new QualifiedName(name + Guid.NewGuid().ToString("N"), m_fixture.Context.InstanceNamespaceIndex));
/// <summary>
/// Mutates one independent specification invariant, not the implementation's internal representation.
/// </summary>
private static MachineToolSnapshot InvalidExample(MachineToolSnapshot initial, string violation)
{
MachineToolSnapshot next = initial with { Revision = 1 };
MachineToolProductionJob job = initial.Production.Jobs[0];
MachineToolProgram program = job.Programs[0];
MachineToolProductionPartSet set = job.PartSets[0];
MachineToolProductionPart part = set.Parts[0];
var invalidReason = new MachineToolProductionInterruption
{
Identifier = "Reason",
ConditionClassId = Opc.Ua.ObjectTypeIds.SafetyConditionClassType,
Message = new LocalizedText("Safety stop"),
Sample = job.Sample
};
switch (violation)
{
case "NullProduction":
return next with { Production = null! };
case "NullActiveProgram":
return next with { ActiveProgram = null! };
case "ActiveProgramUnknownPhase":
return next with { ActiveProgram = program with { Phase = (MachineToolProductionPhase)99 } };
case "ActiveProgramMissingTime":
return next with { ActiveProgram = program with { Sample = new(default, StatusCodes.Good) } };
case "ActiveProgramUnknownJob":
return next with { ActiveProgram = program with { JobIdentifier = "Unknown" } };
case "ActiveProgramWrongName":
return next with { ActiveProgram = program with { Name = "Unknown" } };
case "ActiveProgramWrongState":
return next with { ActiveProgram = program with { Phase = MachineToolProductionPhase.Running } };
case "ActiveProgramWrongSequence":
return next with { ActiveProgram = program with { TransitionSequence = 1 } };
case "NoJobs":
return next with { Production = next.Production with { Jobs = [] } };
case "NullJob":
return next with { Production = next.Production with { Jobs = [null!] } };
case "DuplicateJobs":
return next with { Production = next.Production with { Jobs = [job, job] } };
case "DuplicateJobIdentifiers":
return next with { Production = next.Production with { Jobs = [job, job with { Key = "Other" }] } };
case "UnknownJobPhase":
job = job with { Phase = (MachineToolProductionPhase)99 };
break;
case "JobMissingTime":
job = job with { Sample = new(default, StatusCodes.Good) };
break;
case "JobEmptyIdentifier":
job = job with { Identifier = string.Empty };
break;
case "JobTooLongIdentifier":
job = job with { Identifier = new string('x', 65) };
break;
case "JobTooManyPrograms":
job = job with { Programs = [program, program, program] };
break;
case "NoPrograms":
job = job with { Programs = [] };
break;
case "NullProgram":
job = job with { Programs = [null!] };
break;
case "DuplicatePrograms":
job = job with { Programs = [program, program] };
break;
case "ProgramWrongJob":
job = job with { Programs = [program with { JobIdentifier = "Other" }] };
break;
case "UnknownProgramPhase":
job = job with { Programs = [program with { Phase = (MachineToolProductionPhase)99 }] };
break;
case "ProgramMissingTime":
job = job with { Programs = [program with { Sample = new(default, StatusCodes.Good) }] };
break;
case "JobExceedsPlannedRuns":
job = job with { RunsCompleted = 3 };
break;
case "UnknownPlanEnded":
job = job with { Phase = MachineToolProductionPhase.Ended, RunsPlannedIsValid = false };
break;
case "EndedBeforePlannedRuns":
job = job with { Phase = MachineToolProductionPhase.Ended };
break;
case "RunsIncrementWithoutTransition":
job = job with { RunsCompleted = 1 };
break;
case "MissingJobTransition":
job = job with { Phase = MachineToolProductionPhase.Running };
break;
case "SkippedJobTransition":
job = job with { Phase = MachineToolProductionPhase.Running, TransitionSequence = 2 };
break;
case "InvalidJobTransition":
job = job with { Phase = MachineToolProductionPhase.Interrupted, TransitionSequence = 1 };
break;
case "ChangedJobIdentifier":
job = job with { Identifier = "Other", Key = "Job" };
break;
case "GoodExceedsCompleted":
job = job with { PartsGood = 1 };
break;
case "BadHistoricalCounters":
job = job with { PartsGoodBeforeCurrentRun = 1 };
break;
case "ChangedHistoricalCounters":
job = job with { PartsCompletedBeforeCurrentRun = 1, PartsCompleted = 1 };
break;
case "NullPartSet":
job = job with { PartSets = [null!] };
break;
case "DuplicatePartSets":
job = job with { PartSets = [set, set] };
break;
case "MismatchedPlannedParts":
set = set with { PartsPlannedPerRun = 2 };
goto default;
case "MismatchedCompletedParts":
set = set with { PartsCompletedPerRun = 1 };
goto default;
case "NullPart":
set = set with { Parts = [null!] };
goto default;
case "DuplicatePartKeys":
set = set with { Parts = [part, part], PartsPlannedPerRun = 2 };
goto default;
case "DuplicatePartIdentifiers":
set = set with { Parts = [part, part with { Key = "Other" }], PartsPlannedPerRun = 2 };
goto default;
case "UnknownPartQuality":
set = set with { Parts = [part with { Quality = (PartQuality)99 }] };
goto default;
case "UnknownProcessIrregularity":
set = set with { Parts = [part with { Irregularity = (ProcessIrregularity)99 }] };
goto default;
case "UnknownPartPhase":
set = set with { Parts = [part with { Phase = (MachineToolProductionPhase)99 }] };
goto default;
case "WrongJobPartCounters":
job = job with { PartsCompleted = 1 };
break;
case "StatisticsMissing":
return next with { Production = next.Production with { Statistics = null } };
case "StatisticsDecreased":
return next with
{
Production = next.Production with
{
Statistics = new MachineToolProductionStatistics(99, job.Sample)
}
};
case "StatisticsDisagreesWithModernCounter":
return next with
{
Production = next.Production with
{
Statistics = new MachineToolProductionStatistics(101, job.Sample)
}
};
case "NullInterruption":
job = job with { Interruptions = [null!] };
break;
case "BadConditionClass":
job = job with
{
Interruptions = [invalidReason with { ConditionClassId = Opc.Ua.ObjectIds.Server }]
};
break;
case "InvalidSeverity":
job = job with { Interruptions = [invalidReason with { Severity = 1001 }] };
break;
case "StaleRevision":
return next with { Revision = 0 };
default:
job = job with { PartSets = [set] };
break;
}
return next with { Production = next.Production with { Jobs = [job] } };
}
/// <summary>The shared model owner, with isolated machines per test.</summary>
private MachineToolRuntimeFixture m_fixture = null!;
}
}