AzkabanCommonModule extends AbstractModule { @Inject @Provides @Singleton public AzkabanEventReporter createAzkabanEventReporter() { final boolean eventReporterEnabled = props.getBoolean(AZKABAN_EVENT_REPORTING_ENABLED, false); if (!eventReporterEnabled) { logger.info("Event reporter is not enabled"); return null; } final Class<?> eventReporterClass = props.getClass(AZKABAN_EVENT_REPORTING_CLASS_PARAM, null); if (eventReporterClass != null && eventReporterClass.getConstructors().length > 0) { this.logger.info("Loading event reporter class " + eventReporterClass.getName()); try { final Constructor<?> eventReporterClassConstructor = eventReporterClass.getConstructor(Props.class); return (AzkabanEventReporter) eventReporterClassConstructor.newInstance(props); } catch (final InvocationTargetException e) { this.logger.error(e.getTargetException().getMessage()); if (e.getTargetException() instanceof IllegalArgumentException) { throw new IllegalArgumentException(e); } else { throw new RuntimeException(e); } } catch (final Exception e) { this.logger.error("Could not instantiate EventReporter " + eventReporterClass.getName()); throw new RuntimeException(e); } } return null; } AzkabanCommonModule(final Props props); Class<? extends Storage> resolveStorageClassType(); @Provides QueryRunner createQueryRunner(final AzkabanDataSource dataSource); @Inject @Provides @Singleton AzkabanEventReporter createAzkabanEventReporter();  }
@Test public void testCreateAzkabanEventReporter() { final Props props = new Props(); props.put(AZKABAN_EVENT_REPORTING_ENABLED, "true"); props.put(AZKABAN_EVENT_REPORTING_CLASS_PARAM, "azkaban.AzkabanEventReporterTest1"); final AzkabanCommonModule azkabanCommonModule = new AzkabanCommonModule(props); final AzkabanEventReporter azkabanEventReporter = azkabanCommonModule .createAzkabanEventReporter(); assertThat(azkabanEventReporter).isNotNull(); assertThat(azkabanEventReporter).isInstanceOf(AzkabanEventReporterTest1.class); }
@Test public void testAzkabanEventReporterInvalidProperties() { final Props props = new Props(); props.put(AZKABAN_EVENT_REPORTING_ENABLED, "true"); props.put(AZKABAN_EVENT_REPORTING_CLASS_PARAM, "azkaban.execapp.reporter.AzkabanKafkaAvroEventReporter"); final AzkabanCommonModule azkabanCommonModule = new AzkabanCommonModule(props); assertThatIllegalArgumentException() .isThrownBy(() -> azkabanCommonModule.createAzkabanEventReporter()); }
@Test public void testAzkabanEventReporterInvalidConstructor() { final Props props = new Props(); props.put(AZKABAN_EVENT_REPORTING_ENABLED, "true"); props.put(AZKABAN_EVENT_REPORTING_CLASS_PARAM, "azkaban.execapp.AzkabanEventReporterTest3"); AzkabanCommonModule azkabanCommonModule = new AzkabanCommonModule(props); assertThatExceptionOfType(RuntimeException.class) .isThrownBy(() -> azkabanCommonModule.createAzkabanEventReporter()); }
@Test public void testEventReporterDisabled() { final Props props = new Props(); AzkabanCommonModule AzkabanCommonModule = new AzkabanCommonModule(props); final AzkabanEventReporter azkabanEventReporter = AzkabanCommonModule .createAzkabanEventReporter(); assertThat(azkabanEventReporter).isNull(); }