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.onap.ccsdk.sli.core.sli.provider;
24 import java.io.BufferedReader;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
28 import java.util.Enumeration;
29 import java.util.HashMap;
30 import java.util.LinkedList;
32 import java.util.Properties;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicGraph;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicParser;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicStore;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicStoreFactory;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import ch.vorburger.mariadb4j.DB;
43 import ch.vorburger.mariadb4j.DBConfigurationBuilder;
44 import junit.framework.TestCase;
46 public class ITCaseSvcLogicGraphExecutor extends TestCase {
47 private static final Logger LOG = LoggerFactory
48 .getLogger(SvcLogicGraph.class);
50 private static final Map<String, SvcLogicNodeExecutor> BUILTIN_NODES = new HashMap<String, SvcLogicNodeExecutor>() {
52 put("block", new BlockNodeExecutor());
53 put("call", new CallNodeExecutor());
54 put("configure", new ConfigureNodeExecutor());
55 put("delete", new DeleteNodeExecutor());
56 put("execute", new ExecuteNodeExecutor());
57 put("exists", new ExistsNodeExecutor());
58 put("for", new ForNodeExecutor());
59 put("get-resource", new GetResourceNodeExecutor());
60 put("is-available", new IsAvailableNodeExecutor());
61 put("notify", new NotifyNodeExecutor());
62 put("record", new RecordNodeExecutor());
63 put("release", new ReleaseNodeExecutor());
64 put("reserve", new ReserveNodeExecutor());
65 put("return", new ReturnNodeExecutor());
66 put("save", new SaveNodeExecutor());
67 put("set", new SetNodeExecutor());
68 put("switch", new SwitchNodeExecutor());
69 put("update", new UpdateNodeExecutor());
74 public void testExecute() {
77 InputStream testStr = getClass().getResourceAsStream("/executor.tests");
78 BufferedReader testsReader = new BufferedReader(new InputStreamReader(testStr));
80 InputStream propStr = getClass().getResourceAsStream("/svclogic.properties");
82 Properties svcprops = new Properties();
83 svcprops.load(propStr);
85 // Start MariaDB4j database
86 DBConfigurationBuilder config = DBConfigurationBuilder.newBuilder();
87 config.setPort(0); // 0 => autom. detect free port
88 DB db = DB.newEmbeddedDB(config.build());
93 // Override jdbc URL and database name
94 svcprops.setProperty("org.onap.ccsdk.sli.jdbc.database", "test");
95 svcprops.setProperty("org.onap.ccsdk.sli.jdbc.url", config.getURL("test"));
99 SvcLogicStore store = SvcLogicStoreFactory.getSvcLogicStore(svcprops);
101 assertNotNull(store);
103 store.registerNodeType("switch");
104 store.registerNodeType("block");
105 store.registerNodeType("get-resource");
106 store.registerNodeType("reserve");
107 store.registerNodeType("is-available");
108 store.registerNodeType("exists");
109 store.registerNodeType("configure");
110 store.registerNodeType("return");
111 store.registerNodeType("record");
112 store.registerNodeType("allocate");
113 store.registerNodeType("release");
114 store.registerNodeType("for");
115 store.registerNodeType("set");
116 SvcLogicParser parser = new SvcLogicParser(store);
118 // Loop through executor tests
120 SvcLogicServiceImpl svc = new SvcLogicServiceImpl();
122 for (String nodeType : BUILTIN_NODES.keySet()) {
124 LOG.info("SLI - registering node executor for node type "+nodeType);
126 svc.registerExecutor(nodeType, BUILTIN_NODES.get(nodeType));
129 String testCaseLine = null;
130 while ((testCaseLine = testsReader.readLine()) != null) {
132 String[] testCaseFields = testCaseLine.split(":");
133 String testCaseFile = testCaseFields[0];
134 String testCaseMethod = testCaseFields[1];
135 String testCaseParameters = null;
137 if (testCaseFields.length > 2) {
138 testCaseParameters = testCaseFields[2];
141 SvcLogicContext ctx = new SvcLogicContext();
142 if (testCaseParameters != null) {
143 String[] testCaseParameterSettings = testCaseParameters.split(",");
145 for (int i = 0 ; i < testCaseParameterSettings.length ; i++) {
146 String[] nameValue = testCaseParameterSettings[i].split("=");
147 if (nameValue != null) {
148 String name = nameValue[0];
150 if (nameValue.length > 1) {
151 value = nameValue[1];
154 ctx.setAttribute(name, value);
159 testCaseFile = testCaseFile.trim();
161 if (testCaseFile.length() > 0) {
162 if (!testCaseFile.startsWith("/")) {
163 testCaseFile = "/"+testCaseFile;
165 URL testCaseUrl = getClass().getResource(testCaseFile);
166 if (testCaseUrl == null) {
167 fail("Could not resolve test case file "+testCaseFile);
170 LinkedList<SvcLogicGraph> graphs = parser.parse(testCaseUrl.getPath());
173 assertNotNull(graphs);
175 for (SvcLogicGraph graph: graphs) {
176 if (graph.getRpc().equals(testCaseMethod)) {
177 Properties props = ctx.toProperties();
178 LOG.info("SvcLogicContext before executing "+testCaseMethod+":");
179 for (Enumeration e1 = props.propertyNames(); e1.hasMoreElements() ; ) {
180 String propName = (String) e1.nextElement();
181 LOG.info(propName+" = "+props.getProperty(propName));
184 svc.execute(graph, ctx);
186 props = ctx.toProperties();
187 LOG.info("SvcLogicContext after executing "+testCaseMethod+":");
188 for (Enumeration e2 = props.propertyNames(); e2.hasMoreElements() ; ) {
189 String propName = (String) e2.nextElement();
190 LOG.info(propName+" = "+props.getProperty(propName));
201 } catch (Exception e) {
202 LOG.error("Caught exception executing directed graphs", e);
203 fail("Exception executing graphs");