Change to CCSDK and ODL Carbon
[appc.git] / appc-adapters / appc-iaas-adapter / appc-iaas-adapter-bundle / src / test / java / org / openecomp / appc / test / ExecutorHarness.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25
26
27 package org.openecomp.appc.test;
28
29 import java.lang.reflect.Field;
30 import java.lang.reflect.Method;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35
36 import org.openecomp.appc.test.InterceptLogger;
37
38 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
40
41 /**
42  * This class is used as a test harness to wrap the call to an executor node.
43  */
44
45 public class ExecutorHarness {
46
47     /**
48      * The executor to be tested
49      */
50     private SvcLogicJavaPlugin executor;
51
52     /**
53      * The collection of all exec methods found on the class
54      */
55     private Map<String, Method> methods;
56
57     /**
58      * The field of the class being tested that contains the reference to the logger to be used. This is modified to
59      * point to our interception logger for the test.
60      */
61     private Field contextLogger;
62
63     /**
64      * The interception logger that buffers all messages logged and allows us to look at them as part of the test case.
65      */
66     private InterceptLogger logger;
67
68     /**
69      * Create the harness and initialize it
70      * 
71      * @throws SecurityException
72      *             If a security manager, s, is present and any of the following conditions is met:
73      *             <ul>
74      *             <li>invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared field</li>
75      *             <li>the caller's class loader is not the same as or an ancestor of the class loader for the current
76      *             class and invocation of s.checkPackageAccess() denies access to the package of this class</li>
77      *             </ul>
78      * @throws NoSuchFieldException
79      *             if a field with the specified name is not found.
80      * @throws IllegalAccessException
81      *             if this Field object is enforcing Java language access control and the underlying field is either
82      *             inaccessible or final.
83      * @throws IllegalArgumentException
84      *             if the specified object is not an instance of the class or interface declaring the underlying field
85      *             (or a subclass or implementor thereof), or if an unwrapping conversion fails.
86      */
87     @SuppressWarnings("nls")
88     public ExecutorHarness() throws NoSuchFieldException, SecurityException, IllegalArgumentException,
89                     IllegalAccessException {
90         methods = new HashMap<>();
91         new SvcLogicContext();
92
93         Class<?> contextClass = SvcLogicContext.class;
94         contextLogger = contextClass.getDeclaredField("LOG");
95         contextLogger.setAccessible(true);
96         logger = new InterceptLogger();
97         contextLogger.set(null, logger);
98     }
99
100     /**
101      * Convenience constructor
102      * 
103      * @param executor
104      *            The executor to be tested by the harness
105      * @throws SecurityException
106      *             If a security manager, s, is present and any of the following conditions is met:
107      *             <ul>
108      *             <li>invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared field</li>
109      *             <li>the caller's class loader is not the same as or an ancestor of the class loader for the current
110      *             class and invocation of s.checkPackageAccess() denies access to the package of this class</li>
111      *             </ul>
112      * @throws NoSuchFieldException
113      *             if a field with the specified name is not found.
114      * @throws IllegalAccessException
115      *             if this Field object is enforcing Java language access control and the underlying field is either
116      *             inaccessible or final.
117      * @throws IllegalArgumentException
118      *             if the specified object is not an instance of the class or interface declaring the underlying field
119      *             (or a subclass or implementor thereof), or if an unwrapping conversion fails.
120      */
121     public ExecutorHarness(SvcLogicJavaPlugin executor) throws NoSuchFieldException, SecurityException,
122                     IllegalArgumentException, IllegalAccessException {
123         this();
124         setExecutor(executor);
125     }
126
127     /**
128      * @param executor
129      *            The java plugin class to be executed
130      */
131     public void setExecutor(SvcLogicJavaPlugin executor) {
132         this.executor = executor;
133         scanExecutor();
134     }
135
136     /**
137      * @return The java plugin class to be executed
138      */
139     public SvcLogicJavaPlugin getExecutor() {
140         return executor;
141     }
142
143     /**
144      * @return The set of all methods that meet the signature requirements
145      */
146     public List<String> getExecMethodNames() {
147         List<String> names = new ArrayList<>();
148         names.addAll(methods.keySet());
149         return names;
150     }
151
152     /**
153      * Returns an indication if the named method is a valid executor method that could be called from a DG execute node
154      * 
155      * @param methodName
156      *            The method name to be validated
157      * @return True if the method name meets the signature requirements, false if the method either does not exist or
158      *         does not meet the requirements.
159      */
160     public boolean isExecMethod(String methodName) {
161         return methods.containsKey(methodName);
162     }
163
164     /**
165      * This method scans the executor class hierarchy to locate all methods that match the required signature of the
166      * executor and records these methods in a map.
167      */
168     private void scanExecutor() {
169         methods.clear();
170         Class<?> executorClass = executor.getClass();
171         Method[] publicMethods = executorClass.getMethods();
172         for (Method method : publicMethods) {
173             if (method.getReturnType().equals(Void.class)) {
174                 Class<?>[] paramTypes = method.getParameterTypes();
175                 if (paramTypes.length == 2) {
176                     if (Map.class.isAssignableFrom(paramTypes[0])
177                         && SvcLogicContext.class.isAssignableFrom(paramTypes[1])) {
178                         methods.put(method.getName(), method);
179                     }
180                 }
181             }
182         }
183     }
184 }