From 000f9e2e4cb66950d55fcb9f16a9330d6a599de8 Mon Sep 17 00:00:00 2001
From: Pavel Anihimovsky <47742067+pavelanihimovsky@users.noreply.github.com>
Date: Wed, 20 May 2020 17:51:05 +0300
Subject: [PATCH] Bugfix/49 settings file (#50)
* Fix issue with getMap in JsonSettingsFile
* Increase version
---
pom.xml | 2 +-
.../core/utilities/JsonSettingsFile.java | 32 +++++++++++--------
.../tests/utilities/SettingsFileTests.java | 18 ++++++++---
src/test/resources/settings.jsontest.json | 7 ++++
4 files changed, 40 insertions(+), 19 deletions(-)
diff --git a/pom.xml b/pom.xml
index ff55168..2ff9793 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
com.github.aquality-automation
aquality-selenium-core
- 1.0.1
+ 1.0.2
jar
Aquality Selenium Core
diff --git a/src/main/java/aquality/selenium/core/utilities/JsonSettingsFile.java b/src/main/java/aquality/selenium/core/utilities/JsonSettingsFile.java
index 35ccfb8..2a9ab6d 100644
--- a/src/main/java/aquality/selenium/core/utilities/JsonSettingsFile.java
+++ b/src/main/java/aquality/selenium/core/utilities/JsonSettingsFile.java
@@ -11,6 +11,8 @@
import java.nio.file.Paths;
import java.util.*;
import java.util.Map.Entry;
+import java.util.stream.Collectors;
+import java.util.stream.StreamSupport;
public class JsonSettingsFile implements ISettingsFile {
@@ -34,9 +36,9 @@ public Object getValue(String jsonPath) {
private Object getEnvValueOrDefault(String jsonPath, boolean throwIfEmpty) {
String envVar = getEnvValue(jsonPath);
JsonNode node = getJsonNode(jsonPath, throwIfEmpty && envVar == null);
- return !node.isMissingNode()
- ? castEnvOrDefaulValue(node, envVar)
- : envVar;
+ return node.isMissingNode()
+ ? envVar
+ : castEnvOrDefaultValue(node, envVar);
}
/**
@@ -46,7 +48,7 @@ private Object getEnvValueOrDefault(String jsonPath, boolean throwIfEmpty) {
* @param envVar value got from environment variable
* @return Value, casted to specific type.
*/
- private Object castEnvOrDefaulValue(JsonNode node, String envVar) {
+ private Object castEnvOrDefaultValue(JsonNode node, String envVar) {
if (node.isBoolean()) {
return envVar == null ? node.asBoolean() : Boolean.parseBoolean(envVar);
} else if (node.isLong()) {
@@ -55,6 +57,8 @@ private Object castEnvOrDefaulValue(JsonNode node, String envVar) {
return envVar == null ? node.asInt() : Integer.parseInt(envVar);
} else if (node.isDouble()) {
return envVar == null ? node.asDouble() : Double.parseDouble(envVar);
+ } else if (node.isObject()) {
+ return envVar == null ? node.toString() : envVar;
} else {
return envVar == null ? node.asText() : envVar;
}
@@ -66,29 +70,31 @@ private String getEnvValue(String jsonPath) {
if (envVar != null) {
Logger.getInstance().debug(String.format("***** Using variable passed from environment %1$s=%2$s", key, envVar));
}
-
return envVar;
}
@Override
public List getList(String jsonPath) {
- List list = new ArrayList<>();
+ List list;
String envVar = getEnvValue(jsonPath);
if (envVar != null) {
- Arrays.stream(envVar.split(",")).forEach(element -> list.add(element.trim()));
+ list = Arrays.stream(envVar.split(","))
+ .map(String::trim)
+ .collect(Collectors.toList());
} else {
- getJsonNode(jsonPath).elements().forEachRemaining(node -> list.add(node.asText()));
+ Spliterator spliterator = Spliterators.spliteratorUnknownSize(getJsonNode(jsonPath).elements(), Spliterator.ORDERED);
+ list = StreamSupport.stream(spliterator, false)
+ .map(JsonNode::asText)
+ .collect(Collectors.toList());
}
-
return list;
}
@Override
public Map getMap(String jsonPath) {
- Iterator> iterator = getJsonNode(jsonPath).fields();
- final Map result = new HashMap<>();
- iterator.forEachRemaining(entry -> result.put(entry.getKey(), getValue(jsonPath + "/" + entry.getKey())));
- return result;
+ Spliterator> spliterator = Spliterators.spliteratorUnknownSize(getJsonNode(jsonPath).fields(), Spliterator.ORDERED);
+ return StreamSupport.stream(spliterator, false)
+ .collect(Collectors.toMap(Entry::getKey, entry -> getValue(jsonPath + "/" + entry.getKey())));
}
private JsonNode getJsonNode(String jsonPath) {
diff --git a/src/test/java/tests/utilities/SettingsFileTests.java b/src/test/java/tests/utilities/SettingsFileTests.java
index 7871fb9..a94d38d 100644
--- a/src/test/java/tests/utilities/SettingsFileTests.java
+++ b/src/test/java/tests/utilities/SettingsFileTests.java
@@ -18,6 +18,7 @@
import static org.testng.Assert.*;
public class SettingsFileTests extends BaseProfileTest {
+
private static final String TIMEOUT_POLLING_INTERVAL_PATH = "/timeouts/timeoutPollingInterval";
private static final String NULLVALUE_PATH = "/nullValue";
private static final String ABSENTVALUE_PATH = "/absentvalue";
@@ -27,11 +28,12 @@ public class SettingsFileTests extends BaseProfileTest {
private static final String BOOLEANVALUE_ENV_KEY = "booleanValue";
private static final String PROFILE = "jsontest";
private static final String FILE_NAME = String.format("settings.%s.json", PROFILE);
- private ISettingsFile jsonSettingsFile;
- private static final Map EXPECTED_LANGUAGES = new HashMap() {{
+ private static final Map EXPECTED_LANGUAGES = new HashMap() {{
put("language", "ru");
}};
+ private ISettingsFile jsonSettingsFile;
+
@BeforeMethod
public void before() {
System.setProperty(PROFILE_KEY, PROFILE);
@@ -39,6 +41,12 @@ public void before() {
jsonSettingsFile = CustomAqualityServices.getServiceProvider().getInstance(ISettingsFile.class);
}
+ @Test
+ public void testShouldBePossibleToGetMapWithJsonObject() {
+ Map capabilities = jsonSettingsFile.getMap("/".concat("capabilities"));
+ assertFalse(capabilities.get("loggingPrefs").toString().isEmpty());
+ }
+
@Test
public void testShouldBePossibleToOverrideBooleanValueViaEnvironmentVariable() {
BooleanSupplier getCurrentValue = () -> (Boolean) jsonSettingsFile.getValue("/".concat(BOOLEANVALUE_ENV_KEY));
@@ -83,7 +91,7 @@ public void testShouldBePossibleToGetValue() {
@Test
public void testShouldBePossibleToGetListOfValues() {
String argumentsPath = "/arguments/start";
- List expectedArguments = Arrays.asList("first", "second");
+ List expectedArguments = Arrays.asList("first", "second");
List arguments = jsonSettingsFile.getList(argumentsPath);
assertNotNull(arguments);
@@ -101,12 +109,12 @@ public void testShouldBePossibleToGetListOfValues() {
public void testShouldBePossibleToGetMap() {
String loggerPath = "/logger";
- Map languages = jsonSettingsFile.getMap(loggerPath);
+ Map languages = jsonSettingsFile.getMap(loggerPath);
assertNotNull(languages);
assertEquals(languages, EXPECTED_LANGUAGES, String.format("Map of values in settings file '%s' should be read correctly", FILE_NAME));
String newLanguageValue = "newLangMap";
- Map expectedLanguages = new HashMap() {{
+ Map expectedLanguages = new HashMap() {{
put("language", newLanguageValue);
}};
System.setProperty(LANGUAGE_ENV_KEY, newLanguageValue);
diff --git a/src/test/resources/settings.jsontest.json b/src/test/resources/settings.jsontest.json
index 86a7a86..74522ea 100644
--- a/src/test/resources/settings.jsontest.json
+++ b/src/test/resources/settings.jsontest.json
@@ -1,4 +1,11 @@
{
+ "capabilities": {
+ "loggingPrefs": {
+ "driver": "INFO",
+ "server": "OFF",
+ "browser": "FINE"
+ }
+ },
"timeouts": {
"timeoutImplicit": 1000,
"timeoutCondition": 1000,