Search in sources :

Example 16 with CreateCollectionOptions

use of com.mongodb.client.model.CreateCollectionOptions in project camel by apache.

the class MongoDbTailableCursorConsumerTest method testCustomTailTrackLocation.

@Test
public void testCustomTailTrackLocation() throws Exception {
    assertEquals(0, cappedTestCollection.count());
    final MockEndpoint mock = getMockEndpoint("mock:test");
    // get the custom tracking collection and drop it (tailTrackDb=einstein&tailTrackCollection=curie&tailTrackField=newton)
    MongoCollection<BasicDBObject> trackingCol = mongo.getDatabase("einstein").getCollection("curie", BasicDBObject.class);
    trackingCol.drop();
    trackingCol = mongo.getDatabase("einstein").getCollection("curie", BasicDBObject.class);
    // create a capped collection with max = 1000
    //BasicDBObjectBuilder.start().add("capped", true).add("size", 1000000000).add("max", 1000).get()
    db.createCollection(cappedTestCollectionName, new CreateCollectionOptions().capped(true).sizeInBytes(1000000000).maxDocuments(1000));
    cappedTestCollection = db.getCollection(cappedTestCollectionName, BasicDBObject.class);
    addTestRoutes();
    context.startRoute("tailableCursorConsumer3");
    mock.expectedMessageCount(300);
    // pump 300 records
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            for (int i = 1; i <= 300; i++) {
                cappedTestCollection.insertOne(new BasicDBObject("increasing", i).append("string", "value" + i));
            }
        }
    });
    // start the data pumping
    t.start();
    // before we continue wait for the data pump to end
    t.join();
    mock.assertIsSatisfied();
    mock.reset();
    // stop the route to ensure that our lastVal is persisted, and check it
    context.stopRoute("tailableCursorConsumer3");
    // ensure that the persisted lastVal is 300, newton is the name of the trackingField we are using
    assertEquals(300, trackingCol.find(new BasicDBObject("persistentId", "darwin")).first().get("newton"));
    context.startRoute("tailableCursorConsumer3");
    // expect 300 messages and not 600
    mock.expectedMessageCount(300);
    // pump 300 records
    t = new Thread(new Runnable() {

        @Override
        public void run() {
            for (int i = 301; i <= 600; i++) {
                cappedTestCollection.insertOne(new BasicDBObject("increasing", i).append("string", "value" + i));
            }
        }
    });
    // start the data pumping
    t.start();
    // before we continue wait for the data pump to end
    t.join();
    mock.assertIsSatisfied();
    // check that the first received body contains increasing=301 and not increasing=1, i.e. it's not starting from the top
    Object firstBody = mock.getExchanges().get(0).getIn().getBody();
    assertTrue(firstBody instanceof DBObject);
    assertEquals(301, ((DBObject) firstBody).get("increasing"));
    // check that the persisted lastVal after stopping the route is 600, newton is the name of the trackingField we are using
    context.stopRoute("tailableCursorConsumer3");
    assertEquals(600, trackingCol.find(new BasicDBObject("persistentId", "darwin")).first().get("newton"));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Example 17 with CreateCollectionOptions

use of com.mongodb.client.model.CreateCollectionOptions in project camel by apache.

the class MongoDbTailableCursorConsumerTest method testHundredThousandRecords.

@Test
public void testHundredThousandRecords() throws Exception {
    assertEquals(0, cappedTestCollection.count());
    final MockEndpoint mock = getMockEndpoint("mock:test");
    mock.expectedMessageCount(1000);
    // create a capped collection with max = 1000
    //BasicDBObjectBuilder.start().add("capped", true).add("size", 1000000000).add("max", 1000).get())
    db.createCollection(cappedTestCollectionName, new CreateCollectionOptions().capped(true).sizeInBytes(1000000000).maxDocuments(1000));
    cappedTestCollection = db.getCollection(cappedTestCollectionName, BasicDBObject.class);
    addTestRoutes();
    context.startRoute("tailableCursorConsumer1");
    // continuous pump of 100000 records, asserting incrementally to reduce overhead on the mock endpoint
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            for (int i = 1; i <= 100000; i++) {
                cappedTestCollection.insertOne(new BasicDBObject("increasing", i).append("string", "value" + i));
                // incrementally assert, as the mock endpoint stores all messages and otherwise the test would be sluggish
                if (i % 1000 == 0) {
                    try {
                        MongoDbTailableCursorConsumerTest.this.assertAndResetMockEndpoint(mock);
                    } catch (Exception e) {
                        return;
                    }
                }
            }
        }
    });
    // start the data pumping
    t.start();
    // before we stop the route, wait for the data pumping to end
    t.join();
    context.stopRoute("tailableCursorConsumer1");
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) Test(org.junit.Test)

