@Override public T get() { final Configuration configuration = clazz.getAnnotation(Configuration.class); try { final File baseFile = new File(configuration.filename()); final T baseConfig = baseFile.exists() ? objectMapper.readValue(baseFile, clazz) : clazz.newInstance(); final JsonNode overrideNode = filename.isPresent() ? loadYamlField(filename.get(), configuration.consolidatedField()) : objectMapper.createObjectNode(); final T config = objectMapper.readerForUpdating(baseConfig).readValue(overrideNode); final Set<ConstraintViolation<T>> violations = validator.validate(config); if (!violations.isEmpty()) { throw new ConfigurationValidationException(filename.or(configuration.filename()), violations); } return config; } catch (Exception e) { throw Throwables.propagate(e); } }
public static DdlConfiguration parseDdlConfiguration(File file) throws IOException, ConfigurationException { // Similar to Dropwizard's ConfigurationFactory but ignores System property overrides. ObjectMapper mapper = CustomJsonObjectMapperFactory.build(new YAMLFactory()); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true); DdlConfiguration ddlConfiguration = mapper.readValue(file, DdlConfiguration.class); Set<ConstraintViolation<DdlConfiguration>> errors = _validator.validate(ddlConfiguration); if (!errors.isEmpty()) { throw new ConfigurationValidationException(file.toString(), errors); } return ddlConfiguration; }
@Test(expected = ConfigurationValidationException.class) public void testThatInvalidConfigurationFailsToBeBuilt() throws Exception { buildConfigurationFromString("dropwizard/invalid-conf.yml"); }
@Test(expected = ConfigurationValidationException.class) public void testThatConfigurationWithEmptyValueFailsToBeBuilt() throws Exception { buildConfigurationFromString("dropwizard/empty-conf.yml"); }