feat(medios,secciones): application layer + handlers TDD — ADM-001 B3+B4

- IMedioRepository, ISeccionRepository interfaces
- MediosQuery, SeccionesQuery common records
- TipoSeccion static AllowedTipos helper
- Medios: 6 use cases (Create/Update/Deactivate/Reactivate/List/GetById) with validators, handlers and DTOs
- Secciones: 6 use cases mirroring Medios; Create validates MedioId active via IMedioRepository
- 52 unit tests (xUnit + NSubstitute) all green; audit LogAsync asserted per mutating handler
- DI registrations for all 12 handlers and validators auto-scanned via AddValidatorsFromAssemblyContaining
This commit is contained in:
2026-04-16 18:53:57 -03:00
parent bb98dbf217
commit f672de78ce
56 changed files with 1844 additions and 0 deletions

View File

@@ -0,0 +1,113 @@
using NSubstitute;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Audit;
using SIGCM2.Application.Secciones.Create;
using SIGCM2.Domain.Entities;
using SIGCM2.Domain.Exceptions;
namespace SIGCM2.Application.Tests.Secciones.Create;
public class CreateSeccionCommandHandlerTests
{
private readonly ISeccionRepository _repo = Substitute.For<ISeccionRepository>();
private readonly IMedioRepository _medioRepo = Substitute.For<IMedioRepository>();
private readonly IAuditLogger _audit = Substitute.For<IAuditLogger>();
private readonly CreateSeccionCommandHandler _handler;
private static Medio MakeMedio(int id = 1, bool activo = true) =>
new(id, "COD" + id, "Medio " + id, TipoMedio.Diario, null, activo, DateTime.UtcNow, null);
private static CreateSeccionCommand ValidCommand() => new(
MedioId: 1,
Codigo: "CLASIFICADOS_LUNES",
Nombre: "Clasificados Lunes",
Tipo: "clasificados");
public CreateSeccionCommandHandlerTests()
{
_handler = new CreateSeccionCommandHandler(_repo, _medioRepo, _audit);
_medioRepo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns(MakeMedio(1));
_repo.ExistsByCodigoInMedioAsync(Arg.Any<int>(), Arg.Any<string>(), Arg.Any<CancellationToken>()).Returns(false);
_repo.AddAsync(Arg.Any<Seccion>(), Arg.Any<CancellationToken>()).Returns(10);
}
// ── medio not found → throws ─────────────────────────────────────────────
[Fact]
public async Task Handle_MedioNotFound_ThrowsMedioNotFoundException()
{
_medioRepo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns((Medio?)null);
await Assert.ThrowsAsync<MedioNotFoundException>(
() => _handler.Handle(ValidCommand()));
}
// ── duplicate codigo in medio → throws ──────────────────────────────────
[Fact]
public async Task Handle_DuplicateCodigoInMedio_ThrowsSeccionCodigoDuplicadoEnMedioException()
{
_repo.ExistsByCodigoInMedioAsync(1, "CLASIFICADOS_LUNES", Arg.Any<CancellationToken>()).Returns(true);
await Assert.ThrowsAsync<SeccionCodigoDuplicadoEnMedioException>(
() => _handler.Handle(ValidCommand()));
}
[Fact]
public async Task Handle_DuplicateCodigo_DoesNotCallAddAsync()
{
_repo.ExistsByCodigoInMedioAsync(Arg.Any<int>(), Arg.Any<string>(), Arg.Any<CancellationToken>()).Returns(true);
try { await _handler.Handle(ValidCommand()); } catch (SeccionCodigoDuplicadoEnMedioException) { }
await _repo.DidNotReceive().AddAsync(Arg.Any<Seccion>(), Arg.Any<CancellationToken>());
}
// ── happy path ───────────────────────────────────────────────────────────
[Fact]
public async Task Handle_HappyPath_ReturnsDtoWithIdFromRepository()
{
_repo.AddAsync(Arg.Any<Seccion>(), Arg.Any<CancellationToken>()).Returns(10);
var result = await _handler.Handle(ValidCommand());
Assert.Equal(10, result.Id);
}
[Fact]
public async Task Handle_HappyPath_DtoContainsCorrectFields()
{
var result = await _handler.Handle(ValidCommand());
Assert.Equal(1, result.MedioId);
Assert.Equal("CLASIFICADOS_LUNES", result.Codigo);
Assert.Equal("Clasificados Lunes", result.Nombre);
Assert.Equal("clasificados", result.Tipo);
Assert.True(result.Activo);
}
[Fact]
public async Task Handle_HappyPath_CallsAuditWithCreateAction()
{
_repo.AddAsync(Arg.Any<Seccion>(), Arg.Any<CancellationToken>()).Returns(10);
await _handler.Handle(ValidCommand());
await _audit.Received(1).LogAsync(
action: "seccion.create",
targetType: "Seccion",
targetId: "10",
metadata: Arg.Any<object?>(),
ct: Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_InactiveMedio_ThrowsMedioNotFoundException()
{
_medioRepo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns(MakeMedio(1, false));
await Assert.ThrowsAsync<MedioNotFoundException>(
() => _handler.Handle(ValidCommand()));
}
}

View File

@@ -0,0 +1,81 @@
using NSubstitute;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Audit;
using SIGCM2.Application.Secciones.Deactivate;
using SIGCM2.Domain.Entities;
using SIGCM2.Domain.Exceptions;
namespace SIGCM2.Application.Tests.Secciones.Deactivate;
public class DeactivateSeccionCommandHandlerTests
{
private readonly ISeccionRepository _repo = Substitute.For<ISeccionRepository>();
private readonly IAuditLogger _audit = Substitute.For<IAuditLogger>();
private readonly DeactivateSeccionCommandHandler _handler;
private static Seccion MakeSeccion(int id = 1, bool activo = true)
=> new(id, 1, "COD" + id, "Nombre", "clasificados", activo, DateTime.UtcNow, null);
public DeactivateSeccionCommandHandlerTests()
{
_handler = new DeactivateSeccionCommandHandler(_repo, _audit);
}
[Fact]
public async Task Handle_NotFound_ThrowsSeccionNotFoundException()
{
_repo.GetByIdAsync(999, Arg.Any<CancellationToken>()).Returns((Seccion?)null);
await Assert.ThrowsAsync<SeccionNotFoundException>(
() => _handler.Handle(new DeactivateSeccionCommand(999)));
}
[Fact]
public async Task Handle_AlreadyInactive_IsIdempotentAndDoesNotCallUpdateAsync()
{
_repo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns(MakeSeccion(1, false));
await _handler.Handle(new DeactivateSeccionCommand(1));
await _repo.DidNotReceive().UpdateAsync(Arg.Any<Seccion>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_AlreadyInactive_DoesNotWriteAuditEvent()
{
_repo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns(MakeSeccion(1, false));
await _handler.Handle(new DeactivateSeccionCommand(1));
await _audit.DidNotReceive().LogAsync(
Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(),
Arg.Any<object?>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_ActiveSeccion_CallsUpdateAsyncWithInactiveEntity()
{
_repo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns(MakeSeccion(1, true));
await _handler.Handle(new DeactivateSeccionCommand(1));
await _repo.Received(1).UpdateAsync(
Arg.Is<Seccion>(s => !s.Activo),
Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_ActiveSeccion_WritesAuditWithDeactivateAction()
{
_repo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns(MakeSeccion(1, true));
await _handler.Handle(new DeactivateSeccionCommand(1));
await _audit.Received(1).LogAsync(
action: "seccion.deactivate",
targetType: "Seccion",
targetId: "1",
metadata: Arg.Any<object?>(),
ct: Arg.Any<CancellationToken>());
}
}

View File

@@ -0,0 +1,46 @@
using NSubstitute;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Secciones.GetById;
using SIGCM2.Domain.Entities;
using SIGCM2.Domain.Exceptions;
namespace SIGCM2.Application.Tests.Secciones.GetById;
public class GetSeccionByIdQueryHandlerTests
{
private readonly ISeccionRepository _repo = Substitute.For<ISeccionRepository>();
private readonly GetSeccionByIdQueryHandler _handler;
public GetSeccionByIdQueryHandlerTests()
{
_handler = new GetSeccionByIdQueryHandler(_repo);
}
private static Seccion MakeSeccion(int id) =>
new(id, 2, "COD" + id, "Nombre " + id, "suplementos", true, DateTime.UtcNow, null);
[Fact]
public async Task Handle_NotFound_ThrowsSeccionNotFoundException()
{
_repo.GetByIdAsync(999, Arg.Any<CancellationToken>()).Returns((Seccion?)null);
await Assert.ThrowsAsync<SeccionNotFoundException>(
() => _handler.Handle(new GetSeccionByIdQuery(999)));
}
[Fact]
public async Task Handle_HappyPath_ReturnsDtoWithCorrectFields()
{
var seccion = MakeSeccion(5);
_repo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns(seccion);
var result = await _handler.Handle(new GetSeccionByIdQuery(5));
Assert.Equal(5, result.Id);
Assert.Equal(2, result.MedioId);
Assert.Equal("COD5", result.Codigo);
Assert.Equal("Nombre 5", result.Nombre);
Assert.Equal("suplementos", result.Tipo);
Assert.True(result.Activo);
}
}

View File

@@ -0,0 +1,64 @@
using NSubstitute;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Common;
using SIGCM2.Application.Secciones.List;
using SIGCM2.Domain.Entities;
namespace SIGCM2.Application.Tests.Secciones.List;
public class ListSeccionesQueryHandlerTests
{
private readonly ISeccionRepository _repo = Substitute.For<ISeccionRepository>();
private readonly ListSeccionesQueryHandler _handler;
public ListSeccionesQueryHandlerTests()
{
_handler = new ListSeccionesQueryHandler(_repo);
}
private static Seccion MakeSeccion(int id) =>
new(id, 1, "COD" + id, "Seccion " + id, "clasificados", true, DateTime.UtcNow, null);
[Fact]
public async Task Handle_ReturnsPagedDtoItems()
{
var items = new List<Seccion> { MakeSeccion(1), MakeSeccion(2) };
var pagedResult = new PagedResult<Seccion>(items, 1, 20, 2);
_repo.GetPagedAsync(Arg.Any<SeccionesQuery>(), Arg.Any<CancellationToken>())
.Returns(pagedResult);
var query = new ListSeccionesQuery(1, 20, null, null, null, null);
var result = await _handler.Handle(query);
Assert.Equal(2, result.Total);
Assert.Equal(2, result.Items.Count);
Assert.Equal("COD1", result.Items[0].Codigo);
}
[Fact]
public async Task Handle_ClampsPageSizeToMax100()
{
_repo.GetPagedAsync(Arg.Any<SeccionesQuery>(), Arg.Any<CancellationToken>())
.Returns(new PagedResult<Seccion>([], 1, 100, 0));
await _handler.Handle(new ListSeccionesQuery(1, 500, null, null, null, null));
await _repo.Received(1).GetPagedAsync(
Arg.Is<SeccionesQuery>(q => q.PageSize == 100),
Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_ClampsPageToMin1()
{
_repo.GetPagedAsync(Arg.Any<SeccionesQuery>(), Arg.Any<CancellationToken>())
.Returns(new PagedResult<Seccion>([], 1, 20, 0));
await _handler.Handle(new ListSeccionesQuery(0, 20, null, null, null, null));
await _repo.Received(1).GetPagedAsync(
Arg.Is<SeccionesQuery>(q => q.Page == 1),
Arg.Any<CancellationToken>());
}
}

View File

@@ -0,0 +1,82 @@
using NSubstitute;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Audit;
using SIGCM2.Application.Secciones.Deactivate;
using SIGCM2.Application.Secciones.Reactivate;
using SIGCM2.Domain.Entities;
using SIGCM2.Domain.Exceptions;
namespace SIGCM2.Application.Tests.Secciones.Reactivate;
public class ReactivateSeccionCommandHandlerTests
{
private readonly ISeccionRepository _repo = Substitute.For<ISeccionRepository>();
private readonly IAuditLogger _audit = Substitute.For<IAuditLogger>();
private readonly ReactivateSeccionCommandHandler _handler;
private static Seccion MakeSeccion(int id = 1, bool activo = false)
=> new(id, 1, "COD" + id, "Nombre", "clasificados", activo, DateTime.UtcNow, null);
public ReactivateSeccionCommandHandlerTests()
{
_handler = new ReactivateSeccionCommandHandler(_repo, _audit);
}
[Fact]
public async Task Handle_NotFound_ThrowsSeccionNotFoundException()
{
_repo.GetByIdAsync(999, Arg.Any<CancellationToken>()).Returns((Seccion?)null);
await Assert.ThrowsAsync<SeccionNotFoundException>(
() => _handler.Handle(new ReactivateSeccionCommand(999)));
}
[Fact]
public async Task Handle_AlreadyActive_IsIdempotentAndDoesNotCallUpdateAsync()
{
_repo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns(MakeSeccion(1, true));
await _handler.Handle(new ReactivateSeccionCommand(1));
await _repo.DidNotReceive().UpdateAsync(Arg.Any<Seccion>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_AlreadyActive_DoesNotWriteAuditEvent()
{
_repo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns(MakeSeccion(1, true));
await _handler.Handle(new ReactivateSeccionCommand(1));
await _audit.DidNotReceive().LogAsync(
Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(),
Arg.Any<object?>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_InactiveSeccion_CallsUpdateAsyncWithActiveEntity()
{
_repo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns(MakeSeccion(1, false));
await _handler.Handle(new ReactivateSeccionCommand(1));
await _repo.Received(1).UpdateAsync(
Arg.Is<Seccion>(s => s.Activo),
Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_InactiveSeccion_WritesAuditWithReactivateAction()
{
_repo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns(MakeSeccion(1, false));
await _handler.Handle(new ReactivateSeccionCommand(1));
await _audit.Received(1).LogAsync(
action: "seccion.reactivate",
targetType: "Seccion",
targetId: "1",
metadata: Arg.Any<object?>(),
ct: Arg.Any<CancellationToken>());
}
}

View File

@@ -0,0 +1,74 @@
using NSubstitute;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Audit;
using SIGCM2.Application.Secciones.Update;
using SIGCM2.Domain.Entities;
using SIGCM2.Domain.Exceptions;
namespace SIGCM2.Application.Tests.Secciones.Update;
public class UpdateSeccionCommandHandlerTests
{
private readonly ISeccionRepository _repo = Substitute.For<ISeccionRepository>();
private readonly IAuditLogger _audit = Substitute.For<IAuditLogger>();
private readonly UpdateSeccionCommandHandler _handler;
private static Seccion MakeSeccion(int id = 1, string nombre = "Original", string tipo = "clasificados")
=> new(id, 1, "COD" + id, nombre, tipo, true, DateTime.UtcNow, null);
private static UpdateSeccionCommand ValidCommand(int id = 1) => new(
Id: id,
Nombre: "Nuevo Nombre",
Tipo: "notables");
public UpdateSeccionCommandHandlerTests()
{
_handler = new UpdateSeccionCommandHandler(_repo, _audit);
}
[Fact]
public async Task Handle_NotFound_ThrowsSeccionNotFoundException()
{
_repo.GetByIdAsync(999, Arg.Any<CancellationToken>()).Returns((Seccion?)null);
await Assert.ThrowsAsync<SeccionNotFoundException>(
() => _handler.Handle(new UpdateSeccionCommand(999, "X", "clasificados")));
}
[Fact]
public async Task Handle_HappyPath_CallsUpdateAsyncOnce()
{
_repo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns(MakeSeccion(1));
await _handler.Handle(ValidCommand(1));
await _repo.Received(1).UpdateAsync(Arg.Any<Seccion>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_HappyPath_ReturnsDtoWithUpdatedFields()
{
_repo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns(MakeSeccion(1, "Original", "clasificados"));
var result = await _handler.Handle(ValidCommand(1));
Assert.Equal(1, result.Id);
Assert.Equal("Nuevo Nombre", result.Nombre);
Assert.Equal("notables", result.Tipo);
}
[Fact]
public async Task Handle_HappyPath_CallsAuditWithUpdateAction()
{
_repo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns(MakeSeccion(1));
await _handler.Handle(ValidCommand(1));
await _audit.Received(1).LogAsync(
action: "seccion.update",
targetType: "Seccion",
targetId: "1",
metadata: Arg.Any<object?>(),
ct: Arg.Any<CancellationToken>());
}
}