b6476d9dcb734db32e2cfe0c90a382a14fc0cae1
[ccsdk/sli.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : SLI
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  * ============LICENSE_END=========================================================
21  */
22
23
24 package org.onap.ccsdk.test;
25
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;
31 import java.util.Map;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
34
35 /**
36  * This class is used as a test harness to wrap the call to an executor node.
37  */
38
39 public class ExecutorHarness {
40
41     /**
42      * The executor to be tested
43      */
44     private SvcLogicJavaPlugin executor;
45
46     /**
47      * The collection of all exec methods found on the class
48      */
49     private final Map<String, Method> methods;
50
51     /**
52      * Create the harness and initialize it
53      *
54      * @throws SecurityException        If a security manager, s, is present and any of the following conditions is met:
55      *                                  <ul>
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>
59      *                                  </ul>
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.
65      */
66     @SuppressWarnings("nls")
67     public ExecutorHarness() throws NoSuchFieldException, SecurityException, IllegalArgumentException,
68             IllegalAccessException {
69         methods = new HashMap<>();
70         new SvcLogicContext();
71
72         Class<?> contextClass = SvcLogicContext.class;
73         /**
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.
76          */
77         Field contextLogger = contextClass.getDeclaredField("LOG");
78         contextLogger.setAccessible(true);
79         /**
80          * The interception logger that buffers all messages logged and allows us to look at them as part of the test case.
81          */
82         InterceptLogger logger = new InterceptLogger();
83         contextLogger.set(null, logger);
84     }
85
86     /**
87      * Convenience constructor
88      *
89      * @param executor The executor to be tested by the harness
90      *
91      * @throws SecurityException        If a security manager, s, is present and any of the following conditions is met:
92      *                                  <ul>
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>
96      *                                  </ul>
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.
102      */
103     public ExecutorHarness(SvcLogicJavaPlugin executor) throws NoSuchFieldException, SecurityException,
104             IllegalArgumentException, IllegalAccessException {
105         this();
106         setExecutor(executor);
107     }
108
109     /**
110      * @return The java plugin class to be executed
111      */
112     public SvcLogicJavaPlugin getExecutor() {
113         return executor;
114     }
115
116     /**
117      * @param executor The java plugin class to be executed
118      */
119     public void setExecutor(SvcLogicJavaPlugin executor) {
120         this.executor = executor;
121         scanExecutor();
122     }
123
124     /**
125      * @return The set of all methods that meet the signature requirements
126      */
127     public List<String> getExecMethodNames() {
128         List<String> names = new ArrayList<>();
129         names.addAll(methods.keySet());
130         return names;
131     }
132
133     /**
134      * Returns an indication if the named method is a valid executor method that could be called from a DG execute node
135      *
136      * @param methodName The method name to be validated
137      *
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.
140      */
141     public boolean isExecMethod(String methodName) {
142         return methods.containsKey(methodName);
143     }
144
145     /**
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.
148      */
149     private void scanExecutor() {
150         methods.clear();
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);
160                     }
161                 }
162             }
163         }
164     }
165
166 }