diff --git a/src/main/java/com/mindee/input/LocalResponse.java b/src/main/java/com/mindee/parsing/BaseLocalResponse.java similarity index 66% rename from src/main/java/com/mindee/input/LocalResponse.java rename to src/main/java/com/mindee/parsing/BaseLocalResponse.java index 3eabd1423..3284e9984 100644 --- a/src/main/java/com/mindee/input/LocalResponse.java +++ b/src/main/java/com/mindee/parsing/BaseLocalResponse.java @@ -1,8 +1,5 @@ -package com.mindee.input; +package com.mindee.parsing; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.mindee.MindeeException; -import com.mindee.v2.parsing.CommonResponse; import java.io.BufferedReader; import java.io.File; import java.io.IOException; @@ -24,16 +21,15 @@ * A Mindee response saved locally. */ @Getter -public class LocalResponse { - private final byte[] file; - private static final ObjectMapper mapper = new ObjectMapper(); +public abstract class BaseLocalResponse { + protected final byte[] file; /** * Load from an {@link InputStream}. * * @param input will be decoded as UTF-8. */ - public LocalResponse(InputStream input) { + public BaseLocalResponse(InputStream input) { this.file = this .getBytes(new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)).lines()); } @@ -43,7 +39,10 @@ public LocalResponse(InputStream input) { * * @param input will be decoded as UTF-8. */ - public LocalResponse(String input) { + public BaseLocalResponse(String input) { + if (input == null || input.isEmpty()) { + throw new IllegalArgumentException("Input string cannot be empty or null."); + } this.file = input.getBytes(StandardCharsets.UTF_8); } @@ -52,7 +51,7 @@ public LocalResponse(String input) { * * @param input will be decoded as UTF-8. */ - public LocalResponse(File input) throws IOException { + public BaseLocalResponse(File input) throws IOException { this.file = this.getBytes(Files.lines(input.toPath(), StandardCharsets.UTF_8)); } @@ -61,7 +60,7 @@ public LocalResponse(File input) throws IOException { * * @param input will be decoded as UTF-8. */ - public LocalResponse(Path input) throws IOException { + public BaseLocalResponse(Path input) throws IOException { this.file = this.getBytes(Files.lines(input, StandardCharsets.UTF_8)); } @@ -106,24 +105,4 @@ public String getHmacSignature(String secretKey) { public boolean isValidHmacSignature(String secretKey, String signature) { return signature.equals(getHmacSignature(secretKey)); } - - /** - * Deserialize this local JSON payload into a specific {@link CommonResponse} - * subtype: {@code InferenceResponse}, {@code JobResponse}. - * - * @param responseClass the concrete class to instantiate - * @param generic {@link CommonResponse} - * @return Either a {@code InferenceResponse} or {@code JobResponse} instance. - * @throws MindeeException if the payload cannot be deserialized into the requested type - */ - public T deserializeResponse(Class responseClass) { - ObjectMapper mapper = new ObjectMapper(); - try { - T response = mapper.readValue(this.file, responseClass); - response.setRawResponse(new String(this.file, StandardCharsets.UTF_8)); - return response; - } catch (Exception ex) { - throw new MindeeException("Invalid class specified for deserialization.", ex); - } - } } diff --git a/src/main/java/com/mindee/v1/MindeeClient.java b/src/main/java/com/mindee/v1/MindeeClient.java index 7c401c04a..f9c3ea5b3 100644 --- a/src/main/java/com/mindee/v1/MindeeClient.java +++ b/src/main/java/com/mindee/v1/MindeeClient.java @@ -1,12 +1,9 @@ package com.mindee.v1; -import com.fasterxml.jackson.databind.JavaType; -import com.fasterxml.jackson.databind.ObjectMapper; import com.mindee.AsyncPollingOptions; import com.mindee.MindeeException; import com.mindee.input.InputSourceUtils; import com.mindee.input.LocalInputSource; -import com.mindee.input.LocalResponse; import com.mindee.input.PageOptions; import com.mindee.pdf.PdfBoxApi; import com.mindee.pdf.PdfOperation; @@ -1071,28 +1068,4 @@ public AsyncPredictResponse parseQueued( ) { return this.mindeeApi.documentQueueGet(type, endpoint, jobId); } - - /** - * Load a local prediction. - * Typically used when wanting to load from a webhook callback. - * However, any kind of Mindee response may be loaded. - * - * @param Type of inference. - * @param type Type of inference. - * @param localResponse A loaded local response. - * @return an instance of {@link AsyncPredictResponse}. - * @throws IOException Throws if the file can't be accessed. - */ - public AsyncPredictResponse loadPrediction( - Class type, - LocalResponse localResponse - ) throws IOException { - ObjectMapper objectMapper = new ObjectMapper(); - objectMapper.findAndRegisterModules(); - JavaType parametricType = objectMapper - .getTypeFactory() - .constructParametricType(AsyncPredictResponse.class, type); - return objectMapper.readValue(localResponse.getFile(), parametricType); - } - } diff --git a/src/main/java/com/mindee/v1/parsing/LocalResponse.java b/src/main/java/com/mindee/v1/parsing/LocalResponse.java new file mode 100644 index 000000000..b9244d014 --- /dev/null +++ b/src/main/java/com/mindee/v1/parsing/LocalResponse.java @@ -0,0 +1,85 @@ +package com.mindee.v1.parsing; + +import com.fasterxml.jackson.core.JacksonException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.mindee.MindeeException; +import com.mindee.parsing.BaseLocalResponse; +import com.mindee.v1.parsing.common.AsyncPredictResponse; +import com.mindee.v1.parsing.common.Inference; +import com.mindee.v1.parsing.common.PredictResponse; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; + +/** + * A Mindee response saved locally. + */ +public class LocalResponse extends BaseLocalResponse { + + public LocalResponse(InputStream input) { + super(input); + } + + public LocalResponse(String input) { + super(input); + } + + public LocalResponse(File input) throws IOException { + super(input); + } + + public LocalResponse(Path input) throws IOException { + super(input); + } + + private

R deserialize( + Class responseClass, + Class

productClass + ) throws IOException { + var mapper = new ObjectMapper().findAndRegisterModules(); + var type = mapper.getTypeFactory().constructParametricType(responseClass, productClass); + try { + return mapper.readValue(this.file, type); + } catch (Exception e) { + if (e instanceof JacksonException) { + throw new MindeeException("Invalid JSON payload.", e); + } + throw e; + } + } + + /** + * Deserialize this local JSON payload into a specific {@link AsyncPredictResponse}. + * subtype: {@code InferenceResponse}, {@code JobResponse}. + * + * @param productClass the concrete class to instantiate + * @param generic {@link Inference} + * @return A {@link AsyncPredictResponse} instance. + * @throws MindeeException if the payload cannot be deserialized into the requested type + */ + public AsyncPredictResponse deserializeAsyncResponse( + Class productClass + ) throws IOException { + AsyncPredictResponse response = deserialize(AsyncPredictResponse.class, productClass); + response.setRawResponse(new String(this.file, StandardCharsets.UTF_8)); + return response; + } + + /** + * Deserialize this local JSON payload into a specific {@link PredictResponse}. + * + * @param productClass the concrete class to instantiate + * @param generic {@link Inference} + * @return A {@link PredictResponse} instance. + * @throws MindeeException if the payload cannot be deserialized into the requested type + */ + public PredictResponse deserializeSyncResponse( + Class productClass + ) throws IOException { + PredictResponse response = deserialize(PredictResponse.class, productClass); + response.setRawResponse(new String(this.file, StandardCharsets.UTF_8)); + return response; + } +} diff --git a/src/main/java/com/mindee/v2/parsing/LocalResponse.java b/src/main/java/com/mindee/v2/parsing/LocalResponse.java new file mode 100644 index 000000000..33ec16410 --- /dev/null +++ b/src/main/java/com/mindee/v2/parsing/LocalResponse.java @@ -0,0 +1,52 @@ +package com.mindee.v2.parsing; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.mindee.MindeeException; +import com.mindee.parsing.BaseLocalResponse; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; + +/** + * A Mindee response saved locally. + */ +public class LocalResponse extends BaseLocalResponse { + + public LocalResponse(InputStream input) { + super(input); + } + + public LocalResponse(String input) { + super(input); + } + + public LocalResponse(File input) throws IOException { + super(input); + } + + public LocalResponse(Path input) throws IOException { + super(input); + } + + /** + * Deserialize this local JSON payload into a specific {@link CommonResponse} + * subtype: {@code InferenceResponse}, {@code JobResponse}. + * + * @param responseClass the concrete class to instantiate + * @param generic {@link CommonResponse} + * @return Either a {@code InferenceResponse} or {@code JobResponse} instance. + * @throws MindeeException if the payload cannot be deserialized into the requested type + */ + public T deserializeResponse(Class responseClass) { + var mapper = new ObjectMapper().findAndRegisterModules(); + try { + var response = mapper.readValue(this.file, responseClass); + response.setRawResponse(new String(this.file, StandardCharsets.UTF_8)); + return response; + } catch (Exception e) { + throw new MindeeException("Invalid JSON payload.", e); + } + } +} diff --git a/src/test/java/com/mindee/input/LocalResponseV1Test.java b/src/test/java/com/mindee/input/LocalResponseV1Test.java deleted file mode 100644 index cef89b839..000000000 --- a/src/test/java/com/mindee/input/LocalResponseV1Test.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.mindee.input; - -import static com.mindee.TestingUtilities.getV1ResourcePath; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class LocalResponseV1Test { - /** - * Fake secret key. - */ - String secretKey = "ogNjY44MhvKPGTtVsI8zG82JqWQa68woYQH"; - - /** - * Real signature using fake secret key. - */ - String signature = "5ed1673e34421217a5dbfcad905ee62261a3dd66c442f3edd19302072bbf70d0"; - - /** - * File which the signature applies to. - */ - Path filePath = getV1ResourcePath("async/get_completed_empty.json"); - - @Test - void loadDocument_withFile_mustReturnValidLocalResponse() throws IOException { - LocalResponse localResponse = new LocalResponse(new File(this.filePath.toString())); - Assertions.assertNotNull(localResponse.getFile()); - Assertions - .assertFalse( - localResponse.isValidHmacSignature(this.secretKey, "invalid signature is invalid") - ); - Assertions.assertEquals(this.signature, localResponse.getHmacSignature(this.secretKey)); - Assertions.assertTrue(localResponse.isValidHmacSignature(this.secretKey, this.signature)); - } - - @Test - void loadDocument_withString_mustReturnValidLocalResponse() { - LocalResponse localResponse = new LocalResponse("{'some': 'json', 'with': 'data'}"); - Assertions.assertNotNull(localResponse.getFile()); - Assertions - .assertFalse( - localResponse.isValidHmacSignature(this.secretKey, "invalid signature is invalid") - ); - } - - @Test - void loadDocument_withInputStream_mustReturnValidLocalResponse() throws IOException { - LocalResponse localResponse = new LocalResponse(Files.newInputStream(this.filePath)); - Assertions.assertNotNull(localResponse.getFile()); - Assertions - .assertFalse( - localResponse.isValidHmacSignature(this.secretKey, "invalid signature is invalid") - ); - Assertions.assertEquals(this.signature, localResponse.getHmacSignature(this.secretKey)); - Assertions.assertTrue(localResponse.isValidHmacSignature(this.secretKey, this.signature)); - } -} diff --git a/src/test/java/com/mindee/v1/MindeeClientTest.java b/src/test/java/com/mindee/v1/MindeeClientTest.java index 4bf21570c..c2cc65079 100644 --- a/src/test/java/com/mindee/v1/MindeeClientTest.java +++ b/src/test/java/com/mindee/v1/MindeeClientTest.java @@ -1,11 +1,8 @@ package com.mindee.v1; -import static com.mindee.TestingUtilities.assertStringEqualsFile; import static com.mindee.TestingUtilities.getResourcePath; -import static com.mindee.TestingUtilities.getV1ResourcePathString; import com.mindee.input.LocalInputSource; -import com.mindee.input.LocalResponse; import com.mindee.input.PageOptions; import com.mindee.input.PageOptionsOperation; import com.mindee.v1.clientOptions.PredictOptions; @@ -13,9 +10,7 @@ import com.mindee.v1.parsing.common.Document; import com.mindee.v1.parsing.common.Job; import com.mindee.v1.parsing.common.PredictResponse; -import com.mindee.v1.product.internationalid.InternationalIdV2; import com.mindee.v1.product.invoice.InvoiceV4; -import java.io.File; import java.io.IOException; import java.net.URL; import java.nio.file.Files; @@ -156,30 +151,4 @@ void givenAnAsyncUrl_whenEnqueued_shouldInvokeApiCorrectly() throws IOException Assertions.assertEquals("someid", jobId); } - - @Test - void givenJsonInput_whenSync_shouldDeserializeCorrectly() throws IOException { - File file = new File(getV1ResourcePathString("products/invoices/response_v4/complete.json")); - LocalResponse localResponse = new LocalResponse(file); - AsyncPredictResponse predictResponse = new MindeeClient() - .loadPrediction(InvoiceV4.class, localResponse); - assertStringEqualsFile( - predictResponse.getDocumentObj().toString(), - getV1ResourcePathString("/products/invoices/response_v4/summary_full.rst") - ); - } - - @Test - void givenJsonInput_whenAsync_shouldDeserializeCorrectly() throws IOException { - File file = new File( - getV1ResourcePathString("products/international_id/response_v2/complete.json") - ); - LocalResponse localResponse = new LocalResponse(file); - AsyncPredictResponse predictResponse = new MindeeClient() - .loadPrediction(InternationalIdV2.class, localResponse); - assertStringEqualsFile( - predictResponse.getDocumentObj().toString(), - getV1ResourcePathString("products/international_id/response_v2/summary_full.rst") - ); - } } diff --git a/src/test/java/com/mindee/v1/parsing/LocalResponseTest.java b/src/test/java/com/mindee/v1/parsing/LocalResponseTest.java new file mode 100644 index 000000000..981fb194b --- /dev/null +++ b/src/test/java/com/mindee/v1/parsing/LocalResponseTest.java @@ -0,0 +1,101 @@ +package com.mindee.v1.parsing; + +import static com.mindee.TestingUtilities.assertStringEqualsFile; +import static com.mindee.TestingUtilities.getV1ResourcePath; +import static com.mindee.TestingUtilities.getV1ResourcePathString; + +import com.mindee.MindeeException; +import com.mindee.v1.product.internationalid.InternationalIdV2; +import com.mindee.v1.product.invoice.InvoiceV4; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class LocalResponseTest { + /** + * Fake secret key. + */ + String secretKey = "ogNjY44MhvKPGTtVsI8zG82JqWQa68woYQH"; + + /** + * Real signature using a fake secret key. + */ + String signature = "5ed1673e34421217a5dbfcad905ee62261a3dd66c442f3edd19302072bbf70d0"; + + /** + * File that the signature applies to. + */ + Path filePath = getV1ResourcePath("async/get_completed_empty.json"); + + @Test + void loadDocument_withFile_mustReturnValidLocalResponse() throws IOException { + var localResponse = new LocalResponse(new File(this.filePath.toString())); + Assertions.assertNotNull(localResponse.getFile()); + Assertions + .assertFalse( + localResponse.isValidHmacSignature(this.secretKey, "invalid signature is invalid") + ); + Assertions.assertEquals(this.signature, localResponse.getHmacSignature(this.secretKey)); + Assertions.assertTrue(localResponse.isValidHmacSignature(this.secretKey, this.signature)); + } + + @Test + void loadDocument_withString_mustReturnValidLocalResponse() { + var localResponse = new LocalResponse("{'some': 'json', 'with': 'data'}"); + Assertions.assertNotNull(localResponse.getFile()); + Assertions + .assertFalse( + localResponse.isValidHmacSignature(this.secretKey, "invalid signature is invalid") + ); + } + + @Test + void loadDocument_withInputStream_mustReturnValidLocalResponse() throws IOException { + var localResponse = new LocalResponse(Files.newInputStream(this.filePath)); + Assertions.assertNotNull(localResponse.getFile()); + Assertions + .assertFalse( + localResponse.isValidHmacSignature(this.secretKey, "invalid signature is invalid") + ); + Assertions.assertEquals(this.signature, localResponse.getHmacSignature(this.secretKey)); + Assertions.assertTrue(localResponse.isValidHmacSignature(this.secretKey, this.signature)); + } + + @Test + void givenJsonInput_whenSync_shouldDeserializeCorrectly() throws IOException { + var localResponse = new LocalResponse( + getV1ResourcePath("products/invoices/response_v4/complete.json") + ); + var response = localResponse.deserializeSyncResponse(InvoiceV4.class); + assertStringEqualsFile( + response.getDocument().toString(), + getV1ResourcePath("/products/invoices/response_v4/summary_full.rst") + ); + } + + @Test + void givenJsonInput_whenAsync_shouldDeserializeCorrectly() throws IOException { + var localResponse = new LocalResponse( + getV1ResourcePath("products/international_id/response_v2/complete.json") + ); + var response = localResponse.deserializeAsyncResponse(InternationalIdV2.class); + assertStringEqualsFile( + response.getDocumentObj().toString(), + getV1ResourcePathString("products/international_id/response_v2/summary_full.rst") + ); + } + + @Test + void givenInvalidJsonInput_shouldThrow() { + var localResponse = new LocalResponse("{invalid json"); + var err = Assertions + .assertThrows( + MindeeException.class, + () -> localResponse.deserializeSyncResponse(InvoiceV4.class) + ); + Assertions.assertEquals("Invalid JSON payload.", err.getMessage()); + } +} diff --git a/src/test/java/com/mindee/v2/MindeeClientTest.java b/src/test/java/com/mindee/v2/MindeeClientTest.java index 0696fde08..b3e5f1da6 100644 --- a/src/test/java/com/mindee/v2/MindeeClientTest.java +++ b/src/test/java/com/mindee/v2/MindeeClientTest.java @@ -7,7 +7,6 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.mindee.input.LocalInputSource; -import com.mindee.input.LocalResponse; import com.mindee.input.URLInputSource; import com.mindee.v2.clientOptions.BaseParameters; import com.mindee.v2.http.MindeeApiV2; @@ -34,18 +33,12 @@ public FakeMindeeApiV2(JobResponse jobResponse, CommonResponse resultResponse) { } @Override - public JobResponse reqPostEnqueue( - LocalInputSource inputSource, - BaseParameters options - ) throws IOException { + public JobResponse reqPostEnqueue(LocalInputSource inputSource, BaseParameters options) { return jobResponse; } @Override - public JobResponse reqPostEnqueue( - URLInputSource inputSource, - BaseParameters options - ) throws IOException { + public JobResponse reqPostEnqueue(URLInputSource inputSource, BaseParameters options) { return jobResponse; } @@ -136,36 +129,4 @@ void document_getResult_async() throws IOException { ); } } - - @Nested - @DisplayName("deserializeResponse()") - class DeserializeResponse { - - @Test - @DisplayName("parses local JSON and exposes correct field values") - void inference_loadsLocally() throws IOException { - var localResponse = new LocalResponse( - getResourcePath("v2/products/extraction/financial_document/complete.json") - ); - ExtractionResponse loaded = localResponse.deserializeResponse(ExtractionResponse.class); - - assertNotNull(loaded, "Loaded InferenceResponse must not be null"); - assertEquals( - "12345678-1234-1234-1234-123456789abc", - loaded.getInference().getModel().getId(), - "Model Id mismatch" - ); - assertEquals( - "John Smith", - loaded - .getInference() - .getResult() - .getFields() - .get("supplier_name") - .getSimpleField() - .getValue(), - "Supplier name mismatch" - ); - } - } } diff --git a/src/test/java/com/mindee/input/LocalResponseV2Test.java b/src/test/java/com/mindee/v2/input/LocalResponseTest.java similarity index 75% rename from src/test/java/com/mindee/input/LocalResponseV2Test.java rename to src/test/java/com/mindee/v2/input/LocalResponseTest.java index 5213d31aa..860d83095 100644 --- a/src/test/java/com/mindee/input/LocalResponseV2Test.java +++ b/src/test/java/com/mindee/v2/input/LocalResponseTest.java @@ -1,7 +1,8 @@ -package com.mindee.input; +package com.mindee.v2.input; import static com.mindee.TestingUtilities.getV2ResourcePath; +import com.mindee.v2.parsing.LocalResponse; import com.mindee.v2.product.extraction.ExtractionResponse; import java.io.File; import java.io.IOException; @@ -10,19 +11,19 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -public class LocalResponseV2Test { +public class LocalResponseTest { /** * Fake secret key. */ String secretKey = "ogNjY44MhvKPGTtVsI8zG82JqWQa68woYQH"; /** - * Real signature using fake secret key. + * Real signature using a fake secret key. */ String signature = "e51bdf80f1a08ed44ee161100fc30a25cb35b4ede671b0a575dc9064a3f5dbf1"; /** - * File which the signature applies to. + * File that the signature applies to. */ Path filePath = getV2ResourcePath("products/extraction/standard_field_types.json"); @@ -34,20 +35,19 @@ protected void assertLocalResponse(LocalResponse localResponse) { ); Assertions.assertEquals(this.signature, localResponse.getHmacSignature(this.secretKey)); Assertions.assertTrue(localResponse.isValidHmacSignature(this.secretKey, this.signature)); - ExtractionResponse response = localResponse.deserializeResponse(ExtractionResponse.class); + var response = localResponse.deserializeResponse(ExtractionResponse.class); Assertions.assertNotNull(response); Assertions.assertNotNull(response.getInference()); } @Test void loadDocument_withFile_mustReturnValidLocalResponse() throws IOException { - LocalResponse localResponse = new LocalResponse(new File(this.filePath.toString())); - assertLocalResponse(localResponse); + assertLocalResponse(new LocalResponse(new File(this.filePath.toString()))); } @Test void loadDocument_withString_mustReturnValidLocalResponse() { - LocalResponse localResponse = new LocalResponse("{'some': 'json', 'with': 'data'}"); + var localResponse = new LocalResponse("{'some': 'json', 'with': 'data'}"); Assertions.assertNotNull(localResponse.getFile()); Assertions .assertFalse( @@ -57,7 +57,7 @@ void loadDocument_withString_mustReturnValidLocalResponse() { @Test void loadDocument_withInputStream_mustReturnValidLocalResponse() throws IOException { - LocalResponse localResponse = new LocalResponse(Files.newInputStream(this.filePath)); + var localResponse = new LocalResponse(Files.newInputStream(this.filePath)); assertLocalResponse(localResponse); } } diff --git a/src/test/java/com/mindee/v2/parsing/JobTest.java b/src/test/java/com/mindee/v2/parsing/JobTest.java index 87f27fd26..77e97ffd1 100644 --- a/src/test/java/com/mindee/v2/parsing/JobTest.java +++ b/src/test/java/com/mindee/v2/parsing/JobTest.java @@ -5,7 +5,6 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; -import com.mindee.input.LocalResponse; import java.io.IOException; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; diff --git a/src/test/java/com/mindee/v2/parsing/LocalResponseTest.java b/src/test/java/com/mindee/v2/parsing/LocalResponseTest.java new file mode 100644 index 000000000..ad59a14e3 --- /dev/null +++ b/src/test/java/com/mindee/v2/parsing/LocalResponseTest.java @@ -0,0 +1,50 @@ +package com.mindee.v2.parsing; + +import static com.mindee.TestingUtilities.getResourcePath; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import com.mindee.MindeeException; +import com.mindee.v2.product.extraction.ExtractionResponse; +import java.io.IOException; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class LocalResponseTest { + @Test + void loadDocument_withPath_mustReturnValidLocalResponse() throws IOException { + var localResponse = new LocalResponse( + getResourcePath("v2/products/extraction/financial_document/complete.json") + ); + ExtractionResponse loaded = localResponse.deserializeResponse(ExtractionResponse.class); + + assertNotNull(loaded, "Loaded InferenceResponse must not be null"); + assertEquals( + "12345678-1234-1234-1234-123456789abc", + loaded.getInference().getModel().getId(), + "Model Id mismatch" + ); + assertEquals( + "John Smith", + loaded + .getInference() + .getResult() + .getFields() + .get("supplier_name") + .getSimpleField() + .getValue(), + "Supplier name mismatch" + ); + } + + @Test + void givenInvalidJsonInput_shouldThrow() { + var localResponse = new LocalResponse("{invalid json"); + var err = Assertions + .assertThrows( + MindeeException.class, + () -> localResponse.deserializeResponse(ExtractionResponse.class) + ); + Assertions.assertEquals("Invalid JSON payload.", err.getMessage()); + } +} diff --git a/src/test/java/com/mindee/v2/product/ClassificationTest.java b/src/test/java/com/mindee/v2/product/ClassificationTest.java index d7701c82c..624b5020b 100644 --- a/src/test/java/com/mindee/v2/product/ClassificationTest.java +++ b/src/test/java/com/mindee/v2/product/ClassificationTest.java @@ -4,7 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import com.mindee.input.LocalResponse; +import com.mindee.v2.parsing.LocalResponse; import com.mindee.v2.product.classification.ClassificationResponse; import java.io.IOException; import org.junit.jupiter.api.DisplayName; diff --git a/src/test/java/com/mindee/v2/product/CropTest.java b/src/test/java/com/mindee/v2/product/CropTest.java index b171a1653..5798aa523 100644 --- a/src/test/java/com/mindee/v2/product/CropTest.java +++ b/src/test/java/com/mindee/v2/product/CropTest.java @@ -5,7 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import com.mindee.input.LocalResponse; +import com.mindee.v2.parsing.LocalResponse; import com.mindee.v2.product.crop.CropResponse; import java.io.IOException; import org.junit.jupiter.api.DisplayName; diff --git a/src/test/java/com/mindee/v2/parsing/ExtractionTest.java b/src/test/java/com/mindee/v2/product/ExtractionTest.java similarity index 99% rename from src/test/java/com/mindee/v2/parsing/ExtractionTest.java rename to src/test/java/com/mindee/v2/product/ExtractionTest.java index 9927d20aa..724e37639 100644 --- a/src/test/java/com/mindee/v2/parsing/ExtractionTest.java +++ b/src/test/java/com/mindee/v2/product/ExtractionTest.java @@ -1,4 +1,4 @@ -package com.mindee.v2.parsing; +package com.mindee.v2.product; import static com.mindee.TestingUtilities.getV2ResourcePath; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -11,7 +11,7 @@ import com.mindee.geometry.Point; import com.mindee.geometry.Polygon; -import com.mindee.input.LocalResponse; +import com.mindee.v2.parsing.LocalResponse; import com.mindee.v2.parsing.inference.InferenceActiveOptions; import com.mindee.v2.parsing.inference.InferenceFile; import com.mindee.v2.parsing.inference.InferenceJob; diff --git a/src/test/java/com/mindee/v2/product/OcrTest.java b/src/test/java/com/mindee/v2/product/OcrTest.java index ef734eec0..ae269073e 100644 --- a/src/test/java/com/mindee/v2/product/OcrTest.java +++ b/src/test/java/com/mindee/v2/product/OcrTest.java @@ -4,7 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import com.mindee.input.LocalResponse; +import com.mindee.v2.parsing.LocalResponse; import com.mindee.v2.product.ocr.OcrResponse; import java.io.IOException; import org.junit.jupiter.api.DisplayName; diff --git a/src/test/java/com/mindee/v2/product/SplitTest.java b/src/test/java/com/mindee/v2/product/SplitTest.java index d4ff870d1..b3db37ff4 100644 --- a/src/test/java/com/mindee/v2/product/SplitTest.java +++ b/src/test/java/com/mindee/v2/product/SplitTest.java @@ -4,7 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import com.mindee.input.LocalResponse; +import com.mindee.v2.parsing.LocalResponse; import com.mindee.v2.product.split.SplitResponse; import java.io.IOException; import org.junit.jupiter.api.DisplayName;