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