test: add Application handler tests for all missing features
New test files (4 tests each — 2 success + 2 failure): - MergePdfsCommandHandlerTests: TwoStreams, WithPageRanges, BadRequest, GenericException - AddAnnotationHandlerTests: TextMarkup, FreeText, BadRequest (page OOB), GenericException - AddStampHandlerTests: TextStamp, PredefinedStamp, BadRequest (opacity), GenericException - ExtractPdfAttachmentsHandlerTests: ReturnsZip, NotFoundException, BadRequest, GenericException - ConvertPdfAHandlerTests (ConvertTo + ConvertFrom): ConvertTo: ValidLevel, NotImplemented, BadRequest ConvertFrom: ValidPdfA, BadRequest, GenericException
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
using DocumentService.Application.AddAnnotation;
|
||||
using DocumentService.Application.Common.Interfaces;
|
||||
using DocumentService.Domain.Common.Exceptions;
|
||||
using DocumentService.Domain.Models.ValueObjects;
|
||||
using FluentAssertions;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace DocumentService.Tests.Unit.Application.Features.AddAnnotation;
|
||||
|
||||
public class AddAnnotationHandlerTests
|
||||
{
|
||||
private readonly Mock<IPdfProcessor> _mockProcessor;
|
||||
private readonly AddAnnotationHandler _handler;
|
||||
private static readonly byte[] FakePdfBytes = "%PDF"u8.ToArray();
|
||||
|
||||
public AddAnnotationHandlerTests()
|
||||
{
|
||||
_mockProcessor = new Mock<IPdfProcessor>();
|
||||
_handler = new AddAnnotationHandler(_mockProcessor.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_TextMarkupAnnotation_ReturnsAnnotatedBytes()
|
||||
{
|
||||
// Arrange
|
||||
var command = new AddAnnotationCommand
|
||||
{
|
||||
PdfStream = new MemoryStream(FakePdfBytes),
|
||||
AnnotationType = AnnotationType.TextMarkup,
|
||||
PageNumber = 1,
|
||||
Rectangle = (10, 20, 200, 50),
|
||||
TextMarkupStyle = TextMarkupStyle.Highlight,
|
||||
Author = "TestUser",
|
||||
Color = "FFFF00"
|
||||
};
|
||||
var expected = new byte[] { 0x25, 0x50, 0x44, 0x46, 0x01 };
|
||||
_mockProcessor.Setup(x => x.AddAnnotationAsync(
|
||||
It.IsAny<Stream>(), AnnotationType.TextMarkup, 1,
|
||||
It.IsAny<(double, double, double, double)>(),
|
||||
null, "TestUser", "FFFF00", TextMarkupStyle.Highlight, AnnotationOrigin.BottomLeft))
|
||||
.ReturnsAsync(expected);
|
||||
|
||||
// Act
|
||||
var result = await _handler.Handle(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.Should().BeEquivalentTo(expected);
|
||||
_mockProcessor.Verify(x => x.AddAnnotationAsync(
|
||||
It.IsAny<Stream>(), AnnotationType.TextMarkup, 1,
|
||||
It.IsAny<(double, double, double, double)>(),
|
||||
null, "TestUser", "FFFF00", TextMarkupStyle.Highlight, AnnotationOrigin.BottomLeft), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_FreeTextAnnotation_PassesContentToProcessor()
|
||||
{
|
||||
// Arrange
|
||||
var command = new AddAnnotationCommand
|
||||
{
|
||||
PdfStream = new MemoryStream(FakePdfBytes),
|
||||
AnnotationType = AnnotationType.FreeText,
|
||||
PageNumber = 2,
|
||||
Rectangle = (50, 100, 300, 150),
|
||||
Content = "Review this section"
|
||||
};
|
||||
var expected = new byte[] { 0x25, 0x50, 0x44, 0x46, 0x02 };
|
||||
_mockProcessor.Setup(x => x.AddAnnotationAsync(
|
||||
It.IsAny<Stream>(), AnnotationType.FreeText, 2,
|
||||
It.IsAny<(double, double, double, double)>(),
|
||||
"Review this section", null, null, null, AnnotationOrigin.BottomLeft))
|
||||
.ReturnsAsync(expected);
|
||||
|
||||
// Act
|
||||
var result = await _handler.Handle(command, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
result.Should().BeEquivalentTo(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_ProcessorThrowsBadRequest_PropagatesException()
|
||||
{
|
||||
// Arrange
|
||||
var command = new AddAnnotationCommand
|
||||
{
|
||||
PdfStream = new MemoryStream(FakePdfBytes),
|
||||
AnnotationType = AnnotationType.Circle,
|
||||
PageNumber = 99,
|
||||
Rectangle = (0, 0, 100, 100)
|
||||
};
|
||||
_mockProcessor.Setup(x => x.AddAnnotationAsync(
|
||||
It.IsAny<Stream>(), It.IsAny<AnnotationType>(), It.IsAny<int>(),
|
||||
It.IsAny<(double, double, double, double)>(),
|
||||
It.IsAny<string?>(), It.IsAny<string?>(), It.IsAny<string?>(),
|
||||
It.IsAny<TextMarkupStyle?>(), It.IsAny<AnnotationOrigin>()))
|
||||
.ThrowsAsync(new BadRequestException("Page number 99 exceeds document page count"));
|
||||
|
||||
// Act & Assert
|
||||
var ex = await Assert.ThrowsAsync<BadRequestException>(
|
||||
() => _handler.Handle(command, CancellationToken.None));
|
||||
ex.Message.Should().Contain("Page number 99");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Handle_ProcessorThrowsGenericException_PropagatesException()
|
||||
{
|
||||
// Arrange
|
||||
var command = new AddAnnotationCommand
|
||||
{
|
||||
PdfStream = new MemoryStream(FakePdfBytes),
|
||||
AnnotationType = AnnotationType.Square,
|
||||
PageNumber = 1,
|
||||
Rectangle = (0, 0, 100, 100)
|
||||
};
|
||||
_mockProcessor.Setup(x => x.AddAnnotationAsync(
|
||||
It.IsAny<Stream>(), It.IsAny<AnnotationType>(), It.IsAny<int>(),
|
||||
It.IsAny<(double, double, double, double)>(),
|
||||
It.IsAny<string?>(), It.IsAny<string?>(), It.IsAny<string?>(),
|
||||
It.IsAny<TextMarkupStyle?>(), It.IsAny<AnnotationOrigin>()))
|
||||
.ThrowsAsync(new Exception("Unexpected error"));
|
||||
|
||||
// Act & Assert
|
||||
await Assert.ThrowsAsync<Exception>(() => _handler.Handle(command, CancellationToken.None));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user