Let API tests run without report_portal_integration, using dynamic loading
[vid.git] / vid-automation / src / main / java / vid / automation / reportportal / ReportPortalListenerDelegator.java
1 package vid.automation.reportportal;
2
3 import static org.apache.commons.beanutils.MethodUtils.invokeStaticMethod;
4
5 import org.apache.commons.proxy.ProxyFactory;
6 import org.apache.commons.proxy.factory.javassist.JavassistProxyFactory;
7 import org.apache.commons.proxy.invoker.NullInvoker;
8 import org.openqa.selenium.WebDriver;
9 import org.testng.IExecutionListener;
10 import org.testng.ISuite;
11 import org.testng.ISuiteListener;
12 import org.testng.ITestContext;
13 import org.testng.ITestResult;
14 import org.testng.internal.IResultListener2;
15
16 /**
17  * Loads and delegates to ReportPortalListener. When class not found -- yields no-op object, and no side-effect.
18  */
19 public class ReportPortalListenerDelegator implements IExecutionListener, ISuiteListener, IResultListener2 {
20
21     private static final String CLASSNAME_REPORT_PORTAL_LISTENER = "com.att.automation.common.report_portal_integration.listeners.ReportPortalListener";
22     private static final String CLASSNAME_WEB_DRIVER_SCREENSHOTS_PROVIDER = "com.att.automation.common.report_portal_integration.screenshots.WebDriverScreenshotsProvider";
23
24     private static final Object instance = createReportPortalListener();
25
26     private final IExecutionListener iExecutionListener;
27     private final ISuiteListener iSuiteListener;
28     private final IResultListener2 iResultListener2;
29
30     public ReportPortalListenerDelegator() {
31         iExecutionListener = ((IExecutionListener) instance);
32         iSuiteListener = ((ISuiteListener) instance);
33         iResultListener2 = ((IResultListener2) instance);
34     }
35
36     public static void setScreenShotsWebDriver(WebDriver driver) {
37         try {
38             invokeStaticMethod(instance.getClass(), "setScreenShotsProvider", createScreenshotsProvider(driver));
39         } catch (ClassNotFoundException e) {
40             // if class not found, don't bother
41         } catch (ReflectiveOperationException e) {
42             throw new RuntimeException(e);
43         }
44     }
45
46
47     @Override
48     public void beforeConfiguration(ITestResult tr) {
49         iResultListener2.beforeConfiguration(tr);
50     }
51
52     @Override
53     public void onConfigurationSuccess(ITestResult itr) {
54         iResultListener2.onConfigurationSuccess(itr);
55     }
56
57     @Override
58     public void onConfigurationFailure(ITestResult itr) {
59         iResultListener2.onConfigurationFailure(itr);
60     }
61
62     @Override
63     public void onConfigurationSkip(ITestResult itr) {
64         iResultListener2.onConfigurationSkip(itr);
65     }
66
67     @Override
68     public void onExecutionStart() {
69         iExecutionListener.onExecutionStart();
70     }
71
72     @Override
73     public void onExecutionFinish() {
74         iExecutionListener.onExecutionFinish();
75     }
76
77     @Override
78     public void onStart(ISuite suite) {
79         iSuiteListener.onStart(suite);
80
81     }
82
83     @Override
84     public void onFinish(ISuite suite) {
85         iSuiteListener.onFinish(suite);
86     }
87
88     @Override
89     public void onTestStart(ITestResult result) {
90         iResultListener2.onTestStart(result);
91     }
92
93     @Override
94     public void onTestSuccess(ITestResult result) {
95         iResultListener2.onTestSuccess(result);
96     }
97
98     @Override
99     public void onTestFailure(ITestResult result) {
100         iResultListener2.onTestFailure(result);
101     }
102
103     @Override
104     public void onTestSkipped(ITestResult result) {
105         iResultListener2.onTestSkipped(result);
106     }
107
108     @Override
109     public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
110         iResultListener2.onTestFailedButWithinSuccessPercentage(result);
111     }
112
113     @Override
114     public void onStart(ITestContext context) {
115         iResultListener2.onStart(context);
116     }
117
118     @Override
119     public void onFinish(ITestContext context) {
120         iResultListener2.onFinish(context);
121     }
122
123
124     private static Object createReportPortalListener() {
125         try {
126             final Class<?> classToLoad = Class.forName(CLASSNAME_REPORT_PORTAL_LISTENER,
127                 true, ReportPortalListenerDelegator.class.getClassLoader());
128             return classToLoad.getConstructor().newInstance();
129         } catch (ClassNotFoundException e) {
130             // Fallback to NullInvoker
131             final Class[] classes = {IExecutionListener.class, ISuiteListener.class, IResultListener2.class};
132             final ProxyFactory proxyFactory = new JavassistProxyFactory();
133
134             return proxyFactory.createInvokerProxy(new NullInvoker(), classes);
135         } catch (ReflectiveOperationException e) {
136             throw new RuntimeException(e);
137         }
138     }
139
140     private static Object createScreenshotsProvider(WebDriver driver) throws ReflectiveOperationException {
141         Class<?> classToLoad = Class.forName(CLASSNAME_WEB_DRIVER_SCREENSHOTS_PROVIDER,
142             true, driver.getClass().getClassLoader());
143
144         return classToLoad.getDeclaredConstructor(WebDriver.class).newInstance(driver);
145     }
146
147 }