|
| 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 | +} |
0 commit comments