1
+ package com .blackduck .integration .detectable .util ;
2
+
3
+ import com .google .gson .JsonObject ;
4
+ import com .google .gson .JsonSyntaxException ;
5
+ import org .junit .jupiter .api .Test ;
6
+ import static org .junit .jupiter .api .Assertions .*;
7
+
8
+ class JsonSanitizerTest {
9
+ @ Test
10
+ void testValidJson () {
11
+ String json = "{ " +
12
+ "\" name\" : \" my-project\" , " +
13
+ "\" version\" : \" 1.0.0\" , " +
14
+ "\" private\" : true, " +
15
+ "\" scripts\" : { " +
16
+ "\" start\" : \" node index.js\" , " +
17
+ "\" test\" : \" jest\" " +
18
+ "}, " +
19
+ "\" dependencies\" : { " +
20
+ "\" express\" : \" ^4.17.1\" , " +
21
+ "\" lodash\" : \" ^4.17.21\" " +
22
+ "}, " +
23
+ "\" devDependencies\" : { " +
24
+ "\" jest\" : \" ^27.0.0\" , " +
25
+ "\" eslint\" : \" ^7.32.0\" " +
26
+ "}" +
27
+ "}" ;
28
+
29
+ JsonObject sanitized = JsonSanitizer .sanitize (json );
30
+
31
+ assertEquals ("my-project" , sanitized .get ("name" ).getAsString ());
32
+ assertEquals ("1.0.0" , sanitized .get ("version" ).getAsString ());
33
+ assertTrue (sanitized .get ("private" ).getAsBoolean ());
34
+ assertEquals ("node index.js" , sanitized .getAsJsonObject ("scripts" ).get ("start" ).getAsString ());
35
+ assertEquals ("^4.17.1" , sanitized .getAsJsonObject ("dependencies" ).get ("express" ).getAsString ());
36
+ assertEquals ("^27.0.0" , sanitized .getAsJsonObject ("devDependencies" ).get ("jest" ).getAsString ());
37
+ }
38
+
39
+ @ Test
40
+ void testDuplicateKeysInDependencies () {
41
+ String json = "{ " +
42
+ "\" dependencies\" : { " +
43
+ "\" express\" : \" ^4.17.1\" , " +
44
+ "\" express\" : \" ^5.0.0\" " +
45
+ "}" +
46
+ "}" ;
47
+
48
+ JsonObject sanitized = JsonSanitizer .sanitize (json );
49
+
50
+ assertEquals ("^5.0.0" , sanitized .getAsJsonObject ("dependencies" ).get ("express" ).getAsString ()); // Last value is kept
51
+ }
52
+
53
+ @ Test
54
+ void testNestedScriptsAndDependencies () {
55
+ String json = "{ " +
56
+ "\" scripts\" : { " +
57
+ "\" build\" : \" tsc\" , " +
58
+ "\" start\" : \" node dist/index.js\" " +
59
+ "}, " +
60
+ "\" dependencies\" : { " +
61
+ "\" typescript\" : \" ^4.0.0\" , " +
62
+ "\" node-fetch\" : \" ^2.6.1\" " +
63
+ "}" +
64
+ "}" ;
65
+
66
+ JsonObject sanitized = JsonSanitizer .sanitize (json );
67
+
68
+ assertEquals ("tsc" , sanitized .getAsJsonObject ("scripts" ).get ("build" ).getAsString ());
69
+ assertEquals ("^4.0.0" , sanitized .getAsJsonObject ("dependencies" ).get ("typescript" ).getAsString ());
70
+ }
71
+
72
+ @ Test
73
+ void testInvalidJson () {
74
+ String json = "{ " +
75
+ "\" name\" : \" my-project\" , " +
76
+ "\" version\" : \" 1.0.0\" " ;
77
+
78
+ assertThrows (JsonSyntaxException .class , () -> JsonSanitizer .sanitize (json ));
79
+ }
80
+
81
+ @ Test
82
+ void testEmptyPackageJson () {
83
+ String json = "{}" ;
84
+
85
+ JsonObject sanitized = JsonSanitizer .sanitize (json );
86
+ assertTrue (sanitized .entrySet ().isEmpty ());
87
+ }
88
+
89
+ @ Test
90
+ void testJsonWithNullValues () {
91
+ String json = "{ " +
92
+ "\" name\" : \" my-project\" , " +
93
+ "\" version\" : null, " +
94
+ "\" dependencies\" : { " +
95
+ "\" express\" : \" ^4.17.1\" , " +
96
+ "\" lodash\" : null " +
97
+ "} " +
98
+ "}" ;
99
+
100
+ JsonObject sanitized = JsonSanitizer .sanitize (json );
101
+
102
+ assertTrue (sanitized .get ("version" ).isJsonNull ());
103
+ assertTrue (sanitized .getAsJsonObject ("dependencies" ).get ("lodash" ).isJsonNull ());
104
+ }
105
+ }
0 commit comments