using AutoMapper; using DocumentOperator.Application.Common.DTOs; using DocumentOperator.Application.Common.Interfaces; using DocumentOperator.Application.ValidatePdf.Queries; using DocumentOperator.Domain.Common.Exceptions; using DocumentOperator.Domain.Models.ValueObjects; using FluentAssertions; using Moq; using Xunit; namespace DocumentOperator.Tests.Unit.Application.Features.ValidatePdf; public class ValidatePdfHandlerTests { private readonly Mock _mockPdfProcessor; private readonly Mock _mockMapper; private readonly ValidatePdfQueryHandler _handler; public ValidatePdfHandlerTests() { _mockPdfProcessor = new Mock(); _mockMapper = new Mock(); _handler = new ValidatePdfQueryHandler(_mockPdfProcessor.Object, _mockMapper.Object); } [Fact] public async Task Handle_ValidPdf_ReturnsPdfMetadata() { // Arrange var pdfBytes = new byte[] { 0x25, 0x50, 0x44, 0x46 }; // "%PDF" var query = new ValidatePdfQuery { PdfBytes = pdfBytes }; 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())) .ReturnsAsync(domainMetadata); _mockMapper .Setup(x => x.Map(domainMetadata)) .Returns(expectedDto); // Act var result = await _handler.Handle(query, CancellationToken.None); // Assert result.Should().NotBeNull(); result.PageCount.Should().Be(5); result.FileSizeBytes.Should().Be(1024); result.PdfVersion.Should().Be("1.4"); result.HasAttachments.Should().BeFalse(); result.AttachmentCount.Should().Be(0); _mockPdfProcessor.Verify(x => x.ValidateAsync(It.IsAny()), Times.Once); _mockMapper.Verify(x => x.Map(domainMetadata), Times.Once); } [Fact] public async Task Handle_PdfProcessorThrowsException_PropagatesException() { // Arrange var pdfBytes = new byte[] { 0x25, 0x50, 0x44, 0x46 }; // "%PDF" var query = new ValidatePdfQuery { PdfBytes = pdfBytes }; _mockPdfProcessor .Setup(x => x.ValidateAsync(It.IsAny())) .ThrowsAsync(new PdfProcessingException("Invalid PDF format")); // Act & Assert var exception = await Assert.ThrowsAsync( () => _handler.Handle(query, CancellationToken.None) ); exception.Message.Should().Be("Invalid PDF format"); _mockPdfProcessor.Verify(x => x.ValidateAsync(It.IsAny()), Times.Once); } }