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