Adds Application layer unit tests for file system browser dialog

This commit is contained in:
BinaryAlley committed 2024-10-05 09:35:10 +03:00
1 parent 1f5322fa1d
commit 0e3e688eb7
79 files changed
+5484 -91

No files matched your search

@@ -0,0 +1,99 @@
#region ========================================================================= USING =====================================================================================
using AutoFixture;
using AutoFixture.AutoNSubstitute;
using ErrorOr;
using FluentAssertions;
using Lumina.Application.Core.FileManagement.Paths.Commands.CombinePath;
using Lumina.Application.UnitTests.Core.FileManagement.Pahs.Commands.CombinePath.Fixtures;
using Lumina.Contracts.Responses.FileManagement;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.Services;
using NSubstitute;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.UnitTests.Core.FileManagement.Pahs.Commands.CombinePath;
/// <summary>
/// Contains unit tests for the <see cref="CombinePathCommandHandler"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class CombinePathCommandHandlerTests
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly IFixture _fixture;
private readonly IPathService _mockPathService;
private readonly CombinePathCommandHandler _sut;
#endregion
#region ====================================================================== CTOR =====================================================================================
/// <summary>
/// Initializes a new instance of the <see cref="CombinePathCommandHandlerTests"/> class.
/// </summary>
public CombinePathCommandHandlerTests()
{
_fixture = new Fixture().Customize(new AutoNSubstituteCustomization());
_mockPathService = Substitute.For<IPathService>();
_sut = new CombinePathCommandHandler(_mockPathService);
}
#endregion
#region ===================================================================== METHODS ===================================================================================
[Fact]
public async Task Handle_WhenCalledWithValidCommand_ShouldReturnSuccessResult()
{
// Arrange
CombinePathCommand combinePathCommand = CombinePathCommandFixture.CreateCombinePathCommand();
string combinedPath = System.IO.Path.Combine(combinePathCommand.OriginalPath, combinePathCommand.NewPath);
_mockPathService.CombinePath(combinePathCommand.OriginalPath, combinePathCommand.NewPath)
.Returns(ErrorOrFactory.From(combinedPath));
// Act
ErrorOr<PathSegmentResponse> result = await _sut.Handle(combinePathCommand, CancellationToken.None);
// Assert
result.IsError.Should().BeFalse();
result.Value.Should().BeOfType<PathSegmentResponse>();
result.Value.Path.Should().Be(combinedPath);
_mockPathService.Received(1).CombinePath(combinePathCommand.OriginalPath, combinePathCommand.NewPath);
}
[Fact]
public async Task Handle_WhenPathServiceReturnsError_ShouldReturnFailureResult()
{
// Arrange
CombinePathCommand command = _fixture.Create<CombinePathCommand>();
Error error = Error.Failure("PathService.Error", "An error occurred");
_mockPathService.CombinePath(command.OriginalPath, command.NewPath)
.Returns(error);
// Act
ErrorOr<PathSegmentResponse> result = await _sut.Handle(command, CancellationToken.None);
// Assert
result.IsError.Should().BeTrue();
result.FirstError.Should().Be(error);
_mockPathService.Received(1).CombinePath(command.OriginalPath, command.NewPath);
}
[Fact]
public async Task Handle_WhenPathServiceReturnsEmptyString_ShouldReturnSuccessResultWithEmptyPath()
{
// Arrange
CombinePathCommand command = _fixture.Create<CombinePathCommand>();
_mockPathService.CombinePath(command.OriginalPath, command.NewPath)
.Returns(ErrorOrFactory.From(string.Empty));
// Act
ErrorOr<PathSegmentResponse> result = await _sut.Handle(command, CancellationToken.None);
// Assert
result.IsError.Should().BeFalse();
result.Value.Should().BeOfType<PathSegmentResponse>();
result.Value.Path.Should().BeEmpty();
_mockPathService.Received(1).CombinePath(command.OriginalPath, command.NewPath);
}
#endregion
}
@@ -0,0 +1,137 @@
#region ========================================================================= USING =====================================================================================
using FluentValidation.TestHelper;
using Lumina.Application.Core.FileManagement.Paths.Commands.CombinePath;
using Lumina.Application.UnitTests.Core.FileManagement.Pahs.Commands.CombinePath.Fixtures;
using Lumina.Domain.Common.Errors;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.UnitTests.Core.FileManagement.Pahs.Commands.CombinePath;
/// <summary>
/// Contains unit tests for the <see cref="CombinePathCommandValidator"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class CombinePathCommandValidatorTests
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly CombinePathCommandValidator _validator;
#endregion
#region ====================================================================== CTOR =====================================================================================
/// <summary>
/// Initializes a new instance of the <see cref="CombinePathCommandValidatorTests"/> class.
/// </summary>
public CombinePathCommandValidatorTests()
{
_validator = new CombinePathCommandValidator();
}
#endregion
#region ===================================================================== METHODS ===================================================================================
[Fact]
public void Validate_WhenOriginalPathIsNull_ShouldHaveValidationError()
{
// Arrange
CombinePathCommand command = CombinePathCommandFixture.CreateCombinePathCommand();
command = command with { OriginalPath = null! };
// Act
TestValidationResult<CombinePathCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.OriginalPath)
.WithErrorMessage(Errors.FileManagement.PathCannotBeEmpty.Code);
}
[Fact]
public void Validate_WhenOriginalPathIsEmpty_ShouldHaveValidationError()
{
// Arrange
CombinePathCommand command = CombinePathCommandFixture.CreateCombinePathCommand();
command = command with { OriginalPath = string.Empty };
// Act
TestValidationResult<CombinePathCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.OriginalPath)
.WithErrorMessage(Errors.FileManagement.PathCannotBeEmpty.Code);
}
[Fact]
public void Validate_WhenOriginalPathIsWhitespace_ShouldHaveValidationError()
{
// Arrange
CombinePathCommand command = CombinePathCommandFixture.CreateCombinePathCommand();
command = command with { OriginalPath = " " };
// Act
TestValidationResult<CombinePathCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.OriginalPath)
.WithErrorMessage(Errors.FileManagement.PathCannotBeEmpty.Code);
}
[Fact]
public void Validate_WhenNewPathIsNull_ShouldHaveValidationError()
{
// Arrange
CombinePathCommand command = CombinePathCommandFixture.CreateCombinePathCommand();
command = command with { NewPath = null! };
// Act
TestValidationResult<CombinePathCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.NewPath)
.WithErrorMessage(Errors.FileManagement.PathCannotBeEmpty.Code);
}
[Fact]
public void Validate_WhenNewPathIsEmpty_ShouldHaveValidationError()
{
// Arrange
CombinePathCommand command = CombinePathCommandFixture.CreateCombinePathCommand();
command = command with { NewPath = string.Empty };
// Act
TestValidationResult<CombinePathCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.NewPath)
.WithErrorMessage(Errors.FileManagement.PathCannotBeEmpty.Code);
}
[Fact]
public void Validate_WhenNewPathIsWhitespace_ShouldHaveValidationError()
{
// Arrange
CombinePathCommand command = CombinePathCommandFixture.CreateCombinePathCommand();
command = command with { NewPath = " " };
// Act
TestValidationResult<CombinePathCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.NewPath)
.WithErrorMessage(Errors.FileManagement.PathCannotBeEmpty.Code);
}
[Fact]
public void Validate_WhenBothPathsAreValid_ShouldNotHaveValidationError()
{
// Arrange
CombinePathCommand command = CombinePathCommandFixture.CreateCombinePathCommand();
command = command with { OriginalPath = "/valid/path", NewPath = "new/segment" };
// Act
TestValidationResult<CombinePathCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldNotHaveValidationErrorFor(x => x.OriginalPath);
result.ShouldNotHaveValidationErrorFor(x => x.NewPath);
}
#endregion
}
@@ -0,0 +1,31 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using Lumina.Application.Core.FileManagement.Paths.Commands.CombinePath;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.UnitTests.Core.FileManagement.Pahs.Commands.CombinePath.Fixtures;
/// <summary>
/// Fixture class for the <see cref="CombinePathCommand"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class CombinePathCommandFixture
{
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Creates a random valid command to combine paths.
/// </summary>
/// <returns>The created command.</returns>
public static CombinePathCommand CreateCombinePathCommand()
{
return new Faker<CombinePathCommand>()
.CustomInstantiator(f => new CombinePathCommand(
default!,
default!
))
.RuleFor(x => x.OriginalPath, f => f.System.FilePath())
.RuleFor(x => x.NewPath, f => f.System.FilePath());
}
#endregion
}
@@ -0,0 +1,29 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using Lumina.Application.Core.FileManagement.Paths.Commands.SplitPath;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.UnitTests.Core.FileManagement.Pahs.Commands.SplitPath.Fixtures;
/// <summary>
/// Fixture class for the <see cref="SplitPathCommand"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class SplitPathCommandFixture
{
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Creates a random valid command for splitting paths.
/// </summary>
/// <returns>The created command.</returns>
public static SplitPathCommand CreateSplitPathCommand()
{
return new Faker<SplitPathCommand>()
.CustomInstantiator(f => new SplitPathCommand(
default!
))
.RuleFor(x => x.Path, f => f.System.FilePath());
}
#endregion
}
@@ -0,0 +1,130 @@
#region ========================================================================= USING =====================================================================================
using AutoFixture;
using AutoFixture.AutoNSubstitute;
using ErrorOr;
using FluentAssertions;
using Lumina.Application.Common.Mapping.FileManagement;
using Lumina.Application.Core.FileManagement.Paths.Commands.SplitPath;
using Lumina.Application.UnitTests.Core.FileManagement.Pahs.Commands.SplitPath.Fixtures;
using Lumina.Application.UnitTests.Core.FileManagement.Pahs.Fixtures;
using Lumina.Contracts.Responses.FileManagement;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.Services;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.ValueObjects;
using Mapster;
using MapsterMapper;
using NSubstitute;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.UnitTests.Core.FileManagement.Pahs.Commands.SplitPath;
/// <summary>
/// Contains unit tests for the <see cref="SplitPathCommandHandler"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class SplitPathCommandHandlerTests
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly IFixture _fixture;
private readonly IMapper _mockMapper;
private readonly IPathService _mockPathService;
private readonly SplitPathCommandHandler _sut;
private readonly PathSegmentFixture _pathSegmentFixture;
private readonly PathSegmentResponseFixture _pathSegmentResponseFixture;
private readonly PathSegmentMappingConfig _pathSegmentMappingConfig;
private readonly TypeAdapterConfig _config;
#endregion
#region ====================================================================== CTOR =====================================================================================
/// <summary>
/// Initializes a new instance of the <see cref="SplitPathCommandHandlerTests"/> class.
/// </summary>
public SplitPathCommandHandlerTests()
{
_fixture = new Fixture().Customize(new AutoNSubstituteCustomization());
_mockPathService = Substitute.For<IPathService>();
_mockMapper = Substitute.For<IMapper>();
_sut = new SplitPathCommandHandler(_mockPathService, _mockMapper);
_pathSegmentFixture = new PathSegmentFixture();
_pathSegmentResponseFixture = new PathSegmentResponseFixture();
_config = new TypeAdapterConfig();
_pathSegmentMappingConfig = new PathSegmentMappingConfig();
_pathSegmentMappingConfig.Register(_config);
}
#endregion
#region ===================================================================== METHODS ===================================================================================
[Fact]
public async Task Handle_WhenCalledWithValidCommand_ShouldReturnSuccessResult()
{
// Arrange
SplitPathCommand splitPathCommand = SplitPathCommandFixture.CreateSplitPathCommand();
IEnumerable<PathSegment> pathSegments = _pathSegmentFixture.CreateMany();
IEnumerable<PathSegmentResponse> pathSegmentResponses = _pathSegmentResponseFixture.CreateMany(pathSegments.Count());
_mockPathService.ParsePath(splitPathCommand.Path)
.Returns(ErrorOrFactory.From(pathSegments));
_mockMapper.Map<IEnumerable<PathSegmentResponse>>(Arg.Any<IEnumerable<PathSegment>>())
.Returns(pathSegmentResponses);
// Act
ErrorOr<IEnumerable<PathSegmentResponse>> result = await _sut.Handle(splitPathCommand, CancellationToken.None);
// Assert
result.IsError.Should().BeFalse();
result.Value.Should().BeAssignableTo<IEnumerable<PathSegmentResponse>>();
result.Value.Should().BeEquivalentTo(pathSegmentResponses);
_mockPathService.Received(1).ParsePath(splitPathCommand.Path);
_mockMapper.Received(1).Map<IEnumerable<PathSegmentResponse>>(Arg.Any<IEnumerable<PathSegment>>());
}
[Fact]
public async Task Handle_WhenPathServiceReturnsError_ShouldReturnFailureResult()
{
// Arrange
SplitPathCommand command = _fixture.Create<SplitPathCommand>();
Error error = Error.Failure("PathService.Error", "An error occurred");
_mockPathService.ParsePath(command.Path)
.Returns(error);
// Act
ErrorOr<IEnumerable<PathSegmentResponse>> result = await _sut.Handle(command, CancellationToken.None);
// Assert
result.IsError.Should().BeTrue();
result.FirstError.Should().Be(error);
_mockPathService.Received(1).ParsePath(command.Path);
_mockMapper.DidNotReceive().Map<IEnumerable<PathSegmentResponse>>(Arg.Any<ErrorOr<IEnumerable<PathSegment>>>());
}
[Fact]
public async Task Handle_WhenPathServiceReturnsEmptyList_ShouldReturnEmptySuccessResult()
{
// Arrange
SplitPathCommand command = _fixture.Create<SplitPathCommand>();
ErrorOr<IEnumerable<PathSegment>> emptyList = ErrorOrFactory.From(Enumerable.Empty<PathSegment>());
_mockPathService.ParsePath(command.Path)
.Returns(emptyList);
_mockMapper.Map<IEnumerable<PathSegmentResponse>>(Arg.Any<IEnumerable<PathSegment>>())
.Returns([]);
// Act
ErrorOr<IEnumerable<PathSegmentResponse>> result = await _sut.Handle(command, CancellationToken.None);
// Assert
result.IsError.Should().BeFalse();
result.Value.Should().BeEmpty();
_mockPathService.Received(1).ParsePath(command.Path);
_mockMapper.Received(1).Map<IEnumerable<PathSegmentResponse>>(Arg.Any<IEnumerable<PathSegment>>());
}
#endregion
}
@@ -0,0 +1,91 @@
#region ========================================================================= USING =====================================================================================
using FluentValidation.TestHelper;
using Lumina.Application.Core.FileManagement.Paths.Commands.SplitPath;
using Lumina.Application.UnitTests.Core.FileManagement.Pahs.Commands.SplitPath.Fixtures;
using Lumina.Domain.Common.Errors;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.UnitTests.Core.FileManagement.Pahs.Commands.SplitPath;
/// <summary>
/// Contains unit tests for the <see cref="SplitPathCommandValidator"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class SplitPathCommandValidatorTests
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly SplitPathCommandValidator _validator;
#endregion
#region ====================================================================== CTOR =====================================================================================
/// <summary>
/// Initializes a new instance of the <see cref="SplitPathCommandValidatorTests"/> class.
/// </summary>
public SplitPathCommandValidatorTests()
{
_validator = new SplitPathCommandValidator();
}
#endregion
#region ===================================================================== METHODS ===================================================================================
[Fact]
public void Validate_WhenPathIsNull_ShouldHaveValidationError()
{
// Arrange
SplitPathCommand command = SplitPathCommandFixture.CreateSplitPathCommand();
command = command with { Path = null! };
// Act
TestValidationResult<SplitPathCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Path)
.WithErrorMessage(Errors.FileManagement.PathCannotBeEmpty.Code);
}
[Fact]
public void Validate_WhenPathIsEmpty_ShouldHaveValidationError()
{
// Arrange
SplitPathCommand command = SplitPathCommandFixture.CreateSplitPathCommand();
command = command with { Path = string.Empty };
// Act
TestValidationResult<SplitPathCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Path)
.WithErrorMessage(Errors.FileManagement.PathCannotBeEmpty.Code);
}
[Fact]
public void Validate_WhenPathIsWhitespace_ShouldHaveValidationError()
{
// Arrange
SplitPathCommand command = SplitPathCommandFixture.CreateSplitPathCommand();
command = command with { Path = " " };
// Act
TestValidationResult<SplitPathCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Path)
.WithErrorMessage(Errors.FileManagement.PathCannotBeEmpty.Code);
}
[Fact]
public void Validate_WhenPathIsValid_ShouldNotHaveValidationError()
{
// Arrange
SplitPathCommand command = SplitPathCommandFixture.CreateSplitPathCommand();
command = command with { Path = "/valid/path" };
// Act
TestValidationResult<SplitPathCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldNotHaveValidationErrorFor(x => x.Path);
}
#endregion
}
@@ -0,0 +1,64 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using ErrorOr;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.ValueObjects;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
#endregion
namespace Lumina.Application.UnitTests.Core.FileManagement.Pahs.Fixtures;
/// <summary>
/// Fixture class for the <see cref="PathSegment"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class PathSegmentFixture
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly Faker _faker;
#endregion
#region ====================================================================== CTOR =====================================================================================
/// <summary>
/// Initializes a new instance of the <see cref="PathSegmentFixture"/> class.
/// </summary>
public PathSegmentFixture()
{
_faker = new Faker();
}
#endregion
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Creates a random valid <see cref="PathSegment"/>.
/// </summary>
/// <returns>The created <see cref="PathSegment"/>.</returns>
public PathSegment CreatePathSegment(
string? name = null,
bool? isDirectory = null,
bool? isDrive = null)
{
name ??= _faker.System.FileName();
isDirectory ??= true;
isDrive ??= false;
ErrorOr<PathSegment> pathResult = PathSegment.Create(name, isDirectory.Value, isDrive.Value);
if (pathResult.IsError)
throw new InvalidOperationException("Failed to create PathSegment: " + string.Join(", ", pathResult.Errors));
return pathResult.Value;
}
/// <summary>
/// Creates a list of <see cref="PathSegment"/>.
/// </summary>
/// <param name="count">The number of elements to create.</param>
/// <returns>The created list.</returns>
public List<PathSegment> CreateMany(int count = 3)
{
return Enumerable.Range(0, count).Select(_ => CreatePathSegment()).ToList();
}
#endregion
}
@@ -0,0 +1,41 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using Lumina.Contracts.Responses.FileManagement;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
#endregion
namespace Lumina.Application.UnitTests.Core.FileManagement.Pahs.Fixtures;
/// <summary>
/// Fixture class for the <see cref="PathSegmentResponse"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class PathSegmentResponseFixture
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly Faker _faker = new();
#endregion
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Creates a random valid <see cref="PathSegmentResponse"/>.
/// </summary>
/// <returns>The created <see cref="PathSegmentResponse"/>.</returns>
public PathSegmentResponse Create()
{
return new PathSegmentResponse(_faker.System.DirectoryPath());
}
/// <summary>
/// Creates a list of <see cref="PathSegmentResponse"/>.
/// </summary>
/// <param name="count">The number of elements to create.</param>
/// <returns>The created list.</returns>
public List<PathSegmentResponse> CreateMany(int count = 3)
{
return Enumerable.Range(0, count).Select(_ => Create()).ToList();
}
#endregion
}
@@ -0,0 +1,41 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using Lumina.Contracts.Responses.FileManagement;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
#endregion
namespace Lumina.Application.UnitTests.Core.FileManagement.Pahs.Fixtures;
/// <summary>
/// Fixture class for the <see cref="PathSeparatorResponse"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class PathSeparatorResponseFixture
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly Faker _faker = new();
#endregion
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Creates a random valid <see cref="PathSeparatorResponse"/>.
/// </summary>
/// <returns>The created <see cref="PathSeparatorResponse"/>.</returns>
public PathSeparatorResponse Create()
{
return new PathSeparatorResponse(_faker.System.DirectoryPath());
}
/// <summary>
/// Creates a list of <see cref="PathSeparatorResponse"/>.
/// </summary>
/// <param name="count">The number of elements to create.</param>
/// <returns>The created list.</returns>
public List<PathSeparatorResponse> CreateMany(int count = 3)
{
return Enumerable.Range(0, count).Select(_ => Create()).ToList();
}
#endregion
}
@@ -0,0 +1,99 @@
#region ========================================================================= USING =====================================================================================
using FluentAssertions;
using Lumina.Application.Core.FileManagement.Paths.Queries.CheckPathExists;
using Lumina.Application.UnitTests.Core.FileManagement.Pahs.Queries.CheckPathExists.Fixtures;
using Lumina.Contracts.Responses.FileManagement;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.Services;
using NSubstitute;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.UnitTests.Core.FileManagement.Pahs.Queries.CheckPathExists;
/// <summary>
/// Contains unit tests for the <see cref="CheckPathExistsQueryHandler"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class CheckPathExistsQueryHandlerTests
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly IPathService _mockPathService;
private readonly CheckPathExistsQueryHandler _sut;
#endregion
#region ====================================================================== CTOR =====================================================================================
/// <summary>
/// Initializes a new instance of the <see cref="CheckPathExistsQueryHandlerTests"/> class.
/// </summary>
public CheckPathExistsQueryHandlerTests()
{
_mockPathService = Substitute.For<IPathService>();
_sut = new CheckPathExistsQueryHandler(_mockPathService);
}
#endregion
#region ===================================================================== METHODS ===================================================================================
[Fact]
public async Task Handle_WhenPathExists_ShouldReturnTrueResponse()
{
// Arrange
CheckPathExistsQuery query = CheckPathExistsQueryFixture.CreateCheckPathExistsQuery();
_mockPathService.Exists(query.Path).Returns(true);
// Act
PathExistsResponse result = await _sut.Handle(query, CancellationToken.None);
// Assert
result.Exists.Should().BeTrue();
_mockPathService.Received(1).Exists(query.Path);
}
[Fact]
public async Task Handle_WhenPathDoesNotExist_ShouldReturnFalseResponse()
{
// Arrange
CheckPathExistsQuery query = CheckPathExistsQueryFixture.CreateCheckPathExistsQuery();
_mockPathService.Exists(query.Path).Returns(false);
// Act
PathExistsResponse result = await _sut.Handle(query, CancellationToken.None);
// Assert
result.Exists.Should().BeFalse();
_mockPathService.Received(1).Exists(query.Path);
}
[Fact]
public async Task Handle_WhenCalledWithNullPath_ShouldStillCallPathService()
{
// Arrange
CheckPathExistsQuery query = new(null!);
_mockPathService.Exists(Arg.Any<string>()).Returns(false);
// Act
PathExistsResponse result = await _sut.Handle(query, CancellationToken.None);
// Assert
result.Exists.Should().BeFalse();
_mockPathService.Received(1).Exists(Arg.Any<string>());
}
[Fact]
public async Task Handle_WhenCancellationRequested_ShouldStillCompleteOperation()
{
// Arrange
CheckPathExistsQuery query = CheckPathExistsQueryFixture.CreateCheckPathExistsQuery();
_mockPathService.Exists(query.Path).Returns(true);
CancellationToken cancellationToken = new(true);
// Act
PathExistsResponse result = await _sut.Handle(query, cancellationToken);
// Assert
result.Exists.Should().BeTrue();
_mockPathService.Received(1).Exists(query.Path);
}
#endregion
}
@@ -0,0 +1,91 @@
#region ========================================================================= USING =====================================================================================
using FluentValidation.TestHelper;
using Lumina.Application.Core.FileManagement.Paths.Queries.CheckPathExists;
using Lumina.Application.UnitTests.Core.FileManagement.Pahs.Queries.CheckPathExists.Fixtures;
using Lumina.Domain.Common.Errors;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.UnitTests.Core.FileManagement.Pahs.Queries.CheckPathExists;
/// <summary>
/// Contains unit tests for the <see cref="CheckPathExistsQueryValidator"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class CheckPathExistsQueryValidatorTests
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly CheckPathExistsQueryValidator _validator;
#endregion
#region ====================================================================== CTOR =====================================================================================
/// <summary>
/// Initializes a new instance of the <see cref="CheckPathExistsQueryValidatorTests"/> class.
/// </summary>
public CheckPathExistsQueryValidatorTests()
{
_validator = new CheckPathExistsQueryValidator();
}
#endregion
#region ===================================================================== METHODS ===================================================================================
[Fact]
public void Validate_WhenPathIsNull_ShouldHaveValidationError()
{
// Arrange
CheckPathExistsQuery query = CheckPathExistsQueryFixture.CreateCheckPathExistsQuery();
query = query with { Path = null! };
// Act
TestValidationResult<CheckPathExistsQuery> result = _validator.TestValidate(query);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Path)
.WithErrorMessage(Errors.FileManagement.PathCannotBeEmpty.Code);
}
[Fact]
public void Validate_WhenPathIsEmpty_ShouldHaveValidationError()
{
// Arrange
CheckPathExistsQuery query = CheckPathExistsQueryFixture.CreateCheckPathExistsQuery();
query = query with { Path = string.Empty };
// Act
TestValidationResult<CheckPathExistsQuery> result = _validator.TestValidate(query);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Path)
.WithErrorMessage(Errors.FileManagement.PathCannotBeEmpty.Code);
}
[Fact]
public void Validate_WhenPathIsWhitespace_ShouldHaveValidationError()
{
// Arrange
CheckPathExistsQuery query = CheckPathExistsQueryFixture.CreateCheckPathExistsQuery();
query = query with { Path = " " };
// Act
TestValidationResult<CheckPathExistsQuery> result = _validator.TestValidate(query);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Path)
.WithErrorMessage(Errors.FileManagement.PathCannotBeEmpty.Code);
}
[Fact]
public void Validate_WhenPathIsValid_ShouldNotHaveValidationError()
{
// Arrange
CheckPathExistsQuery query = CheckPathExistsQueryFixture.CreateCheckPathExistsQuery();
query = query with { Path = "/valid/path" };
// Act
TestValidationResult<CheckPathExistsQuery> result = _validator.TestValidate(query);
// Assert
result.ShouldNotHaveValidationErrorFor(x => x.Path);
}
#endregion
}
@@ -0,0 +1,29 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using Lumina.Application.Core.FileManagement.Paths.Queries.CheckPathExists;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.UnitTests.Core.FileManagement.Pahs.Queries.CheckPathExists.Fixtures;
/// <summary>
/// Fixture class for the <see cref="CheckPathExistsQuery"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class CheckPathExistsQueryFixture
{
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Creates a random valid query to check if paths exist.
/// </summary>
/// <returns>The created query.</returns>
public static CheckPathExistsQuery CreateCheckPathExistsQuery()
{
return new Faker<CheckPathExistsQuery>()
.CustomInstantiator(f => new CheckPathExistsQuery(
default!
))
.RuleFor(x => x.Path, f => f.System.FilePath());
}
#endregion
}
@@ -0,0 +1,29 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using Lumina.Application.Core.FileManagement.Paths.Queries.GetPathParent;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.UnitTests.Core.FileManagement.Pahs.Queries.GetPathParent.Fixtures;
/// <summary>
/// Fixture class for the <see cref="GetPathParentQuery"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class GetPathParentQueryFixture
{
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Creates a random valid query to check if paths exist.
/// </summary>
/// <returns>The created query.</returns>
public static GetPathParentQuery CreateGetPathParentQuery()
{
return new Faker<GetPathParentQuery>()
.CustomInstantiator(f => new GetPathParentQuery(
default!
))
.RuleFor(x => x.Path, f => f.System.FilePath());
}
#endregion
}
@@ -0,0 +1,118 @@
#region ========================================================================= USING =====================================================================================
using AutoFixture;
using AutoFixture.AutoNSubstitute;
using ErrorOr;
using FluentAssertions;
using Lumina.Application.Core.FileManagement.Paths.Queries.GetPathParent;
using Lumina.Application.UnitTests.Core.FileManagement.Pahs.Fixtures;
using Lumina.Application.UnitTests.Core.FileManagement.Pahs.Queries.GetPathParent.Fixtures;
using Lumina.Contracts.Responses.FileManagement;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.Services;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.ValueObjects;
using MapsterMapper;
using NSubstitute;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.UnitTests.Core.FileManagement.Pahs.Queries.GetPathParent;
/// <summary>
/// Contains unit tests for the <see cref="GetPathParentQueryHandler"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class GetPathParentQueryHandlerTests
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly IFixture _fixture;
private readonly IMapper _mockMapper;
private readonly IPathService _mockPathService;
private readonly GetPathParentQueryHandler _sut;
private readonly PathSegmentFixture _pathSegmentFixture;
private readonly PathSegmentResponseFixture _pathSegmentResponseFixture;
#endregion
#region ====================================================================== CTOR =====================================================================================
public GetPathParentQueryHandlerTests()
{
_fixture = new Fixture().Customize(new AutoNSubstituteCustomization());
_mockPathService = Substitute.For<IPathService>();
_mockMapper = Substitute.For<IMapper>();
_sut = new GetPathParentQueryHandler(_mockPathService, _mockMapper);
_pathSegmentFixture = new PathSegmentFixture();
_pathSegmentResponseFixture = new PathSegmentResponseFixture();
}
#endregion
#region ===================================================================== METHODS ===================================================================================
[Fact]
public async Task Handle_WhenCalledWithValidQuery_ShouldReturnSuccessResult()
{
// Arrange
GetPathParentQuery query = GetPathParentQueryFixture.CreateGetPathParentQuery();
IEnumerable<PathSegment> pathSegments = _pathSegmentFixture.CreateMany();
IEnumerable<PathSegmentResponse> pathSegmentResponses = _pathSegmentResponseFixture.CreateMany(pathSegments.Count());
_mockPathService.GoUpOneLevel(query.Path)
.Returns(ErrorOrFactory.From(pathSegments));
_mockMapper.Map<IEnumerable<PathSegmentResponse>>(Arg.Is<IEnumerable<PathSegment>>(p => p == pathSegments))
.Returns(pathSegmentResponses);
// Act
ErrorOr<IEnumerable<PathSegmentResponse>> result = await _sut.Handle(query, CancellationToken.None);
// Assert
result.IsError.Should().BeFalse();
result.Value.Should().BeAssignableTo<IEnumerable<PathSegmentResponse>>();
result.Value.Should().BeEquivalentTo(pathSegmentResponses);
_mockPathService.Received(1).GoUpOneLevel(query.Path);
_mockMapper.Received(1).Map<IEnumerable<PathSegmentResponse>>(Arg.Is<IEnumerable<PathSegment>>(p => p == pathSegments));
}
[Fact]
public async Task Handle_WhenPathServiceReturnsError_ShouldReturnFailureResult()
{
// Arrange
GetPathParentQuery query = _fixture.Create<GetPathParentQuery>();
Error error = Error.Failure("PathService.Error", "An error occurred");
_mockPathService.GoUpOneLevel(query.Path)
.Returns(error);
// Act
ErrorOr<IEnumerable<PathSegmentResponse>> result = await _sut.Handle(query, CancellationToken.None);
// Assert
result.IsError.Should().BeTrue();
result.FirstError.Should().Be(error);
_mockPathService.Received(1).GoUpOneLevel(query.Path);
_mockMapper.DidNotReceive().Map<IEnumerable<PathSegmentResponse>>(Arg.Any<IEnumerable<PathSegment>>());
}
[Fact]
public async Task Handle_WhenPathServiceReturnsEmptyList_ShouldReturnEmptySuccessResult()
{
// Arrange
GetPathParentQuery query = _fixture.Create<GetPathParentQuery>();
ErrorOr<IEnumerable<PathSegment>> emptyList = ErrorOrFactory.From(Enumerable.Empty<PathSegment>());
_mockPathService.GoUpOneLevel(query.Path)
.Returns(emptyList);
_mockMapper.Map<IEnumerable<PathSegmentResponse>>(Arg.Is<IEnumerable<PathSegment>>(p => !p.Any()))
.Returns([]);
// Act
ErrorOr<IEnumerable<PathSegmentResponse>> result = await _sut.Handle(query, CancellationToken.None);
// Assert
result.IsError.Should().BeFalse();
result.Value.Should().BeEmpty();
_mockPathService.Received(1).GoUpOneLevel(query.Path);
_mockMapper.Received(1).Map<IEnumerable<PathSegmentResponse>>(Arg.Is<IEnumerable<PathSegment>>(p => !p.Any()));
}
#endregion
}
@@ -0,0 +1,91 @@
#region ========================================================================= USING =====================================================================================
using FluentValidation.TestHelper;
using Lumina.Application.Core.FileManagement.Paths.Queries.GetPathParent;
using Lumina.Application.UnitTests.Core.FileManagement.Pahs.Queries.GetPathParent.Fixtures;
using Lumina.Domain.Common.Errors;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.UnitTests.Core.FileManagement.Pahs.Queries.GetPathParent;
/// <summary>
/// Contains unit tests for the <see cref="GetPathParentQueryValidator"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class GetPathParentQueryValidatorTests
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly GetPathParentQueryValidator _validator;
#endregion
#region ====================================================================== CTOR =====================================================================================
/// <summary>
/// Initializes a new instance of the <see cref="GetPathParentQueryValidatorTests"/> class.
/// </summary>
public GetPathParentQueryValidatorTests()
{
_validator = new GetPathParentQueryValidator();
}
#endregion
#region ===================================================================== METHODS ===================================================================================
[Fact]
public void Validate_WhenPathIsNull_ShouldHaveValidationError()
{
// Arrange
GetPathParentQuery query = GetPathParentQueryFixture.CreateGetPathParentQuery();
query = query with { Path = null! };
// Act
TestValidationResult<GetPathParentQuery> result = _validator.TestValidate(query);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Path)
.WithErrorMessage(Errors.FileManagement.PathCannotBeEmpty.Code);
}
[Fact]
public void Validate_WhenPathIsEmpty_ShouldHaveValidationError()
{
// Arrange
GetPathParentQuery query = GetPathParentQueryFixture.CreateGetPathParentQuery();
query = query with { Path = string.Empty };
// Act
TestValidationResult<GetPathParentQuery> result = _validator.TestValidate(query);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Path)
.WithErrorMessage(Errors.FileManagement.PathCannotBeEmpty.Code);
}
[Fact]
public void Validate_WhenPathIsWhitespace_ShouldHaveValidationError()
{
// Arrange
GetPathParentQuery query = GetPathParentQueryFixture.CreateGetPathParentQuery();
query = query with { Path = " " };
// Act
TestValidationResult<GetPathParentQuery> result = _validator.TestValidate(query);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Path)
.WithErrorMessage(Errors.FileManagement.PathCannotBeEmpty.Code);
}
[Fact]
public void Validate_WhenPathIsValid_ShouldNotHaveValidationError()
{
// Arrange
GetPathParentQuery query = GetPathParentQueryFixture.CreateGetPathParentQuery();
query = query with { Path = "/valid/path" };
// Act
TestValidationResult<GetPathParentQuery> result = _validator.TestValidate(query);
// Assert
result.ShouldNotHaveValidationErrorFor(x => x.Path);
}
#endregion
}
@@ -0,0 +1,29 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using Lumina.Application.Core.FileManagement.Paths.Queries.GetPathRoot;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.UnitTests.Core.FileManagement.Pahs.Queries.GetPathRoot.Fixtures;
/// <summary>
/// Fixture class for the <see cref="GetPathRootQuery"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class GetPathRootQueryFixture
{
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Creates a random valid query to check if paths exist.
/// </summary>
/// <returns>The created query.</returns>
public static GetPathRootQuery CreateGetPathRootQuery()
{
return new Faker<GetPathRootQuery>()
.CustomInstantiator(f => new GetPathRootQuery(
default!
))
.RuleFor(x => x.Path, f => f.System.FilePath());
}
#endregion
}
@@ -0,0 +1,119 @@
#region ========================================================================= USING =====================================================================================
using AutoFixture;
using AutoFixture.AutoNSubstitute;
using ErrorOr;
using FluentAssertions;
using Lumina.Application.Core.FileManagement.Paths.Queries.GetPathRoot;
using Lumina.Application.UnitTests.Core.FileManagement.Pahs.Fixtures;
using Lumina.Application.UnitTests.Core.FileManagement.Pahs.Queries.GetPathRoot.Fixtures;
using Lumina.Contracts.Responses.FileManagement;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.Services;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.ValueObjects;
using MapsterMapper;
using NSubstitute;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.UnitTests.Core.FileManagement.Pahs.Queries.GetPathRoot;
/// <summary>
/// Contains unit tests for the <see cref="GetPathRootQueryHandler"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class GetPathRootQueryHandlerTests
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly IFixture _fixture;
private readonly IMapper _mockMapper;
private readonly IPathService _mockPathService;
private readonly GetPathRootQueryHandler _sut;
private readonly PathSegmentFixture _pathSegmentFixture;
private readonly PathSegmentResponseFixture _pathSegmentResponseFixture;
#endregion
#region ====================================================================== CTOR =====================================================================================
/// <summary>
/// Initializes a new instance of the <see cref="GetPathRootQueryHandlerTests"/> class.
/// </summary>
public GetPathRootQueryHandlerTests()
{
_fixture = new Fixture().Customize(new AutoNSubstituteCustomization());
_mockPathService = Substitute.For<IPathService>();
_mockMapper = Substitute.For<IMapper>();
_sut = new GetPathRootQueryHandler(_mockPathService, _mockMapper);
_pathSegmentFixture = new PathSegmentFixture();
_pathSegmentResponseFixture = new PathSegmentResponseFixture();
}
#endregion
#region ===================================================================== METHODS ===================================================================================
[Fact]
public async Task Handle_WhenCalledWithValidQuery_ShouldReturnSuccessResult()
{
// Arrange
GetPathRootQuery query = GetPathRootQueryFixture.CreateGetPathRootQuery();
PathSegment pathSegment = _pathSegmentFixture.CreatePathSegment(isDrive: true);
PathSegmentResponse pathSegmentResponse = _pathSegmentResponseFixture.Create();
_mockPathService.GetPathRoot(query.Path)
.Returns(ErrorOrFactory.From(pathSegment));
_mockMapper.Map<PathSegmentResponse>(Arg.Is<PathSegment>(p => p == pathSegment))
.Returns(pathSegmentResponse);
// Act
ErrorOr<PathSegmentResponse> result = await _sut.Handle(query, CancellationToken.None);
// Assert
result.IsError.Should().BeFalse();
result.Value.Should().BeEquivalentTo(pathSegmentResponse);
_mockPathService.Received(1).GetPathRoot(query.Path);
_mockMapper.Received(1).Map<PathSegmentResponse>(Arg.Is<PathSegment>(p => p == pathSegment));
}
[Fact]
public async Task Handle_WhenPathServiceReturnsError_ShouldReturnFailureResult()
{
// Arrange
GetPathRootQuery query = GetPathRootQueryFixture.CreateGetPathRootQuery();
Error error = Error.Failure("PathService.Error", "An error occurred");
_mockPathService.GetPathRoot(query.Path)
.Returns(error);
// Act
ErrorOr<PathSegmentResponse> result = await _sut.Handle(query, CancellationToken.None);
// Assert
result.IsError.Should().BeTrue();
result.FirstError.Should().Be(error);
_mockPathService.Received(1).GetPathRoot(query.Path);
_mockMapper.DidNotReceive().Map<PathSegmentResponse>(Arg.Any<PathSegment>());
}
[Fact]
public async Task Handle_WhenPathServiceReturnsRootPathSegment_ShouldReturnRootSuccessResult()
{
// Arrange
GetPathRootQuery query = GetPathRootQueryFixture.CreateGetPathRootQuery();
PathSegment rootPathSegment = _pathSegmentFixture.CreatePathSegment(name: "/", isDirectory: true, isDrive: true);
PathSegmentResponse rootResponse = _pathSegmentResponseFixture.Create();
_mockPathService.GetPathRoot(query.Path)
.Returns(ErrorOrFactory.From(rootPathSegment));
_mockMapper.Map<PathSegmentResponse>(Arg.Is<PathSegment>(p => p == rootPathSegment))
.Returns(rootResponse);
// Act
ErrorOr<PathSegmentResponse> result = await _sut.Handle(query, CancellationToken.None);
// Assert
result.IsError.Should().BeFalse();
result.Value.Should().BeEquivalentTo(rootResponse);
_mockPathService.Received(1).GetPathRoot(query.Path);
_mockMapper.Received(1).Map<PathSegmentResponse>(Arg.Is<PathSegment>(p => p == rootPathSegment));
}
#endregion
}
@@ -0,0 +1,91 @@
#region ========================================================================= USING =====================================================================================
using FluentValidation.TestHelper;
using Lumina.Application.Core.FileManagement.Paths.Queries.GetPathRoot;
using Lumina.Application.UnitTests.Core.FileManagement.Pahs.Queries.GetPathRoot.Fixtures;
using Lumina.Domain.Common.Errors;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.UnitTests.Core.FileManagement.Pahs.Queries.GetPathRoot;
/// <summary>
/// Contains unit tests for the <see cref="GetPathRootQueryValidator"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class GetPathRootQueryValidatorTests
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly GetPathRootQueryValidator _validator;
#endregion
#region ====================================================================== CTOR =====================================================================================
/// <summary>
/// Initializes a new instance of the <see cref="GetPathRootQueryValidatorTests"/> class.
/// </summary>
public GetPathRootQueryValidatorTests()
{
_validator = new GetPathRootQueryValidator();
}
#endregion
#region ===================================================================== METHODS ===================================================================================
[Fact]
public void Validate_WhenPathIsNull_ShouldHaveValidationError()
{
// Arrange
GetPathRootQuery query = GetPathRootQueryFixture.CreateGetPathRootQuery();
query = query with { Path = null! };
// Act
TestValidationResult<GetPathRootQuery> result = _validator.TestValidate(query);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Path)
.WithErrorMessage(Errors.FileManagement.PathCannotBeEmpty.Code);
}
[Fact]
public void Validate_WhenPathIsEmpty_ShouldHaveValidationError()
{
// Arrange
GetPathRootQuery query = GetPathRootQueryFixture.CreateGetPathRootQuery();
query = query with { Path = string.Empty };
// Act
TestValidationResult<GetPathRootQuery> result = _validator.TestValidate(query);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Path)
.WithErrorMessage(Errors.FileManagement.PathCannotBeEmpty.Code);
}
[Fact]
public void Validate_WhenPathIsWhitespace_ShouldHaveValidationError()
{
// Arrange
GetPathRootQuery query = GetPathRootQueryFixture.CreateGetPathRootQuery();
query = query with { Path = " " };
// Act
TestValidationResult<GetPathRootQuery> result = _validator.TestValidate(query);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Path)
.WithErrorMessage(Errors.FileManagement.PathCannotBeEmpty.Code);
}
[Fact]
public void Validate_WhenPathIsValid_ShouldNotHaveValidationError()
{
// Arrange
GetPathRootQuery query = GetPathRootQueryFixture.CreateGetPathRootQuery();
query = query with { Path = "/valid/path" };
// Act
TestValidationResult<GetPathRootQuery> result = _validator.TestValidate(query);
// Assert
result.ShouldNotHaveValidationErrorFor(x => x.Path);
}
#endregion
}
@@ -0,0 +1,105 @@
#region ========================================================================= USING =====================================================================================
using FluentAssertions;
using Lumina.Application.Core.FileManagement.Paths.Queries.GetPathSeparator;
using Lumina.Contracts.Responses.FileManagement;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.Services;
using NSubstitute;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.UnitTests.Core.FileManagement.Pahs.Queries.GetPathSeparator;
/// <summary>
/// Contains unit tests for the <see cref="GetPathSeparatorQueryHandler"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class GetPathSeparatorQueryHandlerTests
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly IPathService _mockPathService;
private readonly GetPathSeparatorQueryHandler _sut;
#endregion
#region ====================================================================== CTOR =====================================================================================
/// <summary>
/// Initializes a new instance of the <see cref="GetPathSeparatorQueryHandlerTests"/> class.
/// </summary>
public GetPathSeparatorQueryHandlerTests()
{
_mockPathService = Substitute.For<IPathService>();
_sut = new GetPathSeparatorQueryHandler(_mockPathService);
}
#endregion
#region ===================================================================== METHODS ===================================================================================
[Fact]
public async Task Handle_WhenCalled_ShouldReturnPathSeparatorResponse()
{
// Arrange
GetPathSeparatorQuery query = new();
char expectedSeparator = System.IO.Path.DirectorySeparatorChar;
_mockPathService.PathSeparator.Returns(expectedSeparator);
// Act
PathSeparatorResponse result = await _sut.Handle(query, CancellationToken.None);
// Assert
result.Should().NotBeNull();
result.Separator.Should().Be(expectedSeparator.ToString());
_mockPathService.Received(1);
}
[Theory]
[InlineData('/')]
[InlineData('\\')]
public async Task Handle_WithDifferentSeparators_ShouldReturnCorrectSeparator(char separator)
{
// Arrange
GetPathSeparatorQuery query = new();
_mockPathService.PathSeparator.Returns(separator);
// Act
PathSeparatorResponse result = await _sut.Handle(query, CancellationToken.None);
// Assert
result.Should().NotBeNull();
result.Separator.Should().Be(separator.ToString());
_mockPathService.Received(1);
}
[Fact]
public async Task Handle_WhenCalledMultipleTimes_ShouldReturnSameResult()
{
// Arrange
GetPathSeparatorQuery query = new();
char expectedSeparator = System.IO.Path.DirectorySeparatorChar;
_mockPathService.PathSeparator.Returns(expectedSeparator);
// Act
PathSeparatorResponse result1 = await _sut.Handle(query, CancellationToken.None);
PathSeparatorResponse result2 = await _sut.Handle(query, CancellationToken.None);
// Assert
result1.Should().BeEquivalentTo(result2);
_mockPathService.Received(2);
}
[Fact]
public async Task Handle_WithCancellationToken_ShouldCompleteSuccessfully()
{
// Arrange
GetPathSeparatorQuery query = new();
using CancellationTokenSource cts = new();
// Act
Func<Task> act = async () => await _sut.Handle(query, cts.Token);
// Assert
await act.Should().NotThrowAsync();
}
#endregion
}
@@ -0,0 +1,29 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using Lumina.Application.Core.FileManagement.Paths.Queries.ValidatePath;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.UnitTests.Core.FileManagement.Pahs.Queries.ValidatePath.Fixtures;
/// <summary>
/// Fixture class for the <see cref="ValidatePathQuery"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class ValidatePathQueryFixure
{
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Creates a random valid query to check if paths exist.
/// </summary>
/// <returns>The created query.</returns>
public static ValidatePathQuery CreateValidatePathQuery()
{
return new Faker<ValidatePathQuery>()
.CustomInstantiator(f => new ValidatePathQuery(
default!
))
.RuleFor(x => x.Path, f => f.System.FilePath());
}
#endregion
}
@@ -0,0 +1,98 @@
#region ========================================================================= USING =====================================================================================
using FluentAssertions;
using Lumina.Application.Core.FileManagement.Paths.Queries.ValidatePath;
using Lumina.Application.UnitTests.Core.FileManagement.Pahs.Queries.ValidatePath.Fixtures;
using Lumina.Contracts.Responses.FileManagement;
using Lumina.Domain.Core.Aggregates.FileManagement.FileManagementAggregate.Services;
using NSubstitute;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.UnitTests.Core.FileManagement.Pahs.Queries.ValidatePath;
/// <summary>
/// Contains unit tests for the <see cref="ValidatePathQueryHandler"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class ValidatePathQueryHandlerTests
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly IPathService _mockPathService;
private readonly ValidatePathQueryHandler _sut;
#endregion
#region ====================================================================== CTOR =====================================================================================
/// <summary>
/// Initializes a new instance of the <see cref="ValidatePathQueryHandlerTests"/> class.
/// </summary>
public ValidatePathQueryHandlerTests()
{
_mockPathService = Substitute.For<IPathService>();
_sut = new ValidatePathQueryHandler(_mockPathService);
}
#endregion
#region ===================================================================== METHODS ===================================================================================
[Fact]
public async Task Handle_WhenCalledWithValidPath_ShouldReturnTrueResponse()
{
// Arrange
ValidatePathQuery query = ValidatePathQueryFixure.CreateValidatePathQuery();
_mockPathService.IsValidPath(query.Path).Returns(true);
// Act
PathValidResponse result = await _sut.Handle(query, CancellationToken.None);
// Assert
result.IsValid.Should().BeTrue();
_mockPathService.Received(1).IsValidPath(query.Path);
}
[Fact]
public async Task Handle_WhenCalledWithInvalidPath_ShouldReturnFalseResponse()
{
// Arrange
ValidatePathQuery query = ValidatePathQueryFixure.CreateValidatePathQuery();
_mockPathService.IsValidPath(query.Path).Returns(false);
// Act
PathValidResponse result = await _sut.Handle(query, CancellationToken.None);
// Assert
result.IsValid.Should().BeFalse();
_mockPathService.Received(1).IsValidPath(query.Path);
}
[Fact]
public async Task Handle_WhenCalledWithNullPath_ShouldReturnFalseResponse()
{
// Arrange
ValidatePathQuery query = new(null!);
_mockPathService.IsValidPath(Arg.Any<string>()).Returns(false);
// Act
PathValidResponse result = await _sut.Handle(query, CancellationToken.None);
// Assert
result.IsValid.Should().BeFalse();
_mockPathService.Received(1).IsValidPath(Arg.Any<string>());
}
[Fact]
public async Task Handle_WhenCalledWithEmptyPath_ShouldReturnFalseResponse()
{
// Arrange
ValidatePathQuery query = new(string.Empty);
_mockPathService.IsValidPath(Arg.Any<string>()).Returns(false);
// Act
PathValidResponse result = await _sut.Handle(query, CancellationToken.None);
// Assert
result.IsValid.Should().BeFalse();
_mockPathService.Received(1).IsValidPath(Arg.Any<string>());
}
#endregion
}
@@ -0,0 +1,91 @@
#region ========================================================================= USING =====================================================================================
using FluentValidation.TestHelper;
using Lumina.Application.Core.FileManagement.Paths.Queries.ValidatePath;
using Lumina.Application.UnitTests.Core.FileManagement.Pahs.Queries.ValidatePath.Fixtures;
using Lumina.Domain.Common.Errors;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.UnitTests.Core.FileManagement.Pahs.Queries.ValidatePath;
/// <summary>
/// Contains unit tests for the <see cref="ValidatePathQueryValidator"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class ValidatePathQueryValidatorTests
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly ValidatePathQueryValidator _validator;
#endregion
#region ====================================================================== CTOR =====================================================================================
/// <summary>
/// Initializes a new instance of the <see cref="ValidatePathQueryValidatorTests"/> class.
/// </summary>
public ValidatePathQueryValidatorTests()
{
_validator = new ValidatePathQueryValidator();
}
#endregion
#region ===================================================================== METHODS ===================================================================================
[Fact]
public void Validate_WhenPathIsNull_ShouldHaveValidationError()
{
// Arrange
ValidatePathQuery query = ValidatePathQueryFixure.CreateValidatePathQuery();
query = query with { Path = null! };
// Act
TestValidationResult<ValidatePathQuery> result = _validator.TestValidate(query);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Path)
.WithErrorMessage(Errors.FileManagement.PathCannotBeEmpty.Code);
}
[Fact]
public void Validate_WhenPathIsEmpty_ShouldHaveValidationError()
{
// Arrange
ValidatePathQuery query = ValidatePathQueryFixure.CreateValidatePathQuery();
query = query with { Path = string.Empty };
// Act
TestValidationResult<ValidatePathQuery> result = _validator.TestValidate(query);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Path)
.WithErrorMessage(Errors.FileManagement.PathCannotBeEmpty.Code);
}
[Fact]
public void Validate_WhenPathIsWhitespace_ShouldHaveValidationError()
{
// Arrange
ValidatePathQuery query = ValidatePathQueryFixure.CreateValidatePathQuery();
query = query with { Path = " " };
// Act
TestValidationResult<ValidatePathQuery> result = _validator.TestValidate(query);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Path)
.WithErrorMessage(Errors.FileManagement.PathCannotBeEmpty.Code);
}
[Fact]
public void Validate_WhenPathIsValid_ShouldNotHaveValidationError()
{
// Arrange
ValidatePathQuery query = ValidatePathQueryFixure.CreateValidatePathQuery();
query = query with { Path = "/valid/path" };
// Act
TestValidationResult<ValidatePathQuery> result = _validator.TestValidate(query);
// Assert
result.ShouldNotHaveValidationErrorFor(x => x.Path);
}
#endregion
}