- 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>
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.");
}
protected JSONStrategy(Object o, String namedType) {
super(o);
- json = (JSONObject) o;
+ json = (JsonNode) o;
this.namedType = namedType;
}
@Override
public void setValue(String name, Object obj) {
- json.put(name, obj);
-
+ ((ObjectNode) json).set(name, (JsonNode) obj);
}
@Override
@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
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();
}
+++ /dev/null
-/**
- * ============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);
-}
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;
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;
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;
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;
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;
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;
@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());
@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
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;
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;
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;
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;
-->
<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>
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;
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;
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;
package org.onap.aai.setup;
-import java.io.*;
+import java.io.IOException;
+import java.io.InputStream;
import java.util.List;
/**
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
package org.onap.aai.validation.edges;
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import com.jayway.jsonpath.DocumentContext;
package org.onap.aai.validation.edges;
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.HashMap;
import java.util.Map;
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;
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;
package org.onap.aai.validation.edges;
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import com.jayway.jsonpath.DocumentContext;
package org.onap.aai.validation.edges;
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import com.jayway.jsonpath.DocumentContext;
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;