forked from quarkus-qe/quarkus-test-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request quarkus-qe#1236 from mocenas/fix_cli_utils
Fix issues in QuarkusCLIUtils
- Loading branch information
Showing
5 changed files
with
128 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
quarkus-test-cli/src/main/java/io/quarkus/test/util/YamlPropertiesHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 : ""); | ||
} | ||
}); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
quarkus-test-cli/src/test/java/io/quarkus/test/YamlPropertiesHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |