test(file-print): extend booking document regression

This commit is contained in:
2026-08-31 20:56:52 +08:00
parent 0cc4eae209
commit 936412f351
8 changed files with 212 additions and 2 deletions
@@ -93,6 +93,19 @@ class InternalPdfControllerTest {
verify(service, never()).generate(any());
}
@Test
void rejectsFrontendStyleFreightPrintWithoutInternalToken() throws Exception {
mockMvc.perform(post("/internal/print/v1/pdfs/freight-a4-documents")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_PDF, MediaType.APPLICATION_JSON)
.content(validFreightA4RequestJson()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.code").value("A0403"));
verify(freightA4PdfService, never()).generate(any());
}
@Test
void returnsServiceValidationFailureAsJson() throws Exception {
when(service.generate(any())).thenThrow(
@@ -0,0 +1,78 @@
package com.baiyue.print.service;
import com.baiyue.print.config.PrintPdfProperties;
import com.baiyue.print.dto.FreightA4PdfDtos;
import com.baiyue.print.renderer.FreightA4Renderer;
import jakarta.annotation.Resource;
import org.apache.pdfbox.Loader;
import org.junit.jupiter.api.Test;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import static org.assertj.core.api.Assertions.assertThat;
@SpringJUnitConfig(classes = {FreightA4PdfService.class, PrintPdfProperties.class,
PdfGenerationLimiter.class, FreightA4Renderer.class})
class FreightA4ConcurrentGenerationTest {
@Resource private FreightA4PdfService service;
@Test
void generatesBothTemplatesConcurrentlyAsIndependentParseableA4Pdfs() throws Exception {
var executor = Executors.newFixedThreadPool(2);
try {
var shipping = CompletableFuture.supplyAsync(() -> service.generate(shippingRequest()), executor);
var bill = CompletableFuture.supplyAsync(() -> service.generate(billRequest()), executor);
CompletableFuture.allOf(shipping, bill).get();
assertPdf(shipping.get(), "OB-260831-0001-出口货物托运单.pdf");
assertPdf(bill.get(), "BL-260831-0001-提单确认件.pdf");
} finally {
executor.shutdownNow();
}
}
private static void assertPdf(FreightA4PdfService.GeneratedPdf generated, String fileName) throws Exception {
assertThat(generated.fileName()).isEqualTo(fileName);
assertThat(generated.pageCount()).isEqualTo(1);
assertThat(generated.content()).startsWith("%PDF-".getBytes(StandardCharsets.US_ASCII));
try (var pdf = Loader.loadPDF(generated.content())) {
assertThat(pdf.getNumberOfPages()).isEqualTo(1);
assertThat(pdf.getPage(0).getMediaBox().getWidth()).isBetween(595f, 596f);
assertThat(pdf.getPage(0).getMediaBox().getHeight()).isBetween(841f, 843f);
}
}
private static FreightA4PdfDtos.Request shippingRequest() {
return request(FreightA4PdfDtos.SHIPPING_INSTRUCTION_TEMPLATE,
"OB-260831-0001-出口货物托运单.pdf", Map.of(
"toCompany", "订舱代理", "shipper", "SHIPPER", "consignee", "CONSIGNEE",
"notifyParty", "NOTIFY", "jobNumber", "OB-260831-0001", "cargoQuantity", "1*40HQ",
"portOfLoading", "NINGBO", "portOfDischarge", "LOS ANGELES",
"placeOfDelivery", "LOS ANGELES", "totalPackagesInWords", "SAY ONE CONTAINER ONLY."));
}
private static FreightA4PdfDtos.Request billRequest() {
return request(FreightA4PdfDtos.BILL_CONFIRMATION_TEMPLATE,
"BL-260831-0001-提单确认件.pdf", Map.of(
"shipper", "SHIPPER", "consignee", "CONSIGNEE", "notifyParty", "NOTIFY",
"bookingNumber", "BL-260831-0001", "portOfLoading", "NINGBO",
"portOfDischarge", "LOS ANGELES", "placeOfDelivery", "LOS ANGELES",
"serviceTerm", "CY-CY", "freightTerm", "FREIGHT PREPAID",
"totalPackagesInWords", "SAY ONE CONTAINER ONLY."));
}
private static FreightA4PdfDtos.Request request(String template, String fileName,
Map<String, String> fields) {
var cargo = new FreightA4PdfDtos.CargoRow("N/M", "1", "CONTAINER", "GENERAL CARGO",
"1000 KGS", "10 CBM");
return new FreightA4PdfDtos.Request(template,
new FreightA4PdfDtos.PageSpec(new BigDecimal("210"), new BigDecimal("297")), fileName,
List.of(new FreightA4PdfDtos.Document(fields, List.of(cargo))));
}
}