bwaldvogel/mongo-java-server


Fake implementation of MongoDB in Java that speaks the wire protocol.

License: BSD-3-Clause

Language: Java

Keywords: in-memory, java, mongodb, netty


Build Status Maven Central Coverage Status BSD 3-Clause License Donate

MongoDB Java Server

Fake implementation of the core MongoDB server in Java that can be used for integration tests.

Think of H2/HSQLDB/SQLite but for MongoDB.

The MongoDB Wire Protocol is implemented with Netty. Different backends are possible and can be extended.

In-Memory backend

The in-memory backend is the default backend that is typically used to fake MongoDB for integration tests. It supports most CRUD operations, commands and the aggregation framework. Some features are not yet implemented, such as full-text search or map/reduce.

Add the following Maven dependency to your project:

<dependency>
    <groupId>de.bwaldvogel</groupId>
    <artifactId>mongo-java-server</artifactId>
    <version>1.18.1</version>
</dependency>

Example

public class SimpleTest {

    private MongoCollection<Document> collection;
    private MongoClient client;
    private MongoServer server;

    @Before
    public void setUp() {
        server = new MongoServer(new MemoryBackend());

        // optionally: server.enableSsl(key, keyPassword, certificate);

        // bind on a random local port
        InetSocketAddress serverAddress = server.bind();

        client = new MongoClient(new ServerAddress(serverAddress));
        collection = client.getDatabase("testdb").getCollection("testcollection");
    }

    @After
    public void tearDown() {
        client.close();
        server.shutdown();
    }

    @Test
    public void testSimpleInsertQuery() throws Exception {
        assertEquals(0, collection.count());

        // creates the database and collection in memory and insert the object
        Document obj = new Document("_id", 1).append("key", "value");
        collection.insertOne(obj);

        assertEquals(1, collection.count());
        assertEquals(obj, collection.find().first());
    }

}

Example with SpringBoot

@RunWith(SpringRunner.class)
@SpringBootTest(classes={SimpleSpringBootTest.TestConfiguration.class})
public class SimpleSpringBootTest {

    @Autowired private MyRepository repository;

    @Before
    public void setUp() {
        // initialize your repository with some test data
        repository.deleteAll();
        repository.save(...);
    }

    @Test
    public void testMyRepository() {
        // test your repository ...
        ...
    }

    @Configuration
    @EnableMongoRepositories(basePackageClasses={MyRepository.class})
    protected static class TestConfiguration {
        @Bean
        public MongoTemplate mongoTemplate(MongoClient mongoClient) {
            return new MongoTemplate(mongoDbFactory(mongoClient));
        }

        @Bean
        public MongoDbFactory mongoDbFactory(MongoClient mongoClient) {
            return new SimpleMongoDbFactory(mongoClient, "test");
        }

        @Bean(destroyMethod="shutdown")
        public MongoServer mongoServer() {
            MongoServer mongoServer = new MongoServer(new MemoryBackend());
            mongoServer.bind();
            return mongoServer;
        }

        @Bean(destroyMethod="close")
        public MongoClient mongoClient(MongoServer mongoServer) {
            return new MongoClient(new ServerAddress(mongoServer.getLocalAddress()));
        }
    }
}

H2 MVStore backend

The H2 MVStore backend connects the server to a MVStore that can either be in-memory or on-disk.

<dependency>
    <groupId>de.bwaldvogel</groupId>
    <artifactId>mongo-java-server-h2-backend</artifactId>
    <version>1.18.1</version>
</dependency>

Example

public class Application {

    public static void main(String[] args) throws Exception {
        MongoServer server = new MongoServer(new H2Backend("database.mv"));
        server.bind("localhost", 27017);
    }

}

PostgreSQL backend

The PostgreSQL backend is a proof-of-concept implementation that connects the server to a database in a running PostgreSQL 9.5+ instance. Each MongoDB database is mapped to a schema in Postgres and each MongoDB collection is stored as a table.

<dependency>
    <groupId>de.bwaldvogel</groupId>
    <artifactId>mongo-java-server-postgresql-backend</artifactId>
    <version>1.18.1</version>
</dependency>

For integration tests, a PostgreSQL instance can be created in a docker container:

$ docker-compose up -d

or manually with:

$ docker run --name postgres-mongo-java-server-test \
             -p 5432:5432 \
             --tmpfs /var/lib/postgresql/data:rw \
             -e POSTGRES_USER=mongo-java-server-test \
             -e POSTGRES_PASSWORD=mongo-java-server-test \
             -e POSTGRES_DB=mongo-java-server-test \
             -d postgres:9.6-alpine

Example

public class Application {

    public static void main(String[] args) throws Exception {
        DataSource dataSource = new org.postgresql.jdbc3.Jdbc3PoolingDataSource();
        dataSource.setDatabaseName(…);
        dataSource.setUser(…);
        dataSource.setPassword(…);
        MongoServer server = new MongoServer(new PostgresqlBackend(dataSource));
        server.bind("localhost", 27017);
    }

}

Contributing

Please read the contributing guidelines if you want to contribute code to the project.

If you want to thank the author for this library or want to support the maintenance work, we are happy to receive a donation.

Donate

Ideas for other backends

Faulty backend

A faulty backend could randomly fail queries or cause timeouts. This could be used to test the client for error resilience.

Fuzzy backend

Fuzzing the wire protocol could be used to check the robustness of client drivers.

Related Work

  • Embedded MongoDB

    • Spins up a real MongoDB instance
  • fongo

    • focus on unit testing
    • no wire protocol implementation
    • intercepts the java mongo driver
    • currently used in nosql-unit

Project Statistics

Sourcerank 11
Repository Size 2.22 MB
Stars 137
Forks 39
Watchers 11
Open issues 4
Dependencies 11
Contributors 12
Tags 39
Created
Last updated
Last pushed

Top Contributors See all

Benedikt Waldvogel Hossomi Tom Allen epheatt jturin talaverete Cody Casterline Benoit Sautel Sangwoo Lee Lucas Cimon Mark Bigler Patrik Dufresne

Packages Referencing this Repo

de.bwaldvogel:mongo-java-server-core
Fake implementation of MongoDB in Java that speaks the wire protocol
Latest release 1.18.1 - Updated - 137 stars
de.bwaldvogel:mongo-java-server-h2-backend
Fake implementation of MongoDB in Java that speaks the wire protocol
Latest release 1.13.0 - Updated - 137 stars
de.bwaldvogel:mongo-java-server
Fake implementation of MongoDB in Java that speaks the wire protocol
Latest release 1.18.1 - Updated - 137 stars
de.bwaldvogel:mongo-java-server-postgresql-backend
Fake implementation of MongoDB in Java that speaks the wire protocol
Latest release 1.13.0 - Updated - 137 stars
de.bwaldvogel:mongo-java-server-memory-backend
Fake implementation of MongoDB in Java that speaks the wire protocol
Latest release 1.18.1 - Updated - 137 stars
de.bwaldvogel:mongo-java-server-test-common
Fake implementation of MongoDB in Java that speaks the wire protocol
Latest release 1.18.1 - Updated - 137 stars

Recent Tags See all

v1.18.1 August 04, 2019
v1.18.0 August 04, 2019
v1.17.1 July 17, 2019
v1.17.1 July 17, 2019
v1.17.0 July 16, 2019
v1.17.0 July 16, 2019
v1.17.0 July 16, 2019
v1.16.0 May 08, 2019
v1.16.0 May 08, 2019
v1.15.0 April 22, 2019
v1.15.0 April 22, 2019
v1.14.0 April 02, 2019
v1.13.0 March 01, 2019
v1.12.0 February 10, 2019
v1.11.1 January 02, 2019

Something wrong with this page? Make a suggestion

Last synced: 2019-08-04 22:51:00 UTC

Login to resync this repository