2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.sli.core.sli.provider.base;
24 import java.io.BufferedReader;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.util.LinkedList;
29 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicExprListener;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicExpression;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicExpressionFactory;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicGraph;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicNode;
35 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicExpressionResolver;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicParser;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 import junit.framework.Assert;
41 import junit.framework.TestCase;
43 public class SvcLogicExpressionResolverTest extends TestCase {
46 private static final Logger LOG = LoggerFactory
47 .getLogger(SvcLogicExpressionResolver.class);
49 public void testEvaluate()
51 InputStream testStr = getClass().getResourceAsStream("/expression.tests");
52 BufferedReader testsReader = new BufferedReader(new InputStreamReader(testStr));
56 SvcLogicContext ctx = new SvcLogicContext();
57 SvcLogicGraph graph = new SvcLogicGraph();
58 SvcLogicNode node = new SvcLogicNode(1, "return", graph);
59 graph.setRootNode(node);
63 while ((line = testsReader.readLine()) != null) {
65 if (line.startsWith("#"))
67 String testExpr = line.trim().substring(1).trim();
68 String[] nameValue = testExpr.split("=");
69 String name = nameValue[0].trim();
70 String value = nameValue[1].trim();
72 if (name.startsWith("$"))
74 LOG.info("Setting context attribute "+name+" = "+value);
75 ctx.setAttribute(name.substring(1), value);
80 LOG.info("Setting node attribute "+name+" = "+value);
81 node.setAttribute(name, value);
87 // if the line contains #, what comes before is the expression to evaluate, and what comes after
88 // is the expected value
89 String[] substrings = line.split("#");
90 String expectedValue = substrings.length > 1 ? substrings[1].trim() : null;
91 String testExpr = substrings[0].trim();
93 LOG.info("Parsing expression "+testExpr);
94 SvcLogicExpression expr = SvcLogicExpressionFactory.parse(testExpr);
97 fail("Unable to parse expression "+testExpr);
101 LOG.info("Evaluating parsed expression "+expr.asParsedExpr());
102 String exprValue = SvcLogicExpressionResolver.evaluate(expr, node, ctx);
103 if (exprValue == null)
105 fail("Unable to evaluate expression "+testExpr);
109 LOG.info("Expression " + testExpr + " evaluates to " + exprValue);
110 if (expectedValue != null) {
111 Assert.assertEquals("Line " + lineNo + ": " + testExpr, expectedValue, exprValue);
120 LOG.error("Caught exception", e);
121 fail("Caught exception");
125 public void testSvcLogicExpressions() throws Exception {
126 SwitchNodeExecutor switchNodeExecutor = new SwitchNodeExecutor();
127 SetNodeExecutor setNodeExecutor = new SetNodeExecutor();
128 SvcLogicContext ctx = new SvcLogicContext();
129 SvcLogicParser slp = new SvcLogicParser();
130 LinkedList<SvcLogicGraph> graph = slp.parse("src/test/resources/expressions.xml");
131 SvcLogicNode root = graph.getFirst().getRootNode();
132 //Test a set node that makes use of arithmetic operations
135 <parameter name='add' value='`1 + 1`' />
136 <parameter name='sub' value='`2 - 1`' />
137 <parameter name='div' value='`6 / 2`' />
138 <parameter name='multi' value='`2 * 2`' />
139 <parameter name='addDoubleQuotes' value="`1 + 1`" />
140 <parameter name='subDoubleQuotes' value="`2 - 1`" />
141 <parameter name='divDoubleQuotes' value="`6 / 2`" />
142 <parameter name='multiDoubleQuotes' value="`2 * 2`" />
145 //the node matching outcome value 1 comes from parsing the block of xml above
146 ctx.setAttribute("a", "5");
147 ctx.setAttribute("b", "3");
148 setNodeExecutor.execute(root.getOutcomeValue("1"), ctx);
149 assertEquals("2", ctx.getAttribute("add"));
150 assertEquals("1", ctx.getAttribute("sub"));
151 assertEquals("3", ctx.getAttribute("div"));
152 assertEquals("4", ctx.getAttribute("multi"));
153 assertEquals("2", ctx.getAttribute("addDoubleQuotes"));
154 assertEquals("1", ctx.getAttribute("subDoubleQuotes"));
155 assertEquals("3", ctx.getAttribute("divDoubleQuotes"));
156 assertEquals("4", ctx.getAttribute("multiDoubleQuotes"));
158 //Test a set node that makes use of string concatenation
161 <parameter name='varA' value='`$a + $b`' />
162 <parameter name='varB' value='`$a + 'literal' `' />
163 <parameter name='varC' value='`'literal' + $b `' />
164 <parameter name='varD' value='`'too' + 'literal'`' />
165 <parameter name='varADoubleQuotes' value="`$a + $b`" />
166 <parameter name='varBDoubleQuotes' value="`$a +'literal' `" />
167 <parameter name='varCDoubleQuotes' value="`'literal' + $b `" />
168 <parameter name='varDDoubleQuotes' value="`'too' + 'literal'`" />
171 //the node matching outcome value 2 comes from parsing the block of xml above
172 ctx.setAttribute("a", "cat");
173 ctx.setAttribute("b", "dog");
174 setNodeExecutor.execute(root.getOutcomeValue("2"), ctx);
175 assertEquals("catdog", ctx.getAttribute("varA"));
176 assertEquals("catliteral", ctx.getAttribute("varB"));
177 assertEquals("literaldog", ctx.getAttribute("varC"));
178 assertEquals("tooliteral", ctx.getAttribute("varD"));
179 assertEquals("catdog", ctx.getAttribute("varADoubleQuotes"));
180 assertEquals("catliteral", ctx.getAttribute("varBDoubleQuotes"));
181 assertEquals("literaldog", ctx.getAttribute("varCDoubleQuotes"));
182 assertEquals("tooliteral", ctx.getAttribute("varDDoubleQuotes"));
184 //Shows how backticks interact with + operator
187 <parameter name='testOne' value='`1 + 1`' />
188 <parameter name='testThree' value='"1" +"1"' />
189 <parameter name='testFour' value='`$portNumber + $slot + $shelf`' />
190 <parameter name='testOneDoubleQuotes' value="`1 + 1`" />
191 <parameter name='testThreeDoubleQuotes' value="'1' +'1'" />
192 <parameter name='testFourDoubleQuotes' value="`$portNumber + $slot + $shelf`" />
195 //the node matching outcome value 3 comes from parsing the block of xml above
196 ctx.setAttribute("portNumber", "2");
197 ctx.setAttribute("slot", "3");
198 ctx.setAttribute("shelf", "1");
200 setNodeExecutor.execute(root.getOutcomeValue("3"), ctx);
201 assertEquals("2", ctx.getAttribute("testOne"));
202 assertEquals("\"1\" +\"1\"", ctx.getAttribute("testThree"));
203 assertEquals("6", ctx.getAttribute("testFour"));
204 assertEquals("2", ctx.getAttribute("testOneDoubleQuotes"));
205 assertEquals("'1' +'1'", ctx.getAttribute("testThreeDoubleQuotes"));
206 assertEquals("6", ctx.getAttribute("testFourDoubleQuotes"));
208 ctx.setAttribute("a", "5");
209 ctx.setAttribute("b", "3");
211 // series of switch statements showing and or != > < >= == <=
212 // the XML for the node is commented above the line that evaluates that node, the switch statements are single line
214 //<switch test="`'PIZZA' == 'NOTPIZZA' or $a != $b`" />
215 assertEquals("true",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("4"), ctx));
217 //<switch test="`'PIZZA' == 'PIZZA' and $a != $b`" />
218 assertEquals("true",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("5"), ctx));
220 //<switch test="`'PIZZA' == 'NOTPIZZA' or $a >= $b`" />
221 assertEquals("true",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("6"), ctx));
223 //<switch test="`'PIZZA' == 'PIZZA' and $b < $a`" />
224 assertEquals("true",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("7"), ctx));
226 //<switch test="`'PIZZA' == 'PIZZA'`" />
227 assertEquals("true",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("8"), ctx));
229 //<switch test="`$a == $b`" />
230 assertEquals("false",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("9"), ctx));
232 //<switch test="`'PIZZA' == 'NOTPIZZA'`" />
233 assertEquals("false",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("10"), ctx));
235 //<switch test="`'PIZZA' != 'PIZZA'`" />
236 assertEquals("false",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("11"), ctx));
238 //<switch test="`'PIZZA' != 'NOTPIZZA'`" />
239 assertEquals("true",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("12"), ctx));
241 //<switch test='`$a != $b`' />
242 assertEquals("true",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("13"), ctx));
244 //<switch test='`1 < 2`' />
245 assertEquals("true",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("14"), ctx));
247 //<switch test='`2 <= 2`' />
248 assertEquals("true",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("15"), ctx));
250 //<switch test='`3 > 2`' />
251 assertEquals("true",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("16"), ctx));
253 //<switch test='`2 >= 2`' />
254 assertEquals("true",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("17"), ctx));
256 // Series of switch statements that show the effect of using backticks
258 ctx.setAttribute("literalStartingWithDollarSign", "DONT READ ME!");
259 //<switch test='$literalStartingWithDollarSign'/>
260 assertEquals("$literalStartingWithDollarSign",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("18"), ctx));
262 ctx.setAttribute("dollarSignFollowedByVariableSurroundedinBackticks", "README");
263 //<switch test='$literalStartingWithDollarSign'/>
264 assertEquals("README",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("19"), ctx));
266 ctx.setAttribute("a", "2");
267 ctx.setAttribute("b", "2");
268 //<switch test='`$a == $b`' />
269 assertEquals("true",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("20"), ctx));
271 //<switch test="`$a == $b`" />
272 assertEquals("true",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("21"), ctx));
274 //<switch test='$a == $b' />
275 assertEquals("$a == $b",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("22"), ctx));
277 //<switch test="$a == $b" />
278 assertEquals("$a == $b",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("23"), ctx));