Skip to content

Commit e490c19

Browse files
ihraskorovarga
authored andcommitted
Use strict JSON parser configuration
With current JSON library we can switch to use JSON parser configuration with strict mode. See: https://github.com/stleary/JSON-java/releases/tag/20241224 stleary/JSON-java#921 Change-Id: Ib689f144a9f28f37e113c550646fe8640257cc79 Signed-off-by: Ivan Hrasko <[email protected]>
1 parent 148bd6a commit e490c19

File tree

8 files changed

+22
-10
lines changed

8 files changed

+22
-10
lines changed

apps/restconf-it/src/test/java/org/opendaylight/restconf/it/openapi/AbstractOpenApiTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.apache.shiro.realm.AuthenticatingRealm;
4040
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
4141
import org.json.JSONObject;
42+
import org.json.JSONParserConfiguration;
4243
import org.junit.jupiter.api.AfterAll;
4344
import org.junit.jupiter.api.AfterEach;
4445
import org.junit.jupiter.api.BeforeAll;
@@ -96,6 +97,9 @@
9697
import org.skyscreamer.jsonassert.JSONCompareMode;
9798

9899
class AbstractOpenApiTest extends AbstractDataBrokerTest {
100+
private static final JSONParserConfiguration JSON_PARSER_CONFIGURATION = new JSONParserConfiguration()
101+
.withStrictMode();
102+
99103
private static final ErrorTagMapping ERROR_TAG_MAPPING = ErrorTagMapping.RFC8040;
100104
private static final String TOPOLOGY_URI =
101105
"/rests/data/network-topology:network-topology/topology=topology-netconf";
@@ -317,7 +321,7 @@ protected void mountDeviceJson(final int devicePort) throws Exception {
317321
private boolean deviceConnectedJson() throws Exception {
318322
final var response = invokeRequest(HttpMethod.GET, DEVICE_STATUS_URI);
319323
assertEquals(HttpResponseStatus.OK, response.status());
320-
final var json = new JSONObject(response.content().toString(StandardCharsets.UTF_8));
324+
final var json = new JSONObject(response.content().toString(StandardCharsets.UTF_8), JSON_PARSER_CONFIGURATION);
321325
//{
322326
// "netconf-node-topology:netconf-node": {
323327
// "connection-status": "connected"

apps/restconf-it/src/test/java/org/opendaylight/restconf/it/server/AbstractE2ETest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.apache.shiro.realm.AuthenticatingRealm;
4848
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
4949
import org.json.JSONObject;
50+
import org.json.JSONParserConfiguration;
5051
import org.junit.jupiter.api.AfterAll;
5152
import org.junit.jupiter.api.AfterEach;
5253
import org.junit.jupiter.api.BeforeAll;
@@ -106,6 +107,8 @@
106107
import org.xmlunit.diff.ElementSelectors;
107108

108109
abstract class AbstractE2ETest extends AbstractDataBrokerTest {
110+
static final JSONParserConfiguration JSON_PARSER_CONFIGURATION = new JSONParserConfiguration().withStrictMode();
111+
109112
private static final Logger LOG = LoggerFactory.getLogger(AbstractE2ETest.class);
110113
private static final ErrorTagMapping ERROR_TAG_MAPPING = ErrorTagMapping.RFC8040;
111114
private static final Splitter COMMA_SPLITTER = Splitter.on(',').trimResults().omitEmptyStrings();
@@ -331,7 +334,7 @@ protected static void assertErrorResponseJson(final FullHttpResponse response, f
331334
// }]
332335
// }
333336
// }
334-
final var json = new JSONObject(response.content().toString(StandardCharsets.UTF_8));
337+
final var json = new JSONObject(response.content().toString(StandardCharsets.UTF_8), JSON_PARSER_CONFIGURATION);
335338
final var error = json.getJSONObject("errors").getJSONArray("error").getJSONObject(0);
336339
assertNotNull(error);
337340
assertEquals(expectedErrorType.elementBody(), error.getString("error-type"));
@@ -414,7 +417,7 @@ private static URI extractStreamUrlJson(final String content) {
414417
// "description": "..."
415418
// }]
416419
// }
417-
final var json = new JSONObject(content);
420+
final var json = new JSONObject(content, JSON_PARSER_CONFIGURATION);
418421
final var stream = json.getJSONArray("ietf-restconf-monitoring:stream").getJSONObject(0);
419422
for (var access : stream.getJSONArray("access")) {
420423
final var accessObj = (JSONObject) access;

apps/restconf-it/src/test/java/org/opendaylight/restconf/it/server/AuthTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private String createStream() throws Exception {
8484
}
8585

8686
private static String extractStreamNameJson(final String content) {
87-
final var json = new JSONObject(content);
87+
final var json = new JSONObject(content, JSON_PARSER_CONFIGURATION);
8888
return json.getJSONObject("sal-remote:output").getString("stream-name");
8989
}
9090

apps/restconf-it/src/test/java/org/opendaylight/restconf/it/server/MountPointE2ETest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ void notificationStreamJsonTest() throws Exception {
211211
// "stream-name": "urn:uuid:01a56682-f2ab-419f-8a77-9cf995a52220"
212212
// }
213213
// }
214-
final var json = new JSONObject(response.content().toString(StandardCharsets.UTF_8));
214+
final var json = new JSONObject(response.content().toString(StandardCharsets.UTF_8), JSON_PARSER_CONFIGURATION);
215215
final var streamName = json.getJSONObject("odl-device-notification:output").getString("stream-name");
216216
assertNotNull(streamName, "Stream name is undefined");
217217

@@ -372,7 +372,7 @@ private void mountDeviceJson() throws Exception {
372372
private boolean deviceConnectedJson() throws Exception {
373373
final var response = invokeRequest(HttpMethod.GET, DEVICE_STATUS_URI);
374374
assertEquals(HttpResponseStatus.OK, response.status());
375-
final var json = new JSONObject(response.content().toString(StandardCharsets.UTF_8));
375+
final var json = new JSONObject(response.content().toString(StandardCharsets.UTF_8), JSON_PARSER_CONFIGURATION);
376376
//{
377377
// "netconf-node-topology:netconf-node": {
378378
// "connection-status": "connected"

apps/restconf-it/src/test/java/org/opendaylight/restconf/it/server/StreamsE2ETest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void dataChangeEventStreamJsonTest() throws Exception {
7777
// "stream-name":"urn:uuid:6413c077-5dfe-464c-b17f-20c5bbb456f4"
7878
// }
7979
// }
80-
final var json = new JSONObject(response.content().toString(StandardCharsets.UTF_8));
80+
final var json = new JSONObject(response.content().toString(StandardCharsets.UTF_8), JSON_PARSER_CONFIGURATION);
8181
final var streamName = json.getJSONObject("sal-remote:output").getString("stream-name");
8282
assertNotNull(streamName, "Stream name is undefined");
8383

apps/restconf-it/src/test/java/org/opendaylight/restconf/it/subscription/AbstractNotificationSubscriptionTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.apache.shiro.realm.AuthenticatingRealm;
3939
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
4040
import org.json.JSONObject;
41+
import org.json.JSONParserConfiguration;
4142
import org.junit.jupiter.api.AfterAll;
4243
import org.junit.jupiter.api.AfterEach;
4344
import org.junit.jupiter.api.BeforeAll;
@@ -89,6 +90,8 @@
8990
import org.slf4j.LoggerFactory;
9091

9192
abstract class AbstractNotificationSubscriptionTest extends AbstractDataBrokerTest {
93+
static final JSONParserConfiguration JSON_PARSER_CONFIGURATION = new JSONParserConfiguration().withStrictMode();
94+
9295
private static final Logger LOG = LoggerFactory.getLogger(AbstractNotificationSubscriptionTest.class);
9396
private static final YangParserFactory PARSER_FACTORY = ServiceLoader.load(YangParserFactory.class)
9497
.findFirst().orElseThrow(() -> new ExceptionInInitializerError("No YangParserFactory found"));
@@ -404,7 +407,8 @@ FullHttpResponse establishFilteredSubscription(final String filter, final HTTPCl
404407
* Utility method to extract subscription ID from response.
405408
*/
406409
static long extractSubscriptionId(final FullHttpResponse response) {
407-
final var jsonContent = new JSONObject(response.content().toString(StandardCharsets.UTF_8));
410+
final var jsonContent = new JSONObject(response.content().toString(StandardCharsets.UTF_8),
411+
JSON_PARSER_CONFIGURATION);
408412
return jsonContent.getJSONObject("ietf-subscribed-notifications:output").getLong("id");
409413
}
410414
}

apps/restconf-it/src/test/java/org/opendaylight/restconf/it/subscription/CountersSubscriptionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ void excludedCounterNotificationTest() throws Exception {
175175
}
176176

177177
private static void assertCounter(final FullHttpResponse response, final String sent, final String excluded) {
178-
final var json = new JSONObject(response.content().toString(StandardCharsets.UTF_8))
178+
final var json = new JSONObject(response.content().toString(StandardCharsets.UTF_8), JSON_PARSER_CONFIGURATION)
179179
.getJSONObject("ietf-subscribed-notifications:receivers")
180180
//there is only one receiver
181181
.getJSONArray("receiver").getJSONObject(0);

apps/restconf-it/src/test/java/org/opendaylight/restconf/it/subscription/NotificationSubscriptionListeningTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ public void beforeEach() throws Exception {
6767
assertEquals(HttpResponseStatus.OK, response.status());
6868

6969
// Extract subscription ID from response
70-
final var jsonContent = new JSONObject(response.content().toString(StandardCharsets.UTF_8));
70+
final var jsonContent = new JSONObject(response.content().toString(StandardCharsets.UTF_8),
71+
JSON_PARSER_CONFIGURATION);
7172
final var subscriptionId = jsonContent.getJSONObject("ietf-subscribed-notifications:output").getLong("id");
7273

7374
// Start listening on notifications

0 commit comments

Comments
 (0)