Replace deprecated Nashorn javascript engine 19/100519/2
authorJim Hahn <jrh3@att.com>
Mon, 20 Jan 2020 22:39:32 +0000 (17:39 -0500)
committerJim Hahn <jrh3@att.com>
Mon, 20 Jan 2020 22:48:21 +0000 (17:48 -0500)
Some test drivers use javascript to evaluate expected results.
In java 8, the standard javascript engine was Nashorn, but that has
been deprecated.  As the test scripts use a simple xxx.yyy.zzz notation,
decided to go with a small expression language, Apache JEXL, instead.

Issue-ID: POLICY-2324
Signed-off-by: Jim Hahn <jrh3@att.com>
Change-Id: Iffff468a880c612bdb31da84964dfa4132fa15e3

utils-test/pom.xml
utils-test/src/main/java/org/onap/policy/common/utils/gson/GsonTestUtils.java
utils-test/src/test/java/org/onap/policy/common/utils/gson/GsonTestUtilsTest.java

index 028a6f8..4b6a4c6 100644 (file)
     </properties>
 
     <dependencies>
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-jexl3</artifactId>
+            <version>3.0</version>
+        </dependency>
         <dependency>
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>
index f37f32a..6ae42fa 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * policy-management
  * ================================================================================
- * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2020 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.
@@ -39,10 +39,11 @@ 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 org.apache.commons.jexl3.JexlBuilder;
+import org.apache.commons.jexl3.JexlContext;
+import org.apache.commons.jexl3.JexlEngine;
+import org.apache.commons.jexl3.JexlException;
+import org.apache.commons.jexl3.MapContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -61,7 +62,7 @@ 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.
@@ -199,9 +200,9 @@ public class GsonTestUtils {
         }
 
         // 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();
@@ -222,10 +223,10 @@ public class GsonTestUtils {
                  * 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);
+                Object result = eng.createExpression(script).evaluate(context);
                 bldr.append(result == null ? "null" : result.toString());
 
-            } catch (ScriptException e) {
+            } catch (JexlException e) {
                 throw new JsonParseException("cannot expand element: " + mat.group(), e);
             }
         }
@@ -241,10 +242,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;
index 35fea57..11ee63c 100644 (file)
@@ -34,7 +34,7 @@ import com.google.gson.JsonParseException;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
-import javax.script.ScriptException;
+import org.apache.commons.jexl3.JexlException;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -139,7 +139,7 @@ public class GsonTestUtilsTest {
 
         assertThatThrownBy(() -> utils.applyScripts("use ${obj.text} this", null))
                         .isInstanceOf(JsonParseException.class)
-                        .hasCauseInstanceOf(ScriptException.class)
+                        .hasCauseInstanceOf(JexlException.class)
                         .hasMessage("cannot expand element: ${obj.text}");
     }