YamlBasedPropertiesProvider extends FormatBasedPropertiesProvider { @Override public Properties getProperties(InputStream inputStream) { requireNonNull(inputStream); Yaml yaml = new Yaml(); Properties properties = new Properties(); try (Reader reader = new UnicodeReader(inputStream)) { Object object = yaml.load(reader); if (object != null) { Map<String, Object> yamlAsMap = convertToMap(object); properties.putAll(flatten(yamlAsMap)); } return properties; } catch (IOException | ScannerException e) { throw new IllegalStateException("Unable to load yaml configuration from provided stream", e); } }  @Override Properties getProperties(InputStream inputStream);  }
@Test void readsSingleValues() throws Exception { String path = "org/cfg4j/source/propertiesprovider/YamlBasedPropertiesProviderTest_readsSingleValues.yaml"; try (InputStream input = getClass().getClassLoader().getResourceAsStream(path)) { assertThat(provider.getProperties(input)).containsOnly(MapEntry.entry("setting", "masterValue"), MapEntry.entry("integerSetting", 42)); } }
@Test void readsNestedValues() throws Exception { String path = "org/cfg4j/source/propertiesprovider/YamlBasedPropertiesProviderTest_readsNestedValues.yaml"; try (InputStream input = getClass().getClassLoader().getResourceAsStream(path)) { assertThat(provider.getProperties(input)).containsOnly(MapEntry.entry("some.setting", "masterValue"), MapEntry.entry("some.integerSetting", 42)); } }
@Test void readsLists() throws Exception { String path = "org/cfg4j/source/propertiesprovider/YamlBasedPropertiesProviderTest_readsLists.yaml"; try (InputStream input = getClass().getClassLoader().getResourceAsStream(path)) { assertThat(provider.getProperties(input)).containsOnly(MapEntry.entry("whitelist", "a,b,33"), MapEntry.entry("blacklist", "x,y,z")); } }
@Test void readsTextBlock() throws Exception { String path = "org/cfg4j/source/propertiesprovider/YamlBasedPropertiesProviderTest_readsTextBlock.yaml"; try (InputStream input = getClass().getClassLoader().getResourceAsStream(path)) { assertThat(provider.getProperties(input)).containsExactly(MapEntry.entry("content", "I'm just a text block document")); } }
@Test void supportsReferences() throws Exception { String path = "org/cfg4j/source/propertiesprovider/YamlBasedPropertiesProviderTest_supportsReferences.yaml"; try (InputStream input = getClass().getClassLoader().getResourceAsStream(path)) { assertThat(provider.getProperties(input)).containsOnly( MapEntry.entry("wheelA.radius", "25cm"), MapEntry.entry("wheelA.color", "black"), MapEntry.entry("wheelB.radius", "25cm"), MapEntry.entry("wheelB.color", "black") ); } }
@Test void throwsForNonYamlFile() throws Exception { String path = "org/cfg4j/source/propertiesprovider/YamlBasedPropertiesProviderTest_throwsForNonYamlFile.yaml"; try (InputStream input = getClass().getClassLoader().getResourceAsStream(path)) { assertThatThrownBy(() -> provider.getProperties(input)).isExactlyInstanceOf(IllegalStateException.class); } }
@Test void supportsEmptyDocument() throws Exception { String path = "org/cfg4j/source/propertiesprovider/YamlBasedPropertiesProviderTest_supportsEmptyDocument.yaml"; try (InputStream input = getClass().getClassLoader().getResourceAsStream(path)) { assertThat(provider.getProperties(input)).isEmpty(); } }
@Test void throwsOnNullInput() throws Exception { String path = "org/cfg4j/source/propertiesprovider/nonexistent.json"; try (InputStream input = getClass().getClassLoader().getResourceAsStream(path)) { assertThatThrownBy(() -> provider.getProperties(input)).isExactlyInstanceOf(NullPointerException.class); } }