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.TestCase;
40 public class SvcLogicExpressionResolverTest extends TestCase {
43 private static final Logger LOG = LoggerFactory
44 .getLogger(SvcLogicExpressionResolver.class);
46 public void testEvaluate()
48 InputStream testStr = getClass().getResourceAsStream("/expression.tests");
49 BufferedReader testsReader = new BufferedReader(new InputStreamReader(testStr));
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);
59 while ((testExpr = testsReader.readLine()) != null) {
60 testExpr = testExpr.trim();
61 if (testExpr.startsWith("#"))
63 testExpr = testExpr.substring(1).trim();
64 String[] nameValue = testExpr.split("=");
65 String name = nameValue[0].trim();
66 String value = nameValue[1].trim();
68 if (name.startsWith("$"))
70 LOG.info("Setting context attribute "+name+" = "+value);
71 ctx.setAttribute(name.substring(1), value);
76 LOG.info("Setting node attribute "+name+" = "+value);
77 node.setAttribute(name, value);
83 LOG.info("Parsing expression "+testExpr);
84 SvcLogicExpression expr = SvcLogicExpressionFactory.parse(testExpr);
87 fail("Unable to parse expression "+testExpr);
91 LOG.info("Evaluating parsed expression "+expr.asParsedExpr());
92 String exprValue = SvcLogicExpressionResolver.evaluate(expr, node, ctx);
93 if (exprValue == null)
95 fail("Unable to evaluate expression "+testExpr);
99 LOG.info("Expression "+testExpr+" evaluates to "+exprValue);
107 LOG.error("Caught exception", e);
108 fail("Caught exception");