Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.Map.Entry;

public class JsonSettingsFile implements ISettingsFile {

Expand All @@ -27,13 +28,13 @@ public JsonSettingsFile(String resourceName) {

@Override
public Object getValue(String jsonPath) {
return getEnvValueOrDefault(jsonPath);
return getEnvValueOrDefault(jsonPath, true);
}

private Object getEnvValueOrDefault(String jsonPath) {
private Object getEnvValueOrDefault(String jsonPath, boolean throwIfEmpty) {
String envVar = getEnvValue(jsonPath);
if (envVar == null) {
JsonNode node = getJsonNode(jsonPath);
JsonNode node = getJsonNode(jsonPath, throwIfEmpty);
if (node.isBoolean()) {
return node.asBoolean();
} else if (node.isLong()) {
Expand Down Expand Up @@ -73,19 +74,29 @@ public List<String> getList(String jsonPath) {

@Override
public Map<String, Object> getMap(String jsonPath) {
Iterator<Map.Entry<String, JsonNode>> iterator = getJsonNode(jsonPath).fields();
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;
}

private JsonNode getJsonNode(String jsonPath) {
return getJsonNode(jsonPath, true);
}

private JsonNode getJsonNode(String jsonPath, boolean throwIfEmpty) {
JsonNode nodeAtPath;
String errorMessage = String.format("Json field by json-path %1$s was not found in the file %2$s", jsonPath, content);
try {
JsonNode node = mapper.readTree(content);
return node.at(jsonPath);
nodeAtPath = node.at(jsonPath);
} catch (IOException e) {
throw new UncheckedIOException(String.format("Json field by json-path %1$s was not found in the file %2$s", jsonPath, content), e);
throw new UncheckedIOException(errorMessage, e);
}
if (throwIfEmpty && nodeAtPath.isMissingNode()) {
throw new IllegalArgumentException(errorMessage);
}
return nodeAtPath;
}

private String getFileContent(String filename) {
Expand All @@ -98,7 +109,7 @@ private String getFileContent(String filename) {

@Override
public boolean isValuePresent(String path) {
String value = getValue(path).toString().trim();
String value = getEnvValueOrDefault(path, false).toString().trim();
return !value.isEmpty();
}
}
31 changes: 27 additions & 4 deletions src/test/java/tests/utilities/SettingsFileTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@

import aquality.selenium.core.applications.AqualityModule;
import aquality.selenium.core.utilities.ISettingsFile;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import tests.application.CustomAqualityServices;
import tests.application.TestModule;
import tests.configurations.BaseProfileTest;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Consumer;

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 TIMEOUT_POLLING_INTERVAL_KEY = "timeouts.timeoutPollingInterval";
private static final String LANGUAGE_ENV_KEY = "logger.language";
private static final String ARGUMENTS_ENV_KEY = "arguments.start";
Expand Down Expand Up @@ -103,6 +104,28 @@ public void testShouldBePossibleToCheckIsValuePresent() {
assertFalse(isWrongPathPresent, String.format("%s value should not be present in settings file '%s'", wrongPath, FILE_NAME));
}

@Test
public void testShouldBePossibleToCheckThatNullValueIsPresent() {
boolean isNullValuePresent = jsonSettingsFile.isValuePresent(NULLVALUE_PATH);
assertTrue(isNullValuePresent, String.format("%s value should be present in settings file '%s'", NULLVALUE_PATH, FILE_NAME));
}

@DataProvider
public Object[] actionsToGetValue() {
List<Consumer<String>> actionsList = new ArrayList<>();
actionsList.add(path -> jsonSettingsFile.getValue(path));
actionsList.add(path -> jsonSettingsFile.getMap(path));
actionsList.add(path -> jsonSettingsFile.getList(path));
return actionsList.toArray();
}

@Test(dataProvider = "actionsToGetValue")
public void testShouldThrowExceptionWhenValueNotFound(Consumer<String> action) {
String wrongPath = "/blabla";
Assert.assertFalse(jsonSettingsFile.isValuePresent(wrongPath), String.format("%s value should not be present in settings file '%s'", wrongPath, FILE_NAME));
Assert.assertThrows(IllegalArgumentException.class, () -> action.accept(wrongPath));
}

@AfterMethod
public void after() {
System.clearProperty(LANGUAGE_ENV_KEY);
Expand Down
3 changes: 2 additions & 1 deletion src/test/resources/settings.jsontest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
},
"arguments": {
"start": ["first","second"]
}
},
"nullValue": null
}