Refactor dblib
[ccsdk/sli/core.git] / sli / provider / src / test / java / org / onap / ccsdk / sli / core / sli / provider / PluginTest.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.lang.reflect.Method;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
28 import org.onap.ccsdk.sli.core.sli.SvcLogicGraph;
29 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicNode;
31 import org.onap.ccsdk.sli.core.sli.provider.ExecuteNodeExecutor;
32 import org.onap.ccsdk.sli.core.sli.provider.SvcLogicExpressionResolver;
33
34 import junit.framework.TestCase;
35
36 public class PluginTest extends TestCase {
37
38     // The existing plugins work just like a VoidDummyPlugin
39     // They will return null simply because they are all void
40     // The attribute emitsOutcome will not be present, the expected outcome is success when no exception is thrown by the plugin
41     public void testOldPlugin() throws Exception {
42         ExecuteNodeExecutor executor = new ExecuteNodeExecutor();
43         SvcLogicJavaPlugin plugin = new VoidDummyPlugin();
44
45         Class pluginClass = plugin.getClass();
46         Method pluginMethod = pluginClass.getMethod("dummy", Map.class, SvcLogicContext.class);
47         Map<String, String> parmMap = new HashMap<String, String>();
48         SvcLogicContext ctx = new SvcLogicContext();
49         Object o = pluginMethod.invoke(plugin, parmMap, ctx);
50
51         SvcLogicGraph graph = new SvcLogicGraph();
52         SvcLogicNode node = new SvcLogicNode(1, "return", graph);
53         String emitsOutcome = SvcLogicExpressionResolver.evaluate(node.getAttribute("emitsOutcome"),  node, ctx);
54         String outValue = executor.mapOutcome(o, emitsOutcome);
55         assertEquals("success",outValue);
56     }
57
58     //Newer plugins can set the attribute emitsOutcome to true, if so they should return a string
59     //The string represents the outcome value
60     public void testNewPlugin() throws Exception {
61         ExecuteNodeExecutor executor = new ExecuteNodeExecutor();
62         SvcLogicJavaPlugin plugin = new LunchSelectorPlugin();
63
64         Class pluginClass = plugin.getClass();
65         Method pluginMethod = pluginClass.getMethod("selectLunch", Map.class, SvcLogicContext.class);
66
67         Map<String, String> parmMap = new HashMap<String, String>();
68         SvcLogicContext ctx = new SvcLogicContext();
69
70         parmMap.put("day", "monday");
71         Object o = pluginMethod.invoke(plugin, parmMap, ctx);
72         SvcLogicGraph graph = new SvcLogicGraph();
73         SvcLogicNode node = new SvcLogicNode(1, "return", graph);
74         node.setAttribute("emitsOutcome", "true");
75         String emitsOutcome = SvcLogicExpressionResolver.evaluate(node.getAttribute("emitsOutcome"),  node, ctx);
76         String outValue = executor.mapOutcome(o, emitsOutcome);
77         assertEquals("pizza", outValue);
78
79         parmMap.put("day", "tuesday");
80         outValue = (String) pluginMethod.invoke(plugin, parmMap, ctx);
81         o = pluginMethod.invoke(plugin, parmMap, ctx);
82         outValue = executor.mapOutcome(o, emitsOutcome);
83         assertEquals("soup",outValue);
84
85     }
86
87     //APPC had some legacy plugins returning objects which should not be treated as outcomes
88     //The attribute emitsOutcome will not be set
89     //The outcome should be success as it has always been
90     public void testObjPlugin() throws Exception{
91         ExecuteNodeExecutor executor = new ExecuteNodeExecutor();
92         SvcLogicJavaPlugin plugin = new LunchSelectorPlugin();
93
94         Class pluginClass = plugin.getClass();
95         Method pluginMethod = pluginClass.getMethod("makeLunch", Map.class, SvcLogicContext.class);
96
97         Map<String, String> parmMap = new HashMap<String, String>();
98         SvcLogicContext ctx = new SvcLogicContext();
99         Object o = pluginMethod.invoke(plugin, parmMap, ctx);
100         SvcLogicGraph graph = new SvcLogicGraph();
101         SvcLogicNode node = new SvcLogicNode(1, "return", graph);
102         String emitsOutcome = SvcLogicExpressionResolver.evaluate(node.getAttribute("emitsOutcome"),  node, ctx);
103         String outValue = executor.mapOutcome(o, emitsOutcome);
104         assertEquals("success",outValue);
105     }
106
107 }