MergingSourceProvider implements ConfigurationSourceProvider { @Override public InputStream open(String path) throws IOException { final JsonNode originalNode = objectMapper.readTree(yamlFactory.createParser(delegate.open(defaultConfigurationPath))); final JsonNode overrideNode = objectMapper.readTree(yamlFactory.createParser(delegate.open(path))); if (!(originalNode instanceof ObjectNode && overrideNode instanceof ObjectNode)) { throw new SingularityConfigurationMergeException(String.format("Both %s and %s need to be YAML objects", defaultConfigurationPath, path)); } merge((ObjectNode)originalNode, (ObjectNode)overrideNode); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); objectMapper.writeTree(yamlFactory.createGenerator(baos), originalNode); return new ByteArrayInputStream(baos.toByteArray()); } MergingSourceProvider(ConfigurationSourceProvider delegate, String defaultConfigurationPath, ObjectMapper objectMapper, YAMLFactory yamlFactory); @Override InputStream open(String path);  }
@Test public void testMergedConfigs() throws Exception { final InputStream mergedConfigStream = buildConfigurationSourceProvider(DEFAULT_PATH).open(OVERRIDE_PATH); final SingularityConfiguration mergedConfig = objectMapper.readValue(YAML_FACTORY.createParser(mergedConfigStream), SingularityConfiguration.class); assertEquals(10000, mergedConfig.getCacheTasksMaxSize()); assertEquals(500, mergedConfig.getCacheTasksInitialSize()); assertEquals(100, mergedConfig.getCheckDeploysEverySeconds()); assertEquals("baseuser", mergedConfig.getDatabaseConfiguration().get().getUser()); assertEquals("overridepassword", mergedConfig.getDatabaseConfiguration().get().getPassword()); }
@Test( expected = SingularityConfigurationMergeException.class ) public void testNonObjectFails() throws Exception { buildConfigurationSourceProvider(DEFAULT_PATH).open(JUST_A_STRING_PATH); }
@Test( expected = FileNotFoundException.class) public void testFileNoExistFail() throws Exception { buildConfigurationSourceProvider(DEFAULT_PATH).open(DOESNT_EXIST_PATH); buildConfigurationSourceProvider(DOESNT_EXIST_PATH).open(OVERRIDE_PATH); }