From f076ec79b9b5d4c533b30c227d1387c94b2beb3b Mon Sep 17 00:00:00 2001 From: Eylon Malin Date: Thu, 15 Aug 2019 16:42:22 +0300 Subject: merge 2 TestNg transformers Testng probably don't behave well with 2 transformers that change same thing. So I merge 2 transformers that skip tests to one transformer. Issue-ID: VID-378 Signed-off-by: Eylon Malin Change-Id: I569d421b853ed1e0dc2a5055dbaf8e5bba6db470 Signed-off-by: Eylon Malin --- vid-automation/TestNg-ApiTest.xml | 3 +- vid-automation/TestNg-UI-half.xml | 3 +- vid-automation/TestNg-dev.xml | 3 +- .../infra/FeatureTogglingTestngTransformer.java | 76 --------------- .../test/infra/SkipTestUntilTestngTransformer.java | 57 ------------ .../test/infra/SkipTestsTestngTransformer.java | 103 +++++++++++++++++++++ 6 files changed, 106 insertions(+), 139 deletions(-) delete mode 100644 vid-automation/src/main/java/vid/automation/test/infra/FeatureTogglingTestngTransformer.java delete mode 100644 vid-automation/src/main/java/vid/automation/test/infra/SkipTestUntilTestngTransformer.java create mode 100644 vid-automation/src/main/java/vid/automation/test/infra/SkipTestsTestngTransformer.java (limited to 'vid-automation') diff --git a/vid-automation/TestNg-ApiTest.xml b/vid-automation/TestNg-ApiTest.xml index 9e9380faa..641f3eba6 100644 --- a/vid-automation/TestNg-ApiTest.xml +++ b/vid-automation/TestNg-ApiTest.xml @@ -3,8 +3,7 @@ - - + diff --git a/vid-automation/TestNg-UI-half.xml b/vid-automation/TestNg-UI-half.xml index b19073869..27abcd76c 100644 --- a/vid-automation/TestNg-UI-half.xml +++ b/vid-automation/TestNg-UI-half.xml @@ -2,8 +2,7 @@ - - + diff --git a/vid-automation/TestNg-dev.xml b/vid-automation/TestNg-dev.xml index 3dc9d749d..d50785eb5 100644 --- a/vid-automation/TestNg-dev.xml +++ b/vid-automation/TestNg-dev.xml @@ -4,8 +4,7 @@ - - + diff --git a/vid-automation/src/main/java/vid/automation/test/infra/FeatureTogglingTestngTransformer.java b/vid-automation/src/main/java/vid/automation/test/infra/FeatureTogglingTestngTransformer.java deleted file mode 100644 index 46794da10..000000000 --- a/vid-automation/src/main/java/vid/automation/test/infra/FeatureTogglingTestngTransformer.java +++ /dev/null @@ -1,76 +0,0 @@ -package vid.automation.test.infra; - -import org.testng.IAnnotationTransformer; -import org.testng.annotations.ITestAnnotation; -import org.togglz.core.context.StaticFeatureManagerProvider; - -import java.lang.reflect.AnnotatedElement; -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; - -/* -In order to skip test classes regards the state of feature flag we add this listener to our testng configuration -There are 2 ways to annotate that tests required featureFlags to be active : -In method level - with @FeatureTogglingTest on the test method and list of Required Feature flags on -In Class level - with @FeatureTogglingTest on the test class and list of Required Feature flags on -For each test annotation of method level, we check if the test shall whole class shall run regards the features flag test. -Pay attention that this listener shall be configured in the testng.xml (or command line) -It can't be used as Listener annotation of base class -*/ -public class FeatureTogglingTestngTransformer implements IAnnotationTransformer { - - @Override - public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) { - - if (testMethod!=null) { - try { - - if (!annotation.getEnabled()) { - return; - } - - if (isIgnoreTest(testMethod)) { - disableTest(annotation, testMethod.getDeclaringClass().getName()); - return; - } - - if (isIgnoreTest(testMethod.getDeclaringClass())) { - disableTest(annotation, testMethod.getDeclaringClass().getName()); - return; - } - - } catch (Exception e) { - e.printStackTrace(); - } - } - } - - private boolean isIgnoreTest(AnnotatedElement annotatedElement) { - - return (annotatedElement.isAnnotationPresent(FeatureTogglingTest.class) && - shallDisableTest(annotatedElement.getAnnotation(FeatureTogglingTest.class))); - - } - - private boolean shallDisableTest(FeatureTogglingTest featureTogglingTest) { - if (featureTogglingTest.value().length==0) { - return false; - } - if (new StaticFeatureManagerProvider().getFeatureManager()==null) { - FeaturesTogglingConfiguration.initializeFeatureManager(); - } - for (Features feature : featureTogglingTest.value()) { - if (!(feature.isActive()==featureTogglingTest.flagActive())) { - return true; - } - } - return false; - } - - private void disableTest(ITestAnnotation annotation, String name) { - System.out.println("Ignore "+ name+" due to feature flags configuration"); - annotation.setEnabled(false); - } - -} - diff --git a/vid-automation/src/main/java/vid/automation/test/infra/SkipTestUntilTestngTransformer.java b/vid-automation/src/main/java/vid/automation/test/infra/SkipTestUntilTestngTransformer.java deleted file mode 100644 index 2d2ce7cde..000000000 --- a/vid-automation/src/main/java/vid/automation/test/infra/SkipTestUntilTestngTransformer.java +++ /dev/null @@ -1,57 +0,0 @@ -package vid.automation.test.infra; - -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; -import java.time.LocalDate; -import org.testng.IAnnotationTransformer; -import org.testng.annotations.ITestAnnotation; - -/* -TestNg listener that skip tests that are annotated with SkipTestUntil annotation -Pay attention that this listener shall be configured in the testng.xml (or command line) -*/ -public class SkipTestUntilTestngTransformer implements IAnnotationTransformer { - - @Override - public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) { - - if (testMethod!=null) { - try { - - if (!annotation.getEnabled()) { - return; - } - - if (!testMethod.isAnnotationPresent(SkipTestUntil.class)) { - return; - } - - String dateAsStr = testMethod.getAnnotation(SkipTestUntil.class).value(); - if (shallDisableTest(dateAsStr)) { - disableTest(annotation, testMethod.getDeclaringClass().getName(), dateAsStr); - } - - } catch (Exception e) { - e.printStackTrace(); - } - } - } - - private boolean shallDisableTest(String dateAsStr) { - try { - return LocalDate.now().isBefore(LocalDate.parse(dateAsStr)); - } - catch (RuntimeException exception) { - System.out.println("Failure during processing of SkipTestUntil annotation value is " + dateAsStr); - exception.printStackTrace(); - return false; - } - } - - private void disableTest(ITestAnnotation annotation, String name, String dateAsStr) { - System.out.println("Ignore "+ name+" till "+dateAsStr); - annotation.setEnabled(false); - } - -} - diff --git a/vid-automation/src/main/java/vid/automation/test/infra/SkipTestsTestngTransformer.java b/vid-automation/src/main/java/vid/automation/test/infra/SkipTestsTestngTransformer.java new file mode 100644 index 000000000..ed9aaaf69 --- /dev/null +++ b/vid-automation/src/main/java/vid/automation/test/infra/SkipTestsTestngTransformer.java @@ -0,0 +1,103 @@ +package vid.automation.test.infra; + +import java.time.LocalDate; +import org.testng.IAnnotationTransformer; +import org.testng.annotations.ITestAnnotation; +import org.togglz.core.context.StaticFeatureManagerProvider; + +import java.lang.reflect.AnnotatedElement; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; + +/* +This transformer skip test we want to ignore during running VID tests. +Pay attention that this listener shall be configured in the testng.xml (or command line) +It can't be used as Listener annotation of base class + +FeatureTogglingTest: +There are 2 ways to annotate that tests required featureFlags to be active : +In method level - with @FeatureTogglingTest on the test method and list of Required Feature flags on +In Class level - with @FeatureTogglingTest on the test class and list of Required Feature flags on +For each test annotation of method level, we check if the test shall whole class shall run regards the features flag test. + +SkipTestUntil: +If test annotated with SkipTestUntil the transformer check if the due date has already pass + +*/ +public class SkipTestsTestngTransformer implements IAnnotationTransformer { + + @Override + public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) { + + if (testMethod!=null) { + try { + + if (!annotation.getEnabled()) { + return; + } + + if (isIgnoreFeatureToggledTest(testMethod)) { + disableTest(annotation, testMethod.getName()); + return; + } + + if (isIgnoreFeatureToggledTest(testMethod.getDeclaringClass())) { + disableTest(annotation, testMethod.getDeclaringClass().getName()); + return; + } + + if (isIgnoreSkipUntilTest(testMethod)) { + disableTest(annotation, testMethod.getName()); + return; + } + + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + private boolean isIgnoreFeatureToggledTest(AnnotatedElement annotatedElement) { + + return (annotatedElement.isAnnotationPresent(FeatureTogglingTest.class) && + shallDisableTest(annotatedElement.getAnnotation(FeatureTogglingTest.class))); + + } + + private boolean shallDisableTest(FeatureTogglingTest featureTogglingTest) { + if (featureTogglingTest.value().length==0) { + return false; + } + if (new StaticFeatureManagerProvider().getFeatureManager()==null) { + FeaturesTogglingConfiguration.initializeFeatureManager(); + } + for (Features feature : featureTogglingTest.value()) { + if (!(feature.isActive()==featureTogglingTest.flagActive())) { + return true; + } + } + return false; + } + + private void disableTest(ITestAnnotation annotation, String name) { + System.out.println("Ignore "+ name+" due to annotation"); + annotation.setEnabled(false); + } + + private boolean isIgnoreSkipUntilTest(AnnotatedElement annotatedElement) { + if (!annotatedElement.isAnnotationPresent(SkipTestUntil.class)) { + return false; + } + + String dateAsStr = annotatedElement.getAnnotation(SkipTestUntil.class).value(); + try { + return LocalDate.now().isBefore(LocalDate.parse(dateAsStr)); + } + catch (RuntimeException exception) { + System.out.println("Failure during processing of SkipTestUntil annotation value is " + dateAsStr); + exception.printStackTrace(); + return false; + } + } +} + -- cgit 1.2.3-korg