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=========================================================
23 package org.openecomp.appc.test;
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;
32 import org.openecomp.appc.test.InterceptLogger;
34 import org.openecomp.sdnc.sli.SvcLogicContext;
35 import org.openecomp.sdnc.sli.SvcLogicJavaPlugin;
38 * This class is used as a test harness to wrap the call to an executor node.
41 public class ExecutorHarness {
44 * The executor to be tested
46 private SvcLogicJavaPlugin executor;
49 * The collection of all exec methods found on the class
51 private Map<String, Method> methods;
54 * The field of the class being tested that contains the reference to the logger to be used. This is modified to
55 * point to our interception logger for the test.
57 private Field contextLogger;
60 * The interception logger that buffers all messages logged and allows us to look at them as part of the test case.
62 private InterceptLogger logger;
65 * Create the harness and initialize it
67 * @throws SecurityException
68 * If a security manager, s, is present and any of the following conditions is met:
70 * <li>invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared field</li>
71 * <li>the caller's class loader is not the same as or an ancestor of the class loader for the current
72 * class and invocation of s.checkPackageAccess() denies access to the package of this class</li>
74 * @throws NoSuchFieldException
75 * if a field with the specified name is not found.
76 * @throws IllegalAccessException
77 * if this Field object is enforcing Java language access control and the underlying field is either
78 * inaccessible or final.
79 * @throws IllegalArgumentException
80 * if the specified object is not an instance of the class or interface declaring the underlying field
81 * (or a subclass or implementor thereof), or if an unwrapping conversion fails.
83 @SuppressWarnings("nls")
84 public ExecutorHarness() throws NoSuchFieldException, SecurityException, IllegalArgumentException,
85 IllegalAccessException {
86 methods = new HashMap<>();
87 new SvcLogicContext();
89 Class<?> contextClass = SvcLogicContext.class;
90 contextLogger = contextClass.getDeclaredField("LOG");
91 contextLogger.setAccessible(true);
92 logger = new InterceptLogger();
93 contextLogger.set(null, logger);
97 * Convenience constructor
100 * The executor to be tested by the harness
101 * @throws SecurityException
102 * If a security manager, s, is present and any of the following conditions is met:
104 * <li>invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared field</li>
105 * <li>the caller's class loader is not the same as or an ancestor of the class loader for the current
106 * class and invocation of s.checkPackageAccess() denies access to the package of this class</li>
108 * @throws NoSuchFieldException
109 * if a field with the specified name is not found.
110 * @throws IllegalAccessException
111 * if this Field object is enforcing Java language access control and the underlying field is either
112 * inaccessible or final.
113 * @throws IllegalArgumentException
114 * if the specified object is not an instance of the class or interface declaring the underlying field
115 * (or a subclass or implementor thereof), or if an unwrapping conversion fails.
117 public ExecutorHarness(SvcLogicJavaPlugin executor) throws NoSuchFieldException, SecurityException,
118 IllegalArgumentException, IllegalAccessException {
120 setExecutor(executor);
125 * The java plugin class to be executed
127 public void setExecutor(SvcLogicJavaPlugin executor) {
128 this.executor = executor;
133 * @return The java plugin class to be executed
135 public SvcLogicJavaPlugin getExecutor() {
140 * @return The set of all methods that meet the signature requirements
142 public List<String> getExecMethodNames() {
143 List<String> names = new ArrayList<>();
144 names.addAll(methods.keySet());
149 * Returns an indication if the named method is a valid executor method that could be called from a DG execute node
152 * The method name to be validated
153 * @return True if the method name meets the signature requirements, false if the method either does not exist or
154 * does not meet the requirements.
156 public boolean isExecMethod(String methodName) {
157 return methods.containsKey(methodName);
161 * This method scans the executor class hierarchy to locate all methods that match the required signature of the
162 * executor and records these methods in a map.
164 private void scanExecutor() {
166 Class<?> executorClass = executor.getClass();
167 Method[] publicMethods = executorClass.getMethods();
168 for (Method method : publicMethods) {
169 if (method.getReturnType().equals(Void.class)) {
170 Class<?>[] paramTypes = method.getParameterTypes();
171 if (paramTypes.length == 2) {
172 if (Map.class.isAssignableFrom(paramTypes[0])
173 && SvcLogicContext.class.isAssignableFrom(paramTypes[1])) {
174 methods.put(method.getName(), method);