-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMindeeClientV2Test.java
More file actions
148 lines (125 loc) · 5.07 KB
/
MindeeClientV2Test.java
File metadata and controls
148 lines (125 loc) · 5.07 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
package com.mindee;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mindee.http.MindeeApiV2;
import com.mindee.input.LocalInputSource;
import com.mindee.input.LocalResponse;
import com.mindee.parsing.v2.InferenceResponse;
import com.mindee.parsing.v2.JobResponse;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import static com.mindee.TestingUtilities.getResourcePath;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
@DisplayName("MindeeV2 – Client and API Tests")
class MindeeClientV2Test {
/**
* Creates a fully mocked MindeeClientV2.
*/
private static MindeeClientV2 makeClientWithMockedApi(MindeeApiV2 mockedApi) {
return new MindeeClientV2(mockedApi);
}
@Nested
@DisplayName("enqueue()")
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.reqPostInferenceEnqueue(any(LocalInputSource.class), any(InferenceParameters.class)))
.thenReturn(new JobResponse());
MindeeClientV2 mindeeClient = makeClientWithMockedApi(predictable);
LocalInputSource input =
new LocalInputSource(getResourcePath("file_types/pdf/blank_1.pdf"));
JobResponse response = mindeeClient.enqueueInference(
input,
InferenceParameters.builder("dummy-model-id").build()
);
assertNotNull(response, "enqueue() must return a response");
verify(predictable, atMostOnce())
.reqPostInferenceEnqueue(any(LocalInputSource.class), any(InferenceParameters.class));
}
}
@Nested
@DisplayName("getJob()")
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();
mapper.findAndRegisterModules();
JobResponse processing = mapper.readValue(json, JobResponse.class);
when(predictable.reqGetJob(anyString()))
.thenReturn(processing);
MindeeClientV2 mindeeClient = makeClientWithMockedApi(predictable);
JobResponse response = mindeeClient.getJob("dummy-id");
assertNotNull(response, "getJob() must return a response");
verify(predictable, atMostOnce()).reqGetJob(anyString());
}
}
@Nested
@DisplayName("getInference()")
class GetInference {
@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);
String json = FileUtils.readFileToString(
getResourcePath("v2/products/financial_document/complete.json").toFile()
);
ObjectMapper mapper = new ObjectMapper();
mapper.findAndRegisterModules();
InferenceResponse processing = mapper.readValue(json, InferenceResponse.class);
when(predictable.reqGetInference(anyString()))
.thenReturn(processing);
MindeeClientV2 mindeeClient = makeClientWithMockedApi(predictable);
InferenceResponse response = mindeeClient.getInference("12345678-1234-1234-1234-123456789abc");
assertNotNull(response, "getInference() must return a response");
assertEquals(
21, response.getInference().getResult().getFields().size(),
"Result must have one field"
);
assertEquals(
"John Smith",
response.getInference().getResult().getFields().get("supplier_name").getSimpleField().getValue(),
"Result must deserialize fields properly."
);
verify(predictable, atMostOnce()).reqGetInference(anyString());
}
}
@Nested
@DisplayName("deserializeResponse()")
class DeserializeResponse {
@Test
@DisplayName("parses local JSON and exposes correct field values")
void inference_loadsLocally() throws IOException {
LocalResponse localResponse = new LocalResponse(
getResourcePath("v2/products/financial_document/complete.json")
);
InferenceResponse loaded = localResponse.deserializeResponse(InferenceResponse.class);
assertNotNull(loaded, "Loaded InferenceResponse must not be null");
assertEquals(
"12345678-1234-1234-1234-123456789abc",
loaded.getInference().getModel().getId(),
"Model Id mismatch"
);
assertEquals(
"John Smith",
loaded.getInference()
.getResult()
.getFields()
.get("supplier_name")
.getSimpleField()
.getValue(),
"Supplier name mismatch"
);
}
}
}