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;
 
  37 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 
  38 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
 
  41  * This class is used as a test harness to wrap the call to an executor node.
 
  44 public class ExecutorHarness {
 
  47      * The executor to be tested
 
  49     private SvcLogicJavaPlugin executor;
 
  52      * The collection of all exec methods found on the class
 
  54     private Map<String, Method> methods;
 
  57      * The field of the class being tested that contains the reference to the logger to be used. This is modified to
 
  58      * point to our interception logger for the test.
 
  60     private Field contextLogger;
 
  63      * The interception logger that buffers all messages logged and allows us to look at them as part of the test case.
 
  65     private InterceptLogger logger;
 
  68      * Create the harness and initialize it
 
  70      * @throws SecurityException
 
  71      *             If a security manager, s, is present and any of the following conditions is met:
 
  73      *             <li>invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared field</li>
 
  74      *             <li>the caller's class loader is not the same as or an ancestor of the class loader for the current
 
  75      *             class and invocation of s.checkPackageAccess() denies access to the package of this class</li>
 
  77      * @throws NoSuchFieldException
 
  78      *             if a field with the specified name is not found.
 
  79      * @throws IllegalAccessException
 
  80      *             if this Field object is enforcing Java language access control and the underlying field is either
 
  81      *             inaccessible or final.
 
  82      * @throws IllegalArgumentException
 
  83      *             if the specified object is not an instance of the class or interface declaring the underlying field
 
  84      *             (or a subclass or implementor thereof), or if an unwrapping conversion fails.
 
  86     @SuppressWarnings("nls")
 
  87     public ExecutorHarness() throws NoSuchFieldException, SecurityException, IllegalArgumentException,
 
  88                     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
 
 103      *            The executor to be tested by the harness
 
 104      * @throws SecurityException
 
 105      *             If a security manager, s, is present and any of the following conditions is met:
 
 107      *             <li>invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared field</li>
 
 108      *             <li>the caller's class loader is not the same as or an ancestor of the class loader for the current
 
 109      *             class and invocation of s.checkPackageAccess() denies access to the package of this class</li>
 
 111      * @throws NoSuchFieldException
 
 112      *             if a field with the specified name is not found.
 
 113      * @throws IllegalAccessException
 
 114      *             if this Field object is enforcing Java language access control and the underlying field is either
 
 115      *             inaccessible or final.
 
 116      * @throws IllegalArgumentException
 
 117      *             if the specified object is not an instance of the class or interface declaring the underlying field
 
 118      *             (or a subclass or implementor thereof), or if an unwrapping conversion fails.
 
 120     public ExecutorHarness(SvcLogicJavaPlugin executor) throws NoSuchFieldException, SecurityException,
 
 121                     IllegalArgumentException, IllegalAccessException {
 
 123         setExecutor(executor);
 
 128      *            The java plugin class to be executed
 
 130     public void setExecutor(SvcLogicJavaPlugin executor) {
 
 131         this.executor = executor;
 
 136      * @return The java plugin class to be executed
 
 138     public SvcLogicJavaPlugin getExecutor() {
 
 143      * @return The set of all methods that meet the signature requirements
 
 145     public List<String> getExecMethodNames() {
 
 146         List<String> names = new ArrayList<>();
 
 147         names.addAll(methods.keySet());
 
 152      * Returns an indication if the named method is a valid executor method that could be called from a DG execute node
 
 155      *            The method name to be validated
 
 156      * @return True if the method name meets the signature requirements, false if the method either does not exist or
 
 157      *         does not meet the requirements.
 
 159     public boolean isExecMethod(String methodName) {
 
 160         return methods.containsKey(methodName);
 
 164      * This method scans the executor class hierarchy to locate all methods that match the required signature of the
 
 165      * executor and records these methods in a map.
 
 167     private void scanExecutor() {
 
 169         Class<?> executorClass = executor.getClass();
 
 170         Method[] publicMethods = executorClass.getMethods();
 
 171         for (Method method : publicMethods) {
 
 172             if (method.getReturnType().equals(Void.class)) {
 
 173                 Class<?>[] paramTypes = method.getParameterTypes();
 
 174                 if (paramTypes.length == 2) {
 
 175                     if (Map.class.isAssignableFrom(paramTypes[0])
 
 176                         && SvcLogicContext.class.isAssignableFrom(paramTypes[1])) {
 
 177                         methods.put(method.getName(), method);