Skip to content

Commit 9129603

Browse files
authored
Fix #22 : JsonSettingsFile should throw an exception (#35)
when there are no values found by jsonPath
1 parent 15c8fc1 commit 9129603

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

src/main/java/aquality/selenium/core/utilities/JsonSettingsFile.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.nio.file.Files;
1111
import java.nio.file.Paths;
1212
import java.util.*;
13+
import java.util.Map.Entry;
1314

1415
public class JsonSettingsFile implements ISettingsFile {
1516

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

2829
@Override
2930
public Object getValue(String jsonPath) {
30-
return getEnvValueOrDefault(jsonPath);
31+
return getEnvValueOrDefault(jsonPath, true);
3132
}
3233

33-
private Object getEnvValueOrDefault(String jsonPath) {
34+
private Object getEnvValueOrDefault(String jsonPath, boolean throwIfEmpty) {
3435
String envVar = getEnvValue(jsonPath);
3536
if (envVar == null) {
36-
JsonNode node = getJsonNode(jsonPath);
37+
JsonNode node = getJsonNode(jsonPath, throwIfEmpty);
3738
if (node.isBoolean()) {
3839
return node.asBoolean();
3940
} else if (node.isLong()) {
@@ -73,19 +74,29 @@ public List<String> getList(String jsonPath) {
7374

7475
@Override
7576
public Map<String, Object> getMap(String jsonPath) {
76-
Iterator<Map.Entry<String, JsonNode>> iterator = getJsonNode(jsonPath).fields();
77+
Iterator<Entry<String, JsonNode>> iterator = getJsonNode(jsonPath).fields();
7778
final Map<String, Object> result = new HashMap<>();
7879
iterator.forEachRemaining(entry -> result.put(entry.getKey(), getValue(jsonPath + "/" + entry.getKey())));
7980
return result;
8081
}
8182

8283
private JsonNode getJsonNode(String jsonPath) {
84+
return getJsonNode(jsonPath, true);
85+
}
86+
87+
private JsonNode getJsonNode(String jsonPath, boolean throwIfEmpty) {
88+
JsonNode nodeAtPath;
89+
String errorMessage = String.format("Json field by json-path %1$s was not found in the file %2$s", jsonPath, content);
8390
try {
8491
JsonNode node = mapper.readTree(content);
85-
return node.at(jsonPath);
92+
nodeAtPath = node.at(jsonPath);
8693
} catch (IOException e) {
87-
throw new UncheckedIOException(String.format("Json field by json-path %1$s was not found in the file %2$s", jsonPath, content), e);
94+
throw new UncheckedIOException(errorMessage, e);
95+
}
96+
if (throwIfEmpty && nodeAtPath.isMissingNode()) {
97+
throw new IllegalArgumentException(errorMessage);
8898
}
99+
return nodeAtPath;
89100
}
90101

91102
private String getFileContent(String filename) {
@@ -98,7 +109,7 @@ private String getFileContent(String filename) {
98109

99110
@Override
100111
public boolean isValuePresent(String path) {
101-
String value = getValue(path).toString().trim();
112+
String value = getEnvValueOrDefault(path, false).toString().trim();
102113
return !value.isEmpty();
103114
}
104115
}

src/test/java/tests/utilities/SettingsFileTests.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,23 @@
22

33
import aquality.selenium.core.applications.AqualityModule;
44
import aquality.selenium.core.utilities.ISettingsFile;
5+
import org.testng.Assert;
56
import org.testng.annotations.AfterMethod;
67
import org.testng.annotations.BeforeMethod;
8+
import org.testng.annotations.DataProvider;
79
import org.testng.annotations.Test;
810
import tests.application.CustomAqualityServices;
911
import tests.application.TestModule;
1012
import tests.configurations.BaseProfileTest;
1113

12-
import java.util.Arrays;
13-
import java.util.HashMap;
14-
import java.util.List;
15-
import java.util.Map;
14+
import java.util.*;
15+
import java.util.function.Consumer;
1616

1717
import static org.testng.Assert.*;
1818

1919
public class SettingsFileTests extends BaseProfileTest {
2020
private static final String TIMEOUT_POLLING_INTERVAL_PATH = "/timeouts/timeoutPollingInterval";
21+
private static final String NULLVALUE_PATH = "/nullValue";
2122
private static final String TIMEOUT_POLLING_INTERVAL_KEY = "timeouts.timeoutPollingInterval";
2223
private static final String LANGUAGE_ENV_KEY = "logger.language";
2324
private static final String ARGUMENTS_ENV_KEY = "arguments.start";
@@ -103,6 +104,28 @@ public void testShouldBePossibleToCheckIsValuePresent() {
103104
assertFalse(isWrongPathPresent, String.format("%s value should not be present in settings file '%s'", wrongPath, FILE_NAME));
104105
}
105106

107+
@Test
108+
public void testShouldBePossibleToCheckThatNullValueIsPresent() {
109+
boolean isNullValuePresent = jsonSettingsFile.isValuePresent(NULLVALUE_PATH);
110+
assertTrue(isNullValuePresent, String.format("%s value should be present in settings file '%s'", NULLVALUE_PATH, FILE_NAME));
111+
}
112+
113+
@DataProvider
114+
public Object[] actionsToGetValue() {
115+
List<Consumer<String>> actionsList = new ArrayList<>();
116+
actionsList.add(path -> jsonSettingsFile.getValue(path));
117+
actionsList.add(path -> jsonSettingsFile.getMap(path));
118+
actionsList.add(path -> jsonSettingsFile.getList(path));
119+
return actionsList.toArray();
120+
}
121+
122+
@Test(dataProvider = "actionsToGetValue")
123+
public void testShouldThrowExceptionWhenValueNotFound(Consumer<String> action) {
124+
String wrongPath = "/blabla";
125+
Assert.assertFalse(jsonSettingsFile.isValuePresent(wrongPath), String.format("%s value should not be present in settings file '%s'", wrongPath, FILE_NAME));
126+
Assert.assertThrows(IllegalArgumentException.class, () -> action.accept(wrongPath));
127+
}
128+
106129
@AfterMethod
107130
public void after() {
108131
System.clearProperty(LANGUAGE_ENV_KEY);

src/test/resources/settings.jsontest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
},
1515
"arguments": {
1616
"start": ["first","second"]
17-
}
17+
},
18+
"nullValue": null
1819
}

0 commit comments

Comments
 (0)