|
1 | 1 | package com.mindee.v2; |
2 | 2 |
|
3 | | -import com.fasterxml.jackson.databind.ObjectMapper; |
4 | 3 | import com.mindee.MindeeClientV2; |
5 | 4 | import com.mindee.input.LocalResponse; |
6 | | -import com.mindee.parsing.v2.DynamicField; |
| 5 | +import com.mindee.parsing.v2.*; |
7 | 6 | import com.mindee.parsing.v2.DynamicField.FieldType; |
8 | | -import com.mindee.parsing.v2.InferenceFields; |
9 | | -import com.mindee.parsing.v2.InferenceResponse; |
10 | | -import com.mindee.parsing.v2.ListField; |
11 | | -import com.mindee.parsing.v2.ObjectField; |
12 | | - |
13 | | -import java.io.File; |
14 | 7 | import java.io.IOException; |
15 | | -import java.io.InputStream; |
| 8 | +import java.util.List; |
16 | 9 | import java.util.Map; |
17 | 10 | import org.junit.jupiter.api.DisplayName; |
18 | 11 | import org.junit.jupiter.api.Nested; |
|
22 | 15 |
|
23 | 16 | @DisplayName("InferenceV2 – field integrity checks") |
24 | 17 | class InferenceTest { |
| 18 | + |
| 19 | + private InferenceResponse loadFromResource(String resourcePath) throws IOException { |
| 20 | + MindeeClientV2 dummyClient = new MindeeClientV2("dummy"); |
| 21 | + return dummyClient.loadInference(new LocalResponse(InferenceTest.class.getClassLoader().getResourceAsStream(resourcePath))); |
| 22 | + } |
| 23 | + |
| 24 | + |
25 | 25 | @Nested |
26 | 26 | @DisplayName("When the async prediction is blank") |
27 | 27 | class BlankPrediction { |
28 | 28 |
|
29 | 29 | @Test |
30 | 30 | @DisplayName("all properties must be valid") |
31 | 31 | void asyncPredict_whenEmpty_mustHaveValidProperties() throws IOException { |
32 | | - MindeeClientV2 mindeeClient = new MindeeClientV2("dummy"); |
33 | | - InferenceResponse response = mindeeClient.loadInference(new LocalResponse(InferenceTest.class.getClassLoader().getResourceAsStream("v2/products/financial_document/blank.json"))); |
| 32 | + InferenceResponse response = loadFromResource("v2/products/financial_document/blank.json"); |
34 | 33 | InferenceFields fields = response.getInference().getResult().getFields(); |
35 | 34 |
|
36 | 35 | assertEquals(21, fields.size(), "Expected 21 fields"); |
@@ -84,8 +83,7 @@ class CompletePrediction { |
84 | 83 | @Test |
85 | 84 | @DisplayName("all properties must be valid") |
86 | 85 | void asyncPredict_whenComplete_mustHaveValidProperties() throws IOException { |
87 | | - MindeeClientV2 mindeeClient = new MindeeClientV2("dummy"); |
88 | | - InferenceResponse response = mindeeClient.loadInference(new LocalResponse(InferenceTest.class.getClassLoader().getResourceAsStream("v2/products/financial_document/complete.json"))); |
| 86 | + InferenceResponse response = loadFromResource("v2/products/financial_document/complete.json"); |
89 | 87 | InferenceFields fields = response.getInference().getResult().getFields(); |
90 | 88 |
|
91 | 89 | assertEquals(21, fields.size(), "Expected 21 fields"); |
@@ -120,4 +118,121 @@ void asyncPredict_whenComplete_mustHaveValidProperties() throws IOException { |
120 | 118 | assertNotNull(supplierAddress.toString(), "'supplier_address'.toString() must not be null"); |
121 | 119 | } |
122 | 120 | } |
| 121 | + |
| 122 | + @Nested |
| 123 | + @DisplayName("deep_nested_fields.json") |
| 124 | + class DeepNestedFields { |
| 125 | + |
| 126 | + @Test |
| 127 | + @DisplayName("all nested structures must be typed correctly") |
| 128 | + void deepNestedFields_mustExposeCorrectTypes() throws IOException { |
| 129 | + InferenceResponse resp = loadFromResource("v2/inference/deep_nested_fields.json"); |
| 130 | + Inference inf = resp.getInference(); |
| 131 | + assertNotNull(inf); |
| 132 | + |
| 133 | + InferenceFields root = inf.getResult().getFields(); |
| 134 | + assertNotNull(root.get("field_simple").getSimpleField()); |
| 135 | + assertNotNull(root.get("field_object").getObjectField()); |
| 136 | + |
| 137 | + ObjectField fieldObject = root.get("field_object").getObjectField(); |
| 138 | + InferenceFields lvl1 = fieldObject.getFields(); |
| 139 | + assertNotNull(lvl1.get("sub_object_list").getListField()); |
| 140 | + assertNotNull(lvl1.get("sub_object_object").getObjectField()); |
| 141 | + |
| 142 | + ObjectField subObjectObject = lvl1.get("sub_object_object").getObjectField(); |
| 143 | + InferenceFields lvl2 = subObjectObject.getFields(); |
| 144 | + assertNotNull(lvl2.get("sub_object_object_sub_object_list").getListField()); |
| 145 | + |
| 146 | + ListField nestedList = lvl2.get("sub_object_object_sub_object_list").getListField(); |
| 147 | + List<DynamicField> items = nestedList.getItems(); |
| 148 | + assertFalse(items.isEmpty()); |
| 149 | + assertNotNull(items.get(0).getObjectField()); |
| 150 | + |
| 151 | + ObjectField firstItem = items.get(0).getObjectField(); |
| 152 | + SimpleField deepSimple = firstItem.getFields() |
| 153 | + .get("sub_object_object_sub_object_list_simple").getSimpleField(); |
| 154 | + assertEquals("value_9", deepSimple.getValue()); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + @Nested |
| 159 | + @DisplayName("standard_field_types.json") |
| 160 | + class StandardFieldTypes { |
| 161 | + |
| 162 | + @Test |
| 163 | + @DisplayName("simple / object / list variants must be recognised") |
| 164 | + void standardFieldTypes_mustExposeCorrectTypes() throws IOException { |
| 165 | + InferenceResponse resp = loadFromResource("v2/inference/standard_field_types.json"); |
| 166 | + Inference inf = resp.getInference(); |
| 167 | + assertNotNull(inf); |
| 168 | + |
| 169 | + InferenceFields root = inf.getResult().getFields(); |
| 170 | + assertNotNull(root.get("field_simple").getSimpleField()); |
| 171 | + assertNotNull(root.get("field_object").getObjectField()); |
| 172 | + assertNotNull(root.get("field_simple_list").getListField()); |
| 173 | + assertNotNull(root.get("field_object_list").getListField()); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + @Nested |
| 178 | + @DisplayName("raw_texts.json") |
| 179 | + class RawTexts { |
| 180 | + |
| 181 | + @Test |
| 182 | + @DisplayName("raw texts option must be parsed and exposed") |
| 183 | + void rawTexts_mustBeAccessible() throws IOException { |
| 184 | + InferenceResponse resp = loadFromResource("v2/inference/raw_texts.json"); |
| 185 | + Inference inf = resp.getInference(); |
| 186 | + assertNotNull(inf); |
| 187 | + |
| 188 | + InferenceOptions opts = inf.getResult().getOptions(); |
| 189 | + assertNotNull(opts, "Options should not be null"); |
| 190 | + |
| 191 | + List<RawText> rawTexts = opts.getRawTexts(); |
| 192 | + assertEquals(2, rawTexts.size()); |
| 193 | + |
| 194 | + RawText first = rawTexts.get(0); |
| 195 | + assertEquals(0, first.getPage()); |
| 196 | + assertEquals("This is the raw text of the first page...", first.getContent()); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + @Nested |
| 201 | + @DisplayName("complete.json – full inference response") |
| 202 | + class FullInference { |
| 203 | + @Test |
| 204 | + @DisplayName("complete financial-document JSON must round-trip correctly") |
| 205 | + void fullInferenceResponse_mustExposeEveryProperty() throws IOException { |
| 206 | + InferenceResponse resp = loadFromResource("v2/products/financial_document/complete.json"); |
| 207 | + |
| 208 | + Inference inf = resp.getInference(); |
| 209 | + assertNotNull(inf); |
| 210 | + assertEquals("12345678-1234-1234-1234-123456789abc", inf.getId()); |
| 211 | + |
| 212 | + InferenceFields f = inf.getResult().getFields(); |
| 213 | + |
| 214 | + SimpleField date = f.get("date").getSimpleField(); |
| 215 | + assertEquals("2019-11-02", date.getValue()); |
| 216 | + |
| 217 | + ListField taxes = f.get("taxes").getListField(); |
| 218 | + ObjectField firstTax = taxes.getItems().get(0).getObjectField(); |
| 219 | + SimpleField baseTax = firstTax.getFields().get("base").getSimpleField(); |
| 220 | + assertEquals(31.5, baseTax.getValue()); |
| 221 | + |
| 222 | + ObjectField customerAddr = f.get("customer_address").getObjectField(); |
| 223 | + SimpleField city = customerAddr.getFields().get("city").getSimpleField(); |
| 224 | + assertEquals("New York", city.getValue()); |
| 225 | + |
| 226 | + InferenceModel model = inf.getModel(); |
| 227 | + assertNotNull(model); |
| 228 | + assertEquals("12345678-1234-1234-1234-123456789abc", model.getId()); |
| 229 | + |
| 230 | + InferenceFile file = inf.getFile(); |
| 231 | + assertNotNull(file); |
| 232 | + assertEquals("complete.jpg", file.getName()); |
| 233 | + assertNull(file.getAlias()); |
| 234 | + |
| 235 | + assertNull(inf.getResult().getOptions()); |
| 236 | + } |
| 237 | + } |
123 | 238 | } |
0 commit comments