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:
@@ -2,16 +2,13 @@ using AutoMapper;
|
||||
using DocumentService.Application.CheckPdfAttachments.Queries;
|
||||
using DocumentService.Application.Common.DTOs;
|
||||
using DocumentService.Application.Common.Interfaces;
|
||||
|
||||
using DocumentService.Domain.Common.Exceptions;
|
||||
using FluentAssertions;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace DocumentService.Tests.Unit.Application.CheckPdfAttachments;
|
||||
|
||||
/// <summary>
|
||||
/// Unit tests for CheckPdfAttachmentsQueryHandler.
|
||||
/// Tests handler logic with mocked dependencies (IPdfProcessor, IMapper).
|
||||
/// </summary>
|
||||
public class CheckPdfAttachmentsQueryHandlerTests
|
||||
{
|
||||
private readonly Mock<IPdfProcessor> _mockPdfProcessor;
|
||||
@@ -26,26 +23,16 @@ public class CheckPdfAttachmentsQueryHandlerTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WithPdfBytes_CallsProcessorAndMapper()
|
||||
public async Task Handle_PdfWithAttachments_ReturnsAttachmentDetails()
|
||||
{
|
||||
// Arrange
|
||||
byte[] pdfBytes = "fake-pdf-content"u8.ToArray();
|
||||
var query = new CheckPdfAttachmentsQuery { PdfStream = new MemoryStream(pdfBytes) };
|
||||
|
||||
var query = new CheckPdfAttachmentsQuery { PdfStream = new MemoryStream("fake-pdf"u8.ToArray()) };
|
||||
var domainResult = new AttachmentInfo(
|
||||
hasAttachments: true,
|
||||
attachmentCount: 2,
|
||||
attachments:
|
||||
[
|
||||
new("invoice.xml", "text/xml", 1024),
|
||||
new("metadata.json", "application/json", 512)
|
||||
]
|
||||
);
|
||||
|
||||
hasAttachments: true, attachmentCount: 2,
|
||||
attachments: [new("invoice.xml", "text/xml", 1024), new("metadata.json", "application/json", 512)]);
|
||||
var expectedDto = new AttachmentCheckResult
|
||||
{
|
||||
HasAttachments = true,
|
||||
AttachmentCount = 2,
|
||||
HasAttachments = true, AttachmentCount = 2,
|
||||
Attachments =
|
||||
[
|
||||
new() { FileName = "invoice.xml", MimeType = "text/xml", Size = 1024 },
|
||||
@@ -61,20 +48,22 @@ public class CheckPdfAttachmentsQueryHandlerTests
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Should().BeEquivalentTo(expectedDto);
|
||||
|
||||
result.HasAttachments.Should().BeTrue();
|
||||
result.AttachmentCount.Should().Be(2);
|
||||
result.Attachments.Should().HaveCount(2);
|
||||
result.Attachments[0].FileName.Should().Be("invoice.xml");
|
||||
result.Attachments[0].MimeType.Should().Be("text/xml");
|
||||
result.Attachments[0].Size.Should().Be(1024);
|
||||
_mockPdfProcessor.Verify(p => p.CheckAttachmentsAsync(It.IsAny<Stream>()), Times.Once);
|
||||
_mockMapper.Verify(m => m.Map<AttachmentCheckResult>(domainResult), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WithBase64Pdf_DecodesAndCallsProcessor()
|
||||
public async Task Handle_PdfWithNoAttachments_ReturnsEmptyResult()
|
||||
{
|
||||
// Arrange
|
||||
byte[] pdfBytes = "fake-pdf-content"u8.ToArray();
|
||||
var query = new CheckPdfAttachmentsQuery { PdfStream = new MemoryStream(pdfBytes) };
|
||||
|
||||
var domainResult = new AttachmentInfo(false, 0, []);
|
||||
var query = new CheckPdfAttachmentsQuery { PdfStream = new MemoryStream("fake-pdf"u8.ToArray()) };
|
||||
var domainResult = AttachmentInfo.Empty;
|
||||
var expectedDto = new AttachmentCheckResult { HasAttachments = false, AttachmentCount = 0, Attachments = [] };
|
||||
|
||||
_mockPdfProcessor.Setup(p => p.CheckAttachmentsAsync(It.IsAny<Stream>())).ReturnsAsync(domainResult);
|
||||
@@ -83,40 +72,37 @@ public class CheckPdfAttachmentsQueryHandlerTests
|
||||
// Act
|
||||
var result = await _sut.Handle(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.HasAttachments.Should().BeFalse();
|
||||
result.AttachmentCount.Should().Be(0);
|
||||
|
||||
_mockPdfProcessor.Verify(p => p.CheckAttachmentsAsync(It.IsAny<Stream>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_WithEmptyAttachments_ReturnsEmptyList()
|
||||
{
|
||||
// Arrange
|
||||
byte[] pdfBytes = "fake-pdf-content"u8.ToArray();
|
||||
var query = new CheckPdfAttachmentsQuery { PdfStream = new MemoryStream(pdfBytes) };
|
||||
|
||||
var domainResult = new AttachmentInfo(false, 0, []);
|
||||
var expectedDto = new AttachmentCheckResult
|
||||
{
|
||||
HasAttachments = false,
|
||||
AttachmentCount = 0,
|
||||
Attachments = []
|
||||
};
|
||||
|
||||
_mockPdfProcessor.Setup(p => p.CheckAttachmentsAsync(It.IsAny<Stream>())).ReturnsAsync(domainResult);
|
||||
_mockMapper.Setup(m => m.Map<AttachmentCheckResult>(domainResult)).Returns(expectedDto);
|
||||
|
||||
// Act
|
||||
var result = await _sut.Handle(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.HasAttachments.Should().BeFalse();
|
||||
result.AttachmentCount.Should().Be(0);
|
||||
result.Attachments.Should().BeEmpty();
|
||||
_mockPdfProcessor.Verify(p => p.CheckAttachmentsAsync(It.IsAny<Stream>()), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_ProcessorThrowsBadRequest_PropagatesException()
|
||||
{
|
||||
// Arrange
|
||||
var query = new CheckPdfAttachmentsQuery { PdfStream = new MemoryStream("fake-pdf"u8.ToArray()) };
|
||||
_mockPdfProcessor.Setup(p => p.CheckAttachmentsAsync(It.IsAny<Stream>()))
|
||||
.ThrowsAsync(new BadRequestException("PDF stream cannot be empty"));
|
||||
|
||||
// Act & Assert
|
||||
var ex = await Assert.ThrowsAsync<BadRequestException>(
|
||||
() => _sut.Handle(query, CancellationToken.None));
|
||||
ex.Message.Should().Be("PDF stream cannot be empty");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_ProcessorThrowsGenericException_PropagatesException()
|
||||
{
|
||||
// Arrange
|
||||
var query = new CheckPdfAttachmentsQuery { PdfStream = new MemoryStream("fake-pdf"u8.ToArray()) };
|
||||
_mockPdfProcessor.Setup(p => p.CheckAttachmentsAsync(It.IsAny<Stream>()))
|
||||
.ThrowsAsync(new InvalidOperationException("Unexpected error"));
|
||||
|
||||
// Act & Assert
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(
|
||||
() => _sut.Handle(query, CancellationToken.None));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user