Search in sources :

Example 46 with DBCollection

use of com.mongodb.DBCollection in project graylog2-server by Graylog2.

the class ClusterEventPeriodicalTest method publishClusterEventHandlesAutoValueCorrectly.

@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.DELETE_ALL)
public void publishClusterEventHandlesAutoValueCorrectly() throws Exception {
    DBCollection collection = mongoConnection.getDatabase().getCollection(ClusterEventPeriodical.COLLECTION_NAME);
    DebugEvent event = DebugEvent.create("Node ID", "Test");
    assertThat(collection.count()).isEqualTo(0L);
    clusterEventPeriodical.publishClusterEvent(event);
    verify(clusterEventBus, never()).post(any());
    assertThat(collection.count()).isEqualTo(1L);
    DBObject dbObject = collection.findOne();
    assertThat((String) dbObject.get("producer")).isEqualTo(nodeId.toString());
    assertThat((String) dbObject.get("event_class")).isEqualTo(DebugEvent.class.getCanonicalName());
}
Also used : DBCollection(com.mongodb.DBCollection) DebugEvent(org.graylog2.system.debug.DebugEvent) DBObject(com.mongodb.DBObject) UsingDataSet(com.lordofthejars.nosqlunit.annotation.UsingDataSet) Test(org.junit.Test)

Example 47 with DBCollection

use of com.mongodb.DBCollection in project graylog2-server by Graylog2.

the class ClusterEventPeriodicalTest method prepareCollectionCreatesIndexesOnExistingCollection.

@Test
public void prepareCollectionCreatesIndexesOnExistingCollection() throws Exception {
    DBCollection original = mongoConnection.getDatabase().getCollection(ClusterEventPeriodical.COLLECTION_NAME);
    original.dropIndexes();
    assertThat(original.getName()).isEqualTo(ClusterEventPeriodical.COLLECTION_NAME);
    assertThat(original.getIndexInfo()).hasSize(1);
    DBCollection collection = ClusterEventPeriodical.prepareCollection(mongoConnection);
    assertThat(collection.getName()).isEqualTo(ClusterEventPeriodical.COLLECTION_NAME);
    assertThat(collection.getIndexInfo()).hasSize(2);
    assertThat(collection.getWriteConcern()).isEqualTo(WriteConcern.JOURNALED);
}
Also used : DBCollection(com.mongodb.DBCollection) Test(org.junit.Test)

Example 48 with DBCollection

use of com.mongodb.DBCollection in project graylog2-server by Graylog2.

the class ClusterEventPeriodicalTest method testPublishClusterEvent.

@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.DELETE_ALL)
public void testPublishClusterEvent() throws Exception {
    DBCollection collection = mongoConnection.getDatabase().getCollection(ClusterEventPeriodical.COLLECTION_NAME);
    SimpleEvent event = new SimpleEvent("test");
    assertThat(collection.count()).isEqualTo(0L);
    clusterEventPeriodical.publishClusterEvent(event);
    verify(clusterEventBus, never()).post(any());
    assertThat(collection.count()).isEqualTo(1L);
    DBObject dbObject = collection.findOne();
    assertThat((String) dbObject.get("producer")).isEqualTo(nodeId.toString());
    assertThat((String) dbObject.get("event_class")).isEqualTo(SimpleEvent.class.getCanonicalName());
    @SuppressWarnings("unchecked") Map<String, Object> payload = (Map<String, Object>) dbObject.get("payload");
    assertThat(payload).containsEntry("payload", "test");
}
Also used : DBCollection(com.mongodb.DBCollection) DBObject(com.mongodb.DBObject) DBObject(com.mongodb.DBObject) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) UsingDataSet(com.lordofthejars.nosqlunit.annotation.UsingDataSet) Test(org.junit.Test)

Example 49 with DBCollection

use of com.mongodb.DBCollection in project graylog2-server by Graylog2.

the class ClusterEventPeriodicalTest method serverEventBusDispatchesTypedEvents.

@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.DELETE_ALL)
public void serverEventBusDispatchesTypedEvents() throws Exception {
    final SimpleEventHandler handler = new SimpleEventHandler();
    serverEventBus.register(handler);
    DBObject event = new BasicDBObjectBuilder().add("timestamp", TIME.getMillis()).add("producer", "TEST-PRODUCER").add("consumers", Collections.emptyList()).add("event_class", SimpleEvent.class.getCanonicalName()).add("payload", ImmutableMap.of("payload", "test")).get();
    final DBCollection collection = mongoConnection.getDatabase().getCollection(ClusterEventPeriodical.COLLECTION_NAME);
    assertThat(collection.save(event).getN()).isEqualTo(1);
    assertThat(collection.count()).isEqualTo(1L);
    assertThat(handler.invocations).isEqualTo(0);
    clusterEventPeriodical.run();
    assertThat(handler.invocations).isEqualTo(1);
    assertThat(collection.count()).isEqualTo(1L);
    @SuppressWarnings("unchecked") final List<String> consumers = (List<String>) collection.findOne().get("consumers");
    assertThat(consumers).containsExactly(nodeId.toString());
    verify(serverEventBus, times(1)).post(any(SimpleEvent.class));
    verify(clusterEventBus, never()).post(any());
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObjectBuilder(com.mongodb.BasicDBObjectBuilder) List(java.util.List) DBObject(com.mongodb.DBObject) UsingDataSet(com.lordofthejars.nosqlunit.annotation.UsingDataSet) Test(org.junit.Test)

Example 50 with DBCollection

use of com.mongodb.DBCollection in project graylog2-server by Graylog2.

the class ClusterConfigServiceImplTest method prepareCollectionCreatesIndexesOnExistingCollection.

@Test
public void prepareCollectionCreatesIndexesOnExistingCollection() throws Exception {
    DBCollection original = mongoConnection.getDatabase().getCollection(COLLECTION_NAME);
    original.dropIndexes();
    assertThat(original.getName()).isEqualTo(COLLECTION_NAME);
    assertThat(original.getIndexInfo()).hasSize(1);
    DBCollection collection = ClusterConfigServiceImpl.prepareCollection(mongoConnection);
    assertThat(collection.getName()).isEqualTo(COLLECTION_NAME);
    assertThat(collection.getIndexInfo()).hasSize(2);
    assertThat(collection.getWriteConcern()).isEqualTo(WriteConcern.JOURNALED);
}
Also used : DBCollection(com.mongodb.DBCollection) Test(org.junit.Test)

Aggregations

DBCollection (com.mongodb.DBCollection)174 DBObject (com.mongodb.DBObject)92 BasicDBObject (com.mongodb.BasicDBObject)91 Test (org.junit.Test)70 UsingDataSet (com.lordofthejars.nosqlunit.annotation.UsingDataSet)29 DBCursor (com.mongodb.DBCursor)25 DB (com.mongodb.DB)23 MongoException (com.mongodb.MongoException)22 BasicDBObjectBuilder (com.mongodb.BasicDBObjectBuilder)17 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)12 JSONObject (org.json.JSONObject)12 MongoClientURI (com.mongodb.MongoClientURI)11 List (java.util.List)11 Map (java.util.Map)11 QueryBuilder (com.mongodb.QueryBuilder)10 HashMap (java.util.HashMap)10 Stopwatch (com.google.common.base.Stopwatch)9 WriteResult (com.mongodb.WriteResult)9 MongoClient (com.mongodb.MongoClient)8 IOException (java.io.IOException)8