Refactor DTOs and tests; add framework-specific handling

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.
This commit is contained in:
2026-08-31 00:54:38 +02:00
parent 108e522316
commit 75bea9ef29
18 changed files with 65 additions and 133 deletions

View File

@@ -32,6 +32,10 @@ namespace DocumentService.Application.Common.DTOs;
/// }
/// </example>
public record SwissQrCodeExtractionResult(
#if NET
SwissQrBillDto Bill,
IReadOnlyList<string> RawLines
);
#else
object? Bill,
#endif
IEnumerable<string> RawLines
);

View File

@@ -52,10 +52,8 @@
<ItemGroup Condition="'$(TargetFramework)' != 'net8.0'">
<Compile Remove="Common\Interfaces\ISwissQrCodeProcessor.cs" />
<Compile Remove="Common\DTOs\SwissQrBillDto.cs" />
<Compile Remove="Common\DTOs\SwissQrCodeExtractionResult.cs" />
<Compile Remove="Common\Mapping\MappingProfile.cs" />
<Compile Remove="SwissQrCode\**" />
<Compile Remove="ExtractZugferd\**" />
<Compile Remove="HasZugferd\**" />
<Compile Remove="DependencyInjection.cs" />
</ItemGroup>

View File

@@ -18,6 +18,7 @@ public record ExtractZugferdCommand : IRequest<ZugferdExtractionResult>
public required Stream PdfStream { get; init; }
}
#if NET
/// <summary>
/// Handler for ExtractZugferdCommand.
/// Extracts ZUGFeRD XML from PDF and returns XML content
@@ -77,6 +78,7 @@ public class ExtractZugferdCommandHandler(
};
}
}
#endif
/// <summary>
/// Validator for ExtractZugferdCommand.

View File

@@ -1,3 +1,4 @@
using DocumentService.Application.Common.DTOs;
using DocumentService.Client.Interfaces;
using DocumentService.Client.Models.Requests;
using Microsoft.Extensions.Logging;

View File

@@ -1,3 +1,4 @@
using DocumentService.Application.Common.DTOs;
using DocumentService.Client.Interfaces;
using DocumentService.Client.Models.Requests;
using Microsoft.Extensions.Logging;

View File

@@ -1,3 +1,4 @@
using DocumentService.Application.Common.DTOs;
using DocumentService.Client.Interfaces;
using DocumentService.Client.Models.Requests;
using Microsoft.Extensions.Logging;

View File

@@ -1,3 +1,5 @@
using DocumentService.Application.Common.DTOs;
using DocumentService.Application.ExtractZugferd;
using DocumentService.Client.Interfaces;
using DocumentService.Client.Models.Requests;
using Microsoft.Extensions.Logging;

View File

@@ -1,3 +1,4 @@
using DocumentService.Application.Common.DTOs;
using DocumentService.Client.Models.Requests;
namespace DocumentService.Client.Interfaces;
@@ -60,30 +61,4 @@ public interface IPdfAttachmentClient
/// <returns>Modified PDF as stream</returns>
[Obsolete("API endpoint not implemented yet")]
Task<Stream> AddAttachmentsAsync(byte[] pdfBytes, List<AttachmentRequestDto> attachments, CancellationToken cancellationToken = default);
}
/// <summary>
/// DTO for attachment check result
/// </summary>
public record AttachmentCheckResult
{
/// <summary>Whether the document contains any embedded file attachments.</summary>
public bool HasAttachments { get; init; }
/// <summary>Number of embedded file attachments found.</summary>
public int AttachmentCount { get; init; }
/// <summary>Metadata for each attachment found in the document.</summary>
public List<AttachmentMetadata> Attachments { get; init; } = new();
}
/// <summary>
/// DTO for attachment metadata
/// </summary>
public record AttachmentMetadata
{
/// <summary>Name of the embedded file.</summary>
public string FileName { get; init; } = string.Empty;
/// <summary>MIME type of the embedded file (e.g. "application/xml"), or <c>null</c> if unknown.</summary>
public string? MimeType { get; init; }
/// <summary>Size of the embedded file in bytes.</summary>
public long Size { get; init; }
}
}

View File

