Upgrade Java 17 in policy-drools-apps
[policy/drools-applications.git] / controlloop / common / rules-test / src / main / java / org / onap / policy / controlloop / common / rules / test / NamedRunner.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2023 Nordix Foundation.
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.controlloop.common.rules.test;
23
24 import org.junit.jupiter.api.extension.ConditionEvaluationResult;
25 import org.junit.jupiter.api.extension.ExecutionCondition;
26 import org.junit.jupiter.api.extension.ExtensionContext;
27
28 /**
29  * Runs tests listed via the {@link TestNames} annotation.
30  */
31 public class NamedRunner implements ExecutionCondition {
32
33     @Override
34     public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext extensionContext) {
35         var testClass = extensionContext.getTestClass();
36         if (testClass.isEmpty()) {
37             return ConditionEvaluationResult.enabled("Empty");
38         }
39         var method = extensionContext.getDisplayName().replace("()", "");
40         if (testClass.get().getSimpleName().equals(method)) {
41             return ConditionEvaluationResult.enabled("Class");
42         }
43         if (isNamed(testClass.get(), method)) {
44             return ConditionEvaluationResult.enabled("OK");
45         } else {
46             return ConditionEvaluationResult.disabled("Disabled");
47         }
48     }
49
50     /**
51      * Determines if the test is in the list of tests to be included.
52      *
53      * @param testClass class under test
54      * @param testName name of the test of interest
55      * @return {@code true} if the test is in the list, {@code false} otherwise
56      */
57     private boolean isNamed(Class<?> testClass, String testName) {
58         var annot = testClass.getAnnotation(TestNames.class);
59         if (annot == null) {
60             // no annotation - everything passes
61             return true;
62         }
63
64         for (String name : annot.names()) {
65             if (testName.equals(name)) {
66                 return true;
67             }
68         }
69
70         for (String prefix : annot.prefixes()) {
71             if (testName.startsWith(prefix)) {
72                 return true;
73             }
74         }
75
76         return false;
77     }
78 }