diff --git a/modules/core/src/main/antlr4/org/apache/synapse/util/synapse_expression/ExpressionLexer.g4 b/modules/core/src/main/antlr4/org/apache/synapse/util/synapse_expression/ExpressionLexer.g4 index 5429268c0d..7062a0b87c 100644 --- a/modules/core/src/main/antlr4/org/apache/synapse/util/synapse_expression/ExpressionLexer.g4 +++ b/modules/core/src/main/antlr4/org/apache/synapse/util/synapse_expression/ExpressionLexer.g4 @@ -5,10 +5,10 @@ JSONPATH_PARAMS: 'in' | 'nin' | 'subsetof' | 'anyof' | 'noneof' | 'size' | 'emp JSONPATH_FUNCTIONS: 'length()' | 'size()' | 'min()' | 'max()' | 'avg()' | 'sum()' | 'stddev()' | 'keys()' | 'first()' | 'last()'; // Tokens for identifiers, operators, and keywords -VAR: 'var'; +VAR: 'vars'; PAYLOAD: 'payload' | '$'; HEADERS: 'headers'; -CONFIG: 'config'; +CONFIG: 'configs'; PARAMS: 'params'; PROPERTY: 'props' | 'properties'; AND: 'and' | '&&'; diff --git a/modules/core/src/main/antlr4/org/apache/synapse/util/synapse_expression/ExpressionParser.g4 b/modules/core/src/main/antlr4/org/apache/synapse/util/synapse_expression/ExpressionParser.g4 index 17883993b2..c71c1d6c21 100644 --- a/modules/core/src/main/antlr4/org/apache/synapse/util/synapse_expression/ExpressionParser.g4 +++ b/modules/core/src/main/antlr4/org/apache/synapse/util/synapse_expression/ExpressionParser.g4 @@ -43,6 +43,15 @@ factor | LPAREN expression RPAREN ; +keywords + : DOT VAR + | DOT PAYLOAD + | DOT HEADERS + | DOT CONFIG + | DOT PARAMS + | DOT PROPERTY + ; + configAccess : CONFIG propertyName ; @@ -61,6 +70,7 @@ propertyAccess propertyName : DOT ID + | keywords | (DOT)? LBRACKET STRING_LITERAL RBRACKET ; @@ -74,9 +84,10 @@ literal jsonPathExpression :( DOT JSONPATH_FUNCTIONS - |DOUBLE_DOT ASTERISK + | DOUBLE_DOT ASTERISK | DOUBLE_DOT ID | DOT ID + | keywords | (DOT)? LBRACKET arrayIndex RBRACKET | DOT ASTERISK | DOUBLE_DOT ID (LBRACKET arrayIndex RBRACKET)?)+ @@ -86,6 +97,7 @@ jsonPathExpression variableAccess : VAR ( DOT ID | DOT STRING_LITERAL + | keywords | (DOT)? LBRACKET STRING_LITERAL RBRACKET // Bracket notation: var["variableName"] ) ( jsonPathExpression )? diff --git a/modules/core/src/main/java/org/apache/synapse/util/synapse/expression/ast/PayloadAccessNode.java b/modules/core/src/main/java/org/apache/synapse/util/synapse/expression/ast/PayloadAccessNode.java index 0827448bc8..49bbbb550f 100644 --- a/modules/core/src/main/java/org/apache/synapse/util/synapse/expression/ast/PayloadAccessNode.java +++ b/modules/core/src/main/java/org/apache/synapse/util/synapse/expression/ast/PayloadAccessNode.java @@ -134,8 +134,8 @@ public ExpressionResult evaluate(EvaluationContext context, boolean isObjectValu result = variable; } else if (variable instanceof Map) { if (StringUtils.isNotEmpty(expressionToEvaluate)) { - expressionToEvaluate = expressionToEvaluate.startsWith(".") ? "var" + expressionToEvaluate - : "var." + expressionToEvaluate; + expressionToEvaluate = expressionToEvaluate.startsWith(".") ? "vars" + expressionToEvaluate + : "vars." + expressionToEvaluate; } String[] keyAndExpression = ExpressionUtils.extractVariableAndJsonPath(expressionToEvaluate); String key = keyAndExpression[0]; diff --git a/modules/core/src/main/java/org/apache/synapse/util/synapse/expression/ast/SignedExpressionNode.java b/modules/core/src/main/java/org/apache/synapse/util/synapse/expression/ast/SignedExpressionNode.java index 7f2aaabea9..0bed371189 100644 --- a/modules/core/src/main/java/org/apache/synapse/util/synapse/expression/ast/SignedExpressionNode.java +++ b/modules/core/src/main/java/org/apache/synapse/util/synapse/expression/ast/SignedExpressionNode.java @@ -20,7 +20,7 @@ import org.apache.synapse.util.synapse.expression.context.EvaluationContext; /** - * Represents a node in the AST that holds a signed expression. ex: ( -var.num1 ) + * Represents a node in the AST that holds a signed expression. ex: ( -vars.num1 ) */ public class SignedExpressionNode implements ExpressionNode { private final ExpressionNode expression; diff --git a/modules/core/src/main/java/org/apache/synapse/util/synapse/expression/utils/ExpressionUtils.java b/modules/core/src/main/java/org/apache/synapse/util/synapse/expression/utils/ExpressionUtils.java index 333207cece..ff8076b5e9 100644 --- a/modules/core/src/main/java/org/apache/synapse/util/synapse/expression/utils/ExpressionUtils.java +++ b/modules/core/src/main/java/org/apache/synapse/util/synapse/expression/utils/ExpressionUtils.java @@ -47,24 +47,27 @@ public static String getCharset(String charsetName) { /** * Extracts the variable name and JSONPath from the input expression. * - * @param input The input string (e.g., var.abc.students[1].name or var["abc"]["students"][1].name). + * @param input The input string (e.g., vars.abc.students[1].name or vars["abc"]["students"][1].name). * @return A string array where index 0 is the variable name and index 1 is the JSONPath. */ public static String[] extractVariableAndJsonPath(String input) { - if (input.startsWith("var.[\"")) { - String remaining = input.substring(6); + String VAR_DOT_ARR = "vars.[\""; + String VAR_ARR = "vars[\""; + String VAR_DOT = "vars."; + if (input.startsWith(VAR_DOT_ARR)) { + String remaining = input.substring(VAR_DOT_ARR.length()); int endBracketIndex = remaining.indexOf("\"]"); String variableName = remaining.substring(0, endBracketIndex); String expression = remaining.substring(endBracketIndex + 2); return new String[]{variableName, expression}; - } else if (input.startsWith("var[\"")) { - String remaining = input.substring(5); + } else if (input.startsWith(VAR_ARR)) { + String remaining = input.substring(VAR_ARR.length()); int endBracketIndex = remaining.indexOf("\"]"); String variableName = remaining.substring(0, endBracketIndex); String expression = remaining.substring(endBracketIndex + 2); return new String[]{variableName, expression}; - } else if (input.startsWith("var.")) { - String remaining = input.substring(4); + } else if (input.startsWith(VAR_DOT)) { + String remaining = input.substring(VAR_DOT.length()); int endDotIndex = remaining.indexOf("."); int beginArrIndex = remaining.indexOf("["); String variableName; diff --git a/modules/core/src/test/java/org/apache/synapse/mediators/transform/pfutils/RegexTemplateProcessorTest.java b/modules/core/src/test/java/org/apache/synapse/mediators/transform/pfutils/RegexTemplateProcessorTest.java index 21009fe981..2f130d150a 100644 --- a/modules/core/src/test/java/org/apache/synapse/mediators/transform/pfutils/RegexTemplateProcessorTest.java +++ b/modules/core/src/test/java/org/apache/synapse/mediators/transform/pfutils/RegexTemplateProcessorTest.java @@ -108,7 +108,7 @@ public static Collection provideConfigsForPrepareReplacementValueWithEscapeXmlCh { "{\n" + " \"description\": \"${payload.test1}\",\n" + - " \"path\": ${var.path}\n" + + " \"path\": ${vars.path}\n" + "}", "json", "application/json", diff --git a/modules/core/src/test/java/org/apache/synapse/util/InlineExpressionUtilTest.java b/modules/core/src/test/java/org/apache/synapse/util/InlineExpressionUtilTest.java index 86ccffce53..059a41d942 100644 --- a/modules/core/src/test/java/org/apache/synapse/util/InlineExpressionUtilTest.java +++ b/modules/core/src/test/java/org/apache/synapse/util/InlineExpressionUtilTest.java @@ -138,7 +138,7 @@ public void testsInLineSynapseExpressionTemplate4() throws Exception { jsonObject.addProperty("street", "Palm Grove"); jsonObject.addProperty("city", "Colombo"); mc.setVariable("address", jsonObject); - String inlineExpression = "Processing user with age : ${var.age} lives at ${var.address}"; + String inlineExpression = "Processing user with age : ${vars.age} lives at ${vars.address}"; boolean isContentAware = InlineExpressionUtil.isInlineSynapseExpressionsContentAware(inlineExpression); Assert.assertFalse("Inline expression content aware should be false", isContentAware); @@ -160,8 +160,8 @@ public void testsInLineSynapseExpressionTemplate5() throws Exception { jsonObject.addProperty("level", "3"); mc.setVariable("experience", jsonObject); mc.setProperty("duration", "years"); - String inlineExpression = "Processing user : ${payload.team[0].name}, role : ${var.role}, " + - "experience : ${var.experience.level} ${properties.synapse.duration}"; + String inlineExpression = "Processing user : ${payload.team[0].name}, role : ${vars.role}, " + + "experience : ${vars.experience.level} ${properties.synapse.duration}"; boolean isContentAware = InlineExpressionUtil.isInlineSynapseExpressionsContentAware(inlineExpression); Assert.assertTrue("Inline expression content aware should be true", isContentAware); @@ -180,7 +180,7 @@ public void testsInLineSynapseExpressionTemplate6() throws Exception { MessageContext mc = TestUtils.getTestContextJson(payload, null); mc.setVariable("endpoint", "https://test.wso2.com/"); mc.setProperty("method", "get"); - String inlineExpression = "Processing using endpoint : ${var.endpoint}, method : ${properties.synapse.method}, role : ${payload.team[2].role}"; + String inlineExpression = "Processing using endpoint : ${vars.endpoint}, method : ${properties.synapse.method}, role : ${payload.team[2].role}"; boolean isContentAware = InlineExpressionUtil.isInlineSynapseExpressionsContentAware(inlineExpression); Assert.assertTrue("Inline expression content aware should be true", isContentAware); @@ -205,7 +205,7 @@ public void testsInLineSynapseExpressionTemplate7() throws Exception { MessageContext mc = TestUtils.getTestContext(xmlPayload); mc.setVariable("endpoint", "https://test.wso2.com/"); - String inlineExpression = "Using endpoint : ${var.endpoint} to process book : ${xpath('//catalog/book[1]')}"; + String inlineExpression = "Using endpoint : ${vars.endpoint} to process book : ${xpath('//catalog/book[1]')}"; boolean isContentAware = InlineExpressionUtil.isInlineSynapseExpressionsContentAware(inlineExpression); Assert.assertTrue("Inline expression content aware should be true", isContentAware); diff --git a/modules/core/src/test/java/org/apache/synapse/util/synapse/expression/ConditionalExpressionTest.java b/modules/core/src/test/java/org/apache/synapse/util/synapse/expression/ConditionalExpressionTest.java index f82127ea2f..bb25d8ed12 100644 --- a/modules/core/src/test/java/org/apache/synapse/util/synapse/expression/ConditionalExpressionTest.java +++ b/modules/core/src/test/java/org/apache/synapse/util/synapse/expression/ConditionalExpressionTest.java @@ -37,7 +37,7 @@ public void testIfElse() { Assert.assertEquals("", TestUtils.evaluateExpression("45 == ( 5 + 34 ? true : 456)")); Assert.assertEquals("", TestUtils.evaluateExpressionWithPayload("45 == ( $[\"null\"] ? true : 456)", 1)); Assert.assertEquals("[22.99]", TestUtils.evaluateExpressionWithPayloadAndVariables( - "var.num1 > var.num2 ? $..book[?(@.author =~ /.*Tolkien/i)].price " + + "vars.num1 > vars.num2 ? $..book[?(@.author =~ /.*Tolkien/i)].price " + ": $..book[(@.\"length\"-1)].title", 2,1)); Assert.assertEquals("", TestUtils.evaluateExpression("null == $[\"null\"] ? 123 : 456")); Assert.assertEquals("", TestUtils.evaluateExpressionWithPayload( diff --git a/modules/core/src/test/java/org/apache/synapse/util/synapse/expression/ContentAwarenessTest.java b/modules/core/src/test/java/org/apache/synapse/util/synapse/expression/ContentAwarenessTest.java index 0138fb1722..b968f5695f 100644 --- a/modules/core/src/test/java/org/apache/synapse/util/synapse/expression/ContentAwarenessTest.java +++ b/modules/core/src/test/java/org/apache/synapse/util/synapse/expression/ContentAwarenessTest.java @@ -51,9 +51,9 @@ public void testComplexUsages() throws JaxenException { Assert.assertTrue(synapsePath.isContentAware()); synapsePath = new SynapseExpression("$..book[?(@.category==\"payload.category\")]"); Assert.assertTrue(synapsePath.isContentAware()); - synapsePath = new SynapseExpression("var.books[?(@.category==payload.category)]"); + synapsePath = new SynapseExpression("vars.books[?(@.category==payload.category)]"); Assert.assertTrue(synapsePath.isContentAware()); - synapsePath = new SynapseExpression("var.books[?(@.category==$.category)]"); + synapsePath = new SynapseExpression("vars.books[?(@.category==$.category)]"); Assert.assertTrue(synapsePath.isContentAware()); } @@ -65,27 +65,27 @@ public void testWithXpath() throws JaxenException { Assert.assertTrue(synapsePath.isContentAware()); synapsePath = new SynapseExpression("xpath(\"/student\")"); Assert.assertTrue(synapsePath.isContentAware()); - synapsePath = new SynapseExpression("xpath(\"//*\") + var.a$bc"); + synapsePath = new SynapseExpression("xpath(\"//*\") + vars.a$bc"); Assert.assertTrue(synapsePath.isContentAware()); synapsePath = new SynapseExpression("xpath(\"$ctx:bla\") + $.age"); Assert.assertTrue(synapsePath.isContentAware()); - synapsePath = new SynapseExpression("var.num1 + var.[\"payload\"] + xpath(\"//num3\")"); + synapsePath = new SynapseExpression("vars.num1 + vars.[\"payload\"] + xpath(\"//num3\")"); Assert.assertTrue(synapsePath.isContentAware()); - synapsePath = new SynapseExpression("var.num1 + var.[\"payload\"] + xpath(\"$ctx:num3\")"); + synapsePath = new SynapseExpression("vars.num1 + vars.[\"payload\"] + xpath(\"$ctx:num3\")"); Assert.assertFalse(synapsePath.isContentAware()); } @Test public void testNegativeCases() throws JaxenException { - SynapseExpression synapsePath = new SynapseExpression("length(var.abc)"); + SynapseExpression synapsePath = new SynapseExpression("length(vars.abc)"); Assert.assertFalse(synapsePath.isContentAware()); - synapsePath = new SynapseExpression("var[\"payload\"]"); + synapsePath = new SynapseExpression("vars[\"payload\"]"); Assert.assertFalse(synapsePath.isContentAware()); synapsePath = new SynapseExpression("5 + var[\"payload\"].age"); Assert.assertFalse(synapsePath.isContentAware()); - synapsePath = new SynapseExpression("var.a$.bc"); + synapsePath = new SynapseExpression("vars.a$.bc"); Assert.assertFalse(synapsePath.isContentAware()); - synapsePath = new SynapseExpression("var.books[?(@.category==\"payload.category\")]"); + synapsePath = new SynapseExpression("vars.books[?(@.category==\"payload.category\")]"); Assert.assertFalse(synapsePath.isContentAware()); } } diff --git a/modules/core/src/test/java/org/apache/synapse/util/synapse/expression/PayloadAndVariableAccessTest.java b/modules/core/src/test/java/org/apache/synapse/util/synapse/expression/PayloadAndVariableAccessTest.java index 7c26e05561..47d87a15c7 100644 --- a/modules/core/src/test/java/org/apache/synapse/util/synapse/expression/PayloadAndVariableAccessTest.java +++ b/modules/core/src/test/java/org/apache/synapse/util/synapse/expression/PayloadAndVariableAccessTest.java @@ -113,45 +113,60 @@ public void testJSONPath() { @Test public void testVariableAccess() { - Assert.assertEquals("John", TestUtils.evaluateExpressionWithPayloadAndVariables("var.name", 1, 1)); - Assert.assertEquals("10", TestUtils.evaluateExpressionWithPayloadAndVariables("var.num1", 1, 1)); + Assert.assertEquals("John", TestUtils.evaluateExpressionWithPayloadAndVariables("vars.name", 1, 1)); + Assert.assertEquals("10", TestUtils.evaluateExpressionWithPayloadAndVariables("vars.num1", 1, 1)); Assert.assertEquals("-29.0", TestUtils.evaluateExpressionWithPayloadAndVariables( - "(var.num1 * var.num3) - var.num2 + payload.index", 1, 1)); + "(vars.num1 * vars.num3) - vars.num2 + payload.index", 1, 1)); Assert.assertEquals("true", TestUtils.evaluateExpressionWithPayloadAndVariables( - "var.num1 >= var.num2", 1, 1)); + "vars.num1 >= vars.num2", 1, 1)); Assert.assertEquals("2", TestUtils.evaluateExpressionWithPayloadAndVariables( - "var.json3[1]", 0, 2)); + "vars.json3[1]", 0, 2)); Assert.assertEquals("2", TestUtils.evaluateExpressionWithPayloadAndVariables( - "var[\"json3\"][1]", 0, 2)); + "vars[\"json3\"][1]", 0, 2)); Assert.assertEquals("[\"The Lord of the Rings\"]", TestUtils.evaluateExpressionWithPayloadAndVariables( - "var[\"json2\"][\"store\"][\"book\"][?(@.author=='J.R.R. Tolkien')].title", 0, 2)); + "vars[\"json2\"][\"store\"][\"book\"][?(@.author=='J.R.R. Tolkien')].title", 0, 2)); Assert.assertEquals("[\"Moby Dick\",\"To Kill a Mockingbird\"]", TestUtils.evaluateExpressionWithPayloadAndVariables( - "var[\"json2\"][\"store\"][\"book\"][1,3].title", 0, 2)); + "vars[\"json2\"][\"store\"][\"book\"][1,3].title", 0, 2)); Assert.assertEquals("[\"Animal Farm\",\"The Diary of a Young Girl\"]", TestUtils.evaluateExpressionWithPayloadAndVariables( - "var[\"json2\"][\"store\"][\"book\"][-2:].title", 0, 2)); + "vars[\"json2\"][\"store\"][\"book\"][-2:].title", 0, 2)); Assert.assertEquals("[\"Moby Dick\",\"The Lord of the Rings\",\"To Kill a Mockingbird\",\"Animal Farm\"]", TestUtils.evaluateExpressionWithPayloadAndVariables( - "var.json2.store.book[?(@.category=='fiction')].title", 0, 2)); + "vars.json2.store.book[?(@.category=='fiction')].title", 0, 2)); Assert.assertEquals("[\"The Lord of the Rings\",\"To Kill a Mockingbird\"]", TestUtils.evaluateExpressionWithPayloadAndVariables( - "var[\"json2\"][\"store\"][\"book\"][?(@.price > payload.expensive)].title", 2, 2)); + "vars[\"json2\"][\"store\"][\"book\"][?(@.price > payload.expensive)].title", 2, 2)); Assert.assertEquals("[\"The Lord of the Rings\",\"To Kill a Mockingbird\"]", TestUtils.evaluateExpressionWithPayloadAndVariables( - "var[\"json2\"].store.[\"book\"][?(@.price > payload.expensive)].title", 2, 2)); + "vars[\"json2\"].store.[\"book\"][?(@.price > payload.expensive)].title", 2, 2)); Assert.assertEquals("[\"The Lord of the Rings\",\"To Kill a Mockingbird\"]", TestUtils.evaluateExpressionWithPayloadAndVariables( - "var.[\"json2\"].store.[\"book\"][?(@.price > payload.expensive)].title", 2, 2)); + "vars.[\"json2\"].store.[\"book\"][?(@.price > payload.expensive)].title", 2, 2)); Assert.assertEquals("", TestUtils.evaluateExpressionWithPayloadAndVariables( - "var.random", 0, 1)); + "vars.random", 0, 1)); Assert.assertEquals("", TestUtils.evaluateExpressionWithPayloadAndVariables( - "var.num1[0]", 0, 1)); + "vars.num1[0]", 0, 1)); Assert.assertEquals("201", TestUtils.evaluateExpressionWithPayloadAndVariables( - "var.fileRead_1.['attributes'].statusCode", 0, 3)); + "vars.fileRead_1.['attributes'].statusCode", 0, 3)); Assert.assertEquals("101", TestUtils.evaluateExpressionWithPayloadAndVariables( - "var[\"fileRead_1\"][\"headers\"]['Content-Length']", 0, 3)); - Assert.assertEquals("[\"Moby Dick\",\"To Kill a Mockingbird\"]", TestUtils.evaluateExpressionWithPayloadAndVariables( - "var.fileRead_1['payload'][\"store\"][\"book\"][1,3].title", 2, 3)); + "vars[\"fileRead_1\"][\"headers\"]['Content-Length']", 0, 3)); + Assert.assertEquals("[\"Moby Dick\",\"To Kill a Mockingbird\"]", + TestUtils.evaluateExpressionWithPayloadAndVariables( + "vars.fileRead_1['payload'][\"store\"][\"book\"][1,3].title", 2, 3)); + } + + @Test + public void testPayloadAccessWithKeywords() { + Assert.assertEquals("2", TestUtils.evaluateExpressionWithPayloadAndVariables( + "payload.payload.vars.pqr", 4, 0)); + Assert.assertEquals("8", TestUtils.evaluateExpressionWithPayloadAndVariables( + "payload.payload.vars.pqr * payload.payload.vars.configs.payload.iop", 4, 0)); + Assert.assertEquals("2", TestUtils.evaluateExpressionWithPayloadAndVariables( + "vars.payload.payload.vars.pqr", 0, 2)); + Assert.assertEquals("2", TestUtils.evaluateExpressionWithPayloadAndVariables( + "vars.vars.payload.vars.pqr", 0, 2)); + Assert.assertEquals("6", TestUtils.evaluateExpressionWithPayloadAndVariables( + "vars.vars.payload.vars.pqr + vars.payload.payload.vars.configs.payload.iop ", 0, 2)); } } diff --git a/modules/core/src/test/java/org/apache/synapse/util/synapse/expression/PreDefinedFunctionsTest.java b/modules/core/src/test/java/org/apache/synapse/util/synapse/expression/PreDefinedFunctionsTest.java index 357843679c..149644e5e2 100644 --- a/modules/core/src/test/java/org/apache/synapse/util/synapse/expression/PreDefinedFunctionsTest.java +++ b/modules/core/src/test/java/org/apache/synapse/util/synapse/expression/PreDefinedFunctionsTest.java @@ -35,9 +35,9 @@ public void testLength() { Assert.assertEquals("6", TestUtils.evaluateExpressionWithPayloadAndVariables( "length(payload.store.book)", 2, 0)); Assert.assertEquals("6", TestUtils.evaluateExpressionWithPayloadAndVariables( - "length(var.cars)", 2, 1)); + "length(vars.cars)", 2, 1)); Assert.assertEquals("0", TestUtils.evaluateExpressionWithPayloadAndVariables( - "length(var[\"empty\"])", 2, 1)); + "length(vars[\"empty\"])", 2, 1)); Assert.assertEquals("", TestUtils.evaluateExpression("length(34)")); Assert.assertEquals("", TestUtils.evaluateExpression("length(null)")); } @@ -52,7 +52,7 @@ public void testToUpper() { TestUtils.evaluateExpressionWithPayload("toUpper(payload.store.book[4].author)", 2)); Assert.assertEquals("", TestUtils.evaluateExpression("toUpper(null)")); Assert.assertEquals("", TestUtils.evaluateExpression("toUpper(34)")); - Assert.assertEquals("", TestUtils.evaluateExpressionWithPayloadAndVariables("toUpper(var[\"empty\"])", 0, 1)); + Assert.assertEquals("", TestUtils.evaluateExpressionWithPayloadAndVariables("toUpper(vars[\"empty\"])", 0, 1)); } @Test @@ -65,7 +65,7 @@ public void testToLower() { TestUtils.evaluateExpressionWithPayload("toLower(payload.store.book[4].author)", 2)); Assert.assertEquals("", TestUtils.evaluateExpression("toLower(34)")); Assert.assertEquals("", TestUtils.evaluateExpression("toLower(null)")); - Assert.assertEquals("", TestUtils.evaluateExpressionWithPayloadAndVariables("toLower(var[\"empty\"])", + Assert.assertEquals("", TestUtils.evaluateExpressionWithPayloadAndVariables("toLower(vars[\"empty\"])", 0, 1)); } @@ -82,7 +82,7 @@ public void testSubString() { Assert.assertEquals("", TestUtils.evaluateExpression("subString(\"Hello\",20)")); Assert.assertEquals("", TestUtils.evaluateExpression("subString(\"Hello\",-2)")); Assert.assertEquals("", TestUtils.evaluateExpression("subString(\"Hello\",-2,4)")); - Assert.assertEquals("", TestUtils.evaluateExpression("subString(\"Hello\",2,var.num1)")); + Assert.assertEquals("", TestUtils.evaluateExpression("subString(\"Hello\",2,vars.num1)")); } @Test @@ -164,11 +164,11 @@ public void testAbs() { Assert.assertEquals("5", TestUtils.evaluateExpression("abs(-5)")); Assert.assertEquals("5", TestUtils.evaluateExpression("abs(5)")); Assert.assertEquals("5.0", TestUtils.evaluateExpression("abs(-5.0)")); - Assert.assertEquals("2.0", TestUtils.evaluateExpressionWithPayloadAndVariables("abs(var.num4)", 0, 1)); + Assert.assertEquals("2.0", TestUtils.evaluateExpressionWithPayloadAndVariables("abs(vars.num4)", 0, 1)); Assert.assertEquals("8.0", TestUtils.evaluateExpressionWithPayloadAndVariables( - "abs(var.num4 + payload.expensive )", 2, 1)); + "abs(vars.num4 + payload.expensive )", 2, 1)); Assert.assertEquals("4.0", TestUtils.evaluateExpressionWithPayloadAndVariables( - "abs(var.num1 / var.num3)", 2, 1)); + "abs(vars.num1 / vars.num3)", 2, 1)); Assert.assertEquals("", TestUtils.evaluateExpressionWithPayload("abs(payload.cars)", 1)); } @@ -176,8 +176,8 @@ public void testAbs() { public void testFloor() { Assert.assertEquals("-6.0", TestUtils.evaluateExpression("floor(-5.4)")); Assert.assertEquals("5.0", TestUtils.evaluateExpression("floor(5.9)")); - Assert.assertEquals("-3.0", TestUtils.evaluateExpressionWithPayloadAndVariables("floor(var.num3)", 0, 1)); - Assert.assertEquals("2.0", TestUtils.evaluateExpressionWithPayloadAndVariables("floor(-1 * var.num3)", 0, 1)); + Assert.assertEquals("-3.0", TestUtils.evaluateExpressionWithPayloadAndVariables("floor(vars.num3)", 0, 1)); + Assert.assertEquals("2.0", TestUtils.evaluateExpressionWithPayloadAndVariables("floor(-1 * vars.num3)", 0, 1)); Assert.assertEquals("", TestUtils.evaluateExpressionWithPayload("floor(payload.cars)", 1)); } @@ -185,8 +185,8 @@ public void testFloor() { public void testCeil() { Assert.assertEquals("-5.0", TestUtils.evaluateExpression("ceil(-5.4)")); Assert.assertEquals("6.0", TestUtils.evaluateExpression("ceil(5.9)")); - Assert.assertEquals("-2.0", TestUtils.evaluateExpressionWithPayloadAndVariables("ceil(var.num3)", 0, 1)); - Assert.assertEquals("3.0", TestUtils.evaluateExpressionWithPayloadAndVariables("ceil(-1 * var.num3)", 0, 1)); + Assert.assertEquals("-2.0", TestUtils.evaluateExpressionWithPayloadAndVariables("ceil(vars.num3)", 0, 1)); + Assert.assertEquals("3.0", TestUtils.evaluateExpressionWithPayloadAndVariables("ceil(-1 * vars.num3)", 0, 1)); Assert.assertEquals("", TestUtils.evaluateExpressionWithPayload("ceil(payload.cars)", 1)); } @@ -194,8 +194,8 @@ public void testCeil() { public void testSqrt() { Assert.assertEquals("5.0", TestUtils.evaluateExpression("sqrt(25)")); Assert.assertEquals("NaN", TestUtils.evaluateExpression("sqrt(-25.0)")); - Assert.assertEquals("10.0", TestUtils.evaluateExpressionWithPayloadAndVariables("sqrt(var.num1 * var.num1)", 0, 1)); - Assert.assertEquals("2.5", TestUtils.evaluateExpressionWithPayloadAndVariables("sqrt(var.num3 * var.num3)", 0, 1)); + Assert.assertEquals("10.0", TestUtils.evaluateExpressionWithPayloadAndVariables("sqrt(vars.num1 * vars.num1)", 0, 1)); + Assert.assertEquals("2.5", TestUtils.evaluateExpressionWithPayloadAndVariables("sqrt(vars.num3 * vars.num3)", 0, 1)); Assert.assertEquals("", TestUtils.evaluateExpressionWithPayload("sqrt(payload.cars)", 1)); } @@ -204,9 +204,9 @@ public void testPow() { Assert.assertEquals("8.0", TestUtils.evaluateExpression("pow(2,3)")); Assert.assertEquals("1.0", TestUtils.evaluateExpression("pow(2,0)")); Assert.assertEquals("0.25", TestUtils.evaluateExpression("pow(2,-2)")); - Assert.assertEquals("1.0", TestUtils.evaluateExpressionWithPayloadAndVariables("pow(var.num1,0)", 0, 1)); - Assert.assertEquals("1.0", TestUtils.evaluateExpressionWithPayloadAndVariables("pow(var.num3,0)", 0, 1)); - Assert.assertEquals("0.16", TestUtils.evaluateExpressionWithPayloadAndVariables("pow(var.num3,-2)", 0, 1)); + Assert.assertEquals("1.0", TestUtils.evaluateExpressionWithPayloadAndVariables("pow(vars.num1,0)", 0, 1)); + Assert.assertEquals("1.0", TestUtils.evaluateExpressionWithPayloadAndVariables("pow(vars.num3,0)", 0, 1)); + Assert.assertEquals("0.16", TestUtils.evaluateExpressionWithPayloadAndVariables("pow(vars.num3,-2)", 0, 1)); Assert.assertEquals("", TestUtils.evaluateExpressionWithPayload("pow(payload.cars,2)", 1)); } @@ -216,7 +216,7 @@ public void testB64Encode() { Assert.assertEquals("Sm9obg==", TestUtils.evaluateExpressionWithPayloadAndVariables( "base64encode(payload[\"name\"])", 1, 0)); Assert.assertEquals("Sm9obg==", TestUtils.evaluateExpressionWithPayloadAndVariables( - "base64encode(var.name,\"UTF-8\")", 0, 1)); + "base64encode(vars.name,\"UTF-8\")", 0, 1)); Assert.assertEquals("", TestUtils.evaluateExpression("base64encode(34)")); Assert.assertEquals("", TestUtils.evaluateExpression("base64encode(\"Hello\",\"UTF-99\")")); } @@ -225,7 +225,7 @@ public void testB64Encode() { public void testB64Decode() { Assert.assertEquals("admin:admin", TestUtils.evaluateExpression("base64decode(\"YWRtaW46YWRtaW4=\")")); Assert.assertEquals("WSO2MI", TestUtils.evaluateExpressionWithPayloadAndVariables( - "base64decode(var.encoded)", 0, 1)); + "base64decode(vars.encoded)", 0, 1)); Assert.assertEquals("", TestUtils.evaluateExpression("base64decode(34)")); } @@ -251,13 +251,13 @@ public void testIsNumber() { Assert.assertEquals("true", TestUtils.evaluateExpression("isNumber(34)")); Assert.assertEquals("false", TestUtils.evaluateExpression("isNumber(\"34\")")); Assert.assertEquals("true", TestUtils.evaluateExpressionWithPayloadAndVariables( - "isNumber(payload[\"age\"]) && isNumber(var.num1)", 1, 1)); + "isNumber(payload[\"age\"]) && isNumber(vars.num1)", 1, 1)); Assert.assertEquals("false", TestUtils.evaluateExpressionWithPayloadAndVariables( - "isNumber(payload[\"string\"]) || isNumber(var.name)", 1, 1)); + "isNumber(payload[\"string\"]) || isNumber(vars.name)", 1, 1)); Assert.assertEquals("false", TestUtils.evaluateExpressionWithPayloadAndVariables( - "isNumber(var[\"empty\"])", 1, 1)); + "isNumber(vars[\"empty\"])", 1, 1)); Assert.assertEquals("", TestUtils.evaluateExpressionWithPayloadAndVariables( - "isNumber(var[\"empty2\"])", 1, 1)); + "isNumber(vars[\"empty2\"])", 1, 1)); Assert.assertEquals("false", TestUtils.evaluateExpression("isNumber(\"Hello\")")); Assert.assertEquals("", TestUtils.evaluateExpression("isNumber(null)")); } @@ -267,9 +267,9 @@ public void testIsString() { Assert.assertEquals("true", TestUtils.evaluateExpression("isString(\"Hello\")")); Assert.assertEquals("false", TestUtils.evaluateExpression("isString(34)")); Assert.assertEquals("true", TestUtils.evaluateExpressionWithPayloadAndVariables( - "isString(payload[\"name\"]) && isString(var.name)", 1, 1)); + "isString(payload[\"name\"]) && isString(vars.name)", 1, 1)); Assert.assertEquals("false", TestUtils.evaluateExpressionWithPayloadAndVariables( - "isString(payload[\"age\"]) || isString(var.num1)", 1, 1)); + "isString(payload[\"age\"]) || isString(vars.num1)", 1, 1)); Assert.assertEquals("true", TestUtils.evaluateExpression("isString(\"34\")")); Assert.assertEquals("", TestUtils.evaluateExpression("isString(null)")); } @@ -279,9 +279,9 @@ public void testIsArray() { Assert.assertEquals("true", TestUtils.evaluateExpression("isArray([\"Hello\",34])")); Assert.assertEquals("false", TestUtils.evaluateExpression("isArray(\"Hello\")")); Assert.assertEquals("true", TestUtils.evaluateExpressionWithPayloadAndVariables( - "isArray(payload[\"cars\"]) && isArray(var.cars)", 1, 1)); + "isArray(payload[\"cars\"]) && isArray(vars.cars)", 1, 1)); Assert.assertEquals("false", TestUtils.evaluateExpressionWithPayloadAndVariables( - "isArray(payload[\"age\"]) || isArray(var.num1)", 1, 1)); + "isArray(payload[\"age\"]) || isArray(vars.num1)", 1, 1)); Assert.assertEquals("true", TestUtils.evaluateExpression("isArray([\"34\"])")); Assert.assertEquals("true", TestUtils.evaluateExpressionWithPayloadAndVariables( "isArray(payload)", 3, 1)); @@ -294,9 +294,9 @@ public void testIsArray() { public void testIsObject() { Assert.assertEquals("false", TestUtils.evaluateExpression("isObject(\"Hello\")")); Assert.assertEquals("false", TestUtils.evaluateExpressionWithPayloadAndVariables( - "isObject(payload) && isObject(var.name)", 3, 1)); + "isObject(payload) && isObject(vars.name)", 3, 1)); Assert.assertEquals("false", TestUtils.evaluateExpressionWithPayloadAndVariables( - "isObject(payload[\"age\"]) || isObject(var.num1)", 1, 1)); + "isObject(payload[\"age\"]) || isObject(vars.num1)", 1, 1)); Assert.assertEquals("true", TestUtils.evaluateExpressionWithPayloadAndVariables( "isObject(payload.store)", 2, 1)); Assert.assertEquals("", TestUtils.evaluateExpression("isObject(null)")); @@ -312,7 +312,7 @@ public void testConvertToString() { Assert.assertEquals("false", TestUtils.evaluateExpression("string(false)")); Assert.assertEquals("[\"When\",\"my\",\"time\",\"comes\",\"Forget\",\"the\",\"wrong\",\"that\"," + "\"I've\",\"done\"]", TestUtils.evaluateExpressionWithPayload("string(payload)", 3)); - Assert.assertEquals("300", TestUtils.evaluateExpressionWithPayloadAndVariables("string(var.age * 10)", 0, 1)); + Assert.assertEquals("300", TestUtils.evaluateExpressionWithPayloadAndVariables("string(vars.age * 10)", 0, 1)); Assert.assertEquals("", TestUtils.evaluateExpression("string(null)")); } @@ -320,7 +320,7 @@ public void testConvertToString() { public void testConvertToInteger() { Assert.assertEquals("34", TestUtils.evaluateExpression("integer(34)")); Assert.assertEquals("20", TestUtils.evaluateExpressionWithPayloadAndVariables( - "integer(payload[\"expensive\"]) + var.num1", 2, 1)); + "integer(payload[\"expensive\"]) + vars.num1", 2, 1)); Assert.assertEquals("", TestUtils.evaluateExpression("integer(\"Hello\")")); Assert.assertEquals("", TestUtils.evaluateExpression("integer(34.5)")); Assert.assertEquals("", TestUtils.evaluateExpression("integer(null)")); @@ -331,7 +331,7 @@ public void testConvertToInteger() { public void testConvertToFloat() { Assert.assertEquals("-34.0", TestUtils.evaluateExpression("float(-34)")); Assert.assertEquals("15.0", TestUtils.evaluateExpressionWithPayloadAndVariables( - "float(payload[\"expensive\"]) + var.num2", 2, 1)); + "float(payload[\"expensive\"]) + vars.num2", 2, 1)); Assert.assertEquals("", TestUtils.evaluateExpression("float(\"Hello\")")); Assert.assertEquals("", TestUtils.evaluateExpressionWithPayload("float(payload)", 3)); } @@ -351,16 +351,16 @@ public void testRound() { Assert.assertEquals("5", TestUtils.evaluateExpression("round(5.4)")); Assert.assertEquals("6", TestUtils.evaluateExpression("round(5.6)")); Assert.assertEquals("-2", TestUtils.evaluateExpressionWithPayloadAndVariables( - "round(var.num4)", 0, 1)); + "round(vars.num4)", 0, 1)); Assert.assertEquals("15", TestUtils.evaluateExpressionWithPayloadAndVariables( - "round(var.num1 + var.num2)", 0, 1)); + "round(vars.num1 + vars.num2)", 0, 1)); Assert. assertEquals("", TestUtils.evaluateExpressionWithPayload("round(payload)", 3)); } @Test public void testExists() { TestUtils.clearMessageContext(); - Assert.assertEquals("false", TestUtils.evaluateExpression("exists(var.num1)")); + Assert.assertEquals("false", TestUtils.evaluateExpression("exists(vars.num1)")); Assert.assertEquals("false", TestUtils.evaluateExpression("exists(null)")); Assert.assertEquals("John", TestUtils.evaluateExpressionWithPayload( "exists($.fullName) ? $.fullName : $.name", 1)); diff --git a/modules/core/src/test/java/org/apache/synapse/util/synapse/expression/PrimitiveExpressionsTest.java b/modules/core/src/test/java/org/apache/synapse/util/synapse/expression/PrimitiveExpressionsTest.java index e8193c4a27..1717d844eb 100644 --- a/modules/core/src/test/java/org/apache/synapse/util/synapse/expression/PrimitiveExpressionsTest.java +++ b/modules/core/src/test/java/org/apache/synapse/util/synapse/expression/PrimitiveExpressionsTest.java @@ -62,7 +62,7 @@ public void testGT() { Assert.assertEquals("false", TestUtils.evaluateExpression("-5 > -3.4")); Assert.assertEquals("", TestUtils.evaluateExpression("5 > \"bla\"")); Assert.assertEquals("", TestUtils.evaluateExpression("5 > null")); - Assert.assertEquals("true", TestUtils.evaluateExpressionWithPayloadAndVariables("$.age > var.num1",1,1)); + Assert.assertEquals("true", TestUtils.evaluateExpressionWithPayloadAndVariables("$.age > vars.num1",1,1)); Assert.assertEquals("", TestUtils.evaluateExpressionWithPayloadAndVariables("$.age > $[\"null\"]", 1, 1)); } @@ -119,12 +119,12 @@ public void testAdd() { Assert.assertEquals("", TestUtils.evaluateExpression("\"abc\" + 5")); Assert.assertEquals("abcxyz", TestUtils.evaluateExpression("\"abc\" + \"xyz\"")); Assert.assertEquals("7.5", TestUtils.evaluateExpressionWithPayloadAndVariables( - "var.num1 + var.num3", 2, 1)); + "vars.num1 + vars.num3", 2, 1)); Assert.assertEquals("20", TestUtils.evaluateExpressionWithPayloadAndVariables( - "var.num1 + payload.expensive", 2, 1)); - Assert.assertEquals("", TestUtils.evaluateExpressionWithPayloadAndVariables("5 + var.name", 2, 1)); + "vars.num1 + payload.expensive", 2, 1)); + Assert.assertEquals("", TestUtils.evaluateExpressionWithPayloadAndVariables("5 + vars.name", 2, 1)); // clear the synCtx to remove previous payload and variables. - Assert.assertEquals("", TestUtils.evaluateExpression("var.num99 + 5")); + Assert.assertEquals("", TestUtils.evaluateExpression("vars.num99 + 5")); } @Test @@ -132,10 +132,10 @@ public void testSubtract() { Assert.assertEquals("-33", TestUtils.evaluateExpression("5 - 30 + 2 - 10")); Assert.assertEquals("2.5", TestUtils.evaluateExpression("5.5 - 3")); Assert.assertEquals("2.0", TestUtils.evaluateExpression("5.5 - 3.5")); - Assert.assertEquals("", TestUtils.evaluateExpression("var.num99 - 5")); + Assert.assertEquals("", TestUtils.evaluateExpression("vars.num99 - 5")); Assert.assertEquals("", TestUtils.evaluateExpression("5 - \"bla\"")); Assert.assertEquals("12.5", TestUtils.evaluateExpressionWithPayloadAndVariables( - "var.num1 - var.num3", 2, 1)); + "vars.num1 - vars.num3", 2, 1)); } @Test @@ -143,10 +143,10 @@ public void testMultiply() { Assert.assertEquals("-30", TestUtils.evaluateExpression("5 * 3 * -2")); Assert.assertEquals("16.5", TestUtils.evaluateExpression("5.5 * 3")); Assert.assertEquals("19.25", TestUtils.evaluateExpression("5.5 * 3.5")); - Assert.assertEquals("", TestUtils.evaluateExpression("var.num99 * 5")); + Assert.assertEquals("", TestUtils.evaluateExpression("vars.num99 * 5")); Assert.assertEquals("", TestUtils.evaluateExpression("5 * \"bla\"")); Assert.assertEquals("-25.0", TestUtils.evaluateExpressionWithPayloadAndVariables( - "var.num1 * var.num3", 2, 1)); + "vars.num1 * vars.num3", 2, 1)); } @Test @@ -154,10 +154,10 @@ public void testDivide() { Assert.assertEquals("-4.0", TestUtils.evaluateExpression("10 / 2 / -2.5 * 2")); Assert.assertEquals("-4.0", TestUtils.evaluateExpression("10 / 2 / -2.5 * 2")); Assert.assertEquals("3", TestUtils.evaluateExpression("9 / 3")); - Assert.assertEquals("", TestUtils.evaluateExpression("var.num99 / 5")); + Assert.assertEquals("", TestUtils.evaluateExpression("vars.num99 / 5")); Assert.assertEquals("", TestUtils.evaluateExpression("5 / \"bla\"")); Assert.assertEquals("5", TestUtils.evaluateExpressionWithPayloadAndVariables( - "var.num1 / 2", 2, 1)); + "vars.num1 / 2", 2, 1)); } @Test @@ -165,9 +165,9 @@ public void testMod() { Assert.assertEquals("1", TestUtils.evaluateExpression("10 % 3")); Assert.assertEquals("2.5", TestUtils.evaluateExpression("5.5 % 3")); Assert.assertEquals("2.0", TestUtils.evaluateExpression("5.5 % 3.5")); - Assert.assertEquals("", TestUtils.evaluateExpression("var.num99 % 5")); + Assert.assertEquals("", TestUtils.evaluateExpression("vars.num99 % 5")); Assert.assertEquals("", TestUtils.evaluateExpression("5 % \"bla\"")); Assert.assertEquals("0", TestUtils.evaluateExpressionWithPayloadAndVariables( - "var.num1 % 2", 2, 1)); + "vars.num1 % 2", 2, 1)); } } diff --git a/modules/core/src/test/java/org/apache/synapse/util/synapse/expression/TestUtils.java b/modules/core/src/test/java/org/apache/synapse/util/synapse/expression/TestUtils.java index 332091e6b7..20ba6d6a30 100644 --- a/modules/core/src/test/java/org/apache/synapse/util/synapse/expression/TestUtils.java +++ b/modules/core/src/test/java/org/apache/synapse/util/synapse/expression/TestUtils.java @@ -98,12 +98,28 @@ public class TestUtils { " \"selectedCategory\": \"biography\"\n" + "}\n"; + private static final String PAYLOAD3 = "[\"When\",\"my\",\"time\",\"comes\",\"Forget\",\"the\",\"wrong\",\"that\"" + + ",\"I've\",\"done\"]"; + + private static final String PAYLOAD4 = "{\n" + + " \"payload\" : {\n" + + " \"vars\" : {\n" + + " \"configs\" : {\n" + + " \"payload\" : {\n" + + " \"iop\" : 4\n" + + " },\n" + + " \"zxc\": 3\n" + + " },\n" + + " \"pqr\": 2\n" + + " },\n" + + " \"abc\" : 1\n" + + " }\n" + + "}"; + private static final JsonObject responseHeaders; private static final JsonObject responseAttributes; - private static final String PAYLOAD3 = "[\"When\",\"my\",\"time\",\"comes\",\"Forget\",\"the\",\"wrong\",\"that\"" + - ",\"I've\",\"done\"]"; private static final Map variableMap1; private static final Map variableMap2; @@ -126,6 +142,8 @@ public class TestUtils { variableMap2.put("json1", PAYLOAD1); variableMap2.put("json2", PAYLOAD2); variableMap2.put("json3", "[1,2,3,\"abc\"]"); + variableMap2.put("payload", PAYLOAD4); + variableMap2.put("vars", PAYLOAD4); variableMap3 = new HashMap<>(); responseHeaders = new JsonObject(); responseHeaders.addProperty("Content-Type", "application/json"); @@ -155,12 +173,21 @@ public static String evaluateExpressionWithPayload(String expression, int payloa public static String evaluateExpressionWithPayloadAndVariables(String expression, int payloadId, int variableMapId) { try { - if (payloadId == 1) { - JsonUtil.getNewJsonPayload(synCtx.getAxis2MessageContext(), PAYLOAD1, true, true); - } else if (payloadId == 2) { - JsonUtil.getNewJsonPayload(synCtx.getAxis2MessageContext(), PAYLOAD2, true, true); - } else if (payloadId == 3) { - JsonUtil.getNewJsonPayload(synCtx.getAxis2MessageContext(), PAYLOAD3, true, true); + switch (payloadId) { + case 1: + JsonUtil.getNewJsonPayload(synCtx.getAxis2MessageContext(), PAYLOAD1, true, true); + break; + case 2: + JsonUtil.getNewJsonPayload(synCtx.getAxis2MessageContext(), PAYLOAD2, true, true); + break; + case 3: + JsonUtil.getNewJsonPayload(synCtx.getAxis2MessageContext(), PAYLOAD3, true, true); + break; + case 4: + JsonUtil.getNewJsonPayload(synCtx.getAxis2MessageContext(), PAYLOAD4, true, true); + break; + default: + break; } if (variableMapId == 1) { for (Map.Entry entry : variableMap1.entrySet()) {