diff --git a/src/main/java/aquality/selenium/core/utilities/JsonSettingsFile.java b/src/main/java/aquality/selenium/core/utilities/JsonSettingsFile.java index 5376a77..1aea1ae 100644 --- a/src/main/java/aquality/selenium/core/utilities/JsonSettingsFile.java +++ b/src/main/java/aquality/selenium/core/utilities/JsonSettingsFile.java @@ -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 { @@ -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()) { @@ -73,19 +74,29 @@ public List getList(String jsonPath) { @Override public Map getMap(String jsonPath) { - Iterator> iterator = getJsonNode(jsonPath).fields(); + Iterator> iterator = getJsonNode(jsonPath).fields(); final Map 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) { @@ -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(); } } diff --git a/src/test/java/tests/utilities/SettingsFileTests.java b/src/test/java/tests/utilities/SettingsFileTests.java index 15397b6..c38d830 100644 --- a/src/test/java/tests/utilities/SettingsFileTests.java +++ b/src/test/java/tests/utilities/SettingsFileTests.java @@ -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"; @@ -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> 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 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); diff --git a/src/test/resources/settings.jsontest.json b/src/test/resources/settings.jsontest.json index 6e9a9cd..aaa69db 100644 --- a/src/test/resources/settings.jsontest.json +++ b/src/test/resources/settings.jsontest.json @@ -14,5 +14,6 @@ }, "arguments": { "start": ["first","second"] - } + }, + "nullValue": null }