|
| 1 | +package com.mindee.v2; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import com.mindee.parsing.v2.DynamicField; |
| 5 | +import com.mindee.parsing.v2.DynamicField.FieldType; |
| 6 | +import com.mindee.parsing.v2.InferenceFields; |
| 7 | +import com.mindee.parsing.v2.InferenceResponse; |
| 8 | +import com.mindee.parsing.v2.ListField; |
| 9 | +import com.mindee.parsing.v2.ObjectField; |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.InputStream; |
| 12 | +import java.util.Map; |
| 13 | +import org.junit.jupiter.api.DisplayName; |
| 14 | +import org.junit.jupiter.api.Nested; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +import static org.junit.jupiter.api.Assertions.*; |
| 18 | + |
| 19 | +@DisplayName("InferenceV2 – field integrity checks") |
| 20 | +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 | + |
| 39 | + @Nested |
| 40 | + @DisplayName("When the async prediction is blank") |
| 41 | + class BlankPrediction { |
| 42 | + |
| 43 | + @Test |
| 44 | + @DisplayName("all properties must be valid") |
| 45 | + void asyncPredict_whenEmpty_mustHaveValidProperties() throws IOException { |
| 46 | + InferenceResponse response = loadPrediction("blank"); |
| 47 | + InferenceFields fields = response.getInference().getResult().getFields(); |
| 48 | + |
| 49 | + assertEquals(21, fields.size(), "Expected 21 fields"); |
| 50 | + |
| 51 | + /* taxes ----------------------------------------------------------------- */ |
| 52 | + DynamicField taxes = fields.get("taxes"); |
| 53 | + assertNotNull(taxes, "'taxes' field must exist"); |
| 54 | + ListField taxesList = taxes.getListField(); |
| 55 | + assertNotNull(taxesList, "'taxes' must be a ListField"); |
| 56 | + assertTrue(taxesList.getItems().isEmpty(), "'taxes' list must be empty"); |
| 57 | + |
| 58 | + /* supplier_address ------------------------------------------------------- */ |
| 59 | + DynamicField supplierAddress = fields.get("supplier_address"); |
| 60 | + assertNotNull(supplierAddress, "'supplier_address' field must exist"); |
| 61 | + ObjectField supplierObj = supplierAddress.getObjectField(); |
| 62 | + assertNotNull(supplierObj, "'supplier_address' must be an ObjectField"); |
| 63 | + |
| 64 | + /* generic checks --------------------------------------------------------- */ |
| 65 | + for (Map.Entry<String, DynamicField> entry : fields.entrySet()) { |
| 66 | + DynamicField value = entry.getValue(); |
| 67 | + if (value == null) { |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + FieldType type = value.getType(); |
| 72 | + switch (type) { |
| 73 | + case LIST_FIELD: |
| 74 | + assertNotNull(value.getListField(), entry.getKey() + " – ListField expected"); |
| 75 | + assertNull(value.getObjectField(), entry.getKey() + " – ObjectField must be null"); |
| 76 | + assertNull(value.getSimpleField(), entry.getKey() + " – SimpleField must be null"); |
| 77 | + break; |
| 78 | + |
| 79 | + case OBJECT_FIELD: |
| 80 | + assertNotNull(value.getObjectField(), entry.getKey() + " – ObjectField expected"); |
| 81 | + assertNull(value.getListField(), entry.getKey() + " – ListField must be null"); |
| 82 | + assertNull(value.getSimpleField(), entry.getKey() + " – SimpleField must be null"); |
| 83 | + break; |
| 84 | + |
| 85 | + default: // SimpleField (or any scalar) |
| 86 | + assertNotNull(value.getSimpleField(), entry.getKey() + " – SimpleField expected"); |
| 87 | + assertNull(value.getListField(), entry.getKey() + " – ListField must be null"); |
| 88 | + assertNull(value.getObjectField(), entry.getKey() + " – ObjectField must be null"); |
| 89 | + break; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Nested |
| 96 | + @DisplayName("When the async prediction is complete") |
| 97 | + class CompletePrediction { |
| 98 | + |
| 99 | + @Test |
| 100 | + @DisplayName("all properties must be valid") |
| 101 | + void asyncPredict_whenComplete_mustHaveValidProperties() throws IOException { |
| 102 | + InferenceResponse response = loadPrediction("complete"); |
| 103 | + InferenceFields fields = response.getInference().getResult().getFields(); |
| 104 | + |
| 105 | + assertEquals(21, fields.size(), "Expected 21 fields"); |
| 106 | + |
| 107 | + DynamicField taxes = fields.get("taxes"); |
| 108 | + assertNotNull(taxes, "'taxes' field must exist"); |
| 109 | + ListField taxesList = taxes.getListField(); |
| 110 | + assertNotNull(taxesList, "'taxes' must be a ListField"); |
| 111 | + assertEquals(1, taxesList.getItems().size(), "'taxes' list must contain exactly one item"); |
| 112 | + assertNotNull(taxes.toString(), "'taxes' toString() must not be null"); |
| 113 | + |
| 114 | + ObjectField taxItemObj = taxesList.getItems().get(0).getObjectField(); |
| 115 | + assertNotNull(taxItemObj, "First item of 'taxes' must be an ObjectField"); |
| 116 | + assertEquals(3, taxItemObj.getFields().size(), "Tax ObjectField must contain 3 sub-fields"); |
| 117 | + assertEquals( |
| 118 | + 31.5, |
| 119 | + taxItemObj.getFields().get("base").getSimpleField().getValue(), |
| 120 | + "'taxes.base' value mismatch" |
| 121 | + ); |
| 122 | + |
| 123 | + /* supplier_address ------------------------------------------------------- */ |
| 124 | + DynamicField supplierAddress = fields.get("supplier_address"); |
| 125 | + assertNotNull(supplierAddress, "'supplier_address' field must exist"); |
| 126 | + |
| 127 | + ObjectField supplierObj = supplierAddress.getObjectField(); |
| 128 | + assertNotNull(supplierObj, "'supplier_address' must be an ObjectField"); |
| 129 | + |
| 130 | + DynamicField country = supplierObj.getFields().get("country"); |
| 131 | + assertNotNull(country, "'supplier_address.country' must exist"); |
| 132 | + assertEquals("USA", country.getSimpleField().getValue()); |
| 133 | + assertEquals("USA", country.toString()); |
| 134 | + |
| 135 | + assertNotNull(supplierAddress.toString(), "'supplier_address'.toString() must not be null"); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments