13b5fdfb36b0dcabeb0f7d51aae8ac5ef0d0d85a
[appc.git] / appc-adapters / appc-chef-adapter / appc-chef-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 package org.onap.appc.test;
27
28 import java.lang.reflect.Field;
29 import java.lang.reflect.Method;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34
35 import org.onap.appc.test.InterceptLogger;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
38
39 /**
40  * This class is used as a test harness to wrap the call to an executor node.
41  */
42
43 public class ExecutorHarness {
44
45     /**
46      * The executor to be tested
47      */
48     private SvcLogicJavaPlugin executor;
49
50     /**
51      * The collection of all exec methods found on the class
52      */
53     private Map<String, Method> methods;
54
55     /**
56      * The field of the class being tested that contains the reference to the logger to be used. This is modified to
57      * point to our interception logger for the test.
58      */
59     private Field contextLogger;
60
61     /**
62      * The interception logger that buffers all messages logged and allows us to look at them as part of the test case.
63      */
64     private InterceptLogger logger;
65
66     /**
67      * Create the harness and initialize it
68      * 
69      * @throws SecurityException
70      *             If a security manager, s, is present and any of the following conditions is met:
71      *             <ul>
72      *             <li>invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared field</li>
73      *             <li>the caller's class loader is not the same as or an ancestor of the class loader for the current
74      *             class and invocation of s.checkPackageAccess() denies access to the package of this class</li>
75      *             </ul>
76      * @throws NoSuchFieldException
77      *             if a field with the specified name is not found.
78      * @throws IllegalAccessException
79      *             if this Field object is enforcing Java language access control and the underlying field is either
80      *             inaccessible or final.
81      * @throws IllegalArgumentException
82      *             if the specified object is not an instance of the class or interface declaring the underlying field
83      *             (or a subclass or implementor thereof), or if an unwrapping conversion fails.
84      */
85     @SuppressWarnings("nls")
86     public ExecutorHarness() throws NoSuchFieldException, SecurityException, IllegalArgumentException,
87                     IllegalAccessException {
88         methods = new HashMap<>();
89         new SvcLogicContext();
90
91         Class<?> contextClass = SvcLogicContext.class;
92         contextLogger = contextClass.getDeclaredField("LOG");
93         contextLogger.setAccessible(true);
94         logger = new InterceptLogger();
95         contextLogger.set(null, logger);
96     }
97
98     /**
99      * Convenience constructor
100      * 
101      * @param executor
102      *            The executor to be tested by the harness
103      * @throws SecurityException
104      *             If a security manager, s, is present and any of the following conditions is met:
105      *             <ul>
106      *             <li>invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared field</li>
107      *             <li>the caller's class loader is not the same as or an ancestor of the class loader for the current
108      *             class and invocation of s.checkPackageAccess() denies access to the package of this class</li>
109      *             </ul>
110      * @throws NoSuchFieldException
111      *             if a field with the specified name is not found.
112      * @throws IllegalAccessException
113      *             if this Field object is enforcing Java language access control and the underlying field is either
114      *             inaccessible or final.
115      * @throws IllegalArgumentException
116      *             if the specified object is not an instance of the class or interface declaring the underlying field
117      *             (or a subclass or implementor thereof), or if an unwrapping conversion fails.
118      */
119     public ExecutorHarness(SvcLogicJavaPlugin executor) throws NoSuchFieldException, SecurityException,
120                     IllegalArgumentException, IllegalAccessException {
121         this();
122         setExecutor(executor);
123     }
124
125     /**
126      * @param executor
127      *            The java plugin class to be executed
128      */
129     public void setExecutor(SvcLogicJavaPlugin executor) {
130         this.executor = executor;
131         scanExecutor();
132     }
133
134     /**
135      * @return The java plugin class to be executed
136      */
137     public SvcLogicJavaPlugin getExecutor() {
138         return executor;
139     }
140
141     /**
142      * @return The set of all methods that meet the signature requirements
143      */
144     public List<String> getExecMethodNames() {
145         List<String> names = new ArrayList<>();
146         names.addAll(methods.keySet());
147         return names;
148     }
149
150     /**
151      * Returns an indication if the named method is a valid executor method that could be called from a DG execute node
152      * 
153      * @param methodName
154      *            The method name to be validated
155      * @return True if the method name meets the signature requirements, false if the method either does not exist or
156      *         does not meet the requirements.
157      */
158     public boolean isExecMethod(String methodName) {
159         return methods.containsKey(methodName);
160     }
161
162     /**
163      * This method scans the executor class hierarchy to locate all methods that match the required signature of the
164      * executor and records these methods in a map.
165      */
166     private void scanExecutor() {
167         methods.clear();
168         Class<?> executorClass = executor.getClass();
169         Method[] publicMethods = executorClass.getMethods();
170         for (Method method : publicMethods) {
171             if (method.getReturnType().equals(Void.class)) {
172                 Class<?>[] paramTypes = method.getParameterTypes();
173                 if (paramTypes.length == 2) {
174                     if (Map.class.isAssignableFrom(paramTypes[0])
175                         && SvcLogicContext.class.isAssignableFrom(paramTypes[1])) {
176                         methods.put(method.getName(), method);
177                     }
178                 }
179             }
180         }
181     }
182 }