86 lines
4.5 KiB
C#
86 lines
4.5 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Authorization;
|
|
using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
|
|
|
|
[Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Phase", "P08")]
|
|
public sealed class GrpcDataProjectedRouteTests
|
|
{
|
|
[Fact]
|
|
public void Routes_use_exact_origin_metadata_and_keep_original_authority_live()
|
|
{
|
|
var catalog = new Catalog();
|
|
var allowed = true;
|
|
string? connection = "link";
|
|
var source = new ProjectedAdapterRouteSource(catalog, _ => connection, "link", _ => allowed);
|
|
source.TryResolve(catalog.Reference, out var route).ShouldBeTrue();
|
|
route.ShouldNotBeNull();
|
|
route.View.ShouldBe(catalog.Proof.OriginMetadata.View);
|
|
route.View.Subject.ShouldBe("Original/Value");
|
|
route.View.MappingId.ShouldBeNull();
|
|
route.Lease.IsValid.ShouldBeTrue();
|
|
allowed = false;
|
|
route.Lease.IsValid.ShouldBeFalse();
|
|
source.TryResolve(catalog.Reference, out _).ShouldBeFalse();
|
|
allowed = true;
|
|
connection = "other-link";
|
|
route.Lease.IsValid.ShouldBeFalse();
|
|
source.TryResolve(catalog.Reference, out _).ShouldBeFalse();
|
|
connection = "link";
|
|
catalog.Proof = catalog.Proof with { Generation = 2 };
|
|
route.Lease.IsValid.ShouldBeFalse();
|
|
source.TryResolve(catalog.Reference, out var replacement).ShouldBeTrue();
|
|
replacement.ShouldNotBeNull().ProjectionGeneration.ShouldBe(2);
|
|
catalog.Authority.Active = false;
|
|
replacement.Lease.IsValid.ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void Ordinary_paths_and_another_realm_cannot_alias_a_projected_route()
|
|
{
|
|
var catalog = new Catalog();
|
|
var source = new ProjectedAdapterRouteSource(catalog, _ => "link", "link", _ => true);
|
|
source.TryResolve("Original/Value", out _).ShouldBeFalse();
|
|
source.TryResolve(ProjectedDataIds.DependencyReference(DataRealm.Uns, catalog.Proof.LocalIdentifier), out _).ShouldBeFalse();
|
|
}
|
|
|
|
private sealed class Authority : IDataMappedCatalogAuthority
|
|
{
|
|
public bool Active = true;
|
|
public bool IsActive => Active;
|
|
public long CatalogEpoch => 1;
|
|
public long AuthorityEpoch => 1;
|
|
}
|
|
private sealed class Catalog : IFederatedProjectionCatalog
|
|
{
|
|
internal readonly Authority Authority = new();
|
|
internal ProjectedNodeProof Proof;
|
|
internal string Reference => ProjectedDataIds.DependencyReference(DataRealm.Raw, Proof.LocalIdentifier);
|
|
public Catalog()
|
|
{
|
|
var serving = new ServingIdentity("node", "boot", "Primary", 1, 1, 1);
|
|
var origin = new DataView(new("source", "urn:origin", "string", "Original/Value"), DataRealm.Raw, null, 0, "Original/Value");
|
|
var mapped = origin with { MappingId = "mapping", MappingRevision = 1, Subject = "Local/Mount/Renamed" };
|
|
var subject = new MappedNamespaceSubject(mapped,
|
|
new() { ClusterId = "local", Kind = NodeHierarchyKind.Raw, Segments = ["Local", "Mount", "Renamed"] },
|
|
["folder"], serving, DataOperations.Read | DataOperations.Subscribe);
|
|
var metadata = new DataMetadata(mapped, "Renamed", "Variable", DataValueKind.Int32, -1, [], false,
|
|
DataOperations.Read | DataOperations.Subscribe, 1, null, null, false, Serving: serving);
|
|
var lease = new MetadataAuthorityLease(serving, new(1, 1, TimeSpan.FromSeconds(15)),
|
|
Clock.GetTimestamp(), TimeSpan.FromSeconds(15), TimeSpan.Zero, Clock);
|
|
Proof = new(ProjectedDataIds.Identifier("mapping", origin.Address), metadata, subject,
|
|
new([subject], 1, Authority), lease, 1, metadata with { View = origin });
|
|
}
|
|
public TimeProvider Clock => TimeProvider.System;
|
|
public event Action<string, long>? Invalidated { add { } remove { } }
|
|
public bool TryResolveProjected(DataRealm realm, string id, out ProjectedNodeProof? proof)
|
|
{ proof = realm == DataRealm.Raw && id == Proof.LocalIdentifier ? Proof : null; return proof is not null; }
|
|
public bool TryPinProjected(DataRealm realm, string id, out IDisposable? pin) { pin = null; return false; }
|
|
public ProjectedParentSnapshot TryGetParent(ProjectedBrowseRequest request) => throw new NotSupportedException();
|
|
public Task<IProjectedPreparedPage> PreparePageAsync(ProjectedBrowseRequest request, CancellationToken token) => throw new NotSupportedException();
|
|
}
|
|
}
|