Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions spring-boot-project/spring-boot-actuator-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,16 @@
<artifactId>spring-data-mongodb</artifactId>
<optional>true</optional>
</dependency>
<dependency>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dependencies are ordered alphabetically. Please reorder those two entries.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dependencies been re-ordered.

<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-async</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-reactivestreams</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.autoconfigure.health.ConditionalOnEnabledHealthIndicator;
import org.springframework.boot.actuate.autoconfigure.health.HealthIndicatorAutoConfiguration;
import org.springframework.boot.actuate.health.ReactiveHealthIndicator;
import org.springframework.boot.actuate.mongo.MongoReactiveHealthIndicator;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.mongo.MongoReactiveDataAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;



/**
* {@link EnableAutoConfiguration Auto-configuration} for {@link MongoReactiveHealthIndicator}.
*
* @author Yulin Qin
* @since 2.0.0
*/
@Configuration
@ConditionalOnClass(ReactiveMongoTemplate.class)
@ConditionalOnBean(ReactiveMongoTemplate.class)
@ConditionalOnEnabledHealthIndicator("mongo")
@AutoConfigureBefore(HealthIndicatorAutoConfiguration.class)
@AutoConfigureAfter({MongoReactiveAutoConfiguration.class, MongoReactiveDataAutoConfiguration.class})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think only the latter is necessary here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

public class MongoReactiveHealthIndicatorAutoConfiguration extends
CompositeReactiveHealthIndicatorConfiguration<MongoReactiveHealthIndicator, ReactiveMongoTemplate> {

private final Map<String, ReactiveMongoTemplate> reactiveMongoTemplate;

public MongoReactiveHealthIndicatorAutoConfiguration(
Map<String, ReactiveMongoTemplate> reactiveMongoTemplate) {
this.reactiveMongoTemplate = reactiveMongoTemplate;
}

@Bean
@ConditionalOnMissingBean(name = "mongoReactiveHealthIndicator")
public ReactiveHealthIndicator mongoReactiveHealthIndicator() {
return createHealthIndicator(this.reactiveMongoTemplate);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ org.springframework.boot.actuate.autoconfigure.metrics.web.client.RestTemplateMe
org.springframework.boot.actuate.autoconfigure.metrics.web.reactive.WebFluxMetricsAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.web.servlet.WebMvcMetricsAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.mongo.MongoHealthIndicatorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.mongo.MongoReactiveHealthIndicatorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.neo4j.Neo4jHealthIndicatorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.redis.RedisHealthIndicatorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.scheduling.ScheduledTasksEndpointAutoConfiguration,\
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.MongoReactiveDataAutoConfiguration;
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 MongoReactiveHealthIndicatorAutoConfiguration}
*
* @author Yulin Qin
*/
public class MongoReactiveHealthIndicatorAutoConfigurationTests {

private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
MongoReactiveAutoConfiguration.class,
MongoReactiveDataAutoConfiguration.class,
MongoReactiveHealthIndicatorAutoConfiguration.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));
}

}
Original file line number Diff line number Diff line change
@@ -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> documentMono = this.reactiveMongoTemplate.executeCommand("{ buildInfo: 1 }");
return documentMono.map(document -> up(builder, document));
}

private Health up(Health.Builder builder, Document document) {
return builder.up().withDetail("version", document.getString("version")).build();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
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
*/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary extra space.

public class MongoReactiveHealthIndicatorTest {
@Mock
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use this construct, please mock the template in the test as you've done for the rest.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

completed.

private ReactiveMongoTemplate reactiveMongoTemplate;

private MongoReactiveHealthIndicator mongoReactiveHealthIndicator;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need to be a field

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);

}

@Test
public void testMongoIsUp() throws Exception {
Document document = mock(Document.class);
given(this.reactiveMongoTemplate.executeCommand("{ buildInfo: 1 }")).willReturn(Mono.just(document));
given(document.getString("version")).willReturn("2.6.4");

this.mongoReactiveHealthIndicator = new MongoReactiveHealthIndicator(this.reactiveMongoTemplate);
Mono<Health> health = this.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() throws Exception {
given(this.reactiveMongoTemplate.executeCommand("{ buildInfo: 1 }")).willThrow(new MongoException("Connection failed"));

this.mongoReactiveHealthIndicator = new MongoReactiveHealthIndicator(this.reactiveMongoTemplate);
Mono<Health> health = this.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();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.boot.autoconfigure.data.mongo;

import java.util.Collections;

import com.mongodb.reactivestreams.client.MongoClient;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
Expand All @@ -30,7 +32,11 @@
import org.springframework.data.mongodb.ReactiveMongoDatabaseFactory;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.data.mongodb.core.SimpleReactiveMongoDatabaseFactory;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;


/**
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's reactive mongo
Expand All @@ -43,6 +49,7 @@
* to the {@literal test} database.
*
* @author Mark Paluch
* @author Yulin Qin
* @since 2.0.0
*/
@Configuration
Expand Down Expand Up @@ -73,4 +80,19 @@ public ReactiveMongoTemplate reactiveMongoTemplate(
return new ReactiveMongoTemplate(reactiveMongoDatabaseFactory, converter);
}

@Bean
@ConditionalOnMissingBean(MongoConverter.class)
public MappingMongoConverter mappingMongoConverter() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this related to this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is required by “ReactiveMongoTemplate” bean while it is missing from Spring IOC factory, that is why I provided default implementation of MongoConverter here when it is missing in context, otherwise, it will complains “unsatisfied dependencies ”

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@qinnnyul sorry I am not following. Why is it required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@snicoll In current class, we can see “reactiveMongoTemplate” method, when it constructs instance, it needs spring to inject two type of beans(one is ReactiveMongoDatabaseFactory,another is MongoConverter), however, it only has method to construct ReactiveMongoDatabaseFactory before, another is missing. You can find the similar way in MongoDataAutoConfiguration.class, but I did not want to mix it up, that is why I added default implementation of MongoConverter here, does that make sense to you? what do you recommend?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw that but this is provided by MongoDataAutoConfiguration and there isn't a reason to copy/paste that here. Can you please rework the PR so that tests also include MongoDataAutoConfiguration?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See also #12001

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@snicoll thanks for your suggestion, but I am not sure if it is good idea to make an explicit link between MongoReactiveDataAutoConfiguration and MongoDataAutoConfiguration, cause it has different conditions. Besides that, even I tried what you suggested, it throws out the following error: “Error creating bean with name 'reactiveMongoTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoReactiveDataAutoConfiguration.class]: Unsatisfied dependency expressed through method 'reactiveMongoTemplate' parameter 1; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.mongodb.core.convert.MongoConverter' available: “

MongoCustomConversions conversions = new MongoCustomConversions(Collections.emptyList());

MongoMappingContext context = new MongoMappingContext();
context.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
context.afterPropertiesSet();

MappingMongoConverter converter = new MappingMongoConverter(org.springframework.data.mongodb.core.ReactiveMongoTemplate.NO_OP_REF_RESOLVER, context);
converter.setCustomConversions(conversions);
converter.afterPropertiesSet();

return converter;
}
}