merge 2 TestNg transformers
[vid.git] / vid-automation / src / main / java / vid / automation / test / infra / SkipTestsTestngTransformer.java
1 package vid.automation.test.infra;
2
3 import java.time.LocalDate;
4 import org.testng.IAnnotationTransformer;
5 import org.testng.annotations.ITestAnnotation;
6 import org.togglz.core.context.StaticFeatureManagerProvider;
7
8 import java.lang.reflect.AnnotatedElement;
9 import java.lang.reflect.Constructor;
10 import java.lang.reflect.Method;
11
12 /*
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
16
17 FeatureTogglingTest:
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.
22
23 SkipTestUntil:
24 If test annotated with SkipTestUntil the transformer check if the due date has already pass
25
26 */
27 public class SkipTestsTestngTransformer implements IAnnotationTransformer {
28
29     @Override
30     public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
31
32             if (testMethod!=null) {
33                 try {
34
35                     if (!annotation.getEnabled()) {
36                         return;
37                     }
38
39                     if (isIgnoreFeatureToggledTest(testMethod)) {
40                         disableTest(annotation, testMethod.getName());
41                         return;
42                     }
43
44                     if (isIgnoreFeatureToggledTest(testMethod.getDeclaringClass())) {
45                         disableTest(annotation, testMethod.getDeclaringClass().getName());
46                         return;
47                     }
48
49                     if (isIgnoreSkipUntilTest(testMethod)) {
50                         disableTest(annotation, testMethod.getName());
51                         return;
52                     }
53
54                 } catch (Exception e) {
55                     e.printStackTrace();
56                 }
57             }
58     }
59
60     private boolean isIgnoreFeatureToggledTest(AnnotatedElement annotatedElement) {
61
62         return (annotatedElement.isAnnotationPresent(FeatureTogglingTest.class) &&
63             shallDisableTest(annotatedElement.getAnnotation(FeatureTogglingTest.class)));
64
65     }
66
67     private boolean shallDisableTest(FeatureTogglingTest featureTogglingTest) {
68         if (featureTogglingTest.value().length==0) {
69             return false;
70         }
71         if (new StaticFeatureManagerProvider().getFeatureManager()==null) {
72             FeaturesTogglingConfiguration.initializeFeatureManager();
73         }
74         for (Features feature : featureTogglingTest.value()) {
75             if (!(feature.isActive()==featureTogglingTest.flagActive())) {
76                 return true;
77             }
78         }
79         return false;
80     }
81
82     private void disableTest(ITestAnnotation annotation, String name) {
83         System.out.println("Ignore "+ name+" due to annotation");
84         annotation.setEnabled(false);
85     }
86
87     private boolean isIgnoreSkipUntilTest(AnnotatedElement annotatedElement) {
88         if (!annotatedElement.isAnnotationPresent(SkipTestUntil.class)) {
89             return false;
90         }
91
92         String dateAsStr = annotatedElement.getAnnotation(SkipTestUntil.class).value();
93         try {
94             return LocalDate.now().isBefore(LocalDate.parse(dateAsStr));
95         }
96         catch (RuntimeException exception) {
97             System.out.println("Failure during processing of SkipTestUntil annotation value is " + dateAsStr);
98             exception.printStackTrace();
99             return false;
100         }
101     }
102 }
103