1
1
package com .blackduck .integration .detectable .util ;
2
2
3
+ import com .google .gson .JsonObject ;
3
4
import com .google .gson .JsonSyntaxException ;
4
- import static org .junit .jupiter .api .Assertions .*;
5
5
import org .junit .jupiter .api .Test ;
6
+ import static org .junit .jupiter .api .Assertions .*;
6
7
7
- public class JsonSanitizerTest {
8
-
8
+ class JsonSanitizerTest {
9
9
@ Test
10
- void testValidJsonNoDuplicates () {
11
- String input = "{\" name\" : \" John\" , \" age\" : 30}" ;
12
- String expected = "{\" name\" :\" John\" ,\" age\" :30}" ;
13
- String actual = JsonSanitizer .sanitize (input );
14
- assertEquals (expected , actual );
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 ());
15
37
}
16
38
17
39
@ Test
18
- void testValidJsonWithDuplicates () {
19
- String input = "{\" name\" : \" John\" , \" name\" : \" Doe\" , \" age\" : 30}" ;
20
- String expected = "{\" name\" :\" Doe\" ,\" age\" :30}" ;
21
- String actual = JsonSanitizer .sanitize (input );
22
- assertEquals (expected , actual );
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
23
51
}
24
52
25
53
@ Test
26
- void testNestedJsonWithDuplicates () {
27
- String input = "{\" person\" : {\" name\" : \" John\" , \" name\" : \" Doe\" }, \" age\" : 30}" ;
28
- String expected = "{\" person\" :{\" name\" :\" Doe\" },\" age\" :30}" ;
29
- String actual = JsonSanitizer .sanitize (input );
30
- assertEquals (expected , actual );
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 ());
31
70
}
32
71
33
72
@ Test
34
73
void testInvalidJson () {
35
- String invalidJson = "{\" name\" :\" example\" ,\" version\" :}" ;
36
- assertThrows (JsonSyntaxException .class , () -> {
37
- JsonSanitizer .sanitize (invalidJson );
38
- });
74
+ String json = "{ " +
75
+ "\" name\" : \" my-project\" , " +
76
+ "\" version\" : \" 1.0.0\" " ;
77
+
78
+ assertThrows (JsonSyntaxException .class , () -> JsonSanitizer .sanitize (json ));
39
79
}
40
80
41
81
@ Test
42
- void testEmptyJson () {
43
- String emptyJson = "{}" ;
44
- String sanitizedJson = JsonSanitizer .sanitize (emptyJson );
45
- assertEquals (emptyJson , sanitizedJson );
82
+ void testEmptyPackageJson () {
83
+ String json = "{}" ;
84
+
85
+ JsonObject sanitized = JsonSanitizer .sanitize (json );
86
+ assertTrue (sanitized .entrySet ().isEmpty ());
46
87
}
47
88
48
89
@ Test
49
90
void testJsonWithNullValues () {
50
- String input = "{\" name\" : null, \" age\" : 30}" ;
51
- String expected = "{\" name\" :null,\" age\" :30}" ;
52
- String actual = JsonSanitizer .sanitize (input );
53
- assertEquals (expected , actual );
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 ());
54
104
}
55
105
}
0 commit comments