Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce configurable DownloadConfig of EmbeddedMongo for easy custo…
Browse files Browse the repository at this point in the history
…mization of 'DownloadPath'
micgme-WittGruppe committed Dec 19, 2018
1 parent 0ad72d5 commit 2481896
Showing 3 changed files with 95 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
}

}

}
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 2481896

Please sign in to comment.