2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
20 * ============LICENSE_END=========================================================
24 package org.onap.ccsdk.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.ccsdk.sli.core.sli.SvcLogicContext;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
36 * This class is used as a test harness to wrap the call to an executor node.
39 public class ExecutorHarness {
42 * The executor to be tested
44 private SvcLogicJavaPlugin executor;
47 * The collection of all exec methods found on the class
49 private final Map<String, Method> methods;
52 * Create the harness and initialize it
54 * @throws SecurityException If a security manager, s, is present and any of the following conditions is met:
56 * <li>invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared field</li>
57 * <li>the caller's class loader is not the same as or an ancestor of the class loader for the current
58 * class and invocation of s.checkPackageAccess() denies access to the package of this class</li>
60 * @throws NoSuchFieldException if a field with the specified name is not found.
61 * @throws IllegalAccessException if this Field object is enforcing Java language access control and the underlying field is either
62 * inaccessible or final.
63 * @throws IllegalArgumentException if the specified object is not an instance of the class or interface declaring the underlying field
64 * (or a subclass or implementor thereof), or if an unwrapping conversion fails.
66 @SuppressWarnings("nls")
67 public ExecutorHarness() throws NoSuchFieldException, SecurityException, IllegalArgumentException,
68 IllegalAccessException {
69 methods = new HashMap<>();
70 new SvcLogicContext();
72 Class<?> contextClass = SvcLogicContext.class;
74 * The field of the class being tested that contains the reference to the logger to be used. This is modified to
75 * point to our interception logger for the test.
77 Field contextLogger = contextClass.getDeclaredField("LOG");
78 contextLogger.setAccessible(true);
80 * The interception logger that buffers all messages logged and allows us to look at them as part of the test case.
82 InterceptLogger logger = new InterceptLogger();
83 contextLogger.set(null, logger);
87 * Convenience constructor
89 * @param executor The executor to be tested by the harness
91 * @throws SecurityException If a security manager, s, is present and any of the following conditions is met:
93 * <li>invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared field</li>
94 * <li>the caller's class loader is not the same as or an ancestor of the class loader for the current
95 * class and invocation of s.checkPackageAccess() denies access to the package of this class</li>
97 * @throws NoSuchFieldException if a field with the specified name is not found.
98 * @throws IllegalAccessException if this Field object is enforcing Java language access control and the underlying field is either
99 * inaccessible or final.
100 * @throws IllegalArgumentException if the specified object is not an instance of the class or interface declaring the underlying field
101 * (or a subclass or implementor thereof), or if an unwrapping conversion fails.
103 public ExecutorHarness(SvcLogicJavaPlugin executor) throws NoSuchFieldException, SecurityException,
104 IllegalArgumentException, IllegalAccessException {
106 setExecutor(executor);
110 * @return The java plugin class to be executed
112 public SvcLogicJavaPlugin getExecutor() {
117 * @param executor The java plugin class to be executed
119 public void setExecutor(SvcLogicJavaPlugin executor) {
120 this.executor = executor;
125 * @return The set of all methods that meet the signature requirements
127 public List<String> getExecMethodNames() {
128 List<String> names = new ArrayList<>();
129 names.addAll(methods.keySet());
134 * Returns an indication if the named method is a valid executor method that could be called from a DG execute node
136 * @param methodName The method name to be validated
138 * @return True if the method name meets the signature requirements, false if the method either does not exist or
139 * does not meet the requirements.
141 public boolean isExecMethod(String methodName) {
142 return methods.containsKey(methodName);
146 * This method scans the executor class hierarchy to locate all methods that match the required signature of the
147 * executor and records these methods in a map.
149 private void scanExecutor() {
151 Class<?> executorClass = executor.getClass();
152 Method[] publicMethods = executorClass.getMethods();
153 for (Method method : publicMethods) {
154 if (method.getReturnType().equals(Void.class)) {
155 Class<?>[] paramTypes = method.getParameterTypes();
156 if (paramTypes.length == 2) {
157 if (Map.class.isAssignableFrom(paramTypes[0])
158 && SvcLogicContext.class.isAssignableFrom(paramTypes[1])) {
159 methods.put(method.getName(), method);