2d2ce7cde19a3605192c6614159135edfadce95b
[vid.git] / vid-automation / src / main / java / vid / automation / test / infra / SkipTestUntilTestngTransformer.java
1 package vid.automation.test.infra;
2
3 import java.lang.reflect.Constructor;
4 import java.lang.reflect.Method;
5 import java.time.LocalDate;
6 import org.testng.IAnnotationTransformer;
7 import org.testng.annotations.ITestAnnotation;
8
9 /*
10 TestNg listener that skip tests that are annotated with SkipTestUntil annotation
11 Pay attention that this listener shall be configured in the testng.xml (or command line)
12 */
13 public class SkipTestUntilTestngTransformer implements IAnnotationTransformer {
14
15     @Override
16     public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
17
18             if (testMethod!=null) {
19                 try {
20
21                     if (!annotation.getEnabled()) {
22                         return;
23                     }
24
25                     if (!testMethod.isAnnotationPresent(SkipTestUntil.class)) {
26                         return;
27                     }
28
29                     String dateAsStr = testMethod.getAnnotation(SkipTestUntil.class).value();
30                     if (shallDisableTest(dateAsStr)) {
31                         disableTest(annotation, testMethod.getDeclaringClass().getName(), dateAsStr);
32                     }
33
34                 } catch (Exception e) {
35                     e.printStackTrace();
36                 }
37             }
38     }
39
40     private boolean shallDisableTest(String dateAsStr) {
41         try {
42             return LocalDate.now().isBefore(LocalDate.parse(dateAsStr));
43         }
44         catch (RuntimeException exception) {
45             System.out.println("Failure during processing of SkipTestUntil annotation value is " + dateAsStr);
46             exception.printStackTrace();
47             return false;
48         }
49     }
50
51     private void disableTest(ITestAnnotation annotation, String name, String dateAsStr) {
52         System.out.println("Ignore "+ name+" till "+dateAsStr);
53         annotation.setEnabled(false);
54     }
55
56 }
57