Update groupId to org.onap.ccsdk.sli
[ccsdk/sli/core.git] / sli / provider / src / test / java / org / onap / ccsdk / sli / core / sli / provider / SvcLogicExpressionResolverTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2017 ONAP
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.ccsdk.sli.core.sli.provider;
22
23 import java.io.BufferedReader;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26
27 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
28 import org.onap.ccsdk.sli.core.sli.SvcLogicExprListener;
29 import org.onap.ccsdk.sli.core.sli.SvcLogicExpression;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicExpressionFactory;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicGraph;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicNode;
33 import org.onap.ccsdk.sli.core.sli.provider.SvcLogicExpressionResolver;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import junit.framework.Assert;
38 import junit.framework.TestCase;
39
40 public class SvcLogicExpressionResolverTest extends TestCase {
41         
42
43         private static final Logger LOG = LoggerFactory
44                         .getLogger(SvcLogicExpressionResolver.class);
45         
46         public void testEvaluate()
47         {
48                 InputStream testStr = getClass().getResourceAsStream("/expression.tests");
49                 BufferedReader testsReader = new BufferedReader(new InputStreamReader(testStr));
50                 
51                 try
52                 {
53                         SvcLogicContext ctx = new SvcLogicContext();
54                         SvcLogicGraph graph = new SvcLogicGraph();
55                         SvcLogicNode node = new SvcLogicNode(1, "return", graph);
56                         graph.setRootNode(node);
57
58                         String line = null;
59                         int lineNo = 0;
60                         while ((line = testsReader.readLine()) != null) {
61                                 ++lineNo;
62                                 if (line.startsWith("#"))
63                                 {
64                                         String testExpr = line.trim().substring(1).trim();
65                                         String[] nameValue = testExpr.split("=");
66                                         String name = nameValue[0].trim();
67                                         String value = nameValue[1].trim();
68
69                                         if (name.startsWith("$"))
70                                         {
71                                                 LOG.info("Setting context attribute "+name+" = "+value);
72                                                 ctx.setAttribute(name.substring(1), value);
73                                         }
74                                         else
75                                         {
76
77                                                 LOG.info("Setting node attribute "+name+" = "+value);
78                                                 node.setAttribute(name, value);
79                                                 
80                                         }
81                                 }
82                                 else
83                                 {
84                                         // if the line contains #, what comes before is the expression to evaluate, and what comes after
85                                         // is the expected value
86                                         String[] substrings = line.split("#");
87                                         String expectedValue = substrings.length > 1 ? substrings[1].trim() : null;
88                                         String testExpr = substrings[0].trim();
89
90                                         LOG.info("Parsing expression "+testExpr);
91                                         SvcLogicExpression expr = SvcLogicExpressionFactory.parse(testExpr);
92                                         if (expr == null)
93                                         {
94                                                 fail("Unable to parse expression "+testExpr);
95                                         }
96                                         else
97                                         {
98                                                 LOG.info("Evaluating parsed expression "+expr.asParsedExpr());
99                                                 String exprValue = SvcLogicExpressionResolver.evaluate(expr,  node, ctx);
100                                                 if (exprValue == null)
101                                                 {
102                                                         fail("Unable to evaluate expression "+testExpr);
103                                                 }
104                                                 else
105                                                 {
106                                                         LOG.info("Expression " + testExpr + " evaluates to " + exprValue);
107                                                         if (expectedValue != null) {
108                                                                 Assert.assertEquals("Line " + lineNo + ": " + testExpr, expectedValue, exprValue);
109                                                         }
110                                                 }
111                                         }
112                                 }
113                         }
114                 }
115                 catch (Exception e)
116                 {
117                         LOG.error("Caught exception", e);
118                         fail("Caught exception");
119                 }
120         }
121
122 }