Java Code Examples for de.flapdoodle.embed.mongo.MongodStarter#prepare()

The following examples show how to use de.flapdoodle.embed.mongo.MongodStarter#prepare() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EmbeddedMongoHelper.java    From edison-microservice with Apache License 2.0 6 votes vote down vote up
public static void startMongoDB() throws IOException {
    if (!started.compareAndSet(false, true)) {
        throw new RuntimeException("Embedded mongo already running, call stopMongoDB before starting it again!");
    }
    final String bindIp = "localhost";
    try {
        final int port = Network.getFreeServerPort();
        final IMongodConfig mongodConfig = new MongodConfigBuilder()
                .version(Version.Main.PRODUCTION)
                .net(new Net(bindIp, port, Network.localhostIsIPv6()))
                .build();
        final MongodStarter runtime = MongodStarter.getInstance(new RuntimeConfigBuilder()
                .defaultsWithLogger(Command.MongoD, LOG)
                .build());
        mongodExecutable = runtime.prepare(mongodConfig, Distribution.detectFor(Version.Main.PRODUCTION));
        mongodProcess = mongodExecutable.start();
        mongoClient = new MongoClient(bindIp, port);
    } catch (final IOException e) {
        stopMongoDB();
        throw e;
    }
}
 
Example 2
Source File: EmbeddedClient.java    From graviteeio-access-management with Apache License 2.0 6 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {

    final IMongodConfig mongodConfig = new MongodConfigBuilder()
            .version(Version.Main.PRODUCTION).build();

    IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
            .defaultsWithLogger(Command.MongoD, logger)
            .processOutput(ProcessOutput.getDefaultInstanceSilent())
            .build();

    MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);

    MongodExecutable mongodExecutable = runtime.prepare(mongodConfig);
    mongod = mongodExecutable.start();

    // cluster configuration
    ClusterSettings clusterSettings = ClusterSettings.builder().hosts(Collections.singletonList(new ServerAddress(mongodConfig.net().getServerAddress().getHostName(), mongodConfig.net().getPort()))).build();
    // codec configuration
    CodecRegistry pojoCodecRegistry = fromRegistries(MongoClients.getDefaultCodecRegistry(),
            fromProviders(PojoCodecProvider.builder().automatic(true).build()));

    MongoClientSettings settings = MongoClientSettings.builder().clusterSettings(clusterSettings).codecRegistry(pojoCodecRegistry).writeConcern(WriteConcern.ACKNOWLEDGED).build();
    mongoClient = MongoClients.create(settings);
    mongoDatabase = mongoClient.getDatabase(databaseName);
}
 
Example 3
Source File: EmbeddedClient.java    From graviteeio-access-management with Apache License 2.0 6 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
    final IMongodConfig mongodConfig = new MongodConfigBuilder()
            .version(Version.Main.PRODUCTION).build();

    IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
            .defaultsWithLogger(Command.MongoD, logger)
            .processOutput(ProcessOutput.getDefaultInstanceSilent())
            .build();

    MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);

    MongodExecutable mongodExecutable = runtime.prepare(mongodConfig);
    mongod = mongodExecutable.start();

    // cluster configuration
    ClusterSettings clusterSettings = ClusterSettings.builder().hosts(Collections.singletonList(new ServerAddress(mongodConfig.net().getServerAddress().getHostName(), mongodConfig.net().getPort()))).build();
    // codec configuration
    CodecRegistry pojoCodecRegistry = fromRegistries(MongoClients.getDefaultCodecRegistry(),
            fromProviders(PojoCodecProvider.builder().automatic(true).build()));

    MongoClientSettings settings = MongoClientSettings.builder().clusterSettings(clusterSettings).codecRegistry(pojoCodecRegistry).writeConcern(WriteConcern.ACKNOWLEDGED).build();
    mongoClient = MongoClients.create(settings);
    mongoDatabase = mongoClient.getDatabase(databaseName);
}
 
