2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.ccsdk.sli.core.sli.provider;
23 import java.io.BufferedReader;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
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;
37 import junit.framework.Assert;
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 SvcLogicContext ctx = new SvcLogicContext();
54 SvcLogicGraph graph = new SvcLogicGraph();
55 SvcLogicNode node = new SvcLogicNode(1, "return", graph);
56 graph.setRootNode(node);
60 while ((line = testsReader.readLine()) != null) {
62 if (line.startsWith("#"))
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();
69 if (name.startsWith("$"))
71 LOG.info("Setting context attribute "+name+" = "+value);
72 ctx.setAttribute(name.substring(1), value);
77 LOG.info("Setting node attribute "+name+" = "+value);
78 node.setAttribute(name, value);
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();
90 LOG.info("Parsing expression "+testExpr);
91 SvcLogicExpression expr = SvcLogicExpressionFactory.parse(testExpr);
94 fail("Unable to parse expression "+testExpr);
98 LOG.info("Evaluating parsed expression "+expr.asParsedExpr());
99 String exprValue = SvcLogicExpressionResolver.evaluate(expr, node, ctx);
100 if (exprValue == null)
102 fail("Unable to evaluate expression "+testExpr);
106 LOG.info("Expression " + testExpr + " evaluates to " + exprValue);
107 if (expectedValue != null) {
108 Assert.assertEquals("Line " + lineNo + ": " + testExpr, expectedValue, exprValue);
117 LOG.error("Caught exception", e);
118 fail("Caught exception");