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.openecomp.sdnc.sli.provider;
24 import java.io.BufferedReader;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
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;
38 import junit.framework.Assert;
39 import junit.framework.TestCase;
41 public class SvcLogicExpressionResolverTest extends TestCase {
44 private static final Logger LOG = LoggerFactory
45 .getLogger(SvcLogicExpressionResolver.class);
47 public void testEvaluate()
49 InputStream testStr = getClass().getResourceAsStream("/expression.tests");
50 BufferedReader testsReader = new BufferedReader(new InputStreamReader(testStr));
54 SvcLogicContext ctx = new SvcLogicContext();
55 SvcLogicGraph graph = new SvcLogicGraph();
56 SvcLogicNode node = new SvcLogicNode(1, "return", graph);
57 graph.setRootNode(node);
61 while ((line = testsReader.readLine()) != null) {
63 if (line.startsWith("#"))
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();
70 if (name.startsWith("$"))
72 LOG.info("Setting context attribute "+name+" = "+value);
73 ctx.setAttribute(name.substring(1), value);
78 LOG.info("Setting node attribute "+name+" = "+value);
79 node.setAttribute(name, value);
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();
91 LOG.info("Parsing expression "+testExpr);
92 SvcLogicExpression expr = SvcLogicExpressionFactory.parse(testExpr);
95 fail("Unable to parse expression "+testExpr);
99 LOG.info("Evaluating parsed expression "+expr.asParsedExpr());
100 String exprValue = SvcLogicExpressionResolver.evaluate(expr, node, ctx);
101 if (exprValue == null)
103 fail("Unable to evaluate expression "+testExpr);
107 LOG.info("Expression " + testExpr + " evaluates to " + exprValue);
108 if (expectedValue != null) {
109 Assert.assertEquals("Line " + lineNo + ": " + testExpr, expectedValue, exprValue);
118 LOG.error("Caught exception", e);
119 fail("Caught exception");