From eb006746466fe6ab65c78386f72c8b4964a0554a Mon Sep 17 00:00:00 2001 From: Martin Ocenas Date: Fri, 2 Aug 2024 10:57:14 +0200 Subject: [PATCH] Fix issues in QuarkusCLIUtils --- quarkus-test-cli/pom.xml | 4 ++ .../io/quarkus/test/util/QuarkusCLIUtils.java | 17 ++---- .../test/util/YamlPropertiesHandler.java | 60 +++++++++++++++++++ .../test/YamlPropertiesHandlerTest.java | 40 +++++++++++++ .../src/test/resources/example.yaml | 19 ++++++ 5 files changed, 128 insertions(+), 12 deletions(-) create mode 100644 quarkus-test-cli/src/main/java/io/quarkus/test/util/YamlPropertiesHandler.java create mode 100644 quarkus-test-cli/src/test/java/io/quarkus/test/YamlPropertiesHandlerTest.java create mode 100644 quarkus-test-cli/src/test/resources/example.yaml diff --git a/quarkus-test-cli/pom.xml b/quarkus-test-cli/pom.xml index 97c281e2c..e5bf60c4a 100644 --- a/quarkus-test-cli/pom.xml +++ b/quarkus-test-cli/pom.xml @@ -17,5 +17,9 @@ org.apache.maven maven-core + + org.yaml + snakeyaml + diff --git a/quarkus-test-cli/src/main/java/io/quarkus/test/util/QuarkusCLIUtils.java b/quarkus-test-cli/src/main/java/io/quarkus/test/util/QuarkusCLIUtils.java index dcaf8b70d..6bc6f80db 100644 --- a/quarkus-test-cli/src/main/java/io/quarkus/test/util/QuarkusCLIUtils.java +++ b/quarkus-test-cli/src/main/java/io/quarkus/test/util/QuarkusCLIUtils.java @@ -10,8 +10,8 @@ import java.io.FileWriter; import java.io.IOException; import java.io.OutputStream; -import java.io.PrintWriter; import java.nio.file.Files; +import java.nio.file.Paths; import java.util.List; import java.util.Map; import java.util.Properties; @@ -28,7 +28,7 @@ import io.quarkus.test.bootstrap.QuarkusCliRestService; public abstract class QuarkusCLIUtils { - public static final String RESOURCES_DIR = "src/main/resources"; + public static final String RESOURCES_DIR = Paths.get("src", "main", "resources").toString(); public static final String PROPERTIES_FILE = "application.properties"; public static final String PROPERTIES_YAML_FILE = "application.yml"; public static final String POM_FILE = "pom.xml"; @@ -60,15 +60,7 @@ public static void writePropertiesToPropertiesFile(QuarkusCliRestService app, Pr */ public static void writePropertiesToYamlFile(QuarkusCliRestService app, Properties properties) throws IOException { File yaml = getPropertiesYamlFile(app); - // we're using print writer to overwrite existing content of the file - PrintWriter writer = new PrintWriter(new FileWriter(yaml)); - for (Map.Entry entry : properties.entrySet()) { - writer.append(entry.getKey().toString()); - writer.append(": "); - writer.append(entry.getValue().toString()); - writer.append("\n"); - } - writer.close(); + YamlPropertiesHandler.writePropertiesIntoYaml(yaml, properties); } public static Properties readPropertiesFile(QuarkusCliRestService app) throws IOException { @@ -76,7 +68,8 @@ public static Properties readPropertiesFile(QuarkusCliRestService app) throws IO } public static Properties readPropertiesYamlFile(QuarkusCliRestService app) throws IOException { - return loadPropertiesFromFile(getPropertiesYamlFile(app)); + File yamlFile = getPropertiesYamlFile(app); + return YamlPropertiesHandler.readYamlFileIntoProperties(yamlFile); } public static Properties loadPropertiesFromFile(File file) throws IOException { diff --git a/quarkus-test-cli/src/main/java/io/quarkus/test/util/YamlPropertiesHandler.java b/quarkus-test-cli/src/main/java/io/quarkus/test/util/YamlPropertiesHandler.java new file mode 100644 index 000000000..705a144aa --- /dev/null +++ b/quarkus-test-cli/src/main/java/io/quarkus/test/util/YamlPropertiesHandler.java @@ -0,0 +1,60 @@ +package io.quarkus.test.util; + +import static java.util.Collections.singletonMap; +import static org.apache.maven.surefire.shared.lang3.StringUtils.isBlank; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Properties; + +import org.yaml.snakeyaml.Yaml; + +public abstract class YamlPropertiesHandler { + + public static void writePropertiesIntoYaml(File yamlFile, Properties properties) throws IOException { + Yaml yaml = new Yaml(); + yaml.dump(properties, new FileWriter(yamlFile)); + } + + public static Properties readYamlFileIntoProperties(File yamlFile) throws FileNotFoundException { + Yaml yaml = new Yaml(); + Map obj = yaml.load(new FileInputStream(yamlFile)); + + Properties properties = new Properties(); + properties.putAll(getFlattenedMap(obj)); + + return properties; + } + + private static Map getFlattenedMap(Map source) { + Map result = new LinkedHashMap<>(); + buildFlattenedMap(result, source, null); + return result; + } + + private static void buildFlattenedMap(Map result, Map source, String path) { + source.forEach((key, value) -> { + if (!isBlank(path)) { + key = path + (key.startsWith("[") ? key : '.' + key); + } + if (value instanceof String) { + result.put(key, value); + } else if (value instanceof Map) { + buildFlattenedMap(result, (Map) value, key); + } else if (value instanceof Collection) { + int count = 0; + for (Object object : (Collection) value) { + buildFlattenedMap(result, singletonMap("[" + (count++) + "]", object), key); + } + } else { + result.put(key, value != null ? "" + value : ""); + } + }); + } +} diff --git a/quarkus-test-cli/src/test/java/io/quarkus/test/YamlPropertiesHandlerTest.java b/quarkus-test-cli/src/test/java/io/quarkus/test/YamlPropertiesHandlerTest.java new file mode 100644 index 000000000..8876334a5 --- /dev/null +++ b/quarkus-test-cli/src/test/java/io/quarkus/test/YamlPropertiesHandlerTest.java @@ -0,0 +1,40 @@ +package io.quarkus.test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.Properties; + +import org.junit.jupiter.api.Test; + +import io.quarkus.test.util.YamlPropertiesHandler; + +public class YamlPropertiesHandlerTest { + + @Test + public void testYamlParsing() throws URISyntaxException, IOException { + File yamlFile = new File(getClass().getClassLoader().getResource("example.yaml").toURI()); + Properties properties = YamlPropertiesHandler.readYamlFileIntoProperties(yamlFile); + + assertEquals("http://localhost:8180/auth/realms/quarkus", properties.getProperty("quarkus.oidc.auth-server-url"), + "Loaded property should have the value from yaml"); + } + + @Test + public void testYamlWriteAndRead() throws IOException { + File tempYamlFile = File.createTempFile("_yamlTest", ".yaml"); + tempYamlFile.deleteOnExit(); + + Properties properties = new Properties(); + properties.put("quarkus.hibernate-search-orm.automatic-indexing.synchronization.strategy", "sync"); + properties.put("quarkus.hibernate-search-orm.quarkusQE.automatic-indexing.synchronization.strategy", "sync"); + + YamlPropertiesHandler.writePropertiesIntoYaml(tempYamlFile, properties); + + Properties parsedProperties = YamlPropertiesHandler.readYamlFileIntoProperties(tempYamlFile); + + assertEquals(properties, parsedProperties, "Parsed properties should be the same, as those written to the file"); + } +} diff --git a/quarkus-test-cli/src/test/resources/example.yaml b/quarkus-test-cli/src/test/resources/example.yaml new file mode 100644 index 000000000..e9f73d5f0 --- /dev/null +++ b/quarkus-test-cli/src/test/resources/example.yaml @@ -0,0 +1,19 @@ +quarkus: + datasource: + jdbc: + url: jdbc:postgresql://localhost:5432/quarkus_test + + hibernate-orm: + database: + generation: drop-and-create + + oidc: + enabled: true + auth-server-url: http://localhost:8180/auth/realms/quarkus + client-id: app + +app: + frontend: + oidc-realm: quarkus + oidc-app: app + oidc-server: http://localhost:8180/auth