Created
August 13, 2022 13:50
-
-
Save masa2146/49a36211a3a37470d7dab9429989d628 to your computer and use it in GitHub Desktop.
embedded replica set mongo for test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.mongodb.client.MongoClient; | |
import com.mongodb.client.MongoClients; | |
import de.flapdoodle.embed.mongo.MongodExecutable; | |
import de.flapdoodle.embed.mongo.MongodProcess; | |
import de.flapdoodle.embed.mongo.MongodStarter; | |
import de.flapdoodle.embed.mongo.config.MongoCmdOptions; | |
import de.flapdoodle.embed.mongo.config.MongodConfig; | |
import de.flapdoodle.embed.mongo.config.Net; | |
import de.flapdoodle.embed.mongo.config.Storage; | |
import de.flapdoodle.embed.mongo.distribution.Version; | |
import de.flapdoodle.embed.process.runtime.Network; | |
import org.bson.Document; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.boot.test.context.TestConfiguration; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.data.mongodb.core.MongoTemplate; | |
import javax.annotation.PostConstruct; | |
import javax.annotation.PreDestroy; | |
import java.io.IOException; | |
@TestConfiguration | |
public class EmbeddedMongoConfig { | |
@Value("${spring.data.mongodb.database:my_db}") | |
private String mongoDbName; | |
@Value("${spring.data.mongodb.port:27018}") | |
private Integer port; | |
private static final String IP = "localhost"; | |
private MongoClient client; | |
private MongodExecutable mongodExecutable; | |
// This variable is defined so that different test profiles use the same mongo configuration | |
private static boolean isRunning = false; | |
@PostConstruct | |
public void createMongoDB() throws IOException { | |
String mongoUri = "mongodb://" + IP + ":" + port; | |
if (!isRunning) { | |
MongoCmdOptions cmdOptions = MongoCmdOptions.builder() | |
.useNoPrealloc(false) | |
.useSmallFiles(false) | |
.isVerbose(false) | |
.master(false) | |
.useNoJournal(false) | |
.syncDelay(0) | |
.build(); | |
MongodConfig mongodConfig = MongodConfig.builder().version(Version.Main.PRODUCTION) | |
.net(new Net(IP, port, Network.localhostIsIPv6())) | |
.replication(new Storage(null, "testRepSet", 5000)) | |
.cmdOptions(cmdOptions) | |
.putParams("maxTransactionLockRequestTimeoutMillis", "100") | |
.build(); | |
MongodStarter starter = MongodStarter.getDefaultInstance(); | |
mongodExecutable = starter.prepare(mongodConfig); | |
mongodExecutable.start(); | |
isRunning = true; | |
} | |
client = MongoClients.create(mongoUri); | |
try { | |
client.getDatabase("admin").runCommand(new Document("replSetInitiate", new Document())); | |
} catch (Exception e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
@PreDestroy | |
public void onDestroy() { | |
if (mongodExecutable != null) { | |
mongodExecutable.stop(); | |
} | |
} | |
@Bean(name = "mongoTestTemplate") | |
public MongoTemplate mongoTemplateTest() { | |
return new MongoTemplate(client, mongoDbName); | |
} | |
@Bean(destroyMethod = "stop") | |
public MongodProcess mongodProcess(MongodExecutable mongodExecutable) throws IOException { | |
return mongodExecutable.start(); | |
} | |
@Bean(destroyMethod = "stop") | |
public MongodExecutable mongodExecutable(MongodStarter mongodStarter, MongodConfig mongodConfig) { | |
return mongodStarter.prepare(mongodConfig); | |
} | |
@Bean | |
public MongodConfig mongodConfig() { | |
return MongodConfig.builder().version(Version.Main.PRODUCTION).build(); | |
} | |
@Bean | |
public MongodStarter mongodStarter() { | |
return MongodStarter.getDefaultInstance(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment