Skip to content

Commit df7ca94

Browse files
add tests
1 parent e15675b commit df7ca94

File tree

3 files changed

+240
-25
lines changed

3 files changed

+240
-25
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package com.mindee;
2+
3+
import com.mindee.InferencePredictOptions;
4+
import com.mindee.MindeeClientV2;
5+
import com.mindee.http.MindeeHttpExceptionV2;
6+
import com.mindee.input.LocalInputSource;
7+
import com.mindee.parsing.v2.InferenceResponse;
8+
9+
import java.io.File;
10+
import java.io.IOException;
11+
12+
import org.junit.jupiter.api.*;
13+
14+
import static org.junit.jupiter.api.Assertions.*;
15+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
16+
17+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
18+
@Tag("integration")
19+
@DisplayName("MindeeClientV2 – integration tests (V2)")
20+
class MindeeClientV2IntegrationTest {
21+
22+
private MindeeClientV2 mindeeClient;
23+
private String modelId;
24+
25+
@BeforeAll
26+
void setUp() {
27+
String apiKey = System.getenv("MINDEE_V2_API_KEY");
28+
modelId = System.getenv("MINDEE_V2_FINDOC_MODEL_ID");
29+
30+
assumeTrue(
31+
apiKey != null && !apiKey.trim().isEmpty(),
32+
"MINDEE_V2_API_KEY env var is missing – integration tests skipped"
33+
);
34+
assumeTrue(
35+
modelId != null && !modelId.trim().isEmpty(),
36+
"MINDEE_V2_FINDOC_MODEL_ID env var is missing – integration tests skipped"
37+
);
38+
39+
mindeeClient = new MindeeClientV2(apiKey);
40+
}
41+
42+
@Test
43+
@DisplayName("Empty, multi-page PDF – enqueue & parse must succeed")
44+
void parseFile_emptyMultiPage_mustSucceed() throws IOException, InterruptedException {
45+
LocalInputSource source = new LocalInputSource(
46+
new File("src/test/resources/file_types/pdf/multipage_cut-2.pdf"));
47+
48+
InferencePredictOptions options =
49+
InferencePredictOptions.builder(modelId).build();
50+
51+
InferenceResponse response = mindeeClient.enqueueAndParse(source, options);
52+
53+
assertNotNull(response);
54+
assertNotNull(response.getInference());
55+
56+
assertNotNull(response.getInference().getFile());
57+
assertEquals("multipage_cut-2.pdf", response.getInference().getFile().getName());
58+
59+
assertNotNull(response.getInference().getModel());
60+
assertEquals(modelId, response.getInference().getModel().getId());
61+
62+
assertNotNull(response.getInference().getResult());
63+
assertNull(response.getInference().getResult().getOptions());
64+
}
65+
66+
@Test
67+
@DisplayName("Filled, single-page image – enqueue & parse must succeed")
68+
void parseFile_filledSinglePage_mustSucceed() throws IOException, InterruptedException {
69+
LocalInputSource source = new LocalInputSource(
70+
new File("src/test/resources/products/financial_document/default_sample.jpg"));
71+
72+
InferencePredictOptions options =
73+
InferencePredictOptions.builder(modelId).build();
74+
75+
InferenceResponse response = mindeeClient.enqueueAndParse(source, options);
76+
77+
assertNotNull(response);
78+
assertNotNull(response.getInference());
79+
80+
assertNotNull(response.getInference().getFile());
81+
assertEquals("default_sample.jpg", response.getInference().getFile().getName());
82+
83+
assertNotNull(response.getInference().getModel());
84+
assertEquals(modelId, response.getInference().getModel().getId());
85+
86+
assertNotNull(response.getInference().getResult());
87+
assertNotNull(response.getInference().getResult().getFields());
88+
assertNotNull(response.getInference().getResult().getFields().get("supplier_name"));
89+
assertEquals(
90+
"John Smith",
91+
response.getInference()
92+
.getResult()
93+
.getFields()
94+
.get("supplier_name")
95+
.getSimpleField()
96+
.getValue()
97+
);
98+
}
99+
100+
@Test
101+
@DisplayName("Invalid model ID – enqueue must raise 422")
102+
void invalidModel_mustThrowError() throws IOException {
103+
LocalInputSource source = new LocalInputSource(
104+
new File("src/test/resources/file_types/pdf/multipage_cut-2.pdf"));
105+
106+
InferencePredictOptions options =
107+
InferencePredictOptions.builder("INVALID MODEL ID").build();
108+
109+
MindeeHttpExceptionV2 ex = assertThrows(
110+
MindeeHttpExceptionV2.class,
111+
() -> mindeeClient.enqueue(source, options)
112+
);
113+
assertEquals(422, ex.getStatus());
114+
}
115+
116+
@Test
117+
@DisplayName("Invalid job ID – parseQueued must raise an error")
118+
void invalidJob_mustThrowError() {
119+
MindeeHttpExceptionV2 ex = assertThrows(
120+
MindeeHttpExceptionV2.class,
121+
() -> mindeeClient.parseQueued("not-a-valid-job-ID")
122+
);
123+
assertEquals(404, ex.getStatus());
124+
assertNotNull(ex);
125+
}
126+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.mindee;
2+
3+
import com.mindee.http.MindeeApiV2;
4+
import com.mindee.input.LocalInputSource;
5+
import com.mindee.input.LocalResponse;
6+
import com.mindee.parsing.v2.CommonResponse;
7+
import com.mindee.parsing.v2.InferenceResponse;
8+
import com.mindee.parsing.v2.JobResponse;
9+
import java.io.File;
10+
import java.io.IOException;
11+
import org.junit.jupiter.api.DisplayName;
12+
import org.junit.jupiter.api.Nested;
13+
import org.junit.jupiter.api.Test;
14+
import org.mockito.Mockito;
15+
16+
import static org.junit.jupiter.api.Assertions.*;
17+
import static org.mockito.ArgumentMatchers.*;
18+
import static org.mockito.Mockito.*;
19+
20+
@DisplayName("MindeeClientV2 – client / API interaction tests")
21+
class MindeeClientV2Test {
22+
/**
23+
* Creates a fully mocked MindeeClientV2.
24+
*/
25+
private static MindeeClientV2 makeClientWithMockedApi(MindeeApiV2 mockedApi) {
26+
return new MindeeClientV2(mockedApi);
27+
}
28+
29+
@Nested
30+
@DisplayName("enqueue()")
31+
class Enqueue {
32+
@Test
33+
@DisplayName("sends exactly one HTTP call and yields a non-null response")
34+
void enqueue_post_async() throws IOException {
35+
MindeeApiV2 predictable = Mockito.mock(MindeeApiV2.class);
36+
when(predictable.enqueuePost(any(LocalInputSource.class), any(InferencePredictOptions.class)))
37+
.thenReturn(new JobResponse());
38+
39+
MindeeClientV2 mindeeClient = makeClientWithMockedApi(predictable);
40+
41+
LocalInputSource input =
42+
new LocalInputSource(new File("src/test/resources/file_types/pdf/blank_1.pdf"));
43+
JobResponse response = mindeeClient.enqueue(
44+
input,
45+
InferencePredictOptions.builder("dummy-model-id").build()
46+
);
47+
48+
assertNotNull(response, "enqueue() must return a response");
49+
verify(predictable, atMostOnce())
50+
.enqueuePost(any(LocalInputSource.class), any(InferencePredictOptions.class));
51+
}
52+
}
53+
54+
@Nested
55+
@DisplayName("parseQueued()")
56+
class ParseQueued {
57+
@Test
58+
@DisplayName("hits the HTTP endpoint once and returns a non-null response")
59+
void document_getQueued_async() {
60+
MindeeApiV2 predictable = Mockito.mock(MindeeApiV2.class);
61+
when(predictable.getInferenceFromQueue(anyString()))
62+
.thenReturn(new JobResponse());
63+
64+
MindeeClientV2 mindeeClient = makeClientWithMockedApi(predictable);
65+
66+
CommonResponse response = mindeeClient.parseQueued("dummy-id");
67+
assertNotNull(response, "parseQueued() must return a response");
68+
verify(predictable, atMostOnce()).getInferenceFromQueue(anyString());
69+
}
70+
}
71+
72+
@Nested
73+
@DisplayName("loadInference()")
74+
class LoadInference {
75+
76+
@Test
77+
@DisplayName("parses local JSON and exposes correct field values")
78+
void inference_loadsLocally() throws IOException {
79+
MindeeClientV2 mindeeClient = new MindeeClientV2("dummy");
80+
File jsonFile =
81+
new File("src/test/resources/v2/products/financial_document/complete.json");
82+
LocalResponse localResponse = new LocalResponse(jsonFile);
83+
84+
InferenceResponse loaded = mindeeClient.loadInference(localResponse);
85+
86+
assertNotNull(loaded, "Loaded InferenceResponse must not be null");
87+
assertEquals(
88+
"12345678-1234-1234-1234-123456789abc",
89+
loaded.getInference().getModel().getId(),
90+
"Model Id mismatch"
91+
);
92+
assertEquals(
93+
"John Smith",
94+
loaded.getInference()
95+
.getResult()
96+
.getFields()
97+
.get("supplier_name")
98+
.getSimpleField()
99+
.getValue(),
100+
"Supplier name mismatch"
101+
);
102+
}
103+
}
104+
}

src/test/java/com/mindee/v2/InferenceTest.java

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package com.mindee.v2;
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.mindee.MindeeClientV2;
5+
import com.mindee.input.LocalResponse;
46
import com.mindee.parsing.v2.DynamicField;
57
import com.mindee.parsing.v2.DynamicField.FieldType;
68
import com.mindee.parsing.v2.InferenceFields;
79
import com.mindee.parsing.v2.InferenceResponse;
810
import com.mindee.parsing.v2.ListField;
911
import com.mindee.parsing.v2.ObjectField;
12+
13+
import java.io.File;
1014
import java.io.IOException;
1115
import java.io.InputStream;
1216
import java.util.Map;
@@ -18,50 +22,30 @@
1822

1923
@DisplayName("InferenceV2 – field integrity checks")
2024
class InferenceTest {
21-
22-
/* ------------------------------------------------------------------ */
23-
/* Helper */
24-
/* ------------------------------------------------------------------ */
25-
26-
private static InferenceResponse loadPrediction(String name) throws IOException {
27-
String resourcePath = "v2/products/financial_document/" + name + ".json";
28-
try (InputStream is = InferenceTest.class.getClassLoader().getResourceAsStream(resourcePath)) {
29-
assertNotNull(is, "Test resource not found: " + resourcePath);
30-
ObjectMapper mapper = new ObjectMapper().findAndRegisterModules();
31-
return mapper.readValue(is, InferenceResponse.class);
32-
}
33-
}
34-
35-
/* ------------------------------------------------------------------ */
36-
/* Tests – “blank” */
37-
/* ------------------------------------------------------------------ */
38-
3925
@Nested
4026
@DisplayName("When the async prediction is blank")
4127
class BlankPrediction {
4228

4329
@Test
4430
@DisplayName("all properties must be valid")
4531
void asyncPredict_whenEmpty_mustHaveValidProperties() throws IOException {
46-
InferenceResponse response = loadPrediction("blank");
32+
MindeeClientV2 mindeeClient = new MindeeClientV2("dummy");
33+
InferenceResponse response = mindeeClient.loadInference(new LocalResponse(InferenceTest.class.getClassLoader().getResourceAsStream("v2/products/financial_document/blank.json")));
4734
InferenceFields fields = response.getInference().getResult().getFields();
4835

4936
assertEquals(21, fields.size(), "Expected 21 fields");
5037

51-
/* taxes ----------------------------------------------------------------- */
5238
DynamicField taxes = fields.get("taxes");
5339
assertNotNull(taxes, "'taxes' field must exist");
5440
ListField taxesList = taxes.getListField();
5541
assertNotNull(taxesList, "'taxes' must be a ListField");
5642
assertTrue(taxesList.getItems().isEmpty(), "'taxes' list must be empty");
5743

58-
/* supplier_address ------------------------------------------------------- */
5944
DynamicField supplierAddress = fields.get("supplier_address");
6045
assertNotNull(supplierAddress, "'supplier_address' field must exist");
6146
ObjectField supplierObj = supplierAddress.getObjectField();
6247
assertNotNull(supplierObj, "'supplier_address' must be an ObjectField");
6348

64-
/* generic checks --------------------------------------------------------- */
6549
for (Map.Entry<String, DynamicField> entry : fields.entrySet()) {
6650
DynamicField value = entry.getValue();
6751
if (value == null) {
@@ -82,7 +66,8 @@ void asyncPredict_whenEmpty_mustHaveValidProperties() throws IOException {
8266
assertNull(value.getSimpleField(), entry.getKey() + " – SimpleField must be null");
8367
break;
8468

85-
default: // SimpleField (or any scalar)
69+
case SIMPLE_FIELD:
70+
default:
8671
assertNotNull(value.getSimpleField(), entry.getKey() + " – SimpleField expected");
8772
assertNull(value.getListField(), entry.getKey() + " – ListField must be null");
8873
assertNull(value.getObjectField(), entry.getKey() + " – ObjectField must be null");
@@ -99,7 +84,8 @@ class CompletePrediction {
9984
@Test
10085
@DisplayName("all properties must be valid")
10186
void asyncPredict_whenComplete_mustHaveValidProperties() throws IOException {
102-
InferenceResponse response = loadPrediction("complete");
87+
MindeeClientV2 mindeeClient = new MindeeClientV2("dummy");
88+
InferenceResponse response = mindeeClient.loadInference(new LocalResponse(InferenceTest.class.getClassLoader().getResourceAsStream("v2/products/financial_document/complete.json")));
10389
InferenceFields fields = response.getInference().getResult().getFields();
10490

10591
assertEquals(21, fields.size(), "Expected 21 fields");
@@ -120,7 +106,6 @@ void asyncPredict_whenComplete_mustHaveValidProperties() throws IOException {
120106
"'taxes.base' value mismatch"
121107
);
122108

123-
/* supplier_address ------------------------------------------------------- */
124109
DynamicField supplierAddress = fields.get("supplier_address");
125110
assertNotNull(supplierAddress, "'supplier_address' field must exist");
126111

0 commit comments

Comments
 (0)