Skip to content

Commit

Permalink
Bugfix/49 settings file (#50)
Browse files Browse the repository at this point in the history
* Fix issue with getMap in JsonSettingsFile

* Increase version
  • Loading branch information
paveliam authored May 20, 2020
1 parent 118d637 commit 000f9e2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.aquality-automation</groupId>
<artifactId>aquality-selenium-core</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>

<packaging>jar</packaging>
<name>Aquality Selenium Core</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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);
}

/**
Expand All @@ -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()) {
Expand All @@ -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;
}
Expand All @@ -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<String> getList(String jsonPath) {
List<String> list = new ArrayList<>();
List<String> 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<JsonNode> spliterator = Spliterators.spliteratorUnknownSize(getJsonNode(jsonPath).elements(), Spliterator.ORDERED);
list = StreamSupport.stream(spliterator, false)
.map(JsonNode::asText)
.collect(Collectors.toList());
}

return list;
}

@Override
public Map<String, Object> getMap(String jsonPath) {
Iterator<Entry<String, JsonNode>> iterator = getJsonNode(jsonPath).fields();
final Map<String, Object> result = new HashMap<>();
iterator.forEachRemaining(entry -> result.put(entry.getKey(), getValue(jsonPath + "/" + entry.getKey())));
return result;
Spliterator<Entry<String, JsonNode>> 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) {
Expand Down
18 changes: 13 additions & 5 deletions src/test/java/tests/utilities/SettingsFileTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -27,18 +28,25 @@ 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<String, String>() {{
private static final Map<String, String> EXPECTED_LANGUAGES = new HashMap<String, String>() {{
put("language", "ru");
}};

private ISettingsFile jsonSettingsFile;

@BeforeMethod
public void before() {
System.setProperty(PROFILE_KEY, PROFILE);
CustomAqualityServices.initInjector(getTestModule());
jsonSettingsFile = CustomAqualityServices.getServiceProvider().getInstance(ISettingsFile.class);
}

@Test
public void testShouldBePossibleToGetMapWithJsonObject() {
Map<String, Object> capabilities = jsonSettingsFile.getMap("/".concat("capabilities"));
assertFalse(capabilities.get("loggingPrefs").toString().isEmpty());
}

@Test
public void testShouldBePossibleToOverrideBooleanValueViaEnvironmentVariable() {
BooleanSupplier getCurrentValue = () -> (Boolean) jsonSettingsFile.getValue("/".concat(BOOLEANVALUE_ENV_KEY));
Expand Down Expand Up @@ -83,7 +91,7 @@ public void testShouldBePossibleToGetValue() {
@Test
public void testShouldBePossibleToGetListOfValues() {
String argumentsPath = "/arguments/start";
List expectedArguments = Arrays.asList("first", "second");
List<String> expectedArguments = Arrays.asList("first", "second");

List<String> arguments = jsonSettingsFile.getList(argumentsPath);
assertNotNull(arguments);
Expand All @@ -101,12 +109,12 @@ public void testShouldBePossibleToGetListOfValues() {
public void testShouldBePossibleToGetMap() {
String loggerPath = "/logger";

Map languages = jsonSettingsFile.getMap(loggerPath);
Map<String, Object> 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<String, String>() {{
Map<String, String> expectedLanguages = new HashMap<String, String>() {{
put("language", newLanguageValue);
}};
System.setProperty(LANGUAGE_ENV_KEY, newLanguageValue);
Expand Down
7 changes: 7 additions & 0 deletions src/test/resources/settings.jsontest.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
{
"capabilities": {
"loggingPrefs": {
"driver": "INFO",
"server": "OFF",
"browser": "FINE"
}
},
"timeouts": {
"timeoutImplicit": 1000,
"timeoutCondition": 1000,
Expand Down

0 comments on commit 000f9e2

Please sign in to comment.