2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
27 package org.openecomp.appc.test;
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;
36 import org.openecomp.appc.test.InterceptLogger;
38 import org.openecomp.sdnc.sli.SvcLogicContext;
39 import org.openecomp.sdnc.sli.SvcLogicJavaPlugin;
42 * This class is used as a test harness to wrap the call to an executor node.
45 public class ExecutorHarness {
48 * The executor to be tested
50 private SvcLogicJavaPlugin executor;
53 * The collection of all exec methods found on the class
55 private Map<String, Method> methods;
58 * The field of the class being tested that contains the reference to the logger to be used. This is modified to
59 * point to our interception logger for the test.
61 private Field contextLogger;
64 * The interception logger that buffers all messages logged and allows us to look at them as part of the test case.
66 private InterceptLogger logger;
69 * Create the harness and initialize it
71 * @throws SecurityException
72 * If a security manager, s, is present and any of the following conditions is met:
74 * <li>invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared field</li>
75 * <li>the caller's class loader is not the same as or an ancestor of the class loader for the current
76 * class and invocation of s.checkPackageAccess() denies access to the package of this class</li>
78 * @throws NoSuchFieldException
79 * if a field with the specified name is not found.
80 * @throws IllegalAccessException
81 * if this Field object is enforcing Java language access control and the underlying field is either
82 * inaccessible or final.
83 * @throws IllegalArgumentException
84 * if the specified object is not an instance of the class or interface declaring the underlying field
85 * (or a subclass or implementor thereof), or if an unwrapping conversion fails.
87 @SuppressWarnings("nls")
88 public ExecutorHarness() throws NoSuchFieldException, SecurityException, IllegalArgumentException,
89 IllegalAccessException {
90 methods = new HashMap<>();
91 new SvcLogicContext();
93 Class<?> contextClass = SvcLogicContext.class;
94 contextLogger = contextClass.getDeclaredField("LOG");
95 contextLogger.setAccessible(true);
96 logger = new InterceptLogger();
97 contextLogger.set(null, logger);
101 * Convenience constructor
104 * The executor to be tested by the harness
105 * @throws SecurityException
106 * If a security manager, s, is present and any of the following conditions is met:
108 * <li>invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared field</li>
109 * <li>the caller's class loader is not the same as or an ancestor of the class loader for the current
110 * class and invocation of s.checkPackageAccess() denies access to the package of this class</li>
112 * @throws NoSuchFieldException
113 * if a field with the specified name is not found.
114 * @throws IllegalAccessException
115 * if this Field object is enforcing Java language access control and the underlying field is either
116 * inaccessible or final.
117 * @throws IllegalArgumentException
118 * if the specified object is not an instance of the class or interface declaring the underlying field
119 * (or a subclass or implementor thereof), or if an unwrapping conversion fails.
121 public ExecutorHarness(SvcLogicJavaPlugin executor) throws NoSuchFieldException, SecurityException,
122 IllegalArgumentException, IllegalAccessException {
124 setExecutor(executor);
129 * The java plugin class to be executed
131 public void setExecutor(SvcLogicJavaPlugin executor) {
132 this.executor = executor;
137 * @return The java plugin class to be executed
139 public SvcLogicJavaPlugin getExecutor() {
144 * @return The set of all methods that meet the signature requirements
146 public List<String> getExecMethodNames() {
147 List<String> names = new ArrayList<>();
148 names.addAll(methods.keySet());
153 * Returns an indication if the named method is a valid executor method that could be called from a DG execute node
156 * The method name to be validated
157 * @return True if the method name meets the signature requirements, false if the method either does not exist or
158 * does not meet the requirements.
160 public boolean isExecMethod(String methodName) {
161 return methods.containsKey(methodName);
165 * This method scans the executor class hierarchy to locate all methods that match the required signature of the
166 * executor and records these methods in a map.
168 private void scanExecutor() {
170 Class<?> executorClass = executor.getClass();
171 Method[] publicMethods = executorClass.getMethods();
172 for (Method method : publicMethods) {
173 if (method.getReturnType().equals(Void.class)) {
174 Class<?>[] paramTypes = method.getParameterTypes();
175 if (paramTypes.length == 2) {
176 if (Map.class.isAssignableFrom(paramTypes[0])
177 && SvcLogicContext.class.isAssignableFrom(paramTypes[1])) {
178 methods.put(method.getName(), method);