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.onap.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;
35 import org.onap.appc.test.InterceptLogger;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
40 * This class is used as a test harness to wrap the call to an executor node.
43 public class ExecutorHarness {
46 * The executor to be tested
48 private SvcLogicJavaPlugin executor;
51 * The collection of all exec methods found on the class
53 private Map<String, Method> methods;
56 * The field of the class being tested that contains the reference to the logger to be used.
57 * This is modified to point to our interception logger for the test.
59 private Field contextLogger;
62 * The interception logger that buffers all messages logged and allows us to look at them as
63 * part of the test case.
65 private InterceptLogger logger;
68 * Create the harness and initialize it
70 * @throws SecurityException If a security manager, s, is present and any of the following
73 * <li>invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the
75 * <li>the caller's class loader is not the same as or an ancestor of the class loader
76 * for the current class and invocation of s.checkPackageAccess() denies access to the
77 * package of this class</li>
79 * @throws NoSuchFieldException if a field with the specified name is not found.
80 * @throws IllegalAccessException if this Field object is enforcing Java language access control
81 * and the underlying field is either inaccessible or final.
82 * @throws IllegalArgumentException if the specified object is not an instance of the class or
83 * interface declaring the underlying field (or a subclass or implementor thereof), or
84 * if an unwrapping conversion fails.
86 @SuppressWarnings("nls")
87 public ExecutorHarness()
88 throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
89 methods = new HashMap<>();
90 new SvcLogicContext();
92 Class<?> contextClass = SvcLogicContext.class;
93 contextLogger = contextClass.getDeclaredField("LOG");
94 contextLogger.setAccessible(true);
95 logger = new InterceptLogger();
96 contextLogger.set(null, logger);
100 * Convenience constructor
102 * @param executor The executor to be tested by the harness
103 * @throws SecurityException If a security manager, s, is present and any of the following
106 * <li>invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the
107 * declared field</li>
108 * <li>the caller's class loader is not the same as or an ancestor of the class loader
109 * for the current class and invocation of s.checkPackageAccess() denies access to the
110 * package of this class</li>
112 * @throws NoSuchFieldException if a field with the specified name is not found.
113 * @throws IllegalAccessException if this Field object is enforcing Java language access control
114 * and the underlying field is either inaccessible or final.
115 * @throws IllegalArgumentException if the specified object is not an instance of the class or
116 * interface declaring the underlying field (or a subclass or implementor thereof), or
117 * if an unwrapping conversion fails.
119 public ExecutorHarness(SvcLogicJavaPlugin executor)
120 throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
122 setExecutor(executor);
126 * @param executor 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
151 * from a DG execute node
153 * @param methodName The method name to be validated
154 * @return True if the method name meets the signature requirements, false if the method either
155 * does not exist or 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
163 * signature of the 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);