Files
UA-.NETStandard/tests/Opc.Ua.Server.Tests/ServerDataTypeSchemaRegistrationTests.cs
2026-07-16 06:25:54 +02:00

104 lines
4.3 KiB
C#

/* ========================================================================
* 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/
* ======================================================================*/
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using Opc.Ua.Schema;
namespace Opc.Ua.Server.Tests
{
/// <summary>
/// Tests server-side data type schema registration.
/// </summary>
[TestFixture]
[Category("Schema")]
[Parallelizable]
public class ServerDataTypeSchemaRegistrationTests
{
[Test]
public void RegisterDataTypeSchemasRegistersDataTypeStateDefinition()
{
const string namespaceUri = "urn:opcfoundation.org:tests:server:schema";
var namespaceUris = new NamespaceTable();
namespaceUris.GetIndexOrAppend(Types.Namespaces.OpcUa);
ushort namespaceIndex = namespaceUris.GetIndexOrAppend(namespaceUri);
var typeId = new NodeId(6001, namespaceIndex);
var dataType = new DataTypeState
{
NodeId = typeId,
BrowseName = new QualifiedName("ServerSchemaType", namespaceIndex),
SuperTypeId = DataTypeIds.Structure,
DataTypeDefinition = new ExtensionObject(new StructureDefinition
{
BaseDataType = DataTypeIds.Structure,
StructureType = StructureType.Structure,
Fields =
[
new StructureField
{
Name = "Value",
DataType = DataTypeIds.Int32,
ValueRank = ValueRanks.Scalar
}
]
})
};
var nodes = new NodeStateCollection
{
dataType,
new BaseObjectState(null)
};
var services = new ServiceCollection();
services.AddOpcUa().AddSchemaGeneration();
using ServiceProvider serviceProvider = services.BuildServiceProvider();
DataTypeDefinitionRegistry registry = serviceProvider.GetRequiredService<DataTypeDefinitionRegistry>();
int registered = nodes.RegisterDataTypeSchemas(registry, namespaceUris);
ISchemaProvider schemaProvider = serviceProvider.GetRequiredService<ISchemaProvider>();
bool resolved = schemaProvider.TryGetSchema(
new ExpandedNodeId(typeId),
UaSchemaFormat.JsonCompact,
UaSchemaScope.Type,
out IUaSchema schema);
Assert.Multiple(() =>
{
Assert.That(registered, Is.EqualTo(1));
Assert.That(registry.TryResolve(typeId, out UaTypeDescription description), Is.True);
Assert.That(description, Is.Not.Null);
Assert.That(description.NamespaceUri, Is.EqualTo(namespaceUri));
Assert.That(resolved, Is.True);
Assert.That(schema, Is.Not.Null);
});
}
}
}