Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Quickly and easily connect to Mindee's API services using Java.
This client library has support for both Mindee platform versions.

### V2 - Latest
This is the new platform located here:
This is the latest platform located here:

https://app.mindee.com

Expand All @@ -19,7 +19,7 @@ Consult the


### V1
This is the legacy platform located here:
This is the platform located here:

https://platform.mindee.com/

Expand Down
14 changes: 13 additions & 1 deletion src/main/java/com/mindee/extraction/ExtractedImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,19 @@ public ExtractedImage(BufferedImage image, String filename, String saveFormat) {
*/
public void writeToFile(String outputPath) throws IOException {
Path imagePath = Paths.get(outputPath, this.filename);
File outputfile = new File(imagePath.toString());
File outputfile = imagePath.toFile();
ImageIO.write(this.image, this.saveFormat, outputfile);
}

/**
* Write the image to a file.
* Uses the default image format and filename.
* @param outputPath the output directory (must exist).
* @throws IOException Throws if the file can't be accessed.
*/
public void writeToFile(Path outputPath) throws IOException {
Path imagePath = outputPath.resolve(this.filename);
File outputfile = imagePath.toFile();
ImageIO.write(this.image, this.saveFormat, outputfile);
}

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/mindee/input/LocalInputSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import lombok.Getter;
import org.apache.pdfbox.io.IOUtils;
Expand All @@ -34,6 +35,11 @@ public LocalInputSource(String filePath) throws IOException {
this.filename = file.getName();
}

public LocalInputSource(Path filePath) throws IOException {
this.file = Files.readAllBytes(filePath);
this.filename = filePath.getFileName().toString();
}

public LocalInputSource(File file) throws IOException {
this.file = Files.readAllBytes(file.toPath());
this.filename = file.getName();
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/mindee/input/LocalResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -55,6 +56,16 @@ public LocalResponse(File input) throws IOException {
);
}

/**
* Load from a {@link Path}.
* @param input will be decoded as UTF-8.
*/
public LocalResponse(Path input) throws IOException {
this.file = this.getBytes(
Files.lines(input, StandardCharsets.UTF_8)
);
}

private byte[] getBytes(Stream<String> stream) {
return stream.collect(Collectors.joining("")).getBytes();
}
Expand Down
18 changes: 0 additions & 18 deletions src/test/java/com/mindee/AppTest.java

This file was deleted.

53 changes: 25 additions & 28 deletions src/test/java/com/mindee/MindeeClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.mindee.parsing.common.Document;
import com.mindee.parsing.common.Job;
import com.mindee.parsing.common.PredictResponse;
import com.mindee.product.ProductTestHelper;
import static com.mindee.TestingUtilities.assertStringEqualsFile;
import com.mindee.product.custom.CustomV1;
import com.mindee.product.generated.GeneratedV1;
import com.mindee.product.invoice.InvoiceV4;
Expand All @@ -33,6 +33,9 @@
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import static com.mindee.TestingUtilities.getResourcePath;
import static com.mindee.TestingUtilities.getV1ResourcePathString;

@ExtendWith(MockitoExtension.class)
class MindeeClientTest {

Expand All @@ -51,8 +54,6 @@ public void setUp() {
void givenAClientForCustom_withFile_parse_thenShouldCallMindeeApi()
throws IOException {

File file = new File("src/test/resources/file_types/pdf/blank_1.pdf");

PredictResponse predictResponse = new PredictResponse();
predictResponse.setDocument(new Document<>());
predictResponse.setApiRequest(null);
Expand All @@ -64,7 +65,7 @@ void givenAClientForCustom_withFile_parse_thenShouldCallMindeeApi()
.thenReturn(predictResponse);

PredictResponse<CustomV1> document = client.parse(
new LocalInputSource(file),
new LocalInputSource(getResourcePath("file_types/pdf/blank_1.pdf")),
new Endpoint("", "", ""));

Assertions.assertNotNull(document);
Expand All @@ -76,7 +77,6 @@ void givenAClientForCustom_withFile_parse_thenShouldCallMindeeApi()
void givenAClientForCustomAndPageOptions_parse_thenShouldOperateCutOnPagesAndCallTheHttpClientCorrectly()
throws IOException {

File file = new File("src/test/resources/file_types/pdf/multipage.pdf");
List<Integer> pageNumberToKeep = new ArrayList<>();
pageNumberToKeep.add(1);

Expand All @@ -93,7 +93,7 @@ void givenAClientForCustomAndPageOptions_parse_thenShouldOperateCutOnPagesAndCal
.thenReturn(new SplitPdf(new byte[0], 0));

PredictResponse<CustomV1> document = client.parse(
new LocalInputSource(file),
new LocalInputSource(getResourcePath("file_types/pdf/multipage.pdf")),
new Endpoint("", "", ""),
new PageOptions(
pageNumberToKeep, PageOptionsOperation.KEEP_ONLY, 0));
Expand All @@ -109,7 +109,6 @@ void givenAClientForCustomAndPageOptions_parse_thenShouldOperateCutOnPagesAndCal
void givenAClientForInvoice_withFile_parse_thenShouldCallMindeeApi()
throws IOException {

File file = new File("src/test/resources/file_types/pdf/blank_1.pdf");
PredictResponse predictResponse = new PredictResponse();
predictResponse.setDocument(new Document<>());
predictResponse.setApiRequest(null);
Expand All @@ -123,7 +122,7 @@ void givenAClientForInvoice_withFile_parse_thenShouldCallMindeeApi()

PredictResponse<InvoiceV4> document = client.parse(
InvoiceV4.class,
new LocalInputSource(file));
new LocalInputSource(getResourcePath("file_types/pdf/blank_1.pdf")));

Assertions.assertNotNull(document);
Mockito.verify(mindeeApi, Mockito.times(1))
Expand All @@ -134,8 +133,6 @@ void givenAClientForInvoice_withFile_parse_thenShouldCallMindeeApi()
void givenAClientForInvoice_withInputStream_parse_thenShouldCallMindeeApi()
throws IOException {

File file = new File("src/test/resources/file_types/pdf/blank_1.pdf");

PredictResponse predictResponse = new PredictResponse();
predictResponse.setDocument(new Document<>());
predictResponse.setApiRequest(null);
Expand All @@ -149,7 +146,7 @@ void givenAClientForInvoice_withInputStream_parse_thenShouldCallMindeeApi()
PredictResponse<InvoiceV4> document = client.parse(
InvoiceV4.class,
new LocalInputSource(
Files.newInputStream(file.toPath()),
Files.newInputStream(getResourcePath("file_types/pdf/blank_1.pdf")),
""));

Assertions.assertNotNull(document);
Expand All @@ -161,7 +158,6 @@ void givenAClientForInvoice_withInputStream_parse_thenShouldCallMindeeApi()
void givenAClientForInvoice_withByteArray_parse_thenShouldCallMindeeApi()
throws IOException {

File file = new File("src/test/resources/file_types/pdf/blank_1.pdf");
PredictResponse predictResponse = new PredictResponse();
predictResponse.setDocument(new Document<>());
predictResponse.setApiRequest(null);
Expand All @@ -175,7 +171,7 @@ void givenAClientForInvoice_withByteArray_parse_thenShouldCallMindeeApi()
PredictResponse<InvoiceV4> document = client.parse(
InvoiceV4.class,
new LocalInputSource(
Files.readAllBytes(file.toPath()),
Files.readAllBytes(getResourcePath("file_types/pdf/blank_1.pdf")),
""));

Assertions.assertNotNull(document);
Expand All @@ -187,7 +183,6 @@ void givenAClientForInvoice_withByteArray_parse_thenShouldCallMindeeApi()
void givenAClientForInvoiceAndPageOptions_parse_thenShouldOperateCutOnPagesAndCallTheHttpClientCorrectly()
throws IOException {

File file = new File("src/test/resources/file_types/pdf/multipage.pdf");
List<Integer> pageNumberToKeep = new ArrayList<>();
pageNumberToKeep.add(1);
PredictResponse predictResponse = new PredictResponse();
Expand All @@ -206,7 +201,7 @@ void givenAClientForInvoiceAndPageOptions_parse_thenShouldOperateCutOnPagesAndCa

PredictResponse<InvoiceV4> document = client.parse(
InvoiceV4.class,
new LocalInputSource(file),
new LocalInputSource(getResourcePath("file_types/pdf/multipage.pdf")),
new PageOptions(
pageNumberToKeep, PageOptionsOperation.KEEP_ONLY, 0));

Expand Down Expand Up @@ -286,8 +281,9 @@ void givenACustomDocumentUrl_whenParsed_shouldCallApiWithCorrectParams() throws
@Test
void givenAnAsyncDoc_whenEnqueued_shouldInvokeApiCorrectly() throws IOException {

File file = new File("src/test/resources/file_types/pdf/blank_1.pdf");
LocalInputSource localInputSource = new LocalInputSource(file);
LocalInputSource localInputSource = new LocalInputSource(
getResourcePath("file_types/pdf/blank_1.pdf")
);

Job job = new Job(LocalDateTime.now(), "someid", LocalDateTime.now(), "Completed", null);
AsyncPredictResponse predictResponse = new AsyncPredictResponse();
Expand Down Expand Up @@ -326,8 +322,9 @@ void givenAnAsyncDoc_whenEnqueued_shouldInvokeApiCorrectly() throws IOException
@Test
void givenAnAsyncDoc_whenEnqueuedNoParams_shouldInvokeApiCorrectly() throws IOException {

File file = new File("src/test/resources/file_types/pdf/blank_1.pdf");
LocalInputSource localInputSource = new LocalInputSource(file);
LocalInputSource localInputSource = new LocalInputSource(
getResourcePath("file_types/pdf/blank_1.pdf")
);

Job job = new Job(LocalDateTime.now(), "someid", LocalDateTime.now(), "Completed", null);
AsyncPredictResponse predictResponse = new AsyncPredictResponse();
Expand Down Expand Up @@ -399,9 +396,9 @@ void givenAnAsyncUrl_whenEnqueued_shouldInvokeApiCorrectly() throws IOException
@Test
void givenAnAsyncGeneratedDoc_whenEnqueuedNoParams_shouldInvokeApiCorrectly() throws IOException, InterruptedException {

File file = new File("src/test/resources/file_types/pdf/blank_1.pdf");
LocalInputSource localInputSource = new LocalInputSource(file);

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();
Expand Down Expand Up @@ -438,26 +435,26 @@ void givenAnAsyncGeneratedDoc_whenEnqueuedNoParams_shouldInvokeApiCorrectly() th

@Test
void givenJsonInput_whenSync_shouldDeserializeCorrectly() throws IOException {
File file = new File("src/test/resources/products/invoices/response_v4/complete.json");
File file = new File(getV1ResourcePathString("products/invoices/response_v4/complete.json"));
LocalResponse localResponse = new LocalResponse(file);
AsyncPredictResponse<InvoiceV4> predictResponse = client.loadPrediction(InvoiceV4.class, localResponse);
ProductTestHelper.assertStringEqualsFile(
assertStringEqualsFile(
predictResponse.getDocumentObj().toString(),
"src/test/resources/products/invoices/response_v4/summary_full.rst"
getV1ResourcePathString("/products/invoices/response_v4/summary_full.rst")
);
}

@Test
void givenJsonInput_whenAsync_shouldDeserializeCorrectly() throws IOException {
File file = new File("src/test/resources/products/international_id/response_v2/complete.json");
File file = new File(getV1ResourcePathString("products/international_id/response_v2/complete.json"));
LocalResponse localResponse = new LocalResponse(file);
AsyncPredictResponse<InternationalIdV2> predictResponse = client.loadPrediction(
InternationalIdV2.class,
localResponse
);
ProductTestHelper.assertStringEqualsFile(
assertStringEqualsFile(
predictResponse.getDocumentObj().toString(),
"src/test/resources/products/international_id/response_v2/summary_full.rst"
getV1ResourcePathString("products/international_id/response_v2/summary_full.rst")
);
}
}
18 changes: 10 additions & 8 deletions src/test/java/com/mindee/MindeeClientV2IT.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
import com.mindee.parsing.v2.RawText;
import com.mindee.parsing.v2.field.InferenceFields;
import com.mindee.parsing.v2.field.SimpleField;
import java.io.File;
import java.io.IOException;
import org.junit.jupiter.api.*;

import static com.mindee.TestingUtilities.getResourcePath;
import static com.mindee.TestingUtilities.getV1ResourcePathString;
import static org.junit.jupiter.api.Assertions.*;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
Expand All @@ -35,8 +37,8 @@ void setUp() {
@DisplayName("Empty, multi-page PDF – enqueue & parse must succeed")
void parseFile_emptyMultiPage_mustSucceed() throws IOException, InterruptedException {
LocalInputSource source = new LocalInputSource(
new File("src/test/resources/file_types/pdf/multipage_cut-2.pdf"));

getResourcePath("file_types/pdf/multipage_cut-2.pdf")
);
InferenceParameters params = InferenceParameters
.builder(modelId)
.rag(false)
Expand Down Expand Up @@ -87,7 +89,7 @@ void parseFile_emptyMultiPage_mustSucceed() throws IOException, InterruptedExcep
@DisplayName("Filled, single-page image – enqueue & parse must succeed")
void parseFile_filledSinglePage_mustSucceed() throws IOException, InterruptedException {
LocalInputSource source = new LocalInputSource(
new File("src/test/resources/products/financial_document/default_sample.jpg"));
getV1ResourcePathString("products/financial_document/default_sample.jpg"));

InferenceParameters params = InferenceParameters
.builder(modelId)
Expand Down Expand Up @@ -134,8 +136,8 @@ void parseFile_filledSinglePage_mustSucceed() throws IOException, InterruptedExc
@DisplayName("Invalid model ID – enqueue must raise 422")
void invalidModel_mustThrowError() throws IOException {
LocalInputSource source = new LocalInputSource(
new File("src/test/resources/file_types/pdf/blank_1.pdf"));

getResourcePath("file_types/pdf/blank_1.pdf")
);
InferenceParameters params = InferenceParameters
.builder("INVALID_MODEL_ID")
.build();
Expand All @@ -151,8 +153,8 @@ void invalidModel_mustThrowError() throws IOException {
@DisplayName("Invalid webhook ID – enqueue must raise 422")
void invalidWebhook_mustThrowError() throws IOException {
LocalInputSource source = new LocalInputSource(
new File("src/test/resources/file_types/pdf/blank_1.pdf"));

getResourcePath("file_types/pdf/blank_1.pdf")
);
InferenceParameters params = InferenceParameters
.builder(modelId)
.webhookIds(new String[]{"INVALID_WEBHOOK_ID"})
Expand Down
Loading