Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,10 @@ public String toString(int indent) {
} else if (fieldInstance.getType() == DynamicField.FieldType.OBJECT_FIELD) {
strBuilder.append(fieldInstance.getObjectField());
} else if (fieldInstance.getType() == DynamicField.FieldType.SIMPLE_FIELD) {
strBuilder
.append(
fieldInstance.getSimpleField().getValue() != null
? fieldInstance.getSimpleField().toString()
: ""
);

strBuilder.append(fieldInstance.getSimpleField());
}
joiner.add(strBuilder);
});

return SummaryHelper.cleanSummary(joiner.toString());
}

Expand Down
20 changes: 17 additions & 3 deletions src/main/java/com/mindee/parsing/v2/field/SimpleField.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.math.BigDecimal;
import java.util.List;
import lombok.EqualsAndHashCode;
import lombok.Getter;
Expand All @@ -28,7 +29,7 @@ public SimpleField(Object value, FieldConfidence confidence, List<FieldLocation>
}

/**
* Retrieves the value of the field as a string.
* Retrieves the value of the field as a {@link String}.
*
* @return the field value as a string
* @throws ClassCastException if the value cannot be cast to a string
Expand All @@ -38,7 +39,7 @@ public String getStringValue() throws ClassCastException {
}

/**
* Retrieves the value of the field as a Double.
* Retrieves the value of the field as a {@link Double}.
*
* @return the field value as a Double
* @throws ClassCastException if the value cannot be cast to a Double
Expand All @@ -48,7 +49,20 @@ public Double getDoubleValue() throws ClassCastException {
}

/**
* Retrieves the value of the field as a Boolean.
* Retrieves the value of the field as a {@link BigDecimal}.
*
* @return the field value as a BigDecimal
* @throws ClassCastException if the value cannot be cast to a BigDecimal
*/
public BigDecimal getBigDecimalValue() throws ClassCastException {
if (value == null) {
return null;
}
return BigDecimal.valueOf(getDoubleValue());
}

/**
* Retrieves the value of the field as a {@link Boolean}.
*
* @return the field value as a Boolean
* @throws ClassCastException if the value cannot be cast to a Boolean
Expand Down
65 changes: 37 additions & 28 deletions src/test/java/com/mindee/parsing/v2/InferenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.mindee.parsing.v2.field.ObjectField;
import com.mindee.parsing.v2.field.SimpleField;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -93,51 +94,52 @@ class CompletePredictionTest {
void asyncPredict_whenComplete_mustExposeAllProperties() throws IOException {
InferenceResponse response = loadInference("products/financial_document/complete.json");
Inference inference = response.getInference();
assertNotNull(inference, "Inference must not be null");
assertEquals(
"12345678-1234-1234-1234-123456789abc",
inference.getId(),
"Inference ID mismatch"
);
assertNotNull(inference);
assertEquals("12345678-1234-1234-1234-123456789abc", inference.getId());

InferenceModel model = inference.getModel();
assertNotNull(model, "Model must not be null");
assertEquals("12345678-1234-1234-1234-123456789abc", model.getId(), "Model ID mismatch");
assertEquals("12345678-1234-1234-1234-123456789abc", model.getId());

InferenceFile file = inference.getFile();
assertNotNull(file, "File must not be null");
assertEquals("complete.jpg", file.getName(), "File name mismatch");
assertEquals(1, file.getPageCount(), "Page count mismatch");
assertEquals("image/jpeg", file.getMimeType(), "MIME type mismatch");
assertNull(file.getAlias(), "File alias must be null for this payload");
assertNotNull(file);
assertEquals("complete.jpg", file.getName());
assertEquals(1, file.getPageCount());
assertEquals("image/jpeg", file.getMimeType());
assertNull(file.getAlias());

InferenceFields fields = inference.getResult().getFields();
assertEquals(21, fields.size(), "Expected 21 fields in the payload");
assertEquals(21, fields.size());

SimpleField date = fields.get("date").getSimpleField();
assertEquals("2019-11-02", date.getValue(), "'date' value mismatch");
assertEquals("2019-11-02", date.getValue());

// list of objects
DynamicField taxes = fields.get("taxes");
assertNotNull(taxes, "'taxes' field must exist");
assertNotNull(taxes);
ListField taxesList = taxes.getListField();
assertNotNull(taxesList, "'taxes' must be a ListField");
assertEquals(1, taxesList.getItems().size(), "'taxes' list must contain exactly one item");
assertNotNull(taxesList);
assertEquals(1, taxesList.getItems().size());
ObjectField taxItemObj = taxesList.getItems().get(0).getObjectField();
assertNotNull(taxItemObj, "First item of 'taxes' must be an ObjectField");
assertEquals(3, taxItemObj.getFields().size(), "Tax ObjectField must contain 3 sub-fields");
assertNotNull(taxItemObj);
assertEquals(3, taxItemObj.getFields().size());
SimpleField baseTax = taxItemObj.getFields().get("base").getSimpleField();
assertEquals(31.5, baseTax.getValue(), "'taxes.base' value mismatch");
assertNotNull(taxes.toString(), "'taxes'.toString() must not be null");
assertEquals(31.5, baseTax.getValue());
assertEquals(31.5, baseTax.getDoubleValue());
assertEquals(BigDecimal.valueOf(31.5), baseTax.getBigDecimalValue());
assertNotNull(taxes.toString());

// single object
DynamicField supplierAddress = fields.get("supplier_address");
assertNotNull(supplierAddress, "'supplier_address' field must exist");
assertNotNull(supplierAddress);
ObjectField supplierObj = supplierAddress.getObjectField();
assertNotNull(supplierObj, "'supplier_address' must be an ObjectField");
assertEquals(9, supplierObj.getFields().size());
assertNotNull(supplierObj);
DynamicField country = supplierObj.getFields().get("country");
assertNotNull(country, "'supplier_address.country' must exist");
assertEquals("USA", country.getSimpleField().getValue(), "Country mismatch");
assertEquals("USA", country.toString(), "'country'.toString() mismatch");
assertNotNull(supplierAddress.toString(), "'supplier_address'.toString() must not be null");
assertNotNull(country);
assertEquals("USA", country.getSimpleField().getValue());
assertEquals("USA", country.toString());
assertNotNull(supplierAddress.toString());

ObjectField customerAddr = fields.get("customer_address").getObjectField();
SimpleField city = customerAddr.getFields().get("city").getSimpleField();
Expand Down Expand Up @@ -214,7 +216,7 @@ void standardFieldTypes_mustExposeSimpleFieldValues() throws IOException {

assertNotNull(fields.get("field_simple_string").getSimpleField());

SimpleField fieldSimpleString = fields.get("field_simple_string").getSimpleField();
SimpleField fieldSimpleString = fields.getSimpleField("field_simple_string");
testSimpleFieldString(fieldSimpleString);
assertEquals(FieldConfidence.Certain, fieldSimpleString.getConfidence());
assertEquals(1, fieldSimpleString.getLocations().size());
Expand All @@ -232,25 +234,32 @@ void standardFieldTypes_mustExposeSimpleFieldValues() throws IOException {
assertInstanceOf(Double.class, fieldSimpleInt.getValue());
assertEquals(fieldSimpleInt.getValue(), fieldSimpleInt.getDoubleValue());
assertEquals(FieldConfidence.Medium, fieldSimpleInt.getConfidence());
assertThrows(ClassCastException.class, fieldSimpleInt::getStringValue);

SimpleField fieldSimpleZero = fields.get("field_simple_zero").getSimpleField();
assertNotNull(fieldSimpleZero);
assertEquals(FieldConfidence.Low, fieldSimpleZero.getConfidence());
assertInstanceOf(Double.class, fieldSimpleZero.getValue());
assertEquals(0.0, fieldSimpleZero.getDoubleValue());
assertEquals(BigDecimal.valueOf(0.0), fieldSimpleZero.getBigDecimalValue());
assertThrows(ClassCastException.class, fieldSimpleZero::getStringValue);
assertThrows(ClassCastException.class, fieldSimpleZero::getBooleanValue);

SimpleField fieldSimpleBool = fields.get("field_simple_bool").getSimpleField();
assertNotNull(fieldSimpleBool);
assertInstanceOf(Boolean.class, fieldSimpleBool.getValue());
assertEquals(fieldSimpleBool.getValue(), fieldSimpleBool.getBooleanValue());
assertThrows(ClassCastException.class, fieldSimpleBool::getStringValue);
assertThrows(ClassCastException.class, fieldSimpleBool::getDoubleValue);
assertThrows(ClassCastException.class, fieldSimpleBool::getBigDecimalValue);

SimpleField fieldSimpleNull = fields.get("field_simple_null").getSimpleField();
assertNotNull(fieldSimpleNull);
assertNull(fieldSimpleNull.getValue());
assertNull(fieldSimpleNull.getStringValue());
assertNull(fieldSimpleNull.getBooleanValue());
assertNull(fieldSimpleNull.getDoubleValue());
assertNull(fieldSimpleNull.getBigDecimalValue());
}

@Test
Expand Down
Loading