Moving all files to root directory
[appc.git] / appc-adapters / appc-iaas-adapter / appc-iaas-adapter-bundle / src / test / java / org / openecomp / appc / test / ExecutorHarness.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22
23
24 package org.openecomp.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.openecomp.appc.test.InterceptLogger;
34
35 import org.openecomp.sdnc.sli.SvcLogicContext;
36 import org.openecomp.sdnc.sli.SvcLogicJavaPlugin;
37
38 /**
39  * This class is used as a test harness to wrap the call to an executor node.
40  */
41
42 public class ExecutorHarness {
43
44     /**
45      * The executor to be tested
46      */
47     private SvcLogicJavaPlugin executor;
48
49     /**
50      * The collection of all exec methods found on the class
51      */
52     private Map<String, Method> methods;
53
54     /**
55      * The field of the class being tested that contains the reference to the logger to be used. This is modified to
56      * point to our interception logger for the test.
57      */
58     private Field contextLogger;
59
60     /**
61      * The interception logger that buffers all messages logged and allows us to look at them as part of the test case.
62      */
63     private InterceptLogger logger;
64
65     /**
66      * Create the harness and initialize it
67      * 
68      * @throws SecurityException
69      *             If a security manager, s, is present and any of the following conditions is met:
70      *             <ul>
71      *             <li>invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared field</li>
72      *             <li>the caller's class loader is not the same as or an ancestor of the class loader for the current
73      *             class and invocation of s.checkPackageAccess() denies access to the package of this class</li>
74      *             </ul>
75      * @throws NoSuchFieldException
76      *             if a field with the specified name is not found.
77      * @throws IllegalAccessException
78      *             if this Field object is enforcing Java language access control and the underlying field is either
79      *             inaccessible or final.
80      * @throws IllegalArgumentException
81      *             if the specified object is not an instance of the class or interface declaring the underlying field
82      *             (or a subclass or implementor thereof), or if an unwrapping conversion fails.
83      */
84     @SuppressWarnings("nls")
85     public ExecutorHarness() throws NoSuchFieldException, SecurityException, IllegalArgumentException,
86                     IllegalAccessException {
87         methods = new HashMap<>();
88         new SvcLogicContext();
89
90         Class<?> contextClass = SvcLogicContext.class;
91         contextLogger = contextClass.getDeclaredField("LOG");
92         contextLogger.setAccessible(true);
93         logger = new InterceptLogger();
94         contextLogger.set(null, logger);
95     }
96
97     /**
98      * Convenience constructor
99      * 
100      * @param executor
101      *            The executor to be tested by the harness
102      * @throws SecurityException
103      *             If a security manager, s, is present and any of the following conditions is met:
104      *             <ul>
105      *             <li>invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared field</li>
106      *             <li>the caller's class loader is not the same as or an ancestor of the class loader for the current
107      *             class and invocation of s.checkPackageAccess() denies access to the package of this class</li>
108      *             </ul>
109      * @throws NoSuchFieldException
110      *             if a field with the specified name is not found.
111      * @throws IllegalAccessException
112      *             if this Field object is enforcing Java language access control and the underlying field is either
113      *             inaccessible or final.
114      * @throws IllegalArgumentException
115      *             if the specified object is not an instance of the class or interface declaring the underlying field
116      *             (or a subclass or implementor thereof), or if an unwrapping conversion fails.
117      */
118     public ExecutorHarness(SvcLogicJavaPlugin executor) throws NoSuchFieldException, SecurityException,
119                     IllegalArgumentException, IllegalAccessException {
120         this();
121         setExecutor(executor);
122     }
123
124     /**
125      * @param executor
126      *            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 from a DG execute node
151      * 
152      * @param methodName
153      *            The method name to be validated
154      * @return True if the method name meets the signature requirements, false if the method either does not exist or
155      *         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 signature of the
163      * 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 }