IfPathHasValueValidation implements Validation { @Override public ValidationResult validate(Config config) { if (config.hasPath(ifPath)) { if (config.getAnyRef(ifPath).equals(hasValue)) { return thenValidation.validate(config); } else { return new ValidationResult(this, Validity.VALID, "Conditional configuration '" + ifPath + "' did not have value '" + hasValue + "', so it was not required that the validation (" + thenValidation + ") be checked"); } } else { return new ValidationResult(this, Validity.VALID, "Conditional configuration '" + ifPath + "' does not exist, so it was not required that the validation (" + thenValidation + ") be checked"); } } IfPathHasValueValidation(String ifPath, Object hasValue, Validation thenValidation); @Override ValidationResult validate(Config config); @Override String toString(); @Override Set<String> getKnownPaths();  }
@Test public void testValidWhenPathHasValue() { Validation v = new IfPathHasValueValidation("hello", 2, Validations.single().allowedValues("hello", 1, 2, 3)); ValidationResult vr = v.validate(ConfigFactory.parseString("hello = 2")); assertEquals(vr.getValidity(), Validity.VALID); }
@Test public void testInvalidWhenPathHasValue() { Validation v = new IfPathHasValueValidation("hello", 2, Validations.single().allowedValues("hello", 3, 4, 5)); ValidationResult vr = v.validate(ConfigFactory.parseString("hello = 2")); assertEquals(vr.getValidity(), Validity.INVALID); }
@Test public void testPathDoesNotHaveValue() { Validation v = new IfPathHasValueValidation("hello", 4, Validations.single().allowedValues("hello", 1, 2, 3)); ValidationResult vr = v.validate(ConfigFactory.parseString("hello = 2")); assertEquals(vr.getValidity(), Validity.VALID); }
@Test public void testPathDoesNotExist() { Validation v = new IfPathHasValueValidation("hello", 4, Validations.single().allowedValues("hello", 1, 2, 3)); ValidationResult vr = v.validate(ConfigFactory.parseString("world = 2")); assertEquals(vr.getValidity(), Validity.VALID); }