Search in sources :

Example 91 with ObjectId

use of org.bson.types.ObjectId in project graylog2-server by Graylog2.

the class StreamRouterEngineTest method testRemoveFromAllMessages.

@Test
public void testRemoveFromAllMessages() throws Exception {
    final StreamMock stream = getStreamMock("test");
    final StreamRuleMock rule = new StreamRuleMock(ImmutableMap.of("_id", new ObjectId(), "field", "testfield", "type", StreamRuleType.PRESENCE.toInteger(), "stream_id", stream.getId()));
    stream.setRemoveMatchesFromDefaultStream(true);
    stream.setStreamRules(Collections.singletonList(rule));
    final StreamRouterEngine engine = newEngine(Collections.singletonList(stream));
    final Message message = getMessage();
    message.addStream(defaultStream);
    assertThat(message.getStreams()).containsExactly(defaultStream);
    // Without testfield in the message.
    assertThat(engine.match(message)).isEmpty();
    // With field in the message.
    message.addField("testfield", "testvalue");
    assertThat(engine.match(message)).containsExactly(stream);
    assertThat(message.getStreams()).doesNotContain(defaultStream);
}
Also used : Message(org.graylog2.plugin.Message) ObjectId(org.bson.types.ObjectId) StreamRuleMock(org.graylog2.streams.matchers.StreamRuleMock) Test(org.junit.Test)

Example 92 with ObjectId

use of org.bson.types.ObjectId in project graylog2-server by Graylog2.

the class StreamRouterEngineTest method testContainsMatch.

@Test
public void testContainsMatch() throws Exception {
    final StreamMock stream = getStreamMock("test");
    final StreamRuleMock rule = new StreamRuleMock(ImmutableMap.of("_id", new ObjectId(), "field", "testfield", "value", "testvalue", "type", StreamRuleType.CONTAINS.toInteger(), "stream_id", stream.getId()));
    stream.setStreamRules(Lists.newArrayList(rule));
    final StreamRouterEngine engine = newEngine(Lists.newArrayList(stream));
    final Message message = getMessage();
    // With wrong value for field.
    message.addField("testfield", "no-foobar");
    assertTrue(engine.match(message).isEmpty());
    // With matching value for field.
    message.addField("testfield", "hello testvalue");
    assertEquals(engine.match(message), Lists.newArrayList(stream));
}
Also used : Message(org.graylog2.plugin.Message) ObjectId(org.bson.types.ObjectId) StreamRuleMock(org.graylog2.streams.matchers.StreamRuleMock) Test(org.junit.Test)

Example 93 with ObjectId

use of org.bson.types.ObjectId in project graylog2-server by Graylog2.

the class StreamRouterEngineTest method testSmallerMatch.

@Test
public void testSmallerMatch() throws Exception {
    final StreamMock stream = getStreamMock("test");
    final StreamRuleMock rule = new StreamRuleMock(ImmutableMap.of("_id", new ObjectId(), "field", "testfield", "value", "5", "type", StreamRuleType.SMALLER.toInteger(), "stream_id", stream.getId()));
    stream.setStreamRules(Lists.newArrayList(rule));
    final StreamRouterEngine engine = newEngine(Lists.newArrayList(stream));
    final Message message = getMessage();
    // With bigger value.
    message.addField("testfield", "5");
    assertTrue(engine.match(message).isEmpty());
    // With smaller value.
    message.addField("testfield", "2");
    assertEquals(engine.match(message), Lists.newArrayList(stream));
}
Also used : Message(org.graylog2.plugin.Message) ObjectId(org.bson.types.ObjectId) StreamRuleMock(org.graylog2.streams.matchers.StreamRuleMock) Test(org.junit.Test)

Example 94 with ObjectId

use of org.bson.types.ObjectId in project graylog2-server by Graylog2.

the class MatcherTest method getSampleRule.

protected StreamRule getSampleRule() {
    Map<String, Object> mongoRule = Maps.newHashMap();
    mongoRule.put("_id", new ObjectId());
    mongoRule.put("field", "something");
    return new StreamRuleMock(mongoRule);
}
Also used : ObjectId(org.bson.types.ObjectId)

Example 95 with ObjectId

use of org.bson.types.ObjectId in project gora by apache.

the class MongoStore method fromMongoString.

private Object fromMongoString(final DocumentFieldType storeType, final String docf, final BSONDecorator easybson) {
    Object result;
    if (storeType == DocumentFieldType.OBJECTID) {
        // Try auto-conversion of BSON data to ObjectId
        // It will work if data is stored as String or as ObjectId
        Object bin = easybson.get(docf);
        if (bin instanceof String) {
            ObjectId id = new ObjectId((String) bin);
            result = new Utf8(id.toString());
        } else {
            result = new Utf8(bin.toString());
        }
    } else if (storeType == DocumentFieldType.DATE) {
        Object bin = easybson.get(docf);
        if (bin instanceof Date) {
            Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.getDefault());
            calendar.setTime((Date) bin);
            result = new Utf8(DatatypeConverter.printDateTime(calendar));
        } else {
            result = new Utf8(bin.toString());
        }
    } else {
        result = easybson.getUtf8String(docf);
    }
    return result;
}
Also used : ObjectId(org.bson.types.ObjectId) Utf8(org.apache.avro.util.Utf8)

Aggregations

ObjectId (org.bson.types.ObjectId)249 Test (org.junit.Test)157 BasicDBObject (com.mongodb.BasicDBObject)54 Document (org.bson.Document)38 DBObject (com.mongodb.DBObject)35 ArrayList (java.util.ArrayList)28 BsonObjectId (org.bson.BsonObjectId)27 Date (java.util.Date)24 DBRef (com.mongodb.DBRef)15 List (java.util.List)15 StreamRuleMock (org.graylog2.streams.matchers.StreamRuleMock)14 Message (org.graylog2.plugin.Message)13 BasicBSONObject (org.bson.BasicBSONObject)12 Query (org.springframework.data.mongodb.core.query.Query)11 GridFSFile (com.mongodb.client.gridfs.model.GridFSFile)10 Map (java.util.Map)10 Binary (org.bson.types.Binary)10 GridFSFindIterable (com.mongodb.client.gridfs.GridFSFindIterable)8 UsingDataSet (com.lordofthejars.nosqlunit.annotation.UsingDataSet)7 Code (org.bson.types.Code)7