Search in sources :

Example 11 with MongodConfigBuilder

use of de.flapdoodle.embed.mongo.config.MongodConfigBuilder 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 12 with MongodConfigBuilder

use of de.flapdoodle.embed.mongo.config.MongodConfigBuilder in project embedmongo-maven-plugin by joelittlejohn.

the class StartMojo method executeStart.

@Override
@SuppressWarnings("unchecked")
public void executeStart() throws MojoExecutionException, MojoFailureException {
    MongodExecutable executable;
    try {
        final List<String> mongodArgs = this.createMongodArgsList();
        final ICommandLinePostProcessor commandLinePostProcessor = new ICommandLinePostProcessor() {

            @Override
            public List<String> process(final Distribution distribution, final List<String> args) {
                args.addAll(mongodArgs);
                return args;
            }
        };
        IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder().defaults(Command.MongoD).processOutput(getOutputConfig()).artifactStore(getArtifactStore()).commandLinePostProcessor(commandLinePostProcessor).build();
        int port = getPort();
        if (isRandomPort()) {
            port = NetworkUtils.allocateRandomPort();
        }
        savePortToProjectProperties(port);
        IMongodConfig config = new MongodConfigBuilder().version(getVersion()).net(new Net(bindIp, port, NetworkUtils.localhostIsIPv6())).replication(new Storage(getDataDirectory(), null, 0)).cmdOptions(new MongoCmdOptionsBuilder().enableAuth(authEnabled).useNoJournal(!journal).useStorageEngine(storageEngine).build()).build();
        executable = MongodStarter.getInstance(runtimeConfig).prepare(config);
    } catch (DistributionException e) {
        throw new MojoExecutionException("Failed to download MongoDB distribution: " + e.withDistribution(), e);
    } catch (IOException e) {
        throw new MojoExecutionException("Unable to Config MongoDB: ", e);
    }
    try {
        MongodProcess mongod = executable.start();
        if (isWait()) {
            while (true) {
                try {
                    TimeUnit.MINUTES.sleep(5);
                } catch (InterruptedException e) {
                    break;
                }
            }
        }
        getPluginContext().put(MONGOD_CONTEXT_PROPERTY_NAME, mongod);
    } catch (IOException e) {
        throw new MojoExecutionException("Unable to start the mongod", e);
    }
}
Also used : ICommandLinePostProcessor(de.flapdoodle.embed.process.runtime.ICommandLinePostProcessor) MongodExecutable(de.flapdoodle.embed.mongo.MongodExecutable) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException) IRuntimeConfig(de.flapdoodle.embed.process.config.IRuntimeConfig) Storage(de.flapdoodle.embed.mongo.config.Storage) Distribution(de.flapdoodle.embed.process.distribution.Distribution) MongodProcess(de.flapdoodle.embed.mongo.MongodProcess) IMongodConfig(de.flapdoodle.embed.mongo.config.IMongodConfig) ArrayList(java.util.ArrayList) List(java.util.List) DistributionException(de.flapdoodle.embed.process.exceptions.DistributionException) Net(de.flapdoodle.embed.mongo.config.Net) MongodConfigBuilder(de.flapdoodle.embed.mongo.config.MongodConfigBuilder) MongoCmdOptionsBuilder(de.flapdoodle.embed.mongo.config.MongoCmdOptionsBuilder) RuntimeConfigBuilder(de.flapdoodle.embed.mongo.config.RuntimeConfigBuilder)

Example 13 with MongodConfigBuilder

use of de.flapdoodle.embed.mongo.config.MongodConfigBuilder in project main by JohnPeng739.

the class BaseTest method before.

@Before
public void before() {
    try {
        IMongodConfig config = new MongodConfigBuilder().version(Version.Main.PRODUCTION).net(new Net("localhost", 27017, Network.localhostIsIPv6())).build();
        mongodExecutable = MongodStarter.getDefaultInstance().prepare(config);
        mongod = mongodExecutable.start();
        context = new AnnotationConfigApplicationContext(DalMongodbConfig.class);
    } catch (Exception ex) {
        ex.printStackTrace();
        fail(ex.getMessage());
    }
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) DalMongodbConfig(org.mx.dal.config.DalMongodbConfig) IMongodConfig(de.flapdoodle.embed.mongo.config.IMongodConfig) Net(de.flapdoodle.embed.mongo.config.Net) MongodConfigBuilder(de.flapdoodle.embed.mongo.config.MongodConfigBuilder) Before(org.junit.Before)

Example 14 with MongodConfigBuilder

use of de.flapdoodle.embed.mongo.config.MongodConfigBuilder in project main by JohnPeng739.

the class BaseTest method before.

@Before
public void before() {
    try {
        IMongodConfig config = new MongodConfigBuilder().version(Version.Main.PRODUCTION).net(new Net("localhost", 27017, Network.localhostIsIPv6())).build();
        mongodExecutable = MongodStarter.getDefaultInstance().prepare(config);
        mongod = mongodExecutable.start();
        context = new AnnotationConfigApplicationContext(CompsRbacMongodbConfig.class);
        SessionDataStore sessionDataStore = context.getBean(SessionDataStore.class);
        assertNotNull(sessionDataStore);
        sessionDataStore.setCurrentUserCode("admin");
    } catch (Exception ex) {
        ex.printStackTrace();
        fail(ex.getMessage());
    }
}
Also used : CompsRbacMongodbConfig(org.mx.comps.rbac.mongodb.config.CompsRbacMongodbConfig) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) IMongodConfig(de.flapdoodle.embed.mongo.config.IMongodConfig) Net(de.flapdoodle.embed.mongo.config.Net) MongodConfigBuilder(de.flapdoodle.embed.mongo.config.MongodConfigBuilder) SessionDataStore(org.mx.dal.session.SessionDataStore) Before(org.junit.Before)

Example 15 with MongodConfigBuilder

use of de.flapdoodle.embed.mongo.config.MongodConfigBuilder in project engine by Lumeer.

the class EmbeddedMongoDb method createMongoConfig.

private static IMongodConfig createMongoConfig() {
    try {
        final MongodConfigBuilder builder = new MongodConfigBuilder();
        builder.version(Version.Main.V3_6).net(new Net(HOST, PORT, Network.localhostIsIPv6()));
        if (System.getProperty("os.name").toLowerCase().startsWith("mac")) {
            builder.withLaunchArgument("--storageEngine", "mmapv1");
        }
        return builder.build();
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : IOException(java.io.IOException) Net(de.flapdoodle.embed.mongo.config.Net) MongodConfigBuilder(de.flapdoodle.embed.mongo.config.MongodConfigBuilder)

Aggregations

MongodConfigBuilder (de.flapdoodle.embed.mongo.config.MongodConfigBuilder)19 Net (de.flapdoodle.embed.mongo.config.Net)19 IMongodConfig (de.flapdoodle.embed.mongo.config.IMongodConfig)14 MongoClient (com.mongodb.MongoClient)8 Storage (de.flapdoodle.embed.mongo.config.Storage)7 MongoCmdOptionsBuilder (de.flapdoodle.embed.mongo.config.MongoCmdOptionsBuilder)6 MongodStarter (de.flapdoodle.embed.mongo.MongodStarter)5 IOException (java.io.IOException)5 BeforeClass (org.junit.BeforeClass)5 Document (org.bson.Document)3 Before (org.junit.Before)3 DB (com.mongodb.DB)2 MongoDatabase (com.mongodb.client.MongoDatabase)2 GridFS (com.mongodb.gridfs.GridFS)2 GridFSInputFile (com.mongodb.gridfs.GridFSInputFile)2 MongodExecutable (de.flapdoodle.embed.mongo.MongodExecutable)2 RuntimeConfigBuilder (de.flapdoodle.embed.mongo.config.RuntimeConfigBuilder)2 Timeout (de.flapdoodle.embed.mongo.config.Timeout)2 IRuntimeConfig (de.flapdoodle.embed.process.config.IRuntimeConfig)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2