test: rewrite existing Application handler tests (no mapper mock, full assertions)

ValidatePdfHandlerTests:
- Remove IMapper mock (handler no longer uses mapper)
- Add: EncryptedPdf_ReturnsIsEncryptedTrue
- Add: PdfWithAttachments_ReturnsAttachmentData
- Add: ProcessorThrowsGenericException_PropagatesException

ValidatePdfAQueryHandlerTests:
- Remove IMapper mock
- Strengthen assertions (Errors count, Warnings content)
- Add: ProcessorThrowsGenericException_PropagatesException

CheckPdfAttachmentsQueryHandlerTests:
- Remove duplicate Handle_WithBase64Pdf test (identical to WithPdfBytes)
- Add: ProcessorThrowsBadRequest_PropagatesException
- Add: ProcessorThrowsGenericException_PropagatesException
- Strengthen: assert individual attachment fields (FileName, MimeType, Size)
This commit is contained in:
2026-08-28 11:46:09 +02:00
parent 9a9d775fec
commit 9040aed1da
3 changed files with 148 additions and 230 deletions

View File

@@ -1,4 +1,3 @@
using AutoMapper;
using DocumentService.Application.Common.DTOs;
using DocumentService.Application.Common.Interfaces;
using DocumentService.Application.ValidatePdf.Queries;
@@ -12,47 +11,22 @@ namespace DocumentService.Tests.Unit.Application.Features.ValidatePdf;
public class ValidatePdfHandlerTests
{
private readonly Mock<IPdfProcessor> _mockPdfProcessor;
private readonly Mock<IMapper> _mockMapper;
private readonly ValidatePdfQueryHandler _handler;
public ValidatePdfHandlerTests()
{
_mockPdfProcessor = new Mock<IPdfProcessor>();
_mockMapper = new Mock<IMapper>();
_handler = new ValidatePdfQueryHandler(_mockPdfProcessor.Object, _mockMapper.Object);
_handler = new ValidatePdfQueryHandler(_mockPdfProcessor.Object);
}
[Fact]
public async Task Handle_ValidPdf_ReturnsPdfMetadata()
public async Task Handle_ValidPdf_ReturnsResult()
{
// Arrange
var pdfBytes = "%PDF"u8.ToArray(); // "%PDF"
var query = new ValidatePdfQuery { PdfStream = new MemoryStream(pdfBytes) };
var query = new ValidatePdfQuery { PdfStream = new MemoryStream("%PDF"u8.ToArray()) };
var expected = new PdfValidationResult(5, 1024, 1024 / 1024.0 / 1024.0, "1.4", false, 0, false);
var domainMetadata = new PdfMetadata(
pageCount: 5,
fileSizeBytes: 1024,
pdfVersion: "1.4",
hasAttachments: false,
attachmentCount: 0
);
var expectedDto = new PdfValidationResult(
PageCount: 5,
FileSizeBytes: 1024,
FileSizeMB: 0.00,
PdfVersion: "1.4",
HasAttachments: false,
AttachmentCount: 0
);
_mockPdfProcessor
.Setup(x => x.ValidateAsync(It.IsAny<Stream>()))
.ReturnsAsync(domainMetadata);
_mockMapper
.Setup(x => x.Map<PdfValidationResult>(domainMetadata))
.Returns(expectedDto);
_mockPdfProcessor.Setup(x => x.ValidateAsync(It.IsAny<Stream>())).ReturnsAsync(expected);
// Act
var result = await _handler.Handle(query, CancellationToken.None);
@@ -64,28 +38,71 @@ public class ValidatePdfHandlerTests
result.PdfVersion.Should().Be("1.4");
result.HasAttachments.Should().BeFalse();
result.AttachmentCount.Should().Be(0);
result.IsEncrypted.Should().BeFalse();
_mockPdfProcessor.Verify(x => x.ValidateAsync(It.IsAny<Stream>()), Times.Once);
_mockMapper.Verify(x => x.Map<PdfValidationResult>(domainMetadata), Times.Once);
}
[Fact]
public async Task Handle_PdfProcessorThrowsException_PropagatesException()
public async Task Handle_EncryptedPdf_ReturnsIsEncryptedTrue()
{
// Arrange
var pdfBytes = "%PDF"u8.ToArray(); // "%PDF"
var query = new ValidatePdfQuery { PdfStream = new MemoryStream(pdfBytes) };
var query = new ValidatePdfQuery { PdfStream = new MemoryStream("%PDF"u8.ToArray()) };
var expected = new PdfValidationResult(1, 512, 512 / 1024.0 / 1024.0, "1.7", false, 0, true);
_mockPdfProcessor
.Setup(x => x.ValidateAsync(It.IsAny<Stream>()))
_mockPdfProcessor.Setup(x => x.ValidateAsync(It.IsAny<Stream>())).ReturnsAsync(expected);
// Act
var result = await _handler.Handle(query, CancellationToken.None);
// Assert
result.IsEncrypted.Should().BeTrue();
result.PageCount.Should().Be(1);
_mockPdfProcessor.Verify(x => x.ValidateAsync(It.IsAny<Stream>()), Times.Once);
}
[Fact]
public async Task Handle_PdfWithAttachments_ReturnsAttachmentData()
{
// Arrange
var query = new ValidatePdfQuery { PdfStream = new MemoryStream("%PDF"u8.ToArray()) };
var expected = new PdfValidationResult(2, 4096, 4096 / 1024.0 / 1024.0, "1.6", true, 3, false);
_mockPdfProcessor.Setup(x => x.ValidateAsync(It.IsAny<Stream>())).ReturnsAsync(expected);
// Act
var result = await _handler.Handle(query, CancellationToken.None);
// Assert
result.HasAttachments.Should().BeTrue();
result.AttachmentCount.Should().Be(3);
_mockPdfProcessor.Verify(x => x.ValidateAsync(It.IsAny<Stream>()), Times.Once);
}
[Fact]
public async Task Handle_ProcessorThrowsBadRequest_PropagatesException()
{
// Arrange
var query = new ValidatePdfQuery { PdfStream = new MemoryStream("%PDF"u8.ToArray()) };
_mockPdfProcessor.Setup(x => x.ValidateAsync(It.IsAny<Stream>()))
.ThrowsAsync(new BadRequestException("Invalid PDF format"));
// Act & Assert
var exception = await Assert.ThrowsAsync<BadRequestException>(
() => _handler.Handle(query, CancellationToken.None)
);
exception.Message.Should().Be("Invalid PDF format");
var ex = await Assert.ThrowsAsync<BadRequestException>(
() => _handler.Handle(query, CancellationToken.None));
ex.Message.Should().Be("Invalid PDF format");
_mockPdfProcessor.Verify(x => x.ValidateAsync(It.IsAny<Stream>()), Times.Once);
}
[Fact]
public async Task Handle_ProcessorThrowsGenericException_PropagatesException()
{
// Arrange
var query = new ValidatePdfQuery { PdfStream = new MemoryStream("%PDF"u8.ToArray()) };
_mockPdfProcessor.Setup(x => x.ValidateAsync(It.IsAny<Stream>()))
.ThrowsAsync(new InvalidOperationException("Unexpected error"));
// Act & Assert
await Assert.ThrowsAsync<InvalidOperationException>(
() => _handler.Handle(query, CancellationToken.None));
}
}