054b38d0f551a5eac347fd1c82fa1d788415e25f
[ccsdk/sli.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.onap.ccsdk.sli.core.sli.provider.base;
23
24 import java.io.BufferedReader;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.util.LinkedList;
28
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;
39
40 import junit.framework.Assert;
41 import junit.framework.TestCase;
42
43 public class SvcLogicExpressionResolverTest extends TestCase {
44         
45
46         private static final Logger LOG = LoggerFactory
47                         .getLogger(SvcLogicExpressionResolver.class);
48         
49         public void testEvaluate()
50         {
51                 InputStream testStr = getClass().getResourceAsStream("/expression.tests");
52                 BufferedReader testsReader = new BufferedReader(new InputStreamReader(testStr));
53                 
54                 try
55                 {
56                         SvcLogicContext ctx = new SvcLogicContext();
57                         SvcLogicGraph graph = new SvcLogicGraph();
58                         SvcLogicNode node = new SvcLogicNode(1, "return", graph);
59                         graph.setRootNode(node);
60
61                         String line = null;
62                         int lineNo = 0;
63                         while ((line = testsReader.readLine()) != null) {
64                                 ++lineNo;
65                                 if (line.startsWith("#"))
66                                 {
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();
71
72                                         if (name.startsWith("$"))
73                                         {
74                                                 LOG.info("Setting context attribute "+name+" = "+value);
75                                                 ctx.setAttribute(name.substring(1), value);
76                                         }
77                                         else
78                                         {
79
80                                                 LOG.info("Setting node attribute "+name+" = "+value);
81                                                 node.setAttribute(name, value);
82                                                 
83                                         }
84                                 }
85                                 else
86                                 {
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();
92
93                                         LOG.info("Parsing expression "+testExpr);
94                                         SvcLogicExpression expr = SvcLogicExpressionFactory.parse(testExpr);
95                                         if (expr == null)
96                                         {
97                                                 fail("Unable to parse expression "+testExpr);
98                                         }
99                                         else
100                                         {
101                                                 LOG.info("Evaluating parsed expression "+expr.asParsedExpr());
102                                                 String exprValue = SvcLogicExpressionResolver.evaluate(expr,  node, ctx);
103                                                 if (exprValue == null)
104                                                 {
105                                                         fail("Unable to evaluate expression "+testExpr);
106                                                 }
107                                                 else
108                                                 {
109                                                         LOG.info("Expression " + testExpr + " evaluates to " + exprValue);
110                                                         if (expectedValue != null) {
111                                                                 Assert.assertEquals("Line " + lineNo + ": " + testExpr, expectedValue, exprValue);
112                                                         }
113                                                 }
114                                         }
115                                 }
116                         }
117                 }
118                 catch (Exception e)
119                 {
120                         LOG.error("Caught exception", e);
121                         fail("Caught exception");
122                 }
123         }
124
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
133 /*      
134 <set>
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`" />
143 </set>
144 */      
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"));
157
158 //Test a set node that makes use of string concatenation        
159 /*
160 <set>
161         <parameter name='varA' value='`$a + $b`' />
162         <parameter name='varB' value='`$a + &apos;literal&apos; `' />
163         <parameter name='varC' value='`&apos;literal&apos; + $b `' />
164         <parameter name='varD' value='`&apos;too&apos; + &apos;literal&apos;`' />
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'`" />
169 </set>        
170 */
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"));
183
184 //Shows how backticks interact with + operator
185 /*
186 <set>
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`" />
193 </set>
194 */
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");
199
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"));
207
208         ctx.setAttribute("a", "5");
209         ctx.setAttribute("b", "3");
210
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     
213
214         //<switch test="`'PIZZA' == 'NOTPIZZA' or $a != $b`" />
215         assertEquals("true",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("4"), ctx));
216
217         //<switch test="`'PIZZA' == 'PIZZA' and $a != $b`" />
218         assertEquals("true",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("5"), ctx));
219
220         //<switch test="`'PIZZA' == 'NOTPIZZA' or $a &gt;= $b`" />
221         assertEquals("true",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("6"), ctx));
222
223         //<switch test="`'PIZZA' == 'PIZZA' and $b &lt; $a`" />
224         assertEquals("true",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("7"), ctx));
225
226         //<switch test="`'PIZZA' == 'PIZZA'`" />
227         assertEquals("true",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("8"), ctx));
228
229         //<switch test="`$a == $b`" />
230         assertEquals("false",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("9"), ctx));
231
232         //<switch test="`'PIZZA' == 'NOTPIZZA'`" />
233         assertEquals("false",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("10"), ctx));
234
235         //<switch test="`'PIZZA' != 'PIZZA'`" />
236         assertEquals("false",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("11"), ctx));
237
238         //<switch test="`'PIZZA' != 'NOTPIZZA'`" />
239         assertEquals("true",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("12"), ctx));
240
241         //<switch test='`$a != $b`' />
242         assertEquals("true",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("13"), ctx));
243
244         //<switch test='`1 &lt; 2`' />
245         assertEquals("true",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("14"), ctx));
246
247         //<switch test='`2 &lt;= 2`' />
248         assertEquals("true",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("15"), ctx));
249
250         //<switch test='`3 &gt; 2`' />
251         assertEquals("true",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("16"), ctx));
252
253         //<switch test='`2 &gt;= 2`' />
254         assertEquals("true",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("17"), ctx));
255
256         // Series of switch statements that show the effect of using backticks
257         
258         ctx.setAttribute("literalStartingWithDollarSign", "DONT READ ME!");
259         //<switch test='$literalStartingWithDollarSign'/>
260         assertEquals("$literalStartingWithDollarSign",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("18"), ctx));
261
262         ctx.setAttribute("dollarSignFollowedByVariableSurroundedinBackticks", "README");
263         //<switch test='$literalStartingWithDollarSign'/>
264         assertEquals("README",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("19"), ctx));
265
266         ctx.setAttribute("a", "2");
267         ctx.setAttribute("b", "2");
268         //<switch test='`$a == $b`' />
269         assertEquals("true",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("20"), ctx));
270
271         //<switch test="`$a == $b`" />
272         assertEquals("true",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("21"), ctx));
273         
274         //<switch test='$a == $b' />
275         assertEquals("$a == $b",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("22"), ctx));
276
277         //<switch test="$a == $b" />
278         assertEquals("$a == $b",switchNodeExecutor.evaluateNodeTest(root.getOutcomeValue("23"), ctx));
279     }
280 }