Skip to content

Commit

Permalink
Merge pull request quarkus-qe#1236 from mocenas/fix_cli_utils
Browse files Browse the repository at this point in the history
Fix issues in QuarkusCLIUtils
  • Loading branch information
jedla97 authored Aug 5, 2024
2 parents afc01dc + eb00674 commit a6f6c8e
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 12 deletions.
4 changes: 4 additions & 0 deletions quarkus-test-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -60,23 +60,16 @@ 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<Object, Object> 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 {
return loadPropertiesFromFile(getPropertiesFile(app));
}

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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Object> obj = yaml.load(new FileInputStream(yamlFile));

Properties properties = new Properties();
properties.putAll(getFlattenedMap(obj));

return properties;
}

private static Map<String, Object> getFlattenedMap(Map<String, Object> source) {
Map<String, Object> result = new LinkedHashMap<>();
buildFlattenedMap(result, source, null);
return result;
}

private static void buildFlattenedMap(Map<String, Object> result, Map<String, Object> 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<String, Object>) 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 : "");
}
});
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
19 changes: 19 additions & 0 deletions quarkus-test-cli/src/test/resources/example.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a6f6c8e

Please sign in to comment.