Reduce org.json usage in aai-common 10/139710/3
authorFiete Ostkamp <Fiete.Ostkamp@telekom.de>
Wed, 11 Dec 2024 08:21:34 +0000 (09:21 +0100)
committerFiete Ostkamp <fiete.ostkamp@telekom.de>
Wed, 11 Dec 2024 08:27:21 +0000 (08:27 +0000)
- org.json [is slow](https://github.com/fabienrenaud/java-json-benchmark?tab=readme-ov-file#users-model)
- Jackson should be consistently used everywhere
- only XmlFormatTransformer is left, but that warrants a dedicated change with
  (likely) further tests since it appears to be quite a critical execution path
- remove unused imports
- update org.json to a non-vulnerable version

Issue-ID: AAI-4085
Change-Id: I84610523447d70a1729348392ffd302d17e9379d
Signed-off-by: Fiete Ostkamp <Fiete.Ostkamp@telekom.de>
33 files changed:
aai-core/src/main/java/org/onap/aai/introspection/JSONStrategy.java
aai-core/src/main/java/org/onap/aai/kafka/MessageProducer.java [deleted file]
aai-core/src/main/java/org/onap/aai/util/HttpsAuthClient.java
aai-core/src/test/java/org/onap/aai/HttpTestUtil.java
aai-core/src/test/java/org/onap/aai/IntegrationTest.java
aai-core/src/test/java/org/onap/aai/JanusgraphCassandraConfiguration.java
aai-core/src/test/java/org/onap/aai/introspection/JSONStrategyTest.java
aai-core/src/test/java/org/onap/aai/rest/ImpliedDeleteIntegrationTest.java
aai-core/src/test/java/org/onap/aai/rest/NotificationDmaapEventTest.java
aai-core/src/test/java/org/onap/aai/rest/db/HttpEntryNotificationIntegrationTest.java
aai-core/src/test/java/org/onap/aai/rest/notification/NotificationServiceTest.java
aai-parent/pom.xml
aai-rest/src/main/java/org/onap/aai/restclient/AAIRestClient.java
aai-schema-ingest/src/main/java/org/onap/aai/config/RestConfiguration.java
aai-schema-ingest/src/main/java/org/onap/aai/config/TranslatorConfiguration.java
aai-schema-ingest/src/main/java/org/onap/aai/setup/Translator.java
aai-schema-ingest/src/test/java/org/onap/aai/edges/EdgeIngestorWiringTest.java
aai-schema-ingest/src/test/java/org/onap/aai/edges/EdgeRuleQueryTest.java
aai-schema-ingest/src/test/java/org/onap/aai/edges/EdgeRuleTest.java
aai-schema-ingest/src/test/java/org/onap/aai/edges/JsonIngestorTest.java
aai-schema-ingest/src/test/java/org/onap/aai/edges/TypeAlphabetizerTest.java
aai-schema-ingest/src/test/java/org/onap/aai/nodes/NodeIngestorWiringTest.java
aai-schema-ingest/src/test/java/org/onap/aai/validation/CheckEverythingStrategyTest.java
aai-schema-ingest/src/test/java/org/onap/aai/validation/FailFastStrategyTest.java
aai-schema-ingest/src/test/java/org/onap/aai/validation/VersionValidatorRainyDayTest.java
aai-schema-ingest/src/test/java/org/onap/aai/validation/VersionValidatorSunnyDayTest.java
aai-schema-ingest/src/test/java/org/onap/aai/validation/edges/CousinDefaultingValidationModuleTest.java
aai-schema-ingest/src/test/java/org/onap/aai/validation/edges/DefaultEdgeFieldsValidationModuleTest.java
aai-schema-ingest/src/test/java/org/onap/aai/validation/edges/EdgeRuleValidatorSunnyDayTest.java
aai-schema-ingest/src/test/java/org/onap/aai/validation/edges/NodeTypesValidationModuleTest.java
aai-schema-ingest/src/test/java/org/onap/aai/validation/edges/SingleContainmentValidationModuleTest.java
aai-schema-ingest/src/test/java/org/onap/aai/validation/edges/UniqueLabelValidationModuleTest.java
aai-schema-ingest/src/test/java/org/onap/aai/validation/nodes/NodeValidatorSunnyDayTest.java

index 496c1e8..5ecc3a9 100644 (file)
@@ -22,30 +22,41 @@ package org.onap.aai.introspection;
 
 import java.io.UnsupportedEncodingException;
 import java.lang.reflect.InvocationTargetException;
+import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
 
-import org.json.simple.JSONArray;
-import org.json.simple.JSONObject;
 import org.onap.aai.schema.enums.ObjectMetadata;
 import org.onap.aai.schema.enums.PropertyMetadata;
 import org.onap.aai.setup.SchemaVersion;
 
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
 public class JSONStrategy extends Introspector {
 
-    private JSONObject json = null;
+    private JsonNode json = null;
     private String namedType = "";
+    private static final ObjectMapper mapper = new ObjectMapper();
 
     protected JSONStrategy(Object o) {
         super(o);
-        json = (JSONObject) o;
+        json = mapper.valueToTree(o);
         // Assumes you provide a wrapper
-        Set<String> keySet = json.keySet();
-        if (keySet.size() == 1) {
-            namedType = keySet.iterator().next();
-            json = (JSONObject) json.get(namedType);
+        Iterator<Map.Entry<String, JsonNode>> fields = json.fields();
+        if (fields.hasNext()) {
+            Map.Entry<String, JsonNode> entry = fields.next();
+            if (!fields.hasNext()) {
+                namedType = entry.getKey();
+                json = entry.getValue();
+            } else {
+                throw new IllegalArgumentException("This object has no named type.");
+            }
         } else {
             throw new IllegalArgumentException("This object has no named type.");
         }
@@ -53,7 +64,7 @@ public class JSONStrategy extends Introspector {
 
     protected JSONStrategy(Object o, String namedType) {
         super(o);
-        json = (JSONObject) o;
+        json = (JsonNode) o;
         this.namedType = namedType;
 
     }
@@ -74,8 +85,7 @@ public class JSONStrategy extends Introspector {
 
     @Override
     public void setValue(String name, Object obj) {
-        json.put(name, obj);
-
+        ((ObjectNode) json).set(name, (JsonNode) obj);
     }
 
     @Override
@@ -85,8 +95,14 @@ public class JSONStrategy extends Introspector {
 
     @Override
     public Set<String> getProperties() {
-        Set<String> result = json.keySet();
-        return result;
+        Iterator<String> fieldNames = json.fieldNames();
+        Set<String> properties = new HashSet<>();
+
+        // Iterate through the iterator and add each element to the set
+        while (fieldNames.hasNext()) {
+            properties.add(fieldNames.next());
+        }
+        return properties;
     }
 
     @Override
@@ -150,7 +166,7 @@ public class JSONStrategy extends Introspector {
     public Class<?> getGenericTypeClass(String name) {
         Class<?> resultClass = null;
         Object resultObject = this.getValue(name);
-        if (resultObject instanceof JSONArray) {
+        if (resultObject instanceof ArrayNode) {
             resultClass = ((List<?>) resultObject).get(0).getClass();
         }
 
diff --git a/aai-core/src/main/java/org/onap/aai/kafka/MessageProducer.java b/aai-core/src/main/java/org/onap/aai/kafka/MessageProducer.java
deleted file mode 100644 (file)
index 09fc68a..0000000
+++ /dev/null
@@ -1,36 +0,0 @@
-/**
- * ============LICENSE_START=======================================================
- * org.onap.aai
- * ================================================================================
- * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *    http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.aai.kafka;
-
-import org.json.JSONObject;
-
-/**
- * MessageProducer interface based on untyped messages
- *
- * @deprecated use {@link org.onap.aai.kafka.NotificationProducer} instead
- */
-@Deprecated
-public interface MessageProducer {
-
-    void sendMessageToDefaultDestination(JSONObject finalJson);
-
-    void sendMessageToDefaultDestination(String msg);
-}
index b31bfcb..2320159 100644 (file)
@@ -39,9 +39,7 @@ import javax.ws.rs.client.ClientBuilder;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 
-import org.apache.commons.configuration2.JSONConfiguration;
 import org.glassfish.jersey.client.ClientConfig;
-import org.glassfish.jersey.client.ClientProperties;
 import org.onap.aai.aailog.filter.RestControllerClientRequestLoggingInterceptor;
 import org.onap.aai.aailog.filter.RestControllerClientResponseLoggingInterceptor;
 import org.onap.aai.exceptions.AAIException;
index 62f63ea..2759ab9 100644 (file)
@@ -21,7 +21,6 @@
 package org.onap.aai;
 
 import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.when;
 
index 9ee5fce..9398ed3 100644 (file)
@@ -22,7 +22,6 @@ package org.onap.aai;
 
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.onap.aai.config.ConfigConfiguration;
-import org.onap.aai.config.GraphConfig;
 import org.onap.aai.config.IntrospectionConfig;
 import org.onap.aai.config.KafkaConfig;
 import org.onap.aai.config.RestBeanConfig;
@@ -36,7 +35,6 @@ import org.onap.aai.prevalidation.ValidationService;
 import org.onap.aai.rest.notification.NotificationService;
 import org.onap.aai.serialization.db.EdgeSerializer;
 import org.onap.aai.setup.AAIConfigTranslator;
-import org.onap.aai.util.GraphChecker;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.TestPropertySource;
 import org.springframework.test.context.junit.jupiter.SpringExtension;
index 08c9917..f991444 100644 (file)
@@ -5,9 +5,6 @@ import java.io.FileNotFoundException;
 import org.apache.commons.configuration2.Configuration;
 import org.apache.commons.configuration2.PropertiesConfiguration;
 import org.apache.commons.configuration2.ex.ConfigurationException;
-import org.janusgraph.core.JanusGraphProperty;
-import org.janusgraph.core.schema.JanusGraphConfiguration;
-import org.onap.aai.dbmap.AAIGraphConfig;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.test.context.TestConfiguration;
 import org.springframework.context.annotation.Bean;
index 4982f1d..b1e22f2 100644 (file)
@@ -23,15 +23,19 @@ package org.onap.aai.introspection;
 import java.util.HashSet;
 import java.util.Set;
 
-import org.json.simple.JSONArray;
-import org.json.simple.JSONObject;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
 import org.onap.aai.AAISetup;
 
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
 @Ignore("Not a used/flushed out feature")
+// This has been converted from org.json to Jackson,
+// but not in a way that tests are working
 public class JSONStrategyTest extends AAISetup {
     private JSONStrategy jsonStrategy;
     private JSONStrategy jsonStrategyContainer;
@@ -40,21 +44,25 @@ public class JSONStrategyTest extends AAISetup {
     @Before
     public void setup() {
         try {
-            JSONObject pserver = new JSONObject();
+
+            ObjectMapper mapper = new ObjectMapper();
+            ObjectNode pserver = mapper.createObjectNode();
+
             pserver.put("hostname", "value1");
             pserver.put("numberofCpus", 4);
             jsonStrategy = new JSONStrategy(pserver, "pserver-type");
 
             // The values of this object are arrays containing JSONObjects
-            JSONArray pservers = new JSONArray();
+            ArrayNode pservers = mapper.createArrayNode();
             pservers.add(pserver);
-            JSONObject container = new JSONObject();
-            container.put("pservers", pservers);
+
+            ObjectNode container = mapper.createObjectNode();
+            container.set("pservers", pservers);
             jsonStrategyContainer = new JSONStrategy(container, "pservers-type");
 
             // The values of this object are JSONObjects
-            JSONObject complex = new JSONObject();
-            complex.put("pserver", pserver);
+            ObjectNode complex = mapper.createObjectNode();
+            complex.set("pserver", pserver);
             jsonStrategyComplex = new JSONStrategy(complex, "pservers-type");
         } catch (Exception e) {
             System.out.println("error during setup: " + e.getMessage());
@@ -96,8 +104,8 @@ public class JSONStrategyTest extends AAISetup {
 
     @Test
     public void getJavaClassNameTest() {
-        Assert.assertEquals("org.json.simple.JSONObject", jsonStrategy.getJavaClassName());
-        Assert.assertEquals("org.json.simple.JSONObject", jsonStrategyContainer.getJavaClassName());
+        Assert.assertEquals("com.fasterxml.jackson.databind.node.ObjectNode", jsonStrategy.getJavaClassName());
+        Assert.assertEquals("com.fasterxml.jackson.databind.node.ObjectNode", jsonStrategyContainer.getJavaClassName());
     }
 
     @Test
index 3873128..48c3e60 100644 (file)
@@ -47,7 +47,6 @@ import org.onap.aai.db.props.AAIProperties;
 import org.onap.aai.dbmap.AAIGraph;
 import org.onap.aai.domain.notificationEvent.NotificationEvent;
 import org.onap.aai.domain.notificationEvent.NotificationEvent.EventHeader;
-import org.onap.aai.introspection.ModelType;
 import org.onap.aai.rest.notification.UEBNotification;
 import org.onap.aai.serialization.engines.QueryStyle;
 import org.skyscreamer.jsonassert.JSONAssert;
index 0eafdf1..50a8176 100644 (file)
@@ -59,7 +59,6 @@ import org.onap.aai.dbmap.AAIGraph;
 import org.onap.aai.domain.notificationEvent.NotificationEvent;
 import org.onap.aai.domain.notificationEvent.NotificationEvent.EventHeader;
 import org.onap.aai.exceptions.AAIException;
-import org.onap.aai.introspection.ModelType;
 import org.onap.aai.rest.notification.UEBNotification;
 import org.onap.aai.serialization.engines.QueryStyle;
 import org.skyscreamer.jsonassert.JSONAssert;
index 6b90782..ba7fcae 100644 (file)
@@ -25,7 +25,6 @@ import static org.hamcrest.Matchers.not;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertEquals;
 import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.when;
 
 import java.io.UnsupportedEncodingException;
index 4fd2149..a042d5d 100644 (file)
@@ -24,7 +24,6 @@ import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.ArgumentMatchers.anyList;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.doNothing;
-import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
index c55f7b1..f4933e3 100644 (file)
@@ -88,7 +88,7 @@ limitations under the License.
       -->
     <spring.boot.version>2.7.18</spring.boot.version>
     <json.path.version>2.2.0</json.path.version>
-    <json.version>20190722</json.version>
+    <json.version>20240303</json.version>
     <junit.version>4.12</junit.version>
     <httpclient.version>4.5.13</httpclient.version>
     <io.swagger.version>1.5.24</io.swagger.version>
index 393c663..3e4eceb 100644 (file)
@@ -27,8 +27,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
-import org.springframework.context.annotation.Conditional;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.MediaType;
 import org.springframework.stereotype.Component;
index c46ab78..01e265f 100644 (file)
 
 package org.onap.aai.config;
 
-import org.onap.aai.restclient.*;
+import org.onap.aai.restclient.RestClient;
+import org.onap.aai.restclient.SchemaServiceNoAuthClient;
+import org.onap.aai.restclient.SchemaServiceOneWayClient;
+import org.onap.aai.restclient.SchemaServiceRestClient;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
index d49e6ec..8498d90 100644 (file)
@@ -22,7 +22,7 @@
 
 package org.onap.aai.config;
 
-import org.onap.aai.setup.*;
+import org.onap.aai.setup.ConfigTranslator;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.context.annotation.Configuration;
index 45ca0cd..a9144a6 100644 (file)
@@ -20,7 +20,8 @@
 
 package org.onap.aai.setup;
 
-import java.io.*;
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.List;
 
 /**
index fa7f04c..e780b2e 100644 (file)
@@ -20,7 +20,8 @@
 
 package org.onap.aai.edges;
 
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import com.google.common.collect.Multimap;
 import org.junit.jupiter.api.Test;
index 3396b93..ecca4c8 100644 (file)
@@ -20,7 +20,7 @@
 
 package org.onap.aai.edges;
 
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import com.jayway.jsonpath.DocumentContext;
 import com.jayway.jsonpath.JsonPath;
index 798a32f..59561f6 100644 (file)
@@ -20,7 +20,7 @@
 
 package org.onap.aai.edges;
 
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.util.HashMap;
 import java.util.Map;
index 66fbcce..106ac15 100644 (file)
@@ -22,7 +22,7 @@ package org.onap.aai.edges;
 
 import static com.jayway.jsonpath.Criteria.where;
 import static com.jayway.jsonpath.Filter.filter;
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import com.jayway.jsonpath.DocumentContext;
 import com.jayway.jsonpath.Filter;
index b106310..d6de1c4 100644 (file)
@@ -20,7 +20,7 @@
 
 package org.onap.aai.edges;
 
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import org.junit.jupiter.api.Test;
 
index 07631ce..fa7d001 100644 (file)
@@ -20,7 +20,7 @@
 
 package org.onap.aai.nodes;
 
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import org.eclipse.persistence.dynamic.DynamicEntity;
 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
index 0022e15..d78976d 100644 (file)
@@ -20,7 +20,8 @@
 
 package org.onap.aai.validation;
 
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import org.junit.jupiter.api.Test;
 
index 043e9c7..9aef2c4 100644 (file)
@@ -20,7 +20,8 @@
 
 package org.onap.aai.validation;
 
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import org.junit.jupiter.api.Test;
 
index bcf79d2..49dcbe5 100644 (file)
@@ -20,7 +20,8 @@
 
 package org.onap.aai.validation;
 
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import org.junit.jupiter.api.Test;
 import org.onap.aai.config.NodesConfiguration;
index f2bb85a..d3012c8 100644 (file)
@@ -20,7 +20,7 @@
 
 package org.onap.aai.validation;
 
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import org.junit.jupiter.api.Test;
 import org.onap.aai.config.NodesConfiguration;
index 53cad53..42dd859 100644 (file)
@@ -20,7 +20,8 @@
 
 package org.onap.aai.validation.edges;
 
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import org.junit.jupiter.api.Test;
 import org.onap.aai.config.NodesConfiguration;
index 191a10d..78e8e25 100644 (file)
@@ -20,7 +20,7 @@
 
 package org.onap.aai.validation.edges;
 
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.util.ArrayList;
 import java.util.List;
index d8a6933..e478da1 100644 (file)
@@ -20,7 +20,8 @@
 
 package org.onap.aai.validation.nodes;
 
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import org.junit.jupiter.api.Test;
 import org.onap.aai.config.NodesConfiguration;