Example 4
Source File: NonReactiveMongoClientTestConfiguration.java    From mongodb-aggregate-query-support with Apache License 2.0 5 votes vote down vote up
public NonReactiveMongoClientTestConfiguration() throws IOException {

    Command command = Command.MongoD;
    IDownloadConfig downloadConfig = new Mongo42xDownloadConfigBuilder().defaultsForCommand(command)
                                                                        .progressListener(new Slf4jProgressListener(LOGGER))
                                                                        .build();
    IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder().defaults(command)
                                                             .artifactStore(new ExtractedArtifactStoreBuilder()
                                                                                .defaults(command)
                                                                                .download(downloadConfig))
                                                             .build();
    final MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);
    mongodExecutable = runtime.prepare(newMongodConfig(MongoDbVersion.V4_2_4));
    startMongodExecutable();
  }
 
Example 5
Source File: MyFirstVerticleTest.java    From my-vertx-first-app with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void initialize() throws IOException {
  MongodStarter starter = MongodStarter.getDefaultInstance();

  IMongodConfig mongodConfig = new MongodConfigBuilder()
      .version(Version.Main.PRODUCTION)
      .net(new Net(MONGO_PORT, Network.localhostIsIPv6()))
      .build();

  MongodExecutable mongodExecutable = starter.prepare(mongodConfig);
  MONGO = mongodExecutable.start();
}
 
Example 6
Source File: SimpleEmbedMongo.java    From eagle with Apache License 2.0 5 votes vote down vote up
public void start() throws Exception {
    MongodStarter starter = MongodStarter.getDefaultInstance();
    mongodExe = starter.prepare(new MongodConfigBuilder().version(Version.V3_2_1)
            .net(new Net(27017, Network.localhostIsIPv6())).build());
    mongod = mongodExe.start();

    client = new MongoClient("localhost");
}
 
Example 7
Source File: MongoImplTest.java    From eagle with Apache License 2.0 5 votes vote down vote up
public static void before() {
    try {
        MongodStarter starter = MongodStarter.getDefaultInstance();
        mongodExe = starter.prepare(new MongodConfigBuilder().version(Version.V3_2_1)
            .net(new Net(27017, Network.localhostIsIPv6())).build());
        mongod = mongodExe.start();
    } catch (Exception e) {
        LOG.error("start embed mongod failed, assume some external mongo running. continue run test!", e);
    }
}
 
Example 8
Source File: MyFirstVerticleTest.java    From df_data_service with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void initialize() throws IOException {
  MongodStarter starter = MongodStarter.getDefaultInstance();

  IMongodConfig mongodConfig = new MongodConfigBuilder()
      .version(Version.Main.PRODUCTION)
      .net(new Net(MONGO_PORT, Network.localhostIsIPv6()))
      .build();

  MongodExecutable mongodExecutable = starter.prepare(mongodConfig);
  MONGO = mongodExecutable.start();
}
 
Example 9
Source File: MongodManager.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
private void startMongo(final List<IMongodConfig> mongodConfigList) throws IOException {
    // @formatter:off
    final ProcessOutput processOutput = new ProcessOutput(
            logTo(LOGGER, Slf4jLevel.INFO),
            logTo(LOGGER, Slf4jLevel.ERROR),
            named("[console>]", logTo(LOGGER, Slf4jLevel.DEBUG)));

    final IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
            .defaultsWithLogger(Command.MongoD,LOGGER)
            .processOutput(processOutput)
            .artifactStore(new ExtractedArtifactStoreBuilder()
                .defaults(Command.MongoD)
                .download(new DownloadConfigBuilder()
                    .defaultsForCommand(Command.MongoD)
                    .progressListener(new Slf4jProgressListener(LOGGER))
                    .build()))
            .build();
    // @formatter:on
    final MongodStarter starter = MongodStarter.getInstance(runtimeConfig);

    for (final IMongodConfig mongodConfig : mongodConfigList) {
        final MongodExecutable mongodExecutable = starter.prepare(mongodConfig);
        final MongodProcess mongod = mongodExecutable.start();

        mongoProcesses.put(mongod, mongodExecutable);
    }
}
 
