### Question:
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);  }### Answer:
@Test public void testCheckConstraint_WithEnum_FieldValueIsInSet() { final String name = "Fake Name"; Enum e = new Enum(name); e.setValues(new HashSet<>(Arrays.asList(new EnumValue(name, null)))); 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, never()).addDocError(any(Error.class)); }
@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)); }
@Test public void testCheckConstraint_NullEnum() { String name = "Fake Name"; ConstraintValidator validator = mock(ConstraintValidator.class); mockEnum(validator, null); EnumConstraint constraint = new EnumConstraint(); constraint.setName(name); JsonNode fieldValue = mock(JsonNode.class); new EnumChecker().checkConstraint(validator, null, null, constraint, null, null, fieldValue); verify(validator, times(1)).addDocError(any(Error.class)); }
@Test public void testCheckConstraint_NullName() { ConstraintValidator validator = mock(ConstraintValidator.class); EnumConstraint constraint = new EnumConstraint(); constraint.setName(null); JsonNode fieldValue = mock(JsonNode.class); when(fieldValue.asText()).thenReturn("String value"); new EnumChecker().checkConstraint(validator, null, null, constraint, null, null, fieldValue); verify(validator, times(1)).addDocError(any(Error.class)); }