Skip to content

Commit 2481896

Browse files
committed
introduce configurable DownloadConfig of EmbeddedMongo for easy customization of 'DownloadPath'
1 parent 0ad72d5 commit 2481896

File tree

3 files changed

+95
-13
lines changed

3 files changed

+95
-13
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import de.flapdoodle.embed.mongo.distribution.Versions;
4040
import de.flapdoodle.embed.process.config.IRuntimeConfig;
4141
import de.flapdoodle.embed.process.config.io.ProcessOutput;
42+
import de.flapdoodle.embed.process.config.store.IDownloadConfig;
4243
import de.flapdoodle.embed.process.distribution.GenericVersion;
4344
import de.flapdoodle.embed.process.io.Processors;
4445
import de.flapdoodle.embed.process.io.Slf4jLevel;
@@ -202,24 +203,48 @@ private Map<String, Object> getMongoPorts(MutablePropertySources sources) {
202203
@ConditionalOnMissingBean(IRuntimeConfig.class)
203204
static class RuntimeConfigConfiguration {
204205

206+
private static final Logger EMBEDDED_MONGO_LOGGER = LoggerFactory
207+
.getLogger(RuntimeConfigConfiguration.class.getPackage().getName()
208+
+ ".EmbeddedMongo");
209+
205210
@Bean
206-
public IRuntimeConfig embeddedMongoRuntimeConfig() {
207-
Logger logger = LoggerFactory
208-
.getLogger(getClass().getPackage().getName() + ".EmbeddedMongo");
211+
public IRuntimeConfig embeddedMongoRuntimeConfig(
212+
IDownloadConfig embeddedMongoDownloadConfig) {
209213
ProcessOutput processOutput = new ProcessOutput(
210-
Processors.logTo(logger, Slf4jLevel.INFO),
211-
Processors.logTo(logger, Slf4jLevel.ERROR), Processors.named(
212-
"[console>]", Processors.logTo(logger, Slf4jLevel.DEBUG)));
213-
return new RuntimeConfigBuilder().defaultsWithLogger(Command.MongoD, logger)
214-
.processOutput(processOutput).artifactStore(getArtifactStore(logger))
215-
.build();
214+
Processors.logTo(EMBEDDED_MONGO_LOGGER, Slf4jLevel.INFO),
215+
Processors.logTo(EMBEDDED_MONGO_LOGGER, Slf4jLevel.ERROR),
216+
Processors.named("[console>]",
217+
Processors.logTo(EMBEDDED_MONGO_LOGGER, Slf4jLevel.DEBUG)));
218+
return new RuntimeConfigBuilder()
219+
.defaultsWithLogger(Command.MongoD, EMBEDDED_MONGO_LOGGER)
220+
.processOutput(processOutput)
221+
.artifactStore(getArtifactStore(embeddedMongoDownloadConfig)).build();
222+
}
223+
224+
@Bean
225+
@ConditionalOnMissingBean
226+
public IDownloadConfig embeddedMongoDownloadConfig(
227+
EmbeddedMongoProperties embeddedProperties) {
228+
de.flapdoodle.embed.process.config.store.DownloadConfigBuilder downloadConfigBuilder = new DownloadConfigBuilder()
229+
.defaultsForCommand(Command.MongoD)
230+
.progressListener(new Slf4jProgressListener(EMBEDDED_MONGO_LOGGER));
231+
232+
if (embeddedProperties.getDownload().getPath() != null) {
233+
downloadConfigBuilder
234+
.downloadPath(embeddedProperties.getDownload().getPath());
235+
}
236+
237+
if (embeddedProperties.getDownload().getUserAgent() != null) {
238+
downloadConfigBuilder
239+
.userAgent(embeddedProperties.getDownload().getUserAgent());
240+
}
241+
242+
return downloadConfigBuilder.build();
216243
}
217244

218-
private ArtifactStoreBuilder getArtifactStore(Logger logger) {
245+
private ArtifactStoreBuilder getArtifactStore(IDownloadConfig downloadConfig) {
219246
return new ExtractedArtifactStoreBuilder().defaults(Command.MongoD)
220-
.download(new DownloadConfigBuilder()
221-
.defaultsForCommand(Command.MongoD)
222-
.progressListener(new Slf4jProgressListener(logger)).build());
247+
.download(downloadConfig);
223248
}
224249

225250
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoProperties.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public class EmbeddedMongoProperties {
4242

4343
private final Storage storage = new Storage();
4444

45+
private final Download download = new Download();
46+
4547
/**
4648
* Comma-separated list of features to enable. Uses the defaults of the configured
4749
* version by default.
@@ -68,6 +70,10 @@ public Storage getStorage() {
6870
return this.storage;
6971
}
7072

73+
public Download getDownload() {
74+
return this.download;
75+
}
76+
7177
public static class Storage {
7278

7379
/**
@@ -112,4 +118,34 @@ public void setDatabaseDir(String databaseDir) {
112118

113119
}
114120

121+
public static class Download {
122+
123+
/**
124+
* Root URL of the files for download.
125+
*/
126+
private String path;
127+
128+
/**
129+
* Sent User-Agent HTTP-Header of the download request.
130+
*/
131+
private String userAgent;
132+
133+
public String getPath() {
134+
return this.path;
135+
}
136+
137+
public void setPath(String path) {
138+
this.path = path;
139+
}
140+
141+
public String getUserAgent() {
142+
return this.userAgent;
143+
}
144+
145+
public void setUserAgent(String userAgent) {
146+
this.userAgent = userAgent;
147+
}
148+
149+
}
150+
115151
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfigurationTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import de.flapdoodle.embed.mongo.config.Storage;
2727
import de.flapdoodle.embed.mongo.distribution.Feature;
2828
import de.flapdoodle.embed.mongo.distribution.Version;
29+
import de.flapdoodle.embed.process.config.store.IDownloadConfig;
2930
import org.bson.Document;
3031
import org.junit.After;
3132
import org.junit.Rule;
@@ -180,6 +181,26 @@ public void customReplicaSetNameIsAppliedToConfiguration() {
180181
.isEqualTo("testing");
181182
}
182183

184+
@Test
185+
public void defaultDownloadConfiguration() {
186+
load();
187+
IDownloadConfig downloadConfig = this.context.getBean(IDownloadConfig.class);
188+
assertThat(downloadConfig.getDownloadPath().getClass().getSimpleName())
189+
.isEqualTo("PlatformDependentDownloadPath");
190+
assertThat(downloadConfig.getUserAgent()).isEqualTo(
191+
"Mozilla/5.0 (compatible; Embedded MongoDB; +https://github.com/flapdoodle-oss/embedmongo.flapdoodle.de)");
192+
}
193+
194+
@Test
195+
public void customDownloadConfiguration() {
196+
load("spring.mongodb.embedded.download.path=http://test-url/",
197+
"spring.mongodb.embedded.download.user-agent=Mozilla/5.0");
198+
IDownloadConfig downloadConfig = this.context.getBean(IDownloadConfig.class);
199+
assertThat(downloadConfig.getDownloadPath().getPath(null))
200+
.isEqualTo("http://test-url/");
201+
assertThat(downloadConfig.getUserAgent()).isEqualTo("Mozilla/5.0");
202+
}
203+
183204
private void assertVersionConfiguration(String configuredVersion,
184205
String expectedVersion) {
185206
this.context = new AnnotationConfigApplicationContext();

0 commit comments

Comments
 (0)