Eliminate ranges
[sdnc/core.git] / sli / provider / src / test / java / org / openecomp / sdnc / sli / provider / SvcLogicExpressionResolverTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
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.openecomp.sdnc.sli.provider;
23
24 import java.io.BufferedReader;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27
28 import org.openecomp.sdnc.sli.SvcLogicContext;
29 import org.openecomp.sdnc.sli.SvcLogicExprListener;
30 import org.openecomp.sdnc.sli.SvcLogicExpression;
31 import org.openecomp.sdnc.sli.SvcLogicExpressionFactory;
32 import org.openecomp.sdnc.sli.SvcLogicGraph;
33 import org.openecomp.sdnc.sli.SvcLogicNode;
34 import org.openecomp.sdnc.sli.provider.SvcLogicExpressionResolver;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import junit.framework.Assert;
39 import junit.framework.TestCase;
40
41 public class SvcLogicExpressionResolverTest extends TestCase {
42         
43
44         private static final Logger LOG = LoggerFactory
45                         .getLogger(SvcLogicExpressionResolver.class);
46         
47         public void testEvaluate()
48         {
49                 InputStream testStr = getClass().getResourceAsStream("/expression.tests");
50                 BufferedReader testsReader = new BufferedReader(new InputStreamReader(testStr));
51                 
52                 try
53                 {
54                         SvcLogicContext ctx = new SvcLogicContext();
55                         SvcLogicGraph graph = new SvcLogicGraph();
56                         SvcLogicNode node = new SvcLogicNode(1, "return", graph);
57                         graph.setRootNode(node);
58
59                         String line = null;
60                         int lineNo = 0;
61                         while ((line = testsReader.readLine()) != null) {
62                                 ++lineNo;
63                                 if (line.startsWith("#"))
64                                 {
65                                         String testExpr = line.trim().substring(1).trim();
66                                         String[] nameValue = testExpr.split("=");
67                                         String name = nameValue[0].trim();
68                                         String value = nameValue[1].trim();
69
70                                         if (name.startsWith("$"))
71                                         {
72                                                 LOG.info("Setting context attribute "+name+" = "+value);
73                                                 ctx.setAttribute(name.substring(1), value);
74                                         }
75                                         else
76                                         {
77
78                                                 LOG.info("Setting node attribute "+name+" = "+value);
79                                                 node.setAttribute(name, value);
80                                                 
81                                         }
82                                 }
83                                 else
84                                 {
85                                         // if the line contains #, what comes before is the expression to evaluate, and what comes after
86                                         // is the expected value
87                                         String[] substrings = line.split("#");
88                                         String expectedValue = substrings.length > 1 ? substrings[1].trim() : null;
89                                         String testExpr = substrings[0].trim();
90
91                                         LOG.info("Parsing expression "+testExpr);
92                                         SvcLogicExpression expr = SvcLogicExpressionFactory.parse(testExpr);
93                                         if (expr == null)
94                                         {
95                                                 fail("Unable to parse expression "+testExpr);
96                                         }
97                                         else
98                                         {
99                                                 LOG.info("Evaluating parsed expression "+expr.asParsedExpr());
100                                                 String exprValue = SvcLogicExpressionResolver.evaluate(expr,  node, ctx);
101                                                 if (exprValue == null)
102                                                 {
103                                                         fail("Unable to evaluate expression "+testExpr);
104                                                 }
105                                                 else
106                                                 {
107                                                         LOG.info("Expression " + testExpr + " evaluates to " + exprValue);
108                                                         if (expectedValue != null) {
109                                                                 Assert.assertEquals("Line " + lineNo + ": " + testExpr, expectedValue, exprValue);
110                                                         }
111                                                 }
112                                         }
113                                 }
114                         }
115                 }
116                 catch (Exception e)
117                 {
118                         LOG.error("Caught exception", e);
119                         fail("Caught exception");
120                 }
121         }
122
123 }