Repository navigation
Expand file tree
/
Copy pathMachineToolNotificationValidationTests.cs
More file actions
235 lines (224 loc) · 10.5 KB
/
Copy pathMachineToolNotificationValidationTests.cs
File metadata and controls
235 lines (224 loc) · 10.5 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
// SPDX-FileCopyrightText: Copyright (c) 2005-2026, OPC Federation AISBL
// SPDX-License-Identifier: MIT
using System;
using System.Threading.Tasks;
using NUnit.Framework;
using Opc.Ua.MachineTool.Server;
namespace Opc.Ua.MachineTool.Tests
{
/// <summary>
/// Verifies complete prepublication validation of section 8.6.2 severity bands and bounded provider inventories.
/// </summary>
[TestFixture]
[NonParallelizable]
[Category("MachineTool")]
public sealed class MachineToolNotificationValidationTests
{
/// <summary>
/// An invalid batch cannot allocate even the root factory or publish the valid prefix of its inventory.
/// </summary>
[TestCase(0)]
[TestCase(1)]
[TestCase(2)]
[TestCase(3)]
[TestCase(4)]
[TestCase(5)]
[TestCase(6)]
[TestCase(7)]
[TestCase(8)]
[TestCase(9)]
[TestCase(10)]
[TestCase(11)]
[TestCase(12)]
[TestCase(13)]
[TestCase(14)]
[TestCase(15)]
[TestCase(16)]
[TestCase(17)]
[TestCase(18)]
public async Task InvalidProviderBatchFailsBeforeFactoryAsync(int scenario)
{
await using var fixture = new MachineToolRuntimeFixture();
await fixture.StartAsync().ConfigureAwait(false);
MachineToolAlert alert = Alert();
MachineToolMessage message = Message(1);
MachineToolNotificationSnapshot snapshot = scenario switch
{
0 => null!,
1 => new() { Alerts = [alert with { Severity = 333 }] },
2 => new() { Alerts = [alert with { Severity = 1001 }] },
3 => new() { Alerts = [alert, alert] },
4 => new() { Alerts = [alert with { Key = string.Empty }] },
5 => new() { Alerts = [alert with { ErrorCode = string.Empty }] },
6 => new() { Alerts = [alert with { Message = LocalizedText.Null }] },
7 => new() { Alerts = [alert with { ConditionClassId = Ua.DataTypeIds.String }] },
8 => new() { Alerts = [alert with { Sample = new MachineToolSampleInfo(default, StatusCodes.Good) }] },
9 => new() { Messages = [message with { Sequence = 2 }] },
10 => new() { Messages = [message with { Severity = 334 }] },
11 => new() { Messages = [message with { Time = default }] },
12 => new() { Alerts = [alert, alert with { Key = "Other" }] },
13 => new() { Messages = [message, Message(2)] },
14 => new() { Alerts = [alert with { AllowedOperations = (MachineToolConditionOperations)16 }] },
15 => new() { Alerts = [alert with { ErrorCode = new string('x', 4097) }] },
16 => LargeBatch(alert),
17 => new() { Alerts = [null!] },
_ => new() { Messages = [null!] }
};
var provider = new MachineToolNotificationTests.NotificationProvider(snapshot);
var options = new MachineToolNotificationOptions
{
Provider = provider,
MaximumAlerts = scenario == 12 ? 1 : 128,
MaximumMessages = scenario == 13 ? 1 : 128,
MaximumTextLength = scenario == 16 ? 65536 : 4096
};
bool factoryCalled = false;
MachineToolBuilder<MachineToolState> builder = fixture.Context.AddMachineTool("Invalid",
(context, parent, name) =>
{
factoryCalled = true;
return context.CreateInstanceOfMachineToolType(parent, name);
}).WithProvider(new InMemoryMachineToolProvider(new MachineToolSnapshot()))
.WithIdentification(identity =>
{
identity.Manufacturer = new LocalizedText("Validation");
identity.SerialNumber = "Invalid";
identity.ProductInstanceUri = "urn:notification-validation";
}).WithNotifications(options);
ServiceResultException error = (await Assert.ThrowsAsync<ServiceResultException>(async () =>
await builder.BuildAsync().ConfigureAwait(false)))!;
StatusCode expected = scenario switch
{
0 => StatusCodes.BadNoData,
12 or 13 or 16 => StatusCodes.BadEncodingLimitsExceeded,
_ => StatusCodes.BadInvalidState
};
Assert.That(error.StatusCode, Is.EqualTo(expected));
Assert.That(factoryCalled, Is.False);
}
/// <summary>
/// Every invalid configuration is rejected before any provider-driven node is published.
/// </summary>
[TestCase(0)]
[TestCase(1)]
[TestCase(2)]
[TestCase(3)]
[TestCase(4)]
[TestCase(5)]
public async Task InvalidBoundsRejectSelectionAsync(int scenario)
{
await using var fixture = new MachineToolRuntimeFixture();
await fixture.StartAsync().ConfigureAwait(false);
var options = new MachineToolNotificationOptions
{
Provider = new MachineToolNotificationTests.NotificationProvider(new MachineToolNotificationSnapshot())
};
options = scenario switch
{
0 => options with { MaximumAlerts = 0 },
1 => options with { MaximumMessages = 1025 },
2 => options with { MaximumTextLength = 0 },
3 => options with { MaximumTextLength = 65537 },
4 => options with { RefreshTimeout = TimeSpan.Zero },
_ => options with { RefreshTimeout = TimeSpan.FromMinutes(2) }
};
MachineToolBuilder<MachineToolState> builder = fixture.Context.AddMachineTool("Limits")
.WithProvider(new InMemoryMachineToolProvider(new MachineToolSnapshot()))
.WithIdentification(identity =>
{
identity.Manufacturer = new LocalizedText("Validation");
identity.SerialNumber = "Limits";
identity.ProductInstanceUri = "urn:notification-limits";
}).WithNotifications(options);
ServiceResultException error = (await Assert.ThrowsAsync<ServiceResultException>(async () =>
await builder.BuildAsync().ConfigureAwait(false)))!;
Assert.That(error.StatusCode, Is.EqualTo(StatusCodes.BadConfigurationError));
}
/// <summary>
/// Existing alert state and event identity are unchanged by an invalid later inventory.
/// </summary>
[Test]
public async Task RejectedRefreshPreservesExistingAlertAndPendingRetentionAsync()
{
await using var fixture = new MachineToolRuntimeFixture();
await fixture.StartAsync().ConfigureAwait(false);
MachineToolAlert alert = Alert();
var provider = new MachineToolNotificationTests.NotificationProvider(
new MachineToolNotificationSnapshot { Alerts = [alert] });
await using MachineToolRuntime<MachineToolState> runtime =
await MachineToolNotificationTests.BuildAsync(fixture, provider).ConfigureAwait(false);
Assert.That(runtime.Notifications!.TryGetAlert(alert.Key, out AlertState? state), Is.True);
ByteString eventId = state!.EventId!.Value;
provider.Snapshot = new MachineToolNotificationSnapshot
{
Revision = 1,
Alerts = [alert with { Severity = 800 }, alert with { Key = "Invalid", Severity = 1 }]
};
await Assert.ThrowsAsync<ServiceResultException>(async () =>
await runtime.RefreshNotificationsAsync().ConfigureAwait(false));
Assert.That(state.Severity!.Value, Is.EqualTo(500));
Assert.That(state.EventId.Value, Is.EqualTo(eventId));
provider.Snapshot = new MachineToolNotificationSnapshot { Revision = 1 };
await Assert.ThrowsAsync<ServiceResultException>(async () =>
await runtime.RefreshNotificationsAsync().ConfigureAwait(false));
Assert.That(runtime.Notifications.TryGetAlert(alert.Key, out _), Is.True);
provider.Snapshot = new MachineToolNotificationSnapshot
{
Revision = 1,
Alerts = [alert with { Severity = 800 }]
};
await runtime.RefreshNotificationsAsync().ConfigureAwait(false);
provider.Snapshot = new MachineToolNotificationSnapshot { Alerts = [alert] };
await Assert.ThrowsAsync<ServiceResultException>(async () =>
await runtime.RefreshNotificationsAsync().ConfigureAwait(false));
Assert.That(state.Severity.Value, Is.EqualTo(800));
}
/// <summary>
/// Supplies one deterministic valid warning.
/// </summary>
internal static MachineToolAlert Alert()
{
return new()
{
Key = "Warning",
ErrorCode = "W1",
Message = new LocalizedText("Warning"),
Severity = 500,
IsActive = true,
Sample = new MachineToolSampleInfo(
new DateTime(2026, 8, 1, 2, 3, 4, DateTimeKind.Utc), StatusCodes.Good)
};
}
/// <summary>
/// Supplies one deterministic informational notification.
/// </summary>
internal static MachineToolMessage Message(ulong sequence)
{
return new()
{
Sequence = sequence,
Identifier = "M1",
Message = new LocalizedText("Ready"),
Severity = 100,
Time = new DateTime(2026, 8, 1, 2, 3, 4, DateTimeKind.Utc)
};
}
/// <summary>
/// Exceeds the aggregate bound without exceeding an individual field's configured bound.
/// </summary>
private static MachineToolNotificationSnapshot LargeBatch(MachineToolAlert alert)
{
var alerts = new MachineToolAlert[17];
string text = new('x', 65536);
for (int index = 0; index < alerts.Length; index++)
{
alerts[index] = alert with
{
Key = index.ToString(System.Globalization.CultureInfo.InvariantCulture),
Message = new LocalizedText(text)
};
}
return new MachineToolNotificationSnapshot { Alerts = alerts.ToArrayOf() };
}
}
}