1 package vid.automation.test.infra;
3 import java.time.LocalDate;
4 import org.testng.IAnnotationTransformer;
5 import org.testng.annotations.ITestAnnotation;
6 import org.togglz.core.context.StaticFeatureManagerProvider;
8 import java.lang.reflect.AnnotatedElement;
9 import java.lang.reflect.Constructor;
10 import java.lang.reflect.Method;
13 This transformer skip test we want to ignore during running VID tests.
14 Pay attention that this listener shall be configured in the testng.xml (or command line)
15 It can't be used as Listener annotation of base class
18 There are 2 ways to annotate that tests required featureFlags to be active :
19 In method level - with @FeatureTogglingTest on the test method and list of Required Feature flags on
20 In Class level - with @FeatureTogglingTest on the test class and list of Required Feature flags on
21 For each test annotation of method level, we check if the test shall whole class shall run regards the features flag test.
24 If test annotated with SkipTestUntil the transformer check if the due date has already pass
27 public class SkipTestsTestngTransformer implements IAnnotationTransformer {
30 public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
32 if (testMethod!=null) {
35 if (!annotation.getEnabled()) {
39 if (isIgnoreFeatureToggledTest(testMethod)) {
40 disableTest(annotation, testMethod.getName());
44 if (isIgnoreFeatureToggledTest(testMethod.getDeclaringClass())) {
45 disableTest(annotation, testMethod.getDeclaringClass().getName());
49 if (isIgnoreSkipUntilTest(testMethod)) {
50 disableTest(annotation, testMethod.getName());
54 } catch (Exception e) {
60 private boolean isIgnoreFeatureToggledTest(AnnotatedElement annotatedElement) {
62 return (annotatedElement.isAnnotationPresent(FeatureTogglingTest.class) &&
63 shallDisableTest(annotatedElement.getAnnotation(FeatureTogglingTest.class)));
67 private boolean shallDisableTest(FeatureTogglingTest featureTogglingTest) {
68 if (featureTogglingTest.value().length==0) {
71 if (new StaticFeatureManagerProvider().getFeatureManager()==null) {
72 FeaturesTogglingConfiguration.initializeFeatureManager();
74 for (Features feature : featureTogglingTest.value()) {
75 if (!(feature.isActive()==featureTogglingTest.flagActive())) {
82 private void disableTest(ITestAnnotation annotation, String name) {
83 System.out.println("Ignore "+ name+" due to annotation");
84 annotation.setEnabled(false);
87 private boolean isIgnoreSkipUntilTest(AnnotatedElement annotatedElement) {
88 if (!annotatedElement.isAnnotationPresent(SkipTestUntil.class)) {
92 String dateAsStr = annotatedElement.getAnnotation(SkipTestUntil.class).value();
94 return LocalDate.now().isBefore(LocalDate.parse(dateAsStr));
96 catch (RuntimeException exception) {
97 System.out.println("Failure during processing of SkipTestUntil annotation value is " + dateAsStr);
98 exception.printStackTrace();