|
1 | 1 | package com.mindee.parsing.v2; |
2 | 2 |
|
| 3 | +import com.mindee.geometry.Point; |
| 4 | +import com.mindee.geometry.Polygon; |
3 | 5 | import com.mindee.input.LocalResponse; |
4 | | -import com.mindee.parsing.v2.field.*; |
| 6 | +import com.mindee.parsing.v2.field.DynamicField; |
| 7 | +import com.mindee.parsing.v2.field.FieldConfidence; |
| 8 | +import com.mindee.parsing.v2.field.FieldLocation; |
| 9 | +import com.mindee.parsing.v2.field.InferenceFields; |
| 10 | +import com.mindee.parsing.v2.field.SimpleField; |
| 11 | +import com.mindee.parsing.v2.field.ListField; |
| 12 | +import com.mindee.parsing.v2.field.ObjectField; |
5 | 13 | import com.mindee.parsing.v2.field.DynamicField.FieldType; |
6 | 14 | import java.io.IOException; |
7 | 15 | import java.util.List; |
@@ -62,21 +70,19 @@ void asyncPredict_whenEmpty_mustHaveValidProperties() throws IOException { |
62 | 70 | switch (type) { |
63 | 71 | case LIST_FIELD: |
64 | 72 | assertNotNull(value.getListField(), entry.getKey() + " – ListField expected"); |
65 | | - assertNull(value.getObjectField(), entry.getKey() + " – ObjectField must be null"); |
66 | | - assertNull(value.getSimpleField(), entry.getKey() + " – SimpleField must be null"); |
| 73 | + assertThrows(IllegalStateException.class, value::getSimpleField); |
| 74 | + assertThrows(IllegalStateException.class, value::getObjectField); |
67 | 75 | break; |
68 | | - |
69 | 76 | case OBJECT_FIELD: |
70 | 77 | assertNotNull(value.getObjectField(), entry.getKey() + " – ObjectField expected"); |
71 | | - assertNull(value.getListField(), entry.getKey() + " – ListField must be null"); |
72 | | - assertNull(value.getSimpleField(), entry.getKey() + " – SimpleField must be null"); |
| 78 | + assertThrows(IllegalStateException.class, value::getSimpleField); |
| 79 | + assertThrows(IllegalStateException.class, value::getListField); |
73 | 80 | break; |
74 | | - |
75 | 81 | case SIMPLE_FIELD: |
76 | 82 | default: |
77 | 83 | assertNotNull(value.getSimpleField(), entry.getKey() + " – SimpleField expected"); |
78 | | - assertNull(value.getListField(), entry.getKey() + " – ListField must be null"); |
79 | | - assertNull(value.getObjectField(), entry.getKey() + " – ObjectField must be null"); |
| 84 | + assertThrows(IllegalStateException.class, value::getListField); |
| 85 | + assertThrows(IllegalStateException.class, value::getObjectField); |
80 | 86 | break; |
81 | 87 | } |
82 | 88 | } |
@@ -263,6 +269,73 @@ void standardFieldTypes_mustExposeCorrectTypes() throws IOException { |
263 | 269 | } |
264 | 270 | } |
265 | 271 |
|
| 272 | + @Test |
| 273 | + @DisplayName("allow getting fields using generics") |
| 274 | + void standardFieldTypes_getWithGenerics() throws IOException { |
| 275 | + InferenceResponse response = loadFromResource("v2/inference/standard_field_types.json"); |
| 276 | + Inference inference = response.getInference(); |
| 277 | + assertNotNull(inference); |
| 278 | + InferenceFields fields = inference.getResult().getFields(); |
| 279 | + |
| 280 | + assertEquals( |
| 281 | + fields.get("field_simple_bool").getSimpleField(), |
| 282 | + fields.get("field_simple_bool").getField(SimpleField.class) |
| 283 | + ); |
| 284 | + assertEquals( |
| 285 | + fields.get("field_simple_bool").getSimpleField(), |
| 286 | + fields.getSimpleField("field_simple_bool") |
| 287 | + ); |
| 288 | + |
| 289 | + assertEquals( |
| 290 | + fields.get("field_simple_list").getListField(), |
| 291 | + fields.get("field_simple_list").getField(ListField.class) |
| 292 | + ); |
| 293 | + assertEquals( |
| 294 | + fields.get("field_simple_list").getListField(), |
| 295 | + fields.getListField("field_simple_list") |
| 296 | + ); |
| 297 | + |
| 298 | + assertEquals( |
| 299 | + fields.get("field_object").getObjectField(), |
| 300 | + fields.get("field_object").getField(ObjectField.class) |
| 301 | + ); |
| 302 | + assertEquals( |
| 303 | + fields.get("field_object").getObjectField(), |
| 304 | + fields.getObjectField("field_object") |
| 305 | + ); |
| 306 | + } |
| 307 | + |
| 308 | + @Test |
| 309 | + @DisplayName("confidence and locations must be usable") |
| 310 | + void standardFieldTypes_confidenceAndLocations() throws IOException { |
| 311 | + InferenceResponse response = loadFromResource("v2/inference/standard_field_types.json"); |
| 312 | + Inference inference = response.getInference(); |
| 313 | + assertNotNull(inference); |
| 314 | + |
| 315 | + InferenceFields fields = inference.getResult().getFields(); |
| 316 | + |
| 317 | + SimpleField fieldSimpleString = fields.get("field_simple_string").getField(SimpleField.class); |
| 318 | + FieldConfidence confidence = fieldSimpleString.getConfidence(); |
| 319 | + boolean isCertain = confidence == FieldConfidence.Certain; |
| 320 | + assertTrue(isCertain); |
| 321 | + |
| 322 | + List<FieldLocation> locations = fieldSimpleString.getLocations(); |
| 323 | + assertEquals(1, locations.size()); |
| 324 | + FieldLocation location = locations.get(0); |
| 325 | + |
| 326 | + Polygon polygon = location.getPolygon(); |
| 327 | + List<Point> coords = polygon.getCoordinates(); |
| 328 | + assertEquals(4, coords.size()); |
| 329 | + double topX = coords.get(0).getX(); |
| 330 | + assertEquals(0.0, topX); |
| 331 | + |
| 332 | + Point center = polygon.getCentroid(); |
| 333 | + assertEquals(0.5, center.getX(), 0.00001); |
| 334 | + |
| 335 | + int pageIndex = location.getPage(); |
| 336 | + assertEquals(0, pageIndex); |
| 337 | + } |
| 338 | + |
266 | 339 | @Nested |
267 | 340 | @DisplayName("raw_texts.json") |
268 | 341 | class RawTexts { |
|
0 commit comments