mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 12:11:10 +00:00
Standardizes test projects against conventions
This commit is contained in:
1 parent
59e16d645a
commit
ced1f778c4
78 files changed
+283
-201
No files matched your search
+134
@@ -0,0 +1,134 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Entities;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.FileSystem;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.UnitTests.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Entities;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="Directory"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class DirectoryTests
|
||||
{
|
||||
[Fact]
|
||||
public void Create_WhenCalledWithValidParameters_ShouldReturnSuccessfulResult()
|
||||
{
|
||||
// Arrange
|
||||
string path = "/valid/path";
|
||||
string name = "TestDirectory";
|
||||
Optional<DateTime> dateCreated = DateTime.Now;
|
||||
Optional<DateTime> dateModified = DateTime.Now;
|
||||
|
||||
// Act
|
||||
Result<Directory> result = Directory.Create(path, name, dateCreated, dateModified);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.NotNull(result.Value);
|
||||
Assert.Equal(name, result.Value.Name);
|
||||
Assert.Equal(dateCreated, result.Value.DateCreated);
|
||||
Assert.Equal(dateModified, result.Value.DateModified);
|
||||
Assert.Equal(FileSystemItemStatus.Accessible, result.Value.Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Create_WhenCalledWithInvalidPath_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
string invalidPath = "";
|
||||
string name = "TestDirectory";
|
||||
Optional<DateTime> dateCreated = DateTime.Now;
|
||||
Optional<DateTime> dateModified = DateTime.Now;
|
||||
|
||||
// Act
|
||||
Result<Directory> result = Directory.Create(invalidPath, name, dateCreated, dateModified);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Create_WhenCalledWithFileSystemPathId_ShouldReturnSuccessfulResult()
|
||||
{
|
||||
// Arrange
|
||||
Result<FileSystemPathId> pathIdResult = FileSystemPathId.Create("/valid/path");
|
||||
Assert.False(pathIdResult.IsFailure);
|
||||
FileSystemPathId pathId = pathIdResult.Value;
|
||||
string name = "TestDirectory";
|
||||
Optional<DateTime> dateCreated = DateTime.Now;
|
||||
Optional<DateTime> dateModified = DateTime.Now;
|
||||
|
||||
// Act
|
||||
Result<Directory> result = Directory.Create(pathId, name, dateCreated, dateModified);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.NotNull(result.Value);
|
||||
Assert.Equal(pathId, result.Value.Id);
|
||||
Assert.Equal(name, result.Value.Name);
|
||||
Assert.Equal(dateCreated, result.Value.DateCreated);
|
||||
Assert.Equal(dateModified, result.Value.DateModified);
|
||||
Assert.Equal(FileSystemItemStatus.Accessible, result.Value.Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UpdateLastModified_WhenCalled_ShouldUpdateDateModified()
|
||||
{
|
||||
// Arrange
|
||||
Result<Directory> directoryResult = Directory.Create("/valid/path", "TestDirectory", Optional<DateTime>.None(), Optional<DateTime>.None());
|
||||
Assert.False(directoryResult.IsFailure);
|
||||
Directory directory = directoryResult.Value;
|
||||
DateTime newDate = DateTime.Now;
|
||||
|
||||
// Act
|
||||
Result<Updated> result = directory.UpdateLastModified(newDate);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(newDate, directory.DateModified.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddItem_WhenCalled_ShouldAddItemToCollection()
|
||||
{
|
||||
// Arrange
|
||||
Result<Directory> directoryResult = Directory.Create("/valid/path", "TestDirectory", Optional<DateTime>.None(), Optional<DateTime>.None());
|
||||
Assert.False(directoryResult.IsFailure);
|
||||
Directory directory = directoryResult.Value;
|
||||
FileSystemItem item = new FileSystemItemFixture(FileSystemPathId.Create("/mock/path").Value, "MockItem", FileSystemItemType.File);
|
||||
|
||||
// Act
|
||||
Result<Updated> result = directory.AddItem(item);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Contains(item, directory.Items);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveItem_WhenCalledWithExistingItem_ShouldRemoveItemFromCollection()
|
||||
{
|
||||
// Arrange
|
||||
Result<Directory> directoryResult = Directory.Create("/valid/path", "TestDirectory", Optional<DateTime>.None(), Optional<DateTime>.None());
|
||||
Assert.False(directoryResult.IsFailure);
|
||||
Directory directory = directoryResult.Value;
|
||||
FileSystemItem item = new FileSystemItemFixture(FileSystemPathId.Create("/mock/path").Value, "MockItem", FileSystemItemType.File);
|
||||
directory.AddItem(item);
|
||||
|
||||
// Act
|
||||
Result<Updated> result = directory.RemoveItem(item);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.DoesNotContain(item, directory.Items);
|
||||
}
|
||||
}
|
||||
+154
@@ -0,0 +1,154 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Entities;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.FileSystem;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.UnitTests.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Entities;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="File"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class FileTests
|
||||
{
|
||||
[Fact]
|
||||
public void Create_WhenCalledWithValidParameters_ShouldReturnSuccessfulResult()
|
||||
{
|
||||
// Arrange
|
||||
string path = "/valid/path/file.txt";
|
||||
string name = "file.txt";
|
||||
Optional<DateTime> dateCreated = DateTime.Now;
|
||||
Optional<DateTime> dateModified = DateTime.Now;
|
||||
long size = 1024;
|
||||
|
||||
// Act
|
||||
Result<File> result = File.Create(path, name, dateCreated, dateModified, size);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.NotNull(result.Value);
|
||||
Assert.Equal(name, result.Value.Name);
|
||||
Assert.Equal(dateCreated, result.Value.DateCreated);
|
||||
Assert.Equal(dateModified, result.Value.DateModified);
|
||||
Assert.Equal(size, result.Value.Size);
|
||||
Assert.Equal(FileSystemItemStatus.Accessible, result.Value.Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Create_WhenCalledWithInvalidPath_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
string invalidPath = "";
|
||||
string name = "file.txt";
|
||||
Optional<DateTime> dateCreated = DateTime.Now;
|
||||
Optional<DateTime> dateModified = DateTime.Now;
|
||||
long size = 1024;
|
||||
|
||||
// Act
|
||||
Result<File> result = File.Create(invalidPath, name, dateCreated, dateModified, size);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Create_WhenCalledWithFileSystemPathId_ShouldReturnSuccessfulResult()
|
||||
{
|
||||
// Arrange
|
||||
Result<FileSystemPathId> pathIdResult = FileSystemPathId.Create("/valid/path/file.txt");
|
||||
Assert.False(pathIdResult.IsFailure);
|
||||
FileSystemPathId pathId = pathIdResult.Value;
|
||||
string name = "file.txt";
|
||||
Optional<DateTime> dateCreated = DateTime.Now;
|
||||
Optional<DateTime> dateModified = DateTime.Now;
|
||||
long size = 1024;
|
||||
|
||||
// Act
|
||||
Result<File> result = File.Create(pathId, name, dateCreated, dateModified, size);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.NotNull(result.Value);
|
||||
Assert.Equal(pathId, result.Value.Id);
|
||||
Assert.Equal(name, result.Value.Name);
|
||||
Assert.Equal(dateCreated, result.Value.DateCreated);
|
||||
Assert.Equal(dateModified, result.Value.DateModified);
|
||||
Assert.Equal(size, result.Value.Size);
|
||||
Assert.Equal(FileSystemItemStatus.Accessible, result.Value.Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UpdateLastModified_WhenCalled_ShouldUpdateDateModified()
|
||||
{
|
||||
// Arrange
|
||||
Result<File> fileResult = File.Create("/valid/path/file.txt", "file.txt", Optional<DateTime>.None(), Optional<DateTime>.None(), 1024);
|
||||
Assert.False(fileResult.IsFailure);
|
||||
File file = fileResult.Value;
|
||||
DateTime newDate = DateTime.Now;
|
||||
|
||||
// Act
|
||||
Result<Updated> result = file.UpdateLastModified(newDate);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(newDate, file.DateModified.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UpdateSize_WhenCalled_ShouldUpdateFileSize()
|
||||
{
|
||||
// Arrange
|
||||
Result<File> fileResult = File.Create("/valid/path/file.txt", "file.txt", Optional<DateTime>.None(), Optional<DateTime>.None(), 1024);
|
||||
Assert.False(fileResult.IsFailure);
|
||||
File file = fileResult.Value;
|
||||
long newSize = 2048;
|
||||
|
||||
// Act
|
||||
Result<Updated> result = file.UpdateSize(newSize);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(newSize, file.Size);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rename_WhenCalledWithValidName_ShouldUpdateFileName()
|
||||
{
|
||||
// Arrange
|
||||
Result<File> fileResult = File.Create("/valid/path/file.txt", "file.txt", Optional<DateTime>.None(), Optional<DateTime>.None(), 1024);
|
||||
Assert.False(fileResult.IsFailure);
|
||||
File file = fileResult.Value;
|
||||
string newName = "newfile.txt";
|
||||
|
||||
// Act
|
||||
Result<Updated> result = file.Rename(newName);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(newName, file.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Rename_WhenCalledWithEmptyName_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
Result<File> fileResult = File.Create("/valid/path/file.txt", "file.txt", Optional<DateTime>.None(), Optional<DateTime>.None(), 1024);
|
||||
Assert.False(fileResult.IsFailure);
|
||||
File file = fileResult.Value;
|
||||
string emptyName = "";
|
||||
|
||||
// Act
|
||||
Result<Updated> result = file.Rename(emptyName);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.NameCannotBeEmpty, result.FirstError);
|
||||
Assert.Equal("file.txt", file.Name);
|
||||
}
|
||||
}
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Entities;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.FileSystem;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.UnitTests.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Entities;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="UnixRootItem"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class UnixRootItemTests
|
||||
{
|
||||
[Fact]
|
||||
public void Create_WhenCalledWithDefaultStatus_ShouldReturnSuccessfulResult()
|
||||
{
|
||||
// Arrange & Act
|
||||
Result<UnixRootItem> result = UnixRootItem.Create();
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.NotNull(result.Value);
|
||||
Assert.Equal("/", result.Value.Name);
|
||||
Assert.Equal("/", result.Value.Id.Path);
|
||||
Assert.Equal(FileSystemItemType.Root, result.Value.Type);
|
||||
Assert.Equal(FileSystemItemStatus.Accessible, result.Value.Status);
|
||||
Assert.Empty(result.Value.Items);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Create_WhenCalledWithCustomStatus_ShouldReturnSuccessfulResultWithSpecifiedStatus()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemItemStatus customStatus = FileSystemItemStatus.Accessible;
|
||||
|
||||
// Act
|
||||
Result<UnixRootItem> result = UnixRootItem.Create(customStatus);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.NotNull(result.Value);
|
||||
Assert.Equal("/", result.Value.Name);
|
||||
Assert.Equal("/", result.Value.Id.Path);
|
||||
Assert.Equal(FileSystemItemType.Root, result.Value.Type);
|
||||
Assert.Equal(customStatus, result.Value.Status);
|
||||
Assert.Empty(result.Value.Items);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Items_WhenAccessed_ShouldReturnEmptyReadOnlyCollection()
|
||||
{
|
||||
// Arrange
|
||||
Result<UnixRootItem> createResult = UnixRootItem.Create();
|
||||
Assert.False(createResult.IsFailure);
|
||||
UnixRootItem unixRootItem = createResult.Value;
|
||||
|
||||
// Act
|
||||
IReadOnlyCollection<FileSystemItem> items = unixRootItem.Items;
|
||||
|
||||
// Assert
|
||||
Assert.Empty(items);
|
||||
Assert.IsAssignableFrom<IReadOnlyCollection<FileSystemItem>>(items);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetStatus_WhenCalledWithNewStatus_ShouldUpdateStatus()
|
||||
{
|
||||
// Arrange
|
||||
Result<UnixRootItem> createResult = UnixRootItem.Create();
|
||||
Assert.False(createResult.IsFailure);
|
||||
UnixRootItem unixRootItem = createResult.Value;
|
||||
FileSystemItemStatus newStatus = FileSystemItemStatus.Accessible;
|
||||
|
||||
// Act
|
||||
Result<Updated> result = unixRootItem.SetStatus(newStatus);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(newStatus, unixRootItem.Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetParent_WhenCalledWithNullParent_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
Result<UnixRootItem> createResult = UnixRootItem.Create();
|
||||
Assert.False(createResult.IsFailure);
|
||||
UnixRootItem unixRootItem = createResult.Value;
|
||||
|
||||
// Act
|
||||
Result<Updated> result = unixRootItem.SetParent(null!);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.ParentNodeCannotBeNull, result.FirstError);
|
||||
Assert.False(unixRootItem.Parent.HasValue);
|
||||
}
|
||||
}
|
||||
+145
@@ -0,0 +1,145 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Entities;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.FileSystem;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.UnitTests.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Entities;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="WindowsRootItem"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class WindowsRootItemTests
|
||||
{
|
||||
[Fact]
|
||||
public void Create_WhenCalledWithValidParameters_ShouldReturnSuccessfulResult()
|
||||
{
|
||||
// Arrange
|
||||
string path = "C:\\";
|
||||
string name = "C:";
|
||||
|
||||
// Act
|
||||
Result<WindowsRootItem> result = WindowsRootItem.Create(path, name);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.NotNull(result.Value);
|
||||
Assert.Equal(name, result.Value.Name);
|
||||
Assert.Equal(path, result.Value.Id.Path);
|
||||
Assert.Equal(FileSystemItemType.Root, result.Value.Type);
|
||||
Assert.Equal(FileSystemItemStatus.Accessible, result.Value.Status);
|
||||
Assert.Empty(result.Value.Items);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Create_WhenCalledWithInvalidPath_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
string invalidPath = "";
|
||||
string name = "Invalid";
|
||||
|
||||
// Act
|
||||
Result<WindowsRootItem> result = WindowsRootItem.Create(invalidPath, name);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Create_WhenCalledWithCustomStatus_ShouldReturnSuccessfulResultWithSpecifiedStatus()
|
||||
{
|
||||
// Arrange
|
||||
string path = "D:\\";
|
||||
string name = "D:";
|
||||
FileSystemItemStatus customStatus = FileSystemItemStatus.Accessible;
|
||||
|
||||
// Act
|
||||
Result<WindowsRootItem> result = WindowsRootItem.Create(path, name, customStatus);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.NotNull(result.Value);
|
||||
Assert.Equal(name, result.Value.Name);
|
||||
Assert.Equal(path, result.Value.Id.Path);
|
||||
Assert.Equal(FileSystemItemType.Root, result.Value.Type);
|
||||
Assert.Equal(customStatus, result.Value.Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Create_WhenCalledWithFileSystemPathId_ShouldReturnSuccessfulResult()
|
||||
{
|
||||
// Arrange
|
||||
Result<FileSystemPathId> pathIdResult = FileSystemPathId.Create("E:\\");
|
||||
Assert.False(pathIdResult.IsFailure);
|
||||
FileSystemPathId pathId = pathIdResult.Value;
|
||||
string name = "E:";
|
||||
|
||||
// Act
|
||||
Result<WindowsRootItem> result = WindowsRootItem.Create(pathId, name);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.NotNull(result.Value);
|
||||
Assert.Equal(pathId, result.Value.Id);
|
||||
Assert.Equal(name, result.Value.Name);
|
||||
Assert.Equal(FileSystemItemType.Root, result.Value.Type);
|
||||
Assert.Equal(FileSystemItemStatus.Accessible, result.Value.Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Items_WhenAccessed_ShouldReturnEmptyReadOnlyCollection()
|
||||
{
|
||||
// Arrange
|
||||
Result<WindowsRootItem> createResult = WindowsRootItem.Create("F:\\", "F:");
|
||||
Assert.False(createResult.IsFailure);
|
||||
WindowsRootItem windowsRootItem = createResult.Value;
|
||||
|
||||
// Act
|
||||
IReadOnlyCollection<FileSystemItem> items = windowsRootItem.Items;
|
||||
|
||||
// Assert
|
||||
Assert.Empty(items);
|
||||
Assert.IsAssignableFrom<IReadOnlyCollection<FileSystemItem>>(items);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetStatus_WhenCalledWithNewStatus_ShouldUpdateStatus()
|
||||
{
|
||||
// Arrange
|
||||
Result<WindowsRootItem> createResult = WindowsRootItem.Create("G:\\", "G:");
|
||||
Assert.False(createResult.IsFailure);
|
||||
WindowsRootItem windowsRootItem = createResult.Value;
|
||||
FileSystemItemStatus newStatus = FileSystemItemStatus.Accessible;
|
||||
|
||||
// Act
|
||||
Result<Updated> result = windowsRootItem.SetStatus(newStatus);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(newStatus, windowsRootItem.Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetParent_WhenCalledWithNullParent_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
Result<WindowsRootItem> createResult = WindowsRootItem.Create("H:\\", "H:");
|
||||
Assert.False(createResult.IsFailure);
|
||||
WindowsRootItem windowsRootItem = createResult.Value;
|
||||
|
||||
// Act
|
||||
Result<Updated> result = windowsRootItem.SetParent(null!);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.ParentNodeCannotBeNull, result.FirstError);
|
||||
Assert.False(windowsRootItem.Parent.HasValue);
|
||||
}
|
||||
}
|
||||
+166
@@ -0,0 +1,166 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using AutoFixture;
|
||||
using AutoFixture.AutoNSubstitute;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.FileSystem;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.UnitTests.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="FileSystemItem"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class FileSystemItemTests
|
||||
{
|
||||
private readonly IFixture _fixture;
|
||||
private readonly FileSystemPathIdFixture _fileSystemPathIdFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="FileSystemItemTests"/> class.
|
||||
/// </summary>
|
||||
public FileSystemItemTests()
|
||||
{
|
||||
_fixture = new Fixture().Customize(new AutoNSubstituteCustomization());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WhenCalled_ShouldSetPropertiesCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId id = _fileSystemPathIdFixture.Create();
|
||||
string name = "TestItem";
|
||||
FileSystemItemType type = FileSystemItemType.File;
|
||||
|
||||
// Act
|
||||
FileSystemItemFixture item = new(id, name, type);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(id, item.Id);
|
||||
Assert.Equal(name, item.Name);
|
||||
Assert.Equal(type, item.Type);
|
||||
Assert.Equal(FileSystemItemStatus.Accessible, item.Status);
|
||||
Assert.Equal(Optional<FileSystemItem>.None(), item.Parent);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetStatus_WhenCalled_ShouldUpdateStatusAndReturnUpdated()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemItemFixture item = CreateFileSystemItemFixture();
|
||||
FileSystemItemStatus newStatus = FileSystemItemStatus.Inaccessible;
|
||||
|
||||
// Act
|
||||
Result<Updated> result = item.SetStatus(newStatus);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(Result.Updated, result.Value);
|
||||
Assert.Equal(newStatus, item.Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetParent_WithValidParent_ShouldUpdateParentAndReturnUpdated()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemItemFixture item = CreateFileSystemItemFixture();
|
||||
FileSystemItemFixture parent = CreateFileSystemItemFixture();
|
||||
|
||||
// Act
|
||||
Result<Updated> result = item.SetParent(parent);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(Result.Updated, result.Value);
|
||||
Assert.Equal(Optional<FileSystemItem>.Some(parent), item.Parent);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetParent_WithNullParent_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemItemFixture item = CreateFileSystemItemFixture();
|
||||
|
||||
// Act
|
||||
Result<Updated> result = item.SetParent(null!);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.ParentNodeCannotBeNull, result.FirstError);
|
||||
Assert.Equal(Optional<FileSystemItem>.None(), item.Parent);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equals_WithSameId_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId id = _fileSystemPathIdFixture.Create();
|
||||
FileSystemItemFixture item1 = CreateFileSystemItemFixture(id);
|
||||
FileSystemItemFixture item2 = CreateFileSystemItemFixture(id);
|
||||
|
||||
// Act
|
||||
bool result = item1.Equals(item2);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equals_WithDifferentId_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemItemFixture item1 = CreateFileSystemItemFixture();
|
||||
FileSystemItemFixture item2 = CreateFileSystemItemFixture();
|
||||
|
||||
// Act
|
||||
bool result = item1.Equals(item2);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetHashCode_WithSameId_ShouldReturnSameHashCode()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId id = _fileSystemPathIdFixture.Create();
|
||||
FileSystemItemFixture item1 = CreateFileSystemItemFixture(id);
|
||||
FileSystemItemFixture item2 = CreateFileSystemItemFixture(id);
|
||||
|
||||
// Act
|
||||
int hashCode1 = item1.GetHashCode();
|
||||
int hashCode2 = item2.GetHashCode();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(hashCode1, hashCode2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetHashCode_WithDifferentId_ShouldReturnDifferentHashCode()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemItemFixture item1 = CreateFileSystemItemFixture();
|
||||
FileSystemItemFixture item2 = CreateFileSystemItemFixture();
|
||||
|
||||
// Act
|
||||
int hashCode1 = item1.GetHashCode();
|
||||
int hashCode2 = item2.GetHashCode();
|
||||
|
||||
// Assert
|
||||
Assert.NotEqual(hashCode1, hashCode2);
|
||||
}
|
||||
|
||||
private FileSystemItemFixture CreateFileSystemItemFixture(FileSystemPathId? id = null)
|
||||
{
|
||||
id ??= _fileSystemPathIdFixture.Create();
|
||||
string name = _fixture.Create<string>();
|
||||
FileSystemItemType type = _fixture.Create<FileSystemItemType>();
|
||||
return new FileSystemItemFixture(id, name, type);
|
||||
}
|
||||
}
|
||||
+1009
File diff suppressed because it is too large.
Load diff
+807
@@ -0,0 +1,807 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Entities;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Services;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Strategies.Environment;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Strategies.Platform;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Entities;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.FileSystem;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.UnitTests.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="DirectoryService"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class DirectoryServiceTests
|
||||
{
|
||||
private readonly IEnvironmentContext _mockEnvironmentContext;
|
||||
private readonly IPlatformContextManager _mockPlatformContextManager;
|
||||
private readonly IPlatformContext _mockPlatformContext;
|
||||
private readonly DirectoryService _sut;
|
||||
private readonly FileSystemPathIdFixture _fileSystemPathIdFixture = new();
|
||||
private readonly DirectoryFixture _directoryFixture = new();
|
||||
private static readonly bool s_isUnix = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
|
||||
private readonly string _pathDirTest = s_isUnix ? "/TestDir" : @"C:\TestDir";
|
||||
private readonly string _pathDirTestSubDir1 = s_isUnix ? "/TestDir/Sub1" : @"C:\TestDir\Sub1";
|
||||
private readonly string _pathDirTestSubDir2 = s_isUnix ? "/TestDir/Sub2" : @"C:\TestDir\Sub2";
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DirectoryServiceTests"/> class.
|
||||
/// </summary>
|
||||
public DirectoryServiceTests()
|
||||
{
|
||||
_mockEnvironmentContext = Substitute.For<IEnvironmentContext>();
|
||||
_mockPlatformContextManager = Substitute.For<IPlatformContextManager>();
|
||||
_mockPlatformContext = Substitute.For<IPlatformContext>();
|
||||
_mockPlatformContextManager.GetCurrentContext().Returns(_mockPlatformContext);
|
||||
_sut = new DirectoryService(_mockEnvironmentContext, _mockPlatformContextManager);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetSubdirectories_WithValidPath_ShouldReturnListOfDirectories()
|
||||
{
|
||||
// Arrange
|
||||
string path = _pathDirTest;
|
||||
bool includeHiddenElements = false;
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(path);
|
||||
FileSystemPathId[] subPaths =
|
||||
[
|
||||
_fileSystemPathIdFixture.Create(_pathDirTestSubDir1),
|
||||
_fileSystemPathIdFixture.Create(_pathDirTestSubDir2)
|
||||
];
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetSubdirectoryPaths(pathId, includeHiddenElements)
|
||||
.Returns(Result.From(subPaths.AsEnumerable()));
|
||||
|
||||
foreach (FileSystemPathId subPath in subPaths)
|
||||
{
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetFileName(subPath)
|
||||
.Returns(Result.From(System.IO.Path.GetFileName(subPath.Path)));
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetLastWriteTime(subPath)
|
||||
.Returns(Result.From(Optional<DateTime>.FromNullable(DateTime.Now)));
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetCreationTime(subPath)
|
||||
.Returns(Result.From(Optional<DateTime>.FromNullable(DateTime.Now.AddDays(-1))));
|
||||
}
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<Directory>> result = _sut.GetSubdirectories(path, includeHiddenElements);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(2, result.Value.Count());
|
||||
Assert.Equal(["Sub1", "Sub2"], result.Value.Select(d => d.Name));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetSubdirectories_WithInvalidPath_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
string invalidPath = string.Empty;
|
||||
bool includeHiddenElements = false;
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<Directory>> result = _sut.GetSubdirectories(invalidPath, includeHiddenElements);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetSubdirectories_WhenDirectoryProviderServiceReturnsError_ShouldPropagateError()
|
||||
{
|
||||
// Arrange
|
||||
string path = _pathDirTest;
|
||||
bool includeHiddenElements = false;
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(path);
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetSubdirectoryPaths(pathId, includeHiddenElements)
|
||||
.Returns(Errors.Permission.UnauthorizedAccess);
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<Directory>> result = _sut.GetSubdirectories(path, includeHiddenElements);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Permission.UnauthorizedAccess, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetSubdirectories_WhenSubdirectoryDetailsAreInaccessible_ShouldReturnInaccessibleDirectory()
|
||||
{
|
||||
// Arrange
|
||||
string path = _pathDirTest;
|
||||
bool includeHiddenElements = false;
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(path);
|
||||
FileSystemPathId subPath = _fileSystemPathIdFixture.Create(
|
||||
s_isUnix ? "/TestDir/InaccessibleSub" : @"C:\TestDir\InaccessibleSub"
|
||||
);
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetSubdirectoryPaths(pathId, includeHiddenElements)
|
||||
.Returns(Result.From(new[] { subPath }.AsEnumerable()));
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetFileName(subPath)
|
||||
.Returns(Errors.Permission.UnauthorizedAccess);
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetLastWriteTime(subPath)
|
||||
.Returns(Errors.Permission.UnauthorizedAccess);
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetCreationTime(subPath)
|
||||
.Returns(Errors.Permission.UnauthorizedAccess);
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<Directory>> result = _sut.GetSubdirectories(path, includeHiddenElements);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Single(result.Value);
|
||||
Assert.Equal(FileSystemItemStatus.Inaccessible, result.Value.First().Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetSubdirectoriesOverload_WithValidDirectory_ShouldReturnListOfDirectories()
|
||||
{
|
||||
// Arrange
|
||||
Directory parentDirectory = _directoryFixture.Create();
|
||||
bool includeHiddenElements = false;
|
||||
FileSystemPathId[] subPaths =
|
||||
[
|
||||
_fileSystemPathIdFixture.Create(_pathDirTestSubDir1),
|
||||
_fileSystemPathIdFixture.Create(_pathDirTestSubDir2)
|
||||
];
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetSubdirectoryPaths(parentDirectory.Id, includeHiddenElements)
|
||||
.Returns(Result.From(subPaths.AsEnumerable()));
|
||||
|
||||
foreach (FileSystemPathId subPath in subPaths)
|
||||
{
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetFileName(subPath)
|
||||
.Returns(Result.From(System.IO.Path.GetFileName(subPath.Path)));
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetLastWriteTime(subPath)
|
||||
.Returns(Result.From(Optional<DateTime>.FromNullable(DateTime.Now)));
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetCreationTime(subPath)
|
||||
.Returns(Result.From(Optional<DateTime>.FromNullable(DateTime.Now.AddDays(-1))));
|
||||
}
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<Directory>> result = _sut.GetSubdirectories(parentDirectory, includeHiddenElements);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(2, result.Value.Count());
|
||||
Assert.Equal(["Sub1", "Sub2"], result.Value.Select(d => d.Name));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetSubdirectoriesOverload_WhenDirectoryProviderServiceReturnsError_ShouldPropagateError()
|
||||
{
|
||||
// Arrange
|
||||
Directory parentDirectory = _directoryFixture.Create();
|
||||
bool includeHiddenElements = false;
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetSubdirectoryPaths(parentDirectory.Id, includeHiddenElements)
|
||||
.Returns(Errors.Permission.UnauthorizedAccess);
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<Directory>> result = _sut.GetSubdirectories(parentDirectory, includeHiddenElements);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Permission.UnauthorizedAccess, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetSubdirectoriesOverload_WhenSubdirectoryDetailsAreInaccessible_ShouldReturnInaccessibleDirectory()
|
||||
{
|
||||
// Arrange
|
||||
Directory parentDirectory = _directoryFixture.Create();
|
||||
bool includeHiddenElements = false;
|
||||
FileSystemPathId subPath = _fileSystemPathIdFixture.Create(
|
||||
s_isUnix ? "/TestDir/InaccessibleSub" : @"C:\TestDir\InaccessibleSub"
|
||||
);
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetSubdirectoryPaths(parentDirectory.Id, includeHiddenElements)
|
||||
.Returns(Result.From(new[] { subPath }.AsEnumerable()));
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetFileName(subPath)
|
||||
.Returns(Errors.Permission.UnauthorizedAccess);
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetLastWriteTime(subPath)
|
||||
.Returns(Errors.Permission.UnauthorizedAccess);
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetCreationTime(subPath)
|
||||
.Returns(Errors.Permission.UnauthorizedAccess);
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<Directory>> result = _sut.GetSubdirectories(parentDirectory, includeHiddenElements);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Single(result.Value);
|
||||
Assert.Equal(FileSystemItemStatus.Inaccessible, result.Value.First().Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetSubdirectories_WithValidFileSystemPathId_ShouldReturnListOfDirectories()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(_pathDirTest);
|
||||
bool includeHiddenElements = false;
|
||||
FileSystemPathId[] subPaths =
|
||||
[
|
||||
_fileSystemPathIdFixture.Create(_pathDirTestSubDir1),
|
||||
_fileSystemPathIdFixture.Create(_pathDirTestSubDir2)
|
||||
];
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetSubdirectoryPaths(pathId, includeHiddenElements)
|
||||
.Returns(Result.From(subPaths.AsEnumerable()));
|
||||
|
||||
foreach (FileSystemPathId subPath in subPaths)
|
||||
{
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetFileName(subPath)
|
||||
.Returns(Result.From(System.IO.Path.GetFileName(subPath.Path)));
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetLastWriteTime(subPath)
|
||||
.Returns(Result.From(Optional<DateTime>.FromNullable(DateTime.Now)));
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetCreationTime(subPath)
|
||||
.Returns(Result.From(Optional<DateTime>.FromNullable(DateTime.Now.AddDays(-1))));
|
||||
}
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<Directory>> result = _sut.GetSubdirectories(pathId, includeHiddenElements);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(2, result.Value.Count());
|
||||
Assert.Equal(["Sub1", "Sub2"], result.Value.Select(d => d.Name));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetSubdirectoriesWithFileSystemPathId_WhenDirectoryProviderServiceReturnsError_ShouldPropagateError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(_pathDirTest);
|
||||
bool includeHiddenElements = false;
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetSubdirectoryPaths(pathId, includeHiddenElements)
|
||||
.Returns(Errors.Permission.UnauthorizedAccess);
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<Directory>> result = _sut.GetSubdirectories(pathId, includeHiddenElements);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Permission.UnauthorizedAccess, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetSubdirectoriesWithFileSystemPathId_WhenSubdirectoryDetailsAreInaccessible_ShouldReturnInaccessibleDirectory()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(_pathDirTest);
|
||||
bool includeHiddenElements = false;
|
||||
FileSystemPathId subPath = _fileSystemPathIdFixture.Create(
|
||||
s_isUnix ? "/TestDir/InaccessibleSub" : @"C:\TestDir\InaccessibleSub"
|
||||
);
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetSubdirectoryPaths(pathId, includeHiddenElements)
|
||||
.Returns(Result.From(new[] { subPath }.AsEnumerable()));
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetFileName(subPath)
|
||||
.Returns(Errors.Permission.UnauthorizedAccess);
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetLastWriteTime(subPath)
|
||||
.Returns(Errors.Permission.UnauthorizedAccess);
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetCreationTime(subPath)
|
||||
.Returns(Errors.Permission.UnauthorizedAccess);
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<Directory>> result = _sut.GetSubdirectories(pathId, includeHiddenElements);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Single(result.Value);
|
||||
Assert.Equal(FileSystemItemStatus.Inaccessible, result.Value.First().Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateDirectory_WithValidStringPath_ShouldReturnNewDirectory()
|
||||
{
|
||||
// Arrange
|
||||
string parentPath = _pathDirTest;
|
||||
string newDirName = "NewDirectory";
|
||||
FileSystemPathId parentPathId = _fileSystemPathIdFixture.Create(parentPath);
|
||||
FileSystemPathId newDirPathId = _fileSystemPathIdFixture.Create(System.IO.Path.Combine(parentPath, newDirName));
|
||||
|
||||
_mockPlatformContext.PathStrategy.CombinePath(parentPathId, newDirName).Returns(Result.From(newDirPathId));
|
||||
_mockEnvironmentContext.DirectoryProviderService.DirectoryExists(newDirPathId).Returns(Result.From(false));
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.CreateDirectory(parentPathId, newDirName)
|
||||
.Returns(Result.From(newDirPathId));
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetFileName(newDirPathId)
|
||||
.Returns(Result.From(newDirName));
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetLastWriteTime(newDirPathId)
|
||||
.Returns(Result.From(Optional<DateTime>.FromNullable(DateTime.Now)));
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetCreationTime(newDirPathId)
|
||||
.Returns(Result.From(Optional<DateTime>.FromNullable(DateTime.Now)));
|
||||
|
||||
// Act
|
||||
Result<Directory> result = _sut.CreateDirectory(parentPath, newDirName);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(newDirPathId, result.Value.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateDirectory_WithInvalidStringPath_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
string invalidPath = string.Empty;
|
||||
string newDirName = "NewDirectory";
|
||||
|
||||
// Act
|
||||
Result<Directory> result = _sut.CreateDirectory(invalidPath, newDirName);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateDirectory_WithFileSystemPathId_ShouldReturnNewDirectory()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId parentPathId = _fileSystemPathIdFixture.Create(_pathDirTest);
|
||||
string newDirName = "NewDirectory";
|
||||
FileSystemPathId newDirPathId = _fileSystemPathIdFixture.Create(System.IO.Path.Combine(parentPathId.Path, newDirName));
|
||||
|
||||
_mockPlatformContext.PathStrategy.CombinePath(parentPathId, newDirName).Returns(Result.From(newDirPathId));
|
||||
_mockEnvironmentContext.DirectoryProviderService.DirectoryExists(newDirPathId).Returns(Result.From(false));
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.CreateDirectory(parentPathId, newDirName)
|
||||
.Returns(Result.From(newDirPathId));
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetFileName(newDirPathId)
|
||||
.Returns(Result.From(newDirName));
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetLastWriteTime(newDirPathId)
|
||||
.Returns(Result.From(Optional<DateTime>.FromNullable(DateTime.Now)));
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetCreationTime(newDirPathId)
|
||||
.Returns(Result.From(Optional<DateTime>.FromNullable(DateTime.Now)));
|
||||
|
||||
// Act
|
||||
Result<Directory> result = _sut.CreateDirectory(parentPathId, newDirName);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(newDirPathId, result.Value.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateDirectory_WhenDirectoryProviderServiceReturnsError_ShouldPropagateError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId parentPathId = _fileSystemPathIdFixture.Create(_pathDirTest);
|
||||
string newDirName = "NewDirectory";
|
||||
FileSystemPathId newDirPathId = _fileSystemPathIdFixture.Create(System.IO.Path.Combine(parentPathId.Path, newDirName));
|
||||
|
||||
_mockPlatformContext.PathStrategy.CombinePath(parentPathId, newDirName).Returns(Result.From(newDirPathId));
|
||||
_mockEnvironmentContext.DirectoryProviderService.DirectoryExists(newDirPathId).Returns(Result.From(false));
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.CreateDirectory(parentPathId, newDirName)
|
||||
.Returns(Errors.Permission.UnauthorizedAccess);
|
||||
|
||||
// Act
|
||||
Result<Directory> result = _sut.CreateDirectory(parentPathId, newDirName);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Permission.UnauthorizedAccess, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateDirectory_WhenDirectoryDetailsAreInaccessible_ShouldReturnInaccessibleDirectory()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId parentPathId = _fileSystemPathIdFixture.Create(_pathDirTest);
|
||||
string newDirName = "NewDirectory";
|
||||
FileSystemPathId newDirPathId = _fileSystemPathIdFixture.Create(System.IO.Path.Combine(parentPathId.Path, newDirName));
|
||||
|
||||
_mockPlatformContext.PathStrategy.CombinePath(parentPathId, newDirName).Returns(Result.From(newDirPathId));
|
||||
_mockEnvironmentContext.DirectoryProviderService.DirectoryExists(newDirPathId).Returns(Result.From(false));
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.CreateDirectory(parentPathId, newDirName)
|
||||
.Returns(Result.From(newDirPathId));
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetFileName(newDirPathId)
|
||||
.Returns(Errors.Permission.UnauthorizedAccess);
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetLastWriteTime(newDirPathId)
|
||||
.Returns(Errors.Permission.UnauthorizedAccess);
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetCreationTime(newDirPathId)
|
||||
.Returns(Errors.Permission.UnauthorizedAccess);
|
||||
|
||||
// Act
|
||||
Result<Directory> result = _sut.CreateDirectory(parentPathId, newDirName);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(FileSystemItemStatus.Inaccessible, result.Value.Status);
|
||||
Assert.Equal(newDirPathId, result.Value.Id);
|
||||
Assert.False(result.Value.DateCreated.HasValue);
|
||||
Assert.False(result.Value.DateModified.HasValue);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateDirectory_WhenDirectoryExistsReturnsError_ShouldPropagateError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId parentPathId = _fileSystemPathIdFixture.Create(_pathDirTest);
|
||||
string newDirName = "NewDirectory";
|
||||
FileSystemPathId newDirPathId = _fileSystemPathIdFixture.Create(System.IO.Path.Combine(parentPathId.Path, newDirName));
|
||||
|
||||
_mockPlatformContext.PathStrategy.CombinePath(parentPathId, newDirName).Returns(Result.From(newDirPathId));
|
||||
_mockEnvironmentContext.DirectoryProviderService.DirectoryExists(newDirPathId)
|
||||
.Returns(Errors.Permission.UnauthorizedAccess);
|
||||
|
||||
// Act
|
||||
Result<Directory> result = _sut.CreateDirectory(parentPathId, newDirName);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Permission.UnauthorizedAccess, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateDirectory_WhenDirectoryAlreadyExists_ShouldReturnDirectoryAlreadyExistsError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId parentPathId = _fileSystemPathIdFixture.Create(_pathDirTest);
|
||||
string newDirName = "ExistingDirectory";
|
||||
FileSystemPathId newDirPathId = _fileSystemPathIdFixture.Create(System.IO.Path.Combine(parentPathId.Path, newDirName));
|
||||
_mockPlatformContext.PathStrategy.CombinePath(parentPathId, newDirName).Returns(Result.From(newDirPathId));
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.DirectoryExists(newDirPathId).Returns(true);
|
||||
|
||||
// Act
|
||||
Result<Directory> result = _sut.CreateDirectory(parentPathId, newDirName);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.DirectoryAlreadyExists, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateDirectory_WhenCombinePathReturnsError_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId parentPathId = _fileSystemPathIdFixture.Create(_pathDirTest);
|
||||
string newDirName = "NewDirectory";
|
||||
|
||||
_mockPlatformContext.PathStrategy.CombinePath(parentPathId, newDirName)
|
||||
.Returns(Errors.FileSystemManagement.InvalidPath);
|
||||
|
||||
// Act
|
||||
Result<Directory> result = _sut.CreateDirectory(parentPathId, newDirName);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
|
||||
// Verify that DirectoryExists was not called
|
||||
_mockEnvironmentContext.DirectoryProviderService.DidNotReceive().DirectoryExists(Arg.Any<FileSystemPathId>());
|
||||
_mockEnvironmentContext.DirectoryProviderService.DidNotReceive().CreateDirectory(Arg.Any<FileSystemPathId>(), Arg.Any<string>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RenameDirectory_WithValidStringPath_ShouldReturnRenamedDirectory()
|
||||
{
|
||||
// Arrange
|
||||
string oldPath = s_isUnix ? "/TestDir/OldName" : @"C:\TestDir\OldName";
|
||||
string newName = "NewName";
|
||||
FileSystemPathId oldPathId = _fileSystemPathIdFixture.Create(oldPath);
|
||||
FileSystemPathId newPathId = _fileSystemPathIdFixture.Create(
|
||||
s_isUnix ? "/TestDir/NewName" : @"C:\TestDir\NewName"
|
||||
);
|
||||
|
||||
_mockPlatformContext.PathStrategy.CombinePath(oldPathId, newName).Returns(Result.From(newPathId));
|
||||
_mockEnvironmentContext.DirectoryProviderService.DirectoryExists(newPathId).Returns(Result.From(false));
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.RenameDirectory(oldPathId, newName)
|
||||
.Returns(Result.From(newPathId));
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetFileName(newPathId)
|
||||
.Returns(Result.From(newName));
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetLastWriteTime(newPathId)
|
||||
.Returns(Result.From(Optional<DateTime>.FromNullable(DateTime.Now)));
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetCreationTime(newPathId)
|
||||
.Returns(Result.From(Optional<DateTime>.FromNullable(DateTime.Now)));
|
||||
|
||||
// Act
|
||||
Result<Directory> result = _sut.RenameDirectory(oldPath, newName);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(newPathId, result.Value.Id);
|
||||
Assert.Equal(newName, result.Value.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RenameDirectory_WithInvalidStringPath_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
string invalidPath = string.Empty;
|
||||
string newName = "NewName";
|
||||
|
||||
// Act
|
||||
Result<Directory> result = _sut.RenameDirectory(invalidPath, newName);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RenameDirectory_WithFileSystemPathId_ShouldReturnRenamedDirectory()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId oldPathId = _fileSystemPathIdFixture.Create(
|
||||
s_isUnix ? "/TestDir/OldName" : @"C:\TestDir\OldName"
|
||||
);
|
||||
string newName = "NewName";
|
||||
FileSystemPathId newPathId = _fileSystemPathIdFixture.Create(
|
||||
s_isUnix ? "/TestDir/NewName" : @"C:\TestDir\NewName"
|
||||
);
|
||||
|
||||
_mockPlatformContext.PathStrategy.CombinePath(oldPathId, newName).Returns(Result.From(newPathId));
|
||||
_mockEnvironmentContext.DirectoryProviderService.DirectoryExists(newPathId).Returns(Result.From(false));
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.RenameDirectory(oldPathId, newName)
|
||||
.Returns(Result.From(newPathId));
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetFileName(newPathId)
|
||||
.Returns(Result.From(newName));
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetLastWriteTime(newPathId)
|
||||
.Returns(Result.From(Optional<DateTime>.FromNullable(DateTime.Now)));
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetCreationTime(newPathId)
|
||||
.Returns(Result.From(Optional<DateTime>.FromNullable(DateTime.Now)));
|
||||
|
||||
// Act
|
||||
Result<Directory> result = _sut.RenameDirectory(oldPathId, newName);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(newPathId, result.Value.Id);
|
||||
Assert.Equal(newName, result.Value.Name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RenameDirectory_WhenDirectoryProviderServiceReturnsError_ShouldPropagateError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId oldPathId = _fileSystemPathIdFixture.Create(
|
||||
s_isUnix ? "/TestDir/OldName" : @"C:\TestDir\OldName"
|
||||
);
|
||||
string newName = "NewName";
|
||||
FileSystemPathId newPathId = _fileSystemPathIdFixture.Create(
|
||||
s_isUnix ? "/TestDir/NewName" : @"C:\TestDir\NewName"
|
||||
);
|
||||
|
||||
_mockPlatformContext.PathStrategy.CombinePath(oldPathId, newName).Returns(Result.From(newPathId));
|
||||
_mockEnvironmentContext.DirectoryProviderService.DirectoryExists(newPathId).Returns(Result.From(false));
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.RenameDirectory(oldPathId, newName)
|
||||
.Returns(Errors.Permission.UnauthorizedAccess);
|
||||
|
||||
// Act
|
||||
Result<Directory> result = _sut.RenameDirectory(oldPathId, newName);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Permission.UnauthorizedAccess, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RenameDirectory_WhenDirectoryDetailsAreInaccessible_ShouldReturnInaccessibleDirectory()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId oldPathId = _fileSystemPathIdFixture.Create(
|
||||
s_isUnix ? "/TestDir/OldName" : @"C:\TestDir\OldName"
|
||||
);
|
||||
string newName = "NewName";
|
||||
FileSystemPathId newPathId = _fileSystemPathIdFixture.Create(
|
||||
s_isUnix ? "/TestDir/NewName" : @"C:\TestDir\NewName"
|
||||
);
|
||||
|
||||
_mockPlatformContext.PathStrategy.CombinePath(oldPathId, newName).Returns(Result.From(newPathId));
|
||||
_mockEnvironmentContext.DirectoryProviderService.DirectoryExists(newPathId).Returns(Result.From(false));
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.RenameDirectory(oldPathId, newName)
|
||||
.Returns(Result.From(newPathId));
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetFileName(newPathId)
|
||||
.Returns(Errors.Permission.UnauthorizedAccess);
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetLastWriteTime(newPathId)
|
||||
.Returns(Errors.Permission.UnauthorizedAccess);
|
||||
_mockEnvironmentContext.DirectoryProviderService.GetCreationTime(newPathId)
|
||||
.Returns(Errors.Permission.UnauthorizedAccess);
|
||||
|
||||
// Act
|
||||
Result<Directory> result = _sut.RenameDirectory(oldPathId, newName);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(FileSystemItemStatus.Inaccessible, result.Value.Status);
|
||||
Assert.Equal(newPathId, result.Value.Id);
|
||||
Assert.False(result.Value.DateCreated.HasValue);
|
||||
Assert.False(result.Value.DateModified.HasValue);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RenameDirectory_WhenDirectoryAlreadyExists_ShouldReturnDirectoryAlreadyExistsError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId oldPathId = _fileSystemPathIdFixture.Create(
|
||||
s_isUnix ? "/TestDir/OldName" : @"C:\TestDir\OldName"
|
||||
);
|
||||
string newName = "ExistingDirectory";
|
||||
FileSystemPathId newPathId = _fileSystemPathIdFixture.Create(
|
||||
s_isUnix ? "/TestDir/ExistingDirectory" : @"C:\TestDir\ExistingDirectory"
|
||||
);
|
||||
|
||||
_mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName)
|
||||
.Returns(Result.From(newPathId));
|
||||
_mockEnvironmentContext.DirectoryProviderService.DirectoryExists(newPathId)
|
||||
.Returns(Result.From(true));
|
||||
|
||||
// Act
|
||||
Result<Directory> result = _sut.RenameDirectory(oldPathId, newName);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.DirectoryAlreadyExists, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RenameDirectory_WhenCombinePathReturnsError_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId oldPathId = _fileSystemPathIdFixture.Create(
|
||||
s_isUnix ? "/TestDir/OldName" : @"C:\TestDir\OldName"
|
||||
);
|
||||
string newName = "NewName";
|
||||
|
||||
_mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName)
|
||||
.Returns(Errors.FileSystemManagement.InvalidPath);
|
||||
|
||||
// Act
|
||||
Result<Directory> result = _sut.RenameDirectory(oldPathId, newName);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
|
||||
// Verify that DirectoryExists and RenameDirectory were not called
|
||||
_mockEnvironmentContext.DirectoryProviderService.DidNotReceive().DirectoryExists(Arg.Any<FileSystemPathId>());
|
||||
_mockEnvironmentContext.DirectoryProviderService.DidNotReceive().RenameDirectory(Arg.Any<FileSystemPathId>(), Arg.Any<string>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RenameDirectory_WhenDirectoryExistsReturnsError_ShouldPropagateError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId oldPathId = _fileSystemPathIdFixture.Create(
|
||||
s_isUnix ? "/TestDir/OldName" : @"C:\TestDir\OldName"
|
||||
);
|
||||
string newName = "NewName";
|
||||
FileSystemPathId newPathId = _fileSystemPathIdFixture.Create(
|
||||
s_isUnix ? "/TestDir/NewName" : @"C:\TestDir\NewName"
|
||||
);
|
||||
_mockPlatformContext.PathStrategy.CombinePath(Arg.Any<FileSystemPathId>(), newName)
|
||||
.Returns(Result.From(newPathId));
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.DirectoryExists(newPathId)
|
||||
.Returns(Errors.Permission.UnauthorizedAccess);
|
||||
|
||||
// Act
|
||||
Result<Directory> result = _sut.RenameDirectory(oldPathId, newName);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Permission.UnauthorizedAccess, result.FirstError);
|
||||
|
||||
// Verify that RenameDirectory was not called
|
||||
_mockEnvironmentContext.DirectoryProviderService.DidNotReceive().RenameDirectory(Arg.Any<FileSystemPathId>(), Arg.Any<string>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeleteDirectory_WithValidStringPath_ShouldReturnDeleted()
|
||||
{
|
||||
// Arrange
|
||||
string path = s_isUnix ? "/TestDir/ToDelete" : @"C:\TestDir\ToDelete";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(path);
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.DeleteDirectory(pathId)
|
||||
.Returns(Result.Deleted);
|
||||
|
||||
// Act
|
||||
Result<Deleted> result = _sut.DeleteDirectory(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(Result.Deleted, result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeleteDirectory_WithInvalidStringPath_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
string invalidPath = string.Empty;
|
||||
|
||||
// Act
|
||||
Result<Deleted> result = _sut.DeleteDirectory(invalidPath);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeleteDirectory_WithFileSystemPathId_ShouldReturnDeleted()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(
|
||||
s_isUnix ? "/TestDir/ToDelete" : @"C:\TestDir\ToDelete"
|
||||
);
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.DeleteDirectory(pathId)
|
||||
.Returns(Result.Deleted);
|
||||
|
||||
// Act
|
||||
Result<Deleted> result = _sut.DeleteDirectory(pathId);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(Result.Deleted, result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeleteDirectory_WhenDirectoryProviderServiceReturnsError_ShouldPropagateError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(
|
||||
s_isUnix ? "/TestDir/ToDelete" : @"C:\TestDir\ToDelete"
|
||||
);
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.DeleteDirectory(pathId)
|
||||
.Returns(Errors.Permission.UnauthorizedAccess);
|
||||
|
||||
// Act
|
||||
Result<Deleted> result = _sut.DeleteDirectory(pathId);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Permission.UnauthorizedAccess, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeleteDirectory_WhenDirectoryDoesNotExist_ShouldReturnDirectoryNotFoundError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(
|
||||
s_isUnix ? "/TestDir/NonExistent" : @"C:\TestDir\NonExistent"
|
||||
);
|
||||
|
||||
_mockEnvironmentContext.DirectoryProviderService.DeleteDirectory(pathId)
|
||||
.Returns(Errors.FileSystemManagement.DirectoryNotFound);
|
||||
|
||||
// Act
|
||||
Result<Deleted> result = _sut.DeleteDirectory(pathId);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.DirectoryNotFound, result.FirstError);
|
||||
}
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Entities;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Services;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Strategies.Platform;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.FileSystem;
|
||||
using NSubstitute;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO.Abstractions;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.UnitTests.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="DriveService"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class DriveServiceTests
|
||||
{
|
||||
private readonly IFileSystem _mockFileSystem;
|
||||
private readonly IPlatformContextManager _mockPlatformContextManager;
|
||||
private readonly IPlatformContext _mockPlatformContext;
|
||||
private readonly DriveService _sut;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DriveServiceTests"/> class.
|
||||
/// </summary>
|
||||
public DriveServiceTests()
|
||||
{
|
||||
_mockFileSystem = Substitute.For<IFileSystem>();
|
||||
_mockPlatformContextManager = Substitute.For<IPlatformContextManager>();
|
||||
_mockPlatformContext = Substitute.For<IPlatformContext>();
|
||||
_mockPlatformContextManager.GetCurrentContext().Returns(_mockPlatformContext);
|
||||
_sut = new DriveService(_mockFileSystem, _mockPlatformContextManager);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetDrives_OnUnixPlatform_ShouldReturnSingleUnixRootItem()
|
||||
{
|
||||
// Arrange
|
||||
_mockPlatformContext.Platform.Returns(PlatformType.Unix);
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<FileSystemItem>> result = _sut.GetDrives();
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Single(result.Value);
|
||||
Assert.IsType<UnixRootItem>(result.Value.First());
|
||||
Assert.Equal(FileSystemItemStatus.Accessible, result.Value.First().Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetDrives_OnWindowsPlatform_ShouldReturnWindowsRootItems()
|
||||
{
|
||||
// Arrange
|
||||
_mockPlatformContext.Platform.Returns(PlatformType.Windows);
|
||||
IDriveInfo[] mockDrives =
|
||||
[
|
||||
CreateMockDriveInfo("C:\\", true),
|
||||
CreateMockDriveInfo("D:\\", true),
|
||||
CreateMockDriveInfo("E:\\", false) // Not ready
|
||||
];
|
||||
_mockFileSystem.DriveInfo.GetDrives().Returns(mockDrives);
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<FileSystemItem>> result = _sut.GetDrives();
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(2, result.Value.Count()); // Only ready drives
|
||||
Assert.All(result.Value, item => Assert.IsType<WindowsRootItem>(item));
|
||||
Assert.Equal(["C:\\", "D:\\"], result.Value.Select(d => d.Name));
|
||||
}
|
||||
|
||||
private static IDriveInfo CreateMockDriveInfo(string name, bool isReady)
|
||||
{
|
||||
IDriveInfo mockDriveInfo = Substitute.For<IDriveInfo>();
|
||||
mockDriveInfo.Name.Returns(name);
|
||||
mockDriveInfo.IsReady.Returns(isReady);
|
||||
return mockDriveInfo;
|
||||
}
|
||||
}
|
||||
+930
@@ -0,0 +1,930 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using AutoFixture;
|
||||
using AutoFixture.AutoNSubstitute;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Services;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.FileSystem;
|
||||
using NSubstitute;
|
||||
using NSubstitute.ExceptionExtensions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.IO.Abstractions;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.UnitTests.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="FileProviderService"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class FileProviderServiceTests
|
||||
{
|
||||
private readonly IFixture _fixture;
|
||||
private readonly IFileSystem _mockFileSystem;
|
||||
private readonly IFileSystemPermissionsService _mockFileSystemPermissionsService;
|
||||
private readonly FileProviderService _sut;
|
||||
private readonly FileSystemPathIdFixture _fileSystemPathIdFixture = new();
|
||||
private static readonly bool s_isUnix = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
|
||||
private readonly string _pathVisible1 = s_isUnix ? "/Visible.txt" : @"C:\Visible.txt";
|
||||
private readonly string _pathHidden1 = s_isUnix ? "/Hidden.txt" : @"C:\Hidden.txt";
|
||||
private readonly string _pathValid1 = s_isUnix ? "/Valid.txt" : @"C:\Valid.txt";
|
||||
private readonly string _pathInvalid1 = s_isUnix ? "/Invalid.txt" : @"C:\Invalid.txt";
|
||||
private readonly string _pathSourceFile = s_isUnix ? "/Source/file.txt" : @"C:\Source\file.txt";
|
||||
private readonly string _pathDestinationFile = s_isUnix ? "/Destination/file.txt" : @"C:\Destination\file.txt";
|
||||
private readonly string _pathSource = s_isUnix ? "/Source" : @"C:\Source";
|
||||
private readonly string _pathDestination = s_isUnix ? "/Destination" : @"C:\Destination";
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="FileProviderServiceTests"/> class.
|
||||
/// </summary>
|
||||
public FileProviderServiceTests()
|
||||
{
|
||||
_fixture = new Fixture().Customize(new AutoNSubstituteCustomization());
|
||||
_mockFileSystem = Substitute.For<IFileSystem>();
|
||||
_mockFileSystemPermissionsService = Substitute.For<IFileSystemPermissionsService>();
|
||||
_sut = new FileProviderService(_mockFileSystem, _mockFileSystemPermissionsService);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetFilePaths_WhenUserHasNoAccess_ShouldReturnUnauthorizedAccessError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create();
|
||||
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ListDirectory).Returns(false);
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<FileSystemPathId>> result = _sut.GetFilePaths(path, false);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Permission.UnauthorizedAccess, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetFilePaths_WhenUserHasAccess_ShouldReturnFiles()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create();
|
||||
string[] files = [.. _fixture.CreateMany<string>(3)];
|
||||
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ListDirectory).Returns(true);
|
||||
_mockFileSystem.Directory.GetFiles(path.Path).Returns(files);
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<FileSystemPathId>> result = _sut.GetFilePaths(path, false);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(3, result.Value.Count());
|
||||
Assert.Equal(
|
||||
files.OrderBy(f => f),
|
||||
result.Value.Select(x => x.Path).OrderBy(f => f)
|
||||
);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetFilePaths_WhenIncludeHiddenElementsIsFalse_ShouldExcludeHiddenFiles()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create();
|
||||
string[] files = [_pathVisible1, _pathHidden1];
|
||||
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ListDirectory).Returns(true);
|
||||
_mockFileSystem.Directory.GetFiles(path.Path).Returns(files);
|
||||
_mockFileSystem.File.GetAttributes(_pathVisible1).Returns(FileAttributes.Normal);
|
||||
_mockFileSystem.File.GetAttributes(_pathHidden1).Returns(FileAttributes.Hidden);
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<FileSystemPathId>> result = _sut.GetFilePaths(path, false);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Single(result.Value);
|
||||
Assert.Equal(_pathVisible1, result.Value.First().Path);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetFilePaths_WhenIncludeHiddenElementsIsTrue_ShouldIncludeHiddenFiles()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create();
|
||||
string[] files = [_pathVisible1, _pathHidden1];
|
||||
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ListDirectory).Returns(true);
|
||||
_mockFileSystem.Directory.GetFiles(path.Path).Returns(files);
|
||||
_mockFileSystem.File.GetAttributes(_pathVisible1).Returns(FileAttributes.Normal);
|
||||
_mockFileSystem.File.GetAttributes(_pathHidden1).Returns(FileAttributes.Hidden);
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<FileSystemPathId>> result = _sut.GetFilePaths(path, true);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(2, result.Value.Count());
|
||||
Assert.Equal(
|
||||
new[] { _pathVisible1, _pathHidden1 }.OrderBy(p => p),
|
||||
result.Value.Select(x => x.Path).OrderBy(p => p)
|
||||
);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetFilePaths_WhenGetAttributesThrowsException_ShouldStillAddFile()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create();
|
||||
string[] files = [_pathValid1, _pathInvalid1];
|
||||
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ListDirectory).Returns(true);
|
||||
_mockFileSystem.Directory.GetFiles(path.Path).Returns(files);
|
||||
_mockFileSystem.File.GetAttributes(_pathValid1).Returns(FileAttributes.Normal);
|
||||
_mockFileSystem.File.GetAttributes(_pathInvalid1).Throws(new Exception("Access denied"));
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<FileSystemPathId>> result = _sut.GetFilePaths(path, false);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(2, result.Value.Count());
|
||||
Assert.Equal([_pathInvalid1, _pathValid1], result.Value.Select(x => x.Path));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetFilePaths_WhenCalled_ShouldOrderFilesAlphabetically()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create();
|
||||
string[] files = s_isUnix
|
||||
? ["/C.txt", "/A.txt", "/B.txt"]
|
||||
: [@"C:\C.txt", @"C:\A.txt", @"C:\B.txt"];
|
||||
string[] expectedFiles = s_isUnix
|
||||
? ["/A.txt", "/B.txt", "/C.txt"]
|
||||
: [@"C:\A.txt", @"C:\B.txt", @"C:\C.txt"];
|
||||
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ListDirectory).Returns(true);
|
||||
_mockFileSystem.Directory.GetFiles(path.Path).Returns(files);
|
||||
_mockFileSystem.File.GetAttributes(Arg.Any<string>()).Returns(FileAttributes.Normal);
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<FileSystemPathId>> result = _sut.GetFilePaths(path, false);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(expectedFiles, result.Value.Select(x => x.Path));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetFilePaths_WhenDirectoryHasNoFiles_ShouldReturnEmptyList()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create();
|
||||
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ListDirectory).Returns(true);
|
||||
_mockFileSystem.Directory.GetFiles(path.Path).Returns([]);
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<FileSystemPathId>> result = _sut.GetFilePaths(path, false);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Empty(result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FileExists_WhenFileExists_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create();
|
||||
_mockFileSystem.File.Exists(path.Path).Returns(true);
|
||||
|
||||
// Act
|
||||
Result<bool> result = _sut.FileExists(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.True(result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FileExists_WhenFileDoesNotExist_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create();
|
||||
_mockFileSystem.File.Exists(path.Path).Returns(false);
|
||||
|
||||
// Act
|
||||
Result<bool> result = _sut.FileExists(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.False(result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetFileName_WhenPathIsValid_ShouldReturnFileName()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create();
|
||||
string expectedFileName = "testfile.txt";
|
||||
_mockFileSystem.Path.GetFileName(path.Path).Returns(expectedFileName);
|
||||
|
||||
// Act
|
||||
Result<string> result = _sut.GetFileName(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(expectedFileName, result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetFileName_WhenPathIsEmpty_ShouldReturnEmptyString()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create();
|
||||
_mockFileSystem.Path.GetFileName(path.Path).Returns(string.Empty);
|
||||
|
||||
// Act
|
||||
Result<string> result = _sut.GetFileName(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Empty(result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetFileAsync_WhenUserHasAccess_ShouldReturnFileContents()
|
||||
{
|
||||
// Arrange
|
||||
string tempFilePath = Path.GetTempFileName();
|
||||
try
|
||||
{
|
||||
byte[] expectedContents = [1, 2, 3, 4, 5];
|
||||
File.WriteAllBytes(tempFilePath, expectedContents);
|
||||
|
||||
FileSystemPathId path = FileSystemPathId.Create(tempFilePath).Value;
|
||||
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ReadContents).Returns(true);
|
||||
|
||||
// We need to update the _fileSystem mock to use the real file system for this method
|
||||
_mockFileSystem.File.ReadAllBytes(tempFilePath).Returns(x => File.ReadAllBytes(x.Arg<string>()));
|
||||
|
||||
// Act
|
||||
Result<byte[]> result = _sut.GetFileAsync(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(expectedContents, result.Value);
|
||||
}
|
||||
finally
|
||||
{
|
||||
// clean up
|
||||
File.Delete(tempFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetFileAsync_WhenUserHasNoAccess_ShouldReturnUnauthorizedAccessError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create();
|
||||
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ReadContents).Returns(false);
|
||||
|
||||
// Act
|
||||
Result<byte[]> result = _sut.GetFileAsync(path);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Permission.UnauthorizedAccess, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetFileAsync_WhenFileIsEmpty_ShouldReturnEmptyByteArray()
|
||||
{
|
||||
// Arrange
|
||||
string tempFilePath = Path.GetTempFileName();
|
||||
try
|
||||
{
|
||||
FileSystemPathId path = FileSystemPathId.Create(tempFilePath).Value;
|
||||
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ReadContents).Returns(true);
|
||||
|
||||
// Act
|
||||
Result<byte[]> result = _sut.GetFileAsync(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Empty(result.Value);
|
||||
}
|
||||
finally
|
||||
{
|
||||
// clean up
|
||||
File.Delete(tempFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetLastWriteTime_WhenUserHasAccess_ShouldReturnLastWriteTime()
|
||||
{
|
||||
// Arrange
|
||||
string tempFilePath = Path.GetTempFileName();
|
||||
try
|
||||
{
|
||||
File.WriteAllText(tempFilePath, "Test content");
|
||||
FileSystemPathId path = FileSystemPathId.Create(tempFilePath).Value;
|
||||
DateTime expectedDateTime = File.GetLastWriteTime(tempFilePath);
|
||||
|
||||
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ReadProperties).Returns(true);
|
||||
_mockFileSystem.File.GetLastWriteTime(path.Path).Returns(expectedDateTime);
|
||||
|
||||
// Act
|
||||
Result<Optional<DateTime>> result = _sut.GetLastWriteTime(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.True(result.Value.HasValue);
|
||||
Assert.Equal(expectedDateTime, result.Value.Value, TimeSpan.FromSeconds(1));
|
||||
}
|
||||
finally
|
||||
{
|
||||
File.Delete(tempFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetLastWriteTime_WhenUserHasNoAccess_ShouldReturnUnauthorizedAccessError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create();
|
||||
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ReadProperties).Returns(false);
|
||||
|
||||
// Act
|
||||
Result<Optional<DateTime>> result = _sut.GetLastWriteTime(path);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Permission.UnauthorizedAccess, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetCreationTime_WhenUserHasAccess_ShouldReturnCreationTime()
|
||||
{
|
||||
// Arrange
|
||||
string tempFilePath = Path.GetTempFileName();
|
||||
try
|
||||
{
|
||||
File.WriteAllText(tempFilePath, "Test content");
|
||||
FileSystemPathId path = FileSystemPathId.Create(tempFilePath).Value;
|
||||
DateTime expectedDateTime = File.GetCreationTime(tempFilePath);
|
||||
|
||||
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ReadProperties).Returns(true);
|
||||
_mockFileSystem.File.GetCreationTime(path.Path).Returns(expectedDateTime);
|
||||
|
||||
// Act
|
||||
Result<Optional<DateTime>> result = _sut.GetCreationTime(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.True(result.Value.HasValue);
|
||||
Assert.Equal(expectedDateTime, result.Value.Value, TimeSpan.FromSeconds(1));
|
||||
}
|
||||
finally
|
||||
{
|
||||
File.Delete(tempFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetCreationTime_WhenUserHasNoAccess_ShouldReturnUnauthorizedAccessError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create();
|
||||
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ReadProperties).Returns(false);
|
||||
|
||||
// Act
|
||||
Result<Optional<DateTime>> result = _sut.GetCreationTime(path);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Permission.UnauthorizedAccess, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetSize_WhenUserHasAccess_ShouldReturnFileSize()
|
||||
{
|
||||
// Arrange
|
||||
string tempFilePath = Path.GetTempFileName();
|
||||
try
|
||||
{
|
||||
string content = "Test content";
|
||||
File.WriteAllText(tempFilePath, content);
|
||||
FileSystemPathId path = FileSystemPathId.Create(tempFilePath).Value;
|
||||
long expectedSize = new FileInfo(tempFilePath).Length;
|
||||
|
||||
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ReadProperties).Returns(true);
|
||||
_mockFileSystem.FileInfo.New(path.Path).Returns(new FileInfoWrapper(_mockFileSystem, new FileInfo(tempFilePath)));
|
||||
|
||||
// Act
|
||||
Result<long?> result = _sut.GetSize(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.NotNull(result.Value);
|
||||
Assert.Equal(expectedSize, result.Value);
|
||||
}
|
||||
finally
|
||||
{
|
||||
File.Delete(tempFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetSize_WhenUserHasNoAccess_ShouldReturnUnauthorizedAccessError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create();
|
||||
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ReadProperties).Returns(false);
|
||||
|
||||
// Act
|
||||
Result<long?> result = _sut.GetSize(path);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Permission.UnauthorizedAccess, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetSize_WhenFileDoesNotExist_ShouldReturnZero()
|
||||
{
|
||||
// Arrange
|
||||
string nonExistentFilePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
|
||||
FileSystemPathId path = FileSystemPathId.Create(nonExistentFilePath).Value;
|
||||
|
||||
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.ReadProperties).Returns(true);
|
||||
_mockFileSystem.FileInfo.New(path.Path).Returns((IFileInfo)null!);
|
||||
|
||||
// Act
|
||||
Result<long?> result = _sut.GetSize(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(0, result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CopyFile_WhenSourceFileExistsAndUserHasAccess_ShouldCopyFileAndReturnPath()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId sourcePath = _fileSystemPathIdFixture.Create(_pathSourceFile);
|
||||
FileSystemPathId destinationPath = _fileSystemPathIdFixture.Create(_pathDestination);
|
||||
bool overrideExisting = false;
|
||||
|
||||
_mockFileSystem.File.Exists(sourcePath.Path).Returns(true);
|
||||
_mockFileSystem.File.Exists(Path.Combine(destinationPath.Path, "file.txt")).Returns(false);
|
||||
|
||||
_mockFileSystem.Path.GetFileName(sourcePath.Path).Returns("file.txt");
|
||||
_mockFileSystem.Path.Combine(destinationPath.Path, "file.txt").Returns(_pathDestinationFile);
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.CopyFile(sourcePath, destinationPath, overrideExisting);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(_pathDestinationFile, result.Value.Path);
|
||||
_mockFileSystem.File.Received(1).Copy(sourcePath.Path, _pathDestinationFile, overrideExisting);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CopyFile_WhenSourceFileDoesNotExist_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId sourcePath = _fileSystemPathIdFixture.Create();
|
||||
FileSystemPathId destinationPath = _fileSystemPathIdFixture.Create();
|
||||
bool overrideExisting = false;
|
||||
|
||||
_mockFileSystem.File.Exists(sourcePath.Path).Returns(false);
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.CopyFile(sourcePath, destinationPath, overrideExisting);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.FileNotFound, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CopyFile_WhenDestinationFileExistsAndOverrideIsFalse_ShouldReturnFileAlreadyExistsError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId sourcePath = _fileSystemPathIdFixture.Create(_pathSourceFile);
|
||||
FileSystemPathId destinationPath = _fileSystemPathIdFixture.Create(_pathDestination);
|
||||
string expectedDestinationFilePath = _pathDestinationFile;
|
||||
bool overrideExisting = false;
|
||||
|
||||
_mockFileSystem.File.Exists(sourcePath.Path).Returns(true);
|
||||
_mockFileSystem.File.Exists(expectedDestinationFilePath).Returns(true);
|
||||
|
||||
_mockFileSystem.Path.GetFileName(sourcePath.Path).Returns("file.txt");
|
||||
_mockFileSystem.Path.GetDirectoryName(sourcePath.Path).Returns(_pathSource);
|
||||
|
||||
_mockFileSystem.Path.Combine(Arg.Any<string>(), Arg.Any<string>())
|
||||
.Returns(callInfo => Path.Combine(callInfo.ArgAt<string>(0), callInfo.ArgAt<string>(1)));
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.CopyFile(sourcePath, destinationPath, overrideExisting);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.FileAlreadyExists, result.FirstError);
|
||||
_mockFileSystem.File.DidNotReceive().Copy(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CopyFile_WhenOverrideExistingIsTrue_ShouldOverwriteExistingFile()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId sourcePath = _fileSystemPathIdFixture.Create(_pathSourceFile);
|
||||
FileSystemPathId destinationPath = _fileSystemPathIdFixture.Create(_pathDestination);
|
||||
bool overrideExisting = true;
|
||||
|
||||
_mockFileSystem.File.Exists(sourcePath.Path).Returns(true);
|
||||
_mockFileSystem.File.Exists(_pathDestinationFile).Returns(true);
|
||||
|
||||
_mockFileSystem.Path.GetFileName(sourcePath.Path).Returns("file.txt");
|
||||
_mockFileSystem.Path.Combine(destinationPath.Path, "file.txt").Returns(_pathDestinationFile);
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.CopyFile(sourcePath, destinationPath, overrideExisting);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(_pathDestinationFile, result.Value.Path);
|
||||
_mockFileSystem.File.Received(1).Copy(sourcePath.Path, _pathDestinationFile, overrideExisting);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CopyFile_WhenExceptionOccurs_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId sourcePath = _fileSystemPathIdFixture.Create();
|
||||
FileSystemPathId destinationPath = _fileSystemPathIdFixture.Create();
|
||||
bool overrideExisting = false;
|
||||
|
||||
_mockFileSystem.File.Exists(sourcePath.Path).Returns(true);
|
||||
_mockFileSystem.File.When(x => x.Copy(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>()))
|
||||
.Do(x => { throw new Exception("Simulated error"); });
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.CopyFile(sourcePath, destinationPath, overrideExisting);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.FileCopyError, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CopyFile_WhenCopyingToSameDirectory_ShouldCreateUniqueFileName()
|
||||
{
|
||||
// Arrange
|
||||
string sourceDirectory = _pathSource;
|
||||
FileSystemPathId sourcePath = _fileSystemPathIdFixture.Create(Path.Combine(sourceDirectory, "file.txt"));
|
||||
FileSystemPathId destinationPath = _fileSystemPathIdFixture.Create(sourceDirectory);
|
||||
string expectedNewFilePath = Path.Combine(sourceDirectory, "file - Copy (1).txt");
|
||||
bool overrideExisting = false;
|
||||
|
||||
_mockFileSystem.File.Exists(sourcePath.Path).Returns(true);
|
||||
_mockFileSystem.File.Exists(Path.Combine(sourceDirectory, "file.txt")).Returns(true);
|
||||
_mockFileSystem.File.Exists(expectedNewFilePath).Returns(false);
|
||||
|
||||
_mockFileSystem.Path.GetFileName(sourcePath.Path).Returns("file.txt");
|
||||
_mockFileSystem.Path.GetDirectoryName(sourcePath.Path).Returns(sourceDirectory);
|
||||
_mockFileSystem.Path.GetFileNameWithoutExtension(Arg.Any<string>()).Returns(x => Path.GetFileNameWithoutExtension(x.Arg<string>()));
|
||||
_mockFileSystem.Path.GetExtension(Arg.Any<string>()).Returns(x => Path.GetExtension(x.Arg<string>()));
|
||||
|
||||
_mockFileSystem.Path.Combine(Arg.Any<string>(), Arg.Any<string>())
|
||||
.Returns(callInfo => Path.Combine(callInfo.ArgAt<string>(0), callInfo.ArgAt<string>(1)));
|
||||
|
||||
// Simulate the behavior of CreateUniqueFilePath
|
||||
int copyNumber = 1;
|
||||
_mockFileSystem.File.Exists(Arg.Any<string>())
|
||||
.Returns(x =>
|
||||
{
|
||||
string path = x.Arg<string>();
|
||||
if (path == expectedNewFilePath)
|
||||
return false;
|
||||
copyNumber++;
|
||||
return true;
|
||||
});
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.CopyFile(sourcePath, destinationPath, overrideExisting);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(expectedNewFilePath, result.Value.Path);
|
||||
_mockFileSystem.File.Received(1).Copy(sourcePath.Path, expectedNewFilePath, overrideExisting);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MoveFile_WhenSourceFileDoesNotExist_ShouldReturnFileNotFoundError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId sourcePath = _fileSystemPathIdFixture.Create(
|
||||
s_isUnix ? "/NonExistentSource.txt" : @"C:\NonExistentSource.txt"
|
||||
);
|
||||
FileSystemPathId destinationPath = _fileSystemPathIdFixture.Create(_pathDestination);
|
||||
bool overrideExisting = false;
|
||||
|
||||
_mockFileSystem.File.Exists(sourcePath.Path).Returns(false);
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.MoveFile(sourcePath, destinationPath, overrideExisting);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.FileNotFound, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MoveFile_WhenDestinationFileDoesNotExist_ShouldPerformSimpleMove()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId sourcePath = _fileSystemPathIdFixture.Create(_pathSourceFile);
|
||||
FileSystemPathId destinationPath = _fileSystemPathIdFixture.Create(_pathDestination);
|
||||
string destinationFilePath = _pathDestinationFile;
|
||||
bool overrideExisting = false;
|
||||
|
||||
_mockFileSystem.File.Exists(sourcePath.Path).Returns(true);
|
||||
_mockFileSystem.File.Exists(destinationFilePath).Returns(false);
|
||||
_mockFileSystem.Path.GetFileName(sourcePath.Path).Returns("file.txt");
|
||||
_mockFileSystem.Path.Combine(destinationPath.Path, "file.txt").Returns(destinationFilePath);
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.MoveFile(sourcePath, destinationPath, overrideExisting);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(destinationFilePath, result.Value.Path);
|
||||
_mockFileSystem.File.Received(1).Move(sourcePath.Path, destinationFilePath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MoveFile_WhenDestinationFileExistsAndOverrideIsTrue_ShouldOverwriteFile()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId sourcePath = _fileSystemPathIdFixture.Create(_pathSourceFile);
|
||||
FileSystemPathId destinationPath = _fileSystemPathIdFixture.Create(_pathDestination);
|
||||
string destinationFilePath = _pathDestinationFile;
|
||||
bool overrideExisting = true;
|
||||
|
||||
_mockFileSystem.File.Exists(sourcePath.Path).Returns(true);
|
||||
_mockFileSystem.File.Exists(destinationFilePath).Returns(true);
|
||||
_mockFileSystem.Path.GetFileName(sourcePath.Path).Returns("file.txt");
|
||||
_mockFileSystem.Path.Combine(destinationPath.Path, "file.txt").Returns(destinationFilePath);
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.MoveFile(sourcePath, destinationPath, overrideExisting);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(destinationFilePath, result.Value.Path);
|
||||
_mockFileSystem.File.Received(1).Move(sourcePath.Path, destinationFilePath, overrideExisting);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MoveFile_WhenDestinationFileExistsAndOverrideIsFalse_ShouldReturnFileAlreadyExistsError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId sourcePath = _fileSystemPathIdFixture.Create(_pathSourceFile);
|
||||
FileSystemPathId destinationPath = _fileSystemPathIdFixture.Create(_pathDestination);
|
||||
string destinationFilePath = _pathDestinationFile;
|
||||
bool overrideExisting = false;
|
||||
|
||||
_mockFileSystem.File.Exists(sourcePath.Path).Returns(true);
|
||||
_mockFileSystem.File.Exists(destinationFilePath).Returns(true);
|
||||
_mockFileSystem.Path.GetFileName(sourcePath.Path).Returns("file.txt");
|
||||
_mockFileSystem.Path.Combine(destinationPath.Path, "file.txt").Returns(destinationFilePath);
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.MoveFile(sourcePath, destinationPath, overrideExisting);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.FileAlreadyExists, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MoveFile_WhenExceptionOccurs_ShouldReturnFileMoveError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId sourcePath = _fileSystemPathIdFixture.Create(_pathSourceFile);
|
||||
FileSystemPathId destinationPath = _fileSystemPathIdFixture.Create(_pathDestination);
|
||||
string destinationFilePath = _pathDestinationFile;
|
||||
bool overrideExisting = false;
|
||||
|
||||
_mockFileSystem.File.Exists(sourcePath.Path).Returns(true);
|
||||
_mockFileSystem.File.Exists(destinationFilePath).Returns(false);
|
||||
_mockFileSystem.Path.GetFileName(sourcePath.Path).Returns("file.txt");
|
||||
_mockFileSystem.Path.Combine(destinationPath.Path, "file.txt").Returns(destinationFilePath);
|
||||
_mockFileSystem.File.When(x => x.Move(Arg.Any<string>(), Arg.Any<string>()))
|
||||
.Do(x => throw new IOException("Simulated IO error"));
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.MoveFile(sourcePath, destinationPath, overrideExisting);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.FileMoveError, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RenameFile_WhenParentDirectoryIsWritable_ShouldRenameSuccessfully()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(
|
||||
s_isUnix ? "/OldName.txt" : @"C:\OldName.txt"
|
||||
);
|
||||
string newName = "NewName.txt";
|
||||
string parentPath = s_isUnix ? "/" : @"C:\";
|
||||
string newPath = s_isUnix ? "/NewName.txt" : @"C:\NewName.txt";
|
||||
|
||||
IFileInfo fileInfo = Substitute.For<IFileInfo>();
|
||||
fileInfo.DirectoryName.Returns(parentPath);
|
||||
_mockFileSystem.FileInfo.New(path.Path).Returns(fileInfo);
|
||||
_mockFileSystem.Path.Combine(parentPath, newName).Returns(newPath);
|
||||
_mockFileSystemPermissionsService.CanAccessPath(Arg.Any<FileSystemPathId>(), FileAccessMode.Write).Returns(true);
|
||||
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.Execute).Returns(true);
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.RenameFile(path, newName);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(newPath, result.Value.Path);
|
||||
_mockFileSystem.File.Received(1).Move(path.Path, newPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RenameFile_WhenParentDirectoryIsNotWritable_ShouldReturnUnauthorizedAccessError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(
|
||||
s_isUnix ? "/OldName.txt" : @"C:\OldName.txt"
|
||||
);
|
||||
string newName = "NewName.txt";
|
||||
string parentPath = s_isUnix ? "/" : @"C:\";
|
||||
string newPath = s_isUnix ? "/NewName.txt" : @"C:\NewName.txt";
|
||||
|
||||
IFileInfo fileInfo = Substitute.For<IFileInfo>();
|
||||
fileInfo.DirectoryName.Returns(parentPath);
|
||||
_mockFileSystem.FileInfo.New(path.Path).Returns(fileInfo);
|
||||
_mockFileSystem.Path.Combine(parentPath, newName).Returns(newPath);
|
||||
_mockFileSystemPermissionsService.CanAccessPath(Arg.Any<FileSystemPathId>(), FileAccessMode.Write).Returns(false);
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.RenameFile(path, newName);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Permission.UnauthorizedAccess, result.FirstError);
|
||||
_mockFileSystem.File.DidNotReceive().Move(Arg.Any<string>(), Arg.Any<string>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RenameFile_WhenFileIsNotExecutable_ShouldReturnUnauthorizedAccessError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(
|
||||
s_isUnix ? "/OldName.txt" : @"C:\OldName.txt"
|
||||
);
|
||||
string newName = "NewName.txt";
|
||||
string parentPath = s_isUnix ? "/" : @"C:\";
|
||||
string newPath = s_isUnix ? "/NewName.txt" : @"C:\NewName.txt";
|
||||
|
||||
IFileInfo fileInfo = Substitute.For<IFileInfo>();
|
||||
fileInfo.DirectoryName.Returns(parentPath);
|
||||
_mockFileSystem.FileInfo.New(path.Path).Returns(fileInfo);
|
||||
_mockFileSystem.Path.Combine(parentPath, newName).Returns(newPath);
|
||||
_mockFileSystemPermissionsService.CanAccessPath(Arg.Any<FileSystemPathId>(), FileAccessMode.Write).Returns(true);
|
||||
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.Execute).Returns(false);
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.RenameFile(path, newName);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Permission.UnauthorizedAccess, result.FirstError);
|
||||
_mockFileSystem.File.DidNotReceive().Move(Arg.Any<string>(), Arg.Any<string>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RenameFile_WhenParentDirectoryIsNull_ShouldReturnInvalidPathError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(
|
||||
s_isUnix ? "/OldName.txt" : @"C:\OldName.txt"
|
||||
);
|
||||
string newName = "NewName.txt";
|
||||
|
||||
_mockFileSystem.FileInfo.New(path.Path).Returns((IFileInfo)null!);
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.RenameFile(path, newName);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.CannotNavigateUp, result.FirstError);
|
||||
_mockFileSystem.File.DidNotReceive().Move(Arg.Any<string>(), Arg.Any<string>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RenameFile_WhenNewPathIsInvalid_ShouldReturnInvalidPathError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(
|
||||
s_isUnix ? "/OldName.txt" : @"C:\OldName.txt"
|
||||
);
|
||||
string newName = "NewName.txt";
|
||||
string parentPath = s_isUnix ? "/" : @"C:\";
|
||||
|
||||
IFileInfo fileInfo = Substitute.For<IFileInfo>();
|
||||
fileInfo.DirectoryName.Returns(parentPath);
|
||||
_mockFileSystem.FileInfo.New(path.Path).Returns(fileInfo);
|
||||
_mockFileSystem.Path.Combine(parentPath, newName).Returns((string)null!);
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.RenameFile(path, newName);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
_mockFileSystem.File.DidNotReceive().Move(Arg.Any<string>(), Arg.Any<string>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RenameFile_WhenParentDirectoryExistsButNotWritable_ShouldReturnUnauthorizedAccessError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(
|
||||
s_isUnix ? "/OldName.txt" : @"C:\OldName.txt"
|
||||
);
|
||||
string newName = "NewName.txt";
|
||||
string parentPath = s_isUnix ? "/" : @"C:\";
|
||||
string newPath = s_isUnix ? "/NewName.txt" : @"C:\NewName.txt";
|
||||
|
||||
IFileInfo fileInfo = Substitute.For<IFileInfo>();
|
||||
fileInfo.DirectoryName.Returns(parentPath);
|
||||
_mockFileSystem.FileInfo.New(path.Path).Returns(fileInfo);
|
||||
_mockFileSystem.Path.Combine(parentPath, newName).Returns(newPath);
|
||||
|
||||
// Mock successful creation of parent directory FileSystemPathId
|
||||
Result<FileSystemPathId> parentDirectoryPathId = FileSystemPathId.Create(parentPath);
|
||||
_mockFileSystem.Path.GetDirectoryName(path.Path).Returns(parentPath);
|
||||
|
||||
// Mock permissions: executable for the file, but not writable for the parent directory
|
||||
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.Execute).Returns(true);
|
||||
_mockFileSystemPermissionsService.CanAccessPath(Arg.Is<FileSystemPathId>(x => x.Path == parentPath), FileAccessMode.Write).Returns(false);
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.RenameFile(path, newName);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Permission.UnauthorizedAccess, result.FirstError);
|
||||
_mockFileSystem.File.DidNotReceive().Move(Arg.Any<string>(), Arg.Any<string>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeleteFile_WhenFileExistsAndHasDeletePermission_ShouldDeleteSuccessfully()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(
|
||||
s_isUnix ? "/FileToDelete.txt" : @"C:\FileToDelete.txt"
|
||||
);
|
||||
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.Delete).Returns(true);
|
||||
_mockFileSystem.File.Exists(path.Path).Returns(true);
|
||||
|
||||
// Act
|
||||
Result<Deleted> result = _sut.DeleteFile(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(Result.Deleted, result.Value);
|
||||
_mockFileSystem.File.Received(1).Delete(path.Path);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeleteFile_WhenNoDeletePermission_ShouldReturnUnauthorizedAccessError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(
|
||||
s_isUnix ? "/FileToDelete.txt" : @"C:\FileToDelete.txt"
|
||||
);
|
||||
_mockFileSystemPermissionsService.CanAccessPath(path, FileAccessMode.Delete).Returns(false);
|
||||
|
||||
// Act
|
||||
Result<Deleted> result = _sut.DeleteFile(path);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Permission.UnauthorizedAccess, result.FirstError);
|
||||
_mockFileSystem.File.DidNotReceive().Delete(Arg.Any<string>());
|
||||
}
|
||||
}
|
||||
+1211
File diff suppressed because it is too large.
Load diff
+228
@@ -0,0 +1,228 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Entities;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Services;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Entities;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.FileSystem;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.PhotoLibrary;
|
||||
using NSubstitute;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO.Abstractions.TestingHelpers;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.UnitTests.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="FileTypeService"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class FileTypeServiceTests
|
||||
{
|
||||
private MockFileSystem _mockFileSystem = null!;
|
||||
private readonly IFileSystemPermissionsService _mockFileSystemPermissionsService;
|
||||
private FileTypeService? sut;
|
||||
private readonly FileSystemPathIdFixture _fileSystemPathIdFixture = new();
|
||||
private readonly FileFixture _fileFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="FileTypeServiceTests"/> class.
|
||||
/// </summary>
|
||||
public FileTypeServiceTests()
|
||||
{
|
||||
_mockFileSystemPermissionsService = Substitute.For<IFileSystemPermissionsService>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetImageTypeAsync_WithValidFile_ShouldReturnCorrectImageType()
|
||||
{
|
||||
// Arrange
|
||||
File file = _fileFixture.Create();
|
||||
byte[] pngHeader = [137, 80, 78, 71, 13, 10, 26, 10];
|
||||
byte[] fileContent = [.. pngHeader, .. Enumerable.Repeat<byte>(0, 100)];
|
||||
|
||||
SetupMockFileSystem(file.Id, fileContent);
|
||||
sut = new FileTypeService(_mockFileSystem, _mockFileSystemPermissionsService);
|
||||
_mockFileSystemPermissionsService.CanAccessPath(file.Id, FileAccessMode.ReadContents).Returns(true);
|
||||
|
||||
// Act
|
||||
Result<ImageType> result = await sut.GetImageTypeAsync(file, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(ImageType.PNG, result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetImageTypeAsync_WithUnauthorizedAccess_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
File file = _fileFixture.Create();
|
||||
|
||||
SetupMockFileSystem(file.Id, []);
|
||||
sut = new FileTypeService(_mockFileSystem, _mockFileSystemPermissionsService);
|
||||
|
||||
_mockFileSystemPermissionsService.CanAccessPath(file.Id, FileAccessMode.ReadContents).Returns(false);
|
||||
|
||||
// Act
|
||||
Result<ImageType> result = await sut.GetImageTypeAsync(file, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Permission.UnauthorizedAccess, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetImageTypeAsync_WithSmallFile_ShouldReturnNone()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(@"C:\test.jpg");
|
||||
byte[] fileContent = [1, 2, 3]; // Less than BUFFER_SIZE
|
||||
|
||||
SetupMockFileSystem(pathId, fileContent);
|
||||
sut = new FileTypeService(_mockFileSystem, _mockFileSystemPermissionsService);
|
||||
_mockFileSystemPermissionsService.CanAccessPath(pathId, FileAccessMode.ReadContents).Returns(true);
|
||||
|
||||
// Act
|
||||
Result<ImageType> result = await sut.GetImageTypeAsync(pathId, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(ImageType.None, result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetImageTypeAsync_WithSvgFile_ShouldReturnSvgType()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(@"C:\test.svg");
|
||||
string svgContent = "<svg xmlns=\"http://www.w3.org/2000/svg\"></svg>";
|
||||
byte[] fileContent = Encoding.UTF8.GetBytes(svgContent);
|
||||
|
||||
SetupMockFileSystem(pathId, fileContent);
|
||||
sut = new FileTypeService(_mockFileSystem, _mockFileSystemPermissionsService);
|
||||
_mockFileSystemPermissionsService.CanAccessPath(pathId, FileAccessMode.ReadContents).Returns(true);
|
||||
|
||||
// Act
|
||||
Result<ImageType> result = await sut.GetImageTypeAsync(pathId, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(ImageType.SVG, result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetImageTypeAsync_WithTgaFile_ShouldReturnTgaType()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(@"C:\test.tga");
|
||||
byte[] tgaHeader = new byte[18];
|
||||
tgaHeader[2] = 2; // Set image type to 2 (uncompressed true-color image)
|
||||
byte[] fileContent = [.. tgaHeader, .. Enumerable.Repeat<byte>(0, 100)];
|
||||
|
||||
SetupMockFileSystem(pathId, fileContent);
|
||||
sut = new FileTypeService(_mockFileSystem, _mockFileSystemPermissionsService);
|
||||
|
||||
_mockFileSystemPermissionsService.CanAccessPath(pathId, FileAccessMode.ReadContents).Returns(true);
|
||||
|
||||
// Act
|
||||
Result<ImageType> result = await sut.GetImageTypeAsync(pathId, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(ImageType.TGA, result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetImageTypeAsync_WithUnknownImageType_ShouldReturnNone()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(@"C:\test.unknown");
|
||||
byte[] fileContent = [.. Enumerable.Repeat<byte>(0, 100)];
|
||||
|
||||
SetupMockFileSystem(pathId, fileContent);
|
||||
sut = new FileTypeService(_mockFileSystem, _mockFileSystemPermissionsService);
|
||||
_mockFileSystemPermissionsService.CanAccessPath(pathId, FileAccessMode.ReadContents).Returns(true);
|
||||
|
||||
// Act
|
||||
Result<ImageType> result = await sut.GetImageTypeAsync(pathId, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(ImageType.None, result.Value);
|
||||
}
|
||||
|
||||
private void SetupMockFileSystem(FileSystemPathId pathId, byte[] fileContent)
|
||||
{
|
||||
MockFileSystem mockFileSystem = new(new Dictionary<string, MockFileData>
|
||||
{
|
||||
{ pathId.Path, new MockFileData(fileContent) }
|
||||
});
|
||||
|
||||
_mockFileSystem = mockFileSystem;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetImageTypeAsync_WithSvgFileStartingWithXmlDeclaration_ShouldReturnSvgType()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(@"C:\test.svg");
|
||||
string svgContent = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<svg xmlns=\"http://www.w3.org/2000/svg\"></svg>";
|
||||
byte[] fileContent = Encoding.UTF8.GetBytes(svgContent);
|
||||
|
||||
SetupMockFileSystem(pathId, fileContent);
|
||||
sut = new FileTypeService(_mockFileSystem, _mockFileSystemPermissionsService);
|
||||
|
||||
_mockFileSystemPermissionsService.CanAccessPath(pathId, FileAccessMode.ReadContents).Returns(true);
|
||||
|
||||
// Act
|
||||
Result<ImageType> result = await sut.GetImageTypeAsync(pathId, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(ImageType.SVG, result.Value);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(new byte[] { 66, 77 }, ImageType.BMP, "BMP")]
|
||||
[InlineData(new byte[] { 71, 73, 70 }, ImageType.GIF, "GIF")]
|
||||
[InlineData(new byte[] { 137, 80, 78, 71 }, ImageType.PNG, "PNG")]
|
||||
[InlineData(new byte[] { 73, 73, 42 }, ImageType.TIFF, "TIFF (Little Endian)")]
|
||||
[InlineData(new byte[] { 77, 77, 42 }, ImageType.TIFF, "TIFF (Big Endian)")]
|
||||
[InlineData(new byte[] { 255, 216, 255, 224 }, ImageType.JPEG, "JPEG")]
|
||||
[InlineData(new byte[] { 255, 216, 255, 225 }, ImageType.JPEG_CANON, "JPEG (Canon)")]
|
||||
[InlineData(new byte[] { 255, 216, 255, 226 }, ImageType.JPEG_UNKNOWN, "JPEG (Unknown)")]
|
||||
[InlineData(new byte[] { 0x00, 0x11, 0x02, 0xFF }, ImageType.PICT, "PICT")]
|
||||
[InlineData(new byte[] { 0x00, 0x00, 0x01, 0x00 }, ImageType.ICO, "ICO")]
|
||||
[InlineData(new byte[] { 0x38, 0x42, 0x50, 0x53 }, ImageType.PSD, "PSD")]
|
||||
[InlineData(new byte[] { 0xFF, 0x4F, 0xFF, 0x51 }, ImageType.JPEG2000, "JPEG 2000")]
|
||||
[InlineData(new byte[] { 0x00, 0x00, 0x00, 0x0C, 0x6A, 0x70, 0x20, 0x20 }, ImageType.AVIF, "AVIF")]
|
||||
[InlineData(new byte[] { 0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20, 0x20 }, ImageType.JPEG2000, "JPEG 2000 (variant)")]
|
||||
[InlineData(new byte[] { 0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x45, 0x42, 0x50 }, ImageType.WEBP, "WEBP")]
|
||||
[InlineData(new byte[] { 0x00, 0x00, 0x00, 0x00 }, ImageType.None, "Unknown")]
|
||||
public async Task GetImageTypeAsync_WithVariousImageTypes_ShouldIdentifyCorrectly(byte[] header, ImageType expectedType, string testName)
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create($@"C:\test.{testName.ToLower()}");
|
||||
byte[] fileContent = [.. header, .. Enumerable.Repeat<byte>(0, 100)];
|
||||
|
||||
SetupMockFileSystem(pathId, fileContent);
|
||||
sut = new FileTypeService(_mockFileSystem, _mockFileSystemPermissionsService);
|
||||
|
||||
_mockFileSystemPermissionsService.CanAccessPath(pathId, FileAccessMode.ReadContents).Returns(true);
|
||||
|
||||
// Act
|
||||
Result<ImageType> result = await sut.GetImageTypeAsync(pathId, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(expectedType, result.Value);
|
||||
}
|
||||
}
|
||||
+523
@@ -0,0 +1,523 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Services;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Strategies.Path;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Strategies.Platform;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using NSubstitute;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.UnitTests.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="PathService"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class PathServiceTests
|
||||
{
|
||||
private readonly IPlatformContextManager _mockPlatformContextManager;
|
||||
private readonly IPlatformContext _mockPlatformContext;
|
||||
private readonly IPathStrategy _mockPathStrategy;
|
||||
private readonly PathService _sut;
|
||||
private readonly FileSystemPathIdFixture _fileSystemPathIdFixture = new();
|
||||
private readonly PathSegmentFixture _pathSegmentFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PathServiceTests"/> class.
|
||||
/// </summary>
|
||||
public PathServiceTests()
|
||||
{
|
||||
_mockPlatformContextManager = Substitute.For<IPlatformContextManager>();
|
||||
_mockPlatformContext = Substitute.For<IPlatformContext>();
|
||||
_mockPathStrategy = Substitute.For<IPathStrategy>();
|
||||
_mockPlatformContext.PathStrategy.Returns(_mockPathStrategy);
|
||||
_mockPlatformContextManager.GetCurrentContext().Returns(_mockPlatformContext);
|
||||
_sut = new PathService(_mockPlatformContextManager);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsValidPath_WithValidPath_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
string validPath = @"C:\ValidPath";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(validPath);
|
||||
_mockPathStrategy.IsValidPath(pathId).Returns(true);
|
||||
|
||||
// Act
|
||||
bool result = _sut.IsValidPath(validPath);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
_mockPathStrategy.Received(1).IsValidPath(Arg.Is<FileSystemPathId>(id => id.Path == validPath));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsValidPath_WithInvalidPath_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
string invalidPath = @"C:\Invalid\*Path";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(invalidPath);
|
||||
_mockPathStrategy.IsValidPath(pathId).Returns(false);
|
||||
|
||||
// Act
|
||||
bool result = _sut.IsValidPath(invalidPath);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
_mockPathStrategy.Received(1).IsValidPath(Arg.Is<FileSystemPathId>(id => id.Path == invalidPath));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsValidPath_WithEmptyPath_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
string emptyPath = string.Empty;
|
||||
|
||||
// Act
|
||||
bool result = _sut.IsValidPath(emptyPath);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
_mockPathStrategy.DidNotReceive().IsValidPath(Arg.Any<FileSystemPathId>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsValidPath_WithNullPath_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
string? nullPath = null;
|
||||
|
||||
// Act
|
||||
bool result = _sut.IsValidPath(nullPath!);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
_mockPathStrategy.DidNotReceive().IsValidPath(Arg.Any<FileSystemPathId>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Exists_WithValidExistingPath_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
string existingPath = @"C:\ExistingPath";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(existingPath);
|
||||
_mockPathStrategy.Exists(pathId).Returns(true);
|
||||
|
||||
// Act
|
||||
bool result = _sut.Exists(existingPath);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
_mockPathStrategy.Received(1).Exists(Arg.Is<FileSystemPathId>(id => id.Path == existingPath));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Exists_WhenPathIsHiddenAndIncludeHiddenElementsIsTrue_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
string existingPath = @"C:\ExistingPath";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(existingPath);
|
||||
_mockPathStrategy.Exists(pathId, true).Returns(true);
|
||||
|
||||
// Act
|
||||
bool result = _sut.Exists(existingPath, true);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
_mockPathStrategy.Received(1).Exists(Arg.Is<FileSystemPathId>(id => id.Path == existingPath));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Exists_WhenPathIsHiddenAndIncludeHiddenElementsIsFalse_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
string existingPath = @"C:\ExistingPath";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(existingPath);
|
||||
_mockPathStrategy.Exists(pathId, false).Returns(false);
|
||||
|
||||
// Act
|
||||
bool result = _sut.Exists(existingPath, false);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
_mockPathStrategy.Received(1).Exists(Arg.Is<FileSystemPathId>(id => id.Path == existingPath), false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Exists_WithValidNonExistingPath_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
string nonExistingPath = @"C:\NonExistingPath";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(nonExistingPath);
|
||||
_mockPathStrategy.Exists(pathId).Returns(false);
|
||||
|
||||
// Act
|
||||
bool result = _sut.Exists(nonExistingPath);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
_mockPathStrategy.Received(1).Exists(Arg.Is<FileSystemPathId>(id => id.Path == nonExistingPath));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Exists_WithEmptyPath_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
string emptyPath = string.Empty;
|
||||
|
||||
// Act
|
||||
bool result = _sut.Exists(emptyPath);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
_mockPathStrategy.DidNotReceive().Exists(Arg.Any<FileSystemPathId>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Exists_WithNullPath_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
string? nullPath = null;
|
||||
|
||||
// Act
|
||||
bool result = _sut.Exists(nullPath!);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
_mockPathStrategy.DidNotReceive().Exists(Arg.Any<FileSystemPathId>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CombinePath_WithValidPathAndName_ShouldReturnCombinedPath()
|
||||
{
|
||||
// Arrange
|
||||
string path = @"C:\BaseDir";
|
||||
string name = "SubDir";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(path);
|
||||
FileSystemPathId combinedPathId = _fileSystemPathIdFixture.Create(@"C:\BaseDir\SubDir");
|
||||
_mockPathStrategy.CombinePath(pathId, name).Returns(Result.From(combinedPathId));
|
||||
|
||||
// Act
|
||||
Result<string> result = _sut.CombinePath(path, name);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(@"C:\BaseDir\SubDir", result.Value);
|
||||
_mockPathStrategy.Received(1).CombinePath(Arg.Is<FileSystemPathId>(id => id.Path == path), name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CombinePath_WithInvalidPath_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
string invalidPath = string.Empty;
|
||||
string name = "SubDir";
|
||||
|
||||
// Act
|
||||
Result<string> result = _sut.CombinePath(invalidPath, name);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
_mockPathStrategy.DidNotReceive().CombinePath(Arg.Any<FileSystemPathId>(), Arg.Any<string>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CombinePath_WhenPathStrategyReturnsError_ShouldPropagateError()
|
||||
{
|
||||
// Arrange
|
||||
string path = @"C:\BaseDir";
|
||||
string name = "Invalid*Name";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(path);
|
||||
_mockPathStrategy.CombinePath(pathId, name).Returns(Errors.FileSystemManagement.InvalidPath);
|
||||
|
||||
// Act
|
||||
Result<string> result = _sut.CombinePath(path, name);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
_mockPathStrategy.Received(1).CombinePath(Arg.Is<FileSystemPathId>(id => id.Path == path), name);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CombinePath_WithNullName_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
string path = @"C:\BaseDir";
|
||||
string? name = null;
|
||||
|
||||
// Act
|
||||
Result<string> result = _sut.CombinePath(path, name!);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
_mockPathStrategy.DidNotReceive().CombinePath(Arg.Any<FileSystemPathId>(), Arg.Any<string>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParsePath_WithValidPath_ShouldReturnPathSegments()
|
||||
{
|
||||
// Arrange
|
||||
string validPath = @"C:\ValidPath\SubDir";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(validPath);
|
||||
List<PathSegment> expectedSegments = _pathSegmentFixture.CreateMany();
|
||||
_mockPathStrategy.ParsePath(pathId).Returns(Result.From(expectedSegments.AsEnumerable()));
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.ParsePath(validPath);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(expectedSegments, result.Value);
|
||||
_mockPathStrategy.Received(1).ParsePath(Arg.Is<FileSystemPathId>(id => id.Path == validPath));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParsePath_WithInvalidPath_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
string invalidPath = string.Empty;
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.ParsePath(invalidPath);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
_mockPathStrategy.DidNotReceive().ParsePath(Arg.Any<FileSystemPathId>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParsePath_WhenPathStrategyReturnsError_ShouldPropagateError()
|
||||
{
|
||||
// Arrange
|
||||
string path = @"C:\InvalidPath\*SubDir";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(path);
|
||||
_mockPathStrategy.ParsePath(pathId).Returns(Errors.FileSystemManagement.InvalidPath);
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.ParsePath(path);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
_mockPathStrategy.Received(1).ParsePath(Arg.Is<FileSystemPathId>(id => id.Path == path));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParsePath_WithNullPath_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
string? nullPath = null;
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.ParsePath(nullPath!);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
_mockPathStrategy.DidNotReceive().ParsePath(Arg.Any<FileSystemPathId>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GoUpOneLevel_WithValidPath_ShouldReturnParentPathSegments()
|
||||
{
|
||||
// Arrange
|
||||
string validPath = @"C:\ValidPath\SubDir";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(validPath);
|
||||
List<PathSegment> expectedSegments = _pathSegmentFixture.CreateMany(2); // Assuming parent path has 2 segments
|
||||
_mockPathStrategy.GoUpOneLevel(pathId).Returns(Result.From(expectedSegments.AsEnumerable()));
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.GoUpOneLevel(validPath);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(expectedSegments, result.Value);
|
||||
_mockPathStrategy.Received(1).GoUpOneLevel(Arg.Is<FileSystemPathId>(id => id.Path == validPath));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GoUpOneLevel_WithRootPath_ShouldReturnEmptySegmentList()
|
||||
{
|
||||
// Arrange
|
||||
string rootPath = @"C:\";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(rootPath);
|
||||
List<PathSegment> emptySegmentList = [];
|
||||
_mockPathStrategy.GoUpOneLevel(pathId).Returns(Result.From(emptySegmentList.AsEnumerable()));
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.GoUpOneLevel(rootPath);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Empty(result.Value);
|
||||
_mockPathStrategy.Received(1).GoUpOneLevel(Arg.Is<FileSystemPathId>(id => id.Path == rootPath));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GoUpOneLevel_WithInvalidPath_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
string invalidPath = string.Empty;
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.GoUpOneLevel(invalidPath);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
_mockPathStrategy.DidNotReceive().GoUpOneLevel(Arg.Any<FileSystemPathId>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GoUpOneLevel_WhenPathStrategyReturnsError_ShouldPropagateError()
|
||||
{
|
||||
// Arrange
|
||||
string path = @"C:\InvalidPath\*SubDir";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(path);
|
||||
_mockPathStrategy.GoUpOneLevel(pathId).Returns(Errors.FileSystemManagement.InvalidPath);
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.GoUpOneLevel(path);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
_mockPathStrategy.Received(1).GoUpOneLevel(Arg.Is<FileSystemPathId>(id => id.Path == path));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GoUpOneLevel_WithNullPath_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
string? nullPath = null;
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.GoUpOneLevel(nullPath!);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
_mockPathStrategy.DidNotReceive().GoUpOneLevel(Arg.Any<FileSystemPathId>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetInvalidPathCharsForPlatform_WhenCalled_ShouldReturnInvalidCharsFromPathStrategy()
|
||||
{
|
||||
// Arrange
|
||||
char[] expectedInvalidChars = ['<', '>', ':', '"', '|', '?', '*'];
|
||||
_mockPathStrategy.GetInvalidPathCharsForPlatform().Returns(expectedInvalidChars);
|
||||
|
||||
// Act
|
||||
char[] result = _sut.GetInvalidPathCharsForPlatform();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expectedInvalidChars, result);
|
||||
_mockPathStrategy.Received(1).GetInvalidPathCharsForPlatform();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetInvalidPathCharsForPlatform_WhenPathStrategyReturnsEmptyArray_ShouldReturnEmptyArray()
|
||||
{
|
||||
// Arrange
|
||||
char[] emptyArray = [];
|
||||
_mockPathStrategy.GetInvalidPathCharsForPlatform().Returns(emptyArray);
|
||||
|
||||
// Act
|
||||
char[] result = _sut.GetInvalidPathCharsForPlatform();
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result);
|
||||
_mockPathStrategy.Received(1).GetInvalidPathCharsForPlatform();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetInvalidPathCharsForPlatform_WhenReturnedArrayIsModified_ShouldNotAffectSubsequentCalls()
|
||||
{
|
||||
// Arrange
|
||||
char[] originalInvalidChars = ['<', '>', ':', '"', '|', '?', '*'];
|
||||
_mockPathStrategy.GetInvalidPathCharsForPlatform().Returns(originalInvalidChars);
|
||||
|
||||
// Act
|
||||
char[] result = _sut.GetInvalidPathCharsForPlatform();
|
||||
result[0] = 'X'; // Attempt to modify the returned array
|
||||
|
||||
// Assert
|
||||
char[] secondResult = _sut.GetInvalidPathCharsForPlatform();
|
||||
Assert.Equal(secondResult, originalInvalidChars);
|
||||
_mockPathStrategy.Received(2).GetInvalidPathCharsForPlatform();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetPathRoot_WhenCalledWithValidPath_ShouldReturnRootPathSegment()
|
||||
{
|
||||
// Arrange
|
||||
string validPath = @"C:\ValidPath\SubDir";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(validPath);
|
||||
PathSegment expectedRootSegment = _pathSegmentFixture.Create(name: "C:", isDirectory: false, isDrive: true);
|
||||
_mockPathStrategy.GetPathRoot(pathId).Returns(Result.From(expectedRootSegment));
|
||||
|
||||
// Act
|
||||
Result<PathSegment> result = _sut.GetPathRoot(validPath);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(expectedRootSegment, result.Value);
|
||||
_mockPathStrategy.Received(1).GetPathRoot(Arg.Is<FileSystemPathId>(id => id.Path == validPath));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetPathRoot_WhenCalledWithInvalidPath_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
string invalidPath = string.Empty;
|
||||
|
||||
// Act
|
||||
Result<PathSegment> result = _sut.GetPathRoot(invalidPath);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
_mockPathStrategy.DidNotReceive().GetPathRoot(Arg.Any<FileSystemPathId>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetPathRoot_WhenPathStrategyReturnsError_ShouldPropagateError()
|
||||
{
|
||||
// Arrange
|
||||
string path = @"C:\InvalidPath\*SubDir";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(path);
|
||||
_mockPathStrategy.GetPathRoot(pathId).Returns(Errors.FileSystemManagement.InvalidPath);
|
||||
|
||||
// Act
|
||||
Result<PathSegment> result = _sut.GetPathRoot(path);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
_mockPathStrategy.Received(1).GetPathRoot(Arg.Is<FileSystemPathId>(id => id.Path == path));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetPathRoot_WhenCalledWithNullPath_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
string? nullPath = null;
|
||||
|
||||
// Act
|
||||
Result<PathSegment> result = _sut.GetPathRoot(nullPath!);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
_mockPathStrategy.DidNotReceive().GetPathRoot(Arg.Any<FileSystemPathId>());
|
||||
}
|
||||
}
|
||||
+187
@@ -0,0 +1,187 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Services;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Strategies.Environment;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using Lumina.Domain.SharedKernel.Fixtures.Common.Enums.PhotoLibrary;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.PhotoLibrary;
|
||||
using NSubstitute;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.Formats.Jpeg;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.UnitTests.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="ThumbnailService"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class ThumbnailServiceTests
|
||||
{
|
||||
private readonly IEnvironmentContext _mockEnvironmentContext;
|
||||
private readonly ThumbnailService _sut;
|
||||
private readonly FileSystemPathIdFixture _fileSystemPathIdFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ThumbnailServiceTests"/> class.
|
||||
/// </summary>
|
||||
public ThumbnailServiceTests()
|
||||
{
|
||||
_mockEnvironmentContext = Substitute.For<IEnvironmentContext>();
|
||||
_sut = new ThumbnailService(_mockEnvironmentContext);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetThumbnailAsync_WithValidPath_ShouldReturnThumbnail()
|
||||
{
|
||||
// Arrange
|
||||
string path = @"C:\TestImage.jpg";
|
||||
int quality = 80;
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(path);
|
||||
|
||||
// Create a small valid JPEG image
|
||||
byte[] imageBytes;
|
||||
using (Image<Rgba32> image = new(10, 10))
|
||||
{
|
||||
using MemoryStream ms = new();
|
||||
image.Save(ms, new JpegEncoder());
|
||||
imageBytes = ms.ToArray();
|
||||
}
|
||||
|
||||
_mockEnvironmentContext.FileTypeService.GetImageTypeAsync(pathId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From(ImageType.JPEG));
|
||||
_mockEnvironmentContext.FileProviderService.GetFileAsync(pathId)
|
||||
.Returns(Result.From(imageBytes));
|
||||
|
||||
// Act
|
||||
Result<Thumbnail> result = await _sut.GetThumbnailAsync(path, quality, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(ImageType.JPEG, result.Value.Type);
|
||||
Assert.NotEmpty(result.Value.Bytes);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetThumbnailAsync_WithInvalidPath_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
string invalidPath = string.Empty;
|
||||
int quality = 80;
|
||||
|
||||
// Act
|
||||
Result<Thumbnail> result = await _sut.GetThumbnailAsync(invalidPath, quality, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetThumbnailAsync_WhenFileTypeServiceReturnsError_ShouldPropagateError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(@"C:\TestImage.jpg");
|
||||
int quality = 80;
|
||||
|
||||
_mockEnvironmentContext.FileTypeService.GetImageTypeAsync(pathId, Arg.Any<CancellationToken>())
|
||||
.Returns(Errors.Permission.UnauthorizedAccess);
|
||||
|
||||
// Act
|
||||
Result<Thumbnail> result = await _sut.GetThumbnailAsync(pathId, quality, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Permission.UnauthorizedAccess, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetThumbnailAsync_WhenImageTypeIsNone_ShouldReturnNoThumbnailError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(@"C:\TestFile.txt");
|
||||
int quality = 80;
|
||||
|
||||
_mockEnvironmentContext.FileTypeService.GetImageTypeAsync(pathId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From(ImageType.None));
|
||||
|
||||
// Act
|
||||
Result<Thumbnail> result = await _sut.GetThumbnailAsync(pathId, quality, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Thumbnails.NoThumbnail, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetThumbnailAsync_WhenFileProviderServiceReturnsError_ShouldPropagateError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(@"C:\TestImage.jpg");
|
||||
int quality = 80;
|
||||
|
||||
_mockEnvironmentContext.FileTypeService.GetImageTypeAsync(pathId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From(ImageType.JPEG));
|
||||
_mockEnvironmentContext.FileProviderService.GetFileAsync(pathId)
|
||||
.Returns(Errors.Permission.UnauthorizedAccess);
|
||||
|
||||
// Act
|
||||
Result<Thumbnail> result = await _sut.GetThumbnailAsync(pathId, quality, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Permission.UnauthorizedAccess, result.FirstError);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[ClassData(typeof(ImageTypeFixture))]
|
||||
public async Task GetThumbnailAsync_WithDifferentImageTypes_ShouldReturnThumbnail(ImageType imageType, byte[] imageBytes)
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(@"C:\TestImage");
|
||||
int quality = 80;
|
||||
|
||||
_mockEnvironmentContext.FileTypeService.GetImageTypeAsync(pathId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From(imageType));
|
||||
_mockEnvironmentContext.FileProviderService.GetFileAsync(pathId)
|
||||
.Returns(Result.From(imageBytes));
|
||||
|
||||
// Act
|
||||
Result<Thumbnail> result = await _sut.GetThumbnailAsync(pathId, quality, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(imageType, result.Value.Type);
|
||||
Assert.NotEmpty(result.Value.Bytes);
|
||||
|
||||
using (MemoryStream ms = new(result.Value.Bytes))
|
||||
{
|
||||
Image loadedImage = Image.Load(ms);
|
||||
Assert.NotNull(loadedImage);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetThumbnailAsync_WithCancellation_ShouldThrowTaskCanceledException()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(@"C:\TestImage.jpg");
|
||||
int quality = 80;
|
||||
CancellationTokenSource cts = new();
|
||||
cts.Cancel();
|
||||
|
||||
_mockEnvironmentContext.FileTypeService.GetImageTypeAsync(pathId, cts.Token)
|
||||
.Returns(Task.FromCanceled<Result<ImageType>>(cts.Token));
|
||||
|
||||
// Act & Assert
|
||||
await Assert.ThrowsAsync<TaskCanceledException>(() =>
|
||||
_sut.GetThumbnailAsync(pathId, quality, cts.Token));
|
||||
}
|
||||
}
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Services;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Strategies.Environment;
|
||||
using NSubstitute;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.UnitTests.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Strategies.Environment;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="EnvironmentContext"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class EnvironmentContextTests
|
||||
{
|
||||
private readonly IDirectoryProviderService _mockDirectoryProviderService;
|
||||
private readonly IFileProviderService _mockFileProviderService;
|
||||
private readonly IFileTypeService _mockFileTypeService;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="EnvironmentContextTests"/> class.
|
||||
/// </summary>
|
||||
public EnvironmentContextTests()
|
||||
{
|
||||
_mockDirectoryProviderService = Substitute.For<IDirectoryProviderService>();
|
||||
_mockFileProviderService = Substitute.For<IFileProviderService>();
|
||||
_mockFileTypeService = Substitute.For<IFileTypeService>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WhenCalledWithValidParameters_ShouldInitializeAllProperties()
|
||||
{
|
||||
// Arrange & Act
|
||||
EnvironmentContext environmentContext = new(
|
||||
_mockDirectoryProviderService,
|
||||
_mockFileProviderService,
|
||||
_mockFileTypeService);
|
||||
|
||||
// Assert
|
||||
Assert.Same(_mockDirectoryProviderService, environmentContext.DirectoryProviderService);
|
||||
Assert.Same(_mockFileProviderService, environmentContext.FileProviderService);
|
||||
Assert.Same(_mockFileTypeService, environmentContext.FileTypeService);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FileTypeService_WhenAccessed_ShouldReturnInjectedService()
|
||||
{
|
||||
// Arrange
|
||||
EnvironmentContext environmentContext = new(
|
||||
_mockDirectoryProviderService,
|
||||
_mockFileProviderService,
|
||||
_mockFileTypeService);
|
||||
|
||||
// Act
|
||||
IFileTypeService result = environmentContext.FileTypeService;
|
||||
|
||||
// Assert
|
||||
Assert.Same(_mockFileTypeService, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FileProviderService_WhenAccessed_ShouldReturnInjectedService()
|
||||
{
|
||||
// Arrange
|
||||
EnvironmentContext environmentContext = new(
|
||||
_mockDirectoryProviderService,
|
||||
_mockFileProviderService,
|
||||
_mockFileTypeService);
|
||||
|
||||
// Act
|
||||
IFileProviderService result = environmentContext.FileProviderService;
|
||||
|
||||
// Assert
|
||||
Assert.Same(_mockFileProviderService, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DirectoryProviderService_WhenAccessed_ShouldReturnInjectedService()
|
||||
{
|
||||
// Arrange
|
||||
EnvironmentContext environmentContext = new(
|
||||
_mockDirectoryProviderService,
|
||||
_mockFileProviderService,
|
||||
_mockFileTypeService);
|
||||
|
||||
// Act
|
||||
IDirectoryProviderService result = environmentContext.DirectoryProviderService;
|
||||
|
||||
// Assert
|
||||
Assert.Same(_mockDirectoryProviderService, result);
|
||||
}
|
||||
}
|
||||
+551
@@ -0,0 +1,551 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Strategies.Path;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using NSubstitute;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO.Abstractions;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.UnitTests.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Strategies.Path;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="UnixPathStrategy"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class UnixPathStrategyTests
|
||||
{
|
||||
private readonly IFileSystem _mockFileSystem;
|
||||
private readonly UnixPathStrategy _sut;
|
||||
private readonly FileSystemPathIdFixture _fileSystemPathIdFixture = new();
|
||||
private readonly PathSegmentFixture _pathSegmentFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="UnixPathStrategyTests"/> class.
|
||||
/// </summary>
|
||||
public UnixPathStrategyTests()
|
||||
{
|
||||
_mockFileSystem = Substitute.For<IFileSystem>();
|
||||
_sut = new UnixPathStrategy(_mockFileSystem);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("/home/user")]
|
||||
[InlineData("/var/log/")]
|
||||
[InlineData("/etc/config.conf")]
|
||||
[InlineData("/")]
|
||||
[InlineData("/usr/local/bin")]
|
||||
public void IsValidPath_WithValidUnixPaths_ShouldReturnTrue(string path)
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(path);
|
||||
|
||||
// Act
|
||||
bool result = _sut.IsValidPath(pathId);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("C:\\Windows\\System32")]
|
||||
[InlineData("relative/path")]
|
||||
[InlineData("./relative/path")]
|
||||
[InlineData("../parent/path")]
|
||||
public void IsValidPath_WithInvalidUnixPaths_ShouldReturnFalse(string path)
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(path);
|
||||
|
||||
// Act
|
||||
bool result = _sut.IsValidPath(pathId);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsValidPath_WithPathContainingInvalidCharacters_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
string invalidPath = "/home/user/file\0name";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(invalidPath);
|
||||
|
||||
// Act
|
||||
bool result = _sut.IsValidPath(pathId);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("/home/user/file with spaces")]
|
||||
[InlineData("/home/user/file-with-dashes")]
|
||||
[InlineData("/home/user/file_with_underscores")]
|
||||
[InlineData("/home/user/file.with.dots")]
|
||||
[InlineData("/home/user/file~with~tilde")]
|
||||
[InlineData("/home/user/file!with!exclamation")]
|
||||
[InlineData("/home/user/file$with$dollar")]
|
||||
[InlineData("/home/user/file&with&ersand")]
|
||||
[InlineData("/home/user/file'with'singlequote")]
|
||||
[InlineData("/home/user/file(with)parentheses")]
|
||||
public void IsValidPath_WithValidSpecialCharacters_ShouldReturnTrue(string path)
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(path);
|
||||
|
||||
// Act
|
||||
bool result = _sut.IsValidPath(pathId);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsValidPath_WithVeryLongPath_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
string longPath = "/" + string.Join("/", Enumerable.Repeat("a", 100));
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(longPath);
|
||||
|
||||
// Act
|
||||
bool result = _sut.IsValidPath(pathId);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Exists_WhenPathExistsAndIsHiddenAndIncludeHiddenElementsIsTrue_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
string existingPath = "/home/user/existing_file.txt";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(existingPath);
|
||||
|
||||
_mockFileSystem.Path.Exists(existingPath).Returns(true);
|
||||
_mockFileSystem.File.Exists(existingPath).Returns(true);
|
||||
|
||||
// Act
|
||||
bool result = _sut.Exists(pathId, true);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
_mockFileSystem.Path.Received(1).Exists(existingPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Exists_WhenPathExistsAndIsHiddenAndIncludeHiddenElementsIsFalse_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
string existingPath = "/home/user/existing_file.txt";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(existingPath);
|
||||
_mockFileSystem.Path.Exists(existingPath).Returns(true);
|
||||
_mockFileSystem.File.Exists(existingPath).Returns(true);
|
||||
IFileInfo mockFileInfo = Substitute.For<IFileInfo>();
|
||||
mockFileInfo.Attributes.Returns(System.IO.FileAttributes.Hidden);
|
||||
_mockFileSystem.FileInfo.New(Arg.Any<string>()).Returns(mockFileInfo);
|
||||
|
||||
// Act
|
||||
bool result = _sut.Exists(pathId, false);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
_mockFileSystem.Path.Received(1).Exists(existingPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Exists_WhenPathDoesNotExist_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
string nonExistingPath = "/home/user/non_existing_file.txt";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(nonExistingPath);
|
||||
_mockFileSystem.Path.Exists(nonExistingPath).Returns(false);
|
||||
|
||||
// Act
|
||||
bool result = _sut.Exists(pathId);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
_mockFileSystem.Path.Received(1).Exists(nonExistingPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Exists_WithRootPath_ShouldCheckExistence()
|
||||
{
|
||||
// Arrange
|
||||
string rootPath = "/";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(rootPath);
|
||||
_mockFileSystem.Path.Exists(rootPath).Returns(true);
|
||||
|
||||
// Act
|
||||
bool result = _sut.Exists(pathId, true);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
_mockFileSystem.Path.Received(1).Exists(rootPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Exists_WithDirectoryPath_ShouldCheckExistence()
|
||||
{
|
||||
// Arrange
|
||||
string directoryPath = "/home/user/documents/";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(directoryPath);
|
||||
_mockFileSystem.Path.Exists(directoryPath).Returns(true);
|
||||
_mockFileSystem.Directory.Exists(directoryPath).Returns(true);
|
||||
|
||||
// Act
|
||||
bool result = _sut.Exists(pathId, true);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
_mockFileSystem.Path.Received(1).Exists(directoryPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Exists_WithFilePath_ShouldCheckExistence()
|
||||
{
|
||||
// Arrange
|
||||
string filePath = "/home/user/documents/file.txt";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(filePath);
|
||||
_mockFileSystem.Path.Exists(filePath).Returns(true);
|
||||
_mockFileSystem.File.Exists(filePath).Returns(true);
|
||||
|
||||
// Act
|
||||
bool result = _sut.Exists(pathId, true);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
_mockFileSystem.Path.Received(1).Exists(filePath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CombinePath_WithValidPathAndName_ShouldReturnCombinedPath()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create("/home/user");
|
||||
string name = "documents";
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.CombinePath(path, name);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal("/home/user/documents/", result.Value.Path);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CombinePath_WithTrailingSlashInPath_ShouldReturnCorrectlyCombinedPath()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create("/home/user/");
|
||||
string name = "documents";
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.CombinePath(path, name);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal("/home/user/documents/", result.Value.Path);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CombinePath_WithLeadingSlashInName_ShouldReturnCorrectlyCombinedPath()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create("/home/user");
|
||||
string name = "/documents";
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.CombinePath(path, name);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal("/home/user/documents/", result.Value.Path);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CombinePath_WithEmptyName_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create("/home/user");
|
||||
string name = "";
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.CombinePath(path, name);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.NameCannotBeEmpty, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CombinePath_WithNullName_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create("/home/user");
|
||||
string name = null!;
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.CombinePath(path, name);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.NameCannotBeEmpty, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CombinePath_WithRootPath_ShouldReturnCorrectlyCombinedPath()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create("/");
|
||||
string name = "home";
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.CombinePath(path, name);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal("/home/", result.Value.Path);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParsePath_WithValidUnixPath_ShouldReturnCorrectPathSegments()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create("/home/user/documents/file.txt");
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.ParsePath(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(5, result.Value.Count());
|
||||
Assert.Equal(_pathSegmentFixture.Create("/", false, true), result.Value.ElementAt(0));
|
||||
Assert.Equal(_pathSegmentFixture.Create("home", true, false), result.Value.ElementAt(1));
|
||||
Assert.Equal(_pathSegmentFixture.Create("user", true, false), result.Value.ElementAt(2));
|
||||
Assert.Equal(_pathSegmentFixture.Create("documents", true, false), result.Value.ElementAt(3));
|
||||
Assert.Equal(_pathSegmentFixture.Create("file.txt", false, false), result.Value.ElementAt(4));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParsePath_WithRootPath_ShouldReturnSingleRootSegment()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create("/");
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.ParsePath(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Single(result.Value);
|
||||
Assert.Equal(_pathSegmentFixture.Create("/", false, true), result.Value.Single());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParsePath_WithTrailingSlash_ShouldTreatLastSegmentAsDirectory()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create("/home/user/documents/");
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.ParsePath(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(4, result.Value.Count());
|
||||
Assert.Equal(_pathSegmentFixture.Create("documents", true, false), result.Value.Last());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParsePath_WithRelativePath_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create("home/user");
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.ParsePath(path);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParsePath_WithPathContainingDots_ShouldParseCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create("/home/user/file.with.dots.txt");
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.ParsePath(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(4, result.Value.Count());
|
||||
Assert.Equal(_pathSegmentFixture.Create("file.with.dots.txt", false, false), result.Value.Last());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GoUpOneLevel_WithValidPath_ShouldReturnParentPath()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create("/home/user/documents");
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.GoUpOneLevel(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(3, result.Value.Count());
|
||||
Assert.Equal(_pathSegmentFixture.Create("/", false, true), result.Value.ElementAt(0));
|
||||
Assert.Equal(_pathSegmentFixture.Create("home", true, false), result.Value.ElementAt(1));
|
||||
Assert.Equal(_pathSegmentFixture.Create("user", true, false), result.Value.ElementAt(2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GoUpOneLevel_WithRootPath_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create("/");
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.GoUpOneLevel(path);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.CannotNavigateUp, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GoUpOneLevel_WithTrailingSlash_ShouldReturnCorrectParentPath()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create("/home/user/");
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.GoUpOneLevel(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(2, result.Value.Count());
|
||||
Assert.Equal(_pathSegmentFixture.Create("/", false, true), result.Value.ElementAt(0));
|
||||
Assert.Equal(_pathSegmentFixture.Create("home", true, false), result.Value.ElementAt(1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GoUpOneLevel_WithInvalidPath_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create("invalid/path");
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.GoUpOneLevel(path);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GoUpOneLevel_WithSingleLevelPath_ShouldReturnRootPath()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create("/home");
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.GoUpOneLevel(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Single(result.Value);
|
||||
Assert.Equal(_pathSegmentFixture.Create("/", false, true), result.Value.Single());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetInvalidPathCharsForPlatform_WhenCalled_ShouldReturnOnlyNullCharacter()
|
||||
{
|
||||
// Act
|
||||
char[] result = _sut.GetInvalidPathCharsForPlatform();
|
||||
|
||||
// Assert
|
||||
Assert.Single(result);
|
||||
Assert.Contains('\0', result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetInvalidPathCharsForPlatform_WhenCalled_ShouldNotContainOtherCharacters()
|
||||
{
|
||||
// Act
|
||||
char[] result = _sut.GetInvalidPathCharsForPlatform();
|
||||
|
||||
// Assert
|
||||
Assert.DoesNotContain('/', result);
|
||||
Assert.DoesNotContain('\\', result);
|
||||
Assert.DoesNotContain(':', result);
|
||||
Assert.DoesNotContain('*', result);
|
||||
Assert.DoesNotContain('?', result);
|
||||
Assert.DoesNotContain('"', result);
|
||||
Assert.DoesNotContain('<', result);
|
||||
Assert.DoesNotContain('>', result);
|
||||
Assert.DoesNotContain('|', result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetInvalidPathCharsForPlatform_WhenCalled_ShouldReturnSameResultOnMultipleCalls()
|
||||
{
|
||||
// Act
|
||||
char[] result1 = _sut.GetInvalidPathCharsForPlatform();
|
||||
char[] result2 = _sut.GetInvalidPathCharsForPlatform();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(result1, result2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetPathRoot_WithValidUnixPath_ShouldReturnRootSegment()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create("/home/user/documents");
|
||||
|
||||
// Act
|
||||
Result<PathSegment> result = _sut.GetPathRoot(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(_pathSegmentFixture.Create("/", false, true), result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetPathRoot_WithRootPath_ShouldReturnRootSegment()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create("/");
|
||||
|
||||
// Act
|
||||
Result<PathSegment> result = _sut.GetPathRoot(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(_pathSegmentFixture.Create("/", false, true), result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetPathRoot_WithInvalidPath_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create("home/user");
|
||||
|
||||
// Act
|
||||
Result<PathSegment> result = _sut.GetPathRoot(path);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
}
|
||||
}
|
||||
+767
@@ -0,0 +1,767 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Strategies.Path;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using NSubstitute;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO.Abstractions;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.UnitTests.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Strategies.Path;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="WindowsPathStrategy"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class WindowsPathStrategyTests
|
||||
{
|
||||
private readonly IFileSystem _mockFileSystem;
|
||||
private readonly WindowsPathStrategy _sut;
|
||||
private readonly FileSystemPathIdFixture _fileSystemPathIdFixture = new();
|
||||
private readonly PathSegmentFixture _pathSegmentFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="WindowsPathStrategyTests"/> class.
|
||||
/// </summary>
|
||||
public WindowsPathStrategyTests()
|
||||
{
|
||||
_mockFileSystem = Substitute.For<IFileSystem>();
|
||||
_sut = new WindowsPathStrategy(_mockFileSystem);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(@"C:\Users\User")]
|
||||
[InlineData(@"D:\Program Files\")]
|
||||
[InlineData(@"E:\Documents\file.txt")]
|
||||
[InlineData(@"F:\")]
|
||||
[InlineData(@"G:\Projects\Visual Studio 2022\")]
|
||||
public void IsValidPath_WithValidWindowsPaths_ShouldReturnTrue(string path)
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(path);
|
||||
|
||||
// Act
|
||||
bool result = _sut.IsValidPath(pathId);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("/home/user")]
|
||||
[InlineData("relative\\path")]
|
||||
[InlineData(".\\relative\\path")]
|
||||
[InlineData("..\\parent\\path")]
|
||||
[InlineData("C:invalid_path")]
|
||||
public void IsValidPath_WithInvalidWindowsPaths_ShouldReturnFalse(string path)
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(path);
|
||||
|
||||
// Act
|
||||
bool result = _sut.IsValidPath(pathId);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsValidPath_WithPathContainingInvalidCharacters_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
string invalidPath = @"C:\Users\User\file<name>";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(invalidPath);
|
||||
|
||||
// Act
|
||||
bool result = _sut.IsValidPath(pathId);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(@"C:\Users\User\file with spaces")]
|
||||
[InlineData(@"D:\Projects\file-with-dashes")]
|
||||
[InlineData(@"E:\Documents\file_with_underscores")]
|
||||
[InlineData(@"F:\Data\file.with.dots")]
|
||||
[InlineData(@"G:\Backups\file(with)parentheses")]
|
||||
public void IsValidPath_WithValidSpecialCharacters_ShouldReturnTrue(string path)
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(path);
|
||||
|
||||
// Act
|
||||
bool result = _sut.IsValidPath(pathId);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IsValidPath_WithVeryLongPath_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
string longPath = @"C:\" + string.Join("\\", Enumerable.Repeat("a", 100));
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(longPath);
|
||||
|
||||
// Act
|
||||
bool result = _sut.IsValidPath(pathId);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(@"\\server\share")]
|
||||
[InlineData(@"\\server\share\folder")]
|
||||
[InlineData(@"\\server\share\folder\file.txt")]
|
||||
public void IsValidPath_WithValidUNCPaths_ShouldReturnTrue(string path)
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(path);
|
||||
|
||||
// Act
|
||||
bool result = _sut.IsValidPath(pathId);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(@"\\")]
|
||||
[InlineData(@"\\server")]
|
||||
[InlineData(@"\\\server\share")]
|
||||
public void IsValidPath_WithInvalidUNCPaths_ShouldReturnFalse(string path)
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(path);
|
||||
|
||||
// Act
|
||||
bool result = _sut.IsValidPath(pathId);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Exists_WhenPathExistsAndIsHiddenAndIncludeHiddenElementsIsTrue_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
string existingPath = @"C:\Users\User\existing_file.txt";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(existingPath);
|
||||
_mockFileSystem.Path.Exists(existingPath).Returns(true);
|
||||
_mockFileSystem.File.Exists(existingPath).Returns(true);
|
||||
|
||||
// Act
|
||||
bool result = _sut.Exists(pathId, true);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
_mockFileSystem.Path.Received(1).Exists(existingPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Exists_WhenPathExistsAndIsHiddenAndIncludeHiddenElementsIsFalse_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
string existingPath = @"C:\Users\User\existing_file.txt";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(existingPath);
|
||||
_mockFileSystem.Path.Exists(existingPath).Returns(true);
|
||||
_mockFileSystem.File.Exists(existingPath).Returns(true);
|
||||
IFileInfo mockFileInfo = Substitute.For<IFileInfo>();
|
||||
mockFileInfo.Attributes.Returns(System.IO.FileAttributes.Hidden);
|
||||
_mockFileSystem.FileInfo.New(Arg.Any<string>()).Returns(mockFileInfo);
|
||||
|
||||
// Act
|
||||
bool result = _sut.Exists(pathId, false);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
_mockFileSystem.Path.Received(1).Exists(existingPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Exists_WhenPathDoesNotExist_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
string nonExistingPath = @"C:\Users\User\non_existing_file.txt";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(nonExistingPath);
|
||||
_mockFileSystem.Path.Exists(nonExistingPath).Returns(false);
|
||||
|
||||
// Act
|
||||
bool result = _sut.Exists(pathId);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
_mockFileSystem.Path.Received(1).Exists(nonExistingPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Exists_WithRootPath_ShouldCheckExistence()
|
||||
{
|
||||
// Arrange
|
||||
string rootPath = @"C:\";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(rootPath);
|
||||
_mockFileSystem.Path.Exists(rootPath).Returns(true);
|
||||
|
||||
// Act
|
||||
bool result = _sut.Exists(pathId);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
_mockFileSystem.Path.Received(1).Exists(rootPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Exists_WithDirectoryPath_ShouldCheckExistence()
|
||||
{
|
||||
// Arrange
|
||||
string directoryPath = @"C:\Users\User\Documents\";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(directoryPath);
|
||||
_mockFileSystem.Path.Exists(directoryPath).Returns(true);
|
||||
_mockFileSystem.Directory.Exists(directoryPath).Returns(true);
|
||||
|
||||
// Act
|
||||
bool result = _sut.Exists(pathId, true);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
_mockFileSystem.Path.Received(1).Exists(directoryPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Exists_WithFilePath_ShouldCheckExistence()
|
||||
{
|
||||
// Arrange
|
||||
string filePath = @"C:\Users\User\Documents\file.txt";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(filePath);
|
||||
_mockFileSystem.Path.Exists(filePath).Returns(true);
|
||||
_mockFileSystem.File.Exists(filePath).Returns(true);
|
||||
|
||||
// Act
|
||||
bool result = _sut.Exists(pathId, true);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
_mockFileSystem.Path.Received(1).Exists(filePath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Exists_WithUNCPath_ShouldCheckExistence()
|
||||
{
|
||||
// Arrange
|
||||
string uncPath = @"\\server\share\folder\file.txt";
|
||||
FileSystemPathId pathId = _fileSystemPathIdFixture.Create(uncPath);
|
||||
_mockFileSystem.Path.Exists(uncPath).Returns(true);
|
||||
|
||||
// Act
|
||||
bool result = _sut.Exists(pathId, true);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
_mockFileSystem.Path.Received(1).Exists(uncPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CombinePath_WithValidPathAndName_ShouldReturnCombinedPath()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"C:\Users\User");
|
||||
string name = "Documents";
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.CombinePath(path, name);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(@"C:\Users\User\Documents\", result.Value.Path);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CombinePath_WithTrailingBackslashInPath_ShouldReturnCorrectlyCombinedPath()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"C:\Users\User\");
|
||||
string name = "Documents";
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.CombinePath(path, name);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(@"C:\Users\User\Documents\", result.Value.Path);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CombinePath_WithLeadingBackslashInName_ShouldReturnCorrectlyCombinedPath()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"C:\Users\User");
|
||||
string name = @"\Documents";
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.CombinePath(path, name);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(@"C:\Users\User\Documents\", result.Value.Path);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CombinePath_WithNullName_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"C:\Users\User");
|
||||
string name = null!;
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.CombinePath(path, name);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.NameCannotBeEmpty, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CombinePath_WithRootPath_ShouldReturnCorrectlyCombinedPath()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"C:\");
|
||||
string name = "Users";
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.CombinePath(path, name);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(@"C:\Users\", result.Value.Path);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CombinePath_WithUNCPath_ShouldReturnCorrectlyCombinedPath()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"\\server\share");
|
||||
string name = "folder";
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = _sut.CombinePath(path, name);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(@"\\server\share\folder\", result.Value.Path);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParsePath_WithValidWindowsPath_ShouldReturnCorrectPathSegments()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"C:\Users\User\Documents\file.txt");
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.ParsePath(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(5, result.Value.Count());
|
||||
Assert.Equivalent(_pathSegmentFixture.Create("C:", false, true), result.Value.ElementAt(0));
|
||||
Assert.Equivalent(_pathSegmentFixture.Create("Users", true, false), result.Value.ElementAt(1));
|
||||
Assert.Equivalent(_pathSegmentFixture.Create("User", true, false), result.Value.ElementAt(2));
|
||||
Assert.Equivalent(_pathSegmentFixture.Create("Documents", true, false), result.Value.ElementAt(3));
|
||||
Assert.Equivalent(_pathSegmentFixture.Create("file.txt", false, false), result.Value.ElementAt(4));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParsePath_WithRootPath_ShouldReturnSingleRootSegment()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"C:\");
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.ParsePath(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Single(result.Value);
|
||||
Assert.Equivalent(_pathSegmentFixture.Create("C:", false, true), result.Value.Single());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParsePath_WithTrailingBackslash_ShouldTreatLastSegmentAsDirectory()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"C:\Users\User\Documents\");
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.ParsePath(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(4, result.Value.Count());
|
||||
Assert.Equivalent(_pathSegmentFixture.Create("Documents", true, false), result.Value.Last());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParsePath_WithRelativePath_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"Users\User");
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.ParsePath(path);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParsePath_WithPathContainingDots_ShouldParseCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"C:\Users\User\file.with.dots.txt");
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.ParsePath(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(4, result.Value.Count());
|
||||
Assert.Equivalent(_pathSegmentFixture.Create("file.with.dots.txt", false, false), result.Value.Last());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParsePath_WithUNCPath_ShouldReturnCorrectPathSegments()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"\\server\share\folder\file.txt");
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.ParsePath(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(5, result.Value.Count());
|
||||
Assert.Equivalent(_pathSegmentFixture.Create(@"\\", false, true), result.Value.ElementAt(0));
|
||||
Assert.Equivalent(_pathSegmentFixture.Create("server", true, false), result.Value.ElementAt(1));
|
||||
Assert.Equivalent(_pathSegmentFixture.Create("share", true, false), result.Value.ElementAt(2));
|
||||
Assert.Equivalent(_pathSegmentFixture.Create("folder", true, false), result.Value.ElementAt(3));
|
||||
Assert.Equivalent(_pathSegmentFixture.Create("file.txt", false, false), result.Value.ElementAt(4));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GoUpOneLevel_WithValidPath_ShouldReturnParentPath()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"C:\Users\User\Documents");
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.GoUpOneLevel(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(3, result.Value.Count());
|
||||
Assert.Equivalent(_pathSegmentFixture.Create("C:", false, true), result.Value.ElementAt(0));
|
||||
Assert.Equivalent(_pathSegmentFixture.Create("Users", true, false), result.Value.ElementAt(1));
|
||||
Assert.Equivalent(_pathSegmentFixture.Create("User", true, false), result.Value.ElementAt(2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GoUpOneLevel_WithRootPath_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"C:\");
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.GoUpOneLevel(path);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.CannotNavigateUp, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GoUpOneLevel_WithTrailingBackslash_ShouldReturnCorrectParentPath()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"C:\Users\User\");
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.GoUpOneLevel(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(2, result.Value.Count());
|
||||
Assert.Equivalent(_pathSegmentFixture.Create("C:", false, true), result.Value.ElementAt(0));
|
||||
Assert.Equivalent(_pathSegmentFixture.Create("Users", true, false), result.Value.ElementAt(1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GoUpOneLevel_WithInvalidPath_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"invalid\path");
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.GoUpOneLevel(path);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GoUpOneLevel_WithSingleLevelPath_ShouldReturnRootPath()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"C:\Users");
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.GoUpOneLevel(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Single(result.Value);
|
||||
Assert.Equivalent(_pathSegmentFixture.Create("C:", false, true), result.Value.Single());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GoUpOneLevel_WithUNCPath_ShouldReturnParentPath()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"\\server\share\folder\subfolder");
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.GoUpOneLevel(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(4, result.Value.Count());
|
||||
Assert.Equivalent(_pathSegmentFixture.Create(@"\\", false, true), result.Value.ElementAt(0));
|
||||
Assert.Equivalent(_pathSegmentFixture.Create("server", true, false), result.Value.ElementAt(1));
|
||||
Assert.Equivalent(_pathSegmentFixture.Create("share", true, false), result.Value.ElementAt(2));
|
||||
Assert.Equivalent(_pathSegmentFixture.Create("folder", true, false), result.Value.ElementAt(3));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GoUpOneLevel_WithUNCRootPath_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"\\server\share");
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.GoUpOneLevel(path);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.CannotNavigateUp, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GoUpOneLevel_WithUNCPathWithOneFolder_ShouldReturnUNCRoot()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"\\server\share\folder");
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.GoUpOneLevel(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(3, result.Value.Count());
|
||||
Assert.Equivalent(_pathSegmentFixture.Create(@"\\", false, true), result.Value.ElementAt(0));
|
||||
Assert.Equivalent(_pathSegmentFixture.Create("server", true, false), result.Value.ElementAt(1));
|
||||
Assert.Equivalent(_pathSegmentFixture.Create("share", true, false), result.Value.ElementAt(2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GoUpOneLevel_WithDriveRootAndBackslash_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"C:\");
|
||||
|
||||
// Act
|
||||
Result<IEnumerable<PathSegment>> result = _sut.GoUpOneLevel(path);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.CannotNavigateUp, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetInvalidPathCharsForPlatform_WhenCalled_ShouldReturnExpectedCharacters()
|
||||
{
|
||||
// Act
|
||||
char[] result = _sut.GetInvalidPathCharsForPlatform();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(['<', '>', '"', '/', '|', '?', '*'], result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetInvalidPathCharsForPlatform_WhenCalled_ShouldNotContainCertainCharacters()
|
||||
{
|
||||
// Act
|
||||
char[] result = _sut.GetInvalidPathCharsForPlatform();
|
||||
|
||||
// Assert
|
||||
Assert.DoesNotContain('\\', result);
|
||||
Assert.DoesNotContain(':', result);
|
||||
Assert.DoesNotContain('\0', result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetInvalidPathCharsForPlatform_WhenCalled_ShouldReturnSameResultOnMultipleCalls()
|
||||
{
|
||||
// Act
|
||||
char[] result1 = _sut.GetInvalidPathCharsForPlatform();
|
||||
char[] result2 = _sut.GetInvalidPathCharsForPlatform();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(result1, result2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetPathRoot_WithValidWindowsPath_ShouldReturnRootSegment()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"C:\Users\User\Documents");
|
||||
|
||||
// Act
|
||||
Result<PathSegment> result = _sut.GetPathRoot(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equivalent(_pathSegmentFixture.Create("C:\\", true, true), result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetPathRoot_WithRootPath_ShouldReturnRootSegment()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"C:\");
|
||||
|
||||
// Act
|
||||
Result<PathSegment> result = _sut.GetPathRoot(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equivalent(_pathSegmentFixture.Create("C:\\", true, true), result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetPathRoot_WithInvalidPath_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"InvalidFolder");
|
||||
|
||||
// Act
|
||||
Result<PathSegment> result = _sut.GetPathRoot(path);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetPathRoot_WithUNCPath_ShouldReturnUNCRootSegment()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"\\server\share\folder");
|
||||
|
||||
// Act
|
||||
Result<PathSegment> result = _sut.GetPathRoot(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equivalent(_pathSegmentFixture.Create(@"\\server\share\", true, false), result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetPathRoot_WithInvalidUNCPath_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"\\");
|
||||
|
||||
// Act
|
||||
Result<PathSegment> result = _sut.GetPathRoot(path);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetPathRoot_WithUNCPathWithoutShare_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"\\server");
|
||||
|
||||
// Act
|
||||
Result<PathSegment> result = _sut.GetPathRoot(path);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetPathRoot_WithNonDriveNonUNCPath_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"InvalidPath");
|
||||
|
||||
// Act
|
||||
Result<PathSegment> result = _sut.GetPathRoot(path);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetPathRoot_WithUNCPathMissingServerName_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"\\");
|
||||
|
||||
// Act
|
||||
Result<PathSegment> result = _sut.GetPathRoot(path);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetPathRoot_WithUNCPathWithoutThirdBackslash_ShouldReturnServerAndShare()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"\\server\share");
|
||||
|
||||
// Act
|
||||
Result<PathSegment> result = _sut.GetPathRoot(path);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equivalent(_pathSegmentFixture.Create(@"\\server\share\", true, false), result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetPathRoot_WithNeitherUNCNorDrivePath_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId path = _fileSystemPathIdFixture.Create(@"invalid\path");
|
||||
|
||||
// Act
|
||||
Result<PathSegment> result = _sut.GetPathRoot(path);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
}
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Strategies.Path;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Strategies.Platform;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Strategies.Platform;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO.Abstractions;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.UnitTests.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Strategies.Platform;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="PlatformContextFactory"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class PlatformContextFactoryTests
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly PlatformContextFactory _sut;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PlatformContextFactoryTests"/> class.
|
||||
/// </summary>
|
||||
public PlatformContextFactoryTests()
|
||||
{
|
||||
ServiceCollection services = new();
|
||||
services.AddTransient<IWindowsPlatformContext, WindowsPlatformContext>();
|
||||
services.AddTransient<IUnixPlatformContext, UnixPlatformContext>();
|
||||
services.AddTransient<IUnixPathStrategy, UnixPathStrategy>();
|
||||
services.AddTransient<IWindowsPathStrategy, WindowsPathStrategy>();
|
||||
services.AddTransient<IFileSystem, FileSystem>();
|
||||
_serviceProvider = services.BuildServiceProvider();
|
||||
_sut = new PlatformContextFactory(_serviceProvider);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateStrategy_WhenWindowsServiceExists_ShouldReturnWindowsService()
|
||||
{
|
||||
// Act
|
||||
IWindowsPlatformContext result = _sut.CreateStrategy<IWindowsPlatformContext>();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.IsType<WindowsPlatformContext>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateStrategy_WhenUnixServiceExists_ShouldReturnUnixService()
|
||||
{
|
||||
// Act
|
||||
IUnixPlatformContext result = _sut.CreateStrategy<IUnixPlatformContext>();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.IsType<UnixPlatformContext>(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateStrategy_WhenServiceDoesNotExist_ShouldThrowInvalidOperationException()
|
||||
{
|
||||
// Act & Assert
|
||||
Assert.Throws<InvalidOperationException>(() => _sut.CreateStrategy<INonExistentPlatformContextFixture>());
|
||||
}
|
||||
}
|
||||
+150
@@ -0,0 +1,150 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.FileSystem;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Strategies.Platform;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.InteropServices;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.UnitTests.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Strategies.Platform;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="PlatformContextManager"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class PlatformContextManagerTests
|
||||
{
|
||||
private readonly IPlatformContextFactory _mockPlatformContextFactory;
|
||||
private PlatformContextManager? sut;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PlatformContextManagerTests"/> class.
|
||||
/// </summary>
|
||||
public PlatformContextManagerTests()
|
||||
{
|
||||
_mockPlatformContextFactory = Substitute.For<IPlatformContextFactory>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetCurrentContext_WhenNoContextSet_ShouldSetDefaultContext()
|
||||
{
|
||||
// Arrange
|
||||
IWindowsPlatformContext mockWindowsContext = Substitute.For<IWindowsPlatformContext>();
|
||||
_mockPlatformContextFactory.CreateStrategy<IWindowsPlatformContext>().Returns(mockWindowsContext);
|
||||
IOperatingSystemInfo mockOsInfo = Substitute.For<IOperatingSystemInfo>();
|
||||
mockOsInfo.IsOSPlatform(OSPlatform.Linux).Returns(false);
|
||||
sut = new(_mockPlatformContextFactory, mockOsInfo);
|
||||
|
||||
// Act
|
||||
IPlatformContext result = sut.GetCurrentContext();
|
||||
|
||||
// Assert
|
||||
Assert.Same(mockWindowsContext, result);
|
||||
_mockPlatformContextFactory.Received(1).CreateStrategy<IWindowsPlatformContext>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetCurrentContext_WhenContextAlreadySet_ShouldReturnExistingContext()
|
||||
{
|
||||
// Arrange
|
||||
IUnixPlatformContext mockUnixContext = Substitute.For<IUnixPlatformContext>();
|
||||
_mockPlatformContextFactory.CreateStrategy<IUnixPlatformContext>().Returns(mockUnixContext);
|
||||
IOperatingSystemInfo mockOsInfo = Substitute.For<IOperatingSystemInfo>();
|
||||
sut = new(_mockPlatformContextFactory, mockOsInfo);
|
||||
sut.SetCurrentPlatform(PlatformType.Unix);
|
||||
|
||||
// Act
|
||||
IPlatformContext result = sut.GetCurrentContext();
|
||||
|
||||
// Assert
|
||||
Assert.Same(mockUnixContext, result);
|
||||
_mockPlatformContextFactory.Received(1).CreateStrategy<IUnixPlatformContext>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetCurrentPlatform_WhenUnixPlatform_ShouldSetUnixContext()
|
||||
{
|
||||
// Arrange
|
||||
IUnixPlatformContext mockUnixContext = Substitute.For<IUnixPlatformContext>();
|
||||
_mockPlatformContextFactory.CreateStrategy<IUnixPlatformContext>().Returns(mockUnixContext);
|
||||
IOperatingSystemInfo mockOsInfo = Substitute.For<IOperatingSystemInfo>();
|
||||
mockOsInfo.IsOSPlatform(OSPlatform.Linux).Returns(true);
|
||||
sut = new(_mockPlatformContextFactory, mockOsInfo);
|
||||
|
||||
// Act
|
||||
sut.SetCurrentPlatform(PlatformType.Unix);
|
||||
IPlatformContext result = sut.GetCurrentContext();
|
||||
|
||||
// Assert
|
||||
Assert.Same(mockUnixContext, result);
|
||||
_mockPlatformContextFactory.Received(1).CreateStrategy<IUnixPlatformContext>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetCurrentPlatform_WhenWindowsPlatform_ShouldSetWindowsContext()
|
||||
{
|
||||
// Arrange
|
||||
IWindowsPlatformContext mockWindowsContext = Substitute.For<IWindowsPlatformContext>();
|
||||
_mockPlatformContextFactory.CreateStrategy<IWindowsPlatformContext>().Returns(mockWindowsContext);
|
||||
IOperatingSystemInfo mockOsInfo = Substitute.For<IOperatingSystemInfo>();
|
||||
mockOsInfo.IsOSPlatform(OSPlatform.Linux).Returns(false);
|
||||
sut = new(_mockPlatformContextFactory, mockOsInfo);
|
||||
|
||||
// Act
|
||||
sut.SetCurrentPlatform(PlatformType.Windows);
|
||||
IPlatformContext result = sut.GetCurrentContext();
|
||||
|
||||
// Assert
|
||||
Assert.Same(mockWindowsContext, result);
|
||||
_mockPlatformContextFactory.Received(1).CreateStrategy<IWindowsPlatformContext>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetCurrentPlatform_WhenUnsupportedPlatform_ShouldThrowArgumentException()
|
||||
{
|
||||
// Arrange
|
||||
PlatformType unsupportedPlatform = (PlatformType)999; // An unsupported platform type
|
||||
IOperatingSystemInfo mockOsInfo = Substitute.For<IOperatingSystemInfo>();
|
||||
sut = new(_mockPlatformContextFactory, mockOsInfo);
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<ArgumentException>(() => sut.SetCurrentPlatform(unsupportedPlatform));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetCurrentContext_WhenRunningOnLinux_ShouldSetUnixContextByDefault()
|
||||
{
|
||||
// Arrange
|
||||
IUnixPlatformContext mockUnixContext = Substitute.For<IUnixPlatformContext>();
|
||||
_mockPlatformContextFactory.CreateStrategy<IUnixPlatformContext>().Returns(mockUnixContext);
|
||||
IOperatingSystemInfo mockOsInfo = Substitute.For<IOperatingSystemInfo>();
|
||||
mockOsInfo.IsOSPlatform(OSPlatform.Linux).Returns(true);
|
||||
sut = new(_mockPlatformContextFactory, mockOsInfo);
|
||||
|
||||
// Act
|
||||
IPlatformContext result = sut.GetCurrentContext();
|
||||
|
||||
// Assert
|
||||
Assert.Same(mockUnixContext, result);
|
||||
_mockPlatformContextFactory.Received(1).CreateStrategy<IUnixPlatformContext>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetCurrentContext_WhenRunningOnWindows_ShouldSetWindowsContextByDefault()
|
||||
{
|
||||
// Arrange
|
||||
IWindowsPlatformContext mockWindowsContext = Substitute.For<IWindowsPlatformContext>();
|
||||
_mockPlatformContextFactory.CreateStrategy<IWindowsPlatformContext>().Returns(mockWindowsContext);
|
||||
IOperatingSystemInfo mockOsInfo = Substitute.For<IOperatingSystemInfo>();
|
||||
mockOsInfo.IsOSPlatform(OSPlatform.Linux).Returns(false);
|
||||
sut = new(_mockPlatformContextFactory, mockOsInfo);
|
||||
|
||||
// Act
|
||||
IPlatformContext result = sut.GetCurrentContext();
|
||||
|
||||
// Assert
|
||||
Assert.Same(mockWindowsContext, result);
|
||||
_mockPlatformContextFactory.Received(1).CreateStrategy<IWindowsPlatformContext>();
|
||||
}
|
||||
}
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.FileSystem;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Strategies.Path;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Strategies.Platform;
|
||||
using NSubstitute;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.UnitTests.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Strategies.Platform;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="WindowsPlatformContext"/> and <see cref="UnixPlatformContext"/> classes.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class PlatformContextTests
|
||||
{
|
||||
[Fact]
|
||||
public void WindowsPlatformContext_Constructor_ShouldSetPropertiesCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
IWindowsPathStrategy mockWindowsPathStrategy = Substitute.For<IWindowsPathStrategy>();
|
||||
|
||||
// Act
|
||||
WindowsPlatformContext windowsPlatformContext = new(mockWindowsPathStrategy);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(PlatformType.Windows, windowsPlatformContext.Platform);
|
||||
Assert.Equal(mockWindowsPathStrategy, windowsPlatformContext.PathStrategy);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UnixPlatformContext_Constructor_ShouldSetPropertiesCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
IUnixPathStrategy mockUnixPathStrategy = Substitute.For<IUnixPathStrategy>();
|
||||
|
||||
// Act
|
||||
UnixPlatformContext unixPlatformContext = new(mockUnixPathStrategy);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(PlatformType.Unix, unixPlatformContext.Platform);
|
||||
Assert.Equal(mockUnixPathStrategy, unixPlatformContext.PathStrategy);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WindowsPlatformContext_ShouldImplementIWindowsPlatformContext()
|
||||
{
|
||||
// Arrange
|
||||
IWindowsPathStrategy mockWindowsPathStrategy = Substitute.For<IWindowsPathStrategy>();
|
||||
|
||||
// Act
|
||||
WindowsPlatformContext windowsPlatformContext = new(mockWindowsPathStrategy);
|
||||
|
||||
// Assert
|
||||
Assert.IsAssignableFrom<IWindowsPlatformContext>(windowsPlatformContext);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UnixPlatformContext_ShouldImplementIUnixPlatformContext()
|
||||
{
|
||||
// Arrange
|
||||
IUnixPathStrategy mockUnixPathStrategy = Substitute.For<IUnixPathStrategy>();
|
||||
|
||||
// Act
|
||||
UnixPlatformContext unixPlatformContext = new(mockUnixPathStrategy);
|
||||
|
||||
// Assert
|
||||
Assert.IsAssignableFrom<IUnixPlatformContext>(unixPlatformContext);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WindowsPlatformContext_PathStrategy_ShouldBeReadOnly()
|
||||
{
|
||||
// Arrange
|
||||
IWindowsPathStrategy mockWindowsPathStrategy = Substitute.For<IWindowsPathStrategy>();
|
||||
WindowsPlatformContext windowsPlatformContext = new(mockWindowsPathStrategy);
|
||||
|
||||
// Act & Assert
|
||||
Assert.Null(windowsPlatformContext.GetType().GetProperty(nameof(WindowsPlatformContext.PathStrategy))!.SetMethod);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UnixPlatformContext_PathStrategy_ShouldBeReadOnly()
|
||||
{
|
||||
// Arrange
|
||||
IUnixPathStrategy mockUnixPathStrategy = Substitute.For<IUnixPathStrategy>();
|
||||
UnixPlatformContext unixPlatformContext = new(mockUnixPathStrategy);
|
||||
|
||||
// Act & Assert
|
||||
Assert.Null(unixPlatformContext.GetType().GetProperty(nameof(UnixPlatformContext.PathStrategy))!.SetMethod);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WindowsPlatformContext_Platform_ShouldBeReadOnly()
|
||||
{
|
||||
// Arrange
|
||||
IWindowsPathStrategy mockWindowsPathStrategy = Substitute.For<IWindowsPathStrategy>();
|
||||
WindowsPlatformContext windowsPlatformContext = new(mockWindowsPathStrategy);
|
||||
|
||||
// Act & Assert
|
||||
Assert.Null(windowsPlatformContext.GetType().GetProperty(nameof(WindowsPlatformContext.Platform))!.SetMethod);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UnixPlatformContext_Platform_ShouldBeReadOnly()
|
||||
{
|
||||
// Arrange
|
||||
IUnixPathStrategy mockUnixPathStrategy = Substitute.For<IUnixPathStrategy>();
|
||||
UnixPlatformContext unixPlatformContext = new(mockUnixPathStrategy);
|
||||
|
||||
// Act & Assert
|
||||
Assert.Null(unixPlatformContext.GetType().GetProperty(nameof(UnixPlatformContext.Platform))!.SetMethod);
|
||||
}
|
||||
}
|
||||
+135
@@ -0,0 +1,135 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.UnitTests.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="FileSystemPathId"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class FileSystemPathIdTests
|
||||
{
|
||||
private readonly FileSystemPathIdFixture _fileSystemPathIdFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void Create_WithValidPath_ShouldReturnFileSystemPathId()
|
||||
{
|
||||
// Arrange
|
||||
string validPath = @"C:\TestDir\file.txt";
|
||||
|
||||
// Act
|
||||
Result<FileSystemPathId> result = FileSystemPathId.Create(validPath);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(validPath, result.Value.Path);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
[InlineData(" ")]
|
||||
public void Create_WithInvalidPath_ShouldReturnError(string? invalidPath)
|
||||
{
|
||||
// Act
|
||||
Result<FileSystemPathId> result = FileSystemPathId.Create(invalidPath!);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.InvalidPath, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equals_WithSamePath_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
string path = @"C:\TestDir\file.txt";
|
||||
FileSystemPathId id1 = _fileSystemPathIdFixture.Create(path);
|
||||
FileSystemPathId id2 = _fileSystemPathIdFixture.Create(path);
|
||||
|
||||
// Act
|
||||
bool result = id1.Equals(id2);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equals_WithDifferentPath_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId id1 = _fileSystemPathIdFixture.Create(@"C:\TestDir\file1.txt");
|
||||
FileSystemPathId id2 = _fileSystemPathIdFixture.Create(@"C:\TestDir\file2.txt");
|
||||
|
||||
// Act
|
||||
bool result = id1.Equals(id2);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetHashCode_WithSamePath_ShouldReturnSameHashCode()
|
||||
{
|
||||
// Arrange
|
||||
string path = @"C:\TestDir\file.txt";
|
||||
FileSystemPathId id1 = _fileSystemPathIdFixture.Create(path);
|
||||
FileSystemPathId id2 = _fileSystemPathIdFixture.Create(path);
|
||||
|
||||
// Act
|
||||
int hashCode1 = id1.GetHashCode();
|
||||
int hashCode2 = id2.GetHashCode();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(hashCode1, hashCode2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetHashCode_WithDifferentPath_ShouldReturnDifferentHashCode()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId id1 = _fileSystemPathIdFixture.Create(@"C:\TestDir\file1.txt");
|
||||
FileSystemPathId id2 = _fileSystemPathIdFixture.Create(@"C:\TestDir\file2.txt");
|
||||
|
||||
// Act
|
||||
int hashCode1 = id1.GetHashCode();
|
||||
int hashCode2 = id2.GetHashCode();
|
||||
|
||||
// Assert
|
||||
Assert.NotEqual(hashCode1, hashCode2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EqualityOperator_WithSamePath_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
string path = @"C:\TestDir\file.txt";
|
||||
FileSystemPathId id1 = _fileSystemPathIdFixture.Create(path);
|
||||
FileSystemPathId id2 = _fileSystemPathIdFixture.Create(path);
|
||||
|
||||
// Act
|
||||
bool result = id1 == id2;
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InequalityOperator_WithDifferentPath_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
FileSystemPathId id1 = _fileSystemPathIdFixture.Create(@"C:\TestDir\file1.txt");
|
||||
FileSystemPathId id2 = _fileSystemPathIdFixture.Create(@"C:\TestDir\file2.txt");
|
||||
|
||||
// Act
|
||||
bool result = id1 != id2;
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
}
|
||||
+136
@@ -0,0 +1,136 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.UnitTests.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="PathSegment"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class PathSegmentTests
|
||||
{
|
||||
private readonly PathSegmentFixture _pathSegmentFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void Create_WithValidInput_ShouldReturnPathSegment()
|
||||
{
|
||||
// Arrange
|
||||
string name = "testFolder";
|
||||
bool isDirectory = true;
|
||||
bool isDrive = false;
|
||||
|
||||
// Act
|
||||
Result<PathSegment> result = PathSegment.Create(name, isDirectory, isDrive);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(name, result.Value.Name);
|
||||
Assert.Equal(isDirectory, result.Value.IsDirectory);
|
||||
Assert.Equal(isDrive, result.Value.IsDrive);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
[InlineData(" ")]
|
||||
public void Create_WithInvalidName_ShouldReturnError(string? invalidName)
|
||||
{
|
||||
// Act
|
||||
Result<PathSegment> result = PathSegment.Create(invalidName!, true, false);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.NameCannotBeEmpty, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equals_WithSameProperties_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
PathSegment segment1 = _pathSegmentFixture.Create("folder", true, false);
|
||||
PathSegment segment2 = _pathSegmentFixture.Create("folder", true, false);
|
||||
|
||||
// Act
|
||||
bool result = segment1.Equals(segment2);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equals_WithDifferentProperties_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
PathSegment segment1 = _pathSegmentFixture.Create("folder1", true, false);
|
||||
PathSegment segment2 = _pathSegmentFixture.Create("folder2", true, false);
|
||||
|
||||
// Act
|
||||
bool result = segment1.Equals(segment2);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetHashCode_WithSameProperties_ShouldReturnSameHashCode()
|
||||
{
|
||||
// Arrange
|
||||
PathSegment segment1 = _pathSegmentFixture.Create("folder", true, false);
|
||||
PathSegment segment2 = _pathSegmentFixture.Create("folder", true, false);
|
||||
|
||||
// Act
|
||||
int hashCode1 = segment1.GetHashCode();
|
||||
int hashCode2 = segment2.GetHashCode();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(hashCode1, hashCode2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetHashCode_WithDifferentProperties_ShouldReturnDifferentHashCode()
|
||||
{
|
||||
// Arrange
|
||||
PathSegment segment1 = _pathSegmentFixture.Create("folder1", true, false);
|
||||
PathSegment segment2 = _pathSegmentFixture.Create("folder2", true, false);
|
||||
|
||||
// Act
|
||||
int hashCode1 = segment1.GetHashCode();
|
||||
int hashCode2 = segment2.GetHashCode();
|
||||
|
||||
// Assert
|
||||
Assert.NotEqual(hashCode1, hashCode2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EqualityOperator_WithSameProperties_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
PathSegment segment1 = _pathSegmentFixture.Create("folder", true, false);
|
||||
PathSegment segment2 = _pathSegmentFixture.Create("folder", true, false);
|
||||
|
||||
// Act
|
||||
bool result = segment1 == segment2;
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InequalityOperator_WithDifferentProperties_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
PathSegment segment1 = _pathSegmentFixture.Create("folder1", true, false);
|
||||
PathSegment segment2 = _pathSegmentFixture.Create("folder2", true, false);
|
||||
|
||||
// Act
|
||||
bool result = segment1 != segment2;
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
}
|
||||
+160
@@ -0,0 +1,160 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
|
||||
namespace Lumina.Domain.UnitTests.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="StreamInfo"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class StreamInfoTests
|
||||
{
|
||||
private readonly StreamInfoFixture _streamInfoFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void Create_WithValidInput_ShouldReturnStreamInfo()
|
||||
{
|
||||
// Arrange
|
||||
string streamId = "testStream";
|
||||
string mimeType = "video/mp4";
|
||||
int bitrate = 1000;
|
||||
string codec = "h264";
|
||||
Optional<string> resolution = Optional<string>.Some("1920x1080");
|
||||
Optional<float> frameRate = Optional<float>.Some(30.0f);
|
||||
Optional<int> sampleRate = Optional<int>.Some(44100);
|
||||
Optional<int> channels = Optional<int>.Some(2);
|
||||
|
||||
// Act
|
||||
Result<StreamInfo> result = StreamInfo.Create(streamId, mimeType, bitrate, codec, resolution, frameRate, sampleRate, channels);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(streamId, result.Value.StreamId);
|
||||
Assert.Equal(mimeType, result.Value.MimeType);
|
||||
Assert.Equal(bitrate, result.Value.Bitrate);
|
||||
Assert.Equal(codec, result.Value.Codec);
|
||||
Assert.Equal(resolution, result.Value.Resolution);
|
||||
Assert.Equal(frameRate, result.Value.FrameRate);
|
||||
Assert.Equal(sampleRate, result.Value.SampleRate);
|
||||
Assert.Equal(channels, result.Value.Channels);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Create_WithNullStreamId_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
string? streamId = null;
|
||||
string mimeType = "video/mp4";
|
||||
int bitrate = 1000;
|
||||
string codec = "h264";
|
||||
|
||||
// Act
|
||||
Result<StreamInfo> result = StreamInfo.Create(streamId!, mimeType, bitrate, codec, Optional<string>.None(), Optional<float>.None(), Optional<int>.None(), Optional<int>.None());
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.StreamIdCannotBeEmpty, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Create_WithNullCodec_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
string streamId = "testStream";
|
||||
string mimeType = "video/mp4";
|
||||
int bitrate = 1000;
|
||||
string? codec = null;
|
||||
|
||||
// Act
|
||||
Result<StreamInfo> result = StreamInfo.Create(streamId, mimeType, bitrate, codec!, Optional<string>.None(), Optional<float>.None(), Optional<int>.None(), Optional<int>.None());
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.CodecCannotBeEmpty, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Create_WithNonPositiveBitrate_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
string streamId = "testStream";
|
||||
string mimeType = "video/mp4";
|
||||
int bitrate = 0;
|
||||
string codec = "h264";
|
||||
|
||||
// Act
|
||||
Result<StreamInfo> result = StreamInfo.Create(streamId, mimeType, bitrate, codec, Optional<string>.None(), Optional<float>.None(), Optional<int>.None(), Optional<int>.None());
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.FileSystemManagement.BitrateMustBeAPositiveNumber, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equals_WithSameProperties_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
StreamInfo info1 = _streamInfoFixture.Create();
|
||||
StreamInfo info2 = _streamInfoFixture.Create(
|
||||
info1.StreamId, info1.MimeType, info1.Bitrate, info1.Codec,
|
||||
info1.Resolution, info1.FrameRate, info1.SampleRate, info1.Channels);
|
||||
|
||||
// Act
|
||||
bool result = info1.Equals(info2);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equals_WithDifferentProperties_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
StreamInfo info1 = _streamInfoFixture.Create();
|
||||
StreamInfo info2 = _streamInfoFixture.Create();
|
||||
|
||||
// Act
|
||||
bool result = info1.Equals(info2);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetHashCode_WithSameProperties_ShouldReturnSameHashCode()
|
||||
{
|
||||
// Arrange
|
||||
StreamInfo info1 = _streamInfoFixture.Create();
|
||||
StreamInfo info2 = _streamInfoFixture.Create(
|
||||
info1.StreamId, info1.MimeType, info1.Bitrate, info1.Codec,
|
||||
info1.Resolution, info1.FrameRate, info1.SampleRate, info1.Channels);
|
||||
|
||||
// Act
|
||||
int hashCode1 = info1.GetHashCode();
|
||||
int hashCode2 = info2.GetHashCode();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(hashCode1, hashCode2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetHashCode_WithDifferentProperties_ShouldReturnDifferentHashCode()
|
||||
{
|
||||
// Arrange
|
||||
StreamInfo info1 = _streamInfoFixture.Create();
|
||||
StreamInfo info2 = _streamInfoFixture.Create();
|
||||
|
||||
// Act
|
||||
int hashCode1 = info1.GetHashCode();
|
||||
int hashCode2 = info2.GetHashCode();
|
||||
|
||||
// Assert
|
||||
Assert.NotEqual(hashCode1, hashCode2);
|
||||
}
|
||||
}
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.PhotoLibrary;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
|
||||
namespace Lumina.Domain.UnitTests.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.ValueObjects;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="Thumbnail"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class ThumbnailTests
|
||||
{
|
||||
private readonly ThumbnailFixture _thumbnailFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WhenCalled_ShouldSetPropertiesCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
ImageType type = ImageType.PNG;
|
||||
byte[] bytes = [1, 2, 3, 4, 5];
|
||||
|
||||
// Act
|
||||
Thumbnail thumbnail = new(type, bytes);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(type, thumbnail.Type);
|
||||
Assert.Equal(bytes, thumbnail.Bytes);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equals_WithSameProperties_ShouldReturnTrue()
|
||||
{
|
||||
// Arrange
|
||||
Thumbnail thumbnail1 = _thumbnailFixture.Create();
|
||||
Thumbnail thumbnail2 = new(thumbnail1.Type, thumbnail1.Bytes);
|
||||
|
||||
// Act
|
||||
bool result = thumbnail1.Equals(thumbnail2);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equals_WithDifferentType_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
Thumbnail thumbnail1 = _thumbnailFixture.Create(ImageType.PNG);
|
||||
Thumbnail thumbnail2 = _thumbnailFixture.Create(ImageType.JPEG, thumbnail1.Bytes);
|
||||
|
||||
// Act
|
||||
bool result = thumbnail1.Equals(thumbnail2);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Equals_WithDifferentBytes_ShouldReturnFalse()
|
||||
{
|
||||
// Arrange
|
||||
Thumbnail thumbnail1 = _thumbnailFixture.Create();
|
||||
Thumbnail thumbnail2 = _thumbnailFixture.Create(thumbnail1.Type);
|
||||
|
||||
// Act
|
||||
bool result = thumbnail1.Equals(thumbnail2);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetHashCode_WithSameProperties_ShouldReturnSameHashCode()
|
||||
{
|
||||
// Arrange
|
||||
Thumbnail thumbnail1 = _thumbnailFixture.Create();
|
||||
Thumbnail thumbnail2 = new(thumbnail1.Type, thumbnail1.Bytes);
|
||||
|
||||
// Act
|
||||
int hashCode1 = thumbnail1.GetHashCode();
|
||||
int hashCode2 = thumbnail2.GetHashCode();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(hashCode1, hashCode2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetHashCode_WithDifferentProperties_ShouldReturnDifferentHashCode()
|
||||
{
|
||||
// Arrange
|
||||
Thumbnail thumbnail1 = _thumbnailFixture.Create();
|
||||
Thumbnail thumbnail2 = _thumbnailFixture.Create();
|
||||
|
||||
// Act
|
||||
int hashCode1 = thumbnail1.GetHashCode();
|
||||
int hashCode2 = thumbnail2.GetHashCode();
|
||||
|
||||
// Assert
|
||||
Assert.NotEqual(hashCode1, hashCode2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetEqualityComponents_ShouldReturnTypeAndBytes()
|
||||
{
|
||||
// Arrange
|
||||
Thumbnail thumbnail = _thumbnailFixture.Create();
|
||||
|
||||
// Act
|
||||
List<object> components = [.. thumbnail.GetEqualityComponents()];
|
||||
|
||||
// Assert
|
||||
Assert.Equal(2, components.Count);
|
||||
Assert.Equal(thumbnail.Type, components[0]);
|
||||
Assert.Equal(thumbnail.Bytes, components[1]);
|
||||
}
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.BookLibrary;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.UnitTests.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="Book"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class BookTests
|
||||
{
|
||||
private readonly BookFixture _bookFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void Create_WhenCalledWithValidData_ShouldCreateBookWithPendingMetadataStatus()
|
||||
{
|
||||
// Act
|
||||
Book book = _bookFixture.Create();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(book);
|
||||
Assert.Equal(MetadataStatus.Pending, book.MetadataStatus);
|
||||
Assert.False(book.LastMetadataUpdateUtc.HasValue);
|
||||
Assert.False(book.MetadataProvider.HasValue);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MarkMetadataAsEnriched_WhenCalled_ShouldUpdateStatusAndProvider()
|
||||
{
|
||||
// Arrange
|
||||
Book book = _bookFixture.Create();
|
||||
DateTime lastUpdateUtc = DateTime.UtcNow;
|
||||
|
||||
// Act
|
||||
book.MarkMetadataAsEnriched("Goodreads", lastUpdateUtc);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(MetadataStatus.Enriched, book.MetadataStatus);
|
||||
Assert.Equal("Goodreads", book.MetadataProvider.Value);
|
||||
Assert.Equal(lastUpdateUtc, book.LastMetadataUpdateUtc.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MarkMetadataAsFailed_WhenCalled_ShouldUpdateStatus()
|
||||
{
|
||||
// Arrange
|
||||
Book book = _bookFixture.Create();
|
||||
|
||||
// Act
|
||||
book.MarkMetadataAsFailed();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(MetadataStatus.Failed, book.MetadataStatus);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user