/* ========================================================================
* Copyright (c) 2005-2025 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/
* ======================================================================*/
#pragma warning disable CA2007
using System;
using System.Threading.Tasks;
using Moq;
using NUnit.Framework;
using Opc.Ua.Server;
using Opc.Ua.Tests;
namespace Opc.Ua.Redundancy.Server.Tests
{
///
/// Tests for : it attaches a CRDT
/// synchronizer to every opted-in node manager and skips the rest.
///
[TestFixture]
[Category("Distributed")]
public sealed class ReplicatedAddressSpaceStartupTaskTests
{
private const ushort NamespaceIndex = 2;
[Test]
public async Task AttachesSynchronizerToOptedInNodeManagerAsync()
{
ITelemetryContext telemetry = NUnitTelemetryContext.Create();
var messageContext = ServiceMessageContext.CreateEmpty(telemetry);
messageContext.NamespaceUris.GetIndexOrAppend("urn:test:crdt-application");
messageContext.NamespaceUris.GetIndexOrAppend("urn:test:crdt-startup");
var identity = new ReplicaNodeIdFactory("crdt-startup", ["urn:test:crdt-startup"]);
var systemContext = new SystemContext(telemetry)
{
NamespaceUris = messageContext.NamespaceUris,
ServerUris = messageContext.ServerUris,
EncodeableFactory = messageContext.Factory
};
var addressSpace = new DictionaryAddressSpace(systemContext);
await addressSpace.AddOrUpdateNodeAsync(new BaseDataVariableState(null)
{
NodeId = new NodeId("seeded", NamespaceIndex),
BrowseName = new QualifiedName("Seeded", NamespaceIndex),
DisplayName = new LocalizedText("Seeded"),
DataType = DataTypeIds.Double,
ValueRank = ValueRanks.Scalar,
Value = new Variant(1.0)
}).ConfigureAwait(false);
var optedIn = new Mock();
optedIn.Setup(s => s.NamespaceUris).Returns(["urn:test:crdt-startup"]);
optedIn.As()
.Setup(s => s.CreateLocalAddressSpace())
.Returns(addressSpace);
var notOptedIn = new Mock();
var masterNodeManager = new Mock();
masterNodeManager.Setup(m => m.NodeManagers)
.Returns([optedIn.Object, notOptedIn.Object]);
var server = new Mock();
server.As().Setup(s => s.NodeIdFactory).Returns(identity);
server.Setup(s => s.Telemetry).Returns(telemetry);
server.Setup(s => s.MessageContext).Returns(messageContext);
server.Setup(s => s.NamespaceUris).Returns(messageContext.NamespaceUris);
server.Setup(s => s.NodeManager).Returns(masterNodeManager.Object);
server.Setup(s => s.DefaultSystemContext).Returns(new ServerSystemContext(server.Object));
server.Setup(s => s.FindNodeManagers())
.Returns([optedIn.As().Object]);
await using var task = new ReplicatedAddressSpaceStartupTask(
EmptyServices(), new ReplicatedAddressSpaceOptions());
await task.OnServerStartedAsync(server.Object).ConfigureAwait(false);
// The opted-in manager exposed its local address space.
optedIn.As().Verify(s => s.CreateLocalAddressSpace(), Times.Once);
}
[Test]
public void ConstructorRejectsNullArguments()
{
Assert.That(
() => new ReplicatedAddressSpaceStartupTask(null!, new ReplicatedAddressSpaceOptions()),
Throws.ArgumentNullException);
Assert.That(
() => new ReplicatedAddressSpaceStartupTask(EmptyServices(), null!),
Throws.ArgumentNullException);
}
private static IServiceProvider EmptyServices()
{
return Mock.Of();
}
}
}