use of org.yaml.snakeyaml.Yaml in project incubator-heron by apache.
the class ConfigUtilsTests method testStateManagerFileOverrides.
@Test
@SuppressWarnings("unchecked")
public void testStateManagerFileOverrides() throws IOException {
final Properties overrideProperties = createOverrideProperties(Pair.create("heron.statemgr.connection.string", "zookeeper:2181"), Pair.create("heron.kubernetes.scheduler.uri", "http://127.0.0.1:8001"));
final String overridesPath = ConfigUtils.createOverrideConfiguration(overrideProperties);
// write default state manager config
final Path stateManagerPath = Files.createTempFile("statemgr-", ".yaml");
stateManagerPath.toFile().deleteOnExit();
try (Writer writer = Files.newBufferedWriter(stateManagerPath)) {
final Map<String, String> config = new HashMap<>();
config.put("heron.statemgr.connection.string", "<host>:<port>");
new Yaml().dump(config, writer);
}
// apply the overrides
ConfigUtils.applyOverridesToStateManagerConfig(Paths.get(overridesPath), stateManagerPath);
try (Reader reader = Files.newBufferedReader(stateManagerPath)) {
final Map<String, Object> stateManagerWithOverrides = (Map<String, Object>) new Yaml().loadAs(reader, Map.class);
assertEquals(stateManagerWithOverrides.size(), 1);
assertEquals(stateManagerWithOverrides.get("heron.statemgr.connection.string"), "zookeeper:2181");
}
}
use of org.yaml.snakeyaml.Yaml in project incubator-heron by apache.
the class ConfigUtilsTests method testApplyOverrides.
@Test
@SuppressWarnings("unchecked")
public void testApplyOverrides() throws IOException {
final Properties overrideProperties = createOverrideProperties(Pair.create("heron.statemgr.connection.string", "zookeeper:2181"), Pair.create("heron.kubernetes.scheduler.uri", "http://127.0.0.1:8001"));
final String overridesPath = ConfigUtils.createOverrideConfiguration(overrideProperties);
final Map<String, String> overrides = createOverrides(Pair.create("my.override.key", "my.override.value"));
ConfigUtils.applyOverrides(Paths.get(overridesPath), overrides);
final Map<String, String> combinedOverrides = new HashMap<>();
combinedOverrides.putAll(overrides);
for (String key : overrideProperties.stringPropertyNames()) {
combinedOverrides.put(key, overrideProperties.getProperty(key));
}
try (Reader reader = Files.newBufferedReader(Paths.get(overridesPath))) {
final Map<String, Object> newOverrides = (Map<String, Object>) new Yaml().loadAs(reader, Map.class);
assertEquals(newOverrides, combinedOverrides);
}
}
use of org.yaml.snakeyaml.Yaml in project incubator-heron by apache.
the class ConfigUtils method newYaml.
private static Yaml newYaml() {
final DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setPrettyFlow(true);
return new Yaml(options);
}
use of org.yaml.snakeyaml.Yaml in project serverless by bluenimble.
the class SpecUtils method y2j.
public static void y2j(File fileOrFolder, boolean deleteSource) throws Exception {
if (!fileOrFolder.exists()) {
return;
}
if (fileOrFolder.isDirectory()) {
File[] files = fileOrFolder.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return file.isDirectory() || file.getName().endsWith(".yaml");
}
});
if (files == null || files.length == 0) {
return;
}
for (File f : files) {
y2j(f, deleteSource);
}
} else {
File jsonFile = new File(fileOrFolder.getParentFile(), fileOrFolder.getName().substring(0, fileOrFolder.getName().lastIndexOf(Lang.DOT)) + ".json");
Yaml yaml = new Yaml();
InputStream is = null;
try {
is = new FileInputStream(fileOrFolder);
@SuppressWarnings("unchecked") JsonObject o = new JsonObject(yaml.loadAs(is, Map.class), true);
Json.store(o, jsonFile);
} finally {
IOUtils.closeQuietly(is);
}
if (deleteSource) {
FileUtils.delete(fileOrFolder);
}
}
}
use of org.yaml.snakeyaml.Yaml in project serverless by bluenimble.
the class SpecUtils method j2y.
@SuppressWarnings("unchecked")
public static void j2y(File fileOrFolder, boolean deleteSource) throws Exception {
if (!fileOrFolder.exists()) {
return;
}
if (fileOrFolder.isDirectory()) {
File[] files = fileOrFolder.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return file.isDirectory() || file.getName().endsWith(".json");
}
});
if (files == null || files.length == 0) {
return;
}
for (File f : files) {
j2y(f, deleteSource);
}
} else {
File ymlFile = new File(fileOrFolder.getParentFile(), fileOrFolder.getName().substring(0, fileOrFolder.getName().lastIndexOf(Lang.DOT)) + ".yaml");
OutputStream out = null;
try {
out = new FileOutputStream(ymlFile);
new YamlOutputStreamPrinter(out).print(Json.load(fileOrFolder));
} finally {
IOUtils.closeQuietly(out);
}
// validate
Yaml yaml = new Yaml();
InputStream is = null;
try {
is = new FileInputStream(ymlFile);
new JsonObject(yaml.loadAs(is, Map.class), true);
} finally {
IOUtils.closeQuietly(is);
}
if (deleteSource) {
FileUtils.delete(fileOrFolder);
}
}
}
Aggregations