From 24818961e4be54c1ee8fee2ff9a44043006f4146 Mon Sep 17 00:00:00 2001 From: michael Date: Wed, 19 Dec 2018 18:07:40 +0100 Subject: [PATCH] introduce configurable DownloadConfig of EmbeddedMongo for easy customization of 'DownloadPath' --- .../EmbeddedMongoAutoConfiguration.java | 51 ++++++++++++++----- .../embedded/EmbeddedMongoProperties.java | 36 +++++++++++++ .../EmbeddedMongoAutoConfigurationTests.java | 21 ++++++++ 3 files changed, 95 insertions(+), 13 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java index afc8d0f4edfd..0c8b9b544dc6 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java @@ -39,6 +39,7 @@ import de.flapdoodle.embed.mongo.distribution.Versions; import de.flapdoodle.embed.process.config.IRuntimeConfig; import de.flapdoodle.embed.process.config.io.ProcessOutput; +import de.flapdoodle.embed.process.config.store.IDownloadConfig; import de.flapdoodle.embed.process.distribution.GenericVersion; import de.flapdoodle.embed.process.io.Processors; import de.flapdoodle.embed.process.io.Slf4jLevel; @@ -202,24 +203,48 @@ private Map getMongoPorts(MutablePropertySources sources) { @ConditionalOnMissingBean(IRuntimeConfig.class) static class RuntimeConfigConfiguration { + private static final Logger EMBEDDED_MONGO_LOGGER = LoggerFactory + .getLogger(RuntimeConfigConfiguration.class.getPackage().getName() + + ".EmbeddedMongo"); + @Bean - public IRuntimeConfig embeddedMongoRuntimeConfig() { - Logger logger = LoggerFactory - .getLogger(getClass().getPackage().getName() + ".EmbeddedMongo"); + public IRuntimeConfig embeddedMongoRuntimeConfig( + IDownloadConfig embeddedMongoDownloadConfig) { ProcessOutput processOutput = new ProcessOutput( - Processors.logTo(logger, Slf4jLevel.INFO), - Processors.logTo(logger, Slf4jLevel.ERROR), Processors.named( - "[console>]", Processors.logTo(logger, Slf4jLevel.DEBUG))); - return new RuntimeConfigBuilder().defaultsWithLogger(Command.MongoD, logger) - .processOutput(processOutput).artifactStore(getArtifactStore(logger)) - .build(); + Processors.logTo(EMBEDDED_MONGO_LOGGER, Slf4jLevel.INFO), + Processors.logTo(EMBEDDED_MONGO_LOGGER, Slf4jLevel.ERROR), + Processors.named("[console>]", + Processors.logTo(EMBEDDED_MONGO_LOGGER, Slf4jLevel.DEBUG))); + return new RuntimeConfigBuilder() + .defaultsWithLogger(Command.MongoD, EMBEDDED_MONGO_LOGGER) + .processOutput(processOutput) + .artifactStore(getArtifactStore(embeddedMongoDownloadConfig)).build(); + } + + @Bean + @ConditionalOnMissingBean + public IDownloadConfig embeddedMongoDownloadConfig( + EmbeddedMongoProperties embeddedProperties) { + de.flapdoodle.embed.process.config.store.DownloadConfigBuilder downloadConfigBuilder = new DownloadConfigBuilder() + .defaultsForCommand(Command.MongoD) + .progressListener(new Slf4jProgressListener(EMBEDDED_MONGO_LOGGER)); + + if (embeddedProperties.getDownload().getPath() != null) { + downloadConfigBuilder + .downloadPath(embeddedProperties.getDownload().getPath()); + } + + if (embeddedProperties.getDownload().getUserAgent() != null) { + downloadConfigBuilder + .userAgent(embeddedProperties.getDownload().getUserAgent()); + } + + return downloadConfigBuilder.build(); } - private ArtifactStoreBuilder getArtifactStore(Logger logger) { + private ArtifactStoreBuilder getArtifactStore(IDownloadConfig downloadConfig) { return new ExtractedArtifactStoreBuilder().defaults(Command.MongoD) - .download(new DownloadConfigBuilder() - .defaultsForCommand(Command.MongoD) - .progressListener(new Slf4jProgressListener(logger)).build()); + .download(downloadConfig); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoProperties.java index 4582fa5ed5e0..f53a7bca2aae 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoProperties.java @@ -42,6 +42,8 @@ public class EmbeddedMongoProperties { private final Storage storage = new Storage(); + private final Download download = new Download(); + /** * Comma-separated list of features to enable. Uses the defaults of the configured * version by default. @@ -68,6 +70,10 @@ public Storage getStorage() { return this.storage; } + public Download getDownload() { + return this.download; + } + public static class Storage { /** @@ -112,4 +118,34 @@ public void setDatabaseDir(String databaseDir) { } + public static class Download { + + /** + * Root URL of the files for download. + */ + private String path; + + /** + * Sent User-Agent HTTP-Header of the download request. + */ + private String userAgent; + + public String getPath() { + return this.path; + } + + public void setPath(String path) { + this.path = path; + } + + public String getUserAgent() { + return this.userAgent; + } + + public void setUserAgent(String userAgent) { + this.userAgent = userAgent; + } + + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfigurationTests.java index bb73d990133e..9e59787987d5 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfigurationTests.java @@ -26,6 +26,7 @@ import de.flapdoodle.embed.mongo.config.Storage; import de.flapdoodle.embed.mongo.distribution.Feature; import de.flapdoodle.embed.mongo.distribution.Version; +import de.flapdoodle.embed.process.config.store.IDownloadConfig; import org.bson.Document; import org.junit.After; import org.junit.Rule; @@ -180,6 +181,26 @@ public void customReplicaSetNameIsAppliedToConfiguration() { .isEqualTo("testing"); } + @Test + public void defaultDownloadConfiguration() { + load(); + IDownloadConfig downloadConfig = this.context.getBean(IDownloadConfig.class); + assertThat(downloadConfig.getDownloadPath().getClass().getSimpleName()) + .isEqualTo("PlatformDependentDownloadPath"); + assertThat(downloadConfig.getUserAgent()).isEqualTo( + "Mozilla/5.0 (compatible; Embedded MongoDB; +https://github.com/flapdoodle-oss/embedmongo.flapdoodle.de)"); + } + + @Test + public void customDownloadConfiguration() { + load("spring.mongodb.embedded.download.path=http://test-url/", + "spring.mongodb.embedded.download.user-agent=Mozilla/5.0"); + IDownloadConfig downloadConfig = this.context.getBean(IDownloadConfig.class); + assertThat(downloadConfig.getDownloadPath().getPath(null)) + .isEqualTo("http://test-url/"); + assertThat(downloadConfig.getUserAgent()).isEqualTo("Mozilla/5.0"); + } + private void assertVersionConfiguration(String configuredVersion, String expectedVersion) { this.context = new AnnotationConfigApplicationContext();