@Test public void testParentConnectionFactoryInheritedByDefault() { context = SpringApplication.run(SimpleProcessor.class, "--server.port=0"); BinderFactory<?> binderFactory = context.getBean(BinderFactory.class); Binder binder = binderFactory.getBinder(null); assertThat(binder, instanceOf(RedisMessageChannelBinder.class)); DirectFieldAccessor binderFieldAccessor = new DirectFieldAccessor(binder); RedisConnectionFactory binderConnectionFactory = (RedisConnectionFactory) binderFieldAccessor.getPropertyValue("connectionFactory"); assertThat(binderConnectionFactory, instanceOf(RedisConnectionFactory.class)); RedisConnectionFactory connectionFactory = context.getBean(RedisConnectionFactory.class); assertThat(binderConnectionFactory, is(connectionFactory)); CompositeHealthIndicator bindersHealthIndicator = context.getBean("bindersHealthIndicator", CompositeHealthIndicator.class); assertNotNull(bindersHealthIndicator); DirectFieldAccessor directFieldAccessor = new DirectFieldAccessor(bindersHealthIndicator); @SuppressWarnings("unchecked") Map<String,HealthIndicator> healthIndicators = (Map<String, HealthIndicator>) directFieldAccessor.getPropertyValue("indicators"); assertThat(healthIndicators, hasKey("redis")); assertThat(healthIndicators.get("redis").health().getStatus(), equalTo(Status.UP)); }
@Test public void testParentConnectionFactoryInheritedIfOverridden() { context = new SpringApplication(SimpleProcessor.class, ConnectionFactoryConfiguration.class).run("--server.port=0"); BinderFactory<?> binderFactory = context.getBean(BinderFactory.class); Binder binder = binderFactory.getBinder(null); assertThat(binder, instanceOf(RedisMessageChannelBinder.class)); DirectFieldAccessor binderFieldAccessor = new DirectFieldAccessor(binder); RedisConnectionFactory binderConnectionFactory = (RedisConnectionFactory) binderFieldAccessor.getPropertyValue("connectionFactory"); assertThat(binderConnectionFactory, is(MOCK_CONNECTION_FACTORY)); RedisConnectionFactory connectionFactory = context.getBean(RedisConnectionFactory.class); assertThat(binderConnectionFactory, is(connectionFactory)); CompositeHealthIndicator bindersHealthIndicator = context.getBean("bindersHealthIndicator", CompositeHealthIndicator.class); assertNotNull(bindersHealthIndicator); DirectFieldAccessor directFieldAccessor = new DirectFieldAccessor(bindersHealthIndicator); @SuppressWarnings("unchecked") Map<String,HealthIndicator> healthIndicators = (Map<String, HealthIndicator>) directFieldAccessor.getPropertyValue("indicators"); assertThat(healthIndicators, hasKey("redis")); assertThat(healthIndicators.get("redis").health().getStatus(), equalTo(Status.UP)); }
@Test public void dataSourceHealthIndicatorWithCustomValidationQuery() { this.context.register(PropertyPlaceholderAutoConfiguration.class, ManagementServerProperties.class, DataSourceProperties.class, DataSourceConfig.class, DataSourcePoolMetadataProvidersConfiguration.class, HealthIndicatorAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.test.validation-query:SELECT from FOOBAR", "management.health.diskspace.enabled:false"); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); HealthIndicator healthIndicator = beans.values().iterator().next(); assertThat(healthIndicator.getClass()).isEqualTo(DataSourceHealthIndicator.class); DataSourceHealthIndicator dataSourceHealthIndicator = (DataSourceHealthIndicator) healthIndicator; assertThat(dataSourceHealthIndicator.getQuery()).isEqualTo("SELECT from FOOBAR"); }
@Test public void elasticsearchHealthIndicator() { EnvironmentTestUtils.addEnvironment(this.context, "spring.data.elasticsearch.properties.path.home:target", "management.health.diskspace.enabled:false"); this.context.register(JestClientConfiguration.class, JestAutoConfiguration.class, ElasticsearchAutoConfiguration.class, ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); assertThat(beans.values().iterator().next().getClass()) .isEqualTo(ElasticsearchHealthIndicator.class); }
@Test public void notElasticsearchHealthIndicator() { EnvironmentTestUtils.addEnvironment(this.context, "management.health.elasticsearch.enabled:false", "spring.data.elasticsearch.properties.path.home:target", "management.health.diskspace.enabled:false"); this.context.register(JestClientConfiguration.class, JestAutoConfiguration.class, ElasticsearchAutoConfiguration.class, ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); assertThat(beans.values().iterator().next().getClass()) .isEqualTo(ApplicationHealthIndicator.class); }
@Test public void testParentConnectionFactoryInheritedIfOverridden() { context = new SpringApplicationBuilder(SimpleProcessor.class, ConnectionFactoryConfiguration.class) .web(WebApplicationType.NONE) .run("--server.port=0"); BinderFactory binderFactory = context.getBean(BinderFactory.class); Binder<?, ?, ?> binder = binderFactory.getBinder(null, MessageChannel.class); assertThat(binder).isInstanceOf(RabbitMessageChannelBinder.class); DirectFieldAccessor binderFieldAccessor = new DirectFieldAccessor(binder); ConnectionFactory binderConnectionFactory = (ConnectionFactory) binderFieldAccessor .getPropertyValue("connectionFactory"); assertThat(binderConnectionFactory).isSameAs(MOCK_CONNECTION_FACTORY); ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class); assertThat(binderConnectionFactory).isSameAs(connectionFactory); CompositeHealthIndicator bindersHealthIndicator = context.getBean("bindersHealthIndicator", CompositeHealthIndicator.class); assertThat(bindersHealthIndicator).isNotNull(); DirectFieldAccessor directFieldAccessor = new DirectFieldAccessor(bindersHealthIndicator); @SuppressWarnings("unchecked") Map<String, HealthIndicator> healthIndicators = (Map<String, HealthIndicator>) directFieldAccessor .getPropertyValue("indicators"); assertThat(healthIndicators).containsKey("rabbit"); // mock connection factory behaves as if down assertThat(healthIndicators.get("rabbit").health().getStatus()).isEqualTo(Status.DOWN); }
@Bean public HealthIndicator taskHealthIndicator() { return new AbstractHealthIndicator() { @Override protected void doHealthCheck(Health.Builder builder) throws Exception { if (correctNumberOfInstances()) { builder.up(); } else { builder.down(); } builder.withDetail("mesos.resources.count", instanceCount.getCount()); builder.withDetail("instances", stateRepository.allTaskInfos().size()); for (Protos.TaskState taskState : Protos.TaskState.values()) { Map<String, Protos.TaskStatus> state = getTasksForState(taskState); builder.withDetail(taskState.name(), state.size()); } } }; }
@Override public void configure(CommandLine cli) { CommandLine healthCli = new CommandLine(new HelpAwareContainerPicocliCommand() {}); for (Map.Entry<String, HealthIndicator> entry : healthIndicators.entrySet()) { Matcher matcher = HEALTH_PATTERN.matcher(entry.getKey()); if (matcher.matches()) { String name = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, matcher.group(1)); healthCli.addSubcommand(name, new PrintCommand(entry.getValue().health())); } else { logger.warn("Unable to determine a correct name for given indicator: \"{}\", skip it!", entry.getKey()); } } cli.addSubcommand("health", healthCli); }
@Test public void jmsHealthIndicator() { EnvironmentTestUtils.addEnvironment(this.context, "management.health.diskspace.enabled:false"); this.context.register(ActiveMQAutoConfiguration.class, ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); assertThat(beans.values().iterator().next().getClass()) .isEqualTo(JmsHealthIndicator.class); }
protected HealthIndicator createHealthIndicator(Map<String, S> beans) { if (beans.size() == 1) { return createHealthIndicator(beans.values().iterator().next()); } CompositeHealthIndicator composite = new CompositeHealthIndicator( this.healthAggregator); for (Map.Entry<String, S> entry : beans.entrySet()) { composite.addHealthIndicator(entry.getKey(), createHealthIndicator(entry.getValue())); } return composite; }
@Test public void notJmsHealthIndicator() { EnvironmentTestUtils.addEnvironment(this.context, "management.health.jms.enabled:false", "management.health.diskspace.enabled:false"); this.context.register(ActiveMQAutoConfiguration.class, ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); assertThat(beans.values().iterator().next().getClass()) .isEqualTo(ApplicationHealthIndicator.class); }
@Test public void notMailHealthIndicator() { EnvironmentTestUtils.addEnvironment(this.context, "spring.mail.host:smtp.acme.org", "management.health.mail.enabled:false", "management.health.diskspace.enabled:false"); this.context.register(MailSenderAutoConfiguration.class, ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); assertThat(beans.values().iterator().next().getClass()) .isEqualTo(ApplicationHealthIndicator.class); }
@Bean @ConditionalOnMissingBean public HealthEndpoint healthEndpoint() { return new HealthEndpoint( this.healthAggregator == null ? new OrderedHealthAggregator() : this.healthAggregator, this.healthIndicators == null ? Collections.<String, HealthIndicator>emptyMap() : this.healthIndicators); }
/** * Create a new {@link HealthIndicator} instance. * @param healthAggregator the health aggregator * @param healthIndicators the health indicators */ public HealthEndpoint(HealthAggregator healthAggregator, Map<String, HealthIndicator> healthIndicators) { super("health", false); Assert.notNull(healthAggregator, "HealthAggregator must not be null"); Assert.notNull(healthIndicators, "HealthIndicators must not be null"); CompositeHealthIndicator healthIndicator = new CompositeHealthIndicator( healthAggregator); for (Map.Entry<String, HealthIndicator> entry : healthIndicators.entrySet()) { healthIndicator.addHealthIndicator(getKey(entry.getKey()), entry.getValue()); } this.healthIndicator = healthIndicator; }
@Test public void defaultHealthIndicator() { this.context.register(HealthIndicatorAutoConfiguration.class, ManagementServerProperties.class); EnvironmentTestUtils.addEnvironment(this.context, "management.health.diskspace.enabled:false"); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); assertThat(beans.values().iterator().next().getClass()) .isEqualTo(ApplicationHealthIndicator.class); }
@Test public void defaultHealthIndicatorsDisabled() { this.context.register(HealthIndicatorAutoConfiguration.class, ManagementServerProperties.class); EnvironmentTestUtils.addEnvironment(this.context, "management.health.defaults.enabled:false"); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); assertThat(beans.values().iterator().next().getClass()) .isEqualTo(ApplicationHealthIndicator.class); }
@Test public void defaultHealthIndicatorsDisabledWithCustomOne() { this.context.register(CustomHealthIndicator.class, HealthIndicatorAutoConfiguration.class, ManagementServerProperties.class); EnvironmentTestUtils.addEnvironment(this.context, "management.health.defaults.enabled:false"); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); assertThat(this.context.getBean("customHealthIndicator")) .isSameAs(beans.values().iterator().next()); }
@Test public void defaultHealthIndicatorsDisabledButOne() { this.context.register(HealthIndicatorAutoConfiguration.class, ManagementServerProperties.class); EnvironmentTestUtils.addEnvironment(this.context, "management.health.defaults.enabled:false", "management.health.diskspace.enabled:true"); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); assertThat(beans.values().iterator().next().getClass()) .isEqualTo(DiskSpaceHealthIndicator.class); }
@Test public void redisHealthIndicator() { this.context.register(RedisAutoConfiguration.class, ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "management.health.diskspace.enabled:false"); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); assertThat(beans.values().iterator().next().getClass()) .isEqualTo(RedisHealthIndicator.class); }
@Test public void notRedisHealthIndicator() { this.context.register(RedisAutoConfiguration.class, ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "management.health.redis.enabled:false", "management.health.diskspace.enabled:false"); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); assertThat(beans.values().iterator().next().getClass()) .isEqualTo(ApplicationHealthIndicator.class); }
@Test public void mongoHealthIndicator() { this.context.register(MongoAutoConfiguration.class, ManagementServerProperties.class, MongoDataAutoConfiguration.class, HealthIndicatorAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "management.health.diskspace.enabled:false"); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); assertThat(beans.values().iterator().next().getClass()) .isEqualTo(MongoHealthIndicator.class); }
@Test public void notMongoHealthIndicator() { this.context.register(MongoAutoConfiguration.class, ManagementServerProperties.class, MongoDataAutoConfiguration.class, HealthIndicatorAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "management.health.mongo.enabled:false", "management.health.diskspace.enabled:false"); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); assertThat(beans.values().iterator().next().getClass()) .isEqualTo(ApplicationHealthIndicator.class); }
@Test public void combinedHealthIndicator() { this.context.register(MongoAutoConfiguration.class, RedisAutoConfiguration.class, MongoDataAutoConfiguration.class, SolrAutoConfiguration.class, HealthIndicatorAutoConfiguration.class); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(4); }
@Test public void couchbaseHealthIndicator() throws Exception { EnvironmentTestUtils.addEnvironment(this.context, "management.health.diskspace.enabled:false"); this.context.register(CouchbaseConfiguration.class, ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans.size()).isEqualTo(1); assertThat(beans.values().iterator().next().getClass()) .isEqualTo(CouchbaseHealthIndicator.class); }
@Test public void notDataSourceHealthIndicator() { this.context.register(EmbeddedDataSourceConfiguration.class, ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "management.health.db.enabled:false", "management.health.diskspace.enabled:false"); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); assertThat(beans.values().iterator().next().getClass()) .isEqualTo(ApplicationHealthIndicator.class); }
@Test public void elasticSearchHealthIndicator() { EnvironmentTestUtils.addEnvironment(this.context, "spring.data.elasticsearch.properties.path.home:target", "management.health.diskspace.enabled:false"); this.context.register(ElasticsearchAutoConfiguration.class, ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); assertThat(beans.values().iterator().next().getClass()) .isEqualTo(ElasticsearchHealthIndicator.class); }
@Test public void notRabbitHealthIndicator() { this.context.register(RabbitAutoConfiguration.class, ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "management.health.rabbit.enabled:false", "management.health.diskspace.enabled:false"); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); assertThat(beans.values().iterator().next().getClass()) .isEqualTo(ApplicationHealthIndicator.class); }
@Test public void solrHealthIndicator() { this.context.register(SolrAutoConfiguration.class, ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "management.health.diskspace.enabled:false"); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); assertThat(beans.values().iterator().next().getClass()) .isEqualTo(SolrHealthIndicator.class); }
@Test public void notSolrHealthIndicator() { this.context.register(SolrAutoConfiguration.class, ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "management.health.solr.enabled:false", "management.health.diskspace.enabled:false"); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); assertThat(beans.values().iterator().next().getClass()) .isEqualTo(ApplicationHealthIndicator.class); }
@Test public void diskSpaceHealthIndicator() { this.context.register(HealthIndicatorAutoConfiguration.class); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); assertThat(beans.values().iterator().next().getClass()) .isEqualTo(DiskSpaceHealthIndicator.class); }
@Test public void mailHealthIndicator() { EnvironmentTestUtils.addEnvironment(this.context, "spring.mail.host:smtp.acme.org", "management.health.diskspace.enabled:false"); this.context.register(MailSenderAutoConfiguration.class, ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); assertThat(beans.values().iterator().next().getClass()) .isEqualTo(MailHealthIndicator.class); }
@Test public void elasticsearchJestHealthIndicator() { EnvironmentTestUtils.addEnvironment(this.context, "management.health.diskspace.enabled:false"); this.context.register(JestClientConfiguration.class, JestAutoConfiguration.class, ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); assertThat(beans.values().iterator().next().getClass()) .isEqualTo(ElasticsearchJestHealthIndicator.class); }
@Test public void cassandraHealthIndicator() throws Exception { EnvironmentTestUtils.addEnvironment(this.context, "management.health.diskspace.enabled:false"); this.context.register(CassandraConfiguration.class, ManagementServerProperties.class, HealthIndicatorAutoConfiguration.class); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); assertThat(beans.values().iterator().next().getClass()) .isEqualTo(CassandraHealthIndicator.class); }
@Bean public HealthIndicator customHealthIndicator() { return new HealthIndicator() { @Override public Health health() { return Health.down().build(); } }; }
@Bean public HealthIndicator statusHealthIndicator() { return new HealthIndicator() { @Override public Health health() { return new Health.Builder().status("FINE").build(); } }; }