Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring flapdoodle embedded mongo with Mongodb version 4 and replica

I am currently working on a spring boot application 2.0.3.RELEASE. I want to configure Flapdoodle MongoDb with MongoDb version 4.0 and I also want to set a single mongo instance and create replicas for it.

So far i haven't figured out the process of creating cluster and replicas using flapdoodle.

I am using

         MongodConfigBuilder().version(Version.Main.DEVELOPMENT)
        .replication(new Storage(null, null, 0))
        .build();

i have read many questions here related to this configuration but none of them is related to my problem. eg How to configure two instance mongodb use spring boot and spring data

The flapdoodle configuration has an implemetation for this but i am not sure how to access it.

https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/blob/master/src/main/java/de/flapdoodle/embed/mongo/tests/MongosSystemForTestFactory.java

Is there any way to configure it in my test class before application starts. thanks

like image 740
user2083529 Avatar asked Jul 24 '18 13:07

user2083529


People also ask

What is de Flapdoodle embed Mongo?

flapdoodle. embed. mongo: ...will provide a platform neutral way for running mongodb in unittests.

What is Spring Data MongoDB?

Spring Data for MongoDB is part of the umbrella Spring Data project which aims to provide a familiar and consistent Spring-based programming model for new datastores while retaining store-specific features and capabilities.


1 Answers

I had to start the Embedded mongo with replicaset in web Integration tests, used below code

@Configuration
public class MongoConfig {

    public static int mongodPort;
    public static String defaultHost = "localhost";
    static {
        try {
            mongodPort = Network.getFreeServerPort();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Bean
    public IMongodConfig prepareMongodConfig() throws IOException {
        IMongoCmdOptions cmdOptions = new MongoCmdOptionsBuilder()
                .useNoPrealloc(false)
                .useSmallFiles(false)
                .master(false)
                .verbose(false)
                .useNoJournal(false)
                .syncDelay(0)
                .build();

        IMongodConfig mongoConfigConfig = new MongodConfigBuilder()
                .version(Version.Main.PRODUCTION)
                .net(new Net(mongodPort, Network.localhostIsIPv6()))
                .replication(new Storage(null, "testRepSet", 5000))
                .configServer(false)
                .cmdOptions(cmdOptions)
                .build();
        return mongoConfigConfig;
    }

}

and before calling my controller I enabled the DB with replica set using below code

 Public class ITtest {
    public  void  setSystemProperty() {
            System.setProperty("spring.data.mongodb.port", String.valueOf(MongoConfig.mongodPort));
            System.setProperty("spring.data.mongodb.host", MongoConfig.defaultHost);
        }

        public static boolean isReplicaSetRun = false;

        public static void setupMongoReplica() {
            if (! isReplicaSetRun) {
                System.out.println("Starting db on port: " +MongoConfig.mongodPort);
                MongoClient client = new MongoClient(MongoConfig.defaultHost, MongoConfig.mongodPort);
                client.getDatabase("admin").runCommand(new Document("replSetInitiate", new Document()));
                client.close();
                isReplicaSetRun = true;
            }
        }

        @Test
        @Order(1)
        public void testParallel() {
            setSystemProperty();
            setupMongoReplica();
            // call web controller
      }
   }

If want to run application, then enabling of replicaset can be done in implementation of ApplicationListener

like image 90
Nitish Singla Avatar answered Sep 30 '22 08:09

Nitish Singla