mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-17 05:11:13 +08:00
Client: make the UaFileInfo constructor public (#4790)
# Description FileSystemClient hands out UaFileInfo objects only for files it resolves by path below its root directory. An application that already knows the NodeId of a FileType object, for example from a reference or a method result, could not get a UaFileInfo for it because the constructor was internal, and had to fall back to the raw FileTypeClient proxy. The constructor is now public and documented, and it rejects a null segment list. A test creates a UaFileInfo for a known NodeId and reads its metadata through it. ## Related Issues None. ## Checklist - [x] I have signed the [CLA](https://opcfoundation.org/license/cla/ContributorLicenseAgreementv1.0.pdf) and read the [CONTRIBUTING](https://github.com/OPCFoundation/UA-.NETStandard/blob/master/CONTRIBUTING.md) doc. - [x] I have added tests that prove my fix is effective or that my feature works and increased code coverage. - [x] I have added all necessary documentation. - [x] I have verified that my changes do not introduce (new) build or analyzer warnings. - [x] I ran **all** tests locally using the **UA.slnx** solution against at least .net **framework** and .net **10**, and all passed. - [x] I fixed **all** failing and flaky tests in the CI pipelines and **all** CodeQL warnings. - [ ] I have addressed **all** PR feedback received. --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Marc Schier <marcschier@users.noreply.github.com>
This commit is contained in:
@@ -29,6 +29,7 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using NUnit.Framework;
|
||||
@@ -122,6 +123,155 @@ namespace Opc.Ua.Client.Tests.FileSystem
|
||||
Assert.That(closes, Is.EqualTo(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FileInfoPublicConstructorsTakeNoCollectionParameter()
|
||||
{
|
||||
System.Reflection.ConstructorInfo[] constructors = typeof(UaFileInfo).GetConstructors();
|
||||
System.Reflection.ConstructorInfo? inDirectory = typeof(UaFileInfo).GetConstructor(
|
||||
[typeof(UaDirectoryInfo), typeof(NodeId), typeof(QualifiedName)]);
|
||||
System.Reflection.ConstructorInfo? detached = typeof(UaFileInfo).GetConstructor(
|
||||
[typeof(FileSystemClient), typeof(NodeId), typeof(QualifiedName)]);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(inDirectory, Is.Not.Null, "UaFileInfo(UaDirectoryInfo, NodeId, QualifiedName)");
|
||||
Assert.That(detached, Is.Not.Null, "UaFileInfo(FileSystemClient, NodeId, QualifiedName)");
|
||||
Assert.That(constructors, Has.Length.EqualTo(2));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FileInfoCreatedInAKnownDirectoryRefreshesAndDeletesAsync()
|
||||
{
|
||||
var harness = FileSystemSessionHarness.Create();
|
||||
var props = new FileProperties
|
||||
{
|
||||
Size = 4_096UL,
|
||||
Writable = false,
|
||||
UserWritable = false,
|
||||
OpenCount = 1
|
||||
};
|
||||
props.Realize();
|
||||
NodeId subdirId = harness.RegisterDirectory(harness.Root, new QualifiedName("subdir"));
|
||||
var fileId = new NodeId(8100u);
|
||||
var name = new QualifiedName("known.bin");
|
||||
harness.RegisterFile(subdirId, name, fileId, props);
|
||||
var client = new FileSystemClient(harness.Session, harness.Root);
|
||||
UaDirectoryInfo subdir = await client.GetDirectoryAsync("/subdir").ConfigureAwait(false);
|
||||
|
||||
var file = new UaFileInfo(subdir, fileId, name);
|
||||
await file.RefreshAsync().ConfigureAwait(false);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(file.Owner, Is.SameAs(client));
|
||||
Assert.That(file.Parent, Is.SameAs(subdir));
|
||||
Assert.That(file.IsDetached, Is.False);
|
||||
Assert.That(file.NodeId, Is.EqualTo(fileId));
|
||||
Assert.That(file.FullPath, Is.EqualTo("/subdir/known.bin"));
|
||||
Assert.That(file.Size, Is.EqualTo(4_096UL));
|
||||
Assert.That(file.OpenCount, Is.EqualTo(1));
|
||||
Assert.That(file.Proxy, Is.Not.Null);
|
||||
});
|
||||
|
||||
int callsBeforeDelete = harness.CallRequests.Count;
|
||||
await file.DeleteAsync().ConfigureAwait(false);
|
||||
|
||||
Assert.That(harness.CallRequests, Has.Count.EqualTo(callsBeforeDelete + 1));
|
||||
CallMethodRequest request = harness.CallRequests[^1];
|
||||
request.MethodId.TryGetValue(out uint methodId);
|
||||
request.InputArguments[0].TryGetValue(out NodeId deleted);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(methodId, Is.EqualTo(Methods.FileDirectoryType_DeleteFileSystemObject));
|
||||
Assert.That(request.ObjectId, Is.EqualTo(subdirId));
|
||||
Assert.That(deleted, Is.EqualTo(fileId));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DetachedFileInfoRefreshesButRejectsDeleteMoveAndCopyAsync()
|
||||
{
|
||||
var harness = FileSystemSessionHarness.Create();
|
||||
var props = new FileProperties
|
||||
{
|
||||
Size = 2_048UL,
|
||||
Writable = true,
|
||||
UserWritable = true,
|
||||
OpenCount = 0
|
||||
};
|
||||
props.Realize();
|
||||
var fileId = new NodeId(8101u);
|
||||
var name = new QualifiedName("result.bin");
|
||||
harness.RegisterFile(harness.Root, name, fileId, props);
|
||||
var client = new FileSystemClient(harness.Session, harness.Root);
|
||||
|
||||
var file = new UaFileInfo(client, fileId, name);
|
||||
await file.RefreshAsync().ConfigureAwait(false);
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(file.Owner, Is.SameAs(client));
|
||||
Assert.That(file.Parent, Is.Null);
|
||||
Assert.That(file.IsDetached, Is.True);
|
||||
Assert.That(file.Segments, Is.EqualTo(new[] { name }));
|
||||
Assert.That(file.Size, Is.EqualTo(2_048UL));
|
||||
});
|
||||
|
||||
int callsBeforeOperations = harness.CallRequests.Count;
|
||||
int invocationsBeforeOperations = harness.SessionMock.Invocations.Count;
|
||||
InvalidOperationException? delete = Assert.ThrowsAsync<InvalidOperationException>(
|
||||
async () => await file.DeleteAsync().ConfigureAwait(false));
|
||||
InvalidOperationException? move = Assert.ThrowsAsync<InvalidOperationException>(
|
||||
async () => await file.MoveToAsync(client.Root, "moved.bin").ConfigureAwait(false));
|
||||
InvalidOperationException? copy = Assert.ThrowsAsync<InvalidOperationException>(
|
||||
async () => await file.CopyToAsync("/copied.bin").ConfigureAwait(false));
|
||||
// A nested destination is not resolved first: the directory does
|
||||
// not exist, and the detached file is still the reason given.
|
||||
InvalidOperationException? nestedMove = Assert.ThrowsAsync<InvalidOperationException>(
|
||||
async () => await file.MoveToAsync("/missing/moved.bin").ConfigureAwait(false));
|
||||
InvalidOperationException? nestedCopy = Assert.ThrowsAsync<InvalidOperationException>(
|
||||
async () => await file.CopyToAsync("/missing/copied.bin").ConfigureAwait(false));
|
||||
IOException? root = Assert.ThrowsAsync<IOException>(
|
||||
async () => await client.Root.DeleteAsync().ConfigureAwait(false));
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(delete!.Message, Does.Contain("cannot be deleted").And.Contain("not known"));
|
||||
Assert.That(move!.Message, Does.Contain("cannot be moved").And.Contain("not known"));
|
||||
Assert.That(copy!.Message, Does.Contain("cannot be copied").And.Contain("not known"));
|
||||
Assert.That(nestedMove!.Message, Does.Contain("cannot be moved").And.Contain("not known"));
|
||||
Assert.That(nestedCopy!.Message, Does.Contain("cannot be copied").And.Contain("not known"));
|
||||
Assert.That(root!.Message, Does.Contain("root directory"));
|
||||
Assert.That(harness.CallRequests, Has.Count.EqualTo(callsBeforeOperations));
|
||||
Assert.That(harness.SessionMock.Invocations, Has.Count.EqualTo(invocationsBeforeOperations));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FileInfoConstructorsRejectMissingArguments()
|
||||
{
|
||||
var harness = FileSystemSessionHarness.Create();
|
||||
var client = new FileSystemClient(harness.Session, harness.Root);
|
||||
var name = new QualifiedName("file.bin");
|
||||
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(
|
||||
() => new UaFileInfo((UaDirectoryInfo)null!, new NodeId(1u), name),
|
||||
Throws.ArgumentNullException);
|
||||
Assert.That(
|
||||
() => new UaFileInfo(client.Root, NodeId.Null, name),
|
||||
Throws.ArgumentNullException);
|
||||
Assert.That(
|
||||
() => new UaFileInfo((FileSystemClient)null!, new NodeId(1u), name),
|
||||
Throws.ArgumentNullException);
|
||||
Assert.That(
|
||||
() => new UaFileInfo(client, NodeId.Null, name),
|
||||
Throws.ArgumentNullException);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task RefreshAsyncPopulatesMandatoryPropertiesAsync()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user