test: Update Application handler unit tests for Stream API
- Update mock setups: It.IsAny<byte[]>() → It.IsAny<Stream>() - Update mock verifications: Verify Stream parameter instead of byte[] - Remove obsolete namespace imports (Domain.Models.ValueObjects) - Update exception expectations (BadRequestException instead of PdfProcessingException)
This commit is contained in:
@@ -3,7 +3,6 @@ using DocumentOperator.Application.Common.DTOs;
|
|||||||
using DocumentOperator.Application.Common.Interfaces;
|
using DocumentOperator.Application.Common.Interfaces;
|
||||||
using DocumentOperator.Application.ValidatePdf.Queries;
|
using DocumentOperator.Application.ValidatePdf.Queries;
|
||||||
using DocumentOperator.Domain.Common.Exceptions;
|
using DocumentOperator.Domain.Common.Exceptions;
|
||||||
using DocumentOperator.Domain.Models.ValueObjects;
|
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using Moq;
|
using Moq;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
@@ -27,7 +26,7 @@ public class ValidatePdfHandlerTests
|
|||||||
public async Task Handle_ValidPdf_ReturnsPdfMetadata()
|
public async Task Handle_ValidPdf_ReturnsPdfMetadata()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var pdfBytes = new byte[] { 0x25, 0x50, 0x44, 0x46 }; // "%PDF"
|
var pdfBytes = "%PDF"u8.ToArray(); // "%PDF"
|
||||||
var query = new ValidatePdfQuery { PdfBytes = pdfBytes };
|
var query = new ValidatePdfQuery { PdfBytes = pdfBytes };
|
||||||
|
|
||||||
var domainMetadata = new PdfMetadata(
|
var domainMetadata = new PdfMetadata(
|
||||||
@@ -48,7 +47,7 @@ public class ValidatePdfHandlerTests
|
|||||||
);
|
);
|
||||||
|
|
||||||
_mockPdfProcessor
|
_mockPdfProcessor
|
||||||
.Setup(x => x.ValidateAsync(It.IsAny<byte[]>()))
|
.Setup(x => x.ValidateAsync(It.IsAny<Stream>()))
|
||||||
.ReturnsAsync(domainMetadata);
|
.ReturnsAsync(domainMetadata);
|
||||||
|
|
||||||
_mockMapper
|
_mockMapper
|
||||||
@@ -66,7 +65,7 @@ public class ValidatePdfHandlerTests
|
|||||||
result.HasAttachments.Should().BeFalse();
|
result.HasAttachments.Should().BeFalse();
|
||||||
result.AttachmentCount.Should().Be(0);
|
result.AttachmentCount.Should().Be(0);
|
||||||
|
|
||||||
_mockPdfProcessor.Verify(x => x.ValidateAsync(It.IsAny<byte[]>()), Times.Once);
|
_mockPdfProcessor.Verify(x => x.ValidateAsync(It.IsAny<Stream>()), Times.Once);
|
||||||
_mockMapper.Verify(x => x.Map<PdfValidationResult>(domainMetadata), Times.Once);
|
_mockMapper.Verify(x => x.Map<PdfValidationResult>(domainMetadata), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,19 +73,19 @@ public class ValidatePdfHandlerTests
|
|||||||
public async Task Handle_PdfProcessorThrowsException_PropagatesException()
|
public async Task Handle_PdfProcessorThrowsException_PropagatesException()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var pdfBytes = new byte[] { 0x25, 0x50, 0x44, 0x46 }; // "%PDF"
|
var pdfBytes = "%PDF"u8.ToArray(); // "%PDF"
|
||||||
var query = new ValidatePdfQuery { PdfBytes = pdfBytes };
|
var query = new ValidatePdfQuery { PdfBytes = pdfBytes };
|
||||||
|
|
||||||
_mockPdfProcessor
|
_mockPdfProcessor
|
||||||
.Setup(x => x.ValidateAsync(It.IsAny<byte[]>()))
|
.Setup(x => x.ValidateAsync(It.IsAny<Stream>()))
|
||||||
.ThrowsAsync(new PdfProcessingException("Invalid PDF format"));
|
.ThrowsAsync(new BadRequestException("Invalid PDF format"));
|
||||||
|
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
var exception = await Assert.ThrowsAsync<PdfProcessingException>(
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
||||||
() => _handler.Handle(query, CancellationToken.None)
|
() => _handler.Handle(query, CancellationToken.None)
|
||||||
);
|
);
|
||||||
|
|
||||||
exception.Message.Should().Be("Invalid PDF format");
|
exception.Message.Should().Be("Invalid PDF format");
|
||||||
_mockPdfProcessor.Verify(x => x.ValidateAsync(It.IsAny<byte[]>()), Times.Once);
|
_mockPdfProcessor.Verify(x => x.ValidateAsync(It.IsAny<Stream>()), Times.Once);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ using DocumentOperator.Application.Common.DTOs;
|
|||||||
using DocumentOperator.Application.Common.Interfaces;
|
using DocumentOperator.Application.Common.Interfaces;
|
||||||
using DocumentOperator.Application.ValidatePdfA.Queries;
|
using DocumentOperator.Application.ValidatePdfA.Queries;
|
||||||
using DocumentOperator.Domain.Common.Exceptions;
|
using DocumentOperator.Domain.Common.Exceptions;
|
||||||
using DocumentOperator.Domain.Models.ValueObjects;
|
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using Moq;
|
using Moq;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
@@ -27,7 +27,7 @@ public class ValidatePdfAQueryHandlerTests
|
|||||||
public async Task Handle_ValidPdfA_ReturnsPdfAMetadata()
|
public async Task Handle_ValidPdfA_ReturnsPdfAMetadata()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var pdfBytes = new byte[] { 0x25, 0x50, 0x44, 0x46 }; // "%PDF"
|
var pdfBytes = "%PDF"u8.ToArray(); // "%PDF"
|
||||||
var query = new ValidatePdfAQuery { PdfBytes = pdfBytes };
|
var query = new ValidatePdfAQuery { PdfBytes = pdfBytes };
|
||||||
|
|
||||||
var domainMetadata = new PdfAMetadata(
|
var domainMetadata = new PdfAMetadata(
|
||||||
@@ -38,8 +38,8 @@ public class ValidatePdfAQueryHandlerTests
|
|||||||
encrypted: false,
|
encrypted: false,
|
||||||
pdfaVersion: "PDF/A-3b",
|
pdfaVersion: "PDF/A-3b",
|
||||||
pdfaCompliant: true,
|
pdfaCompliant: true,
|
||||||
errors: new List<string>(),
|
errors: [],
|
||||||
warnings: new List<string>()
|
warnings: []
|
||||||
);
|
);
|
||||||
|
|
||||||
var expectedDto = new PdfAValidationResult
|
var expectedDto = new PdfAValidationResult
|
||||||
@@ -56,7 +56,7 @@ public class ValidatePdfAQueryHandlerTests
|
|||||||
};
|
};
|
||||||
|
|
||||||
_mockPdfProcessor
|
_mockPdfProcessor
|
||||||
.Setup(x => x.ValidatePdfAAsync(It.IsAny<byte[]>()))
|
.Setup(x => x.ValidatePdfAAsync(It.IsAny<Stream>()))
|
||||||
.ReturnsAsync(domainMetadata);
|
.ReturnsAsync(domainMetadata);
|
||||||
|
|
||||||
_mockMapper
|
_mockMapper
|
||||||
@@ -77,7 +77,7 @@ public class ValidatePdfAQueryHandlerTests
|
|||||||
result.Errors.Should().BeEmpty();
|
result.Errors.Should().BeEmpty();
|
||||||
result.Warnings.Should().BeEmpty();
|
result.Warnings.Should().BeEmpty();
|
||||||
|
|
||||||
_mockPdfProcessor.Verify(x => x.ValidatePdfAAsync(It.IsAny<byte[]>()), Times.Once);
|
_mockPdfProcessor.Verify(x => x.ValidatePdfAAsync(It.IsAny<Stream>()), Times.Once);
|
||||||
_mockMapper.Verify(x => x.Map<PdfAValidationResult>(domainMetadata), Times.Once);
|
_mockMapper.Verify(x => x.Map<PdfAValidationResult>(domainMetadata), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,7 +117,7 @@ public class ValidatePdfAQueryHandlerTests
|
|||||||
};
|
};
|
||||||
|
|
||||||
_mockPdfProcessor
|
_mockPdfProcessor
|
||||||
.Setup(x => x.ValidatePdfAAsync(It.IsAny<byte[]>()))
|
.Setup(x => x.ValidatePdfAAsync(It.IsAny<Stream>()))
|
||||||
.ReturnsAsync(domainMetadata);
|
.ReturnsAsync(domainMetadata);
|
||||||
|
|
||||||
_mockMapper
|
_mockMapper
|
||||||
@@ -136,7 +136,7 @@ public class ValidatePdfAQueryHandlerTests
|
|||||||
result.Errors.Should().Contain("Missing XMP metadata");
|
result.Errors.Should().Contain("Missing XMP metadata");
|
||||||
result.Warnings.Should().HaveCount(1);
|
result.Warnings.Should().HaveCount(1);
|
||||||
|
|
||||||
_mockPdfProcessor.Verify(x => x.ValidatePdfAAsync(It.IsAny<byte[]>()), Times.Once);
|
_mockPdfProcessor.Verify(x => x.ValidatePdfAAsync(It.IsAny<Stream>()), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -172,7 +172,7 @@ public class ValidatePdfAQueryHandlerTests
|
|||||||
};
|
};
|
||||||
|
|
||||||
_mockPdfProcessor
|
_mockPdfProcessor
|
||||||
.Setup(x => x.ValidatePdfAAsync(It.IsAny<byte[]>()))
|
.Setup(x => x.ValidatePdfAAsync(It.IsAny<Stream>()))
|
||||||
.ReturnsAsync(domainMetadata);
|
.ReturnsAsync(domainMetadata);
|
||||||
|
|
||||||
_mockMapper
|
_mockMapper
|
||||||
@@ -188,7 +188,7 @@ public class ValidatePdfAQueryHandlerTests
|
|||||||
result.PdfACompliant.Should().BeFalse();
|
result.PdfACompliant.Should().BeFalse();
|
||||||
result.Errors.Should().Contain("Encrypted PDFs cannot be PDF/A compliant");
|
result.Errors.Should().Contain("Encrypted PDFs cannot be PDF/A compliant");
|
||||||
|
|
||||||
_mockPdfProcessor.Verify(x => x.ValidatePdfAAsync(It.IsAny<byte[]>()), Times.Once);
|
_mockPdfProcessor.Verify(x => x.ValidatePdfAAsync(It.IsAny<Stream>()), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -199,15 +199,16 @@ public class ValidatePdfAQueryHandlerTests
|
|||||||
var query = new ValidatePdfAQuery { PdfBytes = pdfBytes };
|
var query = new ValidatePdfAQuery { PdfBytes = pdfBytes };
|
||||||
|
|
||||||
_mockPdfProcessor
|
_mockPdfProcessor
|
||||||
.Setup(x => x.ValidatePdfAAsync(It.IsAny<byte[]>()))
|
.Setup(x => x.ValidatePdfAAsync(It.IsAny<Stream>()))
|
||||||
.ThrowsAsync(new PdfProcessingException("Invalid PDF format"));
|
.ThrowsAsync(new BadRequestException("Invalid PDF format"));
|
||||||
|
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
var exception = await Assert.ThrowsAsync<PdfProcessingException>(
|
var exception = await Assert.ThrowsAsync<BadRequestException>(
|
||||||
() => _handler.Handle(query, CancellationToken.None)
|
() => _handler.Handle(query, CancellationToken.None)
|
||||||
);
|
);
|
||||||
|
|
||||||
exception.Message.Should().Be("Invalid PDF format");
|
exception.Message.Should().Be("Invalid PDF format");
|
||||||
_mockPdfProcessor.Verify(x => x.ValidatePdfAAsync(It.IsAny<byte[]>()), Times.Once);
|
_mockPdfProcessor.Verify(x => x.ValidatePdfAAsync(It.IsAny<Stream>()), Times.Once);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user