MandatoryPathValidation implements Validation { @Override public ValidationResult validate(Config config) { if (config.hasPath(path)) { if (type != null) { if (config.getValue(path).valueType().equals(type) || ConfigUtils.canBeCoerced(config, path, type)) { return new ValidationResult(this, Validity.VALID, "Mandatory configuration exists and with the required type: " + path); } else { return new ValidationResult(this, Validity.INVALID, "Mandatory configuration '" + path + "' does not have required type: " + type); } } else { return new ValidationResult(this, Validity.VALID, "Mandatory configuration exists: " + path); } } else { return new ValidationResult(this, Validity.INVALID, "Mandatory configuration does not exist: " + path); } } MandatoryPathValidation(String path, ConfigValueType type);  MandatoryPathValidation(String path); @Override ValidationResult validate(Config config); @Override String toString(); @Override Set<String> getKnownPaths();  }
@Test public void testExistsWithoutSpecifiedType() { Validation v = new MandatoryPathValidation("hello"); ValidationResult vr = v.validate(ConfigFactory.parseString("hello = 2")); assertEquals(vr.getValidity(), Validity.VALID); }
@Test public void testExistsWithCorrectType() { Validation v = new MandatoryPathValidation("hello", ConfigValueType.NUMBER); ValidationResult vr = v.validate(ConfigFactory.parseString("hello = 2")); assertEquals(vr.getValidity(), Validity.VALID); }
@Test public void testExistsWithWrongType() { Validation v = new MandatoryPathValidation("hello", ConfigValueType.STRING); ValidationResult vr = v.validate(ConfigFactory.parseString("hello = 2")); assertEquals(vr.getValidity(), Validity.INVALID); }
@Test public void testDoesntExist() { Validation v = new MandatoryPathValidation("world"); ValidationResult vr = v.validate(ConfigFactory.parseString("hello = 2")); assertEquals(vr.getValidity(), Validity.INVALID); }