@Test @SuppressWarnings("unchecked") public void registersACustomNameOfHealthCheckAndDBPoolMetrics() throws Exception { final HibernateBundle<Configuration> customBundle = new HibernateBundle<Configuration>(entities, factory) { @Override public DataSourceFactory getDataSourceFactory(Configuration configuration) { return dbConfig; } @Override protected String name() { return "custom-hibernate"; } }; when(factory.build(eq(customBundle), any(Environment.class), any(DataSourceFactory.class), anyList(), eq("custom-hibernate"))).thenReturn(sessionFactory); customBundle.run(configuration, environment); final ArgumentCaptor<SessionFactoryHealthCheck> captor = ArgumentCaptor.forClass(SessionFactoryHealthCheck.class); verify(healthChecks).register(eq("custom-hibernate"), captor.capture()); }
@Override public void run(BlogConfiguration configuration, Environment environment) throws Exception { // Crete DAOs final ArticleDAO articleDAO = new ArticleDAO(hibernateBundle.getSessionFactory()); // Create healthchecks final SessionFactoryHealthCheck dbHealthCheck = new SessionFactoryHealthCheck( hibernateBundle.getSessionFactory(), configuration.getDatabase().getValidationQuery() ); // Register resources, filters and healthchecks environment.jersey().register(new BlogResource(articleDAO)); environment.jersey().register(new ArticleResource(articleDAO)); environment.healthChecks().register("databaseHealthcheck", dbHealthCheck); environment.healthChecks().register("blogHealthcheck", new BlogHealthCheck(hibernateBundle.getSessionFactory())); }
@Override public final void run(T configuration, Environment environment) throws Exception { final Map<String, SessionFactory> sessionFactories = new LinkedHashMap<>(); for (DataSourceRoute route : getDataSourceRoutes(configuration)) { final String routeKey = route.getRouteName(); final DataSourceFactory dbConfig = route.getDatabase(); final SessionFactory sessionFactory = sessionFactoryFactory.build(this, environment, dbConfig, entities, routeKey); String validationQuery = "/* Sess Factory Health Check routeKey " + routeKey + " */ " + dbConfig.getValidationQuery(); environment.healthChecks().register( routeKey, new SessionFactoryHealthCheck(environment.getHealthCheckExecutorService(), dbConfig.getValidationQueryTimeout().or(Duration.seconds(5)), sessionFactory, validationQuery)); sessionFactories.put(routeKey, sessionFactory); } this.sessionFactoryMap = ImmutableMap.copyOf(sessionFactories); environment.jersey().register(new RoutingUnitOfWorkApplicationListener(this.sessionFactoryMap)); }
@Test public void registersSessionFactoryHealthChecks() throws Exception { dbConfigRouteOne.setValidationQuery("SELECT something RouteOne"); dbConfigRouteTwo.setValidationQuery("SELECT something RouteTwo"); bundle.run(configuration, environment); final ArgumentCaptor<SessionFactoryHealthCheck> captor = ArgumentCaptor .forClass(SessionFactoryHealthCheck.class); verify(healthChecks).register(eq(ROUTE_ONE), captor.capture()); assertThat(captor.getValue().getSessionFactory()).isEqualTo(sessionFactoryRouteOne); assertThat(captor.getValue().getValidationQuery()).isEqualTo("/* Sess Factory Health Check routeKey RouteOne */ SELECT something RouteOne"); verify(healthChecks).register(eq(ROUTE_TWO), captor.capture()); assertThat(captor.getValue().getSessionFactory()).isEqualTo(sessionFactoryRouteTwo); assertThat(captor.getValue().getValidationQuery()).isEqualTo("/* Sess Factory Health Check routeKey RouteTwo */ SELECT something RouteTwo"); }
@Test public void registersASessionFactoryHealthCheck() throws Exception { dbConfig.setValidationQuery("SELECT something"); bundle.run(configuration, environment); final ArgumentCaptor<SessionFactoryHealthCheck> captor = ArgumentCaptor.forClass(SessionFactoryHealthCheck.class); verify(healthChecks).register(eq("hibernate"), captor.capture()); assertThat(captor.getValue().getSessionFactory()).isEqualTo(sessionFactory); assertThat(captor.getValue().getValidationQuery()).isEqualTo("SELECT something"); }