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