Refactored the codebase to use shared DTOs from `DocumentService.Application.Common.DTOs`, removing inline DTO definitions in client interfaces and tests. Introduced conditional compilation to handle framework-specific differences in DTOs and handlers. Updated test cases to align with the new DTO structures, including `SwissQrCodeExtractionResult`, `PdfValidationResult`, and `ZugferdExtractionResult`. Adjusted test data to match updated DTO properties. Cleaned up solution and project files, ensuring proper references and organization. Removed unused files and fixed project references in `DocumentService.Tests.csproj`. Improved maintainability and consistency across the codebase.
185 lines
8.0 KiB
C#
185 lines
8.0 KiB
C#
using DocumentService.Application.Common.DTOs;
|
|
using DocumentService.Client.Clients;
|
|
using DocumentService.Client.Interfaces;
|
|
using FluentAssertions;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Text.Json;
|
|
using Xunit;
|
|
|
|
namespace DocumentService.Tests.Unit.Client;
|
|
|
|
public class PdfValidationClientTests
|
|
{
|
|
private static (PdfValidationClient client, MockHttpMessageHandler handler) Build<T>(T responseBody)
|
|
{
|
|
var handler = MockHttpMessageHandler.ReturningJson(responseBody);
|
|
var httpClient = new HttpClient(handler) { BaseAddress = new Uri("http://localhost") };
|
|
return (new PdfValidationClient(httpClient, NullLogger<PdfValidationClient>.Instance), handler);
|
|
}
|
|
|
|
private static (PdfValidationClient client, MockHttpMessageHandler handler) BuildError(HttpStatusCode code)
|
|
{
|
|
var handler = MockHttpMessageHandler.ReturningStatus(code);
|
|
var httpClient = new HttpClient(handler) { BaseAddress = new Uri("http://localhost") };
|
|
return (new PdfValidationClient(httpClient, NullLogger<PdfValidationClient>.Instance), handler);
|
|
}
|
|
|
|
private static byte[] FakePdfBytes() => "fake-pdf"u8.ToArray();
|
|
|
|
// ── ValidatePdfAsync (Stream) ────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public async Task ValidatePdfAsync_Stream_SendsMultipartPost()
|
|
{
|
|
var expected = new PdfValidationResult(PageCount: 3, FileSizeBytes: 2048, FileSizeMB: 0, PdfVersion: "1.7", HasAttachments: false, AttachmentCount: 0, IsEncrypted: false);
|
|
var (client, handler) = Build(expected);
|
|
|
|
var result = await client.ValidatePdfAsync(new MemoryStream(FakePdfBytes()));
|
|
|
|
result.PageCount.Should().Be(3);
|
|
result.PdfVersion.Should().Be("1.7");
|
|
result.FileSizeBytes.Should().Be(2048);
|
|
result.IsEncrypted.Should().BeFalse();
|
|
handler.LastRequest!.Method.Should().Be(HttpMethod.Post);
|
|
handler.LastRequest.RequestUri!.PathAndQuery.Should().Be("/api/pdf/validation/validate");
|
|
handler.LastRequest.Content.Should().BeOfType<MultipartFormDataContent>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ValidatePdfAsync_Stream_EncryptedPdf_DeserializesIsEncryptedTrue()
|
|
{
|
|
var expected = new PdfValidationResult(PageCount: 1, FileSizeBytes: 0, FileSizeMB: 0, PdfVersion: "1.6", HasAttachments: false, AttachmentCount: 0, IsEncrypted: true);
|
|
var (client, _) = Build(expected);
|
|
|
|
var result = await client.ValidatePdfAsync(new MemoryStream(FakePdfBytes()));
|
|
|
|
result.IsEncrypted.Should().BeTrue("encrypted PDFs should surface IsEncrypted=true");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ValidatePdfAsync_Stream_ThrowsWhenApiReturnsNull()
|
|
{
|
|
var handler = MockHttpMessageHandler.ReturningJson<PdfValidationResult?>(null);
|
|
var httpClient = new HttpClient(handler) { BaseAddress = new Uri("http://localhost") };
|
|
var client = new PdfValidationClient(httpClient, NullLogger<PdfValidationClient>.Instance);
|
|
|
|
await client.Invoking(c => c.ValidatePdfAsync(new MemoryStream(FakePdfBytes())))
|
|
.Should().ThrowAsync<InvalidOperationException>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ValidatePdfAsync_Stream_WhenApiReturns400_ThrowsHttpRequestException()
|
|
{
|
|
var (client, _) = BuildError(HttpStatusCode.BadRequest);
|
|
await client.Invoking(c => c.ValidatePdfAsync(new MemoryStream(FakePdfBytes())))
|
|
.Should().ThrowAsync<HttpRequestException>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ValidatePdfAsync_Stream_WhenApiReturns500_ThrowsHttpRequestException()
|
|
{
|
|
var (client, _) = BuildError(HttpStatusCode.InternalServerError);
|
|
await client.Invoking(c => c.ValidatePdfAsync(new MemoryStream(FakePdfBytes())))
|
|
.Should().ThrowAsync<HttpRequestException>();
|
|
}
|
|
|
|
// ── ValidatePdfAsync (byte[]) ────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public async Task ValidatePdfAsync_Bytes_SendsJsonWithBase64()
|
|
{
|
|
var expected = new PdfValidationResult(PageCount: 1, FileSizeBytes: 0, FileSizeMB: 0, PdfVersion: "", HasAttachments: true, AttachmentCount: 2, IsEncrypted: false);
|
|
var (client, handler) = Build(expected);
|
|
|
|
var result = await client.ValidatePdfAsync(FakePdfBytes());
|
|
|
|
result.PageCount.Should().Be(1);
|
|
result.HasAttachments.Should().BeTrue();
|
|
result.AttachmentCount.Should().Be(2);
|
|
handler.LastRequest!.Content!.Headers.ContentType!.MediaType.Should().Be("application/json");
|
|
var body = await handler.LastRequest.Content!.ReadAsStringAsync();
|
|
var doc = JsonDocument.Parse(body);
|
|
doc.RootElement.GetProperty("base64Pdf").GetString().Should().NotBeNullOrEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ValidatePdfAsync_Bytes_WhenApiReturns400_ThrowsHttpRequestException()
|
|
{
|
|
var (client, _) = BuildError(HttpStatusCode.BadRequest);
|
|
await client.Invoking(c => c.ValidatePdfAsync(FakePdfBytes()))
|
|
.Should().ThrowAsync<HttpRequestException>();
|
|
}
|
|
|
|
// ── ValidatePdfAAsync (Stream) ───────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public async Task ValidatePdfAAsync_Stream_DeserializesAllFields()
|
|
{
|
|
var expected = new PdfAValidationResult
|
|
{
|
|
IsValid = true, PdfAVersion = "PDF/A-3b", PageCount = 2,
|
|
Errors = [], Warnings = []
|
|
};
|
|
var (client, handler) = Build(expected);
|
|
|
|
var result = await client.ValidatePdfAAsync(new MemoryStream(FakePdfBytes()));
|
|
|
|
result.IsValid.Should().BeTrue();
|
|
result.PdfAVersion.Should().Be("PDF/A-3b");
|
|
result.PageCount.Should().Be(2);
|
|
result.Errors.Should().BeEmpty();
|
|
handler.LastRequest!.RequestUri!.PathAndQuery.Should().Be("/api/pdf/validation/validate-pdfa");
|
|
handler.LastRequest.Content.Should().BeOfType<MultipartFormDataContent>();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ValidatePdfAAsync_Stream_NonCompliant_ReturnsErrors()
|
|
{
|
|
var expected = new PdfAValidationResult
|
|
{
|
|
IsValid = false,
|
|
Errors = ["Missing embedded font", "Encryption not allowed"],
|
|
Warnings = ["Manual verification recommended"]
|
|
};
|
|
var (client, _) = Build(expected);
|
|
|
|
var result = await client.ValidatePdfAAsync(new MemoryStream(FakePdfBytes()));
|
|
|
|
result.IsValid.Should().BeFalse();
|
|
result.Errors.Should().HaveCount(2).And.Contain("Missing embedded font");
|
|
result.Warnings.Should().HaveCount(1);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ValidatePdfAAsync_Stream_WhenApiReturns400_ThrowsHttpRequestException()
|
|
{
|
|
var (client, _) = BuildError(HttpStatusCode.BadRequest);
|
|
await client.Invoking(c => c.ValidatePdfAAsync(new MemoryStream(FakePdfBytes())))
|
|
.Should().ThrowAsync<HttpRequestException>();
|
|
}
|
|
|
|
// ── ValidatePdfAAsync (byte[]) ───────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public async Task ValidatePdfAAsync_Bytes_SendsJsonToCorrectEndpoint()
|
|
{
|
|
var expected = new PdfAValidationResult { IsValid = true };
|
|
var (client, handler) = Build(expected);
|
|
|
|
await client.ValidatePdfAAsync(FakePdfBytes());
|
|
|
|
handler.LastRequest!.RequestUri!.PathAndQuery.Should().Be("/api/pdf/validation/validate-pdfa");
|
|
handler.LastRequest.Content!.Headers.ContentType!.MediaType.Should().Be("application/json");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ValidatePdfAAsync_Bytes_WhenApiReturns500_ThrowsHttpRequestException()
|
|
{
|
|
var (client, _) = BuildError(HttpStatusCode.InternalServerError);
|
|
await client.Invoking(c => c.ValidatePdfAAsync(FakePdfBytes()))
|
|
.Should().ThrowAsync<HttpRequestException>();
|
|
}
|
|
}
|