Skip to content

Commit f964921

Browse files
authored
♻️ rework test structure - ✨ add loading using Path (#276)
1 parent 9230722 commit f964921

File tree

61 files changed

+518
-415
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+518
-415
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Quickly and easily connect to Mindee's API services using Java.
88
This client library has support for both Mindee platform versions.
99

1010
### V2 - Latest
11-
This is the new platform located here:
11+
This is the latest platform located here:
1212

1313
https://app.mindee.com
1414

@@ -19,7 +19,7 @@ Consult the
1919

2020

2121
### V1
22-
This is the legacy platform located here:
22+
This is the platform located here:
2323

2424
https://platform.mindee.com/
2525

src/main/java/com/mindee/extraction/ExtractedImage.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,19 @@ public ExtractedImage(BufferedImage image, String filename, String saveFormat) {
3838
*/
3939
public void writeToFile(String outputPath) throws IOException {
4040
Path imagePath = Paths.get(outputPath, this.filename);
41-
File outputfile = new File(imagePath.toString());
41+
File outputfile = imagePath.toFile();
42+
ImageIO.write(this.image, this.saveFormat, outputfile);
43+
}
44+
45+
/**
46+
* Write the image to a file.
47+
* Uses the default image format and filename.
48+
* @param outputPath the output directory (must exist).
49+
* @throws IOException Throws if the file can't be accessed.
50+
*/
51+
public void writeToFile(Path outputPath) throws IOException {
52+
Path imagePath = outputPath.resolve(this.filename);
53+
File outputfile = imagePath.toFile();
4254
ImageIO.write(this.image, this.saveFormat, outputfile);
4355
}
4456

src/main/java/com/mindee/input/LocalInputSource.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.io.IOException;
1111
import java.io.InputStream;
1212
import java.nio.file.Files;
13+
import java.nio.file.Path;
1314
import java.util.Base64;
1415
import lombok.Getter;
1516
import org.apache.pdfbox.io.IOUtils;
@@ -34,6 +35,11 @@ public LocalInputSource(String filePath) throws IOException {
3435
this.filename = file.getName();
3536
}
3637

38+
public LocalInputSource(Path filePath) throws IOException {
39+
this.file = Files.readAllBytes(filePath);
40+
this.filename = filePath.getFileName().toString();
41+
}
42+
3743
public LocalInputSource(File file) throws IOException {
3844
this.file = Files.readAllBytes(file.toPath());
3945
this.filename = file.getName();

src/main/java/com/mindee/input/LocalResponse.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.io.InputStreamReader;
1111
import java.nio.charset.StandardCharsets;
1212
import java.nio.file.Files;
13+
import java.nio.file.Path;
1314
import java.security.InvalidKeyException;
1415
import java.security.NoSuchAlgorithmException;
1516
import java.util.stream.Collectors;
@@ -55,6 +56,16 @@ public LocalResponse(File input) throws IOException {
5556
);
5657
}
5758

59+
/**
60+
* Load from a {@link Path}.
61+
* @param input will be decoded as UTF-8.
62+
*/
63+
public LocalResponse(Path input) throws IOException {
64+
this.file = this.getBytes(
65+
Files.lines(input, StandardCharsets.UTF_8)
66+
);
67+
}
68+
5869
private byte[] getBytes(Stream<String> stream) {
5970
return stream.collect(Collectors.joining("")).getBytes();
6071
}

src/test/java/com/mindee/AppTest.java

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/test/java/com/mindee/MindeeClientTest.java

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import com.mindee.parsing.common.Document;
1212
import com.mindee.parsing.common.Job;
1313
import com.mindee.parsing.common.PredictResponse;
14-
import com.mindee.product.ProductTestHelper;
14+
import static com.mindee.TestingUtilities.assertStringEqualsFile;
1515
import com.mindee.product.custom.CustomV1;
1616
import com.mindee.product.generated.GeneratedV1;
1717
import com.mindee.product.invoice.InvoiceV4;
@@ -33,6 +33,9 @@
3333
import org.mockito.Mockito;
3434
import org.mockito.junit.jupiter.MockitoExtension;
3535

36+
import static com.mindee.TestingUtilities.getResourcePath;
37+
import static com.mindee.TestingUtilities.getV1ResourcePathString;
38+
3639
@ExtendWith(MockitoExtension.class)
3740
class MindeeClientTest {
3841

@@ -51,8 +54,6 @@ public void setUp() {
5154
void givenAClientForCustom_withFile_parse_thenShouldCallMindeeApi()
5255
throws IOException {
5356

54-
File file = new File("src/test/resources/file_types/pdf/blank_1.pdf");
55-
5657
PredictResponse predictResponse = new PredictResponse();
5758
predictResponse.setDocument(new Document<>());
5859
predictResponse.setApiRequest(null);
@@ -64,7 +65,7 @@ void givenAClientForCustom_withFile_parse_thenShouldCallMindeeApi()
6465
.thenReturn(predictResponse);
6566

6667
PredictResponse<CustomV1> document = client.parse(
67-
new LocalInputSource(file),
68+
new LocalInputSource(getResourcePath("file_types/pdf/blank_1.pdf")),
6869
new Endpoint("", "", ""));
6970

7071
Assertions.assertNotNull(document);
@@ -76,7 +77,6 @@ void givenAClientForCustom_withFile_parse_thenShouldCallMindeeApi()
7677
void givenAClientForCustomAndPageOptions_parse_thenShouldOperateCutOnPagesAndCallTheHttpClientCorrectly()
7778
throws IOException {
7879

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

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

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

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

124123
PredictResponse<InvoiceV4> document = client.parse(
125124
InvoiceV4.class,
126-
new LocalInputSource(file));
125+
new LocalInputSource(getResourcePath("file_types/pdf/blank_1.pdf")));
127126

128127
Assertions.assertNotNull(document);
129128
Mockito.verify(mindeeApi, Mockito.times(1))
@@ -134,8 +133,6 @@ void givenAClientForInvoice_withFile_parse_thenShouldCallMindeeApi()
134133
void givenAClientForInvoice_withInputStream_parse_thenShouldCallMindeeApi()
135134
throws IOException {
136135

137-
File file = new File("src/test/resources/file_types/pdf/blank_1.pdf");
138-
139136
PredictResponse predictResponse = new PredictResponse();
140137
predictResponse.setDocument(new Document<>());
141138
predictResponse.setApiRequest(null);
@@ -149,7 +146,7 @@ void givenAClientForInvoice_withInputStream_parse_thenShouldCallMindeeApi()
149146
PredictResponse<InvoiceV4> document = client.parse(
150147
InvoiceV4.class,
151148
new LocalInputSource(
152-
Files.newInputStream(file.toPath()),
149+
Files.newInputStream(getResourcePath("file_types/pdf/blank_1.pdf")),
153150
""));
154151

155152
Assertions.assertNotNull(document);
@@ -161,7 +158,6 @@ void givenAClientForInvoice_withInputStream_parse_thenShouldCallMindeeApi()
161158
void givenAClientForInvoice_withByteArray_parse_thenShouldCallMindeeApi()
162159
throws IOException {
163160

164-
File file = new File("src/test/resources/file_types/pdf/blank_1.pdf");
165161
PredictResponse predictResponse = new PredictResponse();
166162
predictResponse.setDocument(new Document<>());
167163
predictResponse.setApiRequest(null);
@@ -175,7 +171,7 @@ void givenAClientForInvoice_withByteArray_parse_thenShouldCallMindeeApi()
175171
PredictResponse<InvoiceV4> document = client.parse(
176172
InvoiceV4.class,
177173
new LocalInputSource(
178-
Files.readAllBytes(file.toPath()),
174+
Files.readAllBytes(getResourcePath("file_types/pdf/blank_1.pdf")),
179175
""));
180176

181177
Assertions.assertNotNull(document);
@@ -187,7 +183,6 @@ void givenAClientForInvoice_withByteArray_parse_thenShouldCallMindeeApi()
187183
void givenAClientForInvoiceAndPageOptions_parse_thenShouldOperateCutOnPagesAndCallTheHttpClientCorrectly()
188184
throws IOException {
189185

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

207202
PredictResponse<InvoiceV4> document = client.parse(
208203
InvoiceV4.class,
209-
new LocalInputSource(file),
204+
new LocalInputSource(getResourcePath("file_types/pdf/multipage.pdf")),
210205
new PageOptions(
211206
pageNumberToKeep, PageOptionsOperation.KEEP_ONLY, 0));
212207

@@ -286,8 +281,9 @@ void givenACustomDocumentUrl_whenParsed_shouldCallApiWithCorrectParams() throws
286281
@Test
287282
void givenAnAsyncDoc_whenEnqueued_shouldInvokeApiCorrectly() throws IOException {
288283

289-
File file = new File("src/test/resources/file_types/pdf/blank_1.pdf");
290-
LocalInputSource localInputSource = new LocalInputSource(file);
284+
LocalInputSource localInputSource = new LocalInputSource(
285+
getResourcePath("file_types/pdf/blank_1.pdf")
286+
);
291287

292288
Job job = new Job(LocalDateTime.now(), "someid", LocalDateTime.now(), "Completed", null);
293289
AsyncPredictResponse predictResponse = new AsyncPredictResponse();
@@ -326,8 +322,9 @@ void givenAnAsyncDoc_whenEnqueued_shouldInvokeApiCorrectly() throws IOException
326322
@Test
327323
void givenAnAsyncDoc_whenEnqueuedNoParams_shouldInvokeApiCorrectly() throws IOException {
328324

329-
File file = new File("src/test/resources/file_types/pdf/blank_1.pdf");
330-
LocalInputSource localInputSource = new LocalInputSource(file);
325+
LocalInputSource localInputSource = new LocalInputSource(
326+
getResourcePath("file_types/pdf/blank_1.pdf")
327+
);
331328

332329
Job job = new Job(LocalDateTime.now(), "someid", LocalDateTime.now(), "Completed", null);
333330
AsyncPredictResponse predictResponse = new AsyncPredictResponse();
@@ -399,9 +396,9 @@ void givenAnAsyncUrl_whenEnqueued_shouldInvokeApiCorrectly() throws IOException
399396
@Test
400397
void givenAnAsyncGeneratedDoc_whenEnqueuedNoParams_shouldInvokeApiCorrectly() throws IOException, InterruptedException {
401398

402-
File file = new File("src/test/resources/file_types/pdf/blank_1.pdf");
403-
LocalInputSource localInputSource = new LocalInputSource(file);
404-
399+
LocalInputSource localInputSource = new LocalInputSource(
400+
getResourcePath("file_types/pdf/blank_1.pdf")
401+
);
405402
Job job = new Job(LocalDateTime.now(), "someid", LocalDateTime.now(), "Completed", null);
406403
Endpoint endpoint = new Endpoint("dsddw", "dcsdcd", "dsfdd");
407404
AsyncPredictResponse predictResponse = new AsyncPredictResponse();
@@ -438,26 +435,26 @@ void givenAnAsyncGeneratedDoc_whenEnqueuedNoParams_shouldInvokeApiCorrectly() th
438435

439436
@Test
440437
void givenJsonInput_whenSync_shouldDeserializeCorrectly() throws IOException {
441-
File file = new File("src/test/resources/products/invoices/response_v4/complete.json");
438+
File file = new File(getV1ResourcePathString("products/invoices/response_v4/complete.json"));
442439
LocalResponse localResponse = new LocalResponse(file);
443440
AsyncPredictResponse<InvoiceV4> predictResponse = client.loadPrediction(InvoiceV4.class, localResponse);
444-
ProductTestHelper.assertStringEqualsFile(
441+
assertStringEqualsFile(
445442
predictResponse.getDocumentObj().toString(),
446-
"src/test/resources/products/invoices/response_v4/summary_full.rst"
443+
getV1ResourcePathString("/products/invoices/response_v4/summary_full.rst")
447444
);
448445
}
449446

450447
@Test
451448
void givenJsonInput_whenAsync_shouldDeserializeCorrectly() throws IOException {
452-
File file = new File("src/test/resources/products/international_id/response_v2/complete.json");
449+
File file = new File(getV1ResourcePathString("products/international_id/response_v2/complete.json"));
453450
LocalResponse localResponse = new LocalResponse(file);
454451
AsyncPredictResponse<InternationalIdV2> predictResponse = client.loadPrediction(
455452
InternationalIdV2.class,
456453
localResponse
457454
);
458-
ProductTestHelper.assertStringEqualsFile(
455+
assertStringEqualsFile(
459456
predictResponse.getDocumentObj().toString(),
460-
"src/test/resources/products/international_id/response_v2/summary_full.rst"
457+
getV1ResourcePathString("products/international_id/response_v2/summary_full.rst")
461458
);
462459
}
463460
}

src/test/java/com/mindee/MindeeClientV2IT.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
import com.mindee.parsing.v2.RawText;
1212
import com.mindee.parsing.v2.field.InferenceFields;
1313
import com.mindee.parsing.v2.field.SimpleField;
14-
import java.io.File;
1514
import java.io.IOException;
1615
import org.junit.jupiter.api.*;
16+
17+
import static com.mindee.TestingUtilities.getResourcePath;
18+
import static com.mindee.TestingUtilities.getV1ResourcePathString;
1719
import static org.junit.jupiter.api.Assertions.*;
1820

1921
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@@ -35,8 +37,8 @@ void setUp() {
3537
@DisplayName("Empty, multi-page PDF – enqueue & parse must succeed")
3638
void parseFile_emptyMultiPage_mustSucceed() throws IOException, InterruptedException {
3739
LocalInputSource source = new LocalInputSource(
38-
new File("src/test/resources/file_types/pdf/multipage_cut-2.pdf"));
39-
40+
getResourcePath("file_types/pdf/multipage_cut-2.pdf")
41+
);
4042
InferenceParameters params = InferenceParameters
4143
.builder(modelId)
4244
.rag(false)
@@ -87,7 +89,7 @@ void parseFile_emptyMultiPage_mustSucceed() throws IOException, InterruptedExcep
8789
@DisplayName("Filled, single-page image – enqueue & parse must succeed")
8890
void parseFile_filledSinglePage_mustSucceed() throws IOException, InterruptedException {
8991
LocalInputSource source = new LocalInputSource(
90-
new File("src/test/resources/products/financial_document/default_sample.jpg"));
92+
getV1ResourcePathString("products/financial_document/default_sample.jpg"));
9193

9294
InferenceParameters params = InferenceParameters
9395
.builder(modelId)
@@ -134,8 +136,8 @@ void parseFile_filledSinglePage_mustSucceed() throws IOException, InterruptedExc
134136
@DisplayName("Invalid model ID – enqueue must raise 422")
135137
void invalidModel_mustThrowError() throws IOException {
136138
LocalInputSource source = new LocalInputSource(
137-
new File("src/test/resources/file_types/pdf/blank_1.pdf"));
138-
139+
getResourcePath("file_types/pdf/blank_1.pdf")
140+
);
139141
InferenceParameters params = InferenceParameters
140142
.builder("INVALID_MODEL_ID")
141143
.build();
@@ -151,8 +153,8 @@ void invalidModel_mustThrowError() throws IOException {
151153
@DisplayName("Invalid webhook ID – enqueue must raise 422")
152154
void invalidWebhook_mustThrowError() throws IOException {
153155
LocalInputSource source = new LocalInputSource(
154-
new File("src/test/resources/file_types/pdf/blank_1.pdf"));
155-
156+
getResourcePath("file_types/pdf/blank_1.pdf")
157+
);
156158
InferenceParameters params = InferenceParameters
157159
.builder(modelId)
158160
.webhookIds(new String[]{"INVALID_WEBHOOK_ID"})

0 commit comments

Comments
 (0)