-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMindeeClientV2IT.java
More file actions
231 lines (191 loc) · 7.7 KB
/
MindeeClientV2IT.java
File metadata and controls
231 lines (191 loc) · 7.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package com.mindee;
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.http.MindeeHttpExceptionV2;
import com.mindee.input.LocalInputSource;
import com.mindee.input.URLInputSource;
import com.mindee.parsing.v2.Inference;
import com.mindee.parsing.v2.InferenceActiveOptions;
import com.mindee.parsing.v2.InferenceFile;
import com.mindee.parsing.v2.InferenceResponse;
import com.mindee.parsing.v2.InferenceResult;
import com.mindee.parsing.v2.RawText;
import com.mindee.parsing.v2.field.InferenceFields;
import com.mindee.parsing.v2.field.SimpleField;
import java.io.IOException;
import org.junit.jupiter.api.*;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Tag("integration")
@DisplayName("MindeeV2 – Integration Tests")
class MindeeClientV2IT {
private MindeeClientV2 mindeeClient;
private String modelId;
@BeforeAll
void setUp() {
String apiKey = System.getenv("MINDEE_V2_API_KEY");
modelId = System.getenv("MINDEE_V2_FINDOC_MODEL_ID");
mindeeClient = new MindeeClientV2(apiKey);
}
@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")
);
InferenceParameters params = InferenceParameters
.builder(modelId)
.rag(false)
.rawText(true)
.polygon(null)
.confidence(null)
.alias("java-integration-test_multipage")
.textContext(null)
.pollingOptions(
AsyncPollingOptions.builder()
.initialDelaySec(3.0)
.intervalSec(1.5)
.maxRetries(80)
.build()
)
.build();
InferenceResponse response = mindeeClient.enqueueAndGetInference(source, params);
assertNotNull(response);
Inference inference = response.getInference();
assertNotNull(inference);
InferenceFile file = inference.getFile();
assertNotNull(file);
assertEquals("multipage_cut-2.pdf", file.getName());
assertEquals(2, file.getPageCount());
assertNotNull(inference.getModel());
assertEquals(modelId, inference.getModel().getId());
InferenceActiveOptions activeOptions = inference.getActiveOptions();
assertNotNull(activeOptions);
assertFalse(activeOptions.getRag());
assertTrue(activeOptions.getRawText());
assertFalse(activeOptions.getPolygon());
assertFalse(activeOptions.getConfidence());
InferenceResult result = inference.getResult();
assertNotNull(result);
RawText rawText = result.getRawText();
assertEquals(2, rawText.getPages().size());
InferenceFields 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(
getV2ResourcePath("products/financial_document/default_sample.jpg"));
InferenceParameters params = InferenceParameters
.builder(modelId)
.rag(false)
.alias("java-integration-test_single-page")
.textContext("this is an invoice")
.build();
InferenceResponse response = mindeeClient.enqueueAndGetInference(source, params);
assertNotNull(response);
Inference inference = response.getInference();
assertNotNull(inference);
InferenceFile file = inference.getFile();
assertNotNull(file);
assertEquals("default_sample.jpg", file.getName());
assertEquals(1, file.getPageCount());
assertNotNull(inference.getModel());
assertEquals(modelId, inference.getModel().getId());
InferenceActiveOptions activeOptions = inference.getActiveOptions();
assertNotNull(activeOptions);
assertFalse(activeOptions.getRag());
assertFalse(activeOptions.getRawText());
assertFalse(activeOptions.getPolygon());
assertFalse(activeOptions.getConfidence());
InferenceResult result = inference.getResult();
assertNotNull(result);
RawText rawText = result.getRawText();
assertNull(rawText);
InferenceFields fields = result.getFields();
assertNotNull(fields);
SimpleField supplierName = fields.getSimpleField("supplier_name");
assertNotNull(supplierName);
assertEquals("John Smith", supplierName.getStringValue());
}
@Test
@DisplayName("Data Schema Replace – enqueue & parse must succeed")
void parseFile_dataSchemaReplace_mustSucceed() throws IOException, InterruptedException {
LocalInputSource source = new LocalInputSource(
getV2ResourcePath("products/financial_document/default_sample.jpg"));
InferenceParameters params = InferenceParameters
.builder(modelId)
.rag(false)
.alias("java-integration-test_data-schema-replace")
.dataSchema(readFileAsString(getV2ResourcePath("inference/data_schema_replace_param.json")))
.build();
InferenceResponse response = mindeeClient.enqueueAndGetInference(source, params);
assertNotNull(response);
Inference inference = response.getInference();
assertNotNull(inference);
InferenceResult result = inference.getResult();
assertNotNull(result);
RawText rawText = result.getRawText();
assertNull(rawText);
InferenceFields fields = result.getFields();
assertNotNull(fields);
SimpleField supplierName = fields.getSimpleField("test_replace");
assertNotNull(supplierName);
assertEquals("a test value", supplierName.getStringValue());
}
@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")
);
InferenceParameters params = InferenceParameters
.builder("INVALID_MODEL_ID")
.textContext("this is invalid")
.build();
MindeeHttpExceptionV2 ex = assertThrows(
MindeeHttpExceptionV2.class,
() -> mindeeClient.enqueueInference(source, params)
);
assertEquals(422, ex.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")
);
InferenceParameters params = InferenceParameters
.builder(modelId)
.webhookIds(new String[]{"INVALID_WEBHOOK_ID"})
.build();
MindeeHttpExceptionV2 ex = assertThrows(
MindeeHttpExceptionV2.class,
() -> mindeeClient.enqueueInference(source, params)
);
assertEquals(422, ex.getStatus());
}
@Test
@DisplayName("Invalid job ID – parseQueued must raise an error")
void invalidJob_mustThrowError() {
MindeeHttpExceptionV2 ex = assertThrows(
MindeeHttpExceptionV2.class,
() -> mindeeClient.getInference("INVALID_JOB_ID")
);
assertEquals(422, ex.getStatus());
assertNotNull(ex);
}
@Test
@DisplayName("URL input source - A url param should not raise errors.")
void urlInputSource_mustNotRaiseErrors() throws IOException, InterruptedException {
URLInputSource urlSource = URLInputSource.builder(System.getenv("MINDEE_V2_SE_TESTS_BLANK_PDF_URL")).build();
InferenceParameters options = InferenceParameters
.builder(modelId)
.build();
InferenceResponse response = mindeeClient.enqueueAndGetInference(urlSource, options);
assertNotNull(response);
assertNotNull(response.getInference());
}
}