10
10
import java .nio .file .Files ;
11
11
import java .nio .file .Paths ;
12
12
import java .util .*;
13
+ import java .util .Map .Entry ;
13
14
14
15
public class JsonSettingsFile implements ISettingsFile {
15
16
@@ -27,13 +28,13 @@ public JsonSettingsFile(String resourceName) {
27
28
28
29
@ Override
29
30
public Object getValue (String jsonPath ) {
30
- return getEnvValueOrDefault (jsonPath );
31
+ return getEnvValueOrDefault (jsonPath , true );
31
32
}
32
33
33
- private Object getEnvValueOrDefault (String jsonPath ) {
34
+ private Object getEnvValueOrDefault (String jsonPath , boolean throwIfEmpty ) {
34
35
String envVar = getEnvValue (jsonPath );
35
36
if (envVar == null ) {
36
- JsonNode node = getJsonNode (jsonPath );
37
+ JsonNode node = getJsonNode (jsonPath , throwIfEmpty );
37
38
if (node .isBoolean ()) {
38
39
return node .asBoolean ();
39
40
} else if (node .isLong ()) {
@@ -73,19 +74,29 @@ public List<String> getList(String jsonPath) {
73
74
74
75
@ Override
75
76
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 ();
77
78
final Map <String , Object > result = new HashMap <>();
78
79
iterator .forEachRemaining (entry -> result .put (entry .getKey (), getValue (jsonPath + "/" + entry .getKey ())));
79
80
return result ;
80
81
}
81
82
82
83
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 );
83
90
try {
84
91
JsonNode node = mapper .readTree (content );
85
- return node .at (jsonPath );
92
+ nodeAtPath = node .at (jsonPath );
86
93
} 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 );
88
98
}
99
+ return nodeAtPath ;
89
100
}
90
101
91
102
private String getFileContent (String filename ) {
@@ -98,7 +109,7 @@ private String getFileContent(String filename) {
98
109
99
110
@ Override
100
111
public boolean isValuePresent (String path ) {
101
- String value = getValue (path ).toString ().trim ();
112
+ String value = getEnvValueOrDefault (path , false ).toString ().trim ();
102
113
return !value .isEmpty ();
103
114
}
104
115
}
0 commit comments