Refactor dblib
[ccsdk/sli/core.git] / sli / provider / src / test / java / org / onap / ccsdk / sli / core / sli / provider / SvcLogicGraphExecutorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.ccsdk.sli.core.sli.provider;
22
23 import java.io.BufferedReader;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.net.URL;
27 import java.util.Enumeration;
28 import java.util.HashMap;
29 import java.util.LinkedList;
30 import java.util.Map;
31 import java.util.Properties;
32
33 import org.onap.ccsdk.sli.core.sli.MetricLogger;
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.SvcLogicNode;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicParser;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicStore;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicStoreFactory;
40 import org.onap.ccsdk.sli.core.sli.provider.BlockNodeExecutor;
41 import org.onap.ccsdk.sli.core.sli.provider.CallNodeExecutor;
42 import org.onap.ccsdk.sli.core.sli.provider.ConfigureNodeExecutor;
43 import org.onap.ccsdk.sli.core.sli.provider.DeleteNodeExecutor;
44 import org.onap.ccsdk.sli.core.sli.provider.ExecuteNodeExecutor;
45 import org.onap.ccsdk.sli.core.sli.provider.ExistsNodeExecutor;
46 import org.onap.ccsdk.sli.core.sli.provider.ForNodeExecutor;
47 import org.onap.ccsdk.sli.core.sli.provider.GetResourceNodeExecutor;
48 import org.onap.ccsdk.sli.core.sli.provider.IsAvailableNodeExecutor;
49 import org.onap.ccsdk.sli.core.sli.provider.NotifyNodeExecutor;
50 import org.onap.ccsdk.sli.core.sli.provider.RecordNodeExecutor;
51 import org.onap.ccsdk.sli.core.sli.provider.ReleaseNodeExecutor;
52 import org.onap.ccsdk.sli.core.sli.provider.ReserveNodeExecutor;
53 import org.onap.ccsdk.sli.core.sli.provider.ReturnNodeExecutor;
54 import org.onap.ccsdk.sli.core.sli.provider.SaveNodeExecutor;
55 import org.onap.ccsdk.sli.core.sli.provider.SetNodeExecutor;
56 import org.onap.ccsdk.sli.core.sli.provider.SvcLogicNodeExecutor;
57 import org.onap.ccsdk.sli.core.sli.provider.SvcLogicServiceImpl;
58 import org.onap.ccsdk.sli.core.sli.provider.SwitchNodeExecutor;
59 import org.onap.ccsdk.sli.core.sli.provider.UpdateNodeExecutor;
60 import org.osgi.framework.ServiceRegistration;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63
64 import junit.framework.TestCase;
65
66 public class SvcLogicGraphExecutorTest extends TestCase {
67         private static final Logger LOG = LoggerFactory
68                         .getLogger(SvcLogicGraph.class);
69         
70         private static final Map<String, SvcLogicNodeExecutor> BUILTIN_NODES = new HashMap<String, SvcLogicNodeExecutor>() {
71                 {
72                         put("block", new BlockNodeExecutor());
73                         put("call", new CallNodeExecutor());
74                         put("configure", new ConfigureNodeExecutor());
75                         put("delete", new DeleteNodeExecutor());
76                         put("execute", new ExecuteNodeExecutor());
77                         put("exists", new ExistsNodeExecutor());
78                         put("for", new ForNodeExecutor());
79                         put("get-resource", new GetResourceNodeExecutor());
80                         put("is-available", new IsAvailableNodeExecutor());
81                         put("notify", new NotifyNodeExecutor());
82                         put("record", new RecordNodeExecutor());
83                         put("release", new ReleaseNodeExecutor());
84                         put("reserve", new ReserveNodeExecutor());
85                         put("return", new ReturnNodeExecutor());
86                         put("save", new SaveNodeExecutor());
87                         put("set", new SetNodeExecutor());
88                         put("switch", new SwitchNodeExecutor());
89                         put("update", new UpdateNodeExecutor());
90
91                 }
92         };
93         
94         public void testExecute() {
95                 
96                 try {
97                         InputStream testStr = getClass().getResourceAsStream("/executor.tests");
98                         BufferedReader testsReader = new BufferedReader(new InputStreamReader(testStr));
99                         
100                         InputStream propStr = getClass().getResourceAsStream("/svclogic.properties");
101                         
102                         SvcLogicStore store = SvcLogicStoreFactory.getSvcLogicStore(propStr);
103                         
104                         assertNotNull(store);
105                         
106                         store.registerNodeType("switch");
107                         store.registerNodeType("block");
108                         store.registerNodeType("get-resource");
109                         store.registerNodeType("reserve");
110                         store.registerNodeType("is-available");
111                         store.registerNodeType("exists");
112                         store.registerNodeType("configure");
113                         store.registerNodeType("return");
114                         store.registerNodeType("record");
115                         store.registerNodeType("allocate");
116                         store.registerNodeType("release");
117                         store.registerNodeType("for");
118                         store.registerNodeType("set");
119                         SvcLogicParser parser = new SvcLogicParser(store);
120                         
121                         // Loop through executor tests
122
123                         SvcLogicServiceImpl svc = new SvcLogicServiceImpl();
124                         
125                         for (String nodeType : BUILTIN_NODES.keySet()) {
126
127                                 LOG.info("SLI - registering node executor for node type "+nodeType);
128                                 
129                                 svc.registerExecutor(nodeType, BUILTIN_NODES.get(nodeType));
130
131                         }
132                         String testCaseLine = null;
133                         while ((testCaseLine = testsReader.readLine()) != null) {
134                                 
135                                 String[] testCaseFields = testCaseLine.split(":");
136                                 String testCaseFile = testCaseFields[0];
137                                 String testCaseMethod = testCaseFields[1];
138                                 String testCaseParameters = null;
139                                 
140                                 if (testCaseFields.length > 2) {
141                                         testCaseParameters = testCaseFields[2];
142                                 }
143
144                                 SvcLogicContext ctx = new SvcLogicContext();
145                                 if (testCaseParameters != null) {
146                                         String[] testCaseParameterSettings = testCaseParameters.split(",");
147                                         
148                                         for (int i = 0 ; i < testCaseParameterSettings.length ; i++) {
149                                                 String[] nameValue = testCaseParameterSettings[i].split("=");
150                                                 if (nameValue != null) {
151                                                         String name = nameValue[0];
152                                                         String value = "";
153                                                         if (nameValue.length > 1) {
154                                                                 value = nameValue[1];
155                                                         }
156                                                         
157                                                         ctx.setAttribute(name,  value);
158                                                 }
159                                         }
160                                 }
161                                 
162                                 testCaseFile = testCaseFile.trim();
163                                 
164                                 if (testCaseFile.length() > 0) {
165                                         if (!testCaseFile.startsWith("/")) {
166                                                 testCaseFile = "/"+testCaseFile;
167                                         }
168                                         URL testCaseUrl = getClass().getResource(testCaseFile);
169                                         if (testCaseUrl == null) {
170                                                 fail("Could not resolve test case file "+testCaseFile);
171                                         }
172                                         
173                                         LinkedList<SvcLogicGraph> graphs = parser.parse(testCaseUrl.getPath());
174                                         
175
176                                         assertNotNull(graphs);
177                                         
178                                         for (SvcLogicGraph graph: graphs) {
179                                                 if (graph.getRpc().equals(testCaseMethod)) {
180                                                         Properties props = ctx.toProperties();
181                                                         LOG.info("SvcLogicContext before executing "+testCaseMethod+":");
182                                                         for (Enumeration e1 = props.propertyNames(); e1.hasMoreElements() ; ) {
183                                                                 String propName = (String) e1.nextElement();
184                                                                 LOG.info(propName+" = "+props.getProperty(propName));
185                                                         }
186                                                         
187                                                         svc.execute(graph, ctx);
188                                                         
189                                                         props = ctx.toProperties();
190                                                         LOG.info("SvcLogicContext after executing "+testCaseMethod+":");
191                                                         for (Enumeration e2 = props.propertyNames(); e2.hasMoreElements() ; ) {
192                                                                 String propName = (String) e2.nextElement();
193                                                                 LOG.info(propName+" = "+props.getProperty(propName));
194                                                         }
195                                                 }
196                                         }
197
198                                 }
199                                 
200                                 
201                         }
202                         
203                         
204                 } catch (Exception e) {
205                         LOG.error("Caught exception executing directed graphs", e);
206                         fail("Exception executing graphs");
207                 }
208         }
209
210         
211 }