|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.data.mongo; |
18 | 18 |
|
| 19 | +import java.time.Duration; |
| 20 | + |
19 | 21 | import com.mongodb.ConnectionString; |
| 22 | +import com.mongodb.reactivestreams.client.MongoCollection; |
| 23 | +import com.mongodb.reactivestreams.client.gridfs.GridFSBucket; |
20 | 24 | import org.junit.jupiter.api.Test; |
| 25 | +import reactor.core.publisher.Mono; |
21 | 26 |
|
22 | 27 | import org.springframework.boot.autoconfigure.AutoConfigurations; |
23 | 28 | import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; |
@@ -68,20 +73,26 @@ void whenGridFsDatabaseIsConfiguredThenGridFsTemplateUsesIt() { |
68 | 73 | } |
69 | 74 |
|
70 | 75 | @Test |
| 76 | + @SuppressWarnings("unchecked") |
71 | 77 | void usesMongoConnectionDetailsIfAvailable() { |
72 | 78 | this.contextRunner.withUserConfiguration(ConnectionDetailsConfiguration.class).run((context) -> { |
73 | 79 | assertThat(grisFsTemplateDatabaseName(context)).isEqualTo("grid-database-1"); |
74 | 80 | ReactiveGridFsTemplate template = context.getBean(ReactiveGridFsTemplate.class); |
75 | | - assertThat(template).hasFieldOrPropertyWithValue("bucket", "connection-details-bucket"); |
| 81 | + GridFSBucket bucket = ((Mono<GridFSBucket>) ReflectionTestUtils.getField(template, "bucketSupplier")) |
| 82 | + .block(Duration.ofSeconds(30)); |
| 83 | + assertThat(bucket.getBucketName()).isEqualTo("connection-details-bucket"); |
76 | 84 | }); |
77 | 85 | } |
78 | 86 |
|
79 | 87 | @Test |
| 88 | + @SuppressWarnings("unchecked") |
80 | 89 | void whenGridFsBucketIsConfiguredThenGridFsTemplateUsesIt() { |
81 | 90 | this.contextRunner.withPropertyValues("spring.data.mongodb.gridfs.bucket:test-bucket").run((context) -> { |
82 | 91 | assertThat(context).hasSingleBean(ReactiveGridFsTemplate.class); |
83 | 92 | ReactiveGridFsTemplate template = context.getBean(ReactiveGridFsTemplate.class); |
84 | | - assertThat(template).hasFieldOrPropertyWithValue("bucket", "test-bucket"); |
| 93 | + GridFSBucket bucket = ((Mono<GridFSBucket>) ReflectionTestUtils.getField(template, "bucketSupplier")) |
| 94 | + .block(Duration.ofSeconds(30)); |
| 95 | + assertThat(bucket.getBucketName()).isEqualTo("test-bucket"); |
85 | 96 | }); |
86 | 97 | } |
87 | 98 |
|
@@ -150,12 +161,14 @@ void contextFailsWhenDatabaseNotSet() { |
150 | 161 | .run((context) -> assertThat(context).getFailure().hasMessageContaining("Database name must not be empty")); |
151 | 162 | } |
152 | 163 |
|
| 164 | + @SuppressWarnings("unchecked") |
153 | 165 | private String grisFsTemplateDatabaseName(AssertableApplicationContext context) { |
154 | 166 | assertThat(context).hasSingleBean(ReactiveGridFsTemplate.class); |
155 | 167 | ReactiveGridFsTemplate template = context.getBean(ReactiveGridFsTemplate.class); |
156 | | - ReactiveMongoDatabaseFactory factory = (ReactiveMongoDatabaseFactory) ReflectionTestUtils.getField(template, |
157 | | - "dbFactory"); |
158 | | - return factory.getMongoDatabase().block().getName(); |
| 168 | + GridFSBucket bucket = ((Mono<GridFSBucket>) ReflectionTestUtils.getField(template, "bucketSupplier")) |
| 169 | + .block(Duration.ofSeconds(30)); |
| 170 | + MongoCollection<?> collection = (MongoCollection<?>) ReflectionTestUtils.getField(bucket, "filesCollection"); |
| 171 | + return collection.getNamespace().getDatabaseName(); |
159 | 172 | } |
160 | 173 |
|
161 | 174 | @Configuration(proxyBeanMethods = false) |
|
0 commit comments