Search in sources :

Example 31 with BasicDBObjectBuilder

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

the class ClusterEventPeriodicalTest method runHandlesAutoValueCorrectly.

@Test
public void runHandlesAutoValueCorrectly() throws Exception {
    final DebugEvent event = DebugEvent.create("Node ID", TIME, "test");
    DBObject dbObject = new BasicDBObjectBuilder().add("timestamp", TIME.getMillis()).add("producer", "TEST-PRODUCER").add("consumers", Collections.emptyList()).add("event_class", DebugEvent.class.getCanonicalName()).add("payload", objectMapper.convertValue(event, Map.class)).get();
    @SuppressWarnings("deprecation") final DBCollection collection = mongoConnection.getDatabase().getCollection(ClusterEventPeriodical.COLLECTION_NAME);
    collection.save(dbObject);
    assertThat(collection.count()).isEqualTo(1L);
    clusterEventPeriodical.run();
    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(event);
    verify(clusterEventBus, never()).post(event);
}
Also used : DBCollection(com.mongodb.DBCollection) DebugEvent(org.graylog2.system.debug.DebugEvent) BasicDBObjectBuilder(com.mongodb.BasicDBObjectBuilder) BasicDBList(com.mongodb.BasicDBList) List(java.util.List) DBObject(com.mongodb.DBObject) Test(org.junit.Test)

Example 32 with BasicDBObjectBuilder

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

the class ClusterEventPeriodicalTest method runHandlesInvalidPayloadsGracefully.

@Test
public void runHandlesInvalidPayloadsGracefully() throws Exception {
    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("HAHA", "test")).get();
    @SuppressWarnings("deprecation") final DBCollection collection = mongoConnection.getDatabase().getCollection(ClusterEventPeriodical.COLLECTION_NAME);
    collection.save(event);
    assertThat(collection.count()).isEqualTo(1L);
    clusterEventPeriodical.run();
    assertThat(collection.count()).isEqualTo(1L);
    @SuppressWarnings("unchecked") final List<String> consumers = (List<String>) collection.findOne().get("consumers");
    assertThat(consumers).containsExactly(nodeId.toString());
    verify(serverEventBus, never()).post(any());
    verify(clusterEventBus, never()).post(any());
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObjectBuilder(com.mongodb.BasicDBObjectBuilder) BasicDBList(com.mongodb.BasicDBList) List(java.util.List) DBObject(com.mongodb.DBObject) Test(org.junit.Test)

Example 33 with BasicDBObjectBuilder

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

the class ClusterConfigServiceImplTest method getOrDefaultReturnsExistingConfig.

@Test
public void getOrDefaultReturnsExistingConfig() 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();
    @SuppressWarnings("deprecation") final DBCollection collection = mongoConnection.getDatabase().getCollection(COLLECTION_NAME);
    collection.save(dbObject);
    assertThat(collection.count()).isEqualTo(1L);
    CustomConfig defaultValue = new CustomConfig();
    defaultValue.text = "DEFAULT";
    CustomConfig customConfig = clusterConfigService.getOrDefault(CustomConfig.class, defaultValue);
    assertThat(customConfig.text).isEqualTo("TEST");
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObjectBuilder(com.mongodb.BasicDBObjectBuilder) DBObject(com.mongodb.DBObject) Test(org.junit.Test)

Example 34 with BasicDBObjectBuilder

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

the class ClusterConfigServiceImplTest method getOrDefaultReturnsDefaultValueOnInvalidPayload.

@Test
public void getOrDefaultReturnsDefaultValueOnInvalidPayload() throws Exception {
    DBObject dbObject = new BasicDBObjectBuilder().add("type", CustomConfig.class.getCanonicalName()).add("payload", "wrong payload").add("last_updated", TIME.toString()).add("last_updated_by", "ID").get();
    @SuppressWarnings("deprecation") final DBCollection collection = mongoConnection.getDatabase().getCollection(COLLECTION_NAME);
    collection.save(dbObject);
    assertThat(collection.count()).isEqualTo(1L);
    CustomConfig defaultValue = new CustomConfig();
    defaultValue.text = "DEFAULT";
    assertThat(clusterConfigService.getOrDefault(CustomConfig.class, defaultValue)).isSameAs(defaultValue);
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObjectBuilder(com.mongodb.BasicDBObjectBuilder) DBObject(com.mongodb.DBObject) Test(org.junit.Test)

Example 35 with BasicDBObjectBuilder

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

the class ClusterConfigServiceImplTest method listIgnoresInvalidClasses.

@Test
public void listIgnoresInvalidClasses() throws Exception {
    @SuppressWarnings("deprecation") final DBCollection collection = mongoConnection.getDatabase().getCollection(COLLECTION_NAME);
    collection.save(new BasicDBObjectBuilder().add("type", CustomConfig.class.getCanonicalName()).add("payload", Collections.singletonMap("text", "TEST")).add("last_updated", TIME.toString()).add("last_updated_by", "ID").get());
    collection.save(new BasicDBObjectBuilder().add("type", "invalid.ClassName").add("payload", Collections.emptyMap()).add("last_updated", TIME.toString()).add("last_updated_by", "ID").get());
    assertThat(collection.count()).isEqualTo(2L);
    assertThat(clusterConfigService.list()).hasSize(1).containsOnly(CustomConfig.class);
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObjectBuilder(com.mongodb.BasicDBObjectBuilder) Test(org.junit.Test)

Aggregations

BasicDBObjectBuilder (com.mongodb.BasicDBObjectBuilder)44 DBObject (com.mongodb.DBObject)30 Test (org.junit.Test)21 DBCollection (com.mongodb.DBCollection)18 BasicDBObject (com.mongodb.BasicDBObject)12 ArrayList (java.util.ArrayList)7 BasicDBList (com.mongodb.BasicDBList)6 List (java.util.List)4 ResourceFieldSchema (org.apache.pig.ResourceSchema.ResourceFieldSchema)4 HiveTest (com.mongodb.hadoop.hive.HiveTest)3 IOException (java.io.IOException)3 Map (java.util.Map)3 MongoException (com.mongodb.MongoException)2 Configuration (org.apache.hadoop.conf.Configuration)2 JobConf (org.apache.hadoop.mapred.JobConf)2 InputSplit (org.apache.hadoop.mapreduce.InputSplit)2 ResourceSchema (org.apache.pig.ResourceSchema)2 RyaStatement (org.apache.rya.api.domain.RyaStatement)2 BasicBSONObject (org.bson.BasicBSONObject)2 GeometryType (org.teiid.core.types.GeometryType)2