Merge "Clean up public interfaces"
[ccsdk/sli/core.git] / sli / provider / src / test / java / org / onap / ccsdk / sli / core / sli / provider / ITCaseSvcLogicGraphExecutor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
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.onap.ccsdk.sli.core.sli.provider;
23
24 import java.io.BufferedReader;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.net.URL;
28 import java.util.Enumeration;
29 import java.util.HashMap;
30 import java.util.LinkedList;
31 import java.util.Map;
32 import java.util.Properties;
33
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;
41
42 import ch.vorburger.mariadb4j.DB;
43 import ch.vorburger.mariadb4j.DBConfigurationBuilder;
44 import junit.framework.TestCase;
45
46 public class ITCaseSvcLogicGraphExecutor extends TestCase {
47         private static final Logger LOG = LoggerFactory
48                         .getLogger(SvcLogicGraph.class);
49
50         private static final Map<String, SvcLogicNodeExecutor> BUILTIN_NODES = new HashMap<String, SvcLogicNodeExecutor>() {
51                 {
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());
70
71                 }
72         };
73
74         public void testExecute() {
75
76                 try {
77                         InputStream testStr = getClass().getResourceAsStream("/executor.tests");
78                         BufferedReader testsReader = new BufferedReader(new InputStreamReader(testStr));
79
80                         InputStream propStr = getClass().getResourceAsStream("/svclogic.properties");
81
82                         Properties svcprops = new Properties();
83                         svcprops.load(propStr);
84
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());
89                 db.start();
90
91
92
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"));
96
97
98
99                         SvcLogicStore store = SvcLogicStoreFactory.getSvcLogicStore(svcprops);
100
101                         assertNotNull(store);
102
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);
117
118                         // Loop through executor tests
119
120                         SvcLogicServiceImpl svc = new SvcLogicServiceImpl();
121
122                         for (String nodeType : BUILTIN_NODES.keySet()) {
123
124                                 LOG.info("SLI - registering node executor for node type "+nodeType);
125
126                                 svc.registerExecutor(nodeType, BUILTIN_NODES.get(nodeType));
127
128                         }
129                         String testCaseLine = null;
130                         while ((testCaseLine = testsReader.readLine()) != null) {
131
132                                 String[] testCaseFields = testCaseLine.split(":");
133                                 String testCaseFile = testCaseFields[0];
134                                 String testCaseMethod = testCaseFields[1];
135                                 String testCaseParameters = null;
136
137                                 if (testCaseFields.length > 2) {
138                                         testCaseParameters = testCaseFields[2];
139                                 }
140
141                                 SvcLogicContext ctx = new SvcLogicContext();
142                                 if (testCaseParameters != null) {
143                                         String[] testCaseParameterSettings = testCaseParameters.split(",");
144
145                                         for (int i = 0 ; i < testCaseParameterSettings.length ; i++) {
146                                                 String[] nameValue = testCaseParameterSettings[i].split("=");
147                                                 if (nameValue != null) {
148                                                         String name = nameValue[0];
149                                                         String value = "";
150                                                         if (nameValue.length > 1) {
151                                                                 value = nameValue[1];
152                                                         }
153
154                                                         ctx.setAttribute(name,  value);
155                                                 }
156                                         }
157                                 }
158
159                                 testCaseFile = testCaseFile.trim();
160
161                                 if (testCaseFile.length() > 0) {
162                                         if (!testCaseFile.startsWith("/")) {
163                                                 testCaseFile = "/"+testCaseFile;
164                                         }
165                                         URL testCaseUrl = getClass().getResource(testCaseFile);
166                                         if (testCaseUrl == null) {
167                                                 fail("Could not resolve test case file "+testCaseFile);
168                                         }
169
170                                         LinkedList<SvcLogicGraph> graphs = parser.parse(testCaseUrl.getPath());
171
172
173                                         assertNotNull(graphs);
174
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));
182                                                         }
183
184                                                         svc.execute(graph, ctx);
185
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));
191                                                         }
192                                                 }
193                                         }
194
195                                 }
196
197
198                         }
199
200
201                 } catch (Exception e) {
202                         LOG.error("Caught exception executing directed graphs", e);
203                         fail("Exception executing graphs");
204                 }
205         }
206
207
208 }