Browse Source
* pr/11997: Polish "Add health indicator for reactive MongoDB" Add health indicator for reactive MongoDBpull/12021/merge
11 changed files with 351 additions and 36 deletions
@ -0,0 +1,55 @@
@@ -0,0 +1,55 @@
|
||||
/* |
||||
* Copyright 2012-2018 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.mongo; |
||||
|
||||
import java.util.Map; |
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.health.CompositeHealthIndicatorConfiguration; |
||||
import org.springframework.boot.actuate.health.HealthIndicator; |
||||
import org.springframework.boot.actuate.mongo.MongoHealthIndicator; |
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.data.mongodb.core.MongoTemplate; |
||||
|
||||
/** |
||||
* Configuration for {@link MongoHealthIndicator}. |
||||
* |
||||
* @author Stephane Nicoll |
||||
*/ |
||||
@Configuration |
||||
@ConditionalOnClass(MongoTemplate.class) |
||||
@ConditionalOnBean(MongoTemplate.class) |
||||
class MongoHealthIndicatorConfiguration extends |
||||
CompositeHealthIndicatorConfiguration<MongoHealthIndicator, MongoTemplate> { |
||||
|
||||
private final Map<String, MongoTemplate> mongoTemplates; |
||||
|
||||
MongoHealthIndicatorConfiguration( |
||||
Map<String, MongoTemplate> mongoTemplates) { |
||||
this.mongoTemplates = mongoTemplates; |
||||
} |
||||
|
||||
@Bean |
||||
@ConditionalOnMissingBean(name = "mongoHealthIndicator") |
||||
public HealthIndicator mongoHealthIndicator() { |
||||
return createHealthIndicator(this.mongoTemplates); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,55 @@
@@ -0,0 +1,55 @@
|
||||
/* |
||||
* Copyright 2012-2018 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.mongo; |
||||
|
||||
import java.util.Map; |
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.health.CompositeReactiveHealthIndicatorConfiguration; |
||||
import org.springframework.boot.actuate.health.ReactiveHealthIndicator; |
||||
import org.springframework.boot.actuate.mongo.MongoReactiveHealthIndicator; |
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.data.mongodb.core.ReactiveMongoTemplate; |
||||
|
||||
/** |
||||
* Configuration for {@link MongoReactiveHealthIndicator}. |
||||
* |
||||
* @author Stephane Nicoll |
||||
*/ |
||||
@Configuration |
||||
@ConditionalOnClass(ReactiveMongoTemplate.class) |
||||
@ConditionalOnBean(ReactiveMongoTemplate.class) |
||||
class MongoReactiveHealthIndicatorConfiguration extends |
||||
CompositeReactiveHealthIndicatorConfiguration<MongoReactiveHealthIndicator, ReactiveMongoTemplate> { |
||||
|
||||
private final Map<String, ReactiveMongoTemplate> reactiveMongoTemplate; |
||||
|
||||
MongoReactiveHealthIndicatorConfiguration( |
||||
Map<String, ReactiveMongoTemplate> reactiveMongoTemplate) { |
||||
this.reactiveMongoTemplate = reactiveMongoTemplate; |
||||
} |
||||
|
||||
@Bean |
||||
@ConditionalOnMissingBean(name = "mongoHealthIndicator") |
||||
public ReactiveHealthIndicator mongoHealthIndicator() { |
||||
return createHealthIndicator(this.reactiveMongoTemplate); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,67 @@
@@ -0,0 +1,67 @@
|
||||
/* |
||||
* Copyright 2012-2018 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.mongo; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.health.HealthIndicatorAutoConfiguration; |
||||
import org.springframework.boot.actuate.health.ApplicationHealthIndicator; |
||||
import org.springframework.boot.actuate.mongo.MongoHealthIndicator; |
||||
import org.springframework.boot.actuate.mongo.MongoReactiveHealthIndicator; |
||||
import org.springframework.boot.autoconfigure.AutoConfigurations; |
||||
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration; |
||||
import org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration; |
||||
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; |
||||
import org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration; |
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Tests for {@link MongoReactiveHealthIndicatorConfiguration}. |
||||
* |
||||
* @author Yulin Qin |
||||
*/ |
||||
public class MongoReactiveHealthIndicatorConfigurationTests { |
||||
|
||||
private ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
||||
.withConfiguration(AutoConfigurations.of( |
||||
MongoAutoConfiguration.class, |
||||
MongoDataAutoConfiguration.class, |
||||
MongoReactiveAutoConfiguration.class, |
||||
MongoReactiveDataAutoConfiguration.class, |
||||
MongoHealthIndicatorAutoConfiguration.class, |
||||
HealthIndicatorAutoConfiguration.class)); |
||||
|
||||
@Test |
||||
public void runShouldCreateIndicator() { |
||||
this.contextRunner.run( |
||||
(context) -> assertThat(context).hasSingleBean(MongoReactiveHealthIndicator.class) |
||||
.doesNotHaveBean(MongoHealthIndicator.class) |
||||
.doesNotHaveBean(ApplicationHealthIndicator.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void runWhenDisabledShouldNotCreateIndicator() { |
||||
this.contextRunner.withPropertyValues("management.health.mongo.enabled:false") |
||||
.run((context) -> assertThat(context) |
||||
.doesNotHaveBean(MongoReactiveHealthIndicator.class) |
||||
.doesNotHaveBean(MongoHealthIndicator.class) |
||||
.hasSingleBean(ApplicationHealthIndicator.class)); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,55 @@
@@ -0,0 +1,55 @@
|
||||
/* |
||||
* Copyright 2012-2018 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.boot.actuate.mongo; |
||||
|
||||
import org.bson.Document; |
||||
import reactor.core.publisher.Mono; |
||||
|
||||
import org.springframework.boot.actuate.health.AbstractReactiveHealthIndicator; |
||||
import org.springframework.boot.actuate.health.Health; |
||||
import org.springframework.boot.actuate.health.ReactiveHealthIndicator; |
||||
import org.springframework.data.mongodb.core.ReactiveMongoTemplate; |
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* A {@link ReactiveHealthIndicator} for Mongo. |
||||
* |
||||
* @author Yulin Qin |
||||
* @since 2.0.0 |
||||
*/ |
||||
public class MongoReactiveHealthIndicator extends AbstractReactiveHealthIndicator { |
||||
|
||||
private final ReactiveMongoTemplate reactiveMongoTemplate; |
||||
|
||||
public MongoReactiveHealthIndicator(ReactiveMongoTemplate reactiveMongoTemplate) { |
||||
Assert.notNull(reactiveMongoTemplate, "ReactiveMongoTemplate must not be null"); |
||||
this.reactiveMongoTemplate = reactiveMongoTemplate; |
||||
} |
||||
|
||||
@Override |
||||
protected Mono<Health> doHealthCheck(Health.Builder builder) { |
||||
Mono<Document> buildInfo = this.reactiveMongoTemplate.executeCommand( |
||||
"{ buildInfo: 1 }"); |
||||
return buildInfo.map(document -> up(builder, document)); |
||||
} |
||||
|
||||
private Health up(Health.Builder builder, Document document) { |
||||
return builder.up().withDetail("version", document.getString("version")).build(); |
||||
} |
||||
|
||||
} |
||||
|
||||
@ -0,0 +1,72 @@
@@ -0,0 +1,72 @@
|
||||
/* |
||||
* Copyright 2012-2018 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.boot.actuate.mongo; |
||||
|
||||
import com.mongodb.MongoException; |
||||
import org.bson.Document; |
||||
import org.junit.Test; |
||||
import reactor.core.publisher.Mono; |
||||
import reactor.test.StepVerifier; |
||||
|
||||
import org.springframework.boot.actuate.health.Health; |
||||
import org.springframework.boot.actuate.health.Status; |
||||
import org.springframework.data.mongodb.core.ReactiveMongoTemplate; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.mockito.BDDMockito.given; |
||||
import static org.mockito.Mockito.mock; |
||||
|
||||
/** |
||||
* Tests for {@link MongoReactiveHealthIndicator}. |
||||
* |
||||
* @author Yulin Qin |
||||
*/ |
||||
public class MongoReactiveHealthIndicatorTest { |
||||
|
||||
@Test |
||||
public void testMongoIsUp() { |
||||
Document buildInfo = mock(Document.class); |
||||
given(buildInfo.getString("version")).willReturn("2.6.4"); |
||||
ReactiveMongoTemplate reactiveMongoTemplate = mock(ReactiveMongoTemplate.class); |
||||
given(reactiveMongoTemplate.executeCommand("{ buildInfo: 1 }")).willReturn( |
||||
Mono.just(buildInfo)); |
||||
|
||||
MongoReactiveHealthIndicator mongoReactiveHealthIndicator = new MongoReactiveHealthIndicator(reactiveMongoTemplate); |
||||
Mono<Health> health = mongoReactiveHealthIndicator.health(); |
||||
StepVerifier.create(health).consumeNextWith((h) -> { |
||||
assertThat(h.getStatus()).isEqualTo(Status.UP); |
||||
assertThat(h.getDetails()).containsOnlyKeys("version"); |
||||
assertThat(h.getDetails().get("version")).isEqualTo("2.6.4"); |
||||
}).verifyComplete(); |
||||
} |
||||
|
||||
@Test |
||||
public void testMongoIsDown() { |
||||
ReactiveMongoTemplate reactiveMongoTemplate = mock(ReactiveMongoTemplate.class); |
||||
given(reactiveMongoTemplate.executeCommand("{ buildInfo: 1 }")).willThrow( |
||||
new MongoException("Connection failed")); |
||||
|
||||
MongoReactiveHealthIndicator mongoReactiveHealthIndicator = new MongoReactiveHealthIndicator(reactiveMongoTemplate); |
||||
Mono<Health> health = mongoReactiveHealthIndicator.health(); |
||||
StepVerifier.create(health).consumeNextWith((h) -> { |
||||
assertThat(h.getStatus()).isEqualTo(Status.DOWN); |
||||
assertThat(h.getDetails()).containsOnlyKeys("error"); |
||||
assertThat(h.getDetails().get("error")).isEqualTo("Connection failed"); |
||||
}).verifyComplete(); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue