2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
 
   7  * ================================================================================
 
   8  * Licensed under the Apache License, Version 2.0 (the "License");
 
   9  * you may not use this file except in compliance with the License.
 
  10  * You may obtain a copy of the License at
 
  12  *      http://www.apache.org/licenses/LICENSE-2.0
 
  14  * Unless required by applicable law or agreed to in writing, software
 
  15  * distributed under the License is distributed on an "AS IS" BASIS,
 
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  17  * See the License for the specific language governing permissions and
 
  18  * limitations under the License.
 
  19  * ============LICENSE_END=========================================================
 
  23 package org.openecomp.appc.test;
 
  25 import java.lang.reflect.Field;
 
  26 import java.lang.reflect.Method;
 
  27 import java.util.ArrayList;
 
  28 import java.util.HashMap;
 
  29 import java.util.List;
 
  32 import org.openecomp.appc.test.InterceptLogger;
 
  33 import org.openecomp.sdnc.sli.SvcLogicContext;
 
  34 import org.openecomp.sdnc.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. This is modified to
 
  54      * 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 part of the test case.
 
  61     private InterceptLogger logger;
 
  64      * Create the harness and initialize it
 
  66      * @throws SecurityException
 
  67      *             If a security manager, s, is present and any of the following conditions is met:
 
  69      *             <li>invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared field</li>
 
  70      *             <li>the caller's class loader is not the same as or an ancestor of the class loader for the current
 
  71      *             class and invocation of s.checkPackageAccess() denies access to the package of this class</li>
 
  73      * @throws NoSuchFieldException
 
  74      *             if a field with the specified name is not found.
 
  75      * @throws IllegalAccessException
 
  76      *             if this Field object is enforcing Java language access control and the underlying field is either
 
  77      *             inaccessible or final.
 
  78      * @throws IllegalArgumentException
 
  79      *             if the specified object is not an instance of the class or interface declaring the underlying field
 
  80      *             (or a subclass or implementor thereof), or if an unwrapping conversion fails.
 
  82     @SuppressWarnings("nls")
 
  83     public ExecutorHarness() throws NoSuchFieldException, SecurityException, IllegalArgumentException,
 
  84                     IllegalAccessException {
 
  85         methods = new HashMap<>();
 
  86         new SvcLogicContext();
 
  88         Class<?> contextClass = SvcLogicContext.class;
 
  89         contextLogger = contextClass.getDeclaredField("LOG");
 
  90         contextLogger.setAccessible(true);
 
  91         logger = new InterceptLogger();
 
  92         contextLogger.set(null, logger);
 
  96      * Convenience constructor
 
  99      *            The executor to be tested by the harness
 
 100      * @throws SecurityException
 
 101      *             If a security manager, s, is present and any of the following conditions is met:
 
 103      *             <li>invocation of s.checkMemberAccess(this, Member.DECLARED) denies access to the declared field</li>
 
 104      *             <li>the caller's class loader is not the same as or an ancestor of the class loader for the current
 
 105      *             class and invocation of s.checkPackageAccess() denies access to the package of this class</li>
 
 107      * @throws NoSuchFieldException
 
 108      *             if a field with the specified name is not found.
 
 109      * @throws IllegalAccessException
 
 110      *             if this Field object is enforcing Java language access control and the underlying field is either
 
 111      *             inaccessible or final.
 
 112      * @throws IllegalArgumentException
 
 113      *             if the specified object is not an instance of the class or interface declaring the underlying field
 
 114      *             (or a subclass or implementor thereof), or if an unwrapping conversion fails.
 
 116     public ExecutorHarness(SvcLogicJavaPlugin executor) throws NoSuchFieldException, SecurityException,
 
 117                     IllegalArgumentException, IllegalAccessException {
 
 119         setExecutor(executor);
 
 124      *            The java plugin class to be executed
 
 126     public void setExecutor(SvcLogicJavaPlugin executor) {
 
 127         this.executor = executor;
 
 132      * @return The java plugin class to be executed
 
 134     public SvcLogicJavaPlugin getExecutor() {
 
 139      * @return The set of all methods that meet the signature requirements
 
 141     public List<String> getExecMethodNames() {
 
 142         List<String> names = new ArrayList<>();
 
 143         names.addAll(methods.keySet());
 
 148      * Returns an indication if the named method is a valid executor method that could be called from a DG execute node
 
 151      *            The method name to be validated
 
 152      * @return True if the method name meets the signature requirements, false if the method either does not exist or
 
 153      *         does not meet the requirements.
 
 155     public boolean isExecMethod(String methodName) {
 
 156         return methods.containsKey(methodName);
 
 160      * This method scans the executor class hierarchy to locate all methods that match the required signature of the
 
 161      * executor and records these methods in a map.
 
 163     private void scanExecutor() {
 
 165         Class<?> executorClass = executor.getClass();
 
 166         Method[] publicMethods = executorClass.getMethods();
 
 167         for (Method method : publicMethods) {
 
 168             if (method.getReturnType().equals(Void.class)) {
 
 169                 Class<?>[] paramTypes = method.getParameterTypes();
 
 170                 if (paramTypes.length == 2) {
 
 171                     if (Map.class.isAssignableFrom(paramTypes[0])
 
 172                         && SvcLogicContext.class.isAssignableFrom(paramTypes[1])) {
 
 173                         methods.put(method.getName(), method);