ecfa33e20db8971c1f0bf5c8e5982131edd943fd
[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.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                         String testExpr = null;
54                         SvcLogicContext ctx = new SvcLogicContext();
55                         SvcLogicGraph graph = new SvcLogicGraph();
56                         SvcLogicNode node = new SvcLogicNode(1, "return", graph);
57                         graph.setRootNode(node);
58                         
59                         while ((testExpr = testsReader.readLine()) != null) {
60                                 testExpr = testExpr.trim();
61                                 if (testExpr.startsWith("#"))
62                                 {
63                                         testExpr = testExpr.substring(1).trim();
64                                         String[] nameValue = testExpr.split("=");
65                                         String name = nameValue[0].trim();
66                                         String value = nameValue[1].trim();
67                                         
68                                         if (name.startsWith("$"))
69                                         {
70                                                 LOG.info("Setting context attribute "+name+" = "+value);
71                                                 ctx.setAttribute(name.substring(1), value);
72                                         }
73                                         else
74                                         {
75
76                                                 LOG.info("Setting node attribute "+name+" = "+value);
77                                                 node.setAttribute(name, value);
78                                                 
79                                         }
80                                 }
81                                 else
82                                 {
83                                         LOG.info("Parsing expression "+testExpr);
84                                         SvcLogicExpression expr = SvcLogicExpressionFactory.parse(testExpr);
85                                         if (expr == null)
86                                         {
87                                                 fail("Unable to parse expression "+testExpr);
88                                         }
89                                         else
90                                         {
91                                                 LOG.info("Evaluating parsed expression "+expr.asParsedExpr());
92                                                 String exprValue = SvcLogicExpressionResolver.evaluate(expr,  node, ctx);
93                                                 if (exprValue == null)
94                                                 {
95                                                         fail("Unable to evaluate expression "+testExpr);
96                                                 }
97                                                 else
98                                                 {
99                                                         LOG.info("Expression "+testExpr+" evaluates to "+exprValue);
100                                                 }
101                                         }
102                                 }
103                         }
104                 }
105                 catch (Exception e)
106                 {
107                         LOG.error("Caught exception", e);
108                         fail("Caught exception");
109                 }
110         }
111
112 }