From 0b7ac3eac132bb1347ea2452e553309c10645790 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ianar=C3=A9=20S=C3=A9vi?= Date: Fri, 27 Dec 2024 10:52:17 +0100 Subject: [PATCH] :sparkles: add support for us mail v3 --- docs/code_samples/us_mail_v3_async.txt | 41 ++++++ docs/{us_mail_v2.md => us_mail_v3.md} | 54 ++++++-- .../java/com/mindee/CommandLineInterface.java | 18 +++ .../com/mindee/parsing/SummaryHelper.java | 5 +- .../mindee/parsing/standard/BooleanField.java | 3 +- .../mindee/parsing/standard/StringField.java | 3 +- .../mindee/product/us/usmail/UsMailV3.java | 16 +++ .../product/us/usmail/UsMailV3Document.java | 100 ++++++++++++++ .../us/usmail/UsMailV3RecipientAddress.java | 128 ++++++++++++++++++ .../us/usmail/UsMailV3SenderAddress.java | 86 ++++++++++++ .../product/us/usmail/UsMailV3Test.java | 58 ++++++++ src/test/resources | 2 +- 12 files changed, 496 insertions(+), 18 deletions(-) create mode 100644 docs/code_samples/us_mail_v3_async.txt rename docs/{us_mail_v2.md => us_mail_v3.md} (79%) create mode 100644 src/main/java/com/mindee/product/us/usmail/UsMailV3.java create mode 100644 src/main/java/com/mindee/product/us/usmail/UsMailV3Document.java create mode 100644 src/main/java/com/mindee/product/us/usmail/UsMailV3RecipientAddress.java create mode 100644 src/main/java/com/mindee/product/us/usmail/UsMailV3SenderAddress.java create mode 100644 src/test/java/com/mindee/product/us/usmail/UsMailV3Test.java diff --git a/docs/code_samples/us_mail_v3_async.txt b/docs/code_samples/us_mail_v3_async.txt new file mode 100644 index 000000000..cff5c25d2 --- /dev/null +++ b/docs/code_samples/us_mail_v3_async.txt @@ -0,0 +1,41 @@ +import com.mindee.MindeeClient; +import com.mindee.input.LocalInputSource; +import com.mindee.parsing.common.AsyncPredictResponse; +import com.mindee.product.us.usmail.UsMailV3; +import java.io.File; +import java.io.IOException; + +public class SimpleMindeeClient { + + public static void main(String[] args) throws IOException, InterruptedException { + String apiKey = "my-api-key"; + String filePath = "/path/to/the/file.ext"; + + // Init a new client + MindeeClient mindeeClient = new MindeeClient(apiKey); + + // Load a file from disk + LocalInputSource inputSource = new LocalInputSource(new File(filePath)); + + // Parse the file asynchronously + AsyncPredictResponse response = mindeeClient.enqueueAndParse( + UsMailV3.class, + inputSource + ); + + // Print a summary of the response + System.out.println(response.toString()); + + // Print a summary of the predictions +// System.out.println(response.getDocumentObj().toString()); + + // Print the document-level predictions +// System.out.println(response.getDocumentObj().getInference().getPrediction().toString()); + + // Print the page-level predictions +// response.getDocumentObj().getInference().getPages().forEach( +// page -> System.out.println(page.toString()) +// ); + } + +} diff --git a/docs/us_mail_v2.md b/docs/us_mail_v3.md similarity index 79% rename from docs/us_mail_v2.md rename to docs/us_mail_v3.md index 29a97483c..d2a086b8e 100644 --- a/docs/us_mail_v2.md +++ b/docs/us_mail_v3.md @@ -14,7 +14,7 @@ Using the [sample below](https://github.com/mindee/client-lib-test-data/blob/mai import com.mindee.MindeeClient; import com.mindee.input.LocalInputSource; import com.mindee.parsing.common.AsyncPredictResponse; -import com.mindee.product.us.usmail.UsMailV2; +import com.mindee.product.us.usmail.UsMailV3; import java.io.File; import java.io.IOException; @@ -31,8 +31,8 @@ public class SimpleMindeeClient { LocalInputSource inputSource = new LocalInputSource(new File(filePath)); // Parse the file asynchronously - AsyncPredictResponse response = mindeeClient.enqueueAndParse( - UsMailV2.class, + AsyncPredictResponse response = mindeeClient.enqueueAndParse( + UsMailV3.class, inputSource ); @@ -57,7 +57,20 @@ public class SimpleMindeeClient { **Output (RST):** ```rst -:Sender Name: zed +######## +Document +######## +:Mindee ID: f9c36f59-977d-4ddc-9f2d-31c294c456ac +:Filename: default_sample.jpg + +Inference +######### +:Product: mindee/us_mail v3.0 +:Rotation applied: Yes + +Prediction +========== +:Sender Name: company zed :Sender Address: :City: Dallas :Complete Address: 54321 Elm Street, Dallas, Texas 54321 @@ -66,11 +79,12 @@ public class SimpleMindeeClient { :Street: 54321 Elm Street :Recipient Names: Jane Doe :Recipient Addresses: - +-----------------+-------------------------------------+-------------------+-------------+------------------------+-------+---------------------------+ - | City | Complete Address | Is Address Change | Postal Code | Private Mailbox Number | State | Street | - +=================+=====================================+===================+=============+========================+=======+===========================+ - | Detroit | 1234 Market Street PMB 4321, Det... | | 12345 | 4321 | MI | 1234 Market Street | - +-----------------+-------------------------------------+-------------------+-------------+------------------------+-------+---------------------------+ + +-----------------+-------------------------------------+-------------------+-------------+------------------------+-------+---------------------------+-----------------+ + | City | Complete Address | Is Address Change | Postal Code | Private Mailbox Number | State | Street | Unit | + +=================+=====================================+===================+=============+========================+=======+===========================+=================+ + | Detroit | 1234 Market Street PMB 4321, Det... | False | 12345 | 4321 | MI | 1234 Market Street | | + +-----------------+-------------------------------------+-------------------+-------------+------------------------+-------+---------------------------+-----------------+ +:Return to Sender: False ``` # Field Types @@ -96,13 +110,17 @@ The text field `StringField` extends `BaseField`, but also implements: * **value** (`String`): corresponds to the field value. * **rawValue** (`String`): corresponds to the raw value as it appears on the document. +### BooleanField +The boolean field `BooleanField` extends BaseField, but also implements: +* **value** (`Boolean`): corresponds to the value of the field. + ## Specific Fields Fields which are specific to this product; they are not used in any other product. ### Recipient Addresses Field The addresses of the recipients. -A `UsMailV2RecipientAddress` implements the following attributes: +A `UsMailV3RecipientAddress` implements the following attributes: * **city** (`String`): The city of the recipient's address. * **complete** (`String`): The complete address of the recipient. @@ -111,12 +129,13 @@ A `UsMailV2RecipientAddress` implements the following attributes: * **privateMailboxNumber** (`String`): The private mailbox number of the recipient's address. * **state** (`String`): Second part of the ISO 3166-2 code, consisting of two letters indicating the US State. * **street** (`String`): The street of the recipient's address. +* **unit** (`String`): The unit number of the recipient's address. Fields which are specific to this product; they are not used in any other product. ### Sender Address Field The address of the sender. -A `UsMailV2SenderAddress` implements the following attributes: +A `UsMailV3SenderAddress` implements the following attributes: * **city** (`String`): The city of the sender's address. * **complete** (`String`): The complete address of the sender. @@ -125,10 +144,17 @@ A `UsMailV2SenderAddress` implements the following attributes: * **street** (`String`): The street of the sender's address. # Attributes -The following fields are extracted for US Mail V2: +The following fields are extracted for US Mail V3: + +## Return to Sender +**isReturnToSender**: Whether the mailing is marked as return to sender. + +```java +System.out.println(result.getDocument().getInference().getPrediction().getIsReturnToSender().value); +``` ## Recipient Addresses -**recipientAddresses**(List<[UsMailV2RecipientAddress](#recipient-addresses-field)>): The addresses of the recipients. +**recipientAddresses**(List<[UsMailV3RecipientAddress](#recipient-addresses-field)>): The addresses of the recipients. ```java for (recipientAddressesElem : result.getDocument().getInference().getPrediction().getRecipientAddresses()) @@ -148,7 +174,7 @@ for (recipientNamesElem : result.getDocument().getInference().getPrediction().ge ``` ## Sender Address -**senderAddress**([UsMailV2SenderAddress](#sender-address-field)): The address of the sender. +**senderAddress**([UsMailV3SenderAddress](#sender-address-field)): The address of the sender. ```java System.out.println(result.getDocument().getInference().getPrediction().getSenderAddress().value); diff --git a/src/main/java/com/mindee/CommandLineInterface.java b/src/main/java/com/mindee/CommandLineInterface.java index 800a9272c..57a54d569 100644 --- a/src/main/java/com/mindee/CommandLineInterface.java +++ b/src/main/java/com/mindee/CommandLineInterface.java @@ -9,12 +9,14 @@ import com.mindee.parsing.common.Inference; import com.mindee.parsing.common.PredictResponse; import com.mindee.product.custom.CustomV1; +import com.mindee.product.financialdocument.FinancialDocumentV1; import com.mindee.product.internationalid.InternationalIdV2; import com.mindee.product.invoice.InvoiceV4; import com.mindee.product.invoicesplitter.InvoiceSplitterV1; import com.mindee.product.multireceiptsdetector.MultiReceiptsDetectorV1; import com.mindee.product.passport.PassportV1; import com.mindee.product.receipt.ReceiptV4; +import com.mindee.product.us.usmail.UsMailV3; import java.io.File; import java.io.IOException; import java.util.ArrayList; @@ -105,6 +107,14 @@ void receiptMethod( System.out.println(standardProductOutput(ReceiptV4.class, file)); } + @Command(name = "financial-document", description = "Parse using Financial Document") + void financialDocumentMethod( + @Parameters(index = "0", paramLabel = "", scope = ScopeType.LOCAL) + File file + ) throws IOException { + System.out.println(standardProductOutput(FinancialDocumentV1.class, file)); + } + @Command(name = "multi-receipt-detector", description = "Parse using Multi Receipts Detector") void multiReceiptDetectorMethod( @Parameters(index = "0", paramLabel = "", scope = ScopeType.LOCAL) @@ -137,6 +147,14 @@ void internationalIdMethod( System.out.println(standardProductAsyncOutput(InternationalIdV2.class, file)); } + @Command(name = "us-mail", description = "Parse using US Mail") + void usMailMethod( + @Parameters(index = "0", paramLabel = "", scope = ScopeType.LOCAL) + File file + ) throws IOException, InterruptedException { + System.out.println(standardProductAsyncOutput(UsMailV3.class, file)); + } + @Command(name = "custom", description = "Invokes a Custom API") void customMethod( @Option( diff --git a/src/main/java/com/mindee/parsing/SummaryHelper.java b/src/main/java/com/mindee/parsing/SummaryHelper.java index c3954747e..9060ea93e 100644 --- a/src/main/java/com/mindee/parsing/SummaryHelper.java +++ b/src/main/java/com/mindee/parsing/SummaryHelper.java @@ -55,7 +55,10 @@ public static String formatForDisplay(String inputValue, Integer maxColSize) { if (inputValue == null || inputValue.isEmpty()) { return ""; } - String outputValue = inputValue.replace("\n", "\\n").replace("\t", "\\t").replace("\r", "\\r"); + String outputValue = inputValue + .replace("\n", "\\n") + .replace("\t", "\\t") + .replace("\r", "\\r"); if (maxColSize == null || outputValue.length() <= maxColSize) { return outputValue; } else { diff --git a/src/main/java/com/mindee/parsing/standard/BooleanField.java b/src/main/java/com/mindee/parsing/standard/BooleanField.java index 89cbfafea..ebf650258 100644 --- a/src/main/java/com/mindee/parsing/standard/BooleanField.java +++ b/src/main/java/com/mindee/parsing/standard/BooleanField.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.mindee.geometry.Polygon; import com.mindee.geometry.PolygonDeserializer; +import com.mindee.parsing.SummaryHelper; import lombok.Getter; @@ -45,7 +46,7 @@ public boolean isEmpty() { */ @Override public String toString() { - return this.value == null ? "" : value.toString(); + return SummaryHelper.formatForDisplay(this.value, null); } } diff --git a/src/main/java/com/mindee/parsing/standard/StringField.java b/src/main/java/com/mindee/parsing/standard/StringField.java index d10fdf5d4..f46f6c726 100644 --- a/src/main/java/com/mindee/parsing/standard/StringField.java +++ b/src/main/java/com/mindee/parsing/standard/StringField.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.mindee.geometry.Polygon; import com.mindee.geometry.PolygonDeserializer; +import com.mindee.parsing.SummaryHelper; import lombok.Getter; /** @@ -56,6 +57,6 @@ public boolean isEmpty() { @Override public String toString() { - return value == null ? "" : value; + return SummaryHelper.formatString(this.value); } } diff --git a/src/main/java/com/mindee/product/us/usmail/UsMailV3.java b/src/main/java/com/mindee/product/us/usmail/UsMailV3.java new file mode 100644 index 000000000..22a31eda4 --- /dev/null +++ b/src/main/java/com/mindee/product/us/usmail/UsMailV3.java @@ -0,0 +1,16 @@ +package com.mindee.product.us.usmail; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.mindee.http.EndpointInfo; +import com.mindee.parsing.common.Inference; +import lombok.Getter; + +/** + * US Mail API version 3 inference prediction. + */ +@Getter +@JsonIgnoreProperties(ignoreUnknown = true) +@EndpointInfo(endpointName = "us_mail", version = "3") +public class UsMailV3 + extends Inference { +} diff --git a/src/main/java/com/mindee/product/us/usmail/UsMailV3Document.java b/src/main/java/com/mindee/product/us/usmail/UsMailV3Document.java new file mode 100644 index 000000000..7c6766470 --- /dev/null +++ b/src/main/java/com/mindee/product/us/usmail/UsMailV3Document.java @@ -0,0 +1,100 @@ +package com.mindee.product.us.usmail; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.mindee.parsing.SummaryHelper; +import com.mindee.parsing.common.Prediction; +import com.mindee.parsing.standard.BooleanField; +import com.mindee.parsing.standard.StringField; +import java.util.ArrayList; +import java.util.List; +import lombok.EqualsAndHashCode; +import lombok.Getter; + +/** + * US Mail API version 3.0 document data. + */ +@Getter +@EqualsAndHashCode(callSuper = false) +@JsonIgnoreProperties(ignoreUnknown = true) +public class UsMailV3Document extends Prediction { + + /** + * Whether the mailing is marked as return to sender. + */ + @JsonProperty("is_return_to_sender") + protected BooleanField isReturnToSender; + /** + * The addresses of the recipients. + */ + @JsonProperty("recipient_addresses") + protected List recipientAddresses = new ArrayList<>(); + /** + * The names of the recipients. + */ + @JsonProperty("recipient_names") + protected List recipientNames = new ArrayList<>(); + /** + * The address of the sender. + */ + @JsonProperty("sender_address") + protected UsMailV3SenderAddress senderAddress; + /** + * The name of the sender. + */ + @JsonProperty("sender_name") + protected StringField senderName; + + @Override + public boolean isEmpty() { + return ( + this.senderName == null + && this.senderAddress == null + && (this.recipientNames == null || this.recipientNames.isEmpty()) + && (this.recipientAddresses == null || this.recipientAddresses.isEmpty()) + && this.isReturnToSender == null + ); + } + + @Override + public String toString() { + StringBuilder outStr = new StringBuilder(); + outStr.append( + String.format(":Sender Name: %s%n", this.getSenderName()) + ); + outStr.append( + String.format(":Sender Address:%n%s", this.getSenderAddress().toFieldList()) + ); + String recipientNames = SummaryHelper.arrayToString( + this.getRecipientNames(), + "%n " + ); + outStr.append( + String.format(":Recipient Names: %s%n", recipientNames) + ); + String recipientAddressesSummary = ""; + if (!this.getRecipientAddresses().isEmpty()) { + int[] recipientAddressesColSizes = new int[]{17, 37, 19, 13, 24, 7, 27, 17}; + recipientAddressesSummary = + String.format("%n%s%n ", SummaryHelper.lineSeparator(recipientAddressesColSizes, "-")) + + "| City " + + "| Complete Address " + + "| Is Address Change " + + "| Postal Code " + + "| Private Mailbox Number " + + "| State " + + "| Street " + + "| Unit " + + String.format("|%n%s%n ", SummaryHelper.lineSeparator(recipientAddressesColSizes, "=")); + recipientAddressesSummary += SummaryHelper.arrayToString(this.getRecipientAddresses(), recipientAddressesColSizes); + recipientAddressesSummary += String.format("%n%s", SummaryHelper.lineSeparator(recipientAddressesColSizes, "-")); + } + outStr.append( + String.format(":Recipient Addresses: %s%n", recipientAddressesSummary) + ); + outStr.append( + String.format(":Return to Sender: %s%n", this.getIsReturnToSender()) + ); + return SummaryHelper.cleanSummary(outStr.toString()); + } +} diff --git a/src/main/java/com/mindee/product/us/usmail/UsMailV3RecipientAddress.java b/src/main/java/com/mindee/product/us/usmail/UsMailV3RecipientAddress.java new file mode 100644 index 000000000..a126d2527 --- /dev/null +++ b/src/main/java/com/mindee/product/us/usmail/UsMailV3RecipientAddress.java @@ -0,0 +1,128 @@ +package com.mindee.product.us.usmail; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.mindee.parsing.SummaryHelper; +import com.mindee.parsing.standard.BaseField; +import com.mindee.parsing.standard.LineItemField; +import java.util.HashMap; +import java.util.Map; +import lombok.Getter; + +/** + * The addresses of the recipients. + */ +@Getter +@JsonIgnoreProperties(ignoreUnknown = true) +public class UsMailV3RecipientAddress extends BaseField implements LineItemField { + + /** + * The city of the recipient's address. + */ + @JsonProperty("city") + String city; + /** + * The complete address of the recipient. + */ + @JsonProperty("complete") + String complete; + /** + * Indicates if the recipient's address is a change of address. + */ + @JsonProperty("is_address_change") + Boolean isAddressChange; + /** + * The postal code of the recipient's address. + */ + @JsonProperty("postal_code") + String postalCode; + /** + * The private mailbox number of the recipient's address. + */ + @JsonProperty("private_mailbox_number") + String privateMailboxNumber; + /** + * Second part of the ISO 3166-2 code, consisting of two letters indicating the US State. + */ + @JsonProperty("state") + String state; + /** + * The street of the recipient's address. + */ + @JsonProperty("street") + String street; + /** + * The unit number of the recipient's address. + */ + @JsonProperty("unit") + String unit; + + public boolean isEmpty() { + return ( + (city == null || city.isEmpty()) + && (complete == null || complete.isEmpty()) + && isAddressChange == null + && (postalCode == null || postalCode.isEmpty()) + && (privateMailboxNumber == null || privateMailboxNumber.isEmpty()) + && (state == null || state.isEmpty()) + && (street == null || street.isEmpty()) + && (unit == null || unit.isEmpty()) + ); + } + + private Map tablePrintableValues() { + Map printable = new HashMap<>(); + + printable.put("city", SummaryHelper.formatForDisplay(this.city, 15)); + printable.put("complete", SummaryHelper.formatForDisplay(this.complete, 35)); + printable.put("isAddressChange", SummaryHelper.formatForDisplay(this.isAddressChange, null)); + printable.put("postalCode", SummaryHelper.formatForDisplay(this.postalCode, null)); + printable.put("privateMailboxNumber", SummaryHelper.formatForDisplay(this.privateMailboxNumber, null)); + printable.put("state", SummaryHelper.formatForDisplay(this.state, null)); + printable.put("street", SummaryHelper.formatForDisplay(this.street, 25)); + printable.put("unit", SummaryHelper.formatForDisplay(this.unit, 15)); + return printable; + } + + /** + * Output the line in a format suitable for inclusion in an rST table. + */ + public String toTableLine() { + Map printable = this.tablePrintableValues(); + return String.format("| %-15s ", printable.get("city")) + + String.format("| %-35s ", printable.get("complete")) + + String.format("| %-17s ", printable.get("isAddressChange")) + + String.format("| %-11s ", printable.get("postalCode")) + + String.format("| %-22s ", printable.get("privateMailboxNumber")) + + String.format("| %-5s ", printable.get("state")) + + String.format("| %-25s ", printable.get("street")) + + String.format("| %-15s |", printable.get("unit")); + } + + @Override + public String toString() { + Map printable = this.printableValues(); + return String.format("City: %s", printable.get("city")) + + String.format(", Complete Address: %s", printable.get("complete")) + + String.format(", Is Address Change: %s", printable.get("isAddressChange")) + + String.format(", Postal Code: %s", printable.get("postalCode")) + + String.format(", Private Mailbox Number: %s", printable.get("privateMailboxNumber")) + + String.format(", State: %s", printable.get("state")) + + String.format(", Street: %s", printable.get("street")) + + String.format(", Unit: %s", printable.get("unit")); + } + + private Map printableValues() { + Map printable = new HashMap<>(); + + printable.put("city", SummaryHelper.formatForDisplay(this.city, null)); + printable.put("complete", SummaryHelper.formatForDisplay(this.complete, null)); + printable.put("isAddressChange", SummaryHelper.formatForDisplay(this.isAddressChange, null)); + printable.put("postalCode", SummaryHelper.formatForDisplay(this.postalCode, null)); + printable.put("privateMailboxNumber", SummaryHelper.formatForDisplay(this.privateMailboxNumber, null)); + printable.put("state", SummaryHelper.formatForDisplay(this.state, null)); + printable.put("street", SummaryHelper.formatForDisplay(this.street, null)); + printable.put("unit", SummaryHelper.formatForDisplay(this.unit, null)); + return printable; + } +} diff --git a/src/main/java/com/mindee/product/us/usmail/UsMailV3SenderAddress.java b/src/main/java/com/mindee/product/us/usmail/UsMailV3SenderAddress.java new file mode 100644 index 000000000..1206ba06c --- /dev/null +++ b/src/main/java/com/mindee/product/us/usmail/UsMailV3SenderAddress.java @@ -0,0 +1,86 @@ +package com.mindee.product.us.usmail; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.mindee.parsing.SummaryHelper; +import com.mindee.parsing.standard.BaseField; +import java.util.HashMap; +import java.util.Map; +import lombok.Getter; + +/** + * The address of the sender. + */ +@Getter +@JsonIgnoreProperties(ignoreUnknown = true) +public class UsMailV3SenderAddress extends BaseField { + + /** + * The city of the sender's address. + */ + @JsonProperty("city") + String city; + /** + * The complete address of the sender. + */ + @JsonProperty("complete") + String complete; + /** + * The postal code of the sender's address. + */ + @JsonProperty("postal_code") + String postalCode; + /** + * Second part of the ISO 3166-2 code, consisting of two letters indicating the US State. + */ + @JsonProperty("state") + String state; + /** + * The street of the sender's address. + */ + @JsonProperty("street") + String street; + + public boolean isEmpty() { + return ( + (city == null || city.isEmpty()) + && (complete == null || complete.isEmpty()) + && (postalCode == null || postalCode.isEmpty()) + && (state == null || state.isEmpty()) + && (street == null || street.isEmpty()) + ); + } + + /** + * Output the object in a format suitable for inclusion in an rST field list. + */ + public String toFieldList() { + Map printable = this.printableValues(); + return String.format(" :City: %s%n", printable.get("city")) + + String.format(" :Complete Address: %s%n", printable.get("complete")) + + String.format(" :Postal Code: %s%n", printable.get("postalCode")) + + String.format(" :State: %s%n", printable.get("state")) + + String.format(" :Street: %s%n", printable.get("street")); + } + + @Override + public String toString() { + Map printable = this.printableValues(); + return String.format("City: %s", printable.get("city")) + + String.format(", Complete Address: %s", printable.get("complete")) + + String.format(", Postal Code: %s", printable.get("postalCode")) + + String.format(", State: %s", printable.get("state")) + + String.format(", Street: %s", printable.get("street")); + } + + private Map printableValues() { + Map printable = new HashMap<>(); + + printable.put("city", SummaryHelper.formatForDisplay(this.city, null)); + printable.put("complete", SummaryHelper.formatForDisplay(this.complete, null)); + printable.put("postalCode", SummaryHelper.formatForDisplay(this.postalCode, null)); + printable.put("state", SummaryHelper.formatForDisplay(this.state, null)); + printable.put("street", SummaryHelper.formatForDisplay(this.street, null)); + return printable; + } +} diff --git a/src/test/java/com/mindee/product/us/usmail/UsMailV3Test.java b/src/test/java/com/mindee/product/us/usmail/UsMailV3Test.java new file mode 100644 index 000000000..5a6168b48 --- /dev/null +++ b/src/test/java/com/mindee/product/us/usmail/UsMailV3Test.java @@ -0,0 +1,58 @@ +package com.mindee.product.us.usmail; + +import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.mindee.parsing.common.Document; +import com.mindee.parsing.common.PredictResponse; +import com.mindee.parsing.standard.ClassificationField; +import com.mindee.product.ProductTestHelper; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import java.io.File; +import java.io.IOException; + +/** + * Unit tests for UsMailV3. + */ +public class UsMailV3Test { + + protected PredictResponse getPrediction(String name) throws IOException { + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.findAndRegisterModules(); + + JavaType type = objectMapper.getTypeFactory().constructParametricType( + PredictResponse.class, + UsMailV3.class + ); + return objectMapper.readValue( + new File("src/test/resources/products/us_mail/response_v3/" + name + ".json"), + type + ); + } + + @Test + void whenEmptyDeserialized_mustHaveValidProperties() throws IOException { + PredictResponse response = getPrediction("empty"); + UsMailV3Document docPrediction = response.getDocument().getInference().getPrediction(); + Assertions.assertNull(docPrediction.getSenderName().getValue()); + Assertions.assertNull(docPrediction.getSenderAddress().getCity()); + Assertions.assertNull(docPrediction.getSenderAddress().getComplete()); + Assertions.assertNull(docPrediction.getSenderAddress().getPostalCode()); + Assertions.assertNull(docPrediction.getSenderAddress().getState()); + Assertions.assertNull(docPrediction.getSenderAddress().getStreet()); + Assertions.assertTrue(docPrediction.getRecipientNames().isEmpty()); + Assertions.assertTrue(docPrediction.getRecipientAddresses().isEmpty()); + Assertions.assertNull(docPrediction.getIsReturnToSender().getValue()); + } + + @Test + void whenCompleteDeserialized_mustHaveValidDocumentSummary() throws IOException { + PredictResponse response = getPrediction("complete"); + Document doc = response.getDocument(); + ProductTestHelper.assertStringEqualsFile( + doc.toString(), + "src/test/resources/products/us_mail/response_v3/summary_full.rst" + ); + } + +} diff --git a/src/test/resources b/src/test/resources index 984162b6c..86f3c9de4 160000 --- a/src/test/resources +++ b/src/test/resources @@ -1 +1 @@ -Subproject commit 984162b6c231125583ab42899ac1e3d7b46825f3 +Subproject commit 86f3c9de490a0f29e56cacee0cd63dbebfa86dc8