Example 10
Source File: PolyglotUsageTest.java    From vertx-service-discovery with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void beforeClass() throws Exception {
  MongodStarter runtime = MongodStarter.getDefaultInstance();
  mongodExe = runtime.prepare(
    new MongodConfigBuilder().version(Version.V3_3_1)
      .net(new Net(12345, Network.localhostIsIPv6()))
      .build());
  MongodProcess process = mongodExe.start();
  await().until(() -> process != null);
}
 
Example 11
Source File: MongoDBSessionDataStorageTest.java    From pippo with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setUpClass() throws IOException {
    Integer port = Network.getFreeServerPort();
    MongodStarter starter = MongodStarter.getDefaultInstance();
    mongodExe = starter.prepare(
            new MongodConfigBuilder()
            .version(Version.Main.PRODUCTION)
            .net(new Net(port, Network.localhostIsIPv6()))
            .build());
    mongod = mongodExe.start();
    mongoClient = new MongoClient(HOST, port);
}
 
Example 12
Source File: DevelopmentConfig.java    From entref-spring-boot with MIT License 5 votes vote down vote up
/**
 * Attempts to start a mongo instance, using embedMongo
 * @param bind the net info to bind to
 * @return the instance
 * @throws IOException indicates a failure
 */
private MongodExecutable setupMongoEmbed(Net bind) throws IOException {
    MongodStarter starter;
    starter = MongodStarter.getDefaultInstance();

    IMongodConfig mongodConfig = new MongodConfigBuilder()
            .version(Version.Main.DEVELOPMENT)
            .net(bind)
            .build();

    MongodExecutable mongodExecutable = starter.prepare(mongodConfig);
    MongodProcess mongod = mongodExecutable.start();
    return mongodExecutable;
}
 
Example 13
Source File: ReactiveMongoClientTestConfiguration.java    From mongodb-aggregate-query-support with Apache License 2.0 5 votes vote down vote up
public ReactiveMongoClientTestConfiguration() throws IOException {

    Command command = Command.MongoD;
    IDownloadConfig downloadConfig = new Mongo42xDownloadConfigBuilder().defaultsForCommand(command)
                                                                        .progressListener(new Slf4jProgressListener(LOGGER))
                                                                        .build();
    IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder().defaults(command)
                                                             .artifactStore(new ExtractedArtifactStoreBuilder()
                                                                                .defaults(command)
                                                                                .download(downloadConfig))
                                                             .build();
    final MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);
    mongodExecutable = runtime.prepare(newMongodConfig(MongoDbVersion.V4_2_4));
    startMongodExecutable();
  }
 
Example 14
Source File: ManualEmbeddedMongoDbIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@BeforeEach
void setup() throws Exception {
    String ip = "localhost";
    int randomPort = SocketUtils.findAvailableTcpPort();

    IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.PRODUCTION)
        .net(new Net(ip, randomPort, Network.localhostIsIPv6()))
        .build();

    MongodStarter starter = MongodStarter.getDefaultInstance();
    mongodExecutable = starter.prepare(mongodConfig);
    mongodExecutable.start();
    mongoTemplate = new MongoTemplate(new MongoClient(ip, randomPort), "test");
}
 
Example 15
Source File: MongoDbResource.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
private static MongodExecutable configureMongoDb(final String bindIp,
        final int mongoDbPort,
        final IProxyFactory proxyFactory,
        final Logger logger) throws IOException {

    final Command command = Command.MongoD;

    final ProcessOutput processOutput;
    if (logger != null) {
        processOutput = ProcessOutput.getInstance("mongod", logger);
    } else {
        processOutput = ProcessOutput.getDefaultInstanceSilent();
    }

    final MongodStarter mongodStarter = MongodStarter.getInstance(new RuntimeConfigBuilder()
            .defaults(command)
            .processOutput(processOutput)
            .artifactStore(new ExtractedArtifactStoreBuilder()
                    .defaults(command)
                    .download(new DownloadConfigBuilder()
                            .defaultsForCommand(command)
                            .proxyFactory(proxyFactory)
                            .progressListener(new StandardConsoleProgressListener())
                            .build()))
            .build());

    return mongodStarter.prepare(new MongodConfigBuilder()
            .net(new Net(bindIp, mongoDbPort, false))
            .version(Version.Main.V3_6)
            .cmdOptions(new MongoCmdOptionsBuilder()
                    .useStorageEngine("wiredTiger")
                    .useNoJournal(false)
                    .build())
            .build());
}
 
