Search in sources :

Example 16 with StreamMock

use of org.graylog2.streams.StreamMock in project graylog2-server by Graylog2.

the class StreamRouterEngineTest method testGreaterMatch.

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

Example 17 with StreamMock

use of org.graylog2.streams.StreamMock in project graylog2-server by Graylog2.

the class StreamRouterEngineTest method testInvertedContainsMatch.

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

Example 18 with StreamMock

use of org.graylog2.streams.StreamMock in project graylog2-server by Graylog2.

the class StreamRouterEngineTest method testTestMatch.

@Test
public void testTestMatch() throws Exception {
    final StreamMock stream = getStreamMock("test");
    final StreamRuleMock rule1 = new StreamRuleMock(ImmutableMap.of("_id", new ObjectId(), "field", "testfield1", "type", StreamRuleType.PRESENCE.toInteger(), "stream_id", stream.getId()));
    final StreamRuleMock rule2 = new StreamRuleMock(ImmutableMap.of("_id", new ObjectId(), "field", "testfield2", "value", "^test", "type", StreamRuleType.REGEX.toInteger(), "stream_id", stream.getId()));
    stream.setStreamRules(Lists.newArrayList(rule1, rule2));
    final StreamRouterEngine engine = newEngine(Lists.newArrayList(stream));
    // Without testfield1 and testfield2 in the message.
    final Message message1 = getMessage();
    final StreamRouterEngine.StreamTestMatch testMatch1 = engine.testMatch(message1).get(0);
    final Map<StreamRule, Boolean> matches1 = testMatch1.getMatches();
    assertFalse(testMatch1.isMatched());
    assertFalse(matches1.get(rule1));
    assertFalse(matches1.get(rule2));
    // With testfield1 but no-matching testfield2 in the message.
    final Message message2 = getMessage();
    message2.addField("testfield1", "testvalue");
    message2.addField("testfield2", "no-testvalue");
    final StreamRouterEngine.StreamTestMatch testMatch2 = engine.testMatch(message2).get(0);
    final Map<StreamRule, Boolean> matches2 = testMatch2.getMatches();
    assertFalse(testMatch2.isMatched());
    assertTrue(matches2.get(rule1));
    assertFalse(matches2.get(rule2));
    // With testfield1 and matching testfield2 in the message.
    final Message message3 = getMessage();
    message3.addField("testfield1", "testvalue");
    message3.addField("testfield2", "testvalue2");
    final StreamRouterEngine.StreamTestMatch testMatch3 = engine.testMatch(message3).get(0);
    final Map<StreamRule, Boolean> matches3 = testMatch3.getMatches();
    assertTrue(testMatch3.isMatched());
    assertTrue(matches3.get(rule1));
    assertTrue(matches3.get(rule2));
}
Also used : Message(org.graylog2.plugin.Message) ObjectId(org.bson.types.ObjectId) StreamRule(org.graylog2.plugin.streams.StreamRule) StreamRuleMock(org.graylog2.streams.matchers.StreamRuleMock) Test(org.junit.Test)

Example 19 with StreamMock

use of org.graylog2.streams.StreamMock in project graylog2-server by Graylog2.

the class StreamRouterEngineTest method issue1396.

@Test
public void issue1396() throws Exception {
    final StreamMock stream = getStreamMock("GitHub issue #1396");
    stream.setMatchingType(Stream.MatchingType.AND);
    final StreamRuleMock rule1 = new StreamRuleMock(ImmutableMap.<String, Object>builder().put("_id", new ObjectId()).put("field", "custom1").put("value", "value1").put("type", StreamRuleType.EXACT.toInteger()).put("inverted", false).put("stream_id", stream.getId()).build());
    final StreamRuleMock rule2 = new StreamRuleMock(ImmutableMap.<String, Object>builder().put("_id", new ObjectId()).put("field", "custom2").put("value", "value2").put("type", StreamRuleType.EXACT.toInteger()).put("inverted", false).put("stream_id", stream.getId()).build());
    stream.setStreamRules(Lists.newArrayList(rule1, rule2));
    final StreamRouterEngine engine = newEngine(Lists.newArrayList(stream));
    final Message message1 = getMessage();
    message1.addFields(ImmutableMap.of("custom1", "value1"));
    assertTrue("Message without \"custom2\" should not match conditions", engine.match(message1).isEmpty());
    final Message message2 = getMessage();
    message2.addFields(ImmutableMap.of("custom1", "value1", "custom2", "value2"));
    assertEquals("Message with \"custom1\" and \"custom2\" should match conditions", Lists.newArrayList(stream), engine.match(message2));
}
Also used : Message(org.graylog2.plugin.Message) ObjectId(org.bson.types.ObjectId) StreamRuleMock(org.graylog2.streams.matchers.StreamRuleMock) Test(org.junit.Test)

Example 20 with StreamMock

use of org.graylog2.streams.StreamMock in project graylog2-server by Graylog2.

the class StreamRouterEngineTest method testRegexMatch.

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

Aggregations

Test (org.junit.Test)22 Message (org.graylog2.plugin.Message)16 ObjectId (org.bson.types.ObjectId)15 StreamRuleMock (org.graylog2.streams.matchers.StreamRuleMock)15 StreamMock (org.graylog2.streams.StreamMock)7 Stream (org.graylog2.plugin.streams.Stream)5 Configuration (org.graylog2.plugin.configuration.Configuration)4 DateTime (org.joda.time.DateTime)4 AbstractAlertCondition (org.graylog2.alerts.AbstractAlertCondition)3 DummyAlertCondition (org.graylog2.alerts.types.DummyAlertCondition)3 AlertCondition (org.graylog2.plugin.alarms.AlertCondition)3 MockResponse (okhttp3.mockwebserver.MockResponse)2 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)2 EntityDescriptor (org.graylog2.contentpacks.model.entities.EntityDescriptor)2 NativeEntityDescriptor (org.graylog2.contentpacks.model.entities.NativeEntityDescriptor)2 StreamRule (org.graylog2.plugin.streams.StreamRule)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Date (java.util.Date)1 Event (org.graylog.events.event.Event)1 EventWithContext (org.graylog.events.event.EventWithContext)1