Search in sources :

Example 56 with DBCollection

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

the class ClusterConfigServiceImplTest method getReturnsExistingConfig.

@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.DELETE_ALL)
public void getReturnsExistingConfig() throws Exception {
    DBObject dbObject = new BasicDBObjectBuilder().add("type", CustomConfig.class.getCanonicalName()).add("payload", Collections.singletonMap("text", "TEST")).add("last_updated", TIME.toString()).add("last_updated_by", "ID").get();
    final DBCollection collection = mongoConnection.getDatabase().getCollection(COLLECTION_NAME);
    collection.save(dbObject);
    assertThat(collection.count()).isEqualTo(1L);
    CustomConfig customConfig = clusterConfigService.get(CustomConfig.class);
    assertThat(customConfig.text).isEqualTo("TEST");
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObjectBuilder(com.mongodb.BasicDBObjectBuilder) DBObject(com.mongodb.DBObject) UsingDataSet(com.lordofthejars.nosqlunit.annotation.UsingDataSet) Test(org.junit.Test)

Example 57 with DBCollection

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

the class NodeServiceImplTest method testRegisterServerWithExistingNode.

@Test
@UsingDataSet(locations = "NodeServiceImplTest-one-node.json", loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public void testRegisterServerWithExistingNode() throws Exception {
    final Node node1 = nodeService.byNodeId(nodeId);
    assertThat(node1.getNodeId()).describedAs("There should be one existing node").isEqualTo(NODE_ID);
    nodeService.registerServer(nodeId.toString(), true, TRANSPORT_URI, LOCAL_CANONICAL_HOSTNAME);
    final DBCollection collection = mongoRule.getMongoConnection().getDatabase().getCollection("nodes");
    assertThat(collection.count()).describedAs("There should only be one node").isEqualTo(1);
    final Node node2 = nodeService.byNodeId(nodeId);
    assertThat(node2).isNotNull();
    assertThat(node2.getHostname()).isEqualTo(LOCAL_CANONICAL_HOSTNAME);
    assertThat(node2.getTransportAddress()).isEqualTo(TRANSPORT_URI.toString());
    assertThat(node2.isMaster()).isTrue();
}
Also used : DBCollection(com.mongodb.DBCollection) UsingDataSet(com.lordofthejars.nosqlunit.annotation.UsingDataSet) Test(org.junit.Test)

Example 58 with DBCollection

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

the class ClusterConfigServiceImplTest method getWithKeyReturnsNullOnNonExistingConfig.

@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.DELETE_ALL)
public void getWithKeyReturnsNullOnNonExistingConfig() throws Exception {
    final DBCollection collection = mongoConnection.getDatabase().getCollection(COLLECTION_NAME);
    assertThat(collection.count()).isEqualTo(0L);
    assertThat(clusterConfigService.get("foo", CustomConfig.class)).isNull();
}
Also used : DBCollection(com.mongodb.DBCollection) UsingDataSet(com.lordofthejars.nosqlunit.annotation.UsingDataSet) Test(org.junit.Test)

Example 59 with DBCollection

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

the class ClusterConfigServiceImplTest method removeDoesNothingIfConfigDoesNotExist.

@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.DELETE_ALL)
public void removeDoesNothingIfConfigDoesNotExist() throws Exception {
    final DBCollection collection = mongoConnection.getDatabase().getCollection(COLLECTION_NAME);
    assertThat(collection.count()).isEqualTo(0L);
    assertThat(clusterConfigService.remove(CustomConfig.class)).isEqualTo(0);
}
Also used : DBCollection(com.mongodb.DBCollection) UsingDataSet(com.lordofthejars.nosqlunit.annotation.UsingDataSet) Test(org.junit.Test)

Example 60 with DBCollection

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

the class ClusterConfigServiceImplTest method writeUpdatesExistingClusterConfig.

@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.DELETE_ALL)
public void writeUpdatesExistingClusterConfig() throws Exception {
    CustomConfig customConfig = new CustomConfig();
    customConfig.text = "TEST";
    DBObject seedObject = new BasicDBObjectBuilder().add("type", CustomConfig.class.getCanonicalName()).add("payload", Collections.singletonMap("text", "ORIGINAL")).add("last_updated", TIME.toString()).add("last_updated_by", "NOT ID").get();
    final DBCollection collection = mongoConnection.getDatabase().getCollection(COLLECTION_NAME);
    collection.save(seedObject);
    assertThat(collection.count()).isEqualTo(1L);
    clusterConfigService.write(customConfig);
    assertThat(collection.count()).isEqualTo(1L);
    DBObject dbObject = collection.findOne();
    assertThat((String) dbObject.get("type")).isEqualTo(CustomConfig.class.getCanonicalName());
    assertThat((String) dbObject.get("last_updated_by")).isEqualTo("ID");
    @SuppressWarnings("unchecked") Map<String, Object> payload = (Map<String, Object>) dbObject.get("payload");
    assertThat(payload).containsEntry("text", "TEST");
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObjectBuilder(com.mongodb.BasicDBObjectBuilder) DBObject(com.mongodb.DBObject) DBObject(com.mongodb.DBObject) Map(java.util.Map) UsingDataSet(com.lordofthejars.nosqlunit.annotation.UsingDataSet) 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