Search in sources :

Example 6 with MongodStarter

use of de.flapdoodle.embed.mongo.MongodStarter in project pac4j by pac4j.

the class MongoServer method start.

public void start(final int port) {
    MongodStarter starter = MongodStarter.getDefaultInstance();
    try {
        IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.PRODUCTION).net(new Net(port, Network.localhostIsIPv6())).build();
        mongodExecutable = starter.prepare(mongodConfig);
        mongodExecutable.start();
        // populate
        final MongoClient mongo = new MongoClient("localhost", port);
        final MongoDatabase db = mongo.getDatabase("users");
        db.createCollection("users");
        final MongoCollection<Document> collection = db.getCollection("users");
        final String password = PASSWORD_ENCODER.encode(PASSWORD);
        Map<String, Object> properties1 = new HashMap<>();
        properties1.put(USERNAME, GOOD_USERNAME);
        properties1.put(PASSWORD, password);
        properties1.put(FIRSTNAME, FIRSTNAME_VALUE);
        collection.insertOne(new Document(properties1));
        Map<String, Object> properties2 = new HashMap<>();
        properties2.put(USERNAME, MULTIPLE_USERNAME);
        properties2.put(PASSWORD, password);
        collection.insertOne(new Document(properties2));
        Map<String, Object> properties3 = new HashMap<>();
        properties3.put(USERNAME, MULTIPLE_USERNAME);
        properties3.put(PASSWORD, password);
        collection.insertOne(new Document(properties3));
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) Document(org.bson.Document) MongoClient(com.mongodb.MongoClient) MongodStarter(de.flapdoodle.embed.mongo.MongodStarter) IMongodConfig(de.flapdoodle.embed.mongo.config.IMongodConfig) Net(de.flapdoodle.embed.mongo.config.Net) MongodConfigBuilder(de.flapdoodle.embed.mongo.config.MongodConfigBuilder) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 7 with MongodStarter

use of de.flapdoodle.embed.mongo.MongodStarter in project modesti by jlsalmon.

the class MongoConfig method embeddedMongo.

private Mongo embeddedMongo() throws IOException {
    int port = 12345;
    MongodConfigBuilder builder = new MongodConfigBuilder().version(Version.Main.PRODUCTION).net(new Net(port, Network.localhostIsIPv6()));
    if (env.containsProperty("mongodb.persistent") && env.getProperty("mongodb.persistent", Boolean.class).equals(true)) {
        builder.replication(new Storage("/tmp/mongodb-embedded", null, 0));
    }
    IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder().defaultsWithLogger(Command.MongoD, log).processOutput(ProcessOutput.getDefaultInstanceSilent()).build();
    MongodStarter runtime = MongodStarter.getInstance(runtimeConfig);
    MongodExecutable mongodExecutable = runtime.prepare(builder.build());
    mongodExecutable.start();
    return new MongoClient("localhost", port);
}
Also used : MongoClient(com.mongodb.MongoClient) MongodExecutable(de.flapdoodle.embed.mongo.MongodExecutable) MongodStarter(de.flapdoodle.embed.mongo.MongodStarter) IRuntimeConfig(de.flapdoodle.embed.process.config.IRuntimeConfig)

Example 8 with MongodStarter

use of de.flapdoodle.embed.mongo.MongodStarter in project spring-boot by spring-projects.

the class EmbeddedMongoAutoConfiguration method embeddedMongoServer.

@Bean(initMethod = "start", destroyMethod = "stop")
@ConditionalOnMissingBean
public MongodExecutable embeddedMongoServer(MongodConfig mongodConfig, RuntimeConfig runtimeConfig, ApplicationContext context) {
    Integer configuredPort = this.properties.getPort();
    if (configuredPort == null || configuredPort == 0) {
        setEmbeddedPort(context, mongodConfig.net().getPort());
    }
    MongodStarter mongodStarter = getMongodStarter(runtimeConfig);
    return mongodStarter.prepare(mongodConfig);
}
Also used : MongodStarter(de.flapdoodle.embed.mongo.MongodStarter) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ReactiveMongoClientFactoryBean(org.springframework.data.mongodb.core.ReactiveMongoClientFactoryBean) MongoClientFactoryBean(org.springframework.data.mongodb.core.MongoClientFactoryBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

MongodStarter (de.flapdoodle.embed.mongo.MongodStarter)8 IMongodConfig (de.flapdoodle.embed.mongo.config.IMongodConfig)5 MongodConfigBuilder (de.flapdoodle.embed.mongo.config.MongodConfigBuilder)5 Net (de.flapdoodle.embed.mongo.config.Net)5 MongoClient (com.mongodb.MongoClient)3 MongodExecutable (de.flapdoodle.embed.mongo.MongodExecutable)2 IRuntimeConfig (de.flapdoodle.embed.process.config.IRuntimeConfig)2 IOException (java.io.IOException)2 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)2 Bean (org.springframework.context.annotation.Bean)2 MongoClientFactoryBean (org.springframework.data.mongodb.core.MongoClientFactoryBean)2 ReactiveMongoClientFactoryBean (org.springframework.data.mongodb.core.ReactiveMongoClientFactoryBean)2 MongoDatabase (com.mongodb.client.MongoDatabase)1 RuntimeConfigBuilder (de.flapdoodle.embed.mongo.config.RuntimeConfigBuilder)1 HashMap (java.util.HashMap)1 Document (org.bson.Document)1