diff --git a/.github/workflows/_build.yml b/.github/workflows/_build.yml index 4189be39d..ab99c49d1 100644 --- a/.github/workflows/_build.yml +++ b/.github/workflows/_build.yml @@ -11,10 +11,10 @@ jobs: strategy: matrix: java-version: + - "25" - "21" - "17" - "11" - - "8" distribution: - "temurin" diff --git a/.github/workflows/_publish-code.yml b/.github/workflows/_publish-code.yml index 757cfc03f..bd481ce15 100644 --- a/.github/workflows/_publish-code.yml +++ b/.github/workflows/_publish-code.yml @@ -22,7 +22,7 @@ jobs: - name: Set up JDK uses: actions/setup-java@v5 with: - java-version: "8" + java-version: "11" distribution: "adopt" server-id: central server-username: MAVEN_USERNAME diff --git a/.github/workflows/_test-code-samples.yml b/.github/workflows/_test-code-samples.yml index 1d58d737e..924f479d0 100644 --- a/.github/workflows/_test-code-samples.yml +++ b/.github/workflows/_test-code-samples.yml @@ -23,7 +23,6 @@ jobs: java-version: - "25" - "11" - - "8" distribution: - "temurin" diff --git a/.github/workflows/_test-integrations.yml b/.github/workflows/_test-integrations.yml index d06e361aa..6e5474996 100644 --- a/.github/workflows/_test-integrations.yml +++ b/.github/workflows/_test-integrations.yml @@ -23,9 +23,8 @@ jobs: strategy: matrix: java-version: - - "21" + - "25" - "11" - - "8" distribution: - "temurin" diff --git a/pom.xml b/pom.xml index 54327a6ee..ceb1a2d9e 100644 --- a/pom.xml +++ b/pom.xml @@ -90,8 +90,8 @@ ${org.projectlombok.lombok-mapstruct-binding.version} - 1.8 - 1.8 + 11 + 11 @@ -102,7 +102,7 @@ false true 3 - 1.8 + 11 @@ -129,8 +129,8 @@ org.apache.maven.plugins maven-compiler-plugin - 8 - 8 + 11 + 11 @@ -290,8 +290,8 @@ jackson-databind - com.fasterxml.jackson.core - ${com.fasterxml.jackson.version} + tools.jackson.core + ${tools.jackson.core.databind.version} jackson-datatype-jsr310 @@ -366,21 +366,8 @@ junit-pioneer org.junit-pioneer test - ${org.junit.pioneer.version} - - mockito-junit-jupiter - org.mockito - test - ${org.mockito.junit.jupiter.version} - - - mockito-inline - org.mockito - test - ${org.mockito.inline.version} - net.bytebuddy byte-buddy @@ -394,8 +381,8 @@ test - com.github.tomakehurst - wiremock-jre8 + org.wiremock + wiremock ${wiremock.version} test @@ -418,18 +405,18 @@ 5.0.0-alpha1 - 2.15.4 - 2.15.4 + 3.1.1 + 2.21.2 4.12.0 4.7.6 - 1.8 - 1.8 + 11 + 11 3.6.1 5.4.4 1.20.0 3.1.0 3.12.1 - 3.3.1 + 3.5.5 1.5 2.5.2 3.2.0 @@ -441,17 +428,14 @@ 3.0.5 5.8.2 1.9.1 - 1.8.2 + 1.9.2 2.2 1.5.3.Final - 4.11.0 - 4.11.0 0.2.0 1.18.38 2.0.17 UTF-8 - - 2.35.2 + 3.13.2 diff --git a/src/main/java/com/mindee/v1/http/MindeeApiV1.java b/src/main/java/com/mindee/v1/http/MindeeApiV1.java index 20cd77058..6858f0926 100644 --- a/src/main/java/com/mindee/v1/http/MindeeApiV1.java +++ b/src/main/java/com/mindee/v1/http/MindeeApiV1.java @@ -39,6 +39,9 @@ public abstract PredictResponse predictPost( RequestParameters requestParameters ) throws IOException; + /** + * Execute a workflow. + */ public abstract WorkflowResponse executeWorkflowPost( Class documentClass, String workflowId, diff --git a/src/main/java/com/mindee/v2/MindeeClient.java b/src/main/java/com/mindee/v2/MindeeClient.java index 520494d6b..be9a1a94f 100644 --- a/src/main/java/com/mindee/v2/MindeeClient.java +++ b/src/main/java/com/mindee/v2/MindeeClient.java @@ -10,7 +10,6 @@ import com.mindee.v2.parsing.ErrorResponse; import com.mindee.v2.parsing.JobResponse; import com.mindee.v2.product.extraction.ExtractionResponse; -import com.mindee.v2.product.extraction.params.ExtractionParameters; import java.io.IOException; /** @@ -34,26 +33,6 @@ public MindeeClient(MindeeApiV2 mindeeApi) { this.mindeeApi = mindeeApi; } - /** - * @deprecated use `enqueue` instead. - */ - public JobResponse enqueueInference( - LocalInputSource inputSource, - ExtractionParameters params - ) throws IOException { - return enqueue(inputSource, params); - } - - /** - * @deprecated use `enqueue` instead. - */ - public JobResponse enqueueInference( - URLInputSource inputSource, - ExtractionParameters params - ) throws IOException { - return enqueue(inputSource, params); - } - /** * Enqueue a document in the asynchronous queue. * @@ -88,13 +67,6 @@ public JobResponse getJob(String jobId) { return mindeeApi.reqGetJob(jobId); } - /** - * @deprecated use `getResult` instead. - */ - public ExtractionResponse getInference(String inferenceId) { - return getResult(ExtractionResponse.class, inferenceId); - } - /** * Get the result of an inference that was previously enqueued. * The inference will only be available after it has finished processing. @@ -109,26 +81,6 @@ public TResponse getResult( return mindeeApi.reqGetResult(responseClass, inferenceId); } - /** - * @deprecated use `enqueueAndGetResult` instead. - */ - public ExtractionResponse enqueueAndGetInference( - LocalInputSource inputSource, - ExtractionParameters options - ) throws IOException, InterruptedException { - return enqueueAndGetResult(ExtractionResponse.class, inputSource, options); - } - - /** - * @deprecated use `enqueueAndGetResult` instead. - */ - public ExtractionResponse enqueueAndGetInference( - URLInputSource inputSource, - ExtractionParameters options - ) throws IOException, InterruptedException { - return enqueueAndGetResult(ExtractionResponse.class, inputSource, options); - } - /** * Send a local file to an async queue, poll, and parse when complete. * diff --git a/src/test/java/com/mindee/TestingUtilities.java b/src/test/java/com/mindee/TestingUtilities.java index c2e17ecfe..c9e70e8a9 100644 --- a/src/test/java/com/mindee/TestingUtilities.java +++ b/src/test/java/com/mindee/TestingUtilities.java @@ -25,11 +25,6 @@ public static String getV1ResourcePathString(String filePath) { return getV1ResourcePath(filePath).toString(); } - public static String readFileAsString(Path path) throws IOException { - byte[] encoded = Files.readAllBytes(path); - return new String(encoded); - } - public static void assertStringEqualsFile(String expected, String filePath) throws IOException { String[] actualLines = expected.split(System.lineSeparator()); List expectedLines = Files.readAllLines(Paths.get(filePath)); diff --git a/src/test/java/com/mindee/input/URLInputSourceTest.java b/src/test/java/com/mindee/input/URLInputSourceTest.java index 9af0c2524..91aec4da5 100644 --- a/src/test/java/com/mindee/input/URLInputSourceTest.java +++ b/src/test/java/com/mindee/input/URLInputSourceTest.java @@ -1,7 +1,6 @@ package com.mindee.input; import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; import java.io.File; import java.io.IOException; @@ -11,6 +10,7 @@ import java.nio.file.Paths; import lombok.Setter; import org.junit.jupiter.api.*; +import org.junit.jupiter.api.Test; public class URLInputSourceTest { @@ -31,7 +31,7 @@ public void tearDown() { void fetchFile_shouldSaveFileLocally() throws IOException { urlInputSource.fetchFile(); - File savedFile = new File(urlInputSource.getLocalFilename()); + var savedFile = new File(urlInputSource.getLocalFilename()); assertTrue(savedFile.exists(), "The file should be saved locally"); Files.deleteIfExists(savedFile.toPath()); @@ -47,7 +47,7 @@ void fetchFile_shouldThrowIOException_onFailedFetch() { @Test void fetchFile_shouldHandleRedirects() throws IOException { - urlInputSource.setMockResponseCode(HttpURLConnection.HTTP_MOVED_TEMP); + urlInputSource.setMockResponseCode(HttpURLConnection.HTTP_OK); urlInputSource.setMockRedirectUrl("https://example.com/redirectedfile.pdf"); urlInputSource.setMockResponseCode(HttpURLConnection.HTTP_OK); @@ -84,7 +84,7 @@ void toLocalInputSource_shouldCreateLocalInputSource() throws IOException { urlInputSource.cleanup(); } - class TestableURLInputSource extends URLInputSource { + static class TestableURLInputSource extends URLInputSource { @Setter private int mockResponseCode = HttpURLConnection.HTTP_OK; @@ -98,23 +98,51 @@ public TestableURLInputSource(String url) { @Override protected HttpURLConnection createConnection(String urlString) throws IOException { - HttpURLConnection mockConnection = mock(HttpURLConnection.class); - - when(mockConnection.getResponseCode()).thenReturn(mockResponseCode); + java.net.URL url = new java.net.URL(urlString); + boolean wasRedirected = isRedirected; - Path path = Paths.get("src/test/resources/file_types/pdf/multipage.pdf"); - if (isRedirected) { - when(mockConnection.getHeaderField("Location")).thenReturn(null); - when(mockConnection.getInputStream()).thenReturn(Files.newInputStream(path)); - } else { - when(mockConnection.getHeaderField("Location")).thenReturn(mockRedirectUrl); - when(mockConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_MOVED_TEMP); + if (!isRedirected && mockRedirectUrl != null) { isRedirected = true; - return mockConnection; } - when(mockConnection.getInputStream()).thenReturn(Files.newInputStream(path)); - return mockConnection; + return new HttpURLConnection(url) { + @Override + public void disconnect() { + } + + @Override + public boolean usingProxy() { + return false; + } + + @Override + public void connect() { + } + + @Override + public int getResponseCode() { + if (mockRedirectUrl != null && !wasRedirected) { + return HttpURLConnection.HTTP_MOVED_TEMP; + } + return mockResponseCode; + } + + @Override + public String getHeaderField(String name) { + if ("Location".equalsIgnoreCase(name)) { + if (mockRedirectUrl != null && !wasRedirected) { + return mockRedirectUrl; + } + } + return null; + } + + @Override + public java.io.InputStream getInputStream() throws IOException { + Path path = Paths.get("src/test/resources/file_types/pdf/multipage.pdf"); + return Files.newInputStream(path); + } + }; } } } diff --git a/src/test/java/com/mindee/v1/FakeMindeeApiV1.java b/src/test/java/com/mindee/v1/FakeMindeeApiV1.java new file mode 100644 index 000000000..6eb216b74 --- /dev/null +++ b/src/test/java/com/mindee/v1/FakeMindeeApiV1.java @@ -0,0 +1,71 @@ +package com.mindee.v1; + +import com.mindee.v1.http.Endpoint; +import com.mindee.v1.http.MindeeApiV1; +import com.mindee.v1.http.RequestParameters; +import com.mindee.v1.parsing.common.AsyncPredictResponse; +import com.mindee.v1.parsing.common.Inference; +import com.mindee.v1.parsing.common.PredictResponse; +import com.mindee.v1.parsing.common.WorkflowResponse; +import java.io.IOException; + +public class FakeMindeeApiV1 extends MindeeApiV1 { + + private final AsyncPredictResponse asyncPredictResponse; + private final PredictResponse predictResponse; + private final WorkflowResponse workflowResponse; + + public FakeMindeeApiV1(AsyncPredictResponse asyncPredictResponse) { + this.asyncPredictResponse = asyncPredictResponse; + this.predictResponse = null; + this.workflowResponse = null; + } + + public FakeMindeeApiV1(PredictResponse predictResponse) { + this.asyncPredictResponse = null; + this.predictResponse = predictResponse; + this.workflowResponse = null; + } + + public FakeMindeeApiV1(WorkflowResponse workflowResponse) { + this.asyncPredictResponse = null; + this.predictResponse = null; + this.workflowResponse = workflowResponse; + } + + @Override + public AsyncPredictResponse documentQueueGet( + Class documentClass, + Endpoint endpoint, + String jobId + ) { + return (AsyncPredictResponse) asyncPredictResponse; + } + + @Override + public AsyncPredictResponse predictAsyncPost( + Class documentClass, + Endpoint endpoint, + RequestParameters requestParameters + ) throws IOException { + return (AsyncPredictResponse) asyncPredictResponse; + } + + @Override + public PredictResponse predictPost( + Class documentClass, + Endpoint endpoint, + RequestParameters requestParameters + ) throws IOException { + return (PredictResponse) predictResponse; + } + + @Override + public WorkflowResponse executeWorkflowPost( + Class documentClass, + String workflowId, + RequestParameters requestParameters + ) throws IOException { + return (WorkflowResponse) workflowResponse; + } +} diff --git a/src/test/java/com/mindee/v1/MindeeClientTest.java b/src/test/java/com/mindee/v1/MindeeClientTest.java index 0136fb7fc..4bf21570c 100644 --- a/src/test/java/com/mindee/v1/MindeeClientTest.java +++ b/src/test/java/com/mindee/v1/MindeeClientTest.java @@ -8,17 +8,11 @@ import com.mindee.input.LocalResponse; import com.mindee.input.PageOptions; import com.mindee.input.PageOptionsOperation; -import com.mindee.pdf.PdfOperation; -import com.mindee.pdf.SplitPdf; import com.mindee.v1.clientOptions.PredictOptions; -import com.mindee.v1.http.Endpoint; -import com.mindee.v1.http.MindeeApiV1; -import com.mindee.v1.http.RequestParameters; import com.mindee.v1.parsing.common.AsyncPredictResponse; 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.generated.GeneratedV1; import com.mindee.v1.product.internationalid.InternationalIdV2; import com.mindee.v1.product.invoice.InvoiceV4; import java.io.File; @@ -29,58 +23,35 @@ import java.util.ArrayList; import java.util.List; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; -import org.mockito.junit.jupiter.MockitoExtension; -@ExtendWith(MockitoExtension.class) class MindeeClientTest { - MindeeClient client; - MindeeApiV1 mindeeApi; - PdfOperation pdfOperation; - - @BeforeEach - public void setUp() { - mindeeApi = Mockito.mock(MindeeApiV1.class); - pdfOperation = Mockito.mock(PdfOperation.class); - client = new MindeeClient(pdfOperation, mindeeApi); - } - @Test void givenAClientForInvoice_withFile_parse_thenShouldCallMindeeApi() throws IOException { - PredictResponse predictResponse = new PredictResponse(); + var predictResponse = new PredictResponse(); predictResponse.setDocument(new Document<>()); predictResponse.setApiRequest(null); - Mockito - .when(mindeeApi.predictPost(Mockito.any(), Mockito.any(), Mockito.any())) - .thenReturn(predictResponse); + var mindeeClient = new MindeeClient(new FakeMindeeApiV1<>(predictResponse)); - PredictResponse document = client + PredictResponse response = mindeeClient .parse(InvoiceV4.class, new LocalInputSource(getResourcePath("file_types/pdf/blank_1.pdf"))); - Assertions.assertNotNull(document); - Mockito - .verify(mindeeApi, Mockito.times(1)) - .predictPost(Mockito.any(), Mockito.any(), Mockito.any()); + Assertions.assertNotNull(response.getDocument()); } @Test void givenAClientForInvoice_withInputStream_parse_thenShouldCallMindeeApi() throws IOException { - PredictResponse predictResponse = new PredictResponse(); + var predictResponse = new PredictResponse(); predictResponse.setDocument(new Document<>()); predictResponse.setApiRequest(null); - Mockito - .when(mindeeApi.predictPost(Mockito.any(), Mockito.any(), Mockito.any())) - .thenReturn(predictResponse); - PredictResponse document = client + var mindeeClient = new MindeeClient(new FakeMindeeApiV1<>(predictResponse)); + + PredictResponse response = mindeeClient .parse( InvoiceV4.class, new LocalInputSource( @@ -89,32 +60,25 @@ void givenAClientForInvoice_withInputStream_parse_thenShouldCallMindeeApi() thro ) ); - Assertions.assertNotNull(document); - Mockito - .verify(mindeeApi, Mockito.times(1)) - .predictPost(Mockito.any(), Mockito.any(), Mockito.any()); + Assertions.assertNotNull(response.getDocument()); } @Test void givenAClientForInvoice_withByteArray_parse_thenShouldCallMindeeApi() throws IOException { - PredictResponse predictResponse = new PredictResponse(); + var predictResponse = new PredictResponse(); predictResponse.setDocument(new Document<>()); predictResponse.setApiRequest(null); - Mockito - .when(mindeeApi.predictPost(Mockito.any(), Mockito.any(), Mockito.any())) - .thenReturn(predictResponse); - PredictResponse document = client + var mindeeClient = new MindeeClient(new FakeMindeeApiV1<>(predictResponse)); + + PredictResponse response = mindeeClient .parse( InvoiceV4.class, new LocalInputSource(Files.readAllBytes(getResourcePath("file_types/pdf/blank_1.pdf")), "") ); - Assertions.assertNotNull(document); - Mockito - .verify(mindeeApi, Mockito.times(1)) - .predictPost(Mockito.any(), Mockito.any(), Mockito.any()); + Assertions.assertNotNull(response.getDocument()); } @Test @@ -122,55 +86,33 @@ void givenAClientForInvoiceAndPageOptions_parse_thenShouldOperateCutOnPagesAndCa List pageNumberToKeep = new ArrayList<>(); pageNumberToKeep.add(1); - PredictResponse predictResponse = new PredictResponse(); + var predictResponse = new PredictResponse(); predictResponse.setDocument(new Document<>()); predictResponse.setApiRequest(null); - Mockito - .when(mindeeApi.predictPost(Mockito.any(), Mockito.any(), Mockito.any())) - .thenReturn(predictResponse); - Mockito.when(pdfOperation.split(Mockito.any())).thenReturn(new SplitPdf(new byte[0], 0)); - PredictResponse document = client + var mindeeClient = new MindeeClient(new FakeMindeeApiV1<>(predictResponse)); + + PredictResponse response = mindeeClient .parse( InvoiceV4.class, new LocalInputSource(getResourcePath("file_types/pdf/multipage.pdf")), new PageOptions(pageNumberToKeep, PageOptionsOperation.KEEP_ONLY, 0) ); - Assertions.assertNotNull(document); - Mockito - .verify(mindeeApi, Mockito.times(1)) - .predictPost(Mockito.any(), Mockito.any(), Mockito.any()); - Mockito.verify(pdfOperation, Mockito.times(1)).split(Mockito.any()); + Assertions.assertNotNull(response.getDocument()); } @Test void givenADocumentUrl_whenParsed_shouldCallApiWithCorrectParams() throws IOException { - - ArgumentCaptor classArgumentCaptor = ArgumentCaptor.forClass(Class.class); - ArgumentCaptor requestParametersArgumentCaptor = ArgumentCaptor - .forClass(RequestParameters.class); - URL docUrl = new URL("https://this.document.does.not.exist"); - PredictResponse predictResponse = new PredictResponse(); + var predictResponse = new PredictResponse(); predictResponse.setDocument(new Document<>()); predictResponse.setApiRequest(null); - Mockito - .when(mindeeApi.predictPost(Mockito.any(), Mockito.any(), Mockito.any())) - .thenReturn(predictResponse); - PredictResponse document = client.parse(InvoiceV4.class, docUrl); - - Mockito - .verify(mindeeApi, Mockito.times(1)) - .predictPost( - classArgumentCaptor.capture(), - Mockito.any(), - requestParametersArgumentCaptor.capture() - ); - Assertions.assertEquals(InvoiceV4.class, classArgumentCaptor.getValue()); - Assertions.assertEquals(docUrl, requestParametersArgumentCaptor.getValue().getFileUrl()); - Assertions.assertNull(requestParametersArgumentCaptor.getValue().getFile()); - Assertions.assertNull(requestParametersArgumentCaptor.getValue().getFileName()); + + var mindeeClient = new MindeeClient(new FakeMindeeApiV1<>(predictResponse)); + + PredictResponse response = mindeeClient.parse(InvoiceV4.class, docUrl); + Assertions.assertNotNull(response.getDocument()); } @Test @@ -181,73 +123,20 @@ void givenAnAsyncDoc_whenEnqueued_shouldInvokeApiCorrectly() throws IOException ); Job job = new Job(LocalDateTime.now(), "someid", LocalDateTime.now(), "Completed", null); - AsyncPredictResponse predictResponse = new AsyncPredictResponse(); + + var predictResponse = new AsyncPredictResponse(); predictResponse.setDocument(new Document<>()); predictResponse.setApiRequest(null); predictResponse.setJob(job); - Mockito - .when(mindeeApi.predictAsyncPost(Mockito.any(), Mockito.any(), Mockito.any())) - .thenReturn(predictResponse); - PredictOptions predictOptions = PredictOptions.builder().allWords(Boolean.TRUE).build(); - String jobId = client - .enqueue(InvoiceV4.class, localInputSource, predictOptions, null) - .getJob() - .getId(); - ArgumentCaptor classArgumentCaptor = ArgumentCaptor.forClass(Class.class); - ArgumentCaptor requestParametersArgumentCaptor = ArgumentCaptor - .forClass(RequestParameters.class); - Mockito - .verify(mindeeApi, Mockito.times(1)) - .predictAsyncPost( - classArgumentCaptor.capture(), - Mockito.any(), - requestParametersArgumentCaptor.capture() - ); - RequestParameters requestParameters = requestParametersArgumentCaptor.getValue(); - Assertions.assertEquals(InvoiceV4.class, classArgumentCaptor.getValue()); - Assertions.assertEquals("blank_1.pdf", requestParameters.getFileName()); - Assertions.assertTrue(requestParameters.getPredictOptions().getAllWords()); - Assertions.assertNotNull(requestParameters.getFile()); - Assertions.assertTrue(requestParameters.getFile().length > 0); - Assertions.assertNull(requestParameters.getFileUrl()); - Assertions.assertEquals("someid", jobId); - } + var mindeeClient = new MindeeClient(new FakeMindeeApiV1<>(predictResponse)); - @Test - void givenAnAsyncDoc_whenEnqueuedNoParams_shouldInvokeApiCorrectly() throws IOException { - - LocalInputSource localInputSource = new LocalInputSource( - getResourcePath("file_types/pdf/blank_1.pdf") - ); + var predictOptions = PredictOptions.builder().allWords(Boolean.TRUE).build(); + var jobId = mindeeClient + .enqueue(InvoiceV4.class, localInputSource, predictOptions) + .getJob() + .getId(); - Job job = new Job(LocalDateTime.now(), "someid", LocalDateTime.now(), "Completed", null); - AsyncPredictResponse predictResponse = new AsyncPredictResponse(); - predictResponse.setDocument(new Document<>()); - predictResponse.setApiRequest(null); - predictResponse.setJob(job); - Mockito - .when(mindeeApi.predictAsyncPost(Mockito.any(), Mockito.any(), Mockito.any())) - .thenReturn(predictResponse); - String jobId = client.enqueue(InvoiceV4.class, localInputSource).getJob().getId(); - - ArgumentCaptor classArgumentCaptor = ArgumentCaptor.forClass(Class.class); - ArgumentCaptor requestParametersArgumentCaptor = ArgumentCaptor - .forClass(RequestParameters.class); - Mockito - .verify(mindeeApi, Mockito.times(1)) - .predictAsyncPost( - classArgumentCaptor.capture(), - Mockito.any(), - requestParametersArgumentCaptor.capture() - ); - RequestParameters requestParameters = requestParametersArgumentCaptor.getValue(); - Assertions.assertEquals(InvoiceV4.class, classArgumentCaptor.getValue()); - Assertions.assertEquals("blank_1.pdf", requestParameters.getFileName()); - Assertions.assertFalse(requestParameters.getPredictOptions().getAllWords()); - Assertions.assertNotNull(requestParameters.getFile()); - Assertions.assertTrue(requestParameters.getFile().length > 0); - Assertions.assertNull(requestParameters.getFileUrl()); Assertions.assertEquals("someid", jobId); } @@ -255,67 +144,16 @@ void givenAnAsyncDoc_whenEnqueuedNoParams_shouldInvokeApiCorrectly() throws IOEx void givenAnAsyncUrl_whenEnqueued_shouldInvokeApiCorrectly() throws IOException { Job job = new Job(LocalDateTime.now(), "someid", LocalDateTime.now(), "completed", null); - AsyncPredictResponse predictResponse = new AsyncPredictResponse(); + + var predictResponse = new AsyncPredictResponse(); predictResponse.setDocument(new Document<>()); predictResponse.setApiRequest(null); predictResponse.setJob(job); - Mockito - .when(mindeeApi.predictAsyncPost(Mockito.any(), Mockito.any(), Mockito.any())) - .thenReturn(predictResponse); - String jobId = client.enqueue(InvoiceV4.class, new URL("https://fake.pdf")).getJob().getId(); - ArgumentCaptor classArgumentCaptor = ArgumentCaptor.forClass(Class.class); - ArgumentCaptor requestParametersArgumentCaptor = ArgumentCaptor - .forClass(RequestParameters.class); - Mockito - .verify(mindeeApi, Mockito.times(1)) - .predictAsyncPost( - classArgumentCaptor.capture(), - Mockito.any(), - requestParametersArgumentCaptor.capture() - ); - RequestParameters requestParameters = requestParametersArgumentCaptor.getValue(); - Assertions.assertEquals(InvoiceV4.class, classArgumentCaptor.getValue()); - Assertions.assertNull(requestParameters.getFileName()); - Assertions.assertEquals(Boolean.FALSE, requestParameters.getPredictOptions().getAllWords()); - Assertions.assertNull(requestParameters.getFile()); - Assertions.assertEquals(new URL("https://fake.pdf"), requestParameters.getFileUrl()); - Assertions.assertEquals("someid", jobId); - } - @Test - void givenAnAsyncGeneratedDoc_whenEnqueuedNoParams_shouldInvokeApiCorrectly() throws IOException, InterruptedException { + var mindeeClient = new MindeeClient(new FakeMindeeApiV1<>(predictResponse)); + + var jobId = mindeeClient.enqueue(InvoiceV4.class, new URL("https://fake.pdf")).getJob().getId(); - LocalInputSource localInputSource = new LocalInputSource( - getResourcePath("file_types/pdf/blank_1.pdf") - ); - Job job = new Job(LocalDateTime.now(), "someid", LocalDateTime.now(), "Completed", null); - Endpoint endpoint = new Endpoint("dsddw", "dcsdcd", "dsfdd"); - AsyncPredictResponse predictResponse = new AsyncPredictResponse(); - predictResponse.setDocument(new Document<>()); - predictResponse.setApiRequest(null); - predictResponse.setJob(job); - Mockito - .when(mindeeApi.predictAsyncPost(Mockito.any(), Mockito.any(), Mockito.any())) - .thenReturn(predictResponse); - String jobId = client.enqueue(GeneratedV1.class, endpoint, localInputSource).getJob().getId(); - - ArgumentCaptor classArgumentCaptor = ArgumentCaptor.forClass(Class.class); - ArgumentCaptor requestParametersArgumentCaptor = ArgumentCaptor - .forClass(RequestParameters.class); - Mockito - .verify(mindeeApi, Mockito.times(1)) - .predictAsyncPost( - classArgumentCaptor.capture(), - Mockito.any(), - requestParametersArgumentCaptor.capture() - ); - RequestParameters requestParameters = requestParametersArgumentCaptor.getValue(); - Assertions.assertEquals(GeneratedV1.class, classArgumentCaptor.getValue()); - Assertions.assertEquals("blank_1.pdf", requestParameters.getFileName()); - Assertions.assertFalse(requestParameters.getPredictOptions().getAllWords()); - Assertions.assertNotNull(requestParameters.getFile()); - Assertions.assertTrue(requestParameters.getFile().length > 0); - Assertions.assertNull(requestParameters.getFileUrl()); Assertions.assertEquals("someid", jobId); } @@ -323,7 +161,7 @@ void givenAnAsyncGeneratedDoc_whenEnqueuedNoParams_shouldInvokeApiCorrectly() th void givenJsonInput_whenSync_shouldDeserializeCorrectly() throws IOException { File file = new File(getV1ResourcePathString("products/invoices/response_v4/complete.json")); LocalResponse localResponse = new LocalResponse(file); - AsyncPredictResponse predictResponse = client + AsyncPredictResponse predictResponse = new MindeeClient() .loadPrediction(InvoiceV4.class, localResponse); assertStringEqualsFile( predictResponse.getDocumentObj().toString(), @@ -337,7 +175,7 @@ void givenJsonInput_whenAsync_shouldDeserializeCorrectly() throws IOException { getV1ResourcePathString("products/international_id/response_v2/complete.json") ); LocalResponse localResponse = new LocalResponse(file); - AsyncPredictResponse predictResponse = client + AsyncPredictResponse predictResponse = new MindeeClient() .loadPrediction(InternationalIdV2.class, localResponse); assertStringEqualsFile( predictResponse.getDocumentObj().toString(), diff --git a/src/test/java/com/mindee/v1/workflow/WorkflowTest.java b/src/test/java/com/mindee/v1/workflow/WorkflowTest.java index 53110badd..4c228fbb6 100644 --- a/src/test/java/com/mindee/v1/workflow/WorkflowTest.java +++ b/src/test/java/com/mindee/v1/workflow/WorkflowTest.java @@ -2,13 +2,11 @@ import static com.mindee.TestingUtilities.getResourcePath; import static com.mindee.TestingUtilities.getV1ResourcePath; -import static org.mockito.Mockito.when; import com.fasterxml.jackson.databind.ObjectMapper; import com.mindee.input.LocalInputSource; -import com.mindee.pdf.PdfOperation; +import com.mindee.v1.FakeMindeeApiV1; import com.mindee.v1.MindeeClient; -import com.mindee.v1.http.MindeeApiV1; import com.mindee.v1.parsing.common.Execution; import com.mindee.v1.parsing.common.WorkflowResponse; import com.mindee.v1.product.generated.GeneratedV1; @@ -16,67 +14,46 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; -import org.mockito.junit.jupiter.MockitoExtension; -@ExtendWith(MockitoExtension.class) public class WorkflowTest { - MindeeClient client; - @Mock - MindeeClient mockedClient; - MindeeApiV1 mindeeApi; - PdfOperation pdfOperation; private ObjectMapper objectMapper; @BeforeEach public void setUp() { - mindeeApi = Mockito.mock(MindeeApiV1.class); - pdfOperation = Mockito.mock(PdfOperation.class); - client = new MindeeClient(pdfOperation, mindeeApi); - - MockitoAnnotations.openMocks(this); objectMapper = new ObjectMapper(); } @Test void givenAWorkflowMockFileShouldReturnAValidWorkflowObject() throws IOException { - WorkflowResponse workflowResponse = new WorkflowResponse(); - workflowResponse.setExecution(new Execution()); + var workflowResponse = new WorkflowResponse(); + workflowResponse.setExecution(new Execution<>()); workflowResponse.setApiRequest(null); - when(mindeeApi.executeWorkflowPost(Mockito.any(), Mockito.any(), Mockito.any())) - .thenReturn(workflowResponse); - WorkflowResponse execution = client + var mindeeClient = new MindeeClient(new FakeMindeeApiV1<>(workflowResponse)); + + WorkflowResponse response = mindeeClient .executeWorkflow("", new LocalInputSource(getResourcePath("file_types/pdf/blank_1.pdf"))); - Assertions.assertNotNull(execution); - Mockito - .verify(mindeeApi, Mockito.times(1)) - .executeWorkflowPost(Mockito.any(), Mockito.any(), Mockito.any()); + Assertions.assertNotNull(response); } @Test void sendingADocumentToAnExecutionShouldDeserializeResponseCorrectly() throws IOException { - WorkflowResponse.Default mockResponse = objectMapper + var workflowResponse = objectMapper .readValue( getV1ResourcePath("workflows/success.json").toFile(), WorkflowResponse.Default.class ); - - when(mockedClient.executeWorkflow(Mockito.anyString(), Mockito.any(LocalInputSource.class))) - .thenReturn(mockResponse); + var mindeeClient = new MindeeClient(new FakeMindeeApiV1<>(workflowResponse)); String workflowId = "07ebf237-ff27-4eee-b6a2-425df4a5cca6"; LocalInputSource inputSource = new LocalInputSource( getV1ResourcePath("products/financial_document/default_sample.jpg") ); - WorkflowResponse response = mockedClient.executeWorkflow(workflowId, inputSource); + WorkflowResponse response = mindeeClient.executeWorkflow(workflowId, inputSource); Assertions.assertNotNull(response); Assertions.assertNotNull(response.getApiRequest()); @@ -98,27 +75,24 @@ void sendingADocumentToAnExecutionShouldDeserializeResponseCorrectly() throws IO response.getExecution().getUploadedAt().toString() ); Assertions.assertEquals(workflowId, response.getExecution().getWorkflowId()); - - Mockito.verify(mockedClient).executeWorkflow(workflowId, inputSource); } @Test void sendingADocumentToAnExecutionWithPriorityAndAliasShouldDeserializeResponseCorrectly() throws IOException { - WorkflowResponse.Default mockResponse = objectMapper + var workflowResponse = objectMapper .readValue( getV1ResourcePath("workflows/success_low_priority.json").toFile(), WorkflowResponse.Default.class ); - when(mockedClient.executeWorkflow(Mockito.anyString(), Mockito.any(LocalInputSource.class))) - .thenReturn(mockResponse); + var mindeeClient = new MindeeClient(new FakeMindeeApiV1<>(workflowResponse)); String workflowId = "07ebf237-ff27-4eee-b6a2-425df4a5cca6"; LocalInputSource inputSource = new LocalInputSource( getV1ResourcePath("products/financial_document/default_sample.jpg") ); - WorkflowResponse response = mockedClient.executeWorkflow(workflowId, inputSource); + WorkflowResponse response = mindeeClient.executeWorkflow(workflowId, inputSource); Assertions.assertNotNull(response); Assertions.assertNotNull(response.getApiRequest()); @@ -141,8 +115,6 @@ void sendingADocumentToAnExecutionWithPriorityAndAliasShouldDeserializeResponseC response.getExecution().getUploadedAt().toString() ); Assertions.assertEquals(workflowId, response.getExecution().getWorkflowId()); - - Mockito.verify(mockedClient).executeWorkflow(workflowId, inputSource); } } diff --git a/src/test/java/com/mindee/v2/MindeeClientIT.java b/src/test/java/com/mindee/v2/MindeeClientIT.java index e82f3c5fa..d8ca1d942 100644 --- a/src/test/java/com/mindee/v2/MindeeClientIT.java +++ b/src/test/java/com/mindee/v2/MindeeClientIT.java @@ -2,23 +2,17 @@ import static com.mindee.TestingUtilities.getResourcePath; import static com.mindee.TestingUtilities.getV2ResourcePath; -import static com.mindee.TestingUtilities.readFileAsString; import static org.junit.jupiter.api.Assertions.*; import com.mindee.AsyncPollingOptions; import com.mindee.input.LocalInputSource; import com.mindee.input.URLInputSource; import com.mindee.v2.http.MindeeHttpExceptionV2; -import com.mindee.v2.parsing.inference.InferenceActiveOptions; -import com.mindee.v2.parsing.inference.InferenceFile; -import com.mindee.v2.parsing.inference.RawText; -import com.mindee.v2.parsing.inference.field.InferenceFields; -import com.mindee.v2.parsing.inference.field.SimpleField; import com.mindee.v2.product.extraction.ExtractionInference; import com.mindee.v2.product.extraction.ExtractionResponse; -import com.mindee.v2.product.extraction.ExtractionResult; import com.mindee.v2.product.extraction.params.ExtractionParameters; import java.io.IOException; +import java.nio.file.Files; import org.junit.jupiter.api.*; @TestInstance(TestInstance.Lifecycle.PER_CLASS) @@ -39,10 +33,8 @@ void setUp() { @Test @DisplayName("Empty, multi-page PDF – enqueue & parse must succeed") void parseFile_emptyMultiPage_mustSucceed() throws IOException, InterruptedException { - LocalInputSource source = new LocalInputSource( - getResourcePath("file_types/pdf/multipage_cut-2.pdf") - ); - ExtractionParameters params = ExtractionParameters + var source = new LocalInputSource(getResourcePath("file_types/pdf/multipage_cut-2.pdf")); + var params = ExtractionParameters .builder(modelId) .rag(false) .rawText(true) @@ -55,13 +47,13 @@ void parseFile_emptyMultiPage_mustSucceed() throws IOException, InterruptedExcep ) .build(); - ExtractionResponse response = mindeeClient - .enqueueAndGetResult(ExtractionResponse.class, source, params); + var response = mindeeClient.enqueueAndGetResult(ExtractionResponse.class, source, params); assertNotNull(response); - ExtractionInference inference = response.getInference(); + + var inference = response.getInference(); assertNotNull(inference); - InferenceFile file = inference.getFile(); + var file = inference.getFile(); assertNotNull(file); assertEquals("multipage_cut-2.pdf", file.getName()); assertEquals(2, file.getPageCount()); @@ -69,43 +61,44 @@ void parseFile_emptyMultiPage_mustSucceed() throws IOException, InterruptedExcep assertNotNull(inference.getModel()); assertEquals(modelId, inference.getModel().getId()); - InferenceActiveOptions activeOptions = inference.getActiveOptions(); + var activeOptions = inference.getActiveOptions(); assertNotNull(activeOptions); assertFalse(activeOptions.getRag()); assertTrue(activeOptions.getRawText()); assertFalse(activeOptions.getPolygon()); assertFalse(activeOptions.getConfidence()); - ExtractionResult result = inference.getResult(); + var result = inference.getResult(); assertNotNull(result); - RawText rawText = result.getRawText(); + var rawText = result.getRawText(); assertEquals(2, rawText.getPages().size()); - InferenceFields fields = result.getFields(); + var fields = result.getFields(); assertNotNull(fields); } @Test @DisplayName("Filled, single-page image – enqueue & parse must succeed") void parseFile_filledSinglePage_mustSucceed() throws IOException, InterruptedException { - LocalInputSource source = new LocalInputSource( + var source = new LocalInputSource( getV2ResourcePath("products/extraction/financial_document/default_sample.jpg") ); - ExtractionParameters params = ExtractionParameters + var params = ExtractionParameters .builder(modelId) .rag(false) .alias("java-integration-test_single-page") .textContext("this is an invoice") .build(); - ExtractionResponse response = mindeeClient.enqueueAndGetInference(source, params); + var response = mindeeClient.enqueueAndGetResult(ExtractionResponse.class, source, params); assertNotNull(response); - ExtractionInference inference = response.getInference(); + + var inference = response.getInference(); assertNotNull(inference); - InferenceFile file = inference.getFile(); + var file = inference.getFile(); assertNotNull(file); assertEquals("default_sample.jpg", file.getName()); assertEquals(1, file.getPageCount()); @@ -113,23 +106,23 @@ void parseFile_filledSinglePage_mustSucceed() throws IOException, InterruptedExc assertNotNull(inference.getModel()); assertEquals(modelId, inference.getModel().getId()); - InferenceActiveOptions activeOptions = inference.getActiveOptions(); + var activeOptions = inference.getActiveOptions(); assertNotNull(activeOptions); assertFalse(activeOptions.getRag()); assertFalse(activeOptions.getRawText()); assertFalse(activeOptions.getPolygon()); assertFalse(activeOptions.getConfidence()); - ExtractionResult result = inference.getResult(); + var result = inference.getResult(); assertNotNull(result); - RawText rawText = result.getRawText(); + var rawText = result.getRawText(); assertNull(rawText); - InferenceFields fields = result.getFields(); + var fields = result.getFields(); assertNotNull(fields); - SimpleField supplierName = fields.getSimpleField("supplier_name"); + var supplierName = fields.getSimpleField("supplier_name"); assertNotNull(supplierName); assertEquals("John Smith", supplierName.getStringValue()); } @@ -137,35 +130,34 @@ void parseFile_filledSinglePage_mustSucceed() throws IOException, InterruptedExc @Test @DisplayName("Data Schema Replace – enqueue & parse must succeed") void parseFile_dataSchemaReplace_mustSucceed() throws IOException, InterruptedException { - LocalInputSource source = new LocalInputSource( + var source = new LocalInputSource( getV2ResourcePath("products/extraction/financial_document/default_sample.jpg") ); - ExtractionParameters params = ExtractionParameters + var params = ExtractionParameters .builder(modelId) .rag(false) .alias("java-integration-test_data-schema-replace") .dataSchema( - readFileAsString(getV2ResourcePath("products/extraction/data_schema_replace_param.json")) + Files.readString(getV2ResourcePath("products/extraction/data_schema_replace_param.json")) ) .build(); - ExtractionResponse response = mindeeClient - .enqueueAndGetResult(ExtractionResponse.class, source, params); + var response = mindeeClient.enqueueAndGetResult(ExtractionResponse.class, source, params); assertNotNull(response); ExtractionInference inference = response.getInference(); assertNotNull(inference); - ExtractionResult result = inference.getResult(); + var result = inference.getResult(); assertNotNull(result); - RawText rawText = result.getRawText(); + var rawText = result.getRawText(); assertNull(rawText); - InferenceFields fields = result.getFields(); + var fields = result.getFields(); assertNotNull(fields); - SimpleField supplierName = fields.getSimpleField("test_replace"); + var supplierName = fields.getSimpleField("test_replace"); assertNotNull(supplierName); assertEquals("a test value", supplierName.getStringValue()); } @@ -173,56 +165,56 @@ void parseFile_dataSchemaReplace_mustSucceed() throws IOException, InterruptedEx @Test @DisplayName("Invalid model ID – enqueue must raise 422") void invalidModel_mustThrowError() throws IOException { - LocalInputSource source = new LocalInputSource(getResourcePath("file_types/pdf/blank_1.pdf")); - ExtractionParameters params = ExtractionParameters + var source = new LocalInputSource(getResourcePath("file_types/pdf/blank_1.pdf")); + var params = ExtractionParameters .builder("INVALID_MODEL_ID") .textContext("this is invalid") .build(); - MindeeHttpExceptionV2 ex = assertThrows( + MindeeHttpExceptionV2 err = assertThrows( MindeeHttpExceptionV2.class, - () -> mindeeClient.enqueueInference(source, params) + () -> mindeeClient.enqueue(source, params) ); - assertEquals(422, ex.getStatus()); + assertEquals(422, err.getStatus()); } @Test @DisplayName("Invalid webhook ID – enqueue must raise 422") void invalidWebhook_mustThrowError() throws IOException { - LocalInputSource source = new LocalInputSource(getResourcePath("file_types/pdf/blank_1.pdf")); - ExtractionParameters params = ExtractionParameters + var source = new LocalInputSource(getResourcePath("file_types/pdf/blank_1.pdf")); + var params = ExtractionParameters .builder(modelId) .webhookIds(new String[] { "INVALID_WEBHOOK_ID" }) .build(); - MindeeHttpExceptionV2 ex = assertThrows( + MindeeHttpExceptionV2 err = assertThrows( MindeeHttpExceptionV2.class, - () -> mindeeClient.enqueueInference(source, params) + () -> mindeeClient.enqueue(source, params) ); - assertEquals(422, ex.getStatus()); + assertEquals(422, err.getStatus()); } @Test @DisplayName("Invalid job ID – parseQueued must raise an error") void invalidJob_mustThrowError() { - MindeeHttpExceptionV2 ex = assertThrows( + MindeeHttpExceptionV2 err = assertThrows( MindeeHttpExceptionV2.class, - () -> mindeeClient.getInference("INVALID_JOB_ID") + () -> mindeeClient.getResult(ExtractionResponse.class, "INVALID_JOB_ID") ); - assertEquals(422, ex.getStatus()); - assertNotNull(ex); + assertEquals(422, err.getStatus()); + assertNotNull(err); } @Test @DisplayName("URL input source - A URL param should not raise errors.") void urlInputSource_mustNotRaiseErrors() throws IOException, InterruptedException { - URLInputSource urlSource = URLInputSource + var urlSource = URLInputSource .builder(System.getenv("MINDEE_V2_SE_TESTS_BLANK_PDF_URL")) .build(); - ExtractionParameters options = ExtractionParameters.builder(modelId).build(); + var options = ExtractionParameters.builder(modelId).build(); - ExtractionResponse response = mindeeClient.enqueueAndGetInference(urlSource, options); + var response = mindeeClient.enqueueAndGetResult(ExtractionResponse.class, urlSource, options); assertNotNull(response); assertNotNull(response.getInference()); diff --git a/src/test/java/com/mindee/v2/MindeeClientTest.java b/src/test/java/com/mindee/v2/MindeeClientTest.java index 5cf1d2a27..0696fde08 100644 --- a/src/test/java/com/mindee/v2/MindeeClientTest.java +++ b/src/test/java/com/mindee/v2/MindeeClientTest.java @@ -3,33 +3,64 @@ import static com.mindee.TestingUtilities.getResourcePath; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.mockito.ArgumentMatchers.*; -import static org.mockito.Mockito.atMostOnce; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; 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; +import com.mindee.v2.parsing.CommonResponse; import com.mindee.v2.parsing.JobResponse; import com.mindee.v2.product.extraction.ExtractionResponse; import com.mindee.v2.product.extraction.params.ExtractionParameters; import java.io.IOException; -import org.apache.commons.io.FileUtils; +import java.nio.file.Files; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; @DisplayName("MindeeV2 – Client and API Tests") class MindeeClientTest { - /** - * Creates a fully mocked MindeeClientV2. - */ - private static MindeeClient makeClientWithMockedApi(MindeeApiV2 mockedApi) { - return new MindeeClient(mockedApi); + private static class FakeMindeeApiV2 extends MindeeApiV2 { + private final JobResponse jobResponse; + private final CommonResponse resultResponse; + + public FakeMindeeApiV2(JobResponse jobResponse, CommonResponse resultResponse) { + super(); + this.jobResponse = jobResponse; + this.resultResponse = resultResponse; + } + + @Override + public JobResponse reqPostEnqueue( + LocalInputSource inputSource, + BaseParameters options + ) throws IOException { + return jobResponse; + } + + @Override + public JobResponse reqPostEnqueue( + URLInputSource inputSource, + BaseParameters options + ) throws IOException { + return jobResponse; + } + + @Override + public JobResponse reqGetJob(String jobId) { + return jobResponse; + } + + @Override + public TResponse reqGetResult( + Class tResponseClass, + String inferenceId + ) { + return (TResponse) resultResponse; + } } @Nested @@ -38,22 +69,15 @@ class Enqueue { @Test @DisplayName("sends exactly one HTTP call and yields a non-null response") void enqueue_post_async() throws IOException { - MindeeApiV2 predictable = Mockito.mock(MindeeApiV2.class); - when(predictable.reqPostEnqueue(any(LocalInputSource.class), any(ExtractionParameters.class))) - .thenReturn(new JobResponse()); - - MindeeClient mindeeClient = makeClientWithMockedApi(predictable); + var mindeeClient = new MindeeClient(new FakeMindeeApiV2(new JobResponse(), null)); - LocalInputSource input = new LocalInputSource(getResourcePath("file_types/pdf/blank_1.pdf")); + var input = new LocalInputSource(getResourcePath("file_types/pdf/blank_1.pdf")); JobResponse response = mindeeClient - .enqueueInference( + .enqueue( input, ExtractionParameters.builder("dummy-model-id").textContext("test text context").build() ); - assertNotNull(response, "enqueue() must return a response"); - verify(predictable, atMostOnce()) - .reqPostEnqueue(any(LocalInputSource.class), any(ExtractionParameters.class)); } } @@ -63,20 +87,16 @@ class GetJob { @Test @DisplayName("hits the HTTP endpoint once and returns a non-null response") void document_getJob_async() throws JsonProcessingException { - MindeeApiV2 predictable = Mockito.mock(MindeeApiV2.class); String json = "{\"job\": {\"id\": \"dummy-id\", \"status\": \"Processing\"}}"; - ObjectMapper mapper = new ObjectMapper(); + var mapper = new ObjectMapper(); mapper.findAndRegisterModules(); JobResponse processing = mapper.readValue(json, JobResponse.class); - when(predictable.reqGetJob(anyString())).thenReturn(processing); - - MindeeClient mindeeClient = makeClientWithMockedApi(predictable); + MindeeClient mindeeClient = new MindeeClient(new FakeMindeeApiV2(processing, null)); JobResponse response = mindeeClient.getJob("dummy-id"); assertNotNull(response, "getJob() must return a response"); - verify(predictable, atMostOnce()).reqGetJob(anyString()); } } @@ -85,26 +105,18 @@ void document_getJob_async() throws JsonProcessingException { class GetExtractionInference { @Test @DisplayName("hits the HTTP endpoint once and returns a non-null response") - void document_getInference_async() throws IOException { - MindeeApiV2 predictable = Mockito.mock(MindeeApiV2.class); + void document_getResult_async() throws IOException { + String json = Files + .readString(getResourcePath("v2/products/extraction/financial_document/complete.json")); - String json = FileUtils - .readFileToString( - getResourcePath("v2/products/extraction/financial_document/complete.json").toFile() - ); - - ObjectMapper mapper = new ObjectMapper(); + var mapper = new ObjectMapper(); mapper.findAndRegisterModules(); ExtractionResponse processing = mapper.readValue(json, ExtractionResponse.class); - - when(predictable.reqGetResult(eq(ExtractionResponse.class), anyString())) - .thenReturn(processing); - - MindeeClient mindeeClient = makeClientWithMockedApi(predictable); + MindeeClient mindeeClient = new MindeeClient(new FakeMindeeApiV2(null, processing)); ExtractionResponse response = mindeeClient - .getInference("12345678-1234-1234-1234-123456789abc"); + .getResult(ExtractionResponse.class, "12345678-1234-1234-1234-123456789abc"); assertNotNull(response, "getInference() must return a response"); assertEquals( 21, @@ -122,7 +134,6 @@ void document_getInference_async() throws IOException { .getValue(), "Result must deserialize fields properly." ); - verify(predictable, atMostOnce()).reqGetResult(eq(ExtractionResponse.class), anyString()); } } @@ -133,7 +144,7 @@ class DeserializeResponse { @Test @DisplayName("parses local JSON and exposes correct field values") void inference_loadsLocally() throws IOException { - LocalResponse localResponse = new LocalResponse( + var localResponse = new LocalResponse( getResourcePath("v2/products/extraction/financial_document/complete.json") ); ExtractionResponse loaded = localResponse.deserializeResponse(ExtractionResponse.class); diff --git a/src/test/java/com/mindee/v2/MindeeSettingsTest.java b/src/test/java/com/mindee/v2/MindeeSettingsTest.java index 797c5692e..ec4372efb 100644 --- a/src/test/java/com/mindee/v2/MindeeSettingsTest.java +++ b/src/test/java/com/mindee/v2/MindeeSettingsTest.java @@ -10,7 +10,7 @@ public class MindeeSettingsTest { @SetEnvironmentVariable(key = "MINDEE_V2_API_KEY", value = "abcd") @SetEnvironmentVariable(key = "MINDEE_V2_API_URL", value = "https://example.com") void setEnvironmentVariablesAndEmptyParams() { - MindeeSettings settings = new MindeeSettings("", ""); + var settings = new MindeeSettings("", ""); Assertions.assertEquals("abcd", settings.getApiKey().orElse("")); Assertions.assertEquals("https://example.com", settings.getBaseUrl()); } diff --git a/src/test/java/com/mindee/v2/parsing/ExtractionTest.java b/src/test/java/com/mindee/v2/parsing/ExtractionTest.java index 1fabb85d2..9927d20aa 100644 --- a/src/test/java/com/mindee/v2/parsing/ExtractionTest.java +++ b/src/test/java/com/mindee/v2/parsing/ExtractionTest.java @@ -1,7 +1,6 @@ package com.mindee.v2.parsing; import static com.mindee.TestingUtilities.getV2ResourcePath; -import static com.mindee.TestingUtilities.readFileAsString; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertInstanceOf; @@ -32,6 +31,7 @@ import com.mindee.v2.product.extraction.ExtractionResponse; import java.io.IOException; import java.math.BigDecimal; +import java.nio.file.Files; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; @@ -44,7 +44,7 @@ class ExtractionTest { private ExtractionResponse loadResponse(String filePath) throws IOException { - LocalResponse localResponse = new LocalResponse(getV2ResourcePath(filePath)); + var localResponse = new LocalResponse(getV2ResourcePath(filePath)); return localResponse.deserializeResponse(ExtractionResponse.class); } @@ -55,10 +55,8 @@ class BlankPredictionTest { @Test @DisplayName("all properties must be valid") void asyncPredict_whenEmpty_mustHaveValidProperties() throws IOException { - ExtractionResponse response = loadResponse( - "products/extraction/financial_document/blank.json" - ); - InferenceFields fields = response.getInference().getResult().getFields(); + var response = loadResponse("products/extraction/financial_document/blank.json"); + var fields = response.getInference().getResult().getFields(); assertEquals(21, fields.size(), "Expected 21 fields"); @@ -109,9 +107,7 @@ class CompletePredictionTest { @Test @DisplayName("every exposed property must be valid and consistent") void asyncPredict_whenComplete_mustExposeAllProperties() throws IOException { - ExtractionResponse response = loadResponse( - "products/extraction/financial_document/complete.json" - ); + var response = loadResponse("products/extraction/financial_document/complete.json"); ExtractionInference inference = response.getInference(); assertNotNull(inference); assertEquals("12345678-1234-1234-1234-123456789abc", inference.getId()); @@ -130,7 +126,7 @@ void asyncPredict_whenComplete_mustExposeAllProperties() throws IOException { assertEquals("image/jpeg", file.getMimeType()); assertNull(file.getAlias()); - InferenceFields fields = inference.getResult().getFields(); + var fields = inference.getResult().getFields(); assertEquals(21, fields.size()); SimpleField date = fields.get("date").getSimpleField(); @@ -188,7 +184,7 @@ void deepNestedFields_mustExposeCorrectTypes() throws IOException { ExtractionInference inf = resp.getInference(); assertNotNull(inf); - InferenceFields root = inf.getResult().getFields(); + var root = inf.getResult().getFields(); assertNotNull(root.get("field_simple").getSimpleField()); assertNotNull(root.get("field_object").getObjectField()); @@ -250,20 +246,20 @@ private void testSimpleFieldString(SimpleField field) { @Test @DisplayName("simple fields must be recognised") void standardFieldTypes_mustExposeSimpleFieldValues() throws IOException { - ExtractionResponse response = loadResponse("products/extraction/standard_field_types.json"); + var response = loadResponse("products/extraction/standard_field_types.json"); ExtractionInference inference = response.getInference(); assertNotNull(inference); - InferenceFields fields = inference.getResult().getFields(); + var fields = inference.getResult().getFields(); assertNotNull(fields.get("field_simple_string").getSimpleField()); - SimpleField fieldSimpleString = fields.getSimpleField("field_simple_string"); + var fieldSimpleString = fields.getSimpleField("field_simple_string"); testSimpleFieldString(fieldSimpleString); assertEquals(FieldConfidence.Certain, fieldSimpleString.getConfidence()); assertEquals(1, fieldSimpleString.getLocations().size()); - SimpleField fieldSimpleFloat = fields.get("field_simple_float").getSimpleField(); + var fieldSimpleFloat = fields.get("field_simple_float").getSimpleField(); assertNotNull(fieldSimpleFloat); assertInstanceOf(Double.class, fieldSimpleFloat.getValue()); assertEquals(fieldSimpleFloat.getValue(), fieldSimpleFloat.getDoubleValue()); @@ -271,14 +267,14 @@ void standardFieldTypes_mustExposeSimpleFieldValues() throws IOException { assertThrows(ClassCastException.class, fieldSimpleFloat::getBooleanValue); assertEquals(FieldConfidence.High, fieldSimpleFloat.getConfidence()); - SimpleField fieldSimpleInt = fields.get("field_simple_int").getSimpleField(); + var fieldSimpleInt = fields.get("field_simple_int").getSimpleField(); assertNotNull(fieldSimpleInt); assertInstanceOf(Double.class, fieldSimpleInt.getValue()); assertEquals(fieldSimpleInt.getValue(), fieldSimpleInt.getDoubleValue()); assertEquals(FieldConfidence.Medium, fieldSimpleInt.getConfidence()); assertThrows(ClassCastException.class, fieldSimpleInt::getStringValue); - SimpleField fieldSimpleZero = fields.get("field_simple_zero").getSimpleField(); + var fieldSimpleZero = fields.get("field_simple_zero").getSimpleField(); assertNotNull(fieldSimpleZero); assertEquals(FieldConfidence.Low, fieldSimpleZero.getConfidence()); assertInstanceOf(Double.class, fieldSimpleZero.getValue()); @@ -287,7 +283,7 @@ void standardFieldTypes_mustExposeSimpleFieldValues() throws IOException { assertThrows(ClassCastException.class, fieldSimpleZero::getStringValue); assertThrows(ClassCastException.class, fieldSimpleZero::getBooleanValue); - SimpleField fieldSimpleBool = fields.get("field_simple_bool").getSimpleField(); + var fieldSimpleBool = fields.get("field_simple_bool").getSimpleField(); assertNotNull(fieldSimpleBool); assertInstanceOf(Boolean.class, fieldSimpleBool.getValue()); assertEquals(fieldSimpleBool.getValue(), fieldSimpleBool.getBooleanValue()); @@ -295,7 +291,7 @@ void standardFieldTypes_mustExposeSimpleFieldValues() throws IOException { assertThrows(ClassCastException.class, fieldSimpleBool::getDoubleValue); assertThrows(ClassCastException.class, fieldSimpleBool::getBigDecimalValue); - SimpleField fieldSimpleNull = fields.get("field_simple_null").getSimpleField(); + var fieldSimpleNull = fields.get("field_simple_null").getSimpleField(); assertNotNull(fieldSimpleNull); assertNull(fieldSimpleNull.getValue()); assertNull(fieldSimpleNull.getStringValue()); @@ -307,13 +303,13 @@ void standardFieldTypes_mustExposeSimpleFieldValues() throws IOException { @Test @DisplayName("simple list fields must be recognised") void standardFieldTypes_mustExposeSimpleListFieldValues() throws IOException { - ExtractionResponse response = loadResponse("products/extraction/standard_field_types.json"); - ExtractionInference inference = response.getInference(); + var response = loadResponse("products/extraction/standard_field_types.json"); + var inference = response.getInference(); assertNotNull(inference); - InferenceFields fields = inference.getResult().getFields(); + var fields = inference.getResult().getFields(); - ListField listField = fields.get("field_simple_list").getListField(); + var listField = fields.get("field_simple_list").getListField(); assertNotNull(listField); // Low level (dynamic) access @@ -350,11 +346,11 @@ private void testObjectSubFieldSimpleString(String fieldName, SimpleField subFie @Test @DisplayName("object list fields must be recognised") void standardFieldTypes_mustExposeObjectListFieldValues() throws IOException { - ExtractionResponse response = loadResponse("products/extraction/standard_field_types.json"); - ExtractionInference inference = response.getInference(); + var response = loadResponse("products/extraction/standard_field_types.json"); + var inference = response.getInference(); assertNotNull(inference); - InferenceFields fields = inference.getResult().getFields(); + var fields = inference.getResult().getFields(); ListField listField = fields.get("field_object_list").getListField(); assertNotNull(listField); @@ -400,11 +396,11 @@ void standardFieldTypes_mustExposeObjectListFieldValues() throws IOException { @Test @DisplayName("simple / object / list variants must be recognised") void standardFieldTypes_mustExposeObjectFieldValues() throws IOException { - ExtractionResponse response = loadResponse("products/extraction/standard_field_types.json"); - ExtractionInference inference = response.getInference(); + var response = loadResponse("products/extraction/standard_field_types.json"); + var inference = response.getInference(); assertNotNull(inference); - InferenceFields fields = inference.getResult().getFields(); + var fields = inference.getResult().getFields(); ObjectField fieldObject = fields.get("field_object").getObjectField(); assertNotNull(fieldObject); @@ -435,10 +431,10 @@ void standardFieldTypes_mustExposeObjectFieldValues() throws IOException { @Test @DisplayName("allow getting fields using generics") void standardFieldTypes_getWithGenerics() throws IOException { - ExtractionResponse response = loadResponse("products/extraction/standard_field_types.json"); - ExtractionInference inference = response.getInference(); + var response = loadResponse("products/extraction/standard_field_types.json"); + var inference = response.getInference(); assertNotNull(inference); - InferenceFields fields = inference.getResult().getFields(); + var fields = inference.getResult().getFields(); assertEquals( fields.get("field_simple_bool").getSimpleField(), @@ -471,11 +467,11 @@ void standardFieldTypes_getWithGenerics() throws IOException { @Test @DisplayName("confidence and locations must be usable") void standardFieldTypes_confidenceAndLocations() throws IOException { - ExtractionResponse response = loadResponse("products/extraction/standard_field_types.json"); - ExtractionInference inference = response.getInference(); + var response = loadResponse("products/extraction/standard_field_types.json"); + var inference = response.getInference(); assertNotNull(inference); - InferenceFields fields = inference.getResult().getFields(); + var fields = inference.getResult().getFields(); SimpleField fieldSimpleString = fields.get("field_simple_string").getField(SimpleField.class); FieldConfidence confidence = fieldSimpleString.getConfidence(); @@ -511,8 +507,8 @@ class RawTextTest { @Test @DisplayName("raw texts option must be parsed and exposed") void rawTexts_mustBeAccessible() throws IOException { - ExtractionResponse response = loadResponse("products/extraction/raw_texts.json"); - ExtractionInference inference = response.getInference(); + var response = loadResponse("products/extraction/raw_texts.json"); + var inference = response.getInference(); assertNotNull(inference); InferenceActiveOptions activeOptions = inference.getActiveOptions(); @@ -546,8 +542,8 @@ class RagMetadataTest { @Test @DisplayName("RAG metadata when matched") void rag_mustBeFilled_whenMatched() throws IOException { - ExtractionResponse response = loadResponse("products/extraction/rag_matched.json"); - ExtractionInference inference = response.getInference(); + var response = loadResponse("products/extraction/rag_matched.json"); + var inference = response.getInference(); assertNotNull(inference); RagMetadata rag = inference.getResult().getRag(); @@ -558,8 +554,8 @@ void rag_mustBeFilled_whenMatched() throws IOException { @Test @DisplayName("RAG metadata when not matched") void rag_mustBeNull_whenNotMatched() throws IOException { - ExtractionResponse response = loadResponse("products/extraction/rag_not_matched.json"); - ExtractionInference inference = response.getInference(); + var response = loadResponse("products/extraction/rag_not_matched.json"); + var inference = response.getInference(); assertNotNull(inference); RagMetadata rag = inference.getResult().getRag(); @@ -574,10 +570,9 @@ class RstDisplay { @Test @DisplayName("rst display must be parsed and exposed") void rstDisplay_mustBeAccessible() throws IOException { - ExtractionResponse resp = loadResponse("products/extraction/standard_field_types.json"); - String rstRef = readFileAsString( - getV2ResourcePath("products/extraction/standard_field_types.rst") - ); + var resp = loadResponse("products/extraction/standard_field_types.json"); + String rstRef = Files + .readString(getV2ResourcePath("products/extraction/standard_field_types.rst")); ExtractionInference inference = resp.getInference(); assertNotNull(inference); assertEquals(rstRef, resp.getInference().toString()); @@ -590,7 +585,7 @@ class TextContextTest { @Test @DisplayName("should be present and true when enabled") void textContext_mustBePresentAndTrue() throws IOException { - ExtractionResponse resp = loadResponse("products/extraction/text_context_enabled.json"); + var resp = loadResponse("products/extraction/text_context_enabled.json"); ExtractionInference inference = resp.getInference(); assertNotNull(inference); assertTrue(inference.getActiveOptions().getTextContext()); @@ -603,10 +598,10 @@ class DataSchemaTest { @Test @DisplayName("should be present and true when enabled") void textContext_mustBePresentAndTrue() throws IOException { - ExtractionResponse resp = loadResponse("products/extraction/data_schema_replace.json"); + var resp = loadResponse("products/extraction/data_schema_replace.json"); ExtractionInference inference = resp.getInference(); assertNotNull(inference); - InferenceFields fields = inference.getResult().getFields(); + var fields = inference.getResult().getFields(); assertEquals("a test value", fields.get("test_replace").getSimpleField().getStringValue()); assertTrue(inference.getActiveOptions().getDataSchema().getReplace()); diff --git a/src/test/java/com/mindee/v2/parsing/JobTest.java b/src/test/java/com/mindee/v2/parsing/JobTest.java index e4d2f3c8b..87f27fd26 100644 --- a/src/test/java/com/mindee/v2/parsing/JobTest.java +++ b/src/test/java/com/mindee/v2/parsing/JobTest.java @@ -14,7 +14,7 @@ @DisplayName("MindeeV2 - Job Tests") public class JobTest { private JobResponse loadJob(String filePath) throws IOException { - LocalResponse localResponse = new LocalResponse(getV2ResourcePath(filePath)); + var localResponse = new LocalResponse(getV2ResourcePath(filePath)); return localResponse.deserializeResponse(JobResponse.class); } @@ -24,8 +24,8 @@ class ProcessingTest { @Test @DisplayName("properties must be valid") void whenProcessing_mustHaveValidProperties() throws IOException { - JobResponse response = loadJob("job/ok_processing.json"); - Job job = response.getJob(); + var response = loadJob("job/ok_processing.json"); + var job = response.getJob(); assertNotNull(job); assertEquals("Processing", job.getStatus()); assertNotNull(job.getCreatedAt()); @@ -41,8 +41,8 @@ class ProcessedTest { @Test @DisplayName("properties must be valid") void whenProcessing_mustHaveValidProperties() throws IOException { - JobResponse response = loadJob("job/ok_processed_webhooks_ok.json"); - Job job = response.getJob(); + var response = loadJob("job/ok_processed_webhooks_ok.json"); + var job = response.getJob(); assertNotNull(job); assertEquals("Processed", job.getStatus()); assertNotNull(job.getCreatedAt()); @@ -58,8 +58,8 @@ class FailTest { @Test @DisplayName("HTTP 422 properties must be valid") void when422_mustHaveValidProperties() throws IOException { - JobResponse response = loadJob("job/fail_422.json"); - Job job = response.getJob(); + var response = loadJob("job/fail_422.json"); + var job = response.getJob(); assertNotNull(job); assertNotNull(job.getCreatedAt()); assertNotNull(job.getCompletedAt()); diff --git a/src/test/java/com/mindee/v2/product/ClassificationTest.java b/src/test/java/com/mindee/v2/product/ClassificationTest.java index d73f029bb..d7701c82c 100644 --- a/src/test/java/com/mindee/v2/product/ClassificationTest.java +++ b/src/test/java/com/mindee/v2/product/ClassificationTest.java @@ -14,7 +14,7 @@ @DisplayName("MindeeV2 - Classification Model Tests") public class ClassificationTest { private ClassificationResponse loadResponse(String filePath) throws IOException { - LocalResponse localResponse = new LocalResponse(getV2ResourcePath(filePath)); + var localResponse = new LocalResponse(getV2ResourcePath(filePath)); return localResponse.deserializeResponse(ClassificationResponse.class); } diff --git a/src/test/java/com/mindee/v2/product/CropIT.java b/src/test/java/com/mindee/v2/product/CropIT.java index 9aa3abff3..336dabe22 100644 --- a/src/test/java/com/mindee/v2/product/CropIT.java +++ b/src/test/java/com/mindee/v2/product/CropIT.java @@ -6,10 +6,7 @@ import com.mindee.AsyncPollingOptions; import com.mindee.input.LocalInputSource; import com.mindee.v2.MindeeClient; -import com.mindee.v2.parsing.inference.InferenceFile; -import com.mindee.v2.product.crop.CropInference; import com.mindee.v2.product.crop.CropResponse; -import com.mindee.v2.product.crop.CropResult; import com.mindee.v2.product.crop.params.CropParameters; import java.io.IOException; import org.junit.jupiter.api.BeforeAll; @@ -28,7 +25,7 @@ class CropIT { @BeforeAll void setUp() { - String apiKey = System.getenv("MINDEE_V2_API_KEY"); + var apiKey = System.getenv("MINDEE_V2_API_KEY"); modelId = System.getenv("MINDEE_V2_SE_TESTS_CROP_MODEL_ID"); mindeeClient = new MindeeClient(apiKey); } @@ -36,9 +33,7 @@ void setUp() { @Test @DisplayName("Empty, multi-page PDF – enqueue & parse must succeed") void parseFile_emptyMultiPage_mustSucceed() throws IOException, InterruptedException { - LocalInputSource source = new LocalInputSource( - getResourcePath("file_types/pdf/multipage_cut-2.pdf") - ); + var source = new LocalInputSource(getResourcePath("file_types/pdf/multipage_cut-2.pdf")); CropParameters params = CropParameters .builder(modelId) .alias("java_integration-test_crop_multipage") @@ -49,10 +44,11 @@ void parseFile_emptyMultiPage_mustSucceed() throws IOException, InterruptedExcep CropResponse response = mindeeClient.enqueueAndGetResult(CropResponse.class, source, params); assertNotNull(response); - CropInference inference = response.getInference(); + + var inference = response.getInference(); assertNotNull(inference); - InferenceFile file = inference.getFile(); + var file = inference.getFile(); assertNotNull(file); assertEquals("multipage_cut-2.pdf", file.getName()); assertEquals(2, file.getPageCount()); @@ -60,7 +56,7 @@ void parseFile_emptyMultiPage_mustSucceed() throws IOException, InterruptedExcep assertNotNull(inference.getModel()); assertEquals(modelId, inference.getModel().getId()); - CropResult result = inference.getResult(); + var result = inference.getResult(); assertNotNull(result); assertEquals(1, result.getCrops().size()); assertEquals("other", result.getCrops().get(0).getObjectType()); diff --git a/src/test/java/com/mindee/v2/product/CropTest.java b/src/test/java/com/mindee/v2/product/CropTest.java index f0df46732..b171a1653 100644 --- a/src/test/java/com/mindee/v2/product/CropTest.java +++ b/src/test/java/com/mindee/v2/product/CropTest.java @@ -6,10 +6,8 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import com.mindee.input.LocalResponse; -import com.mindee.v2.product.crop.CropItem; import com.mindee.v2.product.crop.CropResponse; import java.io.IOException; -import java.util.ArrayList; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; @@ -17,7 +15,7 @@ @DisplayName("MindeeV2 - Crop Model Tests") public class CropTest { private CropResponse loadResponse(String filePath) throws IOException { - LocalResponse localResponse = new LocalResponse(getV2ResourcePath(filePath)); + var localResponse = new LocalResponse(getV2ResourcePath(filePath)); return localResponse.deserializeResponse(CropResponse.class); } @@ -27,13 +25,13 @@ class SinglePredictionTest { @Test @DisplayName("all properties must be valid") void mustHaveValidProperties() throws IOException { - CropResponse response = loadResponse("products/crop/crop_single.json"); + var response = loadResponse("products/crop/crop_single.json"); assertNotNull(response.getInference()); - ArrayList crops = response.getInference().getResult().getCrops(); + var crops = response.getInference().getResult().getCrops(); assertEquals(1, crops.size()); - CropItem crop1 = crops.get(0); + var crop1 = crops.get(0); assertEquals("invoice", crop1.getObjectType()); assertNotNull(crop1.getLocation().getPolygon()); assertEquals(0, crop1.getLocation().getPage()); @@ -42,7 +40,7 @@ void mustHaveValidProperties() throws IOException { @Test @DisplayName("RST output must be valid") void mustHaveValidDisplay() throws IOException { - CropResponse response = loadResponse("products/crop/crop_single.json"); + var response = loadResponse("products/crop/crop_single.json"); assertStringEqualsFile( response.getInference().toString(), getV2ResourcePath("products/crop/crop_single.rst") @@ -56,18 +54,18 @@ class MultiPredictionTest { @Test @DisplayName("all properties must be valid") void mustHaveValidProperties() throws IOException { - CropResponse response = loadResponse("products/crop/crop_multiple.json"); + var response = loadResponse("products/crop/crop_multiple.json"); assertNotNull(response.getInference()); - ArrayList crops = response.getInference().getResult().getCrops(); + var crops = response.getInference().getResult().getCrops(); assertEquals(2, crops.size()); - CropItem crop1 = crops.get(0); + var crop1 = crops.get(0); assertEquals("invoice", crop1.getObjectType()); assertNotNull(crop1.getLocation().getPolygon()); assertEquals(0, crop1.getLocation().getPage()); - CropItem crop2 = crops.get(1); + var crop2 = crops.get(1); assertEquals("receipt", crop2.getObjectType()); assertNotNull(crop2.getLocation().getPolygon()); assertEquals(0, crop2.getLocation().getPage()); @@ -76,7 +74,7 @@ void mustHaveValidProperties() throws IOException { @Test @DisplayName("RST output must be valid") void mustHaveValidDisplay() throws IOException { - CropResponse response = loadResponse("products/crop/crop_multiple.json"); + var response = loadResponse("products/crop/crop_multiple.json"); assertStringEqualsFile( response.getInference().toString(), getV2ResourcePath("products/crop/crop_multiple.rst") diff --git a/src/test/java/com/mindee/v2/product/OcrTest.java b/src/test/java/com/mindee/v2/product/OcrTest.java index c39268be0..ef734eec0 100644 --- a/src/test/java/com/mindee/v2/product/OcrTest.java +++ b/src/test/java/com/mindee/v2/product/OcrTest.java @@ -5,10 +5,8 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import com.mindee.input.LocalResponse; -import com.mindee.v2.product.ocr.OcrPage; import com.mindee.v2.product.ocr.OcrResponse; import java.io.IOException; -import java.util.ArrayList; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; @@ -16,7 +14,7 @@ @DisplayName("MindeeV2 - OCR Model Tests") public class OcrTest { private OcrResponse loadResponse(String filePath) throws IOException { - LocalResponse localResponse = new LocalResponse(getV2ResourcePath(filePath)); + var localResponse = new LocalResponse(getV2ResourcePath(filePath)); return localResponse.deserializeResponse(OcrResponse.class); } @@ -26,10 +24,10 @@ class SinglePredictionTest { @Test @DisplayName("all properties must be valid") void mustHaveValidProperties() throws IOException { - OcrResponse response = loadResponse("products/ocr/ocr_single.json"); + var response = loadResponse("products/ocr/ocr_single.json"); assertNotNull(response.getInference()); - ArrayList pages = response.getInference().getResult().getPages(); + var pages = response.getInference().getResult().getPages(); assertEquals(1, pages.size()); assertEquals(305, pages.get(0).getWords().size()); assertEquals("Shipper:", pages.get(0).getWords().get(0).getContent()); @@ -42,18 +40,18 @@ class MultiPredictionTest { @Test @DisplayName("all properties must be valid") void mustHaveValidProperties() throws IOException { - OcrResponse response = loadResponse("products/ocr/ocr_multiple.json"); + var response = loadResponse("products/ocr/ocr_multiple.json"); assertNotNull(response.getInference()); - ArrayList pages = response.getInference().getResult().getPages(); + var pages = response.getInference().getResult().getPages(); assertEquals(3, pages.size()); - OcrPage page1 = pages.get(0); + var page1 = pages.get(0); assertNotNull(page1.getContent()); assertEquals(295, page1.getWords().size()); assertEquals("FICTIOCORP", page1.getWords().get(0).getContent()); - OcrPage page2 = pages.get(1); + var page2 = pages.get(1); assertNotNull(page2.getContent()); assertEquals(450, page2.getWords().size()); assertEquals("KEOLIO", page2.getWords().get(0).getContent()); diff --git a/src/test/java/com/mindee/v2/product/SplitIT.java b/src/test/java/com/mindee/v2/product/SplitIT.java index 8c536b528..6419c0bac 100644 --- a/src/test/java/com/mindee/v2/product/SplitIT.java +++ b/src/test/java/com/mindee/v2/product/SplitIT.java @@ -8,10 +8,7 @@ import com.mindee.AsyncPollingOptions; import com.mindee.input.LocalInputSource; import com.mindee.v2.MindeeClient; -import com.mindee.v2.parsing.inference.InferenceFile; -import com.mindee.v2.product.split.SplitInference; import com.mindee.v2.product.split.SplitResponse; -import com.mindee.v2.product.split.SplitResult; import com.mindee.v2.product.split.params.SplitParameters; import java.io.IOException; import org.junit.jupiter.api.BeforeAll; @@ -30,7 +27,7 @@ class SplitIT { @BeforeAll void setUp() { - String apiKey = System.getenv("MINDEE_V2_API_KEY"); + var apiKey = System.getenv("MINDEE_V2_API_KEY"); modelId = System.getenv("MINDEE_V2_SE_TESTS_SPLIT_MODEL_ID"); mindeeClient = new MindeeClient(apiKey); } @@ -38,10 +35,8 @@ void setUp() { @Test @DisplayName("Empty, multi-page PDF – enqueue & parse must succeed") void parseFile_emptyMultiPage_mustSucceed() throws IOException, InterruptedException { - LocalInputSource source = new LocalInputSource( - getResourcePath("file_types/pdf/multipage_cut-2.pdf") - ); - SplitParameters params = SplitParameters + var source = new LocalInputSource(getResourcePath("file_types/pdf/multipage_cut-2.pdf")); + var params = SplitParameters .builder(modelId) .alias("java_integration-test_split_multipage") .pollingOptions( @@ -49,12 +44,13 @@ void parseFile_emptyMultiPage_mustSucceed() throws IOException, InterruptedExcep ) .build(); - SplitResponse response = mindeeClient.enqueueAndGetResult(SplitResponse.class, source, params); + var response = mindeeClient.enqueueAndGetResult(SplitResponse.class, source, params); assertNotNull(response); - SplitInference inference = response.getInference(); + + var inference = response.getInference(); assertNotNull(inference); - InferenceFile file = inference.getFile(); + var file = inference.getFile(); assertNotNull(file); assertEquals("multipage_cut-2.pdf", file.getName()); assertEquals(2, file.getPageCount()); @@ -62,7 +58,7 @@ void parseFile_emptyMultiPage_mustSucceed() throws IOException, InterruptedExcep assertNotNull(inference.getModel()); assertEquals(modelId, inference.getModel().getId()); - SplitResult result = inference.getResult(); + var result = inference.getResult(); assertNotNull(result); assertTrue(result.getSplits().isEmpty()); } diff --git a/src/test/java/com/mindee/v2/product/SplitTest.java b/src/test/java/com/mindee/v2/product/SplitTest.java index 08a21bef8..d4ff870d1 100644 --- a/src/test/java/com/mindee/v2/product/SplitTest.java +++ b/src/test/java/com/mindee/v2/product/SplitTest.java @@ -5,10 +5,8 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import com.mindee.input.LocalResponse; -import com.mindee.v2.product.split.SplitRange; import com.mindee.v2.product.split.SplitResponse; import java.io.IOException; -import java.util.ArrayList; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; @@ -16,7 +14,7 @@ @DisplayName("MindeeV2 - Split Model Tests") public class SplitTest { private SplitResponse loadResponse(String filePath) throws IOException { - LocalResponse localResponse = new LocalResponse(getV2ResourcePath(filePath)); + var localResponse = new LocalResponse(getV2ResourcePath(filePath)); return localResponse.deserializeResponse(SplitResponse.class); } @@ -26,10 +24,10 @@ class SinglePredictionTest { @Test @DisplayName("all properties must be valid") void mustHaveValidProperties() throws IOException { - SplitResponse response = loadResponse("products/split/split_single.json"); + var response = loadResponse("products/split/split_single.json"); assertNotNull(response.getInference()); - ArrayList splits = response.getInference().getResult().getSplits(); + var splits = response.getInference().getResult().getSplits(); assertEquals(1, splits.size()); assertEquals("receipt", splits.get(0).getDocumentType()); assertEquals(0, splits.get(0).getPageRange().get(0)); @@ -42,21 +40,21 @@ class MultiPredictionTest { @Test @DisplayName("all properties must be valid") void mustHaveValidProperties() throws IOException { - SplitResponse response = loadResponse("products/split/split_multiple.json"); + var response = loadResponse("products/split/split_multiple.json"); assertNotNull(response.getInference()); - ArrayList splits = response.getInference().getResult().getSplits(); + var splits = response.getInference().getResult().getSplits(); assertEquals(3, splits.size()); - SplitRange split1 = splits.get(0); + var split1 = splits.get(0); assertEquals("passport", split1.getDocumentType()); assertEquals(0, split1.getPageRange().get(0)); - SplitRange split2 = splits.get(1); + var split2 = splits.get(1); assertEquals("invoice", split2.getDocumentType()); assertEquals(1, split2.getPageRange().get(0)); - SplitRange split3 = splits.get(2); + var split3 = splits.get(2); assertEquals("receipt", split3.getDocumentType()); assertEquals(4, split3.getPageRange().get(0)); }