@Test public void testCheckConstraint_WithEnum_ButFieldValueNotInSet() { final String name = "Fake Name"; Enum e = new Enum("Fake Enum"); ConstraintValidator validator = mock(ConstraintValidator.class); mockEnum(validator, e); EnumConstraint constraint = new EnumConstraint(); constraint.setName(name); JsonNode fieldValue = mock(JsonNode.class); when(fieldValue.asText()).thenReturn(name); new EnumChecker().checkConstraint(validator, null, null, constraint, null, null, fieldValue); verify(validator, times(1)).addDocError(any(Error.class)); }
@Override public void checkConstraint(ConstraintValidator validator, FieldTreeNode fieldMetadata, Path fieldMetadataPath, FieldConstraint constraint, Path valuePath, JsonDoc doc, JsonNode fieldValue) { if (fieldValue != null && !(fieldValue instanceof NullNode)) { String name = ((EnumConstraint) constraint).getName(); Set<String> values = null; if (name != null) { Enum e = validator.getEntityMetadata().getEntityInfo().getEnums().getEnum(name); if (e != null) { values = e.getValues(); } } if (null == values || !values.contains(fieldValue.asText())) { validator.addDocError(Error.get(CrudConstants.ERR_INVALID_ENUM, fieldValue.asText())); } } }
EnumChecker implements FieldConstraintValueChecker { @Override public void checkConstraint(ConstraintValidator validator, FieldTreeNode fieldMetadata, Path fieldMetadataPath, FieldConstraint constraint, Path valuePath, JsonDoc doc, JsonNode fieldValue) { if (fieldValue != null && !(fieldValue instanceof NullNode)) { String name = ((EnumConstraint) constraint).getName(); Set<String> values = null; if (name != null) { Enum e = validator.getEntityMetadata().getEntityInfo().getEnums().getEnum(name); if (e != null) { values = e.getValues(); } } if (null == values || !values.contains(fieldValue.asText())) { validator.addDocError(Error.get(CrudConstants.ERR_INVALID_ENUM, fieldValue.asText())); } } } }
EnumChecker implements FieldConstraintValueChecker { @Override public void checkConstraint(ConstraintValidator validator, FieldTreeNode fieldMetadata, Path fieldMetadataPath, FieldConstraint constraint, Path valuePath, JsonDoc doc, JsonNode fieldValue) { if (fieldValue != null && !(fieldValue instanceof NullNode)) { String name = ((EnumConstraint) constraint).getName(); Set<String> values = null; if (name != null) { Enum e = validator.getEntityMetadata().getEntityInfo().getEnums().getEnum(name); if (e != null) { values = e.getValues(); } } if (null == values || !values.contains(fieldValue.asText())) { validator.addDocError(Error.get(CrudConstants.ERR_INVALID_ENUM, fieldValue.asText())); } } }  }
EnumChecker implements FieldConstraintValueChecker { @Override public void checkConstraint(ConstraintValidator validator, FieldTreeNode fieldMetadata, Path fieldMetadataPath, FieldConstraint constraint, Path valuePath, JsonDoc doc, JsonNode fieldValue) { if (fieldValue != null && !(fieldValue instanceof NullNode)) { String name = ((EnumConstraint) constraint).getName(); Set<String> values = null; if (name != null) { Enum e = validator.getEntityMetadata().getEntityInfo().getEnums().getEnum(name); if (e != null) { values = e.getValues(); } } if (null == values || !values.contains(fieldValue.asText())) { validator.addDocError(Error.get(CrudConstants.ERR_INVALID_ENUM, fieldValue.asText())); } } }  @Override void checkConstraint(ConstraintValidator validator,
                                FieldTreeNode fieldMetadata,
                                Path fieldMetadataPath,
                                FieldConstraint constraint,
                                Path valuePath,
                                JsonDoc doc,
                                JsonNode fieldValue); }
EnumChecker implements FieldConstraintValueChecker { @Override public void checkConstraint(ConstraintValidator validator, FieldTreeNode fieldMetadata, Path fieldMetadataPath, FieldConstraint constraint, Path valuePath, JsonDoc doc, JsonNode fieldValue) { if (fieldValue != null && !(fieldValue instanceof NullNode)) { String name = ((EnumConstraint) constraint).getName(); Set<String> values = null; if (name != null) { Enum e = validator.getEntityMetadata().getEntityInfo().getEnums().getEnum(name); if (e != null) { values = e.getValues(); } } if (null == values || !values.contains(fieldValue.asText())) { validator.addDocError(Error.get(CrudConstants.ERR_INVALID_ENUM, fieldValue.asText())); } } }  @Override void checkConstraint(ConstraintValidator validator,
                                FieldTreeNode fieldMetadata,
                                Path fieldMetadataPath,
                                FieldConstraint constraint,
                                Path valuePath,
                                JsonDoc doc,
                                JsonNode fieldValue);  }