aboutsummaryrefslogtreecommitdiffstats
path: root/utils-test/src/main/java/org/onap/policy/common/utils/gson/GsonTestUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'utils-test/src/main/java/org/onap/policy/common/utils/gson/GsonTestUtils.java')
-rw-r--r--utils-test/src/main/java/org/onap/policy/common/utils/gson/GsonTestUtils.java90
1 files changed, 38 insertions, 52 deletions
diff --git a/utils-test/src/main/java/org/onap/policy/common/utils/gson/GsonTestUtils.java b/utils-test/src/main/java/org/onap/policy/common/utils/gson/GsonTestUtils.java
index f37f32a1..8276ea4c 100644
--- a/utils-test/src/main/java/org/onap/policy/common/utils/gson/GsonTestUtils.java
+++ b/utils-test/src/main/java/org/onap/policy/common/utils/gson/GsonTestUtils.java
@@ -2,7 +2,8 @@
* ============LICENSE_START=======================================================
* policy-management
* ================================================================================
- * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2024 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,28 +28,28 @@ import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
+import com.google.re2j.Pattern;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
-import java.net.URL;
-import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-import javax.script.Bindings;
-import javax.script.ScriptEngine;
-import javax.script.ScriptEngineManager;
-import javax.script.ScriptException;
+import lombok.AccessLevel;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import org.apache.commons.jexl3.JexlBuilder;
+import org.apache.commons.jexl3.JexlContext;
+import org.apache.commons.jexl3.JexlEngine;
+import org.apache.commons.jexl3.MapContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Utilities used to test encoding and decoding of Policy objects.
*/
+@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class GsonTestUtils {
private static final Logger logger = LoggerFactory.getLogger(GsonTestUtils.class);
@@ -61,11 +62,12 @@ public class GsonTestUtils {
/**
* Engine used to interpolate strings before they're compared.
*/
- private static ScriptEngine engineInstance = null;
+ private static JexlEngine engineInstance = null;
/**
* Used to encode and decode an object via gson.
*/
+ @Getter
private Gson gson;
/**
@@ -78,19 +80,6 @@ public class GsonTestUtils {
}
/**
- * Constructs the object.
- *
- * @param gson used to encode via gson
- */
- protected GsonTestUtils(Gson gson) {
- this.gson = gson;
- }
-
- public Gson getGson() {
- return gson;
- }
-
- /**
* Serializes and then deserializes an object using gson.
*
* @param object the object to be serialized
@@ -109,7 +98,6 @@ public class GsonTestUtils {
*
* @param object the object to be encoded
* @param expected the expected value
- * @throws Exception if the file cannot be read
*/
public void compareGson(Object object, Class<?> expected) {
compareGson(object, new File(expected.getSimpleName() + ".json"));
@@ -122,11 +110,10 @@ public class GsonTestUtils {
*
* @param object the object to be encoded
* @param expected the expected value
- * @throws Exception if the file cannot be read
*/
public void compareGson(Object object, File expected) {
// file is not required to have a full path - find it via getResource()
- URL url = object.getClass().getResource(expected.getName());
+ var url = object.getClass().getResource(expected.getName());
if (url == null) {
throw new JsonParseException(new FileNotFoundException(expected.getName()));
}
@@ -168,7 +155,11 @@ public class GsonTestUtils {
JsonElement gsonjo = reorder(gson.fromJson(sgson, JsonElement.class));
JsonElement expjo = reorder(expected);
- assertEquals(expjo.toString(), gsonjo.toString());
+ /*
+ * As this method is only used within junit tests, it is OK to use assert calls,
+ * thus sonar is disabled.
+ */
+ assertEquals(expjo.toString(), gsonjo.toString()); // NOSONAR
}
/**
@@ -178,7 +169,7 @@ public class GsonTestUtils {
* @throws IOException if an error occurs
*/
protected String readFile(File file) throws IOException {
- return new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
+ return Files.readString(file.toPath());
}
@@ -192,42 +183,37 @@ public class GsonTestUtils {
* @return the text, after interpolating the script elements
*/
public String applyScripts(String text, Object object) {
- Matcher mat = SCRIPT_PAT.matcher(text);
+ var mat = SCRIPT_PAT.matcher(text);
if (!mat.find()) {
// contains no script elements - just return it as is
return text;
}
// bind the object to the variable, "obj"
- ScriptEngine eng = getEngine();
- Bindings bindings = eng.createBindings();
- bindings.put("obj", object);
+ JexlEngine eng = getEngine();
+ JexlContext context = new MapContext();
+ context.set("obj", object);
// work our way through the text, interpolating script elements as we go
- StringBuilder bldr = new StringBuilder();
- int ilast = 0;
+ var bldr = new StringBuilder();
+ var ilast = 0;
mat.reset();
while (mat.find(ilast)) {
// append segment that appears between last match and this
int inext = mat.start();
- bldr.append(text.substring(ilast, inext));
+ bldr.append(text, ilast, inext);
// next match begins after the current match
ilast = mat.end();
// interpolate the script
String script = mat.group(1);
- try {
- /*
- * Note: must use "eng" instead of "engineInstance" to ensure that we use
- * the same engine that's associated with the bindings.
- */
- Object result = eng.eval(script, bindings);
- bldr.append(result == null ? "null" : result.toString());
-
- } catch (ScriptException e) {
- throw new JsonParseException("cannot expand element: " + mat.group(), e);
- }
+ /*
+ * Note: must use "eng" instead of "engineInstance" to ensure that we use
+ * the same engine that's associated with the bindings.
+ */
+ Object result = eng.createExpression(script).evaluate(context);
+ bldr.append(result == null ? "null" : result.toString());
}
// append final segment
@@ -241,10 +227,10 @@ public class GsonTestUtils {
*
* @return the script engine
*/
- private static ScriptEngine getEngine() {
+ private static JexlEngine getEngine() {
if (engineInstance == null) {
// race condition here, but it's ok to overwrite with a new engine
- engineInstance = new ScriptEngineManager().getEngineByName("javascript");
+ engineInstance = new JexlBuilder().create();
}
return engineInstance;
@@ -270,11 +256,11 @@ public class GsonTestUtils {
* @return a new object, without the null items
*/
public JsonObject reorder(JsonObject jsonObj) {
- JsonObject newjo = new JsonObject();
+ var newjo = new JsonObject();
// sort the keys before copying to the new object
List<Entry<String, JsonElement>> sortedSet = new ArrayList<>(jsonObj.entrySet());
- Collections.sort(sortedSet, (left, right) -> left.getKey().compareTo(right.getKey()));
+ sortedSet.sort(Entry.comparingByKey());
for (Entry<String, JsonElement> ent : sortedSet) {
JsonElement val = ent.getValue();
@@ -296,7 +282,7 @@ public class GsonTestUtils {
* @return a new array, with null items removed from all elements
*/
public JsonArray reorder(JsonArray jsonArray) {
- JsonArray newarr = new JsonArray();
+ var newarr = new JsonArray();
for (JsonElement ent : jsonArray) {
newarr.add(reorder(ent));
}