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