Skip to content

Commit 3188d08

Browse files
committed
Merge branch '3.2.x'
Closes spring-projectsgh-41104
2 parents e1aef9d + 31f9677 commit 3188d08

File tree

8 files changed

+177
-22
lines changed

8 files changed

+177
-22
lines changed

spring-boot-project/spring-boot-actuator/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ dependencies {
101101
testImplementation("org.springframework:spring-test")
102102
testImplementation("com.squareup.okhttp3:mockwebserver")
103103
testImplementation("org.testcontainers:junit-jupiter")
104+
testImplementation("org.testcontainers:mongodb")
104105
testImplementation("org.testcontainers:neo4j")
105106
testImplementation("org.testcontainers:testcontainers")
106107

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/data/mongo/MongoHealthIndicator.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,7 +43,7 @@ public MongoHealthIndicator(MongoTemplate mongoTemplate) {
4343

4444
@Override
4545
protected void doHealthCheck(Health.Builder builder) throws Exception {
46-
Document result = this.mongoTemplate.executeCommand("{ isMaster: 1 }");
46+
Document result = this.mongoTemplate.executeCommand("{ hello: 1 }");
4747
builder.up().withDetail("maxWireVersion", result.getInteger("maxWireVersion"));
4848
}
4949

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/data/mongo/MongoReactiveHealthIndicator.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,7 +43,7 @@ public MongoReactiveHealthIndicator(ReactiveMongoTemplate reactiveMongoTemplate)
4343

4444
@Override
4545
protected Mono<Health> doHealthCheck(Health.Builder builder) {
46-
Mono<Document> buildInfo = this.reactiveMongoTemplate.executeCommand("{ isMaster: 1 }");
46+
Mono<Document> buildInfo = this.reactiveMongoTemplate.executeCommand("{ hello: 1 }");
4747
return buildInfo.map((document) -> up(builder, document));
4848
}
4949

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.mongo;
18+
19+
import java.time.Duration;
20+
21+
import com.mongodb.ConnectionString;
22+
import com.mongodb.MongoClientSettings;
23+
import com.mongodb.MongoClientSettings.Builder;
24+
import com.mongodb.ServerApi;
25+
import com.mongodb.ServerApiVersion;
26+
import com.mongodb.client.MongoClient;
27+
import com.mongodb.client.MongoClients;
28+
import org.junit.jupiter.api.Test;
29+
import org.testcontainers.containers.MongoDBContainer;
30+
import org.testcontainers.junit.jupiter.Container;
31+
import org.testcontainers.junit.jupiter.Testcontainers;
32+
33+
import org.springframework.boot.actuate.data.mongo.MongoHealthIndicator;
34+
import org.springframework.boot.actuate.health.Health;
35+
import org.springframework.boot.actuate.health.Status;
36+
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
37+
import org.springframework.data.mongodb.core.MongoTemplate;
38+
39+
import static org.assertj.core.api.Assertions.assertThat;
40+
41+
/**
42+
* Integration tests for {@link MongoHealthIndicator}.
43+
*
44+
* @author Andy Wilkinson
45+
*/
46+
@Testcontainers(disabledWithoutDocker = true)
47+
class MongoHealthIndicatorIntegrationTests {
48+
49+
@Container
50+
static MongoDBContainer mongo = new MongoDBContainer(DockerImageNames.mongo()).withStartupAttempts(3)
51+
.withStartupTimeout(Duration.ofMinutes(2));
52+
53+
@Test
54+
void standardApi() {
55+
Health health = mongoHealth();
56+
assertThat(health.getStatus()).isEqualTo(Status.UP);
57+
}
58+
59+
@Test
60+
void strictV1Api() {
61+
Health health = mongoHealth(ServerApi.builder().strict(true).version(ServerApiVersion.V1).build());
62+
assertThat(health.getStatus()).isEqualTo(Status.UP);
63+
}
64+
65+
private Health mongoHealth() {
66+
return mongoHealth(null);
67+
}
68+
69+
private Health mongoHealth(ServerApi serverApi) {
70+
Builder settingsBuilder = MongoClientSettings.builder()
71+
.applyConnectionString(new ConnectionString(mongo.getConnectionString()));
72+
if (serverApi != null) {
73+
settingsBuilder.serverApi(serverApi);
74+
}
75+
MongoClientSettings settings = settingsBuilder.build();
76+
MongoClient mongoClient = MongoClients.create(settings);
77+
MongoHealthIndicator healthIndicator = new MongoHealthIndicator(new MongoTemplate(mongoClient, "db"));
78+
return healthIndicator.getHealth(true);
79+
}
80+
81+
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/mongo/MongoHealthIndicatorTests.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,24 +42,24 @@ void mongoIsUp() {
4242
Document commandResult = mock(Document.class);
4343
given(commandResult.getInteger("maxWireVersion")).willReturn(10);
4444
MongoTemplate mongoTemplate = mock(MongoTemplate.class);
45-
given(mongoTemplate.executeCommand("{ isMaster: 1 }")).willReturn(commandResult);
45+
given(mongoTemplate.executeCommand("{ hello: 1 }")).willReturn(commandResult);
4646
MongoHealthIndicator healthIndicator = new MongoHealthIndicator(mongoTemplate);
4747
Health health = healthIndicator.health();
4848
assertThat(health.getStatus()).isEqualTo(Status.UP);
4949
assertThat(health.getDetails()).containsEntry("maxWireVersion", 10);
5050
then(commandResult).should().getInteger("maxWireVersion");
51-
then(mongoTemplate).should().executeCommand("{ isMaster: 1 }");
51+
then(mongoTemplate).should().executeCommand("{ hello: 1 }");
5252
}
5353

5454
@Test
5555
void mongoIsDown() {
5656
MongoTemplate mongoTemplate = mock(MongoTemplate.class);
57-
given(mongoTemplate.executeCommand("{ isMaster: 1 }")).willThrow(new MongoException("Connection failed"));
57+
given(mongoTemplate.executeCommand("{ hello: 1 }")).willThrow(new MongoException("Connection failed"));
5858
MongoHealthIndicator healthIndicator = new MongoHealthIndicator(mongoTemplate);
5959
Health health = healthIndicator.health();
6060
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
6161
assertThat((String) health.getDetails().get("error")).contains("Connection failed");
62-
then(mongoTemplate).should().executeCommand("{ isMaster: 1 }");
62+
then(mongoTemplate).should().executeCommand("{ hello: 1 }");
6363
}
6464

6565
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.mongo;
18+
19+
import java.time.Duration;
20+
21+
import com.mongodb.ConnectionString;
22+
import com.mongodb.MongoClientSettings;
23+
import com.mongodb.MongoClientSettings.Builder;
24+
import com.mongodb.ServerApi;
25+
import com.mongodb.ServerApiVersion;
26+
import com.mongodb.reactivestreams.client.MongoClient;
27+
import com.mongodb.reactivestreams.client.MongoClients;
28+
import org.junit.jupiter.api.Test;
29+
import org.testcontainers.containers.MongoDBContainer;
30+
import org.testcontainers.junit.jupiter.Container;
31+
import org.testcontainers.junit.jupiter.Testcontainers;
32+
33+
import org.springframework.boot.actuate.data.mongo.MongoReactiveHealthIndicator;
34+
import org.springframework.boot.actuate.health.Health;
35+
import org.springframework.boot.actuate.health.Status;
36+
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
37+
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
38+
39+
import static org.assertj.core.api.Assertions.assertThat;
40+
41+
/**
42+
* Integration tests for {@link MongoReactiveHealthIndicator}.
43+
*
44+
* @author Andy Wilkinson
45+
*/
46+
@Testcontainers(disabledWithoutDocker = true)
47+
class MongoReactiveHealthIndicatorIntegrationTests {
48+
49+
@Container
50+
static MongoDBContainer mongo = new MongoDBContainer(DockerImageNames.mongo()).withStartupAttempts(3)
51+
.withStartupTimeout(Duration.ofMinutes(2));
52+
53+
@Test
54+
void standardApi() {
55+
Health health = mongoHealth();
56+
assertThat(health.getStatus()).isEqualTo(Status.UP);
57+
}
58+
59+
@Test
60+
void strictV1Api() {
61+
Health health = mongoHealth(ServerApi.builder().strict(true).version(ServerApiVersion.V1).build());
62+
assertThat(health.getStatus()).isEqualTo(Status.UP);
63+
}
64+
65+
private Health mongoHealth() {
66+
return mongoHealth(null);
67+
}
68+
69+
private Health mongoHealth(ServerApi serverApi) {
70+
Builder settingsBuilder = MongoClientSettings.builder()
71+
.applyConnectionString(new ConnectionString(mongo.getConnectionString()));
72+
if (serverApi != null) {
73+
settingsBuilder.serverApi(serverApi);
74+
}
75+
MongoClientSettings settings = settingsBuilder.build();
76+
MongoClient mongoClient = MongoClients.create(settings);
77+
MongoReactiveHealthIndicator healthIndicator = new MongoReactiveHealthIndicator(
78+
new ReactiveMongoTemplate(mongoClient, "db"));
79+
return healthIndicator.getHealth(true).block(Duration.ofSeconds(30));
80+
}
81+
82+
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/mongo/MongoReactiveHealthIndicatorTests.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,7 +45,7 @@ void testMongoIsUp() {
4545
Document buildInfo = mock(Document.class);
4646
given(buildInfo.getInteger("maxWireVersion")).willReturn(10);
4747
ReactiveMongoTemplate reactiveMongoTemplate = mock(ReactiveMongoTemplate.class);
48-
given(reactiveMongoTemplate.executeCommand("{ isMaster: 1 }")).willReturn(Mono.just(buildInfo));
48+
given(reactiveMongoTemplate.executeCommand("{ hello: 1 }")).willReturn(Mono.just(buildInfo));
4949
MongoReactiveHealthIndicator mongoReactiveHealthIndicator = new MongoReactiveHealthIndicator(
5050
reactiveMongoTemplate);
5151
Mono<Health> health = mongoReactiveHealthIndicator.health();
@@ -59,8 +59,7 @@ void testMongoIsUp() {
5959
@Test
6060
void testMongoIsDown() {
6161
ReactiveMongoTemplate reactiveMongoTemplate = mock(ReactiveMongoTemplate.class);
62-
given(reactiveMongoTemplate.executeCommand("{ isMaster: 1 }"))
63-
.willThrow(new MongoException("Connection failed"));
62+
given(reactiveMongoTemplate.executeCommand("{ hello: 1 }")).willThrow(new MongoException("Connection failed"));
6463
MongoReactiveHealthIndicator mongoReactiveHealthIndicator = new MongoReactiveHealthIndicator(
6564
reactiveMongoTemplate);
6665
Mono<Health> health = mongoReactiveHealthIndicator.health();

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-mongo/src/test/java/smoketest/session/mongodb/SampleSessionMongoApplicationTests.java

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -80,14 +80,6 @@ void sessionsEndpointShouldReturnUserSessions() {
8080
assertThat(sessions).hasSize(1);
8181
}
8282

83-
@Test
84-
void health() {
85-
ResponseEntity<String> entity = this.restTemplate
86-
.getForEntity("http://localhost:" + this.port + "/actuator/health", String.class);
87-
assertThat(entity.getBody()).contains("\"status\":\"UP\"");
88-
assertThat(entity.getBody()).contains("maxWireVersion");
89-
}
90-
9183
private String performLogin() {
9284
HttpHeaders headers = new HttpHeaders();
9385
headers.setAccept(Collections.singletonList(MediaType.TEXT_HTML));

0 commit comments

Comments
 (0)