11package dev .blaauwendraad .masker .json ;
22
3- import com .fasterxml .jackson .core .JsonProcessingException ;
4- import com .fasterxml .jackson .databind .JsonNode ;
5- import com .fasterxml .jackson .databind .ObjectMapper ;
6- import com .fasterxml .jackson .databind .node .ArrayNode ;
73import dev .blaauwendraad .masker .json .config .JsonMaskingConfig ;
84import dev .blaauwendraad .masker .json .config .JsonMaskingConfigTestUtil ;
95import dev .blaauwendraad .masker .json .config .KeyMaskingConfig ;
106import org .jspecify .annotations .Nullable ;
117import org .junit .jupiter .api .Assertions ;
8+ import tools .jackson .databind .JsonNode ;
9+ import tools .jackson .databind .json .JsonMapper ;
10+ import tools .jackson .databind .node .ArrayNode ;
1211
1312import java .io .ByteArrayInputStream ;
1413import java .io .ByteArrayOutputStream ;
1514import java .io .IOException ;
16- import java .net .URL ;
1715import java .nio .charset .StandardCharsets ;
1816import java .util .ArrayList ;
1917import java .util .List ;
@@ -28,70 +26,69 @@ public final class JsonMaskerTestUtil {
2826 private JsonMaskerTestUtil () {
2927 }
3028
31- private static final ObjectMapper mapper = new ObjectMapper ();
29+ private static final JsonMapper jsonMapper = new JsonMapper ();
3230
3331 public static List <JsonMaskerTestInstance > getJsonMaskerTestInstancesFromFile (String fileName ) throws IOException {
34- List <JsonMaskerTestInstance > testInstances = new ArrayList <>();
35- URL fileUrl = JsonMaskerTestUtil .class .getClassLoader ().getResource (fileName );
36- if (fileUrl == null ) {
37- throw new IllegalArgumentException ("File not found: " + fileName );
38- }
39- ArrayNode jsonArray = mapper .readValue (fileUrl .openStream (), ArrayNode .class );
40- for (JsonNode jsonNode : jsonArray ) {
41- JsonMaskingConfig .Builder builder = JsonMaskingConfig .builder ();
42- JsonNode jsonMaskingConfig = jsonNode .findValue ("maskingConfig" );
43- if (jsonMaskingConfig != null ) {
44- applyConfig (jsonMaskingConfig , builder );
32+ try (var stream = JsonMaskerTestUtil .class .getClassLoader ().getResourceAsStream (fileName )) {
33+ if (stream == null ) {
34+ throw new IllegalArgumentException ("File not found: " + fileName );
35+ }
36+ List <JsonMaskerTestInstance > testInstances = new ArrayList <>();
37+ ArrayNode jsonArray = jsonMapper .readValue (stream , ArrayNode .class );
38+ for (JsonNode jsonNode : jsonArray ) {
39+ JsonMaskingConfig .Builder builder = JsonMaskingConfig .builder ();
40+ JsonNode jsonMaskingConfig = jsonNode .findValue ("maskingConfig" );
41+ if (jsonMaskingConfig != null ) {
42+ applyConfig (jsonMaskingConfig , builder );
43+ }
44+ JsonMaskingConfig maskingConfig = builder .build ();
45+ var input = jsonNode .get ("input" ).toPrettyString ();
46+ var expectedOutput = jsonNode .get ("expectedOutput" ).toPrettyString ();
47+ testInstances .add (new JsonMaskerTestInstance (input , expectedOutput , new KeyContainsMasker (maskingConfig )));
4548 }
46- JsonMaskingConfig maskingConfig = builder .build ();
47- var input = jsonNode .get ("input" ).toPrettyString ();
48- var expectedOutput = jsonNode .get ("expectedOutput" ).toPrettyString ();
49- testInstances .add (new JsonMaskerTestInstance (input , expectedOutput , new KeyContainsMasker (maskingConfig )));
49+ return testInstances ;
5050 }
51- return testInstances ;
5251 }
5352
5453 private static void applyConfig (JsonNode jsonMaskingConfig , JsonMaskingConfig .Builder builder ) {
55- jsonMaskingConfig .properties ().forEach (p -> {
56- String key = p .getKey ();
57- JsonNode value = p .getValue ();
54+ jsonMaskingConfig .forEachEntry ((key , value ) -> {
5855 switch (key ) {
5956 case "maskKeys" -> StreamSupport .stream (value .spliterator (), false ).forEach (node -> {
60- if (node .isTextual ()) {
61- builder .maskKeys (Set .of (node .asText ()));
57+ if (node .isString ()) {
58+ builder .maskKeys (Set .of (node .asString ()));
6259 } else {
63- builder .maskKeys (asSet (node .get ("keys" ), JsonNode ::asText ), applyKeyConfig (node .get ("keyMaskingConfig" )));
60+ builder .maskKeys (asSet (node .get ("keys" ), JsonNode ::asString ), applyKeyConfig (node .get ("keyMaskingConfig" )));
6461 }
6562 });
6663 case "maskJsonPaths" -> StreamSupport .stream (value .spliterator (), false ).forEach (node -> {
67- if (node .isTextual ()) {
68- builder .maskJsonPaths (Set .of (node .asText ()));
64+ if (node .isString ()) {
65+ builder .maskJsonPaths (Set .of (node .asString ()));
6966 } else {
70- builder .maskJsonPaths (asSet (node .get ("keys" ), JsonNode ::asText ), applyKeyConfig (node .get ("keyMaskingConfig" )));
67+ builder .maskJsonPaths (asSet (node .get ("keys" ), JsonNode ::asString ), applyKeyConfig (node .get ("keyMaskingConfig" )));
7168 }
7269 });
73- case "allowKeys" -> builder .allowKeys (asSet (value , JsonNode ::asText ));
74- case "allowJsonPaths" -> builder .allowJsonPaths (asSet (value , JsonNode ::asText ));
70+ case "allowKeys" -> builder .allowKeys (asSet (value , JsonNode ::asString ));
71+ case "allowJsonPaths" -> builder .allowJsonPaths (asSet (value , JsonNode ::asString ));
7572 case "caseSensitiveTargetKeys" -> {
7673 if (value .booleanValue ()) {
7774 builder .caseSensitiveTargetKeys ();
7875 }
7976 }
80- case "maskStringsWith" -> builder .maskStringsWith (value .textValue ());
81- case "maskStringCharactersWith" -> builder .maskStringCharactersWith (value .textValue ());
77+ case "maskStringsWith" -> builder .maskStringsWith (value .asString ());
78+ case "maskStringCharactersWith" -> builder .maskStringCharactersWith (value .asString ());
8279 case "maskNumbersWith" -> {
8380 if (value .isInt ()) {
8481 builder .maskNumbersWith (value .intValue ());
8582 } else {
86- builder .maskNumbersWith (value .textValue ());
83+ builder .maskNumbersWith (value .asString ());
8784 }
8885 }
8986 case "maskNumberDigitsWith" -> builder .maskNumberDigitsWith (value .intValue ());
9087 case "maskBooleansWith" -> {
9188 if (value .isBoolean ()) {
9289 builder .maskBooleansWith (value .booleanValue ());
9390 }
94- builder .maskBooleansWith (value .textValue ());
91+ builder .maskBooleansWith (value .asString ());
9592 }
9693 default -> throw new IllegalArgumentException ("Unknown option " + key );
9794 }
@@ -100,25 +97,23 @@ private static void applyConfig(JsonNode jsonMaskingConfig, JsonMaskingConfig.Bu
10097
10198 private static KeyMaskingConfig applyKeyConfig (JsonNode jsonNode ) {
10299 KeyMaskingConfig .Builder builder = KeyMaskingConfig .builder ();
103- jsonNode .properties ().forEach (p -> {
104- String key = p .getKey ();
105- JsonNode value = p .getValue ();
100+ jsonNode .forEachEntry ((key , value ) -> {
106101 switch (key ) {
107- case "maskStringsWith" -> builder .maskStringsWith (value .textValue ());
108- case "maskStringCharactersWith" -> builder .maskStringCharactersWith (value .textValue ());
102+ case "maskStringsWith" -> builder .maskStringsWith (value .asString ());
103+ case "maskStringCharactersWith" -> builder .maskStringCharactersWith (value .asString ());
109104 case "maskNumbersWith" -> {
110105 if (value .isInt ()) {
111106 builder .maskNumbersWith (value .intValue ());
112107 } else {
113- builder .maskNumbersWith (value .textValue ());
108+ builder .maskNumbersWith (value .asString ());
114109 }
115110 }
116111 case "maskNumberDigitsWith" -> builder .maskNumberDigitsWith (value .intValue ());
117112 case "maskBooleansWith" -> {
118113 if (value .isBoolean ()) {
119114 builder .maskBooleansWith (value .booleanValue ());
120115 }
121- builder .maskBooleansWith (value .textValue ());
116+ builder .maskBooleansWith (value .asString ());
122117 }
123118 default -> throw new IllegalArgumentException ("Unknown option " + key );
124119 }
@@ -148,13 +143,9 @@ public static void assertJsonMaskerApiEquivalence(JsonMasker jsonMasker,
148143 String minimalBufferStreamsOutput = getStreamingModeOutput (jsonMasker , input );
149144 JsonMaskingConfigTestUtil .setBufferSize (((KeyContainsMasker ) jsonMasker ).maskingConfig , oldBufferSize );
150145 if (pretty ) {
151- try {
152- bytesOutput = ParseAndMaskUtil .DEFAULT_OBJECT_MAPPER .readTree (bytesOutput ).toString ();
153- streamsOutput = ParseAndMaskUtil .DEFAULT_OBJECT_MAPPER .readTree (streamsOutput ).toString ();
154- minimalBufferStreamsOutput = ParseAndMaskUtil .DEFAULT_OBJECT_MAPPER .readTree (minimalBufferStreamsOutput ).toString ();
155- } catch (JsonProcessingException e ) {
156- throw new IllegalStateException ("Failed for input: " + input , e );
157- }
146+ bytesOutput = ParseAndMaskUtil .DEFAULT_JSON_MAPPER .readTree (bytesOutput ).toString ();
147+ streamsOutput = ParseAndMaskUtil .DEFAULT_JSON_MAPPER .readTree (streamsOutput ).toString ();
148+ minimalBufferStreamsOutput = ParseAndMaskUtil .DEFAULT_JSON_MAPPER .readTree (minimalBufferStreamsOutput ).toString ();
158149 }
159150 if (expectedOutput != null ) {
160151 Assertions .assertEquals (expectedOutput , bytesOutput , "Failed for input: " + input );
0 commit comments