@Override public RestlerConfig getConfiguration() { ObjectMapper objectMapper = Jackson.newObjectMapper(); ValidatorFactory validatorFactory = Validation .byProvider(HibernateValidator.class) .configure() .addValidatedValueHandler(new OptionalValidatedValueUnwrapper()) .buildValidatorFactory(); final ConfigurationFactory<RestlerConfig> configurationFactory = new DefaultConfigurationFactoryFactory<RestlerConfig>().create(RestlerConfig.class, validatorFactory.getValidator(), objectMapper, "dw"); try { return configurationFactory.build(new FileConfigurationSourceProvider(), TEST_CONFIG_FILE); } catch (Exception e) { throw new RuntimeException("Cannot get test configuration", e); } }
/** * Parses the given configuration file and returns a configuration object. * * @param configurationFileName The name of the configuration file. * @return A configuration object. * @throws IOException The IO error that contains detail information. * @throws ConfigurationException The configuration error that contains detail information. */ public static ApiConfiguration parse(String configurationFileName) throws IOException, ConfigurationException { if (StringUtils.isBlank(configurationFileName)) { throw new IllegalArgumentException("Configuration file cannot be blank"); } ObjectMapper objectMapper = null; if (configurationFileName.endsWith("yml") || configurationFileName.endsWith("yaml")) { objectMapper = Jackson.newObjectMapper(new YAMLFactory()); } else if (configurationFileName.endsWith("json")) { objectMapper = Jackson.newObjectMapper(new JsonFactory()); } else { throw new IllegalArgumentException("Unrecognized configuration file type"); } ValidatorFactory validatorFactory = Validation .byProvider(HibernateValidator.class) .configure() .addValidatedValueHandler(new OptionalValidatedValueUnwrapper()) .buildValidatorFactory(); final ConfigurationFactory<ApiConfiguration> configurationFactory = new DefaultConfigurationFactoryFactory<ApiConfiguration>() .create(ApiConfiguration.class, validatorFactory.getValidator(), objectMapper, "dw"); final File file = new File(configurationFileName); if (!file.exists()) { throw new FileNotFoundException("Configuration file " + configurationFileName + " not found"); } return configurationFactory.build(file); }