Centralizes and standardizes test fixtures.

This commit is contained in:
BinaryAlley committed 2026-08-15 02:58:17 -07:00
1 parent dfc513da4f
commit b648e2f054
294 files changed
+3807 -4437

No files matched your search

@@ -0,0 +1,98 @@
#region ========================================================================= USING =====================================================================================
using AutoFixture;
using AutoFixture.AutoNSubstitute;
using Lumina.Application.Common.Infrastructure.Validation;
using Lumina.Application.Core.FileSystemManagement.Paths.Commands.CombinePath;
using Lumina.Application.Fixtures.Core.FileSystemManagement.Paths.Commands.CombinePath;
using Lumina.Contracts.Responses.FileSystemManagement.Path;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Services;
using NSubstitute;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.UnitTests.Core.FileSystemManagement.Paths.Commands.CombinePath;
/// <summary>
/// Contains unit tests for the <see cref="CombinePathCommandHandler"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class CombinePathCommandHandlerTests
{
private readonly IFixture _fixture;
private readonly IPathService _mockPathService;
private readonly CombinePathCommandHandler _sut;
private readonly CombinePathCommandFixture _combinePathCommandFixture;
/// <summary>
/// Initializes a new instance of the <see cref="CombinePathCommandHandlerTests"/> class.
/// </summary>
public CombinePathCommandHandlerTests()
{
_fixture = new Fixture().Customize(new AutoNSubstituteCustomization());
_mockPathService = Substitute.For<IPathService>();
IValidator<CombinePathCommand> mockValidator = Substitute.For<IValidator<CombinePathCommand>>();
mockValidator.Validate(Arg.Any<CombinePathCommand>())
.Returns([]);
_sut = new CombinePathCommandHandler(_mockPathService, mockValidator);
_combinePathCommandFixture = new CombinePathCommandFixture();
}
[Fact]
public async Task HandleAsync_WhenCalledWithValidCommand_ShouldReturnSuccessResult()
{
// Arrange
CombinePathCommand combinePathCommand = _combinePathCommandFixture.Create();
string combinedPath = System.IO.Path.Combine(combinePathCommand.OriginalPath!, combinePathCommand.NewPath!);
_mockPathService.CombinePath(combinePathCommand.OriginalPath!, combinePathCommand.NewPath!)
.Returns(Result.From(combinedPath));
// Act
Result<PathSegmentResponse> result = await _sut.HandleAsync(combinePathCommand, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.IsType<PathSegmentResponse>(result.Value);
Assert.Equal(combinedPath, result.Value.Path);
_mockPathService.Received(1).CombinePath(combinePathCommand.OriginalPath!, combinePathCommand.NewPath!);
}
[Fact]
public async Task HandleAsync_WhenPathServiceReturnsError_ShouldReturnFailureResult()
{
// Arrange
CombinePathCommand command = _combinePathCommandFixture.Create();
Error error = Error.Failure("PathService.Error", "An error occurred");
_mockPathService.CombinePath(command.OriginalPath!, command.NewPath!)
.Returns(error);
// Act
Result<PathSegmentResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(error, result.FirstError);
_mockPathService.Received(1).CombinePath(command.OriginalPath!, command.NewPath!);
}
[Fact]
public async Task HandleAsync_WhenPathServiceReturnsEmptyString_ShouldReturnSuccessResultWithEmptyPath()
{
// Arrange
CombinePathCommand command = _combinePathCommandFixture.Create();
_mockPathService.CombinePath(command.OriginalPath!, command.NewPath!)
.Returns(Result.From(string.Empty));
// Act
Result<PathSegmentResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.IsType<PathSegmentResponse>(result.Value);
Assert.Empty(result.Value.Path);
_mockPathService.Received(1).CombinePath(command.OriginalPath!, command.NewPath!);
}
}
@@ -0,0 +1,129 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.FileSystemManagement.Paths.Commands.CombinePath;
using Lumina.Application.Fixtures.Core.FileSystemManagement.Paths.Commands.CombinePath;
using Lumina.Application.UnitTests.Common.Setup;
using Lumina.Domain.Common.Errors;
using Lumina.Domain.Common.Primitives;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.UnitTests.Core.FileSystemManagement.Paths.Commands.CombinePath;
/// <summary>
/// Contains unit tests for the <see cref="CombinePathCommandValidator"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class CombinePathCommandValidatorTests
{
private readonly CombinePathCommandValidator _validator;
private readonly CombinePathCommandFixture _combinePathCommandFixture;
/// <summary>
/// Initializes a new instance of the <see cref="CombinePathCommandValidatorTests"/> class.
/// </summary>
public CombinePathCommandValidatorTests()
{
_validator = new CombinePathCommandValidator();
_combinePathCommandFixture = new CombinePathCommandFixture();
}
[Fact]
public void Validate_WhenOriginalPathIsNull_ShouldHaveValidationError()
{
// Arrange
CombinePathCommand command = _combinePathCommandFixture.Create();
command = command with { OriginalPath = null! };
// Act
List<Error> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
}
[Fact]
public void Validate_WhenOriginalPathIsEmpty_ShouldHaveValidationError()
{
// Arrange
CombinePathCommand command = _combinePathCommandFixture.Create();
command = command with { OriginalPath = string.Empty };
// Act
List<Error> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
}
[Fact]
public void Validate_WhenOriginalPathIsWhitespace_ShouldHaveValidationError()
{
// Arrange
CombinePathCommand command = _combinePathCommandFixture.Create();
command = command with { OriginalPath = " " };
// Act
List<Error> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
}
[Fact]
public void Validate_WhenNewPathIsNull_ShouldHaveValidationError()
{
// Arrange
CombinePathCommand command = _combinePathCommandFixture.Create();
command = command with { NewPath = null! };
// Act
List<Error> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
}
[Fact]
public void Validate_WhenNewPathIsEmpty_ShouldHaveValidationError()
{
// Arrange
CombinePathCommand command = _combinePathCommandFixture.Create();
command = command with { NewPath = string.Empty };
// Act
List<Error> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
}
[Fact]
public void Validate_WhenNewPathIsWhitespace_ShouldHaveValidationError()
{
// Arrange
CombinePathCommand command = _combinePathCommandFixture.Create();
command = command with { NewPath = " " };
// Act
List<Error> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
}
[Fact]
public void Validate_WhenBothPathsAreValid_ShouldNotHaveValidationError()
{
// Arrange
CombinePathCommand command = _combinePathCommandFixture.Create();
command = command with { OriginalPath = "/valid/path", NewPath = "new/segment" };
// Act
List<Error> result = _validator.TestValidate(command);
// Assert
result.ShouldNotHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
result.ShouldNotHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
}
}
@@ -0,0 +1,112 @@
#region ========================================================================= USING =====================================================================================
using AutoFixture;
using AutoFixture.AutoNSubstitute;
using Lumina.Application.Common.Infrastructure.Validation;
using Lumina.Application.Core.FileSystemManagement.Paths.Commands.SplitPath;
using Lumina.Application.Fixtures.Core.FileSystemManagement.Paths.Commands.SplitPath;
using Lumina.Contracts.Responses.FileSystemManagement.Path;
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 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.FileSystemManagement.Paths.Commands.SplitPath;
/// <summary>
/// Contains unit tests for the <see cref="SplitPathCommandHandler"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class SplitPathCommandHandlerTests
{
private readonly IFixture _fixture;
private readonly IPathService _mockPathService;
private readonly SplitPathCommandHandler _sut;
private readonly PathSegmentFixture _pathSegmentFixture;
private readonly SplitPathCommandFixture _splitPathCommandFixture;
/// <summary>
/// Initializes a new instance of the <see cref="SplitPathCommandHandlerTests"/> class.
/// </summary>
public SplitPathCommandHandlerTests()
{
_fixture = new Fixture().Customize(new AutoNSubstituteCustomization());
_mockPathService = Substitute.For<IPathService>();
IValidator<SplitPathCommand> mockValidator = Substitute.For<IValidator<SplitPathCommand>>();
mockValidator.Validate(Arg.Any<SplitPathCommand>())
.Returns([]);
_sut = new SplitPathCommandHandler(_mockPathService, mockValidator);
_pathSegmentFixture = new PathSegmentFixture();
_splitPathCommandFixture = new SplitPathCommandFixture();
}
[Fact]
public async Task HandleAsync_WhenCalledWithValidCommand_ShouldReturnSuccessResult()
{
// Arrange
SplitPathCommand splitPathCommand = _splitPathCommandFixture.Create();
IEnumerable<PathSegment> pathSegments = _pathSegmentFixture.CreateMany();
_mockPathService.ParsePath(splitPathCommand.Path!)
.Returns(Result.From(pathSegments));
// Act
Result<IEnumerable<PathSegmentResponse>> result = await _sut.HandleAsync(splitPathCommand, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.IsAssignableFrom<IEnumerable<PathSegmentResponse>>(result.Value);
Assert.Equal(pathSegments.Count(), result.Value.Count());
List<PathSegmentResponse> resultList = [.. result.Value];
List<PathSegment> segmentsList = [.. pathSegments];
for (int i = 0; i < resultList.Count; i++)
Assert.Equal(segmentsList[i].Name, resultList[i].Path);
_mockPathService.Received(1).ParsePath(splitPathCommand.Path!);
}
[Fact]
public async Task HandleAsync_WhenPathServiceReturnsError_ShouldReturnFailureResult()
{
// Arrange
SplitPathCommand command = _splitPathCommandFixture.Create();
Error error = Error.Failure("PathService.Error", "An error occurred");
_mockPathService.ParsePath(command.Path!)
.Returns(error);
// Act
Result<IEnumerable<PathSegmentResponse>> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(error, result.FirstError);
_mockPathService.Received(1).ParsePath(command.Path!);
}
[Fact]
public async Task HandleAsync_WhenPathServiceReturnsEmptyList_ShouldReturnEmptySuccessResult()
{
// Arrange
SplitPathCommand command = _splitPathCommandFixture.Create();
Result<IEnumerable<PathSegment>> emptyList = Result.From(Enumerable.Empty<PathSegment>());
_mockPathService.ParsePath(command.Path!)
.Returns(emptyList);
// Act
Result<IEnumerable<PathSegmentResponse>> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.Empty(result.Value);
_mockPathService.Received(1).ParsePath(command.Path!);
}
}
@@ -0,0 +1,86 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.FileSystemManagement.Paths.Commands.SplitPath;
using Lumina.Application.Fixtures.Core.FileSystemManagement.Paths.Commands.SplitPath;
using Lumina.Application.UnitTests.Common.Setup;
using Lumina.Domain.Common.Errors;
using Lumina.Domain.Common.Primitives;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.UnitTests.Core.FileSystemManagement.Paths.Commands.SplitPath;
/// <summary>
/// Contains unit tests for the <see cref="SplitPathCommandValidator"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class SplitPathCommandValidatorTests
{
private readonly SplitPathCommandValidator _validator;
private readonly SplitPathCommandFixture _splitPathCommandFixture;
/// <summary>
/// Initializes a new instance of the <see cref="SplitPathCommandValidatorTests"/> class.
/// </summary>
public SplitPathCommandValidatorTests()
{
_validator = new SplitPathCommandValidator();
_splitPathCommandFixture = new SplitPathCommandFixture();
}
[Fact]
public void Validate_WhenPathIsNull_ShouldHaveValidationError()
{
// Arrange
SplitPathCommand command = _splitPathCommandFixture.Create();
command = command with { Path = null! };
// Act
List<Error> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
}
[Fact]
public void Validate_WhenPathIsEmpty_ShouldHaveValidationError()
{
// Arrange
SplitPathCommand command = _splitPathCommandFixture.Create();
command = command with { Path = string.Empty };
// Act
List<Error> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
}
[Fact]
public void Validate_WhenPathIsWhitespace_ShouldHaveValidationError()
{
// Arrange
SplitPathCommand command = _splitPathCommandFixture.Create();
command = command with { Path = " " };
// Act
List<Error> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
}
[Fact]
public void Validate_WhenPathIsValid_ShouldNotHaveValidationError()
{
// Arrange
SplitPathCommand command = _splitPathCommandFixture.Create();
command = command with { Path = "/valid/path" };
// Act
List<Error> result = _validator.TestValidate(command);
// Assert
result.ShouldNotHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
}
}
@@ -0,0 +1,135 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.Infrastructure.Validation;
using Lumina.Application.Core.FileSystemManagement.Paths.Queries.CheckPathExists;
using Lumina.Application.Fixtures.Core.FileSystemManagement.Paths.Queries.CheckPathExists;
using Lumina.Contracts.Responses.FileSystemManagement.Path;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Services;
using NSubstitute;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.UnitTests.Core.FileSystemManagement.Paths.Queries.CheckPathExists;
/// <summary>
/// Contains unit tests for the <see cref="CheckPathExistsQueryHandler"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class CheckPathExistsQueryHandlerTests
{
private readonly IPathService _mockPathService;
private readonly CheckPathExistsQueryHandler _sut;
private readonly CheckPathExistsQueryFixture _checkPathExistsQueryFixture;
/// <summary>
/// Initializes a new instance of the <see cref="CheckPathExistsQueryHandlerTests"/> class.
/// </summary>
public CheckPathExistsQueryHandlerTests()
{
_mockPathService = Substitute.For<IPathService>();
_checkPathExistsQueryFixture = new CheckPathExistsQueryFixture();
IValidator<CheckPathExistsQuery> mockValidator = Substitute.For<IValidator<CheckPathExistsQuery>>();
mockValidator.Validate(Arg.Any<CheckPathExistsQuery>())
.Returns([]);
_sut = new CheckPathExistsQueryHandler(_mockPathService, mockValidator);
}
[Fact]
public async Task HandleAsync_WhenPathExistsAndIsNotHidden_ShouldReturnTrueResponse()
{
// Arrange
CheckPathExistsQuery query = _checkPathExistsQueryFixture.Create();
_mockPathService.Exists(query.Path!).Returns(true);
// Act
Result<PathExistsResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.True(result.Value.Exists);
_mockPathService.Received(1).Exists(query.Path!);
}
[Fact]
public async Task HandleAsync_WhenPathExistsAndIsHiddenAndIncludeHiddenElementsIsTrue_ShouldReturnTrueResponse()
{
// Arrange
CheckPathExistsQuery query = _checkPathExistsQueryFixture.Create(includeHiddenElements: true);
_mockPathService.Exists(query.Path!).Returns(true);
// Act
Result<PathExistsResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.True(result.Value.Exists);
_mockPathService.Received(1).Exists(query.Path!);
}
[Fact]
public async Task HandleAsync_WhenPathExistsAndIsHiddenAndIncludeHiddenElementsIsFalse_ShouldReturnFalseResponse()
{
// Arrange
CheckPathExistsQuery query = _checkPathExistsQueryFixture.Create(includeHiddenElements: false);
_mockPathService.Exists(query.Path!, false).Returns(false);
// Act
Result<PathExistsResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.False(result.Value.Exists);
_mockPathService.Received(1).Exists(query.Path!, false);
}
[Fact]
public async Task HandleAsync_WhenPathDoesNotExist_ShouldReturnFalseResponse()
{
// Arrange
CheckPathExistsQuery query = _checkPathExistsQueryFixture.Create();
_mockPathService.Exists(query.Path!).Returns(false);
// Act
Result<PathExistsResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.False(result.Value.Exists);
_mockPathService.Received(1).Exists(query.Path!);
}
[Fact]
public async Task HandleAsync_WhenCalledWithNullPath_ShouldStillCallPathService()
{
// Arrange
CheckPathExistsQuery query = new(null!, false);
_mockPathService.Exists(Arg.Any<string>(), Arg.Any<bool>()).Returns(false);
// Act
Result<PathExistsResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.False(result.Value.Exists);
_mockPathService.Received(1).Exists(Arg.Any<string>(), Arg.Any<bool>());
}
[Fact]
public async Task HandleAsync_WhenCancellationRequested_ShouldStillCompleteOperation()
{
// Arrange
CheckPathExistsQuery query = _checkPathExistsQueryFixture.Create();
_mockPathService.Exists(query.Path!).Returns(true);
CancellationToken cancellationToken = new(true);
// Act
Result<PathExistsResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.True(result.Value.Exists);
_mockPathService.Received(1).Exists(query.Path!);
}
}
@@ -0,0 +1,86 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.FileSystemManagement.Paths.Queries.CheckPathExists;
using Lumina.Application.Fixtures.Core.FileSystemManagement.Paths.Queries.CheckPathExists;
using Lumina.Application.UnitTests.Common.Setup;
using Lumina.Domain.Common.Errors;
using Lumina.Domain.Common.Primitives;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.UnitTests.Core.FileSystemManagement.Paths.Queries.CheckPathExists;
/// <summary>
/// Contains unit tests for the <see cref="CheckPathExistsQueryValidator"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class CheckPathExistsQueryValidatorTests
{
private readonly CheckPathExistsQueryValidator _validator;
private readonly CheckPathExistsQueryFixture _checkPathExistsQueryFixture;
/// <summary>
/// Initializes a new instance of the <see cref="CheckPathExistsQueryValidatorTests"/> class.
/// </summary>
public CheckPathExistsQueryValidatorTests()
{
_validator = new CheckPathExistsQueryValidator();
_checkPathExistsQueryFixture = new CheckPathExistsQueryFixture();
}
[Fact]
public void Validate_WhenPathIsNull_ShouldHaveValidationError()
{
// Arrange
CheckPathExistsQuery query = _checkPathExistsQueryFixture.Create();
query = query with { Path = null! };
// Act
List<Error> result = _validator.TestValidate(query);
// Assert
result.ShouldHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
}
[Fact]
public void Validate_WhenPathIsEmpty_ShouldHaveValidationError()
{
// Arrange
CheckPathExistsQuery query = _checkPathExistsQueryFixture.Create();
query = query with { Path = string.Empty };
// Act
List<Error> result = _validator.TestValidate(query);
// Assert
result.ShouldHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
}
[Fact]
public void Validate_WhenPathIsWhitespace_ShouldHaveValidationError()
{
// Arrange
CheckPathExistsQuery query = _checkPathExistsQueryFixture.Create();
query = query with { Path = " " };
// Act
List<Error> result = _validator.TestValidate(query);
// Assert
result.ShouldHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
}
[Fact]
public void Validate_WhenPathIsValid_ShouldNotHaveValidationError()
{
// Arrange
CheckPathExistsQuery query = _checkPathExistsQueryFixture.Create();
query = query with { Path = "/valid/path" };
// Act
List<Error> result = _validator.TestValidate(query);
// Assert
result.ShouldNotHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
}
}
@@ -0,0 +1,105 @@
#region ========================================================================= USING =====================================================================================
using AutoFixture;
using AutoFixture.AutoNSubstitute;
using Lumina.Application.Common.Infrastructure.Validation;
using Lumina.Application.Core.FileSystemManagement.Paths.Queries.GetPathParent;
using Lumina.Application.Fixtures.Core.FileSystemManagement.Paths.Queries.GetPathParent;
using Lumina.Contracts.Responses.FileSystemManagement.Path;
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 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.FileSystemManagement.Paths.Queries.GetPathParent;
/// <summary>
/// Contains unit tests for the <see cref="GetPathParentQueryHandler"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class GetPathParentQueryHandlerTests
{
private readonly IFixture _fixture;
private readonly IPathService _mockPathService;
private readonly GetPathParentQueryHandler _sut;
private readonly PathSegmentFixture _pathSegmentFixture;
private readonly GetPathParentQueryFixture _getPathParentQueryFixture;
public GetPathParentQueryHandlerTests()
{
_fixture = new Fixture().Customize(new AutoNSubstituteCustomization());
_mockPathService = Substitute.For<IPathService>();
IValidator<GetPathParentQuery> mockValidator = Substitute.For<IValidator<GetPathParentQuery>>();
mockValidator.Validate(Arg.Any<GetPathParentQuery>())
.Returns([]);
_sut = new GetPathParentQueryHandler(_mockPathService, mockValidator);
_pathSegmentFixture = new PathSegmentFixture();
_getPathParentQueryFixture = new GetPathParentQueryFixture();
}
[Fact]
public async Task HandleAsync_WhenCalledWithValidQuery_ShouldReturnSuccessResult()
{
// Arrange
GetPathParentQuery query = _getPathParentQueryFixture.Create();
IEnumerable<PathSegment> pathSegments = _pathSegmentFixture.CreateMany();
// Create PathSegmentResponses based on the PathSegments
IEnumerable<PathSegmentResponse> pathSegmentResponses = pathSegments.Select(segment => new PathSegmentResponse(segment.Name));
_mockPathService.GoUpOneLevel(query.Path!)
.Returns(Result.From(pathSegments));
// Act
Result<IEnumerable<PathSegmentResponse>> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.IsAssignableFrom<IEnumerable<PathSegmentResponse>>(result.Value);
Assert.Equal(pathSegmentResponses, result.Value);
_mockPathService.Received(1).GoUpOneLevel(query.Path!);
}
[Fact]
public async Task HandleAsync_WhenPathServiceReturnsError_ShouldReturnFailureResult()
{
// Arrange
GetPathParentQuery query = _getPathParentQueryFixture.Create();
Error error = Error.Failure("PathService.Error", "An error occurred");
_mockPathService.GoUpOneLevel(query.Path!)
.Returns(error);
// Act
Result<IEnumerable<PathSegmentResponse>> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(error, result.FirstError);
_mockPathService.Received(1).GoUpOneLevel(query.Path!);
}
[Fact]
public async Task HandleAsync_WhenPathServiceReturnsEmptyList_ShouldReturnEmptySuccessResult()
{
// Arrange
GetPathParentQuery query = _getPathParentQueryFixture.Create();
Result<IEnumerable<PathSegment>> emptyList = Result.From(Enumerable.Empty<PathSegment>());
_mockPathService.GoUpOneLevel(query.Path!)
.Returns(emptyList);
// Act
Result<IEnumerable<PathSegmentResponse>> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.Empty(result.Value);
_mockPathService.Received(1).GoUpOneLevel(query.Path!);
}
}
@@ -0,0 +1,86 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.FileSystemManagement.Paths.Queries.GetPathParent;
using Lumina.Application.Fixtures.Core.FileSystemManagement.Paths.Queries.GetPathParent;
using Lumina.Application.UnitTests.Common.Setup;
using Lumina.Domain.Common.Errors;
using Lumina.Domain.Common.Primitives;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.UnitTests.Core.FileSystemManagement.Paths.Queries.GetPathParent;
/// <summary>
/// Contains unit tests for the <see cref="GetPathParentQueryValidator"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class GetPathParentQueryValidatorTests
{
private readonly GetPathParentQueryValidator _validator;
private readonly GetPathParentQueryFixture _getPathParentQueryFixture;
/// <summary>
/// Initializes a new instance of the <see cref="GetPathParentQueryValidatorTests"/> class.
/// </summary>
public GetPathParentQueryValidatorTests()
{
_validator = new GetPathParentQueryValidator();
_getPathParentQueryFixture = new GetPathParentQueryFixture();
}
[Fact]
public void Validate_WhenPathIsNull_ShouldHaveValidationError()
{
// Arrange
GetPathParentQuery query = _getPathParentQueryFixture.Create();
query = query with { Path = null! };
// Act
List<Error> result = _validator.TestValidate(query);
// Assert
result.ShouldHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
}
[Fact]
public void Validate_WhenPathIsEmpty_ShouldHaveValidationError()
{
// Arrange
GetPathParentQuery query = _getPathParentQueryFixture.Create();
query = query with { Path = string.Empty };
// Act
List<Error> result = _validator.TestValidate(query);
// Assert
result.ShouldHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
}
[Fact]
public void Validate_WhenPathIsWhitespace_ShouldHaveValidationError()
{
// Arrange
GetPathParentQuery query = _getPathParentQueryFixture.Create();
query = query with { Path = " " };
// Act
List<Error> result = _validator.TestValidate(query);
// Assert
result.ShouldHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
}
[Fact]
public void Validate_WhenPathIsValid_ShouldNotHaveValidationError()
{
// Arrange
GetPathParentQuery query = _getPathParentQueryFixture.Create();
query = query with { Path = "/valid/path" };
// Act
List<Error> result = _validator.TestValidate(query);
// Assert
result.ShouldNotHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
}
}
@@ -0,0 +1,100 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.Infrastructure.Validation;
using Lumina.Application.Core.FileSystemManagement.Paths.Queries.GetPathRoot;
using Lumina.Application.Fixtures.Core.FileSystemManagement.Paths.Queries.GetPathRoot;
using Lumina.Contracts.Responses.FileSystemManagement.Path;
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 NSubstitute;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.UnitTests.Core.FileSystemManagement.Paths.Queries.GetPathRoot;
/// <summary>
/// Contains unit tests for the <see cref="GetPathRootQueryHandler"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class GetPathRootQueryHandlerTests
{
private readonly IPathService _mockPathService;
private readonly GetPathRootQueryHandler _sut;
private readonly PathSegmentFixture _pathSegmentFixture;
private readonly GetPathRootQueryFixture _getPathRootQueryFixture;
/// <summary>
/// Initializes a new instance of the <see cref="GetPathRootQueryHandlerTests"/> class.
/// </summary>
public GetPathRootQueryHandlerTests()
{
_mockPathService = Substitute.For<IPathService>();
_getPathRootQueryFixture = new GetPathRootQueryFixture();
IValidator<GetPathRootQuery> mockValidator = Substitute.For<IValidator<GetPathRootQuery>>();
mockValidator.Validate(Arg.Any<GetPathRootQuery>())
.Returns([]);
_sut = new GetPathRootQueryHandler(_mockPathService, mockValidator);
_pathSegmentFixture = new PathSegmentFixture();
}
[Fact]
public async Task HandleAsync_WhenCalledWithValidQuery_ShouldReturnSuccessResult()
{
// Arrange
GetPathRootQuery query = _getPathRootQueryFixture.Create();
PathSegment pathSegment = _pathSegmentFixture.Create(isDrive: true);
_mockPathService.GetPathRoot(query.Path!)
.Returns(Result.From(pathSegment));
// Act
Result<PathSegmentResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.NotNull(result.Value);
Assert.Equal(pathSegment.Name, result.Value.Path);
_mockPathService.Received(1).GetPathRoot(query.Path!);
}
[Fact]
public async Task HandleAsync_WhenPathServiceReturnsError_ShouldReturnFailureResult()
{
// Arrange
GetPathRootQuery query = _getPathRootQueryFixture.Create();
Error error = Error.Failure("PathService.Error", "An error occurred");
_mockPathService.GetPathRoot(query.Path!)
.Returns(error);
// Act
Result<PathSegmentResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(error, result.FirstError);
_mockPathService.Received(1).GetPathRoot(query.Path!);
}
[Fact]
public async Task HandleAsync_WhenPathServiceReturnsRootPathSegment_ShouldReturnRootSuccessResult()
{
// Arrange
GetPathRootQuery query = _getPathRootQueryFixture.Create();
PathSegment rootPathSegment = _pathSegmentFixture.Create(name: "/", isDirectory: true, isDrive: true);
_mockPathService.GetPathRoot(query.Path!)
.Returns(Result.From(rootPathSegment));
// Act
Result<PathSegmentResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.NotNull(result.Value);
Assert.Equal("/", result.Value.Path);
_mockPathService.Received(1).GetPathRoot(query.Path!);
}
}
@@ -0,0 +1,86 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.FileSystemManagement.Paths.Queries.GetPathRoot;
using Lumina.Application.Fixtures.Core.FileSystemManagement.Paths.Queries.GetPathRoot;
using Lumina.Application.UnitTests.Common.Setup;
using Lumina.Domain.Common.Errors;
using Lumina.Domain.Common.Primitives;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.UnitTests.Core.FileSystemManagement.Paths.Queries.GetPathRoot;
/// <summary>
/// Contains unit tests for the <see cref="GetPathRootQueryValidator"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class GetPathRootQueryValidatorTests
{
private readonly GetPathRootQueryValidator _validator;
private readonly GetPathRootQueryFixture _getPathRootQueryFixture;
/// <summary>
/// Initializes a new instance of the <see cref="GetPathRootQueryValidatorTests"/> class.
/// </summary>
public GetPathRootQueryValidatorTests()
{
_validator = new GetPathRootQueryValidator();
_getPathRootQueryFixture = new GetPathRootQueryFixture();
}
[Fact]
public void Validate_WhenPathIsNull_ShouldHaveValidationError()
{
// Arrange
GetPathRootQuery query = _getPathRootQueryFixture.Create();
query = query with { Path = null! };
// Act
List<Error> result = _validator.TestValidate(query);
// Assert
result.ShouldHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
}
[Fact]
public void Validate_WhenPathIsEmpty_ShouldHaveValidationError()
{
// Arrange
GetPathRootQuery query = _getPathRootQueryFixture.Create();
query = query with { Path = string.Empty };
// Act
List<Error> result = _validator.TestValidate(query);
// Assert
result.ShouldHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
}
[Fact]
public void Validate_WhenPathIsWhitespace_ShouldHaveValidationError()
{
// Arrange
GetPathRootQuery query = _getPathRootQueryFixture.Create();
query = query with { Path = " " };
// Act
List<Error> result = _validator.TestValidate(query);
// Assert
result.ShouldHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
}
[Fact]
public void Validate_WhenPathIsValid_ShouldNotHaveValidationError()
{
// Arrange
GetPathRootQuery query = _getPathRootQueryFixture.Create();
query = query with { Path = "/valid/path" };
// Act
List<Error> result = _validator.TestValidate(query);
// Assert
result.ShouldNotHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
}
}
@@ -0,0 +1,100 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.FileSystemManagement.Paths.Queries.GetPathSeparator;
using Lumina.Contracts.Responses.FileSystemManagement.Path;
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Services;
using NSubstitute;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.UnitTests.Core.FileSystemManagement.Paths.Queries.GetPathSeparator;
/// <summary>
/// Contains unit tests for the <see cref="GetPathSeparatorQueryHandler"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class GetPathSeparatorQueryHandlerTests
{
private readonly IPathService _mockPathService;
private readonly GetPathSeparatorQueryHandler _sut;
/// <summary>
/// Initializes a new instance of the <see cref="GetPathSeparatorQueryHandlerTests"/> class.
/// </summary>
public GetPathSeparatorQueryHandlerTests()
{
_mockPathService = Substitute.For<IPathService>();
_sut = new GetPathSeparatorQueryHandler(_mockPathService);
}
[Fact]
public async Task HandleAsync_WhenCalled_ShouldReturnPathSeparatorResponse()
{
// Arrange
GetPathSeparatorQuery query = new();
char expectedSeparator = System.IO.Path.DirectorySeparatorChar;
_mockPathService.PathSeparator.Returns(expectedSeparator);
// Act
PathSeparatorResponse result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.NotNull(result);
Assert.Equal(expectedSeparator.ToString(), result.Separator);
_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.HandleAsync(query, CancellationToken.None);
// Assert
Assert.NotNull(result);
Assert.Equal(separator.ToString(), result.Separator);
_mockPathService.Received(1);
}
[Fact]
public async Task HandleAsync_WhenCalledMultipleTimes_ShouldReturnSameResult()
{
// Arrange
GetPathSeparatorQuery query = new();
char expectedSeparator = System.IO.Path.DirectorySeparatorChar;
_mockPathService.PathSeparator.Returns(expectedSeparator);
// Act
PathSeparatorResponse result1 = await _sut.HandleAsync(query, CancellationToken.None);
PathSeparatorResponse result2 = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.Equal(result1, result2);
_mockPathService.Received(2);
}
[Fact]
public async Task Handle_WithCancellationToken_ShouldCompleteSuccessfully()
{
// Arrange
GetPathSeparatorQuery query = new();
using CancellationTokenSource cts = new();
// Act
Exception? exception = await Record.ExceptionAsync(
async () => await _sut.HandleAsync(query, cts.Token)
);
// Assert
Assert.Null(exception);
}
}
@@ -0,0 +1,121 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.Infrastructure.Validation;
using Lumina.Application.Core.FileSystemManagement.Paths.Queries.ValidatePath;
using Lumina.Application.Fixtures.Core.FileSystemManagement.Paths.Queries.ValidatePath;
using Lumina.Contracts.Responses.FileSystemManagement.Path;
using Lumina.Domain.Common.Errors;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.Core.BoundedContexts.FileSystemManagementBoundedContext.FileSystemManagementAggregate.Services;
using NSubstitute;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.UnitTests.Core.FileSystemManagement.Paths.Queries.ValidatePath;
/// <summary>
/// Contains unit tests for the <see cref="ValidatePathQueryHandler"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class ValidatePathQueryHandlerTests
{
private readonly IPathService _mockPathService;
private readonly IValidator<ValidatePathQuery> _mockValidator;
private readonly ValidatePathQueryHandler _sut;
private readonly ValidatePathQueryFixture _validatePathQueryFixture;
/// <summary>
/// Initializes a new instance of the <see cref="ValidatePathQueryHandlerTests"/> class.
/// </summary>
public ValidatePathQueryHandlerTests()
{
_mockPathService = Substitute.For<IPathService>();
_mockValidator = Substitute.For<IValidator<ValidatePathQuery>>();
_validatePathQueryFixture = new ValidatePathQueryFixture();
_mockValidator.Validate(Arg.Any<ValidatePathQuery>())
.Returns([]);
_sut = new ValidatePathQueryHandler(_mockPathService, _mockValidator);
}
[Fact]
public async Task HandleAsync_WhenCalledWithValidPath_ShouldReturnTrueResponse()
{
// Arrange
ValidatePathQuery query = _validatePathQueryFixture.Create();
_mockPathService.IsValidPath(query.Path!).Returns(true);
// Act
Result<PathValidResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.True(result.Value.IsValid);
_mockPathService.Received(1).IsValidPath(query.Path!);
}
[Fact]
public async Task HandleAsync_WhenCalledWithInvalidPath_ShouldReturnFalseResponse()
{
// Arrange
ValidatePathQuery query = _validatePathQueryFixture.Create();
_mockPathService.IsValidPath(query.Path!).Returns(false);
// Act
Result<PathValidResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.False(result.Value.IsValid);
_mockPathService.Received(1).IsValidPath(query.Path!);
}
[Fact]
public async Task HandleAsync_WhenCalledWithNullPath_ShouldReturnFalseResponse()
{
// Arrange
ValidatePathQuery query = new(null!);
_mockPathService.IsValidPath(Arg.Any<string>()).Returns(false);
// Act
Result<PathValidResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.False(result.Value.IsValid);
_mockPathService.Received(1).IsValidPath(Arg.Any<string>());
}
[Fact]
public async Task HandleAsync_WhenCalledWithEmptyPath_ShouldReturnFalseResponse()
{
// Arrange
ValidatePathQuery query = new(string.Empty);
_mockPathService.IsValidPath(Arg.Any<string>()).Returns(false);
// Act
Result<PathValidResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.False(result.IsFailure);
Assert.False(result.Value.IsValid);
_mockPathService.Received(1).IsValidPath(Arg.Any<string>());
}
[Fact]
public async Task HandleAsync_WhenValidatorReturnsErrors_ShouldReturnValidationErrorsWithoutCallingPathService()
{
// Arrange
ValidatePathQuery query = _validatePathQueryFixture.Create();
_mockValidator.Validate(query)
.Returns([Errors.FileSystemManagement.PathCannotBeEmpty]);
// Act
Result<PathValidResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.FileSystemManagement.PathCannotBeEmpty, result.FirstError);
_mockPathService.DidNotReceive().IsValidPath(Arg.Any<string>());
}
}
@@ -0,0 +1,86 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.FileSystemManagement.Paths.Queries.ValidatePath;
using Lumina.Application.Fixtures.Core.FileSystemManagement.Paths.Queries.ValidatePath;
using Lumina.Application.UnitTests.Common.Setup;
using Lumina.Domain.Common.Errors;
using Lumina.Domain.Common.Primitives;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.UnitTests.Core.FileSystemManagement.Paths.Queries.ValidatePath;
/// <summary>
/// Contains unit tests for the <see cref="ValidatePathQueryValidator"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class ValidatePathQueryValidatorTests
{
private readonly ValidatePathQueryValidator _validator;
private readonly ValidatePathQueryFixture _validatePathQueryFixture;
/// <summary>
/// Initializes a new instance of the <see cref="ValidatePathQueryValidatorTests"/> class.
/// </summary>
public ValidatePathQueryValidatorTests()
{
_validator = new ValidatePathQueryValidator();
_validatePathQueryFixture = new ValidatePathQueryFixture();
}
[Fact]
public void Validate_WhenPathIsNull_ShouldHaveValidationError()
{
// Arrange
ValidatePathQuery query = _validatePathQueryFixture.Create();
query = query with { Path = null! };
// Act
List<Error> result = _validator.TestValidate(query);
// Assert
result.ShouldHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
}
[Fact]
public void Validate_WhenPathIsEmpty_ShouldHaveValidationError()
{
// Arrange
ValidatePathQuery query = _validatePathQueryFixture.Create();
query = query with { Path = string.Empty };
// Act
List<Error> result = _validator.TestValidate(query);
// Assert
result.ShouldHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
}
[Fact]
public void Validate_WhenPathIsWhitespace_ShouldHaveValidationError()
{
// Arrange
ValidatePathQuery query = _validatePathQueryFixture.Create();
query = query with { Path = " " };
// Act
List<Error> result = _validator.TestValidate(query);
// Assert
result.ShouldHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
}
[Fact]
public void Validate_WhenPathIsValid_ShouldNotHaveValidationError()
{
// Arrange
ValidatePathQuery query = _validatePathQueryFixture.Create();
query = query with { Path = "/valid/path" };
// Act
List<Error> result = _validator.TestValidate(query);
// Assert
result.ShouldNotHaveValidationError(Errors.FileSystemManagement.PathCannotBeEmpty);
}
}