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=========================================================
26 package org.onap.appc.test;
28 import java.lang.reflect.Field;
29 import java.lang.reflect.Method;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 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. This is modified to
57 * 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 part of the test case.
64 private InterceptLogger logger;
67 * Create the harness and initialize it
69 * @throws SecurityException
70 * If a security manager, s, is present and any of the following conditions is met:
72 * <li>invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared field</li>
73 * <li>the caller's class loader is not the same as or an ancestor of the class loader for the current
74 * class and invocation of s.checkPackageAccess() denies access to the package of this class</li>
76 * @throws NoSuchFieldException
77 * if a field with the specified name is not found.
78 * @throws IllegalAccessException
79 * if this Field object is enforcing Java language access control and the underlying field is either
80 * inaccessible or final.
81 * @throws IllegalArgumentException
82 * if the specified object is not an instance of the class or interface declaring the underlying field
83 * (or a subclass or implementor thereof), or if an unwrapping conversion fails.
85 @SuppressWarnings("nls")
86 public ExecutorHarness() throws NoSuchFieldException, SecurityException, IllegalArgumentException,
87 IllegalAccessException {
88 methods = new HashMap<>();
89 new SvcLogicContext();
91 Class<?> contextClass = SvcLogicContext.class;
92 contextLogger = contextClass.getDeclaredField("LOG");
93 contextLogger.setAccessible(true);
94 logger = new InterceptLogger();
95 contextLogger.set(null, logger);
99 * Convenience constructor
102 * The executor to be tested by the harness
103 * @throws SecurityException
104 * If a security manager, s, is present and any of the following conditions is met:
106 * <li>invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared field</li>
107 * <li>the caller's class loader is not the same as or an ancestor of the class loader for the current
108 * class and invocation of s.checkPackageAccess() denies access to the package of this class</li>
110 * @throws NoSuchFieldException
111 * if a field with the specified name is not found.
112 * @throws IllegalAccessException
113 * if this Field object is enforcing Java language access control and the underlying field is either
114 * inaccessible or final.
115 * @throws IllegalArgumentException
116 * if the specified object is not an instance of the class or interface declaring the underlying field
117 * (or a subclass or implementor thereof), or if an unwrapping conversion fails.
119 public ExecutorHarness(SvcLogicJavaPlugin executor) throws NoSuchFieldException, SecurityException,
120 IllegalArgumentException, IllegalAccessException {
122 setExecutor(executor);
127 * The java plugin class to be executed
129 public void setExecutor(SvcLogicJavaPlugin executor) {
130 this.executor = executor;
135 * @return The java plugin class to be executed
137 public SvcLogicJavaPlugin getExecutor() {
142 * @return The set of all methods that meet the signature requirements
144 public List<String> getExecMethodNames() {
145 List<String> names = new ArrayList<>();
146 names.addAll(methods.keySet());
151 * Returns an indication if the named method is a valid executor method that could be called from a DG execute node
154 * The method name to be validated
155 * @return True if the method name meets the signature requirements, false if the method either does not exist or
156 * does not meet the requirements.
158 public boolean isExecMethod(String methodName) {
159 return methods.containsKey(methodName);
163 * This method scans the executor class hierarchy to locate all methods that match the required signature of the
164 * executor and records these methods in a map.
166 private void scanExecutor() {
168 Class<?> executorClass = executor.getClass();
169 Method[] publicMethods = executorClass.getMethods();
170 for (Method method : publicMethods) {
171 if (method.getReturnType().equals(Void.class)) {
172 Class<?>[] paramTypes = method.getParameterTypes();
173 if (paramTypes.length == 2) {
174 if (Map.class.isAssignableFrom(paramTypes[0])
175 && SvcLogicContext.class.isAssignableFrom(paramTypes[1])) {
176 methods.put(method.getName(), method);