5642f35fd20d5f3fbc86dd0c60b81ddd7bdf39b1
[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  * ================================================================================
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  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.common.rules.test;
22
23 import org.junit.Ignore;
24 import org.junit.runner.notification.RunNotifier;
25 import org.junit.runners.BlockJUnit4ClassRunner;
26 import org.junit.runners.model.FrameworkMethod;
27 import org.junit.runners.model.InitializationError;
28
29 /**
30  * Runs tests listed via the {@link TestNames} annotation.
31  */
32 public class NamedRunner extends BlockJUnit4ClassRunner {
33
34     /**
35      * Constructs the object.
36      */
37     public NamedRunner(Class<?> testClass) throws InitializationError {
38         super(testClass);
39     }
40
41     @Override
42     protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
43         var description = describeChild(method);
44
45         if (method.getAnnotation(Ignore.class) != null) {
46             notifier.fireTestIgnored(description);
47
48         } else if (!isNamed(description.getTestClass(), method.getName())) {
49             notifier.fireTestIgnored(description);
50
51         } else {
52             runLeaf(methodBlock(method), description, notifier);
53         }
54     }
55
56     /**
57      * Determines if the test is in the list of tests to be included.
58      *
59      * @param testClass class under test
60      * @param testName name of the test of interest
61      * @return {@code true} if the test is in the list, {@code false} otherwise
62      */
63     private boolean isNamed(Class<?> testClass, String testName) {
64         TestNames annot = testClass.getAnnotation(TestNames.class);
65         if (annot == null) {
66             // no annotation - everything passes
67             return true;
68         }
69
70         for (String name : annot.names()) {
71             if (testName.equals(name)) {
72                 return true;
73             }
74         }
75
76         for (String prefix : annot.prefixes()) {
77             if (testName.startsWith(prefix)) {
78                 return true;
79             }
80         }
81
82         return false;
83     }
84 }