2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 * ============LICENSE_END=========================================================
24 package org.onap.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;
32 import org.onap.appc.test.InterceptLogger;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
37 * This class is used as a test harness to wrap the call to an executor node.
40 public class ExecutorHarness {
43 * The executor to be tested
45 private SvcLogicJavaPlugin executor;
48 * The collection of all exec methods found on the class
50 private Map<String, Method> methods;
53 * The field of the class being tested that contains the reference to the logger to be used.
54 * This is modified to point to our interception logger for the test.
56 private Field contextLogger;
59 * The interception logger that buffers all messages logged and allows us to look at them as
60 * part of the test case.
62 private InterceptLogger logger;
65 * Create the harness and initialize it
67 * @throws SecurityException If a security manager, s, is present and any of the following
70 * <li>invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the
72 * <li>the caller's class loader is not the same as or an ancestor of the class loader
73 * for the current class and invocation of s.checkPackageAccess() denies access to the
74 * package of this class</li>
76 * @throws NoSuchFieldException if a field with the specified name is not found.
77 * @throws IllegalAccessException if this Field object is enforcing Java language access control
78 * and the underlying field is either inaccessible or final.
79 * @throws IllegalArgumentException if the specified object is not an instance of the class or
80 * interface declaring the underlying field (or a subclass or implementor thereof), or
81 * if an unwrapping conversion fails.
83 @SuppressWarnings("nls")
84 public ExecutorHarness()
85 throws NoSuchFieldException, SecurityException, IllegalArgumentException, 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
99 * @param executor The executor to be tested by the harness
100 * @throws SecurityException If a security manager, s, is present and any of the following
103 * <li>invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the
104 * declared field</li>
105 * <li>the caller's class loader is not the same as or an ancestor of the class loader
106 * for the current class and invocation of s.checkPackageAccess() denies access to the
107 * package of this class</li>
109 * @throws NoSuchFieldException if a field with the specified name is not found.
110 * @throws IllegalAccessException if this Field object is enforcing Java language access control
111 * and the underlying field is either inaccessible or final.
112 * @throws IllegalArgumentException if the specified object is not an instance of the class or
113 * interface declaring the underlying field (or a subclass or implementor thereof), or
114 * if an unwrapping conversion fails.
116 public ExecutorHarness(SvcLogicJavaPlugin executor)
117 throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
119 setExecutor(executor);
123 * @param executor The java plugin class to be executed
125 public void setExecutor(SvcLogicJavaPlugin executor) {
126 this.executor = executor;
131 * @return The java plugin class to be executed
133 public SvcLogicJavaPlugin getExecutor() {
138 * @return The set of all methods that meet the signature requirements
140 public List<String> getExecMethodNames() {
141 List<String> names = new ArrayList<>();
142 names.addAll(methods.keySet());
147 * Returns an indication if the named method is a valid executor method that could be called
148 * from a DG execute node
150 * @param methodName The method name to be validated
151 * @return True if the method name meets the signature requirements, false if the method either
152 * does not exist or does not meet the requirements.
154 public boolean isExecMethod(String methodName) {
155 return methods.containsKey(methodName);
159 * This method scans the executor class hierarchy to locate all methods that match the required
160 * signature of the executor and records these methods in a map.
162 private void scanExecutor() {
164 Class<?> executorClass = executor.getClass();
165 Method[] publicMethods = executorClass.getMethods();
166 for (Method method : publicMethods) {
167 if (method.getReturnType().equals(Void.class)) {
168 Class<?>[] paramTypes = method.getParameterTypes();
169 if (paramTypes.length == 2) {
170 if (Map.class.isAssignableFrom(paramTypes[0])
171 && SvcLogicContext.class.isAssignableFrom(paramTypes[1])) {
172 methods.put(method.getName(), method);