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