AllowedValuesValidation implements Validation { @Override public ValidationResult validate(Config config) { if (config.hasPath(path)) { if (allowedValues.contains(config.getAnyRef(path))) { return new ValidationResult(this, Validity.VALID, "Configuration '" + path + "' has one of the allowed values: " + allowedValues); } else { return new ValidationResult(this, Validity.INVALID, "Configuration '" + path + "' does not have one of the allowed values: " + allowedValues); } } else { return new ValidationResult(this, Validity.VALID, "Configuration '" + path + "' does not exist, so will not check it for allowed values"); } } AllowedValuesValidation(String path, Object... allowedValues); @Override ValidationResult validate(Config config); @Override String toString(); @Override Set<String> getKnownPaths();  }
@Test public void testValid() { AllowedValuesValidation v = new AllowedValuesValidation("hello", 1, 2, 3); ValidationResult vr = v.validate(ConfigFactory.parseString("hello = 2")); assertEquals(vr.getValidity(), Validity.VALID); }
@Test public void testPathDoesntExist() { AllowedValuesValidation v = new AllowedValuesValidation("hello", 1, 2, 3); ValidationResult vr = v.validate(ConfigFactory.parseString("world = 2")); assertEquals(vr.getValidity(), Validity.VALID); }
@Test public void testInvalid() { AllowedValuesValidation v = new AllowedValuesValidation("hello", 1, 2, 3); ValidationResult vr = v.validate(ConfigFactory.parseString("hello = 4")); assertEquals(vr.getValidity(), Validity.INVALID); }