MergingConfigProvider implements ConfigurationSourceProvider { @Override public InputStream open(String path) throws IOException { JsonNode originalNode = readFromPath(defaultConfigPath); JsonNode overrideNode = readFromPath(path); if (originalNode.isObject() && overrideNode.isObject()) { ObjectNode original = (ObjectNode) originalNode; ObjectNode override = (ObjectNode) overrideNode; merge(original, override); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); objectMapper.writeTree(yamlFactory.createGenerator(outputStream), original); return new ByteArrayInputStream(outputStream.toByteArray()); } else { throw new IllegalArgumentException("Both configuration files must be objects"); } } MergingConfigProvider(ConfigurationSourceProvider delegate, String defaultConfigPath, ObjectMapper objectMapper, YAMLFactory yamlFactory); @Override InputStream open(String path);  }
@Test public void itMergesNestedConfigsProperly() throws Exception { InputStream mergedConfigStream = buildConfigSourceProvider(DEFAULT_PATH) .open(OVERRIDE_PATH); BaragonConfiguration mergedConfig = objectMapper.readValue( YAML_FACTORY.createParser(mergedConfigStream), BaragonConfiguration.class); assertEquals("override-quorum", mergedConfig.getZooKeeperConfiguration().getQuorum()); assertEquals(100, (int) mergedConfig.getZooKeeperConfiguration().getSessionTimeoutMillis()); assertEquals("override-namespace", mergedConfig.getZooKeeperConfiguration().getZkNamespace()); assertEquals(100, (int) mergedConfig.getZooKeeperConfiguration().getConnectTimeoutMillis()); assertEquals(100, (int) mergedConfig.getZooKeeperConfiguration().getRetryBaseSleepTimeMilliseconds()); assertEquals(2, mergedConfig.getAgentMaxAttempts()); assertEquals("127.0.0.1", mergedConfig.getHostname()); assertEquals("master-auth-key", mergedConfig.getMasterAuthKey()); }
@Test public void itThrowsExnOnNonObjectOverride() throws Exception { Assertions.assertThrows(IllegalArgumentException.class, () -> buildConfigSourceProvider(DEFAULT_PATH) .open(JUST_A_STRING_PATH) ); }
@Test public void itThrowsExnOnNonObjectDefault() throws Exception { Assertions.assertThrows(IllegalArgumentException.class, () -> buildConfigSourceProvider(JUST_A_STRING_PATH) .open(OVERRIDE_PATH)); }
@Test public void itThrowsExnOnNotFoundOverridePath() throws Exception { Assertions.assertThrows(FileNotFoundException.class, () -> buildConfigSourceProvider(DEFAULT_PATH) .open(DOESNT_EXIST_PATH)); }
@Test public void itThrowsExnOnNotFoundDefaultPath() throws Exception { Assertions.assertThrows(FileNotFoundException.class, () -> buildConfigSourceProvider(DOESNT_EXIST_PATH) .open(OVERRIDE_PATH)); }