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