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);
}
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());
}
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");
}
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);
}
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);
}
Aggregations