ThunderApplication extends Application<ThunderConfiguration> { @Override public void run(ThunderConfiguration config, Environment env) { ThunderComponent component = DaggerThunderComponent.builder() .daoModule(new DaoModule(config.getUsersDaoFactory())) .emailModule(new EmailModule(config.getEmailServiceFactory())) .thunderModule(new ThunderModule(env.metrics(), config)) .build(); config.getAuthConfiguration().registerAuthentication(env); env.healthChecks().register("Database", component.getDatabaseHealthCheck()); env.jersey().register(component.getUserResource()); if (config.getEmailServiceFactory().isEnabled()) { env.jersey().register(component.getVerificationResource()); env.healthChecks().register("Email", component.getEmailHealthCheck()); } }  static void main(String[] args); @Override void initialize(Bootstrap<ThunderConfiguration> bootstrap); @Override void run(ThunderConfiguration config, Environment env);  }
@Test void testRun() { var environment = mock(Environment.class); var jersey = mock(JerseyEnvironment.class); var healthChecks = mock(HealthCheckRegistry.class); var metrics = mock(MetricRegistry.class); when(environment.jersey()).thenReturn(jersey); when(environment.healthChecks()).thenReturn(healthChecks); when(environment.metrics()).thenReturn(metrics); var captor = ArgumentCaptor.forClass(Object.class); application.run(CONFIG, environment); verify(jersey, atLeastOnce()).register(captor.capture()); verify(healthChecks, times(1)).register(eq("Database"), any(DatabaseHealthCheck.class)); verify(healthChecks, times(1)).register(eq("Email"), any(EmailHealthCheck.class)); List<Object> values = captor.getAllValues(); assertAll("Assert all objects were registered to Jersey", () -> assertEquals(1, values.stream().filter(v -> v instanceof AuthDynamicFeature).count()), () -> assertEquals(1, values.stream().filter(v -> v instanceof UserResource).count()), () -> assertEquals(1, values.stream().filter(v -> v instanceof VerificationResource).count())); }
@Test void testRunWithoutVerification() { var environment = mock(Environment.class); var jersey = mock(JerseyEnvironment.class); var healthChecks = mock(HealthCheckRegistry.class); var metrics = mock(MetricRegistry.class); var emailFactory = mock(EmailServiceFactory.class); var config = mock(ThunderConfiguration.class); when(environment.jersey()).thenReturn(jersey); when(environment.healthChecks()).thenReturn(healthChecks); when(environment.metrics()).thenReturn(metrics); when(emailFactory.isEnabled()).thenReturn(false); when(config.getAuthConfiguration()).thenReturn(new BasicAuthConfiguration()); when(config.getUsersDaoFactory()).thenReturn(DAO_FACTORY); when(config.getEmailServiceFactory()).thenReturn(emailFactory); when(config.getHashConfiguration()).thenReturn(new PasswordHashConfiguration()); when(config.getOpenApiConfiguration()).thenReturn(new OpenApiConfiguration()); ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class); application.run(config, environment); verify(jersey, atLeastOnce()).register(captor.capture()); verify(healthChecks, times(1)).register(eq("Database"), any(DatabaseHealthCheck.class)); verify(healthChecks, times(0)).register(eq("Email"), any(EmailHealthCheck.class)); List<Object> values = captor.getAllValues(); assertAll("Assert all objects were registered to Jersey", () -> assertEquals(1, values.stream().filter(v -> v instanceof AuthDynamicFeature).count()), () -> assertEquals(1, values.stream().filter(v -> v instanceof UserResource).count()), () -> assertEquals(0, values.stream().filter(v -> v instanceof VerificationResource).count())); }