2 * ============LICENSE_START=======================================================
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
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=========================================================
22 package org.onap.policy.controlloop.common.rules.test;
24 import org.junit.jupiter.api.extension.ConditionEvaluationResult;
25 import org.junit.jupiter.api.extension.ExecutionCondition;
26 import org.junit.jupiter.api.extension.ExtensionContext;
29 * Runs tests listed via the {@link TestNames} annotation.
31 public class NamedRunner implements ExecutionCondition {
34 public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext extensionContext) {
35 var testClass = extensionContext.getTestClass();
36 if (testClass.isEmpty()) {
37 return ConditionEvaluationResult.enabled("Empty");
39 var method = extensionContext.getDisplayName().replace("()", "");
40 if (testClass.get().getSimpleName().equals(method)) {
41 return ConditionEvaluationResult.enabled("Class");
43 if (isNamed(testClass.get(), method)) {
44 return ConditionEvaluationResult.enabled("OK");
46 return ConditionEvaluationResult.disabled("Disabled");
51 * Determines if the test is in the list of tests to be included.
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
57 private boolean isNamed(Class<?> testClass, String testName) {
58 var annot = testClass.getAnnotation(TestNames.class);
60 // no annotation - everything passes
64 for (String name : annot.names()) {
65 if (testName.equals(name)) {
70 for (String prefix : annot.prefixes()) {
71 if (testName.startsWith(prefix)) {