diff --git a/src/main/java/com/clivern/kangaroo/constraint/IntegerConstraint.java b/src/main/java/com/clivern/kangaroo/constraint/IntegerConstraint.java index bf591e6..bd8f3d2 100644 --- a/src/main/java/com/clivern/kangaroo/constraint/IntegerConstraint.java +++ b/src/main/java/com/clivern/kangaroo/constraint/IntegerConstraint.java @@ -57,7 +57,14 @@ public Boolean isValidType() { /** {@inheritDoc} */ public Boolean validate() { - return false; + Boolean status = true; + + if (!this.isValidType()) { + status &= false; + this.errors.add(String.format("Error! Field %s must be an integer.", this.fieldName)); + } + + return status; } /** {@inheritDoc} */ diff --git a/src/test/java/com/clivern/kangaroo/constraint/IntegerConstraintTest.java b/src/test/java/com/clivern/kangaroo/constraint/IntegerConstraintTest.java index 010f994..8f05913 100644 --- a/src/test/java/com/clivern/kangaroo/constraint/IntegerConstraintTest.java +++ b/src/test/java/com/clivern/kangaroo/constraint/IntegerConstraintTest.java @@ -24,26 +24,64 @@ public class IntegerConstraintTest { @Test public void testStringValidation() { - IntegerConstraint intConstraint = new IntegerConstraint(); + IntegerConstraint integerConstraint1 = new IntegerConstraint(); + integerConstraint1.setFieldName("fieldName"); + assertEquals(integerConstraint1.getFieldName(), "fieldName"); + integerConstraint1.setValue(1); + assertEquals(integerConstraint1.getValue(), new Integer("1")); + assertEquals(integerConstraint1.validate(), true); + assertEquals(integerConstraint1.hasErrors(), false); + assertEquals(integerConstraint1.getErrors(), new ArrayList()); - intConstraint.setValue(1); - assertEquals(intConstraint.getValue(), new Integer("1")); - assertEquals(intConstraint.isValidType(), true); + IntegerConstraint integerConstraint2 = new IntegerConstraint(); + integerConstraint2.setFieldName("fieldName"); + assertEquals(integerConstraint2.getFieldName(), "fieldName"); + integerConstraint2.setValue(-1); + assertEquals(integerConstraint2.getValue(), new Integer("-1")); + assertEquals(integerConstraint2.validate(), true); + assertEquals(integerConstraint2.hasErrors(), false); + assertEquals(integerConstraint2.getErrors(), new ArrayList()); - intConstraint.setValue(""); - assertEquals(intConstraint.getValue(), new Integer("0")); - assertEquals(intConstraint.isValidType(), false); - - intConstraint.setValue(null); - assertEquals(intConstraint.getValue(), new Integer("0")); - assertEquals(intConstraint.isValidType(), false); + IntegerConstraint integerConstraint3 = new IntegerConstraint(); + integerConstraint3.setFieldName("fieldName"); + assertEquals(integerConstraint3.getFieldName(), "fieldName"); + integerConstraint3.setValue(1.2); + assertEquals(integerConstraint3.getValue(), new Integer("0")); + assertEquals(integerConstraint3.validate(), false); + assertEquals(integerConstraint3.hasErrors(), true); + ArrayList errors = new ArrayList(); + errors.add( + String.format( + "Error! Field %s must be an integer.", integerConstraint3.getFieldName())); + assertEquals(integerConstraint3.getErrors(), errors); ArrayList list = new ArrayList(); list.add("A"); - list.add("B"); - list.add("C"); - intConstraint.setValue(list); - assertEquals(intConstraint.getValue(), new Integer("0")); - assertEquals(intConstraint.isValidType(), false); + IntegerConstraint integerConstraint4 = new IntegerConstraint(); + integerConstraint4.setFieldName("fieldName"); + assertEquals(integerConstraint4.getFieldName(), "fieldName"); + integerConstraint4.setValue(list); + assertEquals(integerConstraint4.getValue(), new Integer("0")); + assertEquals(integerConstraint4.validate(), false); + assertEquals(integerConstraint4.hasErrors(), true); + assertEquals(integerConstraint4.getErrors(), errors); + + IntegerConstraint integerConstraint5 = new IntegerConstraint(); + integerConstraint5.setFieldName("fieldName"); + assertEquals(integerConstraint5.getFieldName(), "fieldName"); + integerConstraint5.setValue(0); + assertEquals(integerConstraint5.getValue(), new Integer("0")); + assertEquals(integerConstraint5.validate(), true); + assertEquals(integerConstraint5.hasErrors(), false); + assertEquals(integerConstraint5.getErrors(), new ArrayList()); + + IntegerConstraint integerConstraint6 = new IntegerConstraint(); + integerConstraint6.setFieldName("fieldName"); + assertEquals(integerConstraint6.getFieldName(), "fieldName"); + integerConstraint6.setValue(100); + assertEquals(integerConstraint6.getValue(), new Integer("100")); + assertEquals(integerConstraint6.validate(), true); + assertEquals(integerConstraint6.hasErrors(), false); + assertEquals(integerConstraint6.getErrors(), new ArrayList()); } }