Returns the unit of work save changes result

This commit is contained in:
BinaryAlley committed 2026-09-26 12:59:17 +03:00
1 parent f3e465255e
commit 64ef3a1e22
141 files changed
+1388 -1151

No files matched your search

@@ -2,6 +2,8 @@
using Lumina.Application.Common.DataAccess.Entities.Authorization;
using Lumina.Application.Common.DataAccess.Repositories.Authorization;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Common.Errors;
using Lumina.Application.Common.Infrastructure.Authentication;
using Lumina.Application.Common.Infrastructure.Authorization;
@@ -46,6 +48,7 @@ public class GetPermissionsQueryHandlerTests
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
_mockPermissionRepository = Substitute.For<IPermissionRepository>();
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_userId = Guid.NewGuid();
_mockCurrentUserService.UserId.Returns(_userId);
@@ -62,7 +65,7 @@ public class GetPermissionsQueryHandlerTests
{
// Arrange
GetPermissionsQuery query = _getPermissionsQueryFixture.Create();
IEnumerable<PermissionEntity> permissions =
List<PermissionEntity> permissions =
[
_permissionEntityFixture.Create(permissionName: AuthorizationPermission.CanViewUsers),
_permissionEntityFixture.Create(permissionName: AuthorizationPermission.CanDeleteUsers)
@@ -70,8 +73,8 @@ public class GetPermissionsQueryHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockPermissionRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From(permissions));
_mockPermissionRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<PermissionEntity> { Data = permissions, CurrentPage = 1, PerPage = permissions.Count, Count = permissions.Count, NumberOfPages = 1 }));
// Act
Result<IEnumerable<PermissionResponse>> result = await _sut.HandleAsync(query, CancellationToken.None);
@@ -80,7 +83,7 @@ public class GetPermissionsQueryHandlerTests
Assert.False(result.IsFailure);
Assert.Equal(2, result.Value.Count());
await _mockAuthorizationService.Received(1).IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>());
await _mockPermissionRepository.Received(1).GetAllAsync(Arg.Any<CancellationToken>());
await _mockPermissionRepository.Received(1).GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -99,7 +102,7 @@ public class GetPermissionsQueryHandlerTests
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authorization.NotAuthorized, result.FirstError);
await _mockAuthorizationService.Received(1).IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>());
await _mockPermissionRepository.DidNotReceive().GetAllAsync(Arg.Any<CancellationToken>());
await _mockPermissionRepository.DidNotReceive().GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -116,7 +119,7 @@ public class GetPermissionsQueryHandlerTests
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authorization.NotAuthorized, result.FirstError);
await _mockAuthorizationService.DidNotReceive().IsInRoleAsync(Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
await _mockPermissionRepository.DidNotReceive().GetAllAsync(Arg.Any<CancellationToken>());
await _mockPermissionRepository.DidNotReceive().GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -128,7 +131,7 @@ public class GetPermissionsQueryHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockPermissionRepository.GetAllAsync(Arg.Any<CancellationToken>())
_mockPermissionRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(error);
// Act
@@ -138,7 +141,7 @@ public class GetPermissionsQueryHandlerTests
Assert.True(result.IsFailure);
Assert.Equal(error, result.FirstError);
await _mockAuthorizationService.Received(1).IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>());
await _mockPermissionRepository.Received(1).GetAllAsync(Arg.Any<CancellationToken>());
await _mockPermissionRepository.Received(1).GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -146,7 +149,7 @@ public class GetPermissionsQueryHandlerTests
{
// Arrange
GetPermissionsQuery query = _getPermissionsQueryFixture.Create();
IEnumerable<PermissionEntity> permissions =
List<PermissionEntity> permissions =
[
_permissionEntityFixture.Create(permissionName: AuthorizationPermission.CanViewUsers)
];
@@ -154,8 +157,8 @@ public class GetPermissionsQueryHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockPermissionRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From(permissions));
_mockPermissionRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<PermissionEntity> { Data = permissions, CurrentPage = 1, PerPage = permissions.Count, Count = permissions.Count, NumberOfPages = 1 }));
// Act
Result<IEnumerable<PermissionResponse>> result = await _sut.HandleAsync(query, cancellationToken);
@@ -164,6 +167,6 @@ public class GetPermissionsQueryHandlerTests
Assert.False(result.IsFailure);
Assert.Single(result.Value);
await _mockAuthorizationService.Received(1).IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>());
await _mockPermissionRepository.Received(1).GetAllAsync(Arg.Any<CancellationToken>());
await _mockPermissionRepository.Received(1).GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
}
}
@@ -49,6 +49,7 @@ public class AddRoleCommandHandlerTests
mockValidator.Validate(Arg.Any<AddRoleCommand>())
.Returns([]);
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
_mockRoleRepository = Substitute.For<IRoleRepository>();
@@ -43,6 +43,7 @@ public class DeleteRoleCommandHandlerTests
mockValidator.Validate(Arg.Any<DeleteRoleCommand>())
.Returns([]);
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
_mockRoleRepository = Substitute.For<IRoleRepository>();
@@ -89,7 +90,7 @@ public class DeleteRoleCommandHandlerTests
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authorization.NotAuthorized, result.FirstError);
await _mockAuthorizationService.DidNotReceive().IsInRoleAsync(Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
await _mockRoleRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockRoleRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -100,7 +101,7 @@ public class DeleteRoleCommandHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockRoleRepository.GetByIdAsync(command.RoleId, Arg.Any<CancellationToken>())
_mockRoleRepository.GetByIdAsync(command.RoleId, cancellationToken: Arg.Any<CancellationToken>())
.Returns((RoleEntity?)null);
// Act
@@ -121,7 +122,7 @@ public class DeleteRoleCommandHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockRoleRepository.GetByIdAsync(command.RoleId, Arg.Any<CancellationToken>())
_mockRoleRepository.GetByIdAsync(command.RoleId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(adminRole);
// Act
@@ -142,7 +143,7 @@ public class DeleteRoleCommandHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockRoleRepository.GetByIdAsync(command.RoleId, Arg.Any<CancellationToken>())
_mockRoleRepository.GetByIdAsync(command.RoleId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(error);
// Act
@@ -164,7 +165,7 @@ public class DeleteRoleCommandHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockRoleRepository.GetByIdAsync(command.RoleId, Arg.Any<CancellationToken>())
_mockRoleRepository.GetByIdAsync(command.RoleId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(role);
_mockRoleRepository.DeleteByIdAsync(command.RoleId, Arg.Any<CancellationToken>())
.Returns(error);
@@ -187,7 +188,7 @@ public class DeleteRoleCommandHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockRoleRepository.GetByIdAsync(command.RoleId, Arg.Any<CancellationToken>())
_mockRoleRepository.GetByIdAsync(command.RoleId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(role);
_mockRoleRepository.DeleteByIdAsync(command.RoleId, Arg.Any<CancellationToken>())
.Returns(Result.Deleted);
@@ -49,6 +49,7 @@ public class UpdateRoleCommandHandlerTests
mockValidator.Validate(Arg.Any<UpdateRoleCommand>())
.Returns([]);
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
_mockRoleRepository = Substitute.For<IRoleRepository>();
@@ -130,7 +131,7 @@ public class UpdateRoleCommandHandlerTests
.Returns(true);
_mockRoleRepository.UpdateAsync(Arg.Any<RoleEntity>(), Arg.Any<CancellationToken>())
.Returns(Result.Updated);
_mockRoleRepository.GetByIdAsync(command.RoleId, Arg.Any<CancellationToken>())
_mockRoleRepository.GetByIdAsync(command.RoleId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(error);
// Act
@@ -151,7 +152,7 @@ public class UpdateRoleCommandHandlerTests
.Returns(true);
_mockRoleRepository.UpdateAsync(Arg.Any<RoleEntity>(), Arg.Any<CancellationToken>())
.Returns(Result.Updated);
_mockRoleRepository.GetByIdAsync(command.RoleId, Arg.Any<CancellationToken>())
_mockRoleRepository.GetByIdAsync(command.RoleId, cancellationToken: Arg.Any<CancellationToken>())
.Returns((RoleEntity?)null);
// Act
@@ -178,7 +179,7 @@ public class UpdateRoleCommandHandlerTests
.Returns(true);
_mockRoleRepository.UpdateAsync(Arg.Any<RoleEntity>(), Arg.Any<CancellationToken>())
.Returns(Result.Updated);
_mockRoleRepository.GetByIdAsync(command.RoleId, Arg.Any<CancellationToken>())
_mockRoleRepository.GetByIdAsync(command.RoleId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(role);
// Act
@@ -47,6 +47,7 @@ public class GetRolePermissionsQueryHandlerTests
mockValidator.Validate(Arg.Any<GetRolePermissionsQuery>())
.Returns([]);
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
_mockRoleRepository = Substitute.For<IRoleRepository>();
@@ -76,7 +77,7 @@ public class GetRolePermissionsQueryHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authorization.NotAuthorized, result.FirstError);
await _mockRoleRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockRoleRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -93,7 +94,7 @@ public class GetRolePermissionsQueryHandlerTests
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authorization.NotAuthorized, result.FirstError);
await _mockAuthorizationService.DidNotReceive().IsInRoleAsync(Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
await _mockRoleRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockRoleRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -105,7 +106,7 @@ public class GetRolePermissionsQueryHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockRoleRepository.GetByIdAsync(query.RoleId, Arg.Any<CancellationToken>())
_mockRoleRepository.GetByIdAsync(query.RoleId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(error);
// Act
@@ -124,7 +125,7 @@ public class GetRolePermissionsQueryHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockRoleRepository.GetByIdAsync(query.RoleId, Arg.Any<CancellationToken>())
_mockRoleRepository.GetByIdAsync(query.RoleId, cancellationToken: Arg.Any<CancellationToken>())
.Returns((RoleEntity?)null);
// Act
@@ -153,7 +154,7 @@ public class GetRolePermissionsQueryHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockRoleRepository.GetByIdAsync(query.RoleId, Arg.Any<CancellationToken>())
_mockRoleRepository.GetByIdAsync(query.RoleId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(role);
// Act
@@ -2,6 +2,8 @@
using Lumina.Application.Common.DataAccess.Entities.Authorization;
using Lumina.Application.Common.DataAccess.Repositories.Authorization;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Common.Errors;
using Lumina.Application.Common.Infrastructure.Authentication;
using Lumina.Application.Common.Infrastructure.Authorization;
@@ -41,6 +43,7 @@ public class GetRolesQueryHandlerTests
public GetRolesQueryHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
_mockRoleRepository = Substitute.For<IRoleRepository>();
@@ -69,7 +72,7 @@ public class GetRolesQueryHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authorization.NotAuthorized, result.FirstError);
await _mockRoleRepository.DidNotReceive().GetAllAsync(Arg.Any<CancellationToken>());
await _mockRoleRepository.DidNotReceive().GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -86,7 +89,7 @@ public class GetRolesQueryHandlerTests
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authorization.NotAuthorized, result.FirstError);
await _mockAuthorizationService.DidNotReceive().IsInRoleAsync(Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
await _mockRoleRepository.DidNotReceive().GetAllAsync(Arg.Any<CancellationToken>());
await _mockRoleRepository.DidNotReceive().GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -98,7 +101,7 @@ public class GetRolesQueryHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockRoleRepository.GetAllAsync(Arg.Any<CancellationToken>())
_mockRoleRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(error);
// Act
@@ -114,7 +117,7 @@ public class GetRolesQueryHandlerTests
{
// Arrange
GetRolesQuery query = _getRolesQueryFixture.Create();
IEnumerable<RoleEntity> roles =
List<RoleEntity> roles =
[
_roleEntityFixture.Create(roleName: "Admin"),
_roleEntityFixture.Create(roleName: "User")
@@ -122,8 +125,8 @@ public class GetRolesQueryHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockRoleRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From(roles));
_mockRoleRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<RoleEntity> { Data = roles, CurrentPage = 1, PerPage = roles.Count, Count = roles.Count, NumberOfPages = 1 }));
// Act
Result<IEnumerable<RoleResponse>> result = await _sut.HandleAsync(query, CancellationToken.None);
@@ -5,6 +5,8 @@ using Lumina.Application.Common.DataAccess.Repositories.Scheduling;
using Lumina.Application.Common.DataAccess.Repositories.Users;
using Lumina.Application.Common.DataAccess.Seed;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Common.DomainEvents;
using Lumina.Application.Common.Errors;
using Lumina.Application.Common.Infrastructure.Authentication;
@@ -22,7 +24,6 @@ using Lumina.Domain.SharedKernel.Common.Enums.Scheduling;
using NSubstitute;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
@@ -57,6 +58,7 @@ public class SetupApplicationCommandHandlerTests
public SetupApplicationCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockHashService = Substitute.For<IPasswordHashService>();
_mockCryptographyService = Substitute.For<ICryptographyService>();
_mockTotpTokenGenerator = Substitute.For<ITotpTokenGenerator>();
@@ -75,8 +77,8 @@ public class SetupApplicationCommandHandlerTests
_mockDateTimeProvider.UtcNow.Returns(DateTime.UtcNow);
_mockDataSeedService.SetDefaultScheduledJobsAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
.Returns(Result.Created);
_mockScheduledJobRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From(Enumerable.Empty<ScheduledJobEntity>()));
_mockScheduledJobRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<ScheduledJobEntity> { Data = [], CurrentPage = 1, PerPage = 0, Count = 0, NumberOfPages = 1 }));
_sut = new SetupApplicationCommandHandler(
_mockUnitOfWork,
@@ -97,8 +99,8 @@ public class SetupApplicationCommandHandlerTests
SetupApplicationCommand command = _setupApplicationCommandFixture.Create() with { Use2fa = false };
string hashedPassword = Uri.EscapeDataString("hashedPassword");
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From(Enumerable.Empty<UserEntity>()));
_mockUserRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<UserEntity> { Data = [], CurrentPage = 1, PerPage = 0, Count = 0, NumberOfPages = 1 }));
_mockHashService.HashString(command.Password!)
.Returns(hashedPassword);
_mockUserRepository.InsertAsync(Arg.Any<UserEntity>(), Arg.Any<CancellationToken>())
@@ -121,7 +123,7 @@ public class SetupApplicationCommandHandlerTests
Assert.Equal(command.Username, result.Value.Username);
Assert.Null(result.Value.TotpSecret);
await _mockUserRepository.Received(1).GetAllAsync(Arg.Any<CancellationToken>());
await _mockUserRepository.Received(1).GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
await _mockUserRepository.Received(1).InsertAsync(Arg.Any<UserEntity>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -136,8 +138,8 @@ public class SetupApplicationCommandHandlerTests
string qrCodeUri = "data:image/png;base64,test";
string encryptedSecret = "encryptedSecret";
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From(Enumerable.Empty<UserEntity>()));
_mockUserRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<UserEntity> { Data = [], CurrentPage = 1, PerPage = 0, Count = 0, NumberOfPages = 1 }));
_mockHashService.HashString(command.Password!)
.Returns(hashedPassword);
_mockTotpTokenGenerator.GenerateSecret()
@@ -166,7 +168,7 @@ public class SetupApplicationCommandHandlerTests
Assert.Equal(command.Username, result.Value.Username);
Assert.Equal(qrCodeUri, result.Value.TotpSecret);
await _mockUserRepository.Received(1).GetAllAsync(Arg.Any<CancellationToken>());
await _mockUserRepository.Received(1).GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
await _mockUserRepository.Received(1).InsertAsync(Arg.Any<UserEntity>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -178,8 +180,8 @@ public class SetupApplicationCommandHandlerTests
SetupApplicationCommand command = _setupApplicationCommandFixture.Create();
UserEntity existingUser = _userEntityFixture.Create();
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(new[] { existingUser });
_mockUserRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<UserEntity> { Data = [existingUser], CurrentPage = 1, PerPage = 1, Count = 1, NumberOfPages = 1 }));
// Act
Result<RegistrationResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
@@ -188,7 +190,7 @@ public class SetupApplicationCommandHandlerTests
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authorization.AdminAccountAlreadyCreated, result.FirstError);
await _mockUserRepository.Received(1).GetAllAsync(Arg.Any<CancellationToken>());
await _mockUserRepository.Received(1).GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
await _mockUserRepository.DidNotReceive().InsertAsync(Arg.Any<UserEntity>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -200,7 +202,7 @@ public class SetupApplicationCommandHandlerTests
SetupApplicationCommand command = _setupApplicationCommandFixture.Create();
Error error = Error.Failure("Database.Error", "Failed to retrieve users");
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
_mockUserRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(error);
// Act
@@ -210,7 +212,7 @@ public class SetupApplicationCommandHandlerTests
Assert.True(result.IsFailure);
Assert.Equal(error, result.FirstError);
await _mockUserRepository.Received(1).GetAllAsync(Arg.Any<CancellationToken>());
await _mockUserRepository.Received(1).GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
await _mockUserRepository.DidNotReceive().InsertAsync(Arg.Any<UserEntity>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -222,8 +224,8 @@ public class SetupApplicationCommandHandlerTests
SetupApplicationCommand command = _setupApplicationCommandFixture.Create();
Error error = Error.Failure("Database.Error", "Failed to insert user");
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From(Enumerable.Empty<UserEntity>()));
_mockUserRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<UserEntity> { Data = [], CurrentPage = 1, PerPage = 0, Count = 0, NumberOfPages = 1 }));
_mockUserRepository.InsertAsync(Arg.Any<UserEntity>(), Arg.Any<CancellationToken>())
.Returns(error);
@@ -234,7 +236,7 @@ public class SetupApplicationCommandHandlerTests
Assert.True(result.IsFailure);
Assert.Equal(error, result.FirstError);
await _mockUserRepository.Received(1).GetAllAsync(Arg.Any<CancellationToken>());
await _mockUserRepository.Received(1).GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
await _mockUserRepository.Received(1).InsertAsync(Arg.Any<UserEntity>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -246,8 +248,8 @@ public class SetupApplicationCommandHandlerTests
SetupApplicationCommand command = _setupApplicationCommandFixture.Create();
Error error = Error.Failure("Database.Error", "Failed to set default permissions");
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From(Enumerable.Empty<UserEntity>()));
_mockUserRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<UserEntity> { Data = [], CurrentPage = 1, PerPage = 0, Count = 0, NumberOfPages = 1 }));
_mockUserRepository.InsertAsync(Arg.Any<UserEntity>(), Arg.Any<CancellationToken>())
.Returns(Result.Created);
_mockDataSeedService.SetDefaultAuthorizationPermissionsAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
@@ -271,8 +273,8 @@ public class SetupApplicationCommandHandlerTests
SetupApplicationCommand command = _setupApplicationCommandFixture.Create();
Error error = Error.Failure("Database.Error", "Failed to set default roles");
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From(Enumerable.Empty<UserEntity>()));
_mockUserRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<UserEntity> { Data = [], CurrentPage = 1, PerPage = 0, Count = 0, NumberOfPages = 1 }));
_mockUserRepository.InsertAsync(Arg.Any<UserEntity>(), Arg.Any<CancellationToken>())
.Returns(Result.Created);
_mockDataSeedService.SetDefaultAuthorizationPermissionsAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
@@ -299,8 +301,8 @@ public class SetupApplicationCommandHandlerTests
SetupApplicationCommand command = _setupApplicationCommandFixture.Create();
Error error = Error.Failure("Database.Error", "Failed to set admin role permissions");
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From(Enumerable.Empty<UserEntity>()));
_mockUserRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<UserEntity> { Data = [], CurrentPage = 1, PerPage = 0, Count = 0, NumberOfPages = 1 }));
_mockUserRepository.InsertAsync(Arg.Any<UserEntity>(), Arg.Any<CancellationToken>())
.Returns(Result.Created);
_mockDataSeedService.SetDefaultAuthorizationPermissionsAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
@@ -330,8 +332,8 @@ public class SetupApplicationCommandHandlerTests
SetupApplicationCommand command = _setupApplicationCommandFixture.Create();
Error error = Error.Failure("Database.Error", "Failed to set admin role to administrator account");
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From(Enumerable.Empty<UserEntity>()));
_mockUserRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<UserEntity> { Data = [], CurrentPage = 1, PerPage = 0, Count = 0, NumberOfPages = 1 }));
_mockUserRepository.InsertAsync(Arg.Any<UserEntity>(), Arg.Any<CancellationToken>())
.Returns(Result.Created);
_mockDataSeedService.SetDefaultAuthorizationPermissionsAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
@@ -364,8 +366,8 @@ public class SetupApplicationCommandHandlerTests
SetupApplicationCommand command = _setupApplicationCommandFixture.Create();
Error error = Error.Failure("Database.Error", "Failed to seed default scheduled jobs");
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From(Enumerable.Empty<UserEntity>()));
_mockUserRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<UserEntity> { Data = [], CurrentPage = 1, PerPage = 0, Count = 0, NumberOfPages = 1 }));
_mockUserRepository.InsertAsync(Arg.Any<UserEntity>(), Arg.Any<CancellationToken>())
.Returns(Result.Created);
_mockDataSeedService.SetDefaultAuthorizationPermissionsAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
@@ -396,8 +398,8 @@ public class SetupApplicationCommandHandlerTests
// Arrange
SetupApplicationCommand command = _setupApplicationCommandFixture.Create();
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From(Enumerable.Empty<UserEntity>()));
_mockUserRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<UserEntity> { Data = [], CurrentPage = 1, PerPage = 0, Count = 0, NumberOfPages = 1 }));
_mockUserRepository.InsertAsync(Arg.Any<UserEntity>(), Arg.Any<CancellationToken>())
.Returns(Result.Created);
_mockDataSeedService.SetDefaultAuthorizationPermissionsAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
@@ -433,8 +435,8 @@ public class SetupApplicationCommandHandlerTests
ScheduledJobEntity activeScheduledJob2 = _scheduledJobEntityFixture.Create(status: ScheduledJobStatus.Active);
ScheduledJobEntity addedScheduledJob = _scheduledJobEntityFixture.Create(status: ScheduledJobStatus.Added);
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From(Enumerable.Empty<UserEntity>()));
_mockUserRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<UserEntity> { Data = [], CurrentPage = 1, PerPage = 0, Count = 0, NumberOfPages = 1 }));
_mockUserRepository.InsertAsync(Arg.Any<UserEntity>(), Arg.Any<CancellationToken>())
.Returns(Result.Created);
_mockDataSeedService.SetDefaultAuthorizationPermissionsAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
@@ -445,8 +447,15 @@ public class SetupApplicationCommandHandlerTests
.Returns(Result.Created);
_mockDataSeedService.SetAdminRoleToAdministratorAccount(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
.Returns(Result.Created);
_mockScheduledJobRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(new[] { activeScheduledJob1, activeScheduledJob2, addedScheduledJob });
_mockScheduledJobRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<ScheduledJobEntity>
{
Data = [activeScheduledJob1, activeScheduledJob2, addedScheduledJob],
CurrentPage = 1,
PerPage = 3,
Count = 3,
NumberOfPages = 1
}));
// Act
Result<RegistrationResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
@@ -472,7 +481,7 @@ public class SetupApplicationCommandHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(validationError, result.FirstError);
await _mockUserRepository.DidNotReceive().GetAllAsync(Arg.Any<CancellationToken>());
await _mockUserRepository.DidNotReceive().GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
await _mockUserRepository.DidNotReceive().InsertAsync(Arg.Any<UserEntity>(), Arg.Any<CancellationToken>());
}
@@ -481,8 +490,8 @@ public class SetupApplicationCommandHandlerTests
{
// Arrange
SetupApplicationCommand command = _setupApplicationCommandFixture.Create();
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From(Enumerable.Empty<UserEntity>()));
_mockUserRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<UserEntity> { Data = [], CurrentPage = 1, PerPage = 0, Count = 0, NumberOfPages = 1 }));
_mockHashService.HashString(command.Password!)
.Returns("hashedPassword");
_mockUserRepository.InsertAsync(Arg.Any<UserEntity>(), Arg.Any<CancellationToken>())
@@ -496,7 +505,7 @@ public class SetupApplicationCommandHandlerTests
_mockDataSeedService.SetAdminRoleToAdministratorAccount(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
.Returns(Result.Created);
Error expectedError = Error.Failure("ScheduledJobs.NotFound", "Failed to read the scheduled jobs");
_mockScheduledJobRepository.GetAllAsync(Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(expectedError);
// Act
@@ -2,6 +2,8 @@
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
using Lumina.Application.Common.DataAccess.Repositories.Users;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Core.Maintenance.ApplicationSetup.Queries.CheckInitialization;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.UsersManagement;
using Lumina.Application.Fixtures.Core.Maintenance.ApplicationSetup.Queries.CheckInitialization;
@@ -10,7 +12,6 @@ using Lumina.Domain.Common.Primitives;
using NSubstitute;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
@@ -35,6 +36,7 @@ public class CheckInitializationQueryHandlerTests
public CheckInitializationQueryHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockUserRepository = Substitute.For<IUserRepository>();
_mockUnitOfWork.UserRepository.Returns(_mockUserRepository);
@@ -47,30 +49,30 @@ public class CheckInitializationQueryHandlerTests
{
// Arrange
List<UserEntity> users = _userEntityFixture.CreateMany();
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From(users.AsEnumerable()));
_mockUserRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<UserEntity> { Data = users, CurrentPage = 1, PerPage = users.Count, Count = users.Count, NumberOfPages = 1 }));
// Act
InitializationResponse result = await _sut.HandleAsync(_checkInitializationQueryFixture.Create(), CancellationToken.None);
// Assert
Assert.True(result.IsInitialized);
await _mockUserRepository.Received(1).GetAllAsync(Arg.Any<CancellationToken>());
await _mockUserRepository.Received(1).GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
public async Task HandleAsync_WhenNoUsersExist_ShouldReturnNotInitialized()
{
// Arrange
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From(Enumerable.Empty<UserEntity>()));
_mockUserRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<UserEntity> { Data = [], CurrentPage = 1, PerPage = 0, Count = 0, NumberOfPages = 1 }));
// Act
InitializationResponse result = await _sut.HandleAsync(_checkInitializationQueryFixture.Create(), CancellationToken.None);
// Assert
Assert.False(result.IsInitialized);
await _mockUserRepository.Received(1).GetAllAsync(Arg.Any<CancellationToken>());
await _mockUserRepository.Received(1).GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -78,7 +80,7 @@ public class CheckInitializationQueryHandlerTests
{
// Arrange
Error error = Error.Failure("Database.Error", "Failed to retrieve users");
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
_mockUserRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(error);
// Act
@@ -86,6 +88,6 @@ public class CheckInitializationQueryHandlerTests
// Assert
Assert.False(result.IsInitialized);
await _mockUserRepository.Received(1).GetAllAsync(Arg.Any<CancellationToken>());
await _mockUserRepository.Received(1).GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
}
}
@@ -54,6 +54,7 @@ public class AddLibraryCommandHandlerTests
public AddLibraryCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockLibraryRepository = Substitute.For<ILibraryRepository>();
_mockUnitOfWork.LibraryRepository.Returns(_mockLibraryRepository);
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
@@ -84,7 +85,7 @@ public class AddLibraryCommandHandlerTests
// Arrange
AddLibraryCommand command = _addLibraryCommandFixture.Create();
LibraryEntity persistedLibrary = _libraryEntityFixture.Create(userId: _userId);
_mockLibraryRepository.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(persistedLibrary));
// Act
@@ -123,7 +124,7 @@ public class AddLibraryCommandHandlerTests
AddLibraryCommand command = _addLibraryCommandFixture.Create();
command = command with { CoverImage = null };
LibraryEntity persistedLibrary = _libraryEntityFixture.Create(userId: _userId);
_mockLibraryRepository.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(persistedLibrary));
// Act
@@ -261,7 +262,7 @@ public class AddLibraryCommandHandlerTests
{
// Arrange
AddLibraryCommand command = _addLibraryCommandFixture.Create();
_mockLibraryRepository.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result<LibraryEntity?>.Success(null));
// Act
@@ -44,6 +44,7 @@ public class CancelLibrariesScanCommandHandlerTests
public CancelLibrariesScanCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockLibraryScanRepository = Substitute.For<ILibraryScanRepository>();
_mockUnitOfWork.LibraryScanRepository.Returns(_mockLibraryScanRepository);
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
@@ -49,6 +49,7 @@ public class CancelLibraryScanCommandHandlerTests
public CancelLibraryScanCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockLibraryScanRepository = Substitute.For<ILibraryScanRepository>();
_mockUnitOfWork.LibraryScanRepository.Returns(_mockLibraryScanRepository);
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
@@ -74,7 +75,7 @@ public class CancelLibraryScanCommandHandlerTests
// Arrange
CancelLibraryScanCommand command = _cancelLibraryScanCommandFixture.Create();
LibraryScanEntity runningScan = _libraryScanEntityFixture.Create(id: command.ScanId, libraryId: command.LibraryId, userId: _userId);
_mockLibraryScanRepository.GetByIdAsync(command.ScanId, Arg.Any<CancellationToken>())
_mockLibraryScanRepository.GetByIdAsync(command.ScanId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryScanEntity?>(runningScan));
// Act
@@ -93,7 +94,7 @@ public class CancelLibraryScanCommandHandlerTests
{
// Arrange
CancelLibraryScanCommand command = _cancelLibraryScanCommandFixture.Create();
_mockLibraryScanRepository.GetByIdAsync(command.ScanId, Arg.Any<CancellationToken>())
_mockLibraryScanRepository.GetByIdAsync(command.ScanId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result<LibraryScanEntity?>.Success(null));
// Act
@@ -111,7 +112,7 @@ public class CancelLibraryScanCommandHandlerTests
{
// Arrange
CancelLibraryScanCommand command = _cancelLibraryScanCommandFixture.Create();
_mockLibraryScanRepository.GetByIdAsync(command.ScanId, Arg.Any<CancellationToken>())
_mockLibraryScanRepository.GetByIdAsync(command.ScanId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Error.Failure(description: "Failed to get library scan"));
// Act
@@ -129,7 +130,7 @@ public class CancelLibraryScanCommandHandlerTests
// Arrange
CancelLibraryScanCommand command = _cancelLibraryScanCommandFixture.Create();
LibraryScanEntity runningScan = _libraryScanEntityFixture.Create(id: command.ScanId, libraryId: command.LibraryId, userId: _userId);
_mockLibraryScanRepository.GetByIdAsync(command.ScanId, Arg.Any<CancellationToken>())
_mockLibraryScanRepository.GetByIdAsync(command.ScanId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryScanEntity?>(runningScan));
_mockAuthorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(_userId, Arg.Any<LibraryOwnershipPolicyContext>(), Arg.Any<CancellationToken>())
.Returns(false);
@@ -157,7 +158,7 @@ public class CancelLibraryScanCommandHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
await _mockLibraryScanRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockLibraryScanRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -167,7 +168,7 @@ public class CancelLibraryScanCommandHandlerTests
// Arrange
CancelLibraryScanCommand command = _cancelLibraryScanCommandFixture.Create();
LibraryScanEntity completedScan = _libraryScanEntityFixture.Create(id: command.ScanId, libraryId: command.LibraryId, userId: _userId, status: LibraryScanJobStatus.Completed);
_mockLibraryScanRepository.GetByIdAsync(command.ScanId, Arg.Any<CancellationToken>())
_mockLibraryScanRepository.GetByIdAsync(command.ScanId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryScanEntity?>(completedScan));
// Act
@@ -186,7 +187,7 @@ public class CancelLibraryScanCommandHandlerTests
// Arrange
CancelLibraryScanCommand command = _cancelLibraryScanCommandFixture.Create();
LibraryScanEntity runningScan = _libraryScanEntityFixture.Create(id: command.ScanId, libraryId: command.LibraryId, userId: _userId);
_mockLibraryScanRepository.GetByIdAsync(command.ScanId, Arg.Any<CancellationToken>())
_mockLibraryScanRepository.GetByIdAsync(command.ScanId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryScanEntity?>(runningScan));
_mockLibraryScanRepository.UpdateAsync(Arg.Any<LibraryScanEntity>(), Arg.Any<CancellationToken>())
.Returns(Error.Failure(description: "Failed to update library scan"));
@@ -212,7 +213,7 @@ public class CancelLibraryScanCommandHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(DomainErrors.LibraryScanning.LibraryScanIdCannotBeEmpty, result.FirstError);
await _mockLibraryScanRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockLibraryScanRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
await _mockLibraryScanRepository.DidNotReceive().UpdateAsync(Arg.Any<LibraryScanEntity>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -50,6 +50,7 @@ public class DeleteLibraryCommandHandlerTests
public DeleteLibraryCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockLibraryRepository = Substitute.For<ILibraryRepository>();
_mockUnitOfWork.LibraryRepository.Returns(_mockLibraryRepository);
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
@@ -78,7 +79,7 @@ public class DeleteLibraryCommandHandlerTests
// Arrange
DeleteLibraryCommand command = _deleteLibraryCommandFixture.Create();
LibraryEntity library = _libraryEntityFixture.Create(id: command.Id, userId: _userId);
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(library));
// Act
@@ -99,7 +100,7 @@ public class DeleteLibraryCommandHandlerTests
// Arrange
DeleteLibraryCommand command = _deleteLibraryCommandFixture.Create();
LibraryEntity library = _libraryEntityFixture.Create(id: command.Id, userId: _userId);
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(library));
_mockProviderConfigurationStore.RemoveProviderConfigurationsForLibraryAsync(command.Id, Arg.Any<CancellationToken>())
.Returns(Error.Failure(description: "Failed to remove provider configurations"));
@@ -118,7 +119,7 @@ public class DeleteLibraryCommandHandlerTests
{
// Arrange
DeleteLibraryCommand command = _deleteLibraryCommandFixture.Create();
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result<LibraryEntity?>.Success(null));
// Act
@@ -136,7 +137,7 @@ public class DeleteLibraryCommandHandlerTests
{
// Arrange
DeleteLibraryCommand command = _deleteLibraryCommandFixture.Create();
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Error.Failure(description: "Failed to get library"));
// Act
@@ -154,7 +155,7 @@ public class DeleteLibraryCommandHandlerTests
// Arrange
DeleteLibraryCommand command = _deleteLibraryCommandFixture.Create();
LibraryEntity library = _libraryEntityFixture.Create(id: command.Id, userId: _userId);
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(library));
_mockAuthorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(_userId, Arg.Any<LibraryOwnershipPolicyContext>(), Arg.Any<CancellationToken>())
.Returns(false);
@@ -182,7 +183,7 @@ public class DeleteLibraryCommandHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -192,7 +193,7 @@ public class DeleteLibraryCommandHandlerTests
// Arrange
DeleteLibraryCommand command = _deleteLibraryCommandFixture.Create();
LibraryEntity library = _libraryEntityFixture.Create(id: command.Id, userId: _userId, contentLocations: [""]);
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(library));
// Act
@@ -211,7 +212,7 @@ public class DeleteLibraryCommandHandlerTests
// Arrange
DeleteLibraryCommand command = _deleteLibraryCommandFixture.Create();
LibraryEntity library = _libraryEntityFixture.Create(id: command.Id, userId: _userId);
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(library));
_mockLibraryRepository.DeleteByIdAsync(command.Id, Arg.Any<CancellationToken>())
.Returns(Error.Failure(description: "Failed to delete library"));
@@ -237,7 +238,7 @@ public class DeleteLibraryCommandHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(DomainErrors.Library.LibraryIdCannotBeEmpty, result.FirstError);
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
await _mockLibraryRepository.DidNotReceive().DeleteByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -48,6 +48,7 @@ public class ScanLibrariesCommandHandlerTests
public ScanLibrariesCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockLibraryRepository = Substitute.For<ILibraryRepository>();
_mockLibraryScanRepository = Substitute.For<ILibraryScanRepository>();
_mockUnitOfWork.LibraryRepository.Returns(_mockLibraryRepository);
@@ -52,6 +52,7 @@ public class ScanLibraryCommandHandlerTests
public ScanLibraryCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockLibraryRepository = Substitute.For<ILibraryRepository>();
_mockLibraryScanRepository = Substitute.For<ILibraryScanRepository>();
_mockUnitOfWork.LibraryRepository.Returns(_mockLibraryRepository);
@@ -81,7 +82,7 @@ public class ScanLibraryCommandHandlerTests
// Arrange
ScanLibraryCommand command = _scanLibraryCommandFixture.Create();
LibraryEntity library = _libraryEntityFixture.Create(id: command.Id, userId: _userId);
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(library));
// Act
@@ -100,7 +101,7 @@ public class ScanLibraryCommandHandlerTests
{
// Arrange
ScanLibraryCommand command = _scanLibraryCommandFixture.Create();
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result<LibraryEntity?>.Success(null));
// Act
@@ -118,7 +119,7 @@ public class ScanLibraryCommandHandlerTests
{
// Arrange
ScanLibraryCommand command = _scanLibraryCommandFixture.Create();
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Error.Failure(description: "Failed to get library"));
// Act
@@ -136,7 +137,7 @@ public class ScanLibraryCommandHandlerTests
// Arrange
ScanLibraryCommand command = _scanLibraryCommandFixture.Create();
LibraryEntity library = _libraryEntityFixture.Create(id: command.Id, userId: _userId);
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(library));
_mockAuthorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(_userId, Arg.Any<LibraryOwnershipPolicyContext>(), Arg.Any<CancellationToken>())
.Returns(false);
@@ -164,7 +165,7 @@ public class ScanLibraryCommandHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -174,7 +175,7 @@ public class ScanLibraryCommandHandlerTests
// Arrange
ScanLibraryCommand command = _scanLibraryCommandFixture.Create();
LibraryEntity library = _libraryEntityFixture.Create(id: command.Id, userId: _userId, isEnabled: false);
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(library));
// Act
@@ -193,7 +194,7 @@ public class ScanLibraryCommandHandlerTests
// Arrange
ScanLibraryCommand command = _scanLibraryCommandFixture.Create();
LibraryEntity library = _libraryEntityFixture.Create(id: command.Id, userId: _userId, isLocked: true);
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(library));
// Act
@@ -212,7 +213,7 @@ public class ScanLibraryCommandHandlerTests
// Arrange
ScanLibraryCommand command = _scanLibraryCommandFixture.Create();
LibraryEntity library = _libraryEntityFixture.Create(id: command.Id, userId: _userId, contentLocations: [""]);
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(library));
// Act
@@ -231,7 +232,7 @@ public class ScanLibraryCommandHandlerTests
// Arrange
ScanLibraryCommand command = _scanLibraryCommandFixture.Create();
LibraryEntity library = _libraryEntityFixture.Create(id: command.Id, userId: _userId);
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(library));
_mockLibraryScanRepository.GetPastMonthScansByLibraryIdAsync(command.Id, Arg.Any<CancellationToken>())
.Returns(Error.Failure(description: "Failed to get past month scans"));
@@ -251,7 +252,7 @@ public class ScanLibraryCommandHandlerTests
// Arrange
ScanLibraryCommand command = _scanLibraryCommandFixture.Create();
LibraryEntity library = _libraryEntityFixture.Create(id: command.Id, userId: _userId);
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(library));
LibraryScanEntity runningScan = _libraryScanEntityFixture.Create(libraryId: command.Id, userId: _userId, status: LibraryScanJobStatus.Running);
_mockLibraryScanRepository.GetPastMonthScansByLibraryIdAsync(command.Id, Arg.Any<CancellationToken>())
@@ -273,7 +274,7 @@ public class ScanLibraryCommandHandlerTests
// Arrange
ScanLibraryCommand command = _scanLibraryCommandFixture.Create();
LibraryEntity library = _libraryEntityFixture.Create(id: command.Id, userId: _userId);
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(library));
_mockLibraryScanRepository.InsertAsync(Arg.Any<LibraryScanEntity>(), Arg.Any<CancellationToken>())
.Returns(Error.Failure(description: "Failed to insert library scan"));
@@ -299,7 +300,7 @@ public class ScanLibraryCommandHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(DomainErrors.Library.LibraryIdCannotBeEmpty, result.FirstError);
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
await _mockLibraryScanRepository.DidNotReceive().InsertAsync(Arg.Any<LibraryScanEntity>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -56,6 +56,7 @@ public class UpdateLibraryCommandHandlerTests
public UpdateLibraryCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockLibraryRepository = Substitute.For<ILibraryRepository>();
_mockUnitOfWork.LibraryRepository.Returns(_mockLibraryRepository);
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
@@ -89,7 +90,7 @@ public class UpdateLibraryCommandHandlerTests
LibraryType commandLibraryType = Enum.Parse<LibraryType>(command.LibraryType);
LibraryEntity existingLibrary = _libraryEntityFixture.Create(id: command.Id, userId: command.OwnerId);
existingLibrary.LibraryType = commandLibraryType == LibraryType.Book ? LibraryType.EBook : LibraryType.Book;
_mockLibraryRepository.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(existingLibrary));
// Act
@@ -112,7 +113,7 @@ public class UpdateLibraryCommandHandlerTests
UpdateLibraryCommand command = _updateLibraryCommandFixture.Create();
LibraryEntity existingLibrary = _libraryEntityFixture.Create(id: command.Id, userId: command.OwnerId);
existingLibrary.LibraryType = Enum.Parse<LibraryType>(command.LibraryType);
_mockLibraryRepository.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(existingLibrary));
// Act
@@ -131,7 +132,7 @@ public class UpdateLibraryCommandHandlerTests
LibraryType commandLibraryType = Enum.Parse<LibraryType>(command.LibraryType);
LibraryEntity existingLibrary = _libraryEntityFixture.Create(id: command.Id, userId: command.OwnerId);
existingLibrary.LibraryType = commandLibraryType == LibraryType.Book ? LibraryType.EBook : LibraryType.Book;
_mockLibraryRepository.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(existingLibrary));
_mockProviderConfigurationStore.ReconcileProviderConfigurationsAsync(Arg.Any<Guid>(), Arg.Any<LibraryType>(), Arg.Any<CancellationToken>())
.Returns(Error.Failure(description: "Failed to reconcile provider configurations"));
@@ -151,7 +152,7 @@ public class UpdateLibraryCommandHandlerTests
UpdateLibraryCommand command = _updateLibraryCommandFixture.Create();
command = command with { CoverImage = null };
LibraryEntity existingLibrary = _libraryEntityFixture.Create(id: command.Id, userId: command.OwnerId);
_mockLibraryRepository.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(existingLibrary));
// Act
@@ -171,7 +172,7 @@ public class UpdateLibraryCommandHandlerTests
command = command with { CoverImage = "C:/Users/user/cover.jpg" };
LibraryEntity existingLibrary = _libraryEntityFixture.Create(id: command.Id, userId: command.OwnerId);
existingLibrary.CoverImage = null;
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(existingLibrary));
_mockEnvironmentContext.FileTypeService.GetImageTypeAsync(Arg.Any<FileSystemPathId>(), Arg.Any<CancellationToken>())
.Returns(Result.From(ImageType.None));
@@ -182,7 +183,7 @@ public class UpdateLibraryCommandHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(DomainErrors.Library.CoverFileMustBeAnImage, result.FirstError);
await _mockLibraryRepository.Received(1).GetByIdAsync(command.Id, Arg.Any<CancellationToken>());
await _mockLibraryRepository.Received(1).GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -194,7 +195,7 @@ public class UpdateLibraryCommandHandlerTests
command = command with { CoverImage = "C:/Users/user/cover.jpg" };
LibraryEntity existingLibrary = _libraryEntityFixture.Create(id: command.Id, userId: command.OwnerId);
existingLibrary.CoverImage = null;
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(existingLibrary));
_mockEnvironmentContext.FileTypeService.GetImageTypeAsync(Arg.Any<FileSystemPathId>(), Arg.Any<CancellationToken>())
.Returns(Error.Failure(description: "Failed to determine image type"));
@@ -204,7 +205,7 @@ public class UpdateLibraryCommandHandlerTests
// Assert
Assert.True(result.IsFailure);
await _mockLibraryRepository.Received(1).GetByIdAsync(command.Id, Arg.Any<CancellationToken>());
await _mockLibraryRepository.Received(1).GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -216,7 +217,7 @@ public class UpdateLibraryCommandHandlerTests
command = command with { CoverImage = " " };
LibraryEntity existingLibrary = _libraryEntityFixture.Create(id: command.Id, userId: command.OwnerId);
existingLibrary.CoverImage = null;
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(existingLibrary));
// Act
@@ -225,7 +226,7 @@ public class UpdateLibraryCommandHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(DomainErrors.FileSystemManagement.InvalidPath, result.FirstError);
await _mockLibraryRepository.Received(1).GetByIdAsync(command.Id, Arg.Any<CancellationToken>());
await _mockLibraryRepository.Received(1).GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -236,7 +237,7 @@ public class UpdateLibraryCommandHandlerTests
UpdateLibraryCommand command = _updateLibraryCommandFixture.Create();
LibraryEntity existingLibrary = _libraryEntityFixture.Create(id: command.Id, userId: command.OwnerId);
existingLibrary.CoverImage = command.CoverImage;
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(existingLibrary));
// Act
@@ -246,7 +247,7 @@ public class UpdateLibraryCommandHandlerTests
Assert.False(result.IsFailure);
await _mockEnvironmentContext.FileTypeService.DidNotReceive()
.GetImageTypeAsync(Arg.Any<FileSystemPathId>(), Arg.Any<CancellationToken>());
await _mockLibraryRepository.Received(2).GetByIdAsync(command.Id, Arg.Any<CancellationToken>());
await _mockLibraryRepository.Received(2).GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>());
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -255,7 +256,7 @@ public class UpdateLibraryCommandHandlerTests
{
// Arrange
UpdateLibraryCommand command = _updateLibraryCommandFixture.Create();
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result<LibraryEntity?>.Success(null));
// Act
@@ -273,7 +274,7 @@ public class UpdateLibraryCommandHandlerTests
{
// Arrange
UpdateLibraryCommand command = _updateLibraryCommandFixture.Create();
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Error.Failure(description: "Failed to get library"));
// Act
@@ -291,7 +292,7 @@ public class UpdateLibraryCommandHandlerTests
// Arrange
UpdateLibraryCommand command = _updateLibraryCommandFixture.Create();
LibraryEntity existingLibrary = _libraryEntityFixture.Create(id: command.Id, userId: command.OwnerId);
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(existingLibrary));
_mockAuthorizationService.HasPermissionAsync(_userId, Arg.Any<AuthorizationPermission>(), Arg.Any<CancellationToken>())
.Returns(false);
@@ -321,7 +322,7 @@ public class UpdateLibraryCommandHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -331,7 +332,7 @@ public class UpdateLibraryCommandHandlerTests
// Arrange
UpdateLibraryCommand command = _updateLibraryCommandFixture.Create();
LibraryEntity existingLibrary = _libraryEntityFixture.Create(id: command.Id, userId: command.OwnerId);
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(existingLibrary));
command = command with { ContentLocations = [""] };
@@ -351,7 +352,7 @@ public class UpdateLibraryCommandHandlerTests
// Arrange
UpdateLibraryCommand command = _updateLibraryCommandFixture.Create();
LibraryEntity existingLibrary = _libraryEntityFixture.Create(id: command.Id, userId: command.OwnerId);
_mockLibraryRepository.GetByIdAsync(command.Id, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(command.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(existingLibrary));
_mockLibraryRepository.UpdateAsync(Arg.Any<LibraryEntity>(), Arg.Any<CancellationToken>())
.Returns(Error.Failure(description: "Failed to update library"));
@@ -370,7 +371,7 @@ public class UpdateLibraryCommandHandlerTests
// Arrange
UpdateLibraryCommand command = _updateLibraryCommandFixture.Create();
LibraryEntity existingLibrary = _libraryEntityFixture.Create(id: command.Id, userId: command.OwnerId);
_mockLibraryRepository.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(existingLibrary), Error.Failure(description: "Failed to retrieve updated library"));
// Act
@@ -386,7 +387,7 @@ public class UpdateLibraryCommandHandlerTests
// Arrange
UpdateLibraryCommand command = _updateLibraryCommandFixture.Create();
LibraryEntity existingLibrary = _libraryEntityFixture.Create(id: command.Id, userId: command.OwnerId);
_mockLibraryRepository.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(existingLibrary), Result<LibraryEntity?>.Success(null));
// Act
@@ -411,7 +412,7 @@ public class UpdateLibraryCommandHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(DomainErrors.Library.LibraryIdCannotBeEmpty, result.FirstError);
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
await _mockLibraryRepository.DidNotReceive().UpdateAsync(Arg.Any<LibraryEntity>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -1,7 +1,7 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
using Lumina.Application.Common.DataAccess.Repositories.Books;
using Lumina.Application.Common.DataAccess.Repositories.BookLibrary;
using Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Core.MediaLibrary.Management.Events;
@@ -68,15 +68,15 @@ public class LibraryMediaItemDeletedDomainEventHandlerTests
LibraryEntity library = _libraryEntityFixture.Create(id: libraryId.Value, title: "My Library");
_mockBookRepository.GetByPathAsync(libraryId.Value, book.Path, Arg.Any<CancellationToken>())
.Returns(Result.From<BookEntity?>(book));
_mockLibraryRepository.GetByIdAsync(libraryId.Value, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(libraryId.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(library));
_mockBookRepository.GetAuthorsDisplayNamesByBookIdsAsync(Arg.Any<IReadOnlyCollection<Guid>>(), Arg.Any<CancellationToken>())
.Returns(Result.From<IReadOnlyDictionary<Guid, string?>>(new Dictionary<Guid, string?> { [book.Id] = "Frank Herbert" }));
_mockBookRepository.DeleteAsync(book.Id, Arg.Any<CancellationToken>())
_mockBookRepository.DeleteByIdAsync(book.Id, Arg.Any<CancellationToken>())
.Returns(Result.From(Result.Deleted));
_mockBookArtworkService.DeleteBookArtwork(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>())
.Returns(Result.Deleted);
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Task.CompletedTask);
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
LibraryMediaItemDeletedDomainEvent domainEvent = _libraryMediaItemDeletedDomainEventFixture.Create(libraryId: libraryId, path: book.Path);
@@ -85,7 +85,7 @@ public class LibraryMediaItemDeletedDomainEventHandlerTests
// Assert
_mockBookArtworkService.Received(1).DeleteBookArtwork(libraryId.Value, book.Id, "My Library", "Frank Herbert", book.Title);
await _mockBookRepository.Received(1).DeleteAsync(book.Id, Arg.Any<CancellationToken>());
await _mockBookRepository.Received(1).DeleteByIdAsync(book.Id, Arg.Any<CancellationToken>());
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -104,7 +104,7 @@ public class LibraryMediaItemDeletedDomainEventHandlerTests
// Assert
_mockBookArtworkService.DidNotReceive().DeleteBookArtwork(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>());
await _mockBookRepository.DidNotReceive().DeleteAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockBookRepository.DidNotReceive().DeleteByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -117,15 +117,15 @@ public class LibraryMediaItemDeletedDomainEventHandlerTests
LibraryEntity library = _libraryEntityFixture.Create(id: libraryId.Value, title: "My Library");
_mockBookRepository.GetByPathAsync(libraryId.Value, book.Path, Arg.Any<CancellationToken>())
.Returns(Result.From<BookEntity?>(book));
_mockLibraryRepository.GetByIdAsync(libraryId.Value, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(libraryId.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(library));
_mockBookRepository.GetAuthorsDisplayNamesByBookIdsAsync(Arg.Any<IReadOnlyCollection<Guid>>(), Arg.Any<CancellationToken>())
.Returns(Result.From<IReadOnlyDictionary<Guid, string?>>(new Dictionary<Guid, string?> { [book.Id] = "Frank Herbert" }));
_mockBookArtworkService.DeleteBookArtwork(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>())
.Returns(Error.Failure("Artwork.DeleteFailed", "Failed to delete the stored artwork"));
_mockBookRepository.DeleteAsync(book.Id, Arg.Any<CancellationToken>())
_mockBookRepository.DeleteByIdAsync(book.Id, Arg.Any<CancellationToken>())
.Returns(Result.From(Result.Deleted));
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Task.CompletedTask);
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
LibraryMediaItemDeletedDomainEvent domainEvent = _libraryMediaItemDeletedDomainEventFixture.Create(libraryId: libraryId, path: book.Path);
@@ -134,7 +134,7 @@ public class LibraryMediaItemDeletedDomainEventHandlerTests
// Assert
// a failing artwork deletion must not prevent the book from being removed, so the deletion is only logged
await _mockBookRepository.Received(1).DeleteAsync(book.Id, Arg.Any<CancellationToken>());
await _mockBookRepository.Received(1).DeleteByIdAsync(book.Id, Arg.Any<CancellationToken>());
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -155,7 +155,7 @@ public class LibraryMediaItemDeletedDomainEventHandlerTests
// Assert
Assert.Equal(error, exception.EventualConsistencyError);
await _mockBookRepository.DidNotReceive().DeleteAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockBookRepository.DidNotReceive().DeleteByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
}
[Fact]
@@ -167,14 +167,14 @@ public class LibraryMediaItemDeletedDomainEventHandlerTests
LibraryEntity library = _libraryEntityFixture.Create(id: libraryId.Value, title: "My Library");
_mockBookRepository.GetByPathAsync(libraryId.Value, book.Path, Arg.Any<CancellationToken>())
.Returns(Result.From<BookEntity?>(book));
_mockLibraryRepository.GetByIdAsync(libraryId.Value, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(libraryId.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(library));
_mockBookRepository.GetAuthorsDisplayNamesByBookIdsAsync(Arg.Any<IReadOnlyCollection<Guid>>(), Arg.Any<CancellationToken>())
.Returns(Result.From<IReadOnlyDictionary<Guid, string?>>(new Dictionary<Guid, string?> { [book.Id] = "Frank Herbert" }));
_mockBookArtworkService.DeleteBookArtwork(Arg.Any<Guid>(), Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>())
.Returns(Result.Deleted);
Error error = Error.Failure("Database.Error", "Failed to delete the book");
_mockBookRepository.DeleteAsync(book.Id, Arg.Any<CancellationToken>())
_mockBookRepository.DeleteByIdAsync(book.Id, Arg.Any<CancellationToken>())
.Returns(error);
LibraryMediaItemDeletedDomainEvent domainEvent = _libraryMediaItemDeletedDomainEventFixture.Create(libraryId: libraryId, path: book.Path);
@@ -55,6 +55,7 @@ public class LibrarySavedDomainEventHandlerTests
public LibrarySavedDomainEventHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockLibraryRepository = Substitute.For<ILibraryRepository>();
_mockUnitOfWork.LibraryRepository.Returns(_mockLibraryRepository);
_mockEnvironmentContext = Substitute.For<IEnvironmentContext>();
@@ -106,7 +107,7 @@ public class LibrarySavedDomainEventHandlerTests
await _sut.HandleAsync(domainEvent, CancellationToken.None);
// Assert
Assert.NotNull(domainEvent.Library.CoverImage);
Assert.True(domainEvent.Library.CoverImage.HasValue);
await _mockLibraryRepository.Received(1).UpdateAsync(Arg.Any<LibraryEntity>(), Arg.Any<CancellationToken>());
await _mockUnitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -177,7 +178,7 @@ public class LibrarySavedDomainEventHandlerTests
await _sut.HandleAsync(domainEvent, CancellationToken.None);
// Assert
Assert.Equal("/Media/Libraries/guid/cover.png", domainEvent.Library.CoverImage);
Assert.Equal("/Media/Libraries/guid/cover.png", domainEvent.Library.CoverImage.Value);
_mockFileProviderService.DidNotReceive().CopyFile(Arg.Any<FileSystemPathId>(), Arg.Any<FileSystemPathId>(), Arg.Any<bool>());
await _mockLibraryRepository.Received(1).UpdateAsync(Arg.Any<LibraryEntity>(), Arg.Any<CancellationToken>());
}
@@ -46,6 +46,7 @@ public class LibraryScanCancelledDomainEventHandlerTests
public LibraryScanCancelledDomainEventHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockLibraryScanRepository = Substitute.For<ILibraryScanRepository>();
_mockLibraryScanStagingResultsRepository = Substitute.For<ILibraryScanStagingResultsRepository>();
_mockUnitOfWork.LibraryScanRepository.Returns(_mockLibraryScanRepository);
@@ -70,7 +71,7 @@ public class LibraryScanCancelledDomainEventHandlerTests
LibraryScanEntity scan = _libraryScanEntityFixture.Create(
id: domainEvent.ScanId.Value,
libraryId: domainEvent.LibraryId.Value);
_mockLibraryScanRepository.GetByIdAsync(scan.Id, Arg.Any<CancellationToken>())
_mockLibraryScanRepository.GetByIdAsync(scan.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryScanEntity?>(scan));
// Act
@@ -89,7 +90,7 @@ public class LibraryScanCancelledDomainEventHandlerTests
// Arrange
LibraryScanCancelledDomainEvent domainEvent = _libraryScanCancelledDomainEventFixture.Create();
Error error = Error.Failure(description: "Failed to get library scan");
_mockLibraryScanRepository.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
_mockLibraryScanRepository.GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>())
.Returns(error);
// Act
@@ -106,7 +107,7 @@ public class LibraryScanCancelledDomainEventHandlerTests
{
// Arrange
LibraryScanCancelledDomainEvent domainEvent = _libraryScanCancelledDomainEventFixture.Create();
_mockLibraryScanRepository.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
_mockLibraryScanRepository.GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result<LibraryScanEntity?>.Success(null));
// Act
@@ -126,7 +127,7 @@ public class LibraryScanCancelledDomainEventHandlerTests
LibraryScanEntity scan = _libraryScanEntityFixture.Create(
id: domainEvent.ScanId.Value,
libraryId: domainEvent.LibraryId.Value);
_mockLibraryScanRepository.GetByIdAsync(scan.Id, Arg.Any<CancellationToken>())
_mockLibraryScanRepository.GetByIdAsync(scan.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryScanEntity?>(scan));
Error error = Error.Failure(description: "Failed to cancel scan");
_mockMediaLibraryScanningService.CancelScan(Arg.Any<LibraryScan>()).Returns(error);
@@ -149,7 +150,7 @@ public class LibraryScanCancelledDomainEventHandlerTests
LibraryScanEntity scan = _libraryScanEntityFixture.Create(
id: domainEvent.ScanId.Value,
libraryId: domainEvent.LibraryId.Value);
_mockLibraryScanRepository.GetByIdAsync(scan.Id, Arg.Any<CancellationToken>())
_mockLibraryScanRepository.GetByIdAsync(scan.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryScanEntity?>(scan));
Error error = Error.Failure(description: "Failed to clear staging results");
_mockLibraryScanStagingResultsRepository.ClearForScanAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
@@ -47,6 +47,7 @@ public class LibraryScanFailedDomainEventHandlerTests
public LibraryScanFailedDomainEventHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockLibraryScanRepository = Substitute.For<ILibraryScanRepository>();
_mockLibraryScanStagingResultsRepository = Substitute.For<ILibraryScanStagingResultsRepository>();
_mockUnitOfWork.LibraryScanRepository.Returns(_mockLibraryScanRepository);
@@ -73,7 +74,7 @@ public class LibraryScanFailedDomainEventHandlerTests
LibraryScanEntity scan = _libraryScanEntityFixture.Create(
id: compositeId.ScanId.Value,
userId: compositeId.UserId.Value);
_mockLibraryScanRepository.GetByIdAsync(scan.Id, Arg.Any<CancellationToken>())
_mockLibraryScanRepository.GetByIdAsync(scan.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryScanEntity?>(scan));
// Act
@@ -98,7 +99,7 @@ public class LibraryScanFailedDomainEventHandlerTests
id: compositeId.ScanId.Value,
userId: compositeId.UserId.Value,
status: LibraryScanJobStatus.Failed);
_mockLibraryScanRepository.GetByIdAsync(scan.Id, Arg.Any<CancellationToken>())
_mockLibraryScanRepository.GetByIdAsync(scan.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryScanEntity?>(scan));
// Act
@@ -119,7 +120,7 @@ public class LibraryScanFailedDomainEventHandlerTests
// Arrange
LibraryScanFailedDomainEvent domainEvent = _libraryScanFailedDomainEventFixture.Create();
Error error = Error.Failure(description: "Failed to get library scan");
_mockLibraryScanRepository.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
_mockLibraryScanRepository.GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>())
.Returns(error);
// Act
@@ -136,7 +137,7 @@ public class LibraryScanFailedDomainEventHandlerTests
{
// Arrange
LibraryScanFailedDomainEvent domainEvent = _libraryScanFailedDomainEventFixture.Create();
_mockLibraryScanRepository.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
_mockLibraryScanRepository.GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result<LibraryScanEntity?>.Success(null));
// Act
@@ -157,7 +158,7 @@ public class LibraryScanFailedDomainEventHandlerTests
LibraryScanEntity scan = _libraryScanEntityFixture.Create(
id: compositeId.ScanId.Value,
userId: compositeId.UserId.Value);
_mockLibraryScanRepository.GetByIdAsync(scan.Id, Arg.Any<CancellationToken>())
_mockLibraryScanRepository.GetByIdAsync(scan.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryScanEntity?>(scan));
Error error = Error.Failure(description: "Failed to update library scan");
_mockLibraryScanRepository.UpdateAsync(Arg.Any<LibraryScanEntity>(), Arg.Any<CancellationToken>())
@@ -181,7 +182,7 @@ public class LibraryScanFailedDomainEventHandlerTests
LibraryScanEntity scan = _libraryScanEntityFixture.Create(
id: compositeId.ScanId.Value,
userId: compositeId.UserId.Value);
_mockLibraryScanRepository.GetByIdAsync(scan.Id, Arg.Any<CancellationToken>())
_mockLibraryScanRepository.GetByIdAsync(scan.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryScanEntity?>(scan));
Error error = Error.Failure(description: "Failed to clear staging results");
_mockLibraryScanStagingResultsRepository.ClearForScanAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
@@ -46,6 +46,7 @@ public class LibraryScanFinishedDomainEventHandlerTests
public LibraryScanFinishedDomainEventHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockLibraryScanRepository = Substitute.For<ILibraryScanRepository>();
_mockUnitOfWork.LibraryScanRepository.Returns(_mockLibraryScanRepository);
_mockMediaLibrariesScanProgressTracker = Substitute.For<IMediaLibrariesScanProgressTracker>();
@@ -68,7 +69,7 @@ public class LibraryScanFinishedDomainEventHandlerTests
LibraryScanEntity scan = _libraryScanEntityFixture.Create(
id: compositeId.ScanId.Value,
userId: compositeId.UserId.Value);
_mockLibraryScanRepository.GetByIdAsync(scan.Id, Arg.Any<CancellationToken>())
_mockLibraryScanRepository.GetByIdAsync(scan.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryScanEntity?>(scan));
_mockMediaLibrariesScanProgressTracker.UpdateScanProgress(Arg.Any<LibraryId>(), compositeId).Returns(Result.Updated);
@@ -89,7 +90,7 @@ public class LibraryScanFinishedDomainEventHandlerTests
// Arrange
LibraryScanFinishedDomainEvent domainEvent = _libraryScanFinishedDomainEventFixture.Create();
Error error = Error.Failure(description: "Failed to get library scan");
_mockLibraryScanRepository.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
_mockLibraryScanRepository.GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>())
.Returns(error);
// Act
@@ -106,7 +107,7 @@ public class LibraryScanFinishedDomainEventHandlerTests
{
// Arrange
LibraryScanFinishedDomainEvent domainEvent = _libraryScanFinishedDomainEventFixture.Create();
_mockLibraryScanRepository.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
_mockLibraryScanRepository.GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result<LibraryScanEntity?>.Success(null));
// Act
@@ -128,7 +129,7 @@ public class LibraryScanFinishedDomainEventHandlerTests
id: compositeId.ScanId.Value,
userId: compositeId.UserId.Value,
status: LibraryScanJobStatus.Pending);
_mockLibraryScanRepository.GetByIdAsync(scan.Id, Arg.Any<CancellationToken>())
_mockLibraryScanRepository.GetByIdAsync(scan.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryScanEntity?>(scan));
// Act
@@ -150,7 +151,7 @@ public class LibraryScanFinishedDomainEventHandlerTests
LibraryScanEntity scan = _libraryScanEntityFixture.Create(
id: compositeId.ScanId.Value,
userId: compositeId.UserId.Value);
_mockLibraryScanRepository.GetByIdAsync(scan.Id, Arg.Any<CancellationToken>())
_mockLibraryScanRepository.GetByIdAsync(scan.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryScanEntity?>(scan));
Error error = Error.Failure(description: "Failed to update library scan");
_mockLibraryScanRepository.UpdateAsync(Arg.Any<LibraryScanEntity>(), Arg.Any<CancellationToken>())
@@ -43,6 +43,7 @@ public class LibraryScanQueuedDomainEventHandlerTests
public LibraryScanQueuedDomainEventHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockLibraryScanRepository = Substitute.For<ILibraryScanRepository>();
_mockUnitOfWork.LibraryScanRepository.Returns(_mockLibraryScanRepository);
_mockMediaLibraryScanningService = Substitute.For<IMediaLibraryScanningService>();
@@ -63,7 +64,7 @@ public class LibraryScanQueuedDomainEventHandlerTests
LibraryScanEntity scan = _libraryScanEntityFixture.Create(
id: domainEvent.ScanId.Value,
libraryId: domainEvent.LibraryId.Value);
_mockLibraryScanRepository.GetByIdAsync(scan.Id, Arg.Any<CancellationToken>())
_mockLibraryScanRepository.GetByIdAsync(scan.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryScanEntity?>(scan));
// Act
@@ -82,7 +83,7 @@ public class LibraryScanQueuedDomainEventHandlerTests
// Arrange
LibraryScanQueuedDomainEvent domainEvent = _libraryScanQueuedDomainEventFixture.Create();
Error error = Error.Failure(description: "Failed to get library scan");
_mockLibraryScanRepository.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
_mockLibraryScanRepository.GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>())
.Returns(error);
// Act
@@ -99,7 +100,7 @@ public class LibraryScanQueuedDomainEventHandlerTests
{
// Arrange
LibraryScanQueuedDomainEvent domainEvent = _libraryScanQueuedDomainEventFixture.Create();
_mockLibraryScanRepository.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
_mockLibraryScanRepository.GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result<LibraryScanEntity?>.Success(null));
// Act
@@ -119,7 +120,7 @@ public class LibraryScanQueuedDomainEventHandlerTests
LibraryScanEntity scan = _libraryScanEntityFixture.Create(
id: domainEvent.ScanId.Value,
libraryId: domainEvent.LibraryId.Value);
_mockLibraryScanRepository.GetByIdAsync(scan.Id, Arg.Any<CancellationToken>())
_mockLibraryScanRepository.GetByIdAsync(scan.Id, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryScanEntity?>(scan));
Error error = Error.Failure(description: "Failed to start scan");
_mockMediaLibraryScanningService.StartScanAsync(Arg.Any<LibraryScan>(), Arg.Any<LibraryType>(), Arg.Any<CancellationToken>())
@@ -34,6 +34,7 @@ public class LibraryScanStartedDomainEventHandlerTests
public LibraryScanStartedDomainEventHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockLibraryScanRepository = Substitute.For<ILibraryScanRepository>();
_mockUnitOfWork.LibraryScanRepository.Returns(_mockLibraryScanRepository);
_mockLibraryScanRepository.UpdateAsync(Arg.Any<LibraryScanEntity>(), Arg.Any<CancellationToken>())
@@ -41,6 +41,7 @@ public class GetEnabledLibrariesQueryHandlerTests
public GetEnabledLibrariesQueryHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockLibraryRepository = Substitute.For<ILibraryRepository>();
_mockUnitOfWork.LibraryRepository.Returns(_mockLibraryRepository);
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
@@ -2,6 +2,8 @@
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
using Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Common.Errors;
using Lumina.Application.Common.Infrastructure.Authentication;
using Lumina.Application.Common.Infrastructure.Authorization;
@@ -41,6 +43,7 @@ public class GetLibrariesQueryHandlerTests
public GetLibrariesQueryHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockLibraryRepository = Substitute.For<ILibraryRepository>();
_mockUnitOfWork.LibraryRepository.Returns(_mockLibraryRepository);
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
@@ -63,7 +66,7 @@ public class GetLibrariesQueryHandlerTests
_libraryEntityFixture.Create(userId: _userId),
_libraryEntityFixture.Create(userId: Guid.NewGuid())
];
_mockLibraryRepository.GetAllAsync(Arg.Any<CancellationToken>()).Returns(Result.From<IEnumerable<LibraryEntity>>(libraries));
_mockLibraryRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From(new PaginatedResultDto<LibraryEntity> { Data = libraries, CurrentPage = 1, PerPage = libraries.Count, Count = libraries.Count, NumberOfPages = 1 }));
// Act
Result<LibraryResponse[]> result = await _sut.HandleAsync(_getLibrariesQueryFixture.Create(), CancellationToken.None);
@@ -85,7 +88,7 @@ public class GetLibrariesQueryHandlerTests
_libraryEntityFixture.Create(userId: _userId),
_libraryEntityFixture.Create(userId: otherUserId)
];
_mockLibraryRepository.GetAllAsync(Arg.Any<CancellationToken>()).Returns(Result.From<IEnumerable<LibraryEntity>>(libraries));
_mockLibraryRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From(new PaginatedResultDto<LibraryEntity> { Data = libraries, CurrentPage = 1, PerPage = libraries.Count, Count = libraries.Count, NumberOfPages = 1 }));
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>()).Returns(false);
// Act
@@ -109,14 +112,14 @@ public class GetLibrariesQueryHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authorization.NotAuthorized, result.FirstError);
await _mockLibraryRepository.DidNotReceive().GetAllAsync(Arg.Any<CancellationToken>());
await _mockLibraryRepository.DidNotReceive().GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
public async Task HandleAsync_WhenGetAllFails_ShouldReturnError()
{
// Arrange
_mockLibraryRepository.GetAllAsync(Arg.Any<CancellationToken>())
_mockLibraryRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Error.Failure(description: "Failed to get libraries"));
// Act
@@ -47,6 +47,7 @@ public class GetLibraryQueryHandlerTests
{
_mockLibraryRepository = Substitute.For<ILibraryRepository>();
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockUnitOfWork.LibraryRepository.Returns(_mockLibraryRepository);
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
@@ -57,7 +58,7 @@ public class GetLibraryQueryHandlerTests
_mockCurrentUserService.UserId.Returns(_userId);
_mockAuthorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(_userId, Arg.Any<LibraryOwnershipPolicyContext>(), Arg.Any<CancellationToken>())
.Returns(true);
_mockLibraryRepository.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From((LibraryEntity?)_libraryEntityFixture.Create(userId: _userId, title: "Test Library", libraryType: LibraryType.EBook, contentLocations: [])));
_mockValidator.Validate(Arg.Any<GetLibraryQuery>()).Returns([]);
@@ -110,7 +111,7 @@ public class GetLibraryQueryHandlerTests
Assert.True(result.IsFailure);
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
await _mockAuthorizationService.DidNotReceive().EvaluatePolicyAsync<ILibraryOwnershipPolicy>(Arg.Any<Guid>(), Arg.Any<LibraryOwnershipPolicyContext>(), Arg.Any<CancellationToken>());
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -118,7 +119,7 @@ public class GetLibraryQueryHandlerTests
{
// Arrange
GetLibraryQuery query = _getLibraryQueryFixture.Create();
_mockLibraryRepository.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result<LibraryEntity?>.Success(null));
// Act
@@ -134,7 +135,7 @@ public class GetLibraryQueryHandlerTests
{
// Arrange
GetLibraryQuery query = _getLibraryQueryFixture.Create();
_mockLibraryRepository.GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>())
.Returns(Errors.Library.LibraryNotFound);
// Act
@@ -159,6 +160,6 @@ public class GetLibraryQueryHandlerTests
Assert.True(result.IsFailure);
Assert.Equal(Errors.Library.LibraryIdCannotBeEmpty, result.FirstError);
await _mockAuthorizationService.DidNotReceive().EvaluatePolicyAsync<ILibraryOwnershipPolicy>(Arg.Any<Guid>(), Arg.Any<LibraryOwnershipPolicyContext>(), Arg.Any<CancellationToken>());
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
}
}
@@ -53,6 +53,7 @@ public class GetLibraryScanProgressQueryHandlerTests
public GetLibraryScanProgressQueryHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockLibraryRepository = Substitute.For<ILibraryRepository>();
_mockUnitOfWork.LibraryRepository.Returns(_mockLibraryRepository);
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
@@ -82,7 +83,7 @@ public class GetLibraryScanProgressQueryHandlerTests
// Arrange
GetLibraryScanProgressQuery query = _getLibraryScanProgressQueryFixture.Create();
LibraryEntity library = _libraryEntityFixture.Create(id: query.LibraryId, userId: _userId);
_mockLibraryRepository.GetByIdAsync(query.LibraryId, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(query.LibraryId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(library));
// Act
@@ -100,7 +101,7 @@ public class GetLibraryScanProgressQueryHandlerTests
{
// Arrange
GetLibraryScanProgressQuery query = _getLibraryScanProgressQueryFixture.Create();
_mockLibraryRepository.GetByIdAsync(query.LibraryId, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(query.LibraryId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result<LibraryEntity?>.Success(null));
// Act
@@ -117,7 +118,7 @@ public class GetLibraryScanProgressQueryHandlerTests
{
// Arrange
GetLibraryScanProgressQuery query = _getLibraryScanProgressQueryFixture.Create();
_mockLibraryRepository.GetByIdAsync(query.LibraryId, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(query.LibraryId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Error.Failure(description: "Failed to get library"));
// Act
@@ -134,7 +135,7 @@ public class GetLibraryScanProgressQueryHandlerTests
// Arrange
GetLibraryScanProgressQuery query = _getLibraryScanProgressQueryFixture.Create();
LibraryEntity library = _libraryEntityFixture.Create(id: query.LibraryId, userId: _userId);
_mockLibraryRepository.GetByIdAsync(query.LibraryId, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(query.LibraryId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(library));
_mockAuthorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(_userId, Arg.Any<LibraryOwnershipPolicyContext>(), Arg.Any<CancellationToken>())
.Returns(false);
@@ -161,7 +162,7 @@ public class GetLibraryScanProgressQueryHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -170,7 +171,7 @@ public class GetLibraryScanProgressQueryHandlerTests
// Arrange
GetLibraryScanProgressQuery query = _getLibraryScanProgressQueryFixture.Create();
LibraryEntity library = _libraryEntityFixture.Create(id: query.LibraryId, userId: _userId);
_mockLibraryRepository.GetByIdAsync(query.LibraryId, Arg.Any<CancellationToken>())
_mockLibraryRepository.GetByIdAsync(query.LibraryId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<LibraryEntity?>(library));
_mockMediaLibrariesScanProgressTracker.GetScanProgress(Arg.Any<MediaLibraryScanCompositeId>())
.Returns(Error.Failure(description: "Failed to get scan progress"));
@@ -195,7 +196,7 @@ public class GetLibraryScanProgressQueryHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(DomainErrors.Library.LibraryIdCannotBeEmpty, result.FirstError);
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockLibraryRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
_mockMediaLibrariesScanProgressTracker.DidNotReceive().GetScanProgress(Arg.Any<MediaLibraryScanCompositeId>());
}
}
@@ -50,6 +50,7 @@ public class GetRunningLibraryScansQueryHandlerTests
public GetRunningLibraryScansQueryHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockLibraryScanRepository = Substitute.For<ILibraryScanRepository>();
_mockUnitOfWork.LibraryScanRepository.Returns(_mockLibraryScanRepository);
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
@@ -44,6 +44,7 @@ public class ReorderLibraryArtworkProvidersCommandHandlerTests
public ReorderLibraryArtworkProvidersCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockConfigurationRepository = Substitute.For<IArtworkProviderConfigurationRepository>();
_mockUnitOfWork.ArtworkProviderConfigurationRepository.Returns(_mockConfigurationRepository);
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
@@ -44,6 +44,7 @@ public class ReorderLibraryMetadataProvidersCommandHandlerTests
public ReorderLibraryMetadataProvidersCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockConfigurationRepository = Substitute.For<ILibraryMetadataProviderConfigurationRepository>();
_mockUnitOfWork.LibraryMetadataProviderConfigurationRepository.Returns(_mockConfigurationRepository);
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
@@ -48,6 +48,7 @@ public class SetLibraryArtworkProviderEnabledCommandHandlerTests
public SetLibraryArtworkProviderEnabledCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockConfigurationRepository = Substitute.For<IArtworkProviderConfigurationRepository>();
_mockUnitOfWork.ArtworkProviderConfigurationRepository.Returns(_mockConfigurationRepository);
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
@@ -48,6 +48,7 @@ public class SetLibraryMetadataProviderEnabledCommandHandlerTests
public SetLibraryMetadataProviderEnabledCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockConfigurationRepository = Substitute.For<ILibraryMetadataProviderConfigurationRepository>();
_mockUnitOfWork.LibraryMetadataProviderConfigurationRepository.Returns(_mockConfigurationRepository);
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
@@ -34,6 +34,7 @@ public class UpdatePluginSettingsCommandHandlerTests
public UpdatePluginSettingsCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockPluginRepository = Substitute.For<IPluginRepository>();
_mockUnitOfWork.PluginRepository.Returns(_mockPluginRepository);
_mockValidator = Substitute.For<IValidator<UpdatePluginSettingsCommand>>();
@@ -2,6 +2,8 @@
using Lumina.Application.Common.DataAccess.Entities.Plugins;
using Lumina.Application.Common.DataAccess.Repositories.Plugins;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Common.Infrastructure.Authentication;
using Lumina.Application.Common.Infrastructure.Authorization;
using Lumina.Application.Common.Infrastructure.Authorization.Policies.LibraryOwnership;
@@ -47,6 +49,7 @@ public class GetLibraryArtworkProvidersQueryHandlerTests
public GetLibraryArtworkProvidersQueryHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockArtworkProviderConfigurationRepository = Substitute.For<IArtworkProviderConfigurationRepository>();
_mockPluginRepository = Substitute.For<IPluginRepository>();
_mockUnitOfWork.ArtworkProviderConfigurationRepository.Returns(_mockArtworkProviderConfigurationRepository);
@@ -100,7 +103,8 @@ public class GetLibraryArtworkProvidersQueryHandlerTests
_pluginEntityFixture.Create(firstPluginId),
_pluginEntityFixture.Create(secondPluginId)
];
_mockPluginRepository.GetAllAsync(Arg.Any<CancellationToken>()).Returns(plugins);
_mockPluginRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<PluginEntity> { Data = plugins, CurrentPage = 1, PerPage = plugins.Count, Count = plugins.Count, NumberOfPages = 1 }));
// Act
Result<IReadOnlyList<LibraryArtworkProviderResponse>> result = await _sut.HandleAsync(query, CancellationToken.None);
@@ -127,8 +131,8 @@ public class GetLibraryArtworkProvidersQueryHandlerTests
];
_mockArtworkProviderConfigurationRepository.GetByLibraryIdAsync(query.LibraryId, Arg.Any<CancellationToken>())
.Returns(Result.From<IReadOnlyList<LibraryArtworkProviderConfigurationEntity>>(configurations));
_mockPluginRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From<IEnumerable<PluginEntity>>([_pluginEntityFixture.Create()]));
_mockPluginRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<PluginEntity> { Data = [_pluginEntityFixture.Create()], CurrentPage = 1, PerPage = 1, Count = 1, NumberOfPages = 1 }));
// Act
Result<IReadOnlyList<LibraryArtworkProviderResponse>> result = await _sut.HandleAsync(query, CancellationToken.None);
@@ -147,8 +151,8 @@ public class GetLibraryArtworkProvidersQueryHandlerTests
GetLibraryArtworkProvidersQuery query = _getLibraryArtworkProvidersQueryFixture.Create();
_mockArtworkProviderConfigurationRepository.GetByLibraryIdAsync(query.LibraryId, Arg.Any<CancellationToken>())
.Returns(Result.From<IReadOnlyList<LibraryArtworkProviderConfigurationEntity>>([]));
_mockPluginRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From<IEnumerable<PluginEntity>>([]));
_mockPluginRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<PluginEntity> { Data = [], CurrentPage = 1, PerPage = 0, Count = 0, NumberOfPages = 1 }));
// Act
Result<IReadOnlyList<LibraryArtworkProviderResponse>> result = await _sut.HandleAsync(query, CancellationToken.None);
@@ -172,7 +176,7 @@ public class GetLibraryArtworkProvidersQueryHandlerTests
// Assert
Assert.True(result.IsFailure);
await _mockPluginRepository.DidNotReceive().GetAllAsync(Arg.Any<CancellationToken>());
await _mockPluginRepository.DidNotReceive().GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -182,7 +186,7 @@ public class GetLibraryArtworkProvidersQueryHandlerTests
GetLibraryArtworkProvidersQuery query = _getLibraryArtworkProvidersQueryFixture.Create();
_mockArtworkProviderConfigurationRepository.GetByLibraryIdAsync(query.LibraryId, Arg.Any<CancellationToken>())
.Returns(Result.From<IReadOnlyList<LibraryArtworkProviderConfigurationEntity>>([]));
_mockPluginRepository.GetAllAsync(Arg.Any<CancellationToken>())
_mockPluginRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Error.Failure(description: "Failed to get plugins"));
// Act
@@ -209,7 +213,7 @@ public class GetLibraryArtworkProvidersQueryHandlerTests
await _mockAuthorizationService.Received(1).EvaluatePolicyAsync<ILibraryOwnershipPolicy>(
_userId, Arg.Is<LibraryOwnershipPolicyContext>(context => context.LibraryId == query.LibraryId), Arg.Any<CancellationToken>());
await _mockArtworkProviderConfigurationRepository.DidNotReceive().GetByLibraryIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockPluginRepository.DidNotReceive().GetAllAsync(Arg.Any<CancellationToken>());
await _mockPluginRepository.DidNotReceive().GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -2,6 +2,8 @@
using Lumina.Application.Common.DataAccess.Entities.Plugins;
using Lumina.Application.Common.DataAccess.Repositories.Plugins;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Common.Infrastructure.Authentication;
using Lumina.Application.Common.Infrastructure.Authorization;
using Lumina.Application.Common.Infrastructure.Authorization.Policies.LibraryOwnership;
@@ -48,6 +50,7 @@ public class GetLibraryMetadataProvidersQueryHandlerTests
public GetLibraryMetadataProvidersQueryHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockPluginRepository = Substitute.For<IPluginRepository>();
_mockUnitOfWork.PluginRepository.Returns(_mockPluginRepository);
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
@@ -100,7 +103,8 @@ public class GetLibraryMetadataProvidersQueryHandlerTests
_pluginEntityFixture.Create(firstPluginId),
_pluginEntityFixture.Create(secondPluginId)
];
_mockPluginRepository.GetAllAsync(Arg.Any<CancellationToken>()).Returns(plugins);
_mockPluginRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<PluginEntity> { Data = plugins, CurrentPage = 1, PerPage = plugins.Count, Count = plugins.Count, NumberOfPages = 1 }));
// Act
Result<IReadOnlyList<LibraryMetadataProviderResponse>> result = await _sut.HandleAsync(query, CancellationToken.None);
@@ -127,8 +131,8 @@ public class GetLibraryMetadataProvidersQueryHandlerTests
];
_mockProviderConfigurationStore.GetConfigurationsAsync(query.LibraryId, Arg.Any<CancellationToken>())
.Returns(Result.From<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>>(configurations));
_mockPluginRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From<IEnumerable<PluginEntity>>([_pluginEntityFixture.Create()]));
_mockPluginRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<PluginEntity> { Data = [_pluginEntityFixture.Create()], CurrentPage = 1, PerPage = 1, Count = 1, NumberOfPages = 1 }));
// Act
Result<IReadOnlyList<LibraryMetadataProviderResponse>> result = await _sut.HandleAsync(query, CancellationToken.None);
@@ -147,8 +151,8 @@ public class GetLibraryMetadataProvidersQueryHandlerTests
GetLibraryMetadataProvidersQuery query = _getLibraryMetadataProvidersQueryFixture.Create();
_mockProviderConfigurationStore.GetConfigurationsAsync(query.LibraryId, Arg.Any<CancellationToken>())
.Returns(Result.From<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>>([]));
_mockPluginRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From<IEnumerable<PluginEntity>>([]));
_mockPluginRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<PluginEntity> { Data = [], CurrentPage = 1, PerPage = 0, Count = 0, NumberOfPages = 1 }));
// Act
Result<IReadOnlyList<LibraryMetadataProviderResponse>> result = await _sut.HandleAsync(query, CancellationToken.None);
@@ -172,7 +176,7 @@ public class GetLibraryMetadataProvidersQueryHandlerTests
// Assert
Assert.True(result.IsFailure);
await _mockPluginRepository.DidNotReceive().GetAllAsync(Arg.Any<CancellationToken>());
await _mockPluginRepository.DidNotReceive().GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -182,7 +186,7 @@ public class GetLibraryMetadataProvidersQueryHandlerTests
GetLibraryMetadataProvidersQuery query = _getLibraryMetadataProvidersQueryFixture.Create();
_mockProviderConfigurationStore.GetConfigurationsAsync(query.LibraryId, Arg.Any<CancellationToken>())
.Returns(Result.From<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>>([]));
_mockPluginRepository.GetAllAsync(Arg.Any<CancellationToken>())
_mockPluginRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Error.Failure(description: "Failed to get plugins"));
// Act
@@ -209,7 +213,7 @@ public class GetLibraryMetadataProvidersQueryHandlerTests
await _mockAuthorizationService.Received(1).EvaluatePolicyAsync<ILibraryOwnershipPolicy>(
_userId, Arg.Is<LibraryOwnershipPolicyContext>(context => context.LibraryId == query.LibraryId), Arg.Any<CancellationToken>());
await _mockProviderConfigurationStore.DidNotReceive().GetConfigurationsAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockPluginRepository.DidNotReceive().GetAllAsync(Arg.Any<CancellationToken>());
await _mockPluginRepository.DidNotReceive().GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -45,6 +45,7 @@ public class GetPluginSettingsQueryHandlerTests
public GetPluginSettingsQueryHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockPluginRepository = Substitute.For<IPluginRepository>();
_mockUnitOfWork.PluginRepository.Returns(_mockPluginRepository);
_mockPluginManager = Substitute.For<IPluginManager>();
@@ -67,7 +68,7 @@ public class GetPluginSettingsQueryHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(DomainErrors.Plugins.PluginIdCannotBeEmpty, result.FirstError);
await _mockPluginRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockPluginRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -76,7 +77,7 @@ public class GetPluginSettingsQueryHandlerTests
// Arrange
GetPluginSettingsQuery query = _getPluginSettingsQueryFixture.Create();
PluginEntity plugin = _pluginEntityFixture.Create(query.PluginId);
_mockPluginRepository.GetByIdAsync(query.PluginId, Arg.Any<CancellationToken>())
_mockPluginRepository.GetByIdAsync(query.PluginId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<PluginEntity?>(plugin));
IPlugin loadedPlugin = Substitute.For<IPlugin>();
@@ -107,7 +108,7 @@ public class GetPluginSettingsQueryHandlerTests
// Arrange
GetPluginSettingsQuery query = _getPluginSettingsQueryFixture.Create();
PluginEntity plugin = _pluginEntityFixture.Create(query.PluginId);
_mockPluginRepository.GetByIdAsync(query.PluginId, Arg.Any<CancellationToken>())
_mockPluginRepository.GetByIdAsync(query.PluginId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<PluginEntity?>(plugin));
_mockPluginManager.GetPlugin(query.PluginId).Returns((IPlugin?)null);
@@ -125,7 +126,7 @@ public class GetPluginSettingsQueryHandlerTests
{
// Arrange
GetPluginSettingsQuery query = _getPluginSettingsQueryFixture.Create();
_mockPluginRepository.GetByIdAsync(query.PluginId, Arg.Any<CancellationToken>())
_mockPluginRepository.GetByIdAsync(query.PluginId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result<PluginEntity?>.Success(null));
// Act
@@ -143,7 +144,7 @@ public class GetPluginSettingsQueryHandlerTests
// Arrange
GetPluginSettingsQuery query = _getPluginSettingsQueryFixture.Create();
Error error = Error.Failure(description: "Failed to get plugin");
_mockPluginRepository.GetByIdAsync(query.PluginId, Arg.Any<CancellationToken>())
_mockPluginRepository.GetByIdAsync(query.PluginId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(error);
// Act
@@ -2,6 +2,8 @@
using Lumina.Application.Common.DataAccess.Entities.Plugins;
using Lumina.Application.Common.DataAccess.Repositories.Plugins;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Core.Plugins.Queries.GetPlugins;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Plugins;
using Lumina.Application.Fixtures.Core.Plugins.Queries.GetPlugins;
@@ -34,6 +36,7 @@ public class GetPluginsQueryHandlerTests
public GetPluginsQueryHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockPluginRepository = Substitute.For<IPluginRepository>();
_mockUnitOfWork.PluginRepository.Returns(_mockPluginRepository);
_sut = new GetPluginsQueryHandler(_mockUnitOfWork);
@@ -44,7 +47,8 @@ public class GetPluginsQueryHandlerTests
{
// Arrange
List<PluginEntity> plugins = [_pluginEntityFixture.Create(), _pluginEntityFixture.Create()];
_mockPluginRepository.GetAllAsync(Arg.Any<CancellationToken>()).Returns(plugins);
_mockPluginRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<PluginEntity> { Data = plugins, CurrentPage = 1, PerPage = plugins.Count, Count = plugins.Count, NumberOfPages = 1 }));
// Act
Result<IReadOnlyList<PluginResponse>> result = await _sut.HandleAsync(_getPluginsQueryFixture.Create(), CancellationToken.None);
@@ -60,7 +64,7 @@ public class GetPluginsQueryHandlerTests
public async Task HandleAsync_WhenRepositoryReturnsError_ShouldReturnError()
{
// Arrange
_mockPluginRepository.GetAllAsync(Arg.Any<CancellationToken>())
_mockPluginRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Error.Failure(description: "Failed to get plugins"));
// Act
@@ -49,6 +49,7 @@ public class AddScheduledJobCommandHandlerTests
public AddScheduledJobCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
_mockDomainEventsQueue = Substitute.For<IDomainEventsQueue>();
@@ -48,6 +48,7 @@ public class FireScheduledJobCommandHandlerTests
public FireScheduledJobCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
_mockDomainEventsQueue = Substitute.For<IDomainEventsQueue>();
@@ -72,7 +73,7 @@ public class FireScheduledJobCommandHandlerTests
id: command.ScheduledJobId,
scheduleType: ScheduleType.WithIntervalInMinutes,
status: ScheduledJobStatus.Active);
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(scheduledJob));
// Act
@@ -98,7 +99,7 @@ public class FireScheduledJobCommandHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(DomainErrors.Scheduling.ScheduledJobNotFound, result.FirstError);
await _mockScheduledJobRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockScheduledJobRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
_mockDomainEventsQueue.DidNotReceive().Enqueue(Arg.Any<IDomainEvent>());
}
@@ -115,7 +116,7 @@ public class FireScheduledJobCommandHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
await _mockScheduledJobRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockScheduledJobRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -131,7 +132,7 @@ public class FireScheduledJobCommandHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
await _mockScheduledJobRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockScheduledJobRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -140,7 +141,7 @@ public class FireScheduledJobCommandHandlerTests
// Arrange
FireScheduledJobCommand command = _fireScheduledJobCommandFixture.Create();
Error error = Error.Failure("Database.Error", "Failed to get the scheduled job");
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, Arg.Any<CancellationToken>()).Returns(error);
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, cancellationToken: Arg.Any<CancellationToken>()).Returns(error);
// Act
Result<ScheduledJobResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
@@ -156,7 +157,7 @@ public class FireScheduledJobCommandHandlerTests
{
// Arrange
FireScheduledJobCommand command = _fireScheduledJobCommandFixture.Create();
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(null));
// Act
@@ -179,7 +180,7 @@ public class FireScheduledJobCommandHandlerTests
scheduleType: ScheduleType.DailyAtHourAndMinute,
hour: 99,
minute: 0);
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(scheduledJob));
// Act
@@ -199,7 +200,7 @@ public class FireScheduledJobCommandHandlerTests
id: command.ScheduledJobId,
scheduleType: ScheduleType.WithIntervalInMinutes,
status: ScheduledJobStatus.Running);
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(scheduledJob));
// Act
@@ -51,6 +51,7 @@ public class RemoveScheduledJobCommandHandlerTests
public RemoveScheduledJobCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
_mockDomainEventsQueue = Substitute.For<IDomainEventsQueue>();
@@ -78,7 +79,7 @@ public class RemoveScheduledJobCommandHandlerTests
id: command.ScheduledJobId,
scheduleType: ScheduleType.WithIntervalInMinutes,
status: ScheduledJobStatus.Active);
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(scheduledJob));
_mockScheduledJobExecutionRepository.DeleteByScheduledJobIdAsync(command.ScheduledJobId, Arg.Any<CancellationToken>())
.Returns(Result.Success);
@@ -113,7 +114,7 @@ public class RemoveScheduledJobCommandHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(DomainErrors.Scheduling.ScheduledJobNotFound, result.FirstError);
await _mockScheduledJobRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockScheduledJobRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
await _mockScheduledJobRepository.DidNotReceive().DeleteByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
}
@@ -130,7 +131,7 @@ public class RemoveScheduledJobCommandHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
await _mockScheduledJobRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockScheduledJobRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -146,7 +147,7 @@ public class RemoveScheduledJobCommandHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
await _mockScheduledJobRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockScheduledJobRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -154,7 +155,7 @@ public class RemoveScheduledJobCommandHandlerTests
{
// Arrange
RemoveScheduledJobCommand command = _removeScheduledJobCommandFixture.Create();
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(null));
// Act
@@ -175,7 +176,7 @@ public class RemoveScheduledJobCommandHandlerTests
id: command.ScheduledJobId,
scheduleType: ScheduleType.WithIntervalInMinutes,
status: ScheduledJobStatus.Active);
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(scheduledJob));
Error error = Error.Failure("Database.Error", "Failed to delete the executions");
_mockScheduledJobExecutionRepository.DeleteByScheduledJobIdAsync(command.ScheduledJobId, Arg.Any<CancellationToken>())
@@ -200,7 +201,7 @@ public class RemoveScheduledJobCommandHandlerTests
id: command.ScheduledJobId,
scheduleType: ScheduleType.WithIntervalInMinutes,
status: ScheduledJobStatus.Active);
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(scheduledJob));
_mockScheduledJobExecutionRepository.DeleteByScheduledJobIdAsync(command.ScheduledJobId, Arg.Any<CancellationToken>())
.Returns(Result.Success);
@@ -223,7 +224,7 @@ public class RemoveScheduledJobCommandHandlerTests
// Arrange
RemoveScheduledJobCommand command = _removeScheduledJobCommandFixture.Create();
Error repositoryError = DomainErrors.Scheduling.ScheduledJobNotFound;
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(repositoryError);
// Act
@@ -247,7 +248,7 @@ public class RemoveScheduledJobCommandHandlerTests
scheduleType: ScheduleType.WithIntervalInMinutes,
intervalMinutes: 0,
status: ScheduledJobStatus.Active);
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(invalidScheduledJob));
// Act
@@ -48,6 +48,7 @@ public class StartScheduledJobCommandHandlerTests
public StartScheduledJobCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
_mockDomainEventsQueue = Substitute.For<IDomainEventsQueue>();
@@ -72,7 +73,7 @@ public class StartScheduledJobCommandHandlerTests
id: command.ScheduledJobId,
scheduleType: ScheduleType.WithIntervalInMinutes,
status: ScheduledJobStatus.Added);
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(scheduledJob));
_mockScheduledJobRepository.UpdateAsync(Arg.Any<ScheduledJobEntity>(), Arg.Any<CancellationToken>()).Returns(Result.Updated);
@@ -102,7 +103,7 @@ public class StartScheduledJobCommandHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(DomainErrors.Scheduling.ScheduledJobNotFound, result.FirstError);
await _mockScheduledJobRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockScheduledJobRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
await _mockUnitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
}
@@ -119,7 +120,7 @@ public class StartScheduledJobCommandHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
await _mockScheduledJobRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockScheduledJobRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -135,7 +136,7 @@ public class StartScheduledJobCommandHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
await _mockScheduledJobRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockScheduledJobRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -143,7 +144,7 @@ public class StartScheduledJobCommandHandlerTests
{
// Arrange
StartScheduledJobCommand command = _startScheduledJobCommandFixture.Create();
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(null));
// Act
@@ -164,7 +165,7 @@ public class StartScheduledJobCommandHandlerTests
id: command.ScheduledJobId,
scheduleType: ScheduleType.WithIntervalInMinutes,
status: ScheduledJobStatus.Active);
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(scheduledJob));
// Act
@@ -186,7 +187,7 @@ public class StartScheduledJobCommandHandlerTests
id: command.ScheduledJobId,
scheduleType: ScheduleType.WithIntervalInMinutes,
status: ScheduledJobStatus.Added);
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(scheduledJob));
_mockScheduledJobRepository.UpdateAsync(Arg.Any<ScheduledJobEntity>(), Arg.Any<CancellationToken>())
.Returns(DomainErrors.Scheduling.ScheduledJobNotFound);
@@ -207,7 +208,7 @@ public class StartScheduledJobCommandHandlerTests
// Arrange
StartScheduledJobCommand command = _startScheduledJobCommandFixture.Create();
Error repositoryError = DomainErrors.Scheduling.ScheduledJobNotFound;
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(repositoryError);
// Act
@@ -230,7 +231,7 @@ public class StartScheduledJobCommandHandlerTests
scheduleType: ScheduleType.WithIntervalInMinutes,
intervalMinutes: 0,
status: ScheduledJobStatus.Added);
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(invalidScheduledJob));
// Act
@@ -50,6 +50,7 @@ public class StopScheduledJobCommandHandlerTests
public StopScheduledJobCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
_mockDomainEventsQueue = Substitute.For<IDomainEventsQueue>();
@@ -75,7 +76,7 @@ public class StopScheduledJobCommandHandlerTests
id: command.ScheduledJobId,
scheduleType: ScheduleType.WithIntervalInMinutes,
status: ScheduledJobStatus.Active);
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(scheduledJob));
_mockScheduledJobRepository.UpdateAsync(Arg.Any<ScheduledJobEntity>(), Arg.Any<CancellationToken>()).Returns(Result.Updated);
@@ -105,7 +106,7 @@ public class StopScheduledJobCommandHandlerTests
scheduleType: ScheduleType.WithIntervalInMinutes,
status: ScheduledJobStatus.Running,
lastStartedOnUtc: DateTime.UtcNow.AddMinutes(-10));
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(scheduledJob));
_mockScheduledJobRepository.UpdateAsync(Arg.Any<ScheduledJobEntity>(), Arg.Any<CancellationToken>()).Returns(Result.Updated);
@@ -132,7 +133,7 @@ public class StopScheduledJobCommandHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(DomainErrors.Scheduling.ScheduledJobNotFound, result.FirstError);
await _mockScheduledJobRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockScheduledJobRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
await _mockScheduledJobScheduler.DidNotReceive().StopCycleAsync(Arg.Any<Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.ValueObjects.ScheduledJobId>(), Arg.Any<CancellationToken>());
}
@@ -149,7 +150,7 @@ public class StopScheduledJobCommandHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
await _mockScheduledJobRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockScheduledJobRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -165,7 +166,7 @@ public class StopScheduledJobCommandHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
await _mockScheduledJobRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockScheduledJobRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -173,7 +174,7 @@ public class StopScheduledJobCommandHandlerTests
{
// Arrange
StopScheduledJobCommand command = _stopScheduledJobCommandFixture.Create();
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(null));
// Act
@@ -194,7 +195,7 @@ public class StopScheduledJobCommandHandlerTests
id: command.ScheduledJobId,
scheduleType: ScheduleType.WithIntervalInMinutes,
status: ScheduledJobStatus.Added);
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(scheduledJob));
// Act
@@ -216,7 +217,7 @@ public class StopScheduledJobCommandHandlerTests
id: command.ScheduledJobId,
scheduleType: ScheduleType.WithIntervalInMinutes,
status: ScheduledJobStatus.Active);
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(scheduledJob));
_mockScheduledJobRepository.UpdateAsync(Arg.Any<ScheduledJobEntity>(), Arg.Any<CancellationToken>())
.Returns(DomainErrors.Scheduling.ScheduledJobNotFound);
@@ -237,7 +238,7 @@ public class StopScheduledJobCommandHandlerTests
// Arrange
StopScheduledJobCommand command = _stopScheduledJobCommandFixture.Create();
Error repositoryError = DomainErrors.Scheduling.ScheduledJobNotFound;
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(repositoryError);
// Act
@@ -260,7 +261,7 @@ public class StopScheduledJobCommandHandlerTests
scheduleType: ScheduleType.WithIntervalInMinutes,
intervalMinutes: 0,
status: ScheduledJobStatus.Active);
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(command.ScheduledJobId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(invalidScheduledJob));
// Act
@@ -41,6 +41,7 @@ public class UpdateSchedulerDisplayPreferencesCommandHandlerTests
public UpdateSchedulerDisplayPreferencesCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
_mockSchedulerDisplayPreferencesRepository = Substitute.For<ISchedulerDisplayPreferencesRepository>();
@@ -2,6 +2,8 @@
using Lumina.Application.Common.DataAccess.Entities.Scheduling;
using Lumina.Application.Common.DataAccess.Repositories.Scheduling;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Core.Scheduling.Events;
using Lumina.Application.Core.Scheduling.Notifications;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Scheduling;
@@ -40,11 +42,12 @@ public class ScheduledJobAddedDomainEventHandlerTests
public ScheduledJobAddedDomainEventHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockScheduledJobNotifier = Substitute.For<IScheduledJobNotifier>();
_mockScheduledJobRepository = Substitute.For<IScheduledJobRepository>();
_mockUnitOfWork.ScheduledJobRepository.Returns(_mockScheduledJobRepository);
_mockScheduledJobRepository.GetAllAsync(Arg.Any<CancellationToken>()).Returns(Result.From<IEnumerable<ScheduledJobEntity>>([]));
_mockScheduledJobRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From(new PaginatedResultDto<ScheduledJobEntity> { Data = [], CurrentPage = 1, PerPage = 0, Count = 0, NumberOfPages = 1 }));
_sut = new ScheduledJobAddedDomainEventHandler(_mockScheduledJobNotifier, _mockUnitOfWork);
}
@@ -56,7 +59,7 @@ public class ScheduledJobAddedDomainEventHandlerTests
ScheduledJobAddedDomainEvent domainEvent = _scheduledJobAddedDomainEventFixture.Create();
ScheduledJobEntity scheduledJob1 = _scheduledJobEntityFixture.Create(name: "Job 1");
ScheduledJobEntity scheduledJob2 = _scheduledJobEntityFixture.Create(name: "Job 2");
_mockScheduledJobRepository.GetAllAsync(Arg.Any<CancellationToken>()).Returns(Result.From<IEnumerable<ScheduledJobEntity>>([scheduledJob1, scheduledJob2]));
_mockScheduledJobRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From(new PaginatedResultDto<ScheduledJobEntity> { Data = [scheduledJob1, scheduledJob2], CurrentPage = 1, PerPage = 2, Count = 2, NumberOfPages = 1 }));
// Act
await _sut.HandleAsync(domainEvent, CancellationToken.None);
@@ -73,7 +76,7 @@ public class ScheduledJobAddedDomainEventHandlerTests
// Arrange
ScheduledJobAddedDomainEvent domainEvent = _scheduledJobAddedDomainEventFixture.Create();
Error error = Error.Failure("Database.Error", "Failed to get the scheduled jobs");
_mockScheduledJobRepository.GetAllAsync(Arg.Any<CancellationToken>()).Returns(error);
_mockScheduledJobRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>()).Returns(error);
// Act
EventualConsistencyException exception = await Assert.ThrowsAsync<EventualConsistencyException>(
@@ -2,6 +2,8 @@
using Lumina.Application.Common.DataAccess.Entities.Scheduling;
using Lumina.Application.Common.DataAccess.Repositories.Scheduling;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Common.Infrastructure.Scheduling;
using Lumina.Application.Core.Scheduling.Events;
using Lumina.Application.Core.Scheduling.Notifications;
@@ -39,12 +41,13 @@ public class ScheduledJobCycleStartedDomainEventHandlerTests
public ScheduledJobCycleStartedDomainEventHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockScheduledJobNotifier = Substitute.For<IScheduledJobNotifier>();
_mockScheduledJobScheduler = Substitute.For<IScheduledJobScheduler>();
_mockScheduledJobRepository = Substitute.For<IScheduledJobRepository>();
_mockUnitOfWork.ScheduledJobRepository.Returns(_mockScheduledJobRepository);
_mockScheduledJobRepository.GetAllAsync(Arg.Any<CancellationToken>()).Returns(Result.From<IEnumerable<ScheduledJobEntity>>([]));
_mockScheduledJobRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From(new PaginatedResultDto<ScheduledJobEntity> { Data = [], CurrentPage = 1, PerPage = 0, Count = 0, NumberOfPages = 1 }));
_sut = new ScheduledJobCycleStartedDomainEventHandler(_mockScheduledJobNotifier, _mockScheduledJobScheduler, _mockUnitOfWork);
}
@@ -69,7 +72,7 @@ public class ScheduledJobCycleStartedDomainEventHandlerTests
// Arrange
ScheduledJobCycleStartedDomainEvent domainEvent = _scheduledJobCycleStartedDomainEventFixture.Create();
Error error = Error.Failure("Database.Error", "Failed to get the scheduled jobs");
_mockScheduledJobRepository.GetAllAsync(Arg.Any<CancellationToken>()).Returns(error);
_mockScheduledJobRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>()).Returns(error);
// Act
EventualConsistencyException exception = await Assert.ThrowsAsync<EventualConsistencyException>(
@@ -2,6 +2,8 @@
using Lumina.Application.Common.DataAccess.Entities.Scheduling;
using Lumina.Application.Common.DataAccess.Repositories.Scheduling;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Core.Scheduling.Events;
using Lumina.Application.Core.Scheduling.Notifications;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Scheduling;
@@ -39,11 +41,12 @@ public class ScheduledJobCycleStoppedDomainEventHandlerTests
public ScheduledJobCycleStoppedDomainEventHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockScheduledJobNotifier = Substitute.For<IScheduledJobNotifier>();
_mockScheduledJobRepository = Substitute.For<IScheduledJobRepository>();
_mockUnitOfWork.ScheduledJobRepository.Returns(_mockScheduledJobRepository);
_mockScheduledJobRepository.GetAllAsync(Arg.Any<CancellationToken>()).Returns(Result.From<IEnumerable<ScheduledJobEntity>>([]));
_mockScheduledJobRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From(new PaginatedResultDto<ScheduledJobEntity> { Data = [], CurrentPage = 1, PerPage = 0, Count = 0, NumberOfPages = 1 }));
_sut = new ScheduledJobCycleStoppedDomainEventHandler(_mockScheduledJobNotifier, _mockUnitOfWork);
}
@@ -54,7 +57,7 @@ public class ScheduledJobCycleStoppedDomainEventHandlerTests
// Arrange
ScheduledJobCycleStoppedDomainEvent domainEvent = _scheduledJobCycleStoppedDomainEventFixture.Create();
ScheduledJobEntity stoppedJob = _scheduledJobEntityFixture.Create(name: "Stopped job", status: Lumina.Domain.SharedKernel.Common.Enums.Scheduling.ScheduledJobStatus.Added);
_mockScheduledJobRepository.GetAllAsync(Arg.Any<CancellationToken>()).Returns(Result.From<IEnumerable<ScheduledJobEntity>>([stoppedJob]));
_mockScheduledJobRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From(new PaginatedResultDto<ScheduledJobEntity> { Data = [stoppedJob], CurrentPage = 1, PerPage = 1, Count = 1, NumberOfPages = 1 }));
// Act
await _sut.HandleAsync(domainEvent, CancellationToken.None);
@@ -71,7 +74,7 @@ public class ScheduledJobCycleStoppedDomainEventHandlerTests
// Arrange
ScheduledJobCycleStoppedDomainEvent domainEvent = _scheduledJobCycleStoppedDomainEventFixture.Create();
Error error = Error.Failure("Database.Error", "Failed to get the scheduled jobs");
_mockScheduledJobRepository.GetAllAsync(Arg.Any<CancellationToken>()).Returns(error);
_mockScheduledJobRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>()).Returns(error);
// Act
EventualConsistencyException exception = await Assert.ThrowsAsync<EventualConsistencyException>(
@@ -2,6 +2,8 @@
using Lumina.Application.Common.DataAccess.Entities.Scheduling;
using Lumina.Application.Common.DataAccess.Repositories.Scheduling;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Core.Scheduling.Events;
using Lumina.Application.Core.Scheduling.Notifications;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Scheduling;
@@ -43,6 +45,7 @@ public class ScheduledJobExecutionCompletedDomainEventHandlerTests
public ScheduledJobExecutionCompletedDomainEventHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockScheduledJobNotifier = Substitute.For<IScheduledJobNotifier>();
_mockScheduledJobRepository = Substitute.For<IScheduledJobRepository>();
_mockScheduledJobExecutionRepository = Substitute.For<IScheduledJobExecutionRepository>();
@@ -51,7 +54,7 @@ public class ScheduledJobExecutionCompletedDomainEventHandlerTests
_mockUnitOfWork.ScheduledJobExecutionRepository.Returns(_mockScheduledJobExecutionRepository);
_mockScheduledJobRepository.UpdateAsync(Arg.Any<ScheduledJobEntity>(), Arg.Any<CancellationToken>()).Returns(Result.Updated);
_mockScheduledJobExecutionRepository.UpdateAsync(Arg.Any<ScheduledJobExecutionEntity>(), Arg.Any<CancellationToken>()).Returns(Result.Updated);
_mockScheduledJobRepository.GetAllAsync(Arg.Any<CancellationToken>()).Returns(Result.From<IEnumerable<ScheduledJobEntity>>([]));
_mockScheduledJobRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From(new PaginatedResultDto<ScheduledJobEntity> { Data = [], CurrentPage = 1, PerPage = 0, Count = 0, NumberOfPages = 1 }));
_sut = new ScheduledJobExecutionCompletedDomainEventHandler(_mockScheduledJobNotifier, _mockUnitOfWork);
}
@@ -68,14 +71,14 @@ public class ScheduledJobExecutionCompletedDomainEventHandlerTests
id: domainEvent.ScheduledJobId.Value,
status: ScheduledJobStatus.Running,
lastStartedOnUtc: completedOnUtc.AddMinutes(-5));
_mockScheduledJobRepository.GetByIdAsync(domainEvent.ScheduledJobId.Value, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(domainEvent.ScheduledJobId.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(scheduledJob));
ScheduledJobExecutionEntity execution = _scheduledJobExecutionEntityFixture.Create(
id: domainEvent.RunId,
scheduledJobId: scheduledJob.Id,
isCycleRun: isCycleRun,
completedOnUtc: null);
_mockScheduledJobExecutionRepository.GetByIdAsync(domainEvent.RunId, Arg.Any<CancellationToken>())
_mockScheduledJobExecutionRepository.GetByIdAsync(domainEvent.RunId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobExecutionEntity?>(execution));
// Act
@@ -100,7 +103,7 @@ public class ScheduledJobExecutionCompletedDomainEventHandlerTests
{
// Arrange
ScheduledJobExecutionCompletedDomainEvent domainEvent = _scheduledJobExecutionCompletedDomainEventFixture.Create(isCycleRun: true);
_mockScheduledJobRepository.GetByIdAsync(domainEvent.ScheduledJobId.Value, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(domainEvent.ScheduledJobId.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(null));
// Act
@@ -109,7 +112,7 @@ public class ScheduledJobExecutionCompletedDomainEventHandlerTests
// Assert
Assert.Equal(Errors.Scheduling.ScheduledJobNotFound, exception.EventualConsistencyError);
await _mockScheduledJobExecutionRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockScheduledJobExecutionRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -118,7 +121,7 @@ public class ScheduledJobExecutionCompletedDomainEventHandlerTests
// Arrange
ScheduledJobExecutionCompletedDomainEvent domainEvent = _scheduledJobExecutionCompletedDomainEventFixture.Create(isCycleRun: true);
ScheduledJobEntity scheduledJob = _scheduledJobEntityFixture.Create(id: domainEvent.ScheduledJobId.Value, status: ScheduledJobStatus.Running);
_mockScheduledJobRepository.GetByIdAsync(domainEvent.ScheduledJobId.Value, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(domainEvent.ScheduledJobId.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(scheduledJob));
Error error = Error.Failure("Database.Error", "Failed to update the scheduled job");
_mockScheduledJobRepository.UpdateAsync(Arg.Any<ScheduledJobEntity>(), Arg.Any<CancellationToken>()).Returns(error);
@@ -129,7 +132,7 @@ public class ScheduledJobExecutionCompletedDomainEventHandlerTests
// Assert
Assert.Equal(error, exception.EventualConsistencyError);
await _mockScheduledJobExecutionRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockScheduledJobExecutionRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -138,9 +141,9 @@ public class ScheduledJobExecutionCompletedDomainEventHandlerTests
// Arrange
ScheduledJobExecutionCompletedDomainEvent domainEvent = _scheduledJobExecutionCompletedDomainEventFixture.Create(isCycleRun: true);
ScheduledJobEntity scheduledJob = _scheduledJobEntityFixture.Create(id: domainEvent.ScheduledJobId.Value, status: ScheduledJobStatus.Running);
_mockScheduledJobRepository.GetByIdAsync(domainEvent.ScheduledJobId.Value, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(domainEvent.ScheduledJobId.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(scheduledJob));
_mockScheduledJobExecutionRepository.GetByIdAsync(domainEvent.RunId, Arg.Any<CancellationToken>())
_mockScheduledJobExecutionRepository.GetByIdAsync(domainEvent.RunId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobExecutionEntity?>(null));
// Act
@@ -158,10 +161,10 @@ public class ScheduledJobExecutionCompletedDomainEventHandlerTests
// Arrange
ScheduledJobExecutionCompletedDomainEvent domainEvent = _scheduledJobExecutionCompletedDomainEventFixture.Create(isCycleRun: true);
ScheduledJobEntity scheduledJob = _scheduledJobEntityFixture.Create(id: domainEvent.ScheduledJobId.Value, status: ScheduledJobStatus.Running);
_mockScheduledJobRepository.GetByIdAsync(domainEvent.ScheduledJobId.Value, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(domainEvent.ScheduledJobId.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(scheduledJob));
ScheduledJobExecutionEntity execution = _scheduledJobExecutionEntityFixture.Create(id: domainEvent.RunId);
_mockScheduledJobExecutionRepository.GetByIdAsync(domainEvent.RunId, Arg.Any<CancellationToken>())
_mockScheduledJobExecutionRepository.GetByIdAsync(domainEvent.RunId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobExecutionEntity?>(execution));
Error error = Error.Failure("Database.Error", "Failed to update the execution");
_mockScheduledJobExecutionRepository.UpdateAsync(Arg.Any<ScheduledJobExecutionEntity>(), Arg.Any<CancellationToken>()).Returns(error);
@@ -181,13 +184,13 @@ public class ScheduledJobExecutionCompletedDomainEventHandlerTests
// Arrange
ScheduledJobExecutionCompletedDomainEvent domainEvent = _scheduledJobExecutionCompletedDomainEventFixture.Create(isCycleRun: true);
ScheduledJobEntity scheduledJob = _scheduledJobEntityFixture.Create(id: domainEvent.ScheduledJobId.Value, status: ScheduledJobStatus.Running);
_mockScheduledJobRepository.GetByIdAsync(domainEvent.ScheduledJobId.Value, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(domainEvent.ScheduledJobId.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(scheduledJob));
ScheduledJobExecutionEntity execution = _scheduledJobExecutionEntityFixture.Create(id: domainEvent.RunId);
_mockScheduledJobExecutionRepository.GetByIdAsync(domainEvent.RunId, Arg.Any<CancellationToken>())
_mockScheduledJobExecutionRepository.GetByIdAsync(domainEvent.RunId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobExecutionEntity?>(execution));
Error error = Error.Failure("Database.Error", "Failed to get the scheduled jobs");
_mockScheduledJobRepository.GetAllAsync(Arg.Any<CancellationToken>()).Returns(error);
_mockScheduledJobRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>()).Returns(error);
// Act
EventualConsistencyException exception = await Assert.ThrowsAsync<EventualConsistencyException>(
@@ -2,6 +2,8 @@
using Lumina.Application.Common.DataAccess.Entities.Scheduling;
using Lumina.Application.Common.DataAccess.Repositories.Scheduling;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Core.Scheduling.Events;
using Lumina.Application.Core.Scheduling.Notifications;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Scheduling;
@@ -44,6 +46,7 @@ public class ScheduledJobExecutionStartedDomainEventHandlerTests
public ScheduledJobExecutionStartedDomainEventHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockScheduledJobNotifier = Substitute.For<IScheduledJobNotifier>();
_mockScheduledJobRepository = Substitute.For<IScheduledJobRepository>();
_mockScheduledJobExecutionRepository = Substitute.For<IScheduledJobExecutionRepository>();
@@ -52,7 +55,7 @@ public class ScheduledJobExecutionStartedDomainEventHandlerTests
_mockUnitOfWork.ScheduledJobExecutionRepository.Returns(_mockScheduledJobExecutionRepository);
_mockScheduledJobRepository.UpdateAsync(Arg.Any<ScheduledJobEntity>(), Arg.Any<CancellationToken>()).Returns(Result.Updated);
_mockScheduledJobExecutionRepository.InsertAsync(Arg.Any<ScheduledJobExecutionEntity>(), Arg.Any<CancellationToken>()).Returns(Result.Created);
_mockScheduledJobRepository.GetAllAsync(Arg.Any<CancellationToken>()).Returns(Result.From<IEnumerable<ScheduledJobEntity>>([]));
_mockScheduledJobRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From(new PaginatedResultDto<ScheduledJobEntity> { Data = [], CurrentPage = 1, PerPage = 0, Count = 0, NumberOfPages = 1 }));
_sut = new ScheduledJobExecutionStartedDomainEventHandler(_mockScheduledJobNotifier, _mockUnitOfWork);
}
@@ -68,7 +71,7 @@ public class ScheduledJobExecutionStartedDomainEventHandlerTests
taskType: domainEvent.TaskType,
scheduleType: ScheduleType.WithIntervalInMinutes,
status: ScheduledJobStatus.Active);
_mockScheduledJobRepository.GetByIdAsync(domainEvent.ScheduledJobId.Value, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(domainEvent.ScheduledJobId.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(scheduledJob));
// Act
@@ -100,7 +103,7 @@ public class ScheduledJobExecutionStartedDomainEventHandlerTests
// Arrange
ScheduledJobExecutionStartedDomainEvent domainEvent = _scheduledJobExecutionStartedDomainEventFixture.Create();
Error error = Error.Failure("Database.Error", "Failed to get the scheduled job");
_mockScheduledJobRepository.GetByIdAsync(domainEvent.ScheduledJobId.Value, Arg.Any<CancellationToken>()).Returns(error);
_mockScheduledJobRepository.GetByIdAsync(domainEvent.ScheduledJobId.Value, cancellationToken: Arg.Any<CancellationToken>()).Returns(error);
// Act
EventualConsistencyException exception = await Assert.ThrowsAsync<EventualConsistencyException>(
@@ -116,7 +119,7 @@ public class ScheduledJobExecutionStartedDomainEventHandlerTests
{
// Arrange
ScheduledJobExecutionStartedDomainEvent domainEvent = _scheduledJobExecutionStartedDomainEventFixture.Create();
_mockScheduledJobRepository.GetByIdAsync(domainEvent.ScheduledJobId.Value, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(domainEvent.ScheduledJobId.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(null));
// Act
@@ -135,7 +138,7 @@ public class ScheduledJobExecutionStartedDomainEventHandlerTests
ScheduledJobEntity scheduledJob = _scheduledJobEntityFixture.Create(
id: domainEvent.ScheduledJobId.Value,
status: ScheduledJobStatus.Active);
_mockScheduledJobRepository.GetByIdAsync(domainEvent.ScheduledJobId.Value, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(domainEvent.ScheduledJobId.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(scheduledJob));
Error error = Error.Failure("Database.Error", "Failed to update the scheduled job");
_mockScheduledJobRepository.UpdateAsync(Arg.Any<ScheduledJobEntity>(), Arg.Any<CancellationToken>()).Returns(error);
@@ -155,7 +158,7 @@ public class ScheduledJobExecutionStartedDomainEventHandlerTests
// Arrange
ScheduledJobExecutionStartedDomainEvent domainEvent = _scheduledJobExecutionStartedDomainEventFixture.Create();
ScheduledJobEntity scheduledJob = _scheduledJobEntityFixture.Create(id: domainEvent.ScheduledJobId.Value);
_mockScheduledJobRepository.GetByIdAsync(domainEvent.ScheduledJobId.Value, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(domainEvent.ScheduledJobId.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(scheduledJob));
Error error = Error.Failure("Database.Error", "Failed to insert the execution");
_mockScheduledJobExecutionRepository.InsertAsync(Arg.Any<ScheduledJobExecutionEntity>(), Arg.Any<CancellationToken>()).Returns(error);
@@ -175,10 +178,10 @@ public class ScheduledJobExecutionStartedDomainEventHandlerTests
// Arrange
ScheduledJobExecutionStartedDomainEvent domainEvent = _scheduledJobExecutionStartedDomainEventFixture.Create();
ScheduledJobEntity scheduledJob = _scheduledJobEntityFixture.Create(id: domainEvent.ScheduledJobId.Value);
_mockScheduledJobRepository.GetByIdAsync(domainEvent.ScheduledJobId.Value, Arg.Any<CancellationToken>())
_mockScheduledJobRepository.GetByIdAsync(domainEvent.ScheduledJobId.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobEntity?>(scheduledJob));
Error error = Error.Failure("Database.Error", "Failed to get the scheduled jobs");
_mockScheduledJobRepository.GetAllAsync(Arg.Any<CancellationToken>()).Returns(error);
_mockScheduledJobRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>()).Returns(error);
// Act
EventualConsistencyException exception = await Assert.ThrowsAsync<EventualConsistencyException>(
@@ -2,6 +2,8 @@
using Lumina.Application.Common.DataAccess.Entities.Scheduling;
using Lumina.Application.Common.DataAccess.Repositories.Scheduling;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Core.Scheduling.Events;
using Lumina.Application.Core.Scheduling.Notifications;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Scheduling;
@@ -40,6 +42,7 @@ public class ScheduledJobExecutionStoppedDomainEventHandlerTests
public ScheduledJobExecutionStoppedDomainEventHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockScheduledJobNotifier = Substitute.For<IScheduledJobNotifier>();
_mockScheduledJobRepository = Substitute.For<IScheduledJobRepository>();
_mockScheduledJobExecutionRepository = Substitute.For<IScheduledJobExecutionRepository>();
@@ -47,7 +50,7 @@ public class ScheduledJobExecutionStoppedDomainEventHandlerTests
_mockUnitOfWork.ScheduledJobRepository.Returns(_mockScheduledJobRepository);
_mockUnitOfWork.ScheduledJobExecutionRepository.Returns(_mockScheduledJobExecutionRepository);
_mockScheduledJobExecutionRepository.UpdateAsync(Arg.Any<ScheduledJobExecutionEntity>(), Arg.Any<CancellationToken>()).Returns(Result.Updated);
_mockScheduledJobRepository.GetAllAsync(Arg.Any<CancellationToken>()).Returns(Result.From<IEnumerable<ScheduledJobEntity>>([]));
_mockScheduledJobRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From(new PaginatedResultDto<ScheduledJobEntity> { Data = [], CurrentPage = 1, PerPage = 0, Count = 0, NumberOfPages = 1 }));
_sut = new ScheduledJobExecutionStoppedDomainEventHandler(_mockScheduledJobNotifier, _mockUnitOfWork);
}
@@ -140,7 +143,7 @@ public class ScheduledJobExecutionStoppedDomainEventHandlerTests
_mockScheduledJobExecutionRepository.GetOpenByScheduledJobIdAsync(domainEvent.ScheduledJobId.Value, Arg.Any<CancellationToken>())
.Returns(Result.From<ScheduledJobExecutionEntity?>(null));
Error error = Error.Failure("Database.Error", "Failed to get the scheduled jobs");
_mockScheduledJobRepository.GetAllAsync(Arg.Any<CancellationToken>()).Returns(error);
_mockScheduledJobRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>()).Returns(error);
// Act
EventualConsistencyException exception = await Assert.ThrowsAsync<EventualConsistencyException>(
@@ -2,6 +2,8 @@
using Lumina.Application.Common.DataAccess.Entities.Scheduling;
using Lumina.Application.Common.DataAccess.Repositories.Scheduling;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Core.Scheduling.Events;
using Lumina.Application.Core.Scheduling.Notifications;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Scheduling;
@@ -39,11 +41,12 @@ public class ScheduledJobRemovedDomainEventHandlerTests
public ScheduledJobRemovedDomainEventHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockScheduledJobNotifier = Substitute.For<IScheduledJobNotifier>();
_mockScheduledJobRepository = Substitute.For<IScheduledJobRepository>();
_mockUnitOfWork.ScheduledJobRepository.Returns(_mockScheduledJobRepository);
_mockScheduledJobRepository.GetAllAsync(Arg.Any<CancellationToken>()).Returns(Result.From<IEnumerable<ScheduledJobEntity>>([]));
_mockScheduledJobRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From(new PaginatedResultDto<ScheduledJobEntity> { Data = [], CurrentPage = 1, PerPage = 0, Count = 0, NumberOfPages = 1 }));
_sut = new ScheduledJobRemovedDomainEventHandler(_mockScheduledJobNotifier, _mockUnitOfWork);
}
@@ -54,7 +57,7 @@ public class ScheduledJobRemovedDomainEventHandlerTests
// Arrange
ScheduledJobRemovedDomainEvent domainEvent = _scheduledJobRemovedDomainEventFixture.Create();
ScheduledJobEntity remainingJob = _scheduledJobEntityFixture.Create(name: "Remaining job");
_mockScheduledJobRepository.GetAllAsync(Arg.Any<CancellationToken>()).Returns(Result.From<IEnumerable<ScheduledJobEntity>>([remainingJob]));
_mockScheduledJobRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>()).Returns(Result.From(new PaginatedResultDto<ScheduledJobEntity> { Data = [remainingJob], CurrentPage = 1, PerPage = 1, Count = 1, NumberOfPages = 1 }));
// Act
await _sut.HandleAsync(domainEvent, CancellationToken.None);
@@ -71,7 +74,7 @@ public class ScheduledJobRemovedDomainEventHandlerTests
// Arrange
ScheduledJobRemovedDomainEvent domainEvent = _scheduledJobRemovedDomainEventFixture.Create();
Error error = Error.Failure("Database.Error", "Failed to get the scheduled jobs");
_mockScheduledJobRepository.GetAllAsync(Arg.Any<CancellationToken>()).Returns(error);
_mockScheduledJobRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>()).Returns(error);
// Act
EventualConsistencyException exception = await Assert.ThrowsAsync<EventualConsistencyException>(
@@ -42,6 +42,7 @@ public class GetScheduledJobHistoryQueryHandlerTests
public GetScheduledJobHistoryQueryHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
_mockScheduledJobExecutionRepository = Substitute.For<IScheduledJobExecutionRepository>();
@@ -2,6 +2,8 @@
using Lumina.Application.Common.DataAccess.Entities.Scheduling;
using Lumina.Application.Common.DataAccess.Repositories.Scheduling;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Common.Infrastructure.Authentication;
using Lumina.Application.Common.Infrastructure.Authorization;
using Lumina.Application.Core.Scheduling.Queries.GetScheduledJobs;
@@ -40,6 +42,7 @@ public class GetScheduledJobsQueryHandlerTests
public GetScheduledJobsQueryHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
_mockScheduledJobRepository = Substitute.For<IScheduledJobRepository>();
@@ -57,8 +60,8 @@ public class GetScheduledJobsQueryHandlerTests
// Arrange
ScheduledJobEntity scheduledJob1 = _scheduledJobEntityFixture.Create(name: "Job 1");
ScheduledJobEntity scheduledJob2 = _scheduledJobEntityFixture.Create(name: "Job 2");
_mockScheduledJobRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From<IEnumerable<ScheduledJobEntity>>([scheduledJob1, scheduledJob2]));
_mockScheduledJobRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<ScheduledJobEntity> { Data = [scheduledJob1, scheduledJob2], CurrentPage = 1, PerPage = 2, Count = 2, NumberOfPages = 1 }));
// Act
Result<IEnumerable<ScheduledJobResponse>> result = await _sut.HandleAsync(new GetScheduledJobsQuery(), CancellationToken.None);
@@ -74,8 +77,8 @@ public class GetScheduledJobsQueryHandlerTests
public async Task HandleAsync_WhenNoScheduledJobsExist_ShouldReturnEmptyCollection()
{
// Arrange
_mockScheduledJobRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From<IEnumerable<ScheduledJobEntity>>([]));
_mockScheduledJobRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<ScheduledJobEntity> { Data = [], CurrentPage = 1, PerPage = 0, Count = 0, NumberOfPages = 1 }));
// Act
Result<IEnumerable<ScheduledJobResponse>> result = await _sut.HandleAsync(new GetScheduledJobsQuery(), CancellationToken.None);
@@ -97,7 +100,7 @@ public class GetScheduledJobsQueryHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
await _mockScheduledJobRepository.DidNotReceive().GetAllAsync(Arg.Any<CancellationToken>());
await _mockScheduledJobRepository.DidNotReceive().GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -112,7 +115,7 @@ public class GetScheduledJobsQueryHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
await _mockScheduledJobRepository.DidNotReceive().GetAllAsync(Arg.Any<CancellationToken>());
await _mockScheduledJobRepository.DidNotReceive().GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -120,7 +123,7 @@ public class GetScheduledJobsQueryHandlerTests
{
// Arrange
Error error = Error.Failure("Database.Error", "Failed to get the scheduled jobs");
_mockScheduledJobRepository.GetAllAsync(Arg.Any<CancellationToken>()).Returns(error);
_mockScheduledJobRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>()).Returns(error);
// Act
Result<IEnumerable<ScheduledJobResponse>> result = await _sut.HandleAsync(new GetScheduledJobsQuery(), CancellationToken.None);
@@ -42,6 +42,7 @@ public class GetSchedulerDisplayPreferencesQueryHandlerTests
public GetSchedulerDisplayPreferencesQueryHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
_mockSchedulerDisplayPreferencesRepository = Substitute.For<ISchedulerDisplayPreferencesRepository>();
@@ -2,6 +2,8 @@
using Lumina.Application.Common.DataAccess.Entities.Themes;
using Lumina.Application.Common.DataAccess.Repositories.Themes;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Common.Infrastructure.Authentication;
using Lumina.Application.Common.Infrastructure.Authorization;
using Lumina.Application.Common.Infrastructure.Themes;
@@ -47,6 +49,7 @@ public class DeleteThemeCommandHandlerTests
public DeleteThemeCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockThemeService = Substitute.For<IThemeService>();
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
@@ -185,7 +188,7 @@ public class DeleteThemeCommandHandlerTests
Error error = Error.Failure("Database.Error", "Failed to get all themes");
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
.Returns(Result.From<ThemeEntity?>(theme));
_mockThemeRepository.GetAllAsync(Arg.Any<CancellationToken>())
_mockThemeRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(error);
// Act
@@ -206,8 +209,8 @@ public class DeleteThemeCommandHandlerTests
ThemeEntity userTheme = _themeEntityFixture.Create(installSource: ThemeInstallSource.Uploaded, isDeleted: false);
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
.Returns(Result.From<ThemeEntity?>(theme));
_mockThemeRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From<IEnumerable<ThemeEntity>>([theme, userTheme]));
_mockThemeRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<ThemeEntity> { Data = [theme, userTheme], CurrentPage = 1, PerPage = 2, Count = 2, NumberOfPages = 1 }));
// Act
Result<Success> result = await _sut.HandleAsync(command, CancellationToken.None);
@@ -228,8 +231,8 @@ public class DeleteThemeCommandHandlerTests
ThemeEntity theme = _themeEntityFixture.Create(themeId: command.ThemeId, installSource: ThemeInstallSource.Uploaded, isCurrent: true, includeIsCurrent: true, isDeleted: false);
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
.Returns(Result.From<ThemeEntity?>(theme));
_mockThemeRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From<IEnumerable<ThemeEntity>>([theme]));
_mockThemeRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<ThemeEntity> { Data = [theme], CurrentPage = 1, PerPage = 1, Count = 1, NumberOfPages = 1 }));
// Act
Result<Success> result = await _sut.HandleAsync(command, CancellationToken.None);
@@ -251,8 +254,8 @@ public class DeleteThemeCommandHandlerTests
ThemeEntity otherBundledTheme = _themeEntityFixture.Create(installSource: ThemeInstallSource.Bundled, isDeleted: false);
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
.Returns(Result.From<ThemeEntity?>(theme));
_mockThemeRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From<IEnumerable<ThemeEntity>>([theme, otherBundledTheme]));
_mockThemeRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<ThemeEntity> { Data = [theme, otherBundledTheme], CurrentPage = 1, PerPage = 2, Count = 2, NumberOfPages = 1 }));
_mockThemeRepository.UpdateAsync(Arg.Any<ThemeEntity>(), Arg.Any<CancellationToken>())
.Returns(Result.Updated);
@@ -278,8 +281,8 @@ public class DeleteThemeCommandHandlerTests
ThemeEntity bundledTheme = _themeEntityFixture.Create(installSource: ThemeInstallSource.Bundled, isDeleted: false);
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
.Returns(Result.From<ThemeEntity?>(theme));
_mockThemeRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From<IEnumerable<ThemeEntity>>([theme, bundledTheme]));
_mockThemeRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<ThemeEntity> { Data = [theme, bundledTheme], CurrentPage = 1, PerPage = 2, Count = 2, NumberOfPages = 1 }));
_mockThemeRepository.DeleteByIdAsync(theme.Id, Arg.Any<CancellationToken>())
.Returns(Result.Deleted);
@@ -304,8 +307,8 @@ public class DeleteThemeCommandHandlerTests
Error error = Error.Failure("Database.Error", "Failed to delete theme");
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
.Returns(Result.From<ThemeEntity?>(theme));
_mockThemeRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From<IEnumerable<ThemeEntity>>([theme, bundledTheme]));
_mockThemeRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<ThemeEntity> { Data = [theme, bundledTheme], CurrentPage = 1, PerPage = 2, Count = 2, NumberOfPages = 1 }));
_mockThemeRepository.DeleteByIdAsync(theme.Id, Arg.Any<CancellationToken>())
.Returns(error);
@@ -329,8 +332,8 @@ public class DeleteThemeCommandHandlerTests
_mockThemeService.DefaultThemeId.Returns(replacementTheme.ThemeId);
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
.Returns(Result.From<ThemeEntity?>(theme));
_mockThemeRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From<IEnumerable<ThemeEntity>>([theme, replacementTheme]));
_mockThemeRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<ThemeEntity> { Data = [theme, replacementTheme], CurrentPage = 1, PerPage = 2, Count = 2, NumberOfPages = 1 }));
_mockThemeRepository.UpdateAsync(Arg.Any<ThemeEntity>(), Arg.Any<CancellationToken>())
.Returns(Result.Updated);
@@ -360,8 +363,8 @@ public class DeleteThemeCommandHandlerTests
_mockThemeService.DefaultThemeId.Returns("no-such-default");
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
.Returns(Result.From<ThemeEntity?>(theme));
_mockThemeRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From<IEnumerable<ThemeEntity>>([theme, betaTheme, alphaTheme]));
_mockThemeRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<ThemeEntity> { Data = [theme, betaTheme, alphaTheme], CurrentPage = 1, PerPage = 3, Count = 3, NumberOfPages = 1 }));
_mockThemeRepository.UpdateAsync(Arg.Any<ThemeEntity>(), Arg.Any<CancellationToken>())
.Returns(Result.Updated);
@@ -388,8 +391,8 @@ public class DeleteThemeCommandHandlerTests
Error error = Error.Failure("Theme.DeleteFailed", "Failed to delete theme files");
_mockThemeRepository.GetByThemeIdAsync(command.ThemeId!, Arg.Any<CancellationToken>())
.Returns(Result.From<ThemeEntity?>(theme));
_mockThemeRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From<IEnumerable<ThemeEntity>>([theme, bundledTheme]));
_mockThemeRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<ThemeEntity> { Data = [theme, bundledTheme], CurrentPage = 1, PerPage = 2, Count = 2, NumberOfPages = 1 }));
_mockThemeRepository.DeleteByIdAsync(theme.Id, Arg.Any<CancellationToken>())
.Returns(Result.Deleted);
_mockThemeService.DeleteAsync(theme.ThemeId, Arg.Any<CancellationToken>())
@@ -51,6 +51,7 @@ public class InstallThemeCommandHandlerTests
public InstallThemeCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockThemeService = Substitute.For<IThemeService>();
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
@@ -45,6 +45,7 @@ public class RestoreThemeCommandHandlerTests
public RestoreThemeCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockThemeService = Substitute.For<IThemeService>();
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
@@ -44,6 +44,7 @@ public class SetCurrentThemeCommandHandlerTests
public SetCurrentThemeCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
_mockValidator = Substitute.For<IValidator<SetCurrentThemeCommand>>();
@@ -35,6 +35,7 @@ public class GetCurrentThemeQueryHandlerTests
public GetCurrentThemeQueryHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockThemeRepository = Substitute.For<IThemeRepository>();
_mockUnitOfWork.ThemeRepository.Returns(_mockThemeRepository);
_sut = new GetCurrentThemeQueryHandler(_mockUnitOfWork);
@@ -41,6 +41,7 @@ public class GetThemeArchiveQueryHandlerTests
public GetThemeArchiveQueryHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockThemeService = Substitute.For<IThemeService>();
_mockValidator = Substitute.For<IValidator<GetThemeArchiveQuery>>();
_mockThemeRepository = Substitute.For<IThemeRepository>();
@@ -42,6 +42,7 @@ public class GetThemeAssetQueryHandlerTests
public GetThemeAssetQueryHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockThemeService = Substitute.For<IThemeService>();
_mockValidator = Substitute.For<IValidator<GetThemeAssetQuery>>();
_mockThemeRepository = Substitute.For<IThemeRepository>();
@@ -44,6 +44,7 @@ public class GetThemeTemplateQueryHandlerTests
public GetThemeTemplateQueryHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockThemeService = Substitute.For<IThemeService>();
_mockValidator = Substitute.For<IValidator<GetThemeTemplateQuery>>();
_mockThemeRepository = Substitute.For<IThemeRepository>();
@@ -2,6 +2,8 @@
using Lumina.Application.Common.DataAccess.Entities.Themes;
using Lumina.Application.Common.DataAccess.Repositories.Themes;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Core.Themes.Management.Queries.GetThemes;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Themes;
using Lumina.Application.Fixtures.Core.Themes.Management.Queries.GetThemes;
@@ -35,6 +37,7 @@ public class GetThemesQueryHandlerTests
public GetThemesQueryHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockThemeRepository = Substitute.For<IThemeRepository>();
_mockUnitOfWork.ThemeRepository.Returns(_mockThemeRepository);
_sut = new GetThemesQueryHandler(_mockUnitOfWork);
@@ -49,8 +52,8 @@ public class GetThemesQueryHandlerTests
ThemeEntity bundledLowerTheme = _themeEntityFixture.Create(themeId: "bundled-lower", name: "apple", installSource: ThemeInstallSource.Bundled, isDeleted: false);
ThemeEntity bundledUpperTheme = _themeEntityFixture.Create(themeId: "bundled-upper", name: "Banana", installSource: ThemeInstallSource.Bundled, isDeleted: false);
ThemeEntity deletedTheme = _themeEntityFixture.Create(themeId: "deleted-theme", name: "Deleted", installSource: ThemeInstallSource.Uploaded, isDeleted: true);
_mockThemeRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From<IEnumerable<ThemeEntity>>([uploadedTheme, bundledLowerTheme, bundledUpperTheme, deletedTheme]));
_mockThemeRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<ThemeEntity> { Data = [uploadedTheme, bundledLowerTheme, bundledUpperTheme, deletedTheme], CurrentPage = 1, PerPage = 4, Count = 4, NumberOfPages = 1 }));
// Act
Result<IReadOnlyList<ThemeResponse>> result = await _sut.HandleAsync(query, CancellationToken.None);
@@ -73,8 +76,8 @@ public class GetThemesQueryHandlerTests
{
// Arrange
GetThemesQuery query = _getThemesQueryFixture.Create();
_mockThemeRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From<IEnumerable<ThemeEntity>>([]));
_mockThemeRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<ThemeEntity> { Data = [], CurrentPage = 1, PerPage = 0, Count = 0, NumberOfPages = 1 }));
// Act
Result<IReadOnlyList<ThemeResponse>> result = await _sut.HandleAsync(query, CancellationToken.None);
@@ -90,7 +93,7 @@ public class GetThemesQueryHandlerTests
// Arrange
GetThemesQuery query = _getThemesQueryFixture.Create();
Error error = Error.Failure("Database.Error", "Failed to get all themes");
_mockThemeRepository.GetAllAsync(Arg.Any<CancellationToken>())
_mockThemeRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(error);
// Act
@@ -36,6 +36,7 @@ public class ChangePasswordCommandHandlerTests
public ChangePasswordCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockHashService = Substitute.For<IPasswordHashService>();
_mockUserRepository = Substitute.For<IUserRepository>();
@@ -39,6 +39,7 @@ public class RecoverPasswordCommandHandlerTests
public RecoverPasswordCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockHashService = Substitute.For<IPasswordHashService>();
_mockTotpTokenGenerator = Substitute.For<ITotpTokenGenerator>();
_mockCryptographyService = Substitute.For<ICryptographyService>();
@@ -44,6 +44,7 @@ public class RegisterUserCommandHandlerTests
public RegisterUserCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockHashService = Substitute.For<IPasswordHashService>();
_mockCryptographyService = Substitute.For<ICryptographyService>();
_mockTotpTokenGenerator = Substitute.For<ITotpTokenGenerator>();
@@ -2,6 +2,8 @@
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
using Lumina.Application.Common.DataAccess.Repositories.Users;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Common.Errors;
using Lumina.Application.Common.Infrastructure.Authentication;
using Lumina.Application.Common.Infrastructure.Authorization;
@@ -42,6 +44,7 @@ public class GetUsersQueryHandlerTests
public GetUsersQueryHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
_mockUserRepository = Substitute.For<IUserRepository>();
@@ -70,7 +73,7 @@ public class GetUsersQueryHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authorization.NotAuthorized, result.FirstError);
await _mockUserRepository.DidNotReceive().GetAllAsync(Arg.Any<CancellationToken>());
await _mockUserRepository.DidNotReceive().GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -87,7 +90,7 @@ public class GetUsersQueryHandlerTests
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authorization.NotAuthorized, result.FirstError);
await _mockAuthorizationService.DidNotReceive().IsInRoleAsync(Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
await _mockUserRepository.DidNotReceive().GetAllAsync(Arg.Any<CancellationToken>());
await _mockUserRepository.DidNotReceive().GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -99,7 +102,7 @@ public class GetUsersQueryHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
_mockUserRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(error);
// Act
@@ -115,13 +118,13 @@ public class GetUsersQueryHandlerTests
{
// Arrange
GetUsersQuery query = _getUsersQueryFixture.Create();
IEnumerable<UserEntity> users = _userEntityFixture.CreateMany(2);
List<UserEntity> users = _userEntityFixture.CreateMany(2);
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From(users));
_mockUserRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<UserEntity> { Data = users, CurrentPage = 1, PerPage = users.Count, Count = users.Count, NumberOfPages = 1 }));
// Act
Result<IEnumerable<UserResponse>> result = await _sut.HandleAsync(query, CancellationToken.None);
@@ -42,6 +42,7 @@ public class LoginUserQueryHandlerTests
public LoginUserQueryHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockHashService = Substitute.For<IPasswordHashService>();
_mockJwtTokenGenerator = Substitute.For<IJwtTokenGenerator>();
_mockTotpTokenGenerator = Substitute.For<ITotpTokenGenerator>();
@@ -4,6 +4,8 @@ using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
using Lumina.Application.Common.DataAccess.Repositories.Authorization;
using Lumina.Application.Common.DataAccess.Repositories.Users;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.DTO.Filtering;
using Lumina.Application.Common.DTO.Pagination;
using Lumina.Application.Common.Infrastructure.Authentication;
using Lumina.Application.Common.Infrastructure.Authorization;
using Lumina.Application.Common.Infrastructure.Validation;
@@ -54,6 +56,7 @@ public class UpdateUserRoleAndPermissionsCommandHandlerTests
public UpdateUserRoleAndPermissionsCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
_mockUserRepository = Substitute.For<IUserRepository>();
@@ -107,7 +110,7 @@ public class UpdateUserRoleAndPermissionsCommandHandlerTests
Assert.True(result.IsFailure);
Assert.Equal(ApplicationErrors.Authorization.NotAuthorized, result.FirstError);
await _mockAuthorizationService.DidNotReceive().IsInRoleAsync(Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
await _mockUserRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockUserRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -117,7 +120,7 @@ public class UpdateUserRoleAndPermissionsCommandHandlerTests
UpdateUserRoleAndPermissionsCommand command = _updateUserRoleAndPermissionsCommandFixture.Create();
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockUserRepository.GetByIdAsync(command.UserId, Arg.Any<CancellationToken>())
_mockUserRepository.GetByIdAsync(command.UserId, cancellationToken: Arg.Any<CancellationToken>())
.Returns((UserEntity?)null);
// Act
@@ -137,9 +140,9 @@ public class UpdateUserRoleAndPermissionsCommandHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockUserRepository.GetByIdAsync(command.UserId, Arg.Any<CancellationToken>())
_mockUserRepository.GetByIdAsync(command.UserId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(user);
_mockRoleRepository.GetByIdAsync(command.RoleId!.Value, Arg.Any<CancellationToken>())
_mockRoleRepository.GetByIdAsync(command.RoleId!.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns((RoleEntity?)null);
// Act
@@ -167,16 +170,16 @@ public class UpdateUserRoleAndPermissionsCommandHandlerTests
includeUserRole: true);
RoleEntity newRole = _roleEntityFixture.Create(roleName: "User");
IEnumerable<UserEntity> users = [user];
List<UserEntity> users = [user];
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockUserRepository.GetByIdAsync(command.UserId, Arg.Any<CancellationToken>())
_mockUserRepository.GetByIdAsync(command.UserId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(user);
_mockRoleRepository.GetByIdAsync(command.RoleId!.Value, Arg.Any<CancellationToken>())
_mockRoleRepository.GetByIdAsync(command.RoleId!.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(newRole);
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From(users));
_mockUserRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<UserEntity> { Data = users, CurrentPage = 1, PerPage = users.Count, Count = users.Count, NumberOfPages = 1 }));
// Act
Result<AuthorizationResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
@@ -199,9 +202,9 @@ public class UpdateUserRoleAndPermissionsCommandHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockUserRepository.GetByIdAsync(command.UserId, Arg.Any<CancellationToken>())
_mockUserRepository.GetByIdAsync(command.UserId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(user);
_mockRoleRepository.GetByIdAsync(command.RoleId!.Value, Arg.Any<CancellationToken>())
_mockRoleRepository.GetByIdAsync(command.RoleId!.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(role);
_mockPermissionRepository.GetByIdsAsync(command.Permissions, Arg.Any<CancellationToken>())
.Returns(Result.From(permissions));
@@ -237,9 +240,9 @@ public class UpdateUserRoleAndPermissionsCommandHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockUserRepository.GetByIdAsync(command.UserId, Arg.Any<CancellationToken>())
_mockUserRepository.GetByIdAsync(command.UserId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(user);
_mockRoleRepository.GetByIdAsync(command.RoleId!.Value, Arg.Any<CancellationToken>())
_mockRoleRepository.GetByIdAsync(command.RoleId!.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(role);
_mockPermissionRepository.GetByIdsAsync(command.Permissions, Arg.Any<CancellationToken>())
.Returns(Result.From(permissions));
@@ -276,11 +279,11 @@ public class UpdateUserRoleAndPermissionsCommandHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockUserRepository.GetByIdAsync(command.UserId, Arg.Any<CancellationToken>())
_mockUserRepository.GetByIdAsync(command.UserId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(user);
_mockRoleRepository.GetByIdAsync(command.RoleId!.Value, Arg.Any<CancellationToken>())
_mockRoleRepository.GetByIdAsync(command.RoleId!.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(_roleEntityFixture.Create(roleName: "User"));
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
_mockUserRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(error);
// Act
@@ -301,9 +304,9 @@ public class UpdateUserRoleAndPermissionsCommandHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockUserRepository.GetByIdAsync(command.UserId, Arg.Any<CancellationToken>())
_mockUserRepository.GetByIdAsync(command.UserId, cancellationToken: Arg.Any<CancellationToken>())
.Returns(user);
_mockRoleRepository.GetByIdAsync(command.RoleId!.Value, Arg.Any<CancellationToken>())
_mockRoleRepository.GetByIdAsync(command.RoleId!.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(_roleEntityFixture.Create(roleName: "User"));
_mockPermissionRepository.GetByIdsAsync(command.Permissions, Arg.Any<CancellationToken>())
.Returns(error);
@@ -47,6 +47,7 @@ public class GetUserPermissionsQueryHandlerTests
public GetUserPermissionsQueryHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
_mockUserRepository = Substitute.For<IUserRepository>();
@@ -79,7 +80,7 @@ public class GetUserPermissionsQueryHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authorization.NotAuthorized, result.FirstError);
await _mockUserRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockUserRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -96,7 +97,7 @@ public class GetUserPermissionsQueryHandlerTests
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authorization.NotAuthorized, result.FirstError);
await _mockAuthorizationService.DidNotReceive().IsInRoleAsync(Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
await _mockUserRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockUserRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -108,7 +109,7 @@ public class GetUserPermissionsQueryHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockUserRepository.GetByIdAsync(query.UserId!.Value, Arg.Any<CancellationToken>())
_mockUserRepository.GetByIdAsync(query.UserId!.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(error);
// Act
@@ -127,7 +128,7 @@ public class GetUserPermissionsQueryHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockUserRepository.GetByIdAsync(query.UserId!.Value, Arg.Any<CancellationToken>())
_mockUserRepository.GetByIdAsync(query.UserId!.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns((UserEntity?)null);
// Act
@@ -157,7 +158,7 @@ public class GetUserPermissionsQueryHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockUserRepository.GetByIdAsync(query.UserId!.Value, Arg.Any<CancellationToken>())
_mockUserRepository.GetByIdAsync(query.UserId!.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(user);
// Act
@@ -44,6 +44,7 @@ public class GetUserRoleQueryHandlerTests
public GetUserRoleQueryHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
_mockUserRepository = Substitute.For<IUserRepository>();
@@ -76,7 +77,7 @@ public class GetUserRoleQueryHandlerTests
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authorization.NotAuthorized, result.FirstError);
await _mockUserRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockUserRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -93,7 +94,7 @@ public class GetUserRoleQueryHandlerTests
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authorization.NotAuthorized, result.FirstError);
await _mockAuthorizationService.DidNotReceive().IsInRoleAsync(Arg.Any<Guid>(), Arg.Any<string>(), Arg.Any<CancellationToken>());
await _mockUserRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
await _mockUserRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), cancellationToken: Arg.Any<CancellationToken>());
}
[Fact]
@@ -105,7 +106,7 @@ public class GetUserRoleQueryHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockUserRepository.GetByIdAsync(query.UserId!.Value, Arg.Any<CancellationToken>())
_mockUserRepository.GetByIdAsync(query.UserId!.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(error);
// Act
@@ -124,7 +125,7 @@ public class GetUserRoleQueryHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockUserRepository.GetByIdAsync(query.UserId!.Value, Arg.Any<CancellationToken>())
_mockUserRepository.GetByIdAsync(query.UserId!.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns((UserEntity?)null);
// Act
@@ -144,7 +145,7 @@ public class GetUserRoleQueryHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockUserRepository.GetByIdAsync(query.UserId!.Value, Arg.Any<CancellationToken>())
_mockUserRepository.GetByIdAsync(query.UserId!.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(user);
// Act
@@ -171,7 +172,7 @@ public class GetUserRoleQueryHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockUserRepository.GetByIdAsync(query.UserId!.Value, Arg.Any<CancellationToken>())
_mockUserRepository.GetByIdAsync(query.UserId!.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(user);
// Act
@@ -40,6 +40,7 @@ public class UpdateUserSettingsCommandHandlerTests
public UpdateUserSettingsCommandHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
_mockUserSettingsRepository = Substitute.For<IUserSettingsRepository>();
@@ -38,6 +38,7 @@ public class GetUserSettingsQueryHandlerTests
public GetUserSettingsQueryHandlerTests()
{
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
_mockUnitOfWork.SaveChangesAsync(Arg.Any<CancellationToken>()).Returns(Result.Success);
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
_mockUserSettingsRepository = Substitute.For<IUserSettingsRepository>();