@@ -1,3 +1,5 @@
using DocumentService.Application.Common.DTOs;
namespace DocumentService.Client.Interfaces;
/// <summary>
@@ -36,40 +38,4 @@ public interface IPdfValidationClient
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>PDF/A validation result with conformance level and errors</returns>
Task<PdfAValidationResult> ValidatePdfAAsync(byte[] pdfBytes, CancellationToken cancellationToken = default);
}
/// <summary>
/// DTO for PDF validation result
/// </summary>
public record PdfValidationResult
{
/// <summary>Total number of pages in the document.</summary>
public int PageCount { get; init; }
/// <summary>File size in bytes.</summary>
public long FileSizeBytes { get; init; }
/// <summary>PDF specification version (e.g. "1.7").</summary>
public string PdfVersion { get; init; } = string.Empty;
/// <summary>Whether the document is password-protected.</summary>
public bool IsEncrypted { get; init; }
/// <summary>Whether the document contains embedded file attachments.</summary>
public bool HasAttachments { get; init; }
/// <summary>Number of embedded file attachments.</summary>
public int AttachmentCount { get; init; }
}
/// <summary>
/// DTO for PDF/A validation result
/// </summary>
public record PdfAValidationResult
{
/// <summary>Whether the document is fully PDF/A conformant.</summary>
public bool IsValid { get; init; }
/// <summary>Detected PDF/A conformance level (e.g. "PDF/A-3b"), or <c>null</c> if not a PDF/A document.</summary>
public string? PdfAVersion { get; init; }
/// <summary>Total number of pages in the document.</summary>
public int PageCount { get; init; }
/// <summary>List of conformance errors found during validation.</summary>
public List<string> Errors { get; init; } = new();
/// <summary>List of conformance warnings found during validation.</summary>
public List<string> Warnings { get; init; } = new();
}
}

View File

@@ -1,3 +1,5 @@
using DocumentService.Application.Common.DTOs;
namespace DocumentService.Client.Interfaces;
/// <summary>
@@ -24,13 +26,3 @@ public interface ISwissQrCodeClient
Task<SwissQrCodeExtractionResult> ExtractSwissQrCodeAsync(byte[] pdfBytes, bool raw = false, CancellationToken cancellationToken = default);
}
/// <summary>
/// DTO for Swiss QR Code extraction result
/// </summary>
public record SwissQrCodeExtractionResult
{
/// <summary>Parsed Swiss QR bill object. <c>null</c> when <c>raw=true</c> was requested.</summary>
public object? Bill { get; init; }
/// <summary>Raw QR code text lines. Populated when <c>raw=true</c> was requested.</summary>
public List<string> RawLines { get; init; } = new();
}

View File

@@ -1,3 +1,6 @@
using DocumentService.Application.Common.DTOs;
using DocumentService.Application.ExtractZugferd;
namespace DocumentService.Client.Interfaces;
/// <summary>
@@ -53,27 +56,3 @@ public interface IZugferdClient
/// <returns>Structured ZUGFeRD extraction result</returns>
Task<ZugferdExtractionResult> ExtractZugferdAsResultAsync(byte[] pdfBytes, CancellationToken cancellationToken = default);
}
/// <summary>DTO for ZUGFeRD detection result</summary>
public record ZugferdCheckResult
{
/// <summary>Whether the document contains a ZUGFeRD XML attachment.</summary>
public bool HasZugferd { get; init; }
/// <summary>Detected ZUGFeRD version (e.g. "2.1"), or <c>null</c> if not present.</summary>
public string? Version { get; init; }
/// <summary>ZUGFeRD profile name (e.g. "EN 16931"), or <c>null</c> if not present.</summary>
public string? Profile { get; init; }
}
/// <summary>DTO for ZUGFeRD extraction result</summary>
public record ZugferdExtractionResult
{
/// <summary>File name of the extracted XML attachment (e.g. "factur-x.xml").</summary>
public string FileName { get; init; } = string.Empty;
/// <summary>Full XML content of the ZUGFeRD attachment.</summary>
public string XmlContent { get; init; } = string.Empty;
/// <summary>ZUGFeRD version (e.g. "2.1"), or <c>null</c> if not detected.</summary>
public string? Version { get; init; }
/// <summary>ZUGFeRD profile name (e.g. "EN 16931"), or <c>null</c> if not detected.</summary>
public string? Profile { get; init; }
}

View File

@@ -41,11 +41,11 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DocumentOperator.API\DocumentService.API.csproj" />
<ProjectReference Include="..\DocumentOperator.Domain\DocumentService.Domain.csproj" />
<ProjectReference Include="..\DocumentOperator.Application\DocumentService.Application.csproj" />
<ProjectReference Include="..\DocumentOperator.Infrastructure\DocumentService.Infrastructure.csproj" />
<ProjectReference Include="..\DocumentService.API\DocumentService.API.csproj" />
<ProjectReference Include="..\DocumentService.Application\DocumentService.Application.csproj" />
<ProjectReference Include="..\DocumentService.Client\DocumentService.Client.csproj" />
<ProjectReference Include="..\DocumentService.Domain\DocumentService.Domain.csproj" />
<ProjectReference Include="..\DocumentService.Infrastructure\DocumentService.Infrastructure.csproj" />
</ItemGroup>
</Project>

View File

@@ -1,3 +1,4 @@
using DocumentService.Application.Common.DTOs;
using DocumentService.Client.Clients;
using DocumentService.Client.Interfaces;
using DocumentService.Client.Models.Requests;
@@ -63,7 +64,7 @@ public class PdfAttachmentClientTests
{
HasAttachments = true,
AttachmentCount = 1,
Attachments = new List<AttachmentMetadata>
Attachments = new List<AttachmentDto>
{
new() { FileName = "factur-x.xml", MimeType = "application/xml", Size = 512 }
}
@@ -85,7 +86,7 @@ public class PdfAttachmentClientTests
public async Task CheckAttachmentsAsync_Stream_PdfWithNoAttachments_ReturnsFalse()
{
// Arrange
var expected = new AttachmentCheckResult { HasAttachments = false, AttachmentCount = 0 };
var expected = new AttachmentCheckResult { HasAttachments = false, AttachmentCount = 0, Attachments = [] };
var (client, _) = BuildJson(expected);
// Act

View File

@@ -2,6 +2,7 @@ using DocumentService.Client.Clients;
using DocumentService.Client.Interfaces;
using DocumentService.Client.Models.Requests;
using DocumentService.Client.Models.ValueObjects;
using DocumentService.Domain.Models.ValueObjects;
using FluentAssertions;
using Microsoft.Extensions.Logging.Abstractions;
using System.Net;

View File

@@ -1,3 +1,4 @@
using DocumentService.Application.Common.DTOs;
using DocumentService.Client.Clients;
using DocumentService.Client.Interfaces;
using FluentAssertions;
@@ -32,7 +33,7 @@ public class PdfValidationClientTests
[Fact]
public async Task ValidatePdfAsync_Stream_SendsMultipartPost()
{
var expected = new PdfValidationResult { PageCount = 3, PdfVersion = "1.7", FileSizeBytes = 2048, IsEncrypted = false };
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()));
@@ -49,7 +50,7 @@ public class PdfValidationClientTests
[Fact]
public async Task ValidatePdfAsync_Stream_EncryptedPdf_DeserializesIsEncryptedTrue()
{
var expected = new PdfValidationResult { PageCount = 1, IsEncrypted = true, PdfVersion = "1.6" };
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()));
@@ -89,7 +90,7 @@ public class PdfValidationClientTests
[Fact]
public async Task ValidatePdfAsync_Bytes_SendsJsonWithBase64()
{
var expected = new PdfValidationResult { PageCount = 1, IsEncrypted = false, HasAttachments = true, AttachmentCount = 2 };
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());

View File

@@ -1,3 +1,4 @@
using DocumentService.Application.Common.DTOs;
using DocumentService.Client.Clients;
using DocumentService.Client.Interfaces;
using FluentAssertions;
@@ -14,7 +15,7 @@ namespace DocumentService.Tests.Unit.Client;
/// </summary>
public class SwissQrCodeClientTests
{
// ?? helpers ?????????????????????????????????????????????????????????????
// ?? helpers ????????????????????????????????????????????????????????????
private static (SwissQrCodeClient client, MockHttpMessageHandler handler) Build<T>(T body)
{
@@ -26,13 +27,23 @@ public class SwissQrCodeClientTests
private static byte[] FakePdfBytes() => "fake-pdf-content"u8.ToArray();
// ?? ExtractSwissQrCodeAsync (Stream) — parsed Bill ???????????????????????
// ?? ExtractSwissQrCodeAsync (Stream) — parsed Bill ??????????????????????
[Fact]
public async Task ExtractSwissQrCodeAsync_Stream_ParsedMode_SendsMultipartToCorrectEndpoint()
{
// Arrange
var expected = new SwissQrCodeExtractionResult { Bill = new { Iban = "CH93-0076-2011-6238-5295-7" } };
var expected = new SwissQrCodeExtractionResult(
Bill: new SwissQrBillDto
{
Version = "V2_0",
Currency = "CHF",
Account = "CH9300762011623852957",
Creditor = new AddressDto { Name = "Test AG", Town = "Bern", CountryCode = "CH", Type = "S", PostalCode = "3000" },
ReferenceType = "NON"
},
RawLines: []
);
var (client, handler) = Build(expected);
// Act
@@ -48,7 +59,7 @@ public class SwissQrCodeClientTests
public async Task ExtractSwissQrCodeAsync_Stream_RawMode_SendsRawFlagInUrl()
{
// Arrange
var expected = new SwissQrCodeExtractionResult { RawLines = new List<string> { "SPC", "0200", "1" } };
var expected = new SwissQrCodeExtractionResult(Bill: null, RawLines: ["SPC", "0200", "1"]);
var (client, handler) = Build(expected);
// Act
@@ -59,13 +70,13 @@ public class SwissQrCodeClientTests
handler.LastRequest!.RequestUri!.PathAndQuery.Should().Be("/api/pdf/qr-code/extract-swiss?raw=True");
}
// ?? ExtractSwissQrCodeAsync (byte[]) ?????????????????????????????????????
// ?? ExtractSwissQrCodeAsync (byte[]) ????????????????????????????????????
[Fact]
public async Task ExtractSwissQrCodeAsync_Bytes_SendsJsonWithBase64()
{
// Arrange
var expected = new SwissQrCodeExtractionResult();
var expected = new SwissQrCodeExtractionResult(Bill: null, RawLines: []);
var (client, handler) = Build(expected);
// Act
@@ -83,7 +94,7 @@ public class SwissQrCodeClientTests
public async Task ExtractSwissQrCodeAsync_Bytes_RawMode_IncludesRawFlagInUrl()
{
// Arrange
var expected = new SwissQrCodeExtractionResult { RawLines = new List<string> { "SPC" } };
var expected = new SwissQrCodeExtractionResult(Bill: null, RawLines: ["SPC"]);
var (client, handler) = Build(expected);
// Act
@@ -93,7 +104,7 @@ public class SwissQrCodeClientTests
handler.LastRequest!.RequestUri!.PathAndQuery.Should().Be("/api/pdf/qr-code/extract-swiss?raw=True");
}
// ?? HTTP error propagation ???????????????????????????????????????????????
// ?? HTTP error propagation ??????????????????????????????????????????????
[Fact]
public async Task ExtractSwissQrCodeAsync_WhenApiReturns404_ThrowsHttpRequestException()

View File

@@ -1,3 +1,5 @@
using DocumentService.Application.Common.DTOs;
using DocumentService.Application.ExtractZugferd;
using DocumentService.Client.Clients;
using DocumentService.Client.Interfaces;
using FluentAssertions;
@@ -41,7 +43,7 @@ public class ZugferdClientTests
public async Task HasZugferdAsync_Stream_WhenZugferdPresent_ReturnsTrue()
{
// Arrange
var expected = new ZugferdCheckResult { HasZugferd = true, Version = "2.1", Profile = "EN 16931" };
var expected = new ZugferdCheckResult { HasZugferd = true, ZugferdFileName = "factur-x.xml" };
var (client, handler) = BuildJson(expected);
// Act
@@ -49,8 +51,6 @@ public class ZugferdClientTests
// Assert
result.HasZugferd.Should().BeTrue();
result.Version.Should().Be("2.1");
result.Profile.Should().Be("EN 16931");
handler.LastRequest!.RequestUri!.PathAndQuery.Should().Be("/api/pdf/zugferd/has-zugferd");
handler.LastRequest.Content.Should().BeOfType<MultipartFormDataContent>();
}
@@ -67,7 +67,7 @@ public class ZugferdClientTests
// Assert
result.HasZugferd.Should().BeFalse();
result.Version.Should().BeNull();
result.ZugferdFileName.Should().BeNull();
}
// ?? HasZugferdAsync (byte[]) ?????????????????????????????????????????????
@@ -149,8 +149,6 @@ public class ZugferdClientTests
{
FileName = "factur-x.xml",
XmlContent = "<invoice/>",
Version = "2.1",
Profile = "EN 16931"
};
var (client, handler) = BuildJson(expected);
@@ -160,7 +158,6 @@ public class ZugferdClientTests
// Assert
result.FileName.Should().Be("factur-x.xml");
result.XmlContent.Should().Be("<invoice/>");
result.Version.Should().Be("2.1");
handler.LastRequest!.RequestUri!.PathAndQuery.Should().Be("/api/pdf/zugferd/extract?asFile=false");
}

View File

@@ -3,8 +3,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.37328.6
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DocumentService.Tests", "DocumentOperator.Tests\DocumentService.Tests.csproj", "{32D2E997-3DA7-4061-8A50-DBB34BBC3E5A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{3F9E8D8E-1234-4567-89AB-CDEF01234567}"
ProjectSection(SolutionItems) = preProject
AGENTS.md = AGENTS.md
@@ -40,16 +38,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DocumentService.Application
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DocumentService.Domain", "DocumentService.Domain\DocumentService.Domain.csproj", "{2F2C937F-3732-4E6C-9720-FAB62DC33C63}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DocumentService.Tests", "DocumentService.Tests\DocumentService.Tests.csproj", "{ECD5BC60-7009-4099-7193-526FDC6E8D99}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{32D2E997-3DA7-4061-8A50-DBB34BBC3E5A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{32D2E997-3DA7-4061-8A50-DBB34BBC3E5A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{32D2E997-3DA7-4061-8A50-DBB34BBC3E5A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{32D2E997-3DA7-4061-8A50-DBB34BBC3E5A}.Release|Any CPU.Build.0 = Release|Any CPU
{626E2002-8EC1-4FF9-A9A6-51D5809E26FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{626E2002-8EC1-4FF9-A9A6-51D5809E26FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{626E2002-8EC1-4FF9-A9A6-51D5809E26FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -70,12 +66,15 @@ Global
{2F2C937F-3732-4E6C-9720-FAB62DC33C63}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2F2C937F-3732-4E6C-9720-FAB62DC33C63}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2F2C937F-3732-4E6C-9720-FAB62DC33C63}.Release|Any CPU.Build.0 = Release|Any CPU
{ECD5BC60-7009-4099-7193-526FDC6E8D99}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ECD5BC60-7009-4099-7193-526FDC6E8D99}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ECD5BC60-7009-4099-7193-526FDC6E8D99}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ECD5BC60-7009-4099-7193-526FDC6E8D99}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{32D2E997-3DA7-4061-8A50-DBB34BBC3E5A} = {D64772B8-6213-4031-8064-2567F1E724F6}
{02EA681E-C7D8-13C7-8484-4AC65E1B71E8} = {3F9E8D8E-1234-4567-89AB-CDEF01234567}
{0B80DDC4-A13C-4C5F-8520-AA2F3650E884} = {2B18D2F8-40C1-4CA0-99AB-7F98F254FEB8}
{62E8F4B3-EA49-41D2-8843-0636EB4421BB} = {2B18D2F8-40C1-4CA0-99AB-7F98F254FEB8}
@@ -85,6 +84,7 @@ Global
{A5AB3D2B-D4A8-1BE2-CACE-B5DBF9A40E51} = {9BACC18A-5C6D-4678-A43C-A19907AE9CF3}
{459EDCC5-240E-A2BC-1330-B5B2326B679C} = {0B80DDC4-A13C-4C5F-8520-AA2F3650E884}
{2F2C937F-3732-4E6C-9720-FAB62DC33C63} = {0B80DDC4-A13C-4C5F-8520-AA2F3650E884}
{ECD5BC60-7009-4099-7193-526FDC6E8D99} = {D64772B8-6213-4031-8064-2567F1E724F6}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {832CA90A-06D6-4312-9B35-16CC665EB37C}