Example 18 with CreateCollectionOptions

use of com.mongodb.client.model.CreateCollectionOptions in project camel by apache.

the class MongoDbTailableCursorConsumerTest method testNoRecords.

@Test
public void testNoRecords() throws Exception {
    assertEquals(0, cappedTestCollection.count());
    MockEndpoint mock = getMockEndpoint("mock:test");
    mock.expectedMessageCount(0);
    //BasicDBObjectBuilder.start().add("capped", true).add("size", 1000000000).add("max", 1000).get()
    // create a capped collection with max = 1000
    CreateCollectionOptions collectionOptions = new CreateCollectionOptions().capped(true).sizeInBytes(1000000000).maxDocuments(1000);
    db.createCollection(cappedTestCollectionName, collectionOptions);
    cappedTestCollection = db.getCollection(cappedTestCollectionName, BasicDBObject.class);
    assertEquals(0, cappedTestCollection.count());
    addTestRoutes();
    context.startRoute("tailableCursorConsumer1");
    Thread.sleep(1000);
    mock.assertIsSatisfied();
    context.stopRoute("tailableCursorConsumer1");
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) Test(org.junit.Test)

Example 19 with CreateCollectionOptions

use of com.mongodb.client.model.CreateCollectionOptions in project mongo-java-driver by mongodb.

the class DatabaseAcceptanceTest method shouldCreateCappedCollection.

@Test
public void shouldCreateCappedCollection() {
    database.createCollection(getCollectionName(), new CreateCollectionOptions().capped(true).sizeInBytes(40 * 1024));
    List<String> collections = database.listCollectionNames().into(new ArrayList<String>());
    assertThat(collections.contains(getCollectionName()), is(true));
    MongoCollection<Document> collection = database.getCollection(getCollectionName());
    Document collStatsCommand = new Document("collStats", getCollectionName());
    Boolean isCapped = database.runCommand(collStatsCommand, ReadPreference.primary()).getBoolean("capped");
    assertThat(isCapped, is(true));
    assertThat("Should have the default index on _id", collection.listIndexes().into(new ArrayList<Document>()).size(), is(1));
}
Also used : CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) Document(org.bson.Document) Test(org.junit.Test)

Example 20 with CreateCollectionOptions

use of com.mongodb.client.model.CreateCollectionOptions in project mongo-java-driver by mongodb.

the class DatabaseAcceptanceTest method shouldCreateCappedCollectionWithoutAutoIndex.

@Test
public void shouldCreateCappedCollectionWithoutAutoIndex() {
    database.createCollection(getCollectionName(), new CreateCollectionOptions().capped(true).sizeInBytes(40 * 1024).autoIndex(false));
    List<String> collections = database.listCollectionNames().into(new ArrayList<String>());
    assertThat(collections.contains(getCollectionName()), is(true));
    MongoCollection<Document> collection = database.getCollection(getCollectionName());
    Document collStatsCommand = new Document("collStats", getCollectionName());
    Boolean isCapped = database.runCommand(collStatsCommand, ReadPreference.primary()).getBoolean("capped");
    assertThat(isCapped, is(true));
    assertThat("Should NOT have the default index on _id", collection.listIndexes().into(new ArrayList<Document>()).size(), is(0));
}
Also used : CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) Document(org.bson.Document) Test(org.junit.Test)

Aggregations

CreateCollectionOptions (com.mongodb.client.model.CreateCollectionOptions)22 Test (org.junit.Test)17 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)14 Document (org.bson.Document)12 BasicDBObject (com.mongodb.BasicDBObject)8 MongoDatabase (com.mongodb.client.MongoDatabase)4 ValidationOptions (com.mongodb.client.model.ValidationOptions)4 DBObject (com.mongodb.DBObject)3 Ignore (org.junit.Ignore)3 MongoCommandException (com.mongodb.MongoCommandException)2 Calendar (java.util.Calendar)2 CommandResult (com.mongodb.CommandResult)1 MongoClient (com.mongodb.MongoClient)1 MongoClientURI (com.mongodb.MongoClientURI)1 TextSearchOptions (com.mongodb.client.model.TextSearchOptions)1 Date (java.util.Date)1 Bson (org.bson.conversions.Bson)1 Book (org.mongodb.morphia.aggregation.AggregationTest.Book)1 CountResult (org.mongodb.morphia.aggregation.AggregationTest.CountResult)1