diff --git a/src/main/java/com/mindee/parsing/BaseLocalResponse.java b/src/main/java/com/mindee/parsing/BaseLocalResponse.java index 6d5b5e14d..13d89d31a 100644 --- a/src/main/java/com/mindee/parsing/BaseLocalResponse.java +++ b/src/main/java/com/mindee/parsing/BaseLocalResponse.java @@ -26,25 +26,43 @@ public abstract class BaseLocalResponse { protected final byte[] file; /** - * Load from an {@link InputStream}. + * Load from a {@link String}. + * + * @param input Assumes UTF-8 encoding. + */ + public BaseLocalResponse(String input) { + if (input == null) { + throw new IllegalArgumentException("Input string cannot be null."); + } + this.file = this.readToCleanUtf8Bytes(input.lines()); + } + + /** + * Load from a byte array. * * @param input will be decoded as UTF-8. */ - public BaseLocalResponse(InputStream input) { - this.file = this - .getBytes(new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)).lines()); + public BaseLocalResponse(byte[] input) { + if (input == null) { + throw new IllegalArgumentException("Input byte array cannot be null."); + } + this.file = this.readToCleanUtf8Bytes(new String(input, StandardCharsets.UTF_8).lines()); } /** - * Load from a {@link String}. + * Load from an {@link InputStream}. + * This method will not close the provided stream. * * @param input will be decoded as UTF-8. */ - public BaseLocalResponse(String input) { - if (input == null || input.isEmpty()) { - throw new IllegalArgumentException("Input string cannot be empty or null."); + public BaseLocalResponse(InputStream input) { + if (input == null) { + throw new IllegalArgumentException("Input stream cannot be null."); } - this.file = input.getBytes(StandardCharsets.UTF_8); + this.file = this + .readToCleanUtf8Bytes( + new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)).lines() + ); } /** @@ -53,7 +71,12 @@ public BaseLocalResponse(String input) { * @param input will be decoded as UTF-8. */ public BaseLocalResponse(File input) throws IOException { - this.file = this.getBytes(Files.lines(input.toPath(), StandardCharsets.UTF_8)); + if (input == null) { + throw new IllegalArgumentException("Input file cannot be null."); + } + try (var lines = Files.lines(input.toPath(), StandardCharsets.UTF_8)) { + this.file = this.readToCleanUtf8Bytes(lines); + } } /** @@ -62,11 +85,20 @@ public BaseLocalResponse(File input) throws IOException { * @param input will be decoded as UTF-8. */ public BaseLocalResponse(Path input) throws IOException { - this.file = this.getBytes(Files.lines(input, StandardCharsets.UTF_8)); + if (input == null) { + throw new IllegalArgumentException("Input path cannot be null."); + } + try (var lines = Files.lines(input, StandardCharsets.UTF_8)) { + this.file = this.readToCleanUtf8Bytes(lines); + } } - private byte[] getBytes(Stream stream) { - return stream.collect(Collectors.joining("")).getBytes(); + private byte[] readToCleanUtf8Bytes(Stream stream) { + var cleanedString = stream.collect(Collectors.joining("")); + if (cleanedString.trim().isEmpty()) { + throw new IllegalArgumentException("Input cannot be empty or contain only whitespace."); + } + return cleanedString.getBytes(StandardCharsets.UTF_8); } /** @@ -120,4 +152,12 @@ public boolean isValidHmacSignature(String secretKey, String signature) { return MessageDigest.isEqual(expectedBytes, actualBytes); } + + /** + * Print the file as a UTF-8 string. + */ + @Override + public String toString() { + return new String(this.file, StandardCharsets.UTF_8); + } } diff --git a/src/main/java/com/mindee/v1/parsing/LocalResponse.java b/src/main/java/com/mindee/v1/parsing/LocalResponse.java index b5f9aa0d6..ca6461096 100644 --- a/src/main/java/com/mindee/v1/parsing/LocalResponse.java +++ b/src/main/java/com/mindee/v1/parsing/LocalResponse.java @@ -18,11 +18,15 @@ */ public class LocalResponse extends BaseLocalResponse { - public LocalResponse(InputStream input) { + public LocalResponse(String input) { super(input); } - public LocalResponse(String input) { + public LocalResponse(byte[] input) { + super(input); + } + + public LocalResponse(InputStream input) { super(input); } diff --git a/src/main/java/com/mindee/v2/parsing/LocalResponse.java b/src/main/java/com/mindee/v2/parsing/LocalResponse.java index 33ec16410..a768d4f3f 100644 --- a/src/main/java/com/mindee/v2/parsing/LocalResponse.java +++ b/src/main/java/com/mindee/v2/parsing/LocalResponse.java @@ -14,11 +14,15 @@ */ public class LocalResponse extends BaseLocalResponse { - public LocalResponse(InputStream input) { + public LocalResponse(String input) { super(input); } - public LocalResponse(String input) { + public LocalResponse(byte[] input) { + super(input); + } + + public LocalResponse(InputStream input) { super(input); } diff --git a/src/test/java/com/mindee/TestingUtilities.java b/src/test/java/com/mindee/TestingUtilities.java index de9a9a980..63448a6ad 100644 --- a/src/test/java/com/mindee/TestingUtilities.java +++ b/src/test/java/com/mindee/TestingUtilities.java @@ -28,12 +28,16 @@ public static Path getV1ResourcePath(String filePath) { return Paths.get("src/test/resources/v1/" + filePath); } + public static String getV1ResourcePathString(String filePath) { + return getV1ResourcePath(filePath).toString(); + } + public static Path getV2ResourcePath(String filePath) { return Paths.get("src/test/resources/v2/" + filePath); } - public static String getV1ResourcePathString(String filePath) { - return getV1ResourcePath(filePath).toString(); + public static Path getV2ProductPath(String filePath) { + return getV2ResourcePath("products/" + filePath); } public static void assertStringEqualsFile(String expected, String filePath) throws IOException { diff --git a/src/test/java/com/mindee/v2/MindeeClientIT.java b/src/test/java/com/mindee/v2/MindeeClientIT.java index c8386c921..f2beb8690 100644 --- a/src/test/java/com/mindee/v2/MindeeClientIT.java +++ b/src/test/java/com/mindee/v2/MindeeClientIT.java @@ -1,7 +1,7 @@ package com.mindee.v2; import static com.mindee.TestingUtilities.getResourcePath; -import static com.mindee.TestingUtilities.getV2ResourcePath; +import static com.mindee.TestingUtilities.getV2ProductPath; import static org.junit.jupiter.api.Assertions.*; import com.mindee.input.LocalInputSource; @@ -88,7 +88,7 @@ void parseFile_emptyMultiPage_mustSucceed() throws IOException, InterruptedExcep @DisplayName("Filled, single-page image – enqueue & parse must succeed") void parseFile_filledSinglePage_mustSucceed() throws IOException, InterruptedException { var source = new LocalInputSource( - getV2ResourcePath("products/extraction/financial_document/default_sample.jpg") + getV2ProductPath("extraction/financial_document/default_sample.jpg") ); var params = ExtractionParameters @@ -137,16 +137,14 @@ void parseFile_filledSinglePage_mustSucceed() throws IOException, InterruptedExc @DisplayName("Data Schema Replace – enqueue & parse must succeed") void parseFile_dataSchemaReplace_mustSucceed() throws IOException, InterruptedException { var source = new LocalInputSource( - getV2ResourcePath("products/extraction/financial_document/default_sample.jpg") + getV2ProductPath("extraction/financial_document/default_sample.jpg") ); var params = ExtractionParameters .builder(modelId) .rag(false) .alias("java-integration-test_data-schema-replace") - .dataSchema( - Files.readString(getV2ResourcePath("products/extraction/data_schema_replace_param.json")) - ) + .dataSchema(Files.readString(getV2ProductPath("extraction/data_schema_replace_param.json"))) .build(); var response = mindeeClient.enqueueAndGetResult(ExtractionResponse.class, source, params); diff --git a/src/test/java/com/mindee/v2/MindeeClientTest.java b/src/test/java/com/mindee/v2/MindeeClientTest.java index 772ee8e9d..614bdfd9b 100644 --- a/src/test/java/com/mindee/v2/MindeeClientTest.java +++ b/src/test/java/com/mindee/v2/MindeeClientTest.java @@ -97,7 +97,7 @@ public TResponse reqGetResultByUrl( class Enqueue { @Test @DisplayName("sends exactly one HTTP call and yields a non-null response") - void enqueue_post_async() throws IOException { + void enqueue_post() throws IOException { var mindeeClient = new MindeeClient(new FakeMindeeApiV2(new JobResponse(), null)); var input = new LocalInputSource(getResourcePath("file_types/pdf/blank_1.pdf")); @@ -115,7 +115,7 @@ void enqueue_post_async() throws IOException { class GetJob { @Test @DisplayName("hits the HTTP endpoint once and returns a non-null response") - void document_getJob_async() throws JsonProcessingException { + void document_getJob() throws JsonProcessingException { String json = "{\"job\": {\"id\": \"dummy-id\", \"status\": \"Processing\"}}"; var mapper = new ObjectMapper(); mapper.findAndRegisterModules(); @@ -134,7 +134,7 @@ void document_getJob_async() throws JsonProcessingException { class GetExtractionInference { @Test @DisplayName("hits the HTTP endpoint once and returns a non-null response") - void document_getResult_async() throws IOException { + void document_getResult() throws IOException { String json = Files .readString(getResourcePath("v2/products/extraction/financial_document/complete.json")); @@ -171,7 +171,7 @@ void document_getResult_async() throws IOException { class GetResultFromUrl { @Test @DisplayName("hits the HTTP endpoint once and returns a non-null response") - void document_getResultFromUrl_async() throws IOException { + void document_getResultFromUrl() throws IOException { String json = Files .readString(getResourcePath("v2/products/extraction/financial_document/complete.json")); diff --git a/src/test/java/com/mindee/v2/fileoperations/CropTest.java b/src/test/java/com/mindee/v2/fileoperations/CropTest.java index 0148ef159..795c45749 100644 --- a/src/test/java/com/mindee/v2/fileoperations/CropTest.java +++ b/src/test/java/com/mindee/v2/fileoperations/CropTest.java @@ -2,7 +2,7 @@ import static com.mindee.TestingUtilities.deleteRecursively; import static com.mindee.TestingUtilities.getResourcePath; -import static com.mindee.TestingUtilities.getV2ResourcePath; +import static com.mindee.TestingUtilities.getV2ProductPath; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -26,8 +26,8 @@ public static void setup() throws IOException { @Test void singlePageCrop_cropsCorrectly() throws Exception { - var inputSample = new LocalInputSource(getV2ResourcePath("products/crop/default_sample.jpg")); - var localResponse = new LocalResponse(getV2ResourcePath("products/crop/default_sample.json")); + var inputSample = new LocalInputSource(getV2ProductPath("crop/default_sample.jpg")); + var localResponse = new LocalResponse(getV2ProductPath("crop/default_sample.json")); var doc = localResponse.deserializeResponse(CropResponse.class); var extractedCrops = new Crop(inputSample) @@ -56,8 +56,8 @@ void singlePageCrop_cropsCorrectly() throws Exception { @Test void multiPageCrop_cropsCorrectly() throws Exception { - var inputSample = new LocalInputSource(getV2ResourcePath("products/crop/multipage_sample.pdf")); - var localResponse = new LocalResponse(getV2ResourcePath("products/crop/multipage_sample.json")); + var inputSample = new LocalInputSource(getV2ProductPath("crop/multipage_sample.pdf")); + var localResponse = new LocalResponse(getV2ProductPath("crop/multipage_sample.json")); var doc = localResponse.deserializeResponse(CropResponse.class); var extractedCrops = new Crop(inputSample) diff --git a/src/test/java/com/mindee/v2/fileoperations/SplitTest.java b/src/test/java/com/mindee/v2/fileoperations/SplitTest.java index 4747cda5a..c2161ec96 100644 --- a/src/test/java/com/mindee/v2/fileoperations/SplitTest.java +++ b/src/test/java/com/mindee/v2/fileoperations/SplitTest.java @@ -2,7 +2,7 @@ import static com.mindee.TestingUtilities.deleteRecursively; import static com.mindee.TestingUtilities.getResourcePath; -import static com.mindee.TestingUtilities.getV2ResourcePath; +import static com.mindee.TestingUtilities.getV2ProductPath; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -26,9 +26,9 @@ public static void setup() throws IOException { @Test void singlePage_splitsCorrectly() throws IOException { - var inputSample = new LocalInputSource(getV2ResourcePath("products/split/default_sample.pdf")); + var inputSample = new LocalInputSource(getV2ProductPath("split/default_sample.pdf")); assertEquals(2, inputSample.getPageCount()); - var localResponse = new LocalResponse(getV2ResourcePath("products/split/default_sample.json")); + var localResponse = new LocalResponse(getV2ProductPath("split/default_sample.json")); var doc = localResponse.deserializeResponse(SplitResponse.class); var extractedSplit = new Split(inputSample) @@ -42,9 +42,9 @@ void singlePage_splitsCorrectly() throws IOException { @Test void multiplePages_splitsCorrectly() throws IOException { - var inputSample = new LocalInputSource(getV2ResourcePath("products/split/default_sample.pdf")); + var inputSample = new LocalInputSource(getV2ProductPath("split/default_sample.pdf")); assertEquals(2, inputSample.getPageCount()); - var localResponse = new LocalResponse(getV2ResourcePath("products/split/default_sample.json")); + var localResponse = new LocalResponse(getV2ProductPath("split/default_sample.json")); var doc = localResponse.deserializeResponse(SplitResponse.class); var extractedSplits = new Split(inputSample) diff --git a/src/test/java/com/mindee/v2/parsing/LocalResponseTest.java b/src/test/java/com/mindee/v2/parsing/LocalResponseTest.java index 19d2cc8fb..c5cfcb21b 100644 --- a/src/test/java/com/mindee/v2/parsing/LocalResponseTest.java +++ b/src/test/java/com/mindee/v2/parsing/LocalResponseTest.java @@ -1,24 +1,32 @@ package com.mindee.v2.parsing; -import static com.mindee.TestingUtilities.getResourcePath; +import static com.mindee.TestingUtilities.getV2ProductPath; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import com.mindee.MindeeException; import com.mindee.v2.product.extraction.ExtractionResponse; +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @DisplayName("MindeeV2 – Load Local Response") public class LocalResponseTest { - private static final String SIGNATURE = "79dd6572f8a97822fb12f2f72bc84ecdc7c968dede712cf23a256ac3eac593d4"; + private static final String SIGNATURE = "e51bdf80f1a08ed44ee161100fc30a25cb35b4ede671b0a575dc9064a3f5dbf1"; private static final String DUMMY_SECRET_KEY = "ogNjY44MhvKPGTtVsI8zG82JqWQa68woYQH"; + private static final String FILE_PATH = "extraction/standard_field_types.json"; - private static void assertLocalResponse(LocalResponse localResponse) { + private static void assertLocalResponse(LocalResponse localResponse, String fileContent) { assertEquals(SIGNATURE, localResponse.getHmacSignature(DUMMY_SECRET_KEY)); assertFalse(localResponse.isValidHmacSignature(DUMMY_SECRET_KEY, "invalid signature")); @@ -30,35 +38,77 @@ private static void assertLocalResponse(LocalResponse localResponse) { assertTrue(localResponse.isValidHmacSignature(DUMMY_SECRET_KEY, SIGNATURE.toUpperCase())); ExtractionResponse response = localResponse.deserializeResponse(ExtractionResponse.class); - assertNotNull(response, "Loaded ExtractionResponse must not be null"); - assertEquals( - "12345678-1234-1234-1234-123456789abc", - response.getInference().getModel().getId(), - "Model Id mismatch" - ); + + assertNotNull(response); + assertNotNull(response.getInference()); + + assertEquals("test-model-id", response.getInference().getModel().getId()); assertEquals( - "John Smith", + "field_simple_string-value", response .getInference() .getResult() .getFields() - .get("supplier_name") - .getSimpleField() - .getValue(), - "Supplier name mismatch" + .getSimpleField("field_simple_string") + .getStringValue() ); + + assertEquals(fileContent.replace("\r", "").replace("\n", ""), localResponse.toString()); } @Test - void loadDocument_withPath_mustReturnValidLocalResponse() throws IOException { - var localResponse = new LocalResponse( - getResourcePath("v2/products/extraction/financial_document/complete.json") - ); - assertLocalResponse(localResponse); + @DisplayName("should load a response from a JSON string") + void validString_mustLoadValidLocalResponse() throws IOException { + var fileContent = Files.readString(getV2ProductPath(FILE_PATH)); + var localResponse = new LocalResponse(fileContent); + assertLocalResponse(localResponse, fileContent); + } + + @Test + @DisplayName("should load a response from a buffer") + void validBuffer_mustLoadValidLocalResponse() throws IOException { + var filePath = getV2ProductPath(FILE_PATH); + var localResponse = new LocalResponse(Files.readAllBytes(filePath)); + assertLocalResponse(localResponse, Files.readString(filePath)); + } + + @Test + @DisplayName("should load a response from a JSON file path") + void validPath_mustLoadValidLocalResponse() throws IOException { + var filePath = getV2ProductPath(FILE_PATH); + var localResponse = new LocalResponse(filePath); + assertLocalResponse(localResponse, Files.readString(filePath)); + } + + @Test + @DisplayName("should load a response from a JSON file") + void validFile_mustLoadValidLocalResponse() throws IOException { + var filePath = getV2ProductPath(FILE_PATH); + var localResponse = new LocalResponse(new File(filePath.toString())); + assertLocalResponse(localResponse, Files.readString(filePath)); + } + + @Test + @DisplayName("should load a response from a stream") + void validStream_mustLoadValidLocalResponse() throws IOException { + var file = new File(getV2ProductPath(FILE_PATH).toString()); + + try (var stream = new BufferedInputStream((new FileInputStream(file)))) { + // Required for the `reset()` later + stream.mark((int) file.length() + 1024); + + var localResponse = new LocalResponse(stream); + assertLocalResponse(localResponse, Files.readString(file.toPath())); + + // Explicitly verify the stream is not closed by the LocalResponse constructor + stream.reset(); + assertNotEquals(-1, stream.read()); + } } @Test - void givenInvalidJsonInput_shouldThrow() { + @DisplayName("should raise an exception when given an invalid JSON string") + void invalidString_mustRaiseException() { var localResponse = new LocalResponse("{invalid json"); var err = assertThrows( MindeeException.class, @@ -66,4 +116,25 @@ void givenInvalidJsonInput_shouldThrow() { ); assertEquals("Invalid JSON payload.", err.getMessage()); } + + @Test + @DisplayName("should raise an exception when given an empty value") + void emptyValue_mustRaiseException() { + assertThrows(IllegalArgumentException.class, () -> new LocalResponse("")); + assertThrows(IllegalArgumentException.class, () -> new LocalResponse(new byte[0])); + assertThrows( + IllegalArgumentException.class, + () -> new LocalResponse(InputStream.nullInputStream()) + ); + } + + @Test + @DisplayName("should raise an exception when given a null value") + void nullValue_mustRaiseException() { + assertThrows(IllegalArgumentException.class, () -> new LocalResponse((String) null)); + assertThrows(IllegalArgumentException.class, () -> new LocalResponse((byte[]) null)); + assertThrows(IllegalArgumentException.class, () -> new LocalResponse((InputStream) null)); + assertThrows(IllegalArgumentException.class, () -> new LocalResponse((File) null)); + assertThrows(IllegalArgumentException.class, () -> new LocalResponse((Path) null)); + } } diff --git a/src/test/java/com/mindee/v2/product/ClassificationTest.java b/src/test/java/com/mindee/v2/product/ClassificationTest.java index feda37644..7f8cf0639 100644 --- a/src/test/java/com/mindee/v2/product/ClassificationTest.java +++ b/src/test/java/com/mindee/v2/product/ClassificationTest.java @@ -1,6 +1,6 @@ package com.mindee.v2.product; -import static com.mindee.TestingUtilities.getV2ResourcePath; +import static com.mindee.TestingUtilities.getV2ProductPath; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; @@ -16,7 +16,7 @@ @DisplayName("MindeeV2 - Classification Model Tests") public class ClassificationTest { private ClassificationResponse loadResponse(String filePath) throws IOException { - var localResponse = new LocalResponse(getV2ResourcePath(filePath)); + var localResponse = new LocalResponse(getV2ProductPath(filePath)); return localResponse.deserializeResponse(ClassificationResponse.class); } @@ -26,7 +26,7 @@ class SinglePredictionTest { @Test @DisplayName("classification properties must be valid") void singleMustHaveValidProperties() throws IOException { - ClassificationResponse response = loadResponse("products/classification/default_sample.json"); + ClassificationResponse response = loadResponse("classification/default_sample.json"); assertNotNull(response.getInference()); assertEquals( "invoice", @@ -39,7 +39,7 @@ void singleMustHaveValidProperties() throws IOException { @DisplayName("extraction properties must be valid") void singleExtractionMustHaveValidProperties() throws IOException { ClassificationResponse response = loadResponse( - "products/classification/default_sample_extraction.json" + "classification/default_sample_extraction.json" ); assertNotNull(response.getInference()); assertEquals( diff --git a/src/test/java/com/mindee/v2/product/CropIT.java b/src/test/java/com/mindee/v2/product/CropIT.java index bb44dda84..13a68ffdb 100644 --- a/src/test/java/com/mindee/v2/product/CropIT.java +++ b/src/test/java/com/mindee/v2/product/CropIT.java @@ -1,6 +1,6 @@ package com.mindee.v2.product; -import static com.mindee.TestingUtilities.getV2ResourcePath; +import static com.mindee.TestingUtilities.getV2ProductPath; import static org.junit.jupiter.api.Assertions.*; import com.mindee.input.LocalInputSource; @@ -35,7 +35,7 @@ void setUp() { @Test @DisplayName("Filled, multi-page PDF – crop must succeed") void filledMultiPage_cropMustSucceed() throws IOException, InterruptedException { - var source = new LocalInputSource(getV2ResourcePath("products/crop/multipage_sample.pdf")); + var source = new LocalInputSource(getV2ProductPath("crop/multipage_sample.pdf")); var params = CropParameters .builder(cropModelId) .alias("java_integration-test_crop_multipage") @@ -74,7 +74,7 @@ void filledMultiPage_cropMustSucceed() throws IOException, InterruptedException @Test @DisplayName("Filled image – crop and extraction must succeed") void filledSinglePage_extractionMustSucceed() throws IOException, InterruptedException { - var source = new LocalInputSource(getV2ResourcePath("products/crop/default_sample.jpg")); + var source = new LocalInputSource(getV2ProductPath("crop/default_sample.jpg")); var params = CropParameters .builder(cropExtractionModelId) .alias("java_integration-test_crop_multipage") diff --git a/src/test/java/com/mindee/v2/product/CropTest.java b/src/test/java/com/mindee/v2/product/CropTest.java index 0a185f55b..e2405cf02 100644 --- a/src/test/java/com/mindee/v2/product/CropTest.java +++ b/src/test/java/com/mindee/v2/product/CropTest.java @@ -3,7 +3,7 @@ import static com.mindee.TestingUtilities.assertStringEqualsFile; import static com.mindee.TestingUtilities.deleteRecursively; import static com.mindee.TestingUtilities.getResourcePath; -import static com.mindee.TestingUtilities.getV2ResourcePath; +import static com.mindee.TestingUtilities.getV2ProductPath; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -32,7 +32,7 @@ public static void setup() throws IOException { } private CropResponse loadResponse(String filePath) throws IOException { - var localResponse = new LocalResponse(getV2ResourcePath(filePath)); + var localResponse = new LocalResponse(getV2ProductPath(filePath)); return localResponse.deserializeResponse(CropResponse.class); } @@ -42,7 +42,7 @@ class SinglePredictionTest { @Test @DisplayName("crop properties must be valid") void mustHaveValidProperties() throws IOException { - var response = loadResponse("products/crop/crop_single.json"); + var response = loadResponse("crop/crop_single.json"); assertNotNull(response.getInference()); var crops = response.getInference().getResult().getCrops(); @@ -57,10 +57,10 @@ void mustHaveValidProperties() throws IOException { @Test @DisplayName("RST output must be valid") void mustHaveValidDisplay() throws IOException { - var response = loadResponse("products/crop/crop_single.json"); + var response = loadResponse("crop/crop_single.json"); assertStringEqualsFile( response.getInference().toString(), - getV2ResourcePath("products/crop/crop_single.rst") + getV2ProductPath("crop/crop_single.rst") ); } } @@ -71,7 +71,7 @@ class MultiPredictionTest { @Test @DisplayName("crop properties must be valid") void mustHaveValidProperties() throws IOException { - var response = loadResponse("products/crop/crop_multiple.json"); + var response = loadResponse("crop/crop_multiple.json"); assertNotNull(response.getInference()); var crops = response.getInference().getResult().getCrops(); @@ -91,17 +91,17 @@ void mustHaveValidProperties() throws IOException { @Test @DisplayName("RST output must be valid") void mustHaveValidDisplay() throws IOException { - var response = loadResponse("products/crop/crop_multiple.json"); + var response = loadResponse("crop/crop_multiple.json"); assertStringEqualsFile( response.getInference().toString(), - getV2ResourcePath("products/crop/crop_multiple.rst") + getV2ProductPath("crop/crop_multiple.rst") ); } @Test @DisplayName("extraction properties must be valid") void extractionMustHaveValidProperties() throws IOException { - CropResponse response = loadResponse("products/crop/default_sample_extraction.json"); + CropResponse response = loadResponse("crop/default_sample_extraction.json"); assertNotNull(response.getInference()); var crops = response.getInference().getResult().getCrops(); @@ -143,9 +143,9 @@ void extractionMustHaveValidProperties() throws IOException { @Test @DisplayName("extract all crops works") void extractMultipleCrops() throws IOException { - var inputSource = new LocalInputSource(getV2ResourcePath("products/crop/default_sample.jpg")); + var inputSource = new LocalInputSource(getV2ProductPath("crop/default_sample.jpg")); - CropResponse response = loadResponse("products/crop/default_sample_extraction.json"); + CropResponse response = loadResponse("crop/default_sample_extraction.json"); assertNotNull(response.getInference()); var crops = response.getInference().getResult().getCrops(); @@ -170,9 +170,9 @@ void extractMultipleCrops() throws IOException { @Test @DisplayName("extract single crop works") void extractSingleCrop() throws IOException { - var inputSource = new LocalInputSource(getV2ResourcePath("products/crop/default_sample.jpg")); + var inputSource = new LocalInputSource(getV2ProductPath("crop/default_sample.jpg")); - CropResponse response = loadResponse("products/crop/default_sample_extraction.json"); + CropResponse response = loadResponse("crop/default_sample_extraction.json"); assertNotNull(response.getInference()); var extractedCrop = response diff --git a/src/test/java/com/mindee/v2/product/ExtractionTest.java b/src/test/java/com/mindee/v2/product/ExtractionTest.java index ef87f438c..ef28647c4 100644 --- a/src/test/java/com/mindee/v2/product/ExtractionTest.java +++ b/src/test/java/com/mindee/v2/product/ExtractionTest.java @@ -1,6 +1,6 @@ package com.mindee.v2.product; -import static com.mindee.TestingUtilities.getV2ResourcePath; +import static com.mindee.TestingUtilities.getV2ProductPath; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertInstanceOf; @@ -44,7 +44,7 @@ class ExtractionTest { private ExtractionResponse loadResponse(String filePath) throws IOException { - var localResponse = new LocalResponse(getV2ResourcePath(filePath)); + var localResponse = new LocalResponse(getV2ProductPath(filePath)); return localResponse.deserializeResponse(ExtractionResponse.class); } @@ -55,7 +55,7 @@ class BlankPredictionTest { @Test @DisplayName("all properties must be valid") void asyncPredict_whenEmpty_mustHaveValidProperties() throws IOException { - var response = loadResponse("products/extraction/financial_document/blank.json"); + var response = loadResponse("extraction/financial_document/blank.json"); var fields = response.getInference().getResult().getFields(); assertEquals(21, fields.size(), "Expected 21 fields"); @@ -107,7 +107,7 @@ class CompletePredictionTest { @Test @DisplayName("every exposed property must be valid and consistent") void asyncPredict_whenComplete_mustExposeAllProperties() throws IOException { - var response = loadResponse("products/extraction/financial_document/complete.json"); + var response = loadResponse("extraction/financial_document/complete.json"); ExtractionInference inference = response.getInference(); assertNotNull(inference); assertEquals("12345678-1234-1234-1234-123456789abc", inference.getId()); @@ -180,7 +180,7 @@ class DeepNestedFieldsTest { @Test @DisplayName("all nested structures must be typed correctly") void deepNestedFields_mustExposeCorrectTypes() throws IOException { - ExtractionResponse resp = loadResponse("products/extraction/deep_nested_fields.json"); + ExtractionResponse resp = loadResponse("extraction/deep_nested_fields.json"); ExtractionInference inf = resp.getInference(); assertNotNull(inf); @@ -246,7 +246,7 @@ private void testSimpleFieldString(SimpleField field) { @Test @DisplayName("simple fields must be recognised") void standardFieldTypes_mustExposeSimpleFieldValues() throws IOException { - var response = loadResponse("products/extraction/standard_field_types.json"); + var response = loadResponse("extraction/standard_field_types.json"); ExtractionInference inference = response.getInference(); assertNotNull(inference); @@ -305,7 +305,7 @@ void standardFieldTypes_mustExposeSimpleFieldValues() throws IOException { @Test @DisplayName("simple list fields must be recognised") void standardFieldTypes_mustExposeSimpleListFieldValues() throws IOException { - var response = loadResponse("products/extraction/standard_field_types.json"); + var response = loadResponse("extraction/standard_field_types.json"); var inference = response.getInference(); assertNotNull(inference); @@ -348,7 +348,7 @@ private void testObjectSubFieldSimpleString(String fieldName, SimpleField subFie @Test @DisplayName("object list fields must be recognised") void standardFieldTypes_mustExposeObjectListFieldValues() throws IOException { - var response = loadResponse("products/extraction/standard_field_types.json"); + var response = loadResponse("extraction/standard_field_types.json"); var inference = response.getInference(); assertNotNull(inference); @@ -398,7 +398,7 @@ void standardFieldTypes_mustExposeObjectListFieldValues() throws IOException { @Test @DisplayName("simple / object / list variants must be recognised") void standardFieldTypes_mustExposeObjectFieldValues() throws IOException { - var response = loadResponse("products/extraction/standard_field_types.json"); + var response = loadResponse("extraction/standard_field_types.json"); var inference = response.getInference(); assertNotNull(inference); @@ -433,7 +433,7 @@ void standardFieldTypes_mustExposeObjectFieldValues() throws IOException { @Test @DisplayName("allow getting fields using generics") void standardFieldTypes_getWithGenerics() throws IOException { - var response = loadResponse("products/extraction/standard_field_types.json"); + var response = loadResponse("extraction/standard_field_types.json"); var inference = response.getInference(); assertNotNull(inference); var fields = inference.getResult().getFields(); @@ -469,7 +469,7 @@ void standardFieldTypes_getWithGenerics() throws IOException { @Test @DisplayName("confidence and locations must be usable") void standardFieldTypes_confidenceAndLocations() throws IOException { - var response = loadResponse("products/extraction/standard_field_types.json"); + var response = loadResponse("extraction/standard_field_types.json"); var inference = response.getInference(); assertNotNull(inference); @@ -509,7 +509,7 @@ class RawTextTest { @Test @DisplayName("raw texts option must be parsed and exposed") void rawTexts_mustBeAccessible() throws IOException { - var response = loadResponse("products/extraction/raw_texts.json"); + var response = loadResponse("extraction/raw_texts.json"); var inference = response.getInference(); assertNotNull(inference); @@ -544,7 +544,7 @@ class RagMetadataTest { @Test @DisplayName("RAG metadata when matched") void rag_mustBeFilled_whenMatched() throws IOException { - var response = loadResponse("products/extraction/rag_matched.json"); + var response = loadResponse("extraction/rag_matched.json"); var inference = response.getInference(); assertNotNull(inference); @@ -556,7 +556,7 @@ void rag_mustBeFilled_whenMatched() throws IOException { @Test @DisplayName("RAG metadata when not matched") void rag_mustBeNull_whenNotMatched() throws IOException { - var response = loadResponse("products/extraction/rag_not_matched.json"); + var response = loadResponse("extraction/rag_not_matched.json"); var inference = response.getInference(); assertNotNull(inference); @@ -572,9 +572,8 @@ class RstDisplay { @Test @DisplayName("rst display must be parsed and exposed") void rstDisplay_mustBeAccessible() throws IOException { - var resp = loadResponse("products/extraction/standard_field_types.json"); - String rstRef = Files - .readString(getV2ResourcePath("products/extraction/standard_field_types.rst")); + var resp = loadResponse("extraction/standard_field_types.json"); + String rstRef = Files.readString(getV2ProductPath("extraction/standard_field_types.rst")); ExtractionInference inference = resp.getInference(); assertNotNull(inference); assertEquals(rstRef, resp.getInference().toString()); @@ -587,7 +586,7 @@ class TextContextTest { @Test @DisplayName("should be present and true when enabled") void textContext_mustBePresentAndTrue() throws IOException { - var resp = loadResponse("products/extraction/text_context_enabled.json"); + var resp = loadResponse("extraction/text_context_enabled.json"); ExtractionInference inference = resp.getInference(); assertNotNull(inference); assertTrue(inference.getActiveOptions().getTextContext()); @@ -600,7 +599,7 @@ class DataSchemaTest { @Test @DisplayName("should be present and true when enabled") void textContext_mustBePresentAndTrue() throws IOException { - var resp = loadResponse("products/extraction/data_schema_replace.json"); + var resp = loadResponse("extraction/data_schema_replace.json"); ExtractionInference inference = resp.getInference(); assertNotNull(inference); var fields = inference.getResult().getFields(); diff --git a/src/test/java/com/mindee/v2/product/OcrTest.java b/src/test/java/com/mindee/v2/product/OcrTest.java index ae269073e..3f1ae485f 100644 --- a/src/test/java/com/mindee/v2/product/OcrTest.java +++ b/src/test/java/com/mindee/v2/product/OcrTest.java @@ -1,6 +1,6 @@ package com.mindee.v2.product; -import static com.mindee.TestingUtilities.getV2ResourcePath; +import static com.mindee.TestingUtilities.getV2ProductPath; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -14,7 +14,7 @@ @DisplayName("MindeeV2 - OCR Model Tests") public class OcrTest { private OcrResponse loadResponse(String filePath) throws IOException { - var localResponse = new LocalResponse(getV2ResourcePath(filePath)); + var localResponse = new LocalResponse(getV2ProductPath(filePath)); return localResponse.deserializeResponse(OcrResponse.class); } @@ -24,7 +24,7 @@ class SinglePredictionTest { @Test @DisplayName("all properties must be valid") void mustHaveValidProperties() throws IOException { - var response = loadResponse("products/ocr/ocr_single.json"); + var response = loadResponse("ocr/ocr_single.json"); assertNotNull(response.getInference()); var pages = response.getInference().getResult().getPages(); @@ -40,7 +40,7 @@ class MultiPredictionTest { @Test @DisplayName("all properties must be valid") void mustHaveValidProperties() throws IOException { - var response = loadResponse("products/ocr/ocr_multiple.json"); + var response = loadResponse("ocr/ocr_multiple.json"); assertNotNull(response.getInference()); var pages = response.getInference().getResult().getPages(); diff --git a/src/test/java/com/mindee/v2/product/SplitTest.java b/src/test/java/com/mindee/v2/product/SplitTest.java index 787ce6755..0b9b43bbd 100644 --- a/src/test/java/com/mindee/v2/product/SplitTest.java +++ b/src/test/java/com/mindee/v2/product/SplitTest.java @@ -2,7 +2,7 @@ import static com.mindee.TestingUtilities.deleteRecursively; import static com.mindee.TestingUtilities.getResourcePath; -import static com.mindee.TestingUtilities.getV2ResourcePath; +import static com.mindee.TestingUtilities.getV2ProductPath; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -32,7 +32,7 @@ public static void setup() throws IOException { } private SplitResponse loadResponse(String filePath) throws IOException { - var localResponse = new LocalResponse(getV2ResourcePath(filePath)); + var localResponse = new LocalResponse(getV2ProductPath(filePath)); return localResponse.deserializeResponse(SplitResponse.class); } @@ -42,7 +42,7 @@ class SinglePredictionTest { @Test @DisplayName("split properties must be valid") void mustHaveValidProperties() throws IOException { - var response = loadResponse("products/split/split_single.json"); + var response = loadResponse("split/split_single.json"); assertNotNull(response.getInference()); var splits = response.getInference().getResult().getSplits(); @@ -59,7 +59,7 @@ class MultiPredictionTest { @Test @DisplayName("split properties must be valid") void mustHaveValidProperties() throws IOException { - var response = loadResponse("products/split/split_multiple.json"); + var response = loadResponse("split/split_multiple.json"); assertNotNull(response.getInference()); var splits = response.getInference().getResult().getSplits(); @@ -81,7 +81,7 @@ void mustHaveValidProperties() throws IOException { @Test @DisplayName("extraction properties must be valid") void extractionMustHaveValidProperties() throws IOException { - SplitResponse response = loadResponse("products/split/default_sample_extraction.json"); + SplitResponse response = loadResponse("split/default_sample_extraction.json"); assertNotNull(response.getInference()); var splits = response.getInference().getResult().getSplits(); @@ -121,11 +121,9 @@ void extractionMustHaveValidProperties() throws IOException { @Test @DisplayName("extract all crops works") void extractMultipleSplits() throws IOException { - var inputSource = new LocalInputSource( - getV2ResourcePath("products/split/default_sample.pdf") - ); + var inputSource = new LocalInputSource(getV2ProductPath("split/default_sample.pdf")); - SplitResponse response = loadResponse("products/split/default_sample_extraction.json"); + SplitResponse response = loadResponse("split/default_sample_extraction.json"); assertNotNull(response.getInference()); var splits = response.getInference().getResult().getSplits(); @@ -150,11 +148,9 @@ void extractMultipleSplits() throws IOException { @Test @DisplayName("extract single crop works") void extractSingleSplit() throws IOException { - var inputSource = new LocalInputSource( - getV2ResourcePath("products/split/default_sample.pdf") - ); + var inputSource = new LocalInputSource(getV2ProductPath("split/default_sample.pdf")); - SplitResponse response = loadResponse("products/split/default_sample_extraction.json"); + SplitResponse response = loadResponse("split/default_sample_extraction.json"); assertNotNull(response.getInference()); var extractedSplit = response