Skip to content

Commit

Permalink
Merge pull request #18 from Clivern/feature/integer-constraint
Browse files Browse the repository at this point in the history
Add Integer Constraint
  • Loading branch information
Clivern authored Oct 27, 2019
2 parents 95a706c + d4fc403 commit e7a220e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>());

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<String>());

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<String> errors = new ArrayList<String>();
errors.add(
String.format(
"Error! Field %s must be an integer.", integerConstraint3.getFieldName()));
assertEquals(integerConstraint3.getErrors(), errors);

ArrayList<String> list = new ArrayList<String>();
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<String>());

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<String>());
}
}

0 comments on commit e7a220e

Please sign in to comment.