2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
24 package org.openecomp.appc.test;
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;
33 import org.openecomp.appc.test.InterceptLogger;
35 import org.openecomp.sdnc.sli.SvcLogicContext;
36 import org.openecomp.sdnc.sli.SvcLogicJavaPlugin;
39 * This class is used as a test harness to wrap the call to an executor node.
42 public class ExecutorHarness {
45 * The executor to be tested
47 private SvcLogicJavaPlugin executor;
50 * The collection of all exec methods found on the class
52 private Map<String, Method> methods;
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.
58 private Field contextLogger;
61 * The interception logger that buffers all messages logged and allows us to look at them as part of the test case.
63 private InterceptLogger logger;
66 * Create the harness and initialize it
68 * @throws SecurityException
69 * If a security manager, s, is present and any of the following conditions is met:
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>
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.
84 @SuppressWarnings("nls")
85 public ExecutorHarness() throws NoSuchFieldException, SecurityException, IllegalArgumentException,
86 IllegalAccessException {
87 methods = new HashMap<>();
88 new SvcLogicContext();
90 Class<?> contextClass = SvcLogicContext.class;
91 contextLogger = contextClass.getDeclaredField("LOG");
92 contextLogger.setAccessible(true);
93 logger = new InterceptLogger();
94 contextLogger.set(null, logger);
98 * Convenience constructor
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:
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>
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.
118 public ExecutorHarness(SvcLogicJavaPlugin executor) throws NoSuchFieldException, SecurityException,
119 IllegalArgumentException, IllegalAccessException {
121 setExecutor(executor);
126 * The java plugin class to be executed
128 public void setExecutor(SvcLogicJavaPlugin executor) {
129 this.executor = executor;
134 * @return The java plugin class to be executed
136 public SvcLogicJavaPlugin getExecutor() {
141 * @return The set of all methods that meet the signature requirements
143 public List<String> getExecMethodNames() {
144 List<String> names = new ArrayList<>();
145 names.addAll(methods.keySet());
150 * Returns an indication if the named method is a valid executor method that could be called from a DG execute node
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.
157 public boolean isExecMethod(String methodName) {
158 return methods.containsKey(methodName);
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.
165 private void scanExecutor() {
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);