Example 16
Source File: MongosSystemForTestFactory.java    From spring-data-examples with Apache License 2.0 5 votes vote down vote up
private void initializeConfigServer(IMongodConfig config) throws Exception {
	if (!config.isConfigServer()) {
		throw new Exception(
				"Mongo configuration is not a defined for a config server.");
	}
	MongodStarter starter = MongodStarter.getDefaultInstance();
	MongodExecutable mongodExe = starter.prepare(config);
	MongodProcess process = mongodExe.start();
	mongodProcessList.add(process);
}
 
Example 17
Source File: AbstractMongoDbTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void startEmbeddedMongoDb() throws IOException {
    MongodStarter starter = getDefaultInstance();

    port = Network.getFreeServerPort();

    IMongodConfig mongodConfig = new MongodConfigBuilder()
            .version(Version.Main.PRODUCTION)
            .net(new Net(HOST, port, Network.localhostIsIPv6()))
            .build();
    mongodExecutable = starter.prepare(mongodConfig);
    mongodExecutable.start();
}
 
Example 18
Source File: MongoDBITBase.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
public void startDB() throws Exception {
    MongodStarter starter = MongodStarter.getDefaultInstance();

    String bindIp = "localhost";
    int port = 27018;

    IMongodConfig mongodConfig = new MongodConfigBuilder()
            .version(Version.Main.PRODUCTION)
            .net(new Net(bindIp, port, Network.localhostIsIPv6()))
            .build();

    MongodExecutable mongodExecutable = null;

    mongodExecutable = starter.prepare(mongodConfig);

    //give time for previous DB close to finish and port to be released"
    Thread.sleep(200L);
    mongod = mongodExecutable.start();
    setClient();
}
 
Example 19
Source File: MongoJavaRDDFT.java    From deep-spark with Apache License 2.0 4 votes vote down vote up
@BeforeSuite
public static void init() throws IOException {
    Command command = Command.MongoD;

    try {
        Files.forceDelete(new File(DB_FOLDER_NAME));
    } catch (Exception e) {

    }

    new File(DB_FOLDER_NAME).mkdirs();

    IMongodConfig mongodConfig = new MongodConfigBuilder()
            .version(Version.Main.PRODUCTION)
            .configServer(false)
            .replication(new Storage(DB_FOLDER_NAME, null, 0))
            .net(new Net(PORT, Network.localhostIsIPv6()))
            .cmdOptions(new MongoCmdOptionsBuilder()
                    .syncDelay(10)
                    .useNoPrealloc(true)
                    .useSmallFiles(true)
                    .useNoJournal(true)
                    .build())
            .build();

    IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
            .defaults(command)
            .artifactStore(new ArtifactStoreBuilder()
                    .defaults(command)
                    .download(new DownloadConfigBuilder()
                            .defaultsForCommand(command)
                            .downloadPath("https://s3-eu-west-1.amazonaws.com/stratio-mongodb-distribution/")))
            .build();

    MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);

    mongodExecutable = null;

    mongodExecutable = runtime.prepare(mongodConfig);

    mongod = mongodExecutable.start();

}
 
Example 20
Source File: EmbeddedMongoFactory.java    From rya with Apache License 2.0 2 votes vote down vote up
/**
 * Create the testing utility using the specified version of MongoDB.
 *
 * @param version
 *            version of MongoDB.
 */
private EmbeddedMongoFactory(final IFeatureAwareVersion version) throws IOException {
    final MongodStarter runtime = MongodStarter.getInstance(new RuntimeConfigBuilder().defaultsWithLogger(Command.MongoD, logger).build());
    mongodExecutable = runtime.prepare(newMongodConfig(version));
    mongodProcess = mongodExecutable.start();
}