Search in sources :

Example 81 with StreamRule

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

the class SmallerMatcherTest method testMissedInvertedMatch.

@Test
public void testMissedInvertedMatch() {
    StreamRule rule = getSampleRule();
    rule.setValue("25");
    rule.setInverted(true);
    Message msg = getSampleMessage();
    msg.addField("something", "23");
    StreamRuleMatcher matcher = getMatcher(rule);
    assertFalse(matcher.match(msg, rule));
}
Also used : Message(org.graylog2.plugin.Message) StreamRule(org.graylog2.plugin.streams.StreamRule) Test(org.junit.Test)

Example 82 with StreamRule

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

the class SmallerMatcherTest method testSuccessfulDoubleMatch.

@Test
public void testSuccessfulDoubleMatch() {
    StreamRule rule = getSampleRule();
    rule.setValue("100");
    Message msg = getSampleMessage();
    msg.addField("something", "20.45");
    StreamRuleMatcher matcher = getMatcher(rule);
    assertTrue(matcher.match(msg, rule));
}
Also used : Message(org.graylog2.plugin.Message) StreamRule(org.graylog2.plugin.streams.StreamRule) Test(org.junit.Test)

Example 83 with StreamRule

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

the class StreamRouterEngineTest method getStreamRuleMock.

private StreamRule getStreamRuleMock(String id, StreamRuleType type, String field, String value) {
    final StreamRule result = mock(StreamRule.class);
    when(result.getId()).thenReturn(id);
    when(result.getType()).thenReturn(type);
    when(result.getField()).thenReturn(field);
    when(result.getValue()).thenReturn(value);
    return result;
}
Also used : StreamRule(org.graylog2.plugin.streams.StreamRule)

Example 84 with StreamRule

use of org.graylog2.plugin.streams.StreamRule 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 85 with StreamRule

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

the class StreamRouterEngineTest method testOrMatching.

@Test
public void testOrMatching() {
    final String dummyField = "dummyField";
    final String dummyValue = "dummyValue";
    final Stream stream = mock(Stream.class);
    when(stream.getMatchingType()).thenReturn(Stream.MatchingType.OR);
    final StreamRule streamRule1 = getStreamRuleMock("StreamRule1Id", StreamRuleType.EXACT, dummyField, dummyValue);
    final StreamRule streamRule2 = getStreamRuleMock("StreamRule2Id", StreamRuleType.EXACT, dummyField, "not" + dummyValue);
    when(stream.getStreamRules()).thenReturn(Lists.newArrayList(streamRule1, streamRule2));
    final Message message = mock(Message.class);
    when(message.getField(eq(dummyField))).thenReturn(dummyValue);
    final StreamRouterEngine engine = newEngine(Lists.newArrayList(stream));
    final List<Stream> result = engine.match(message);
    assertThat(result).hasSize(1);
    assertThat(result).contains(stream);
}
Also used : Message(org.graylog2.plugin.Message) StreamRule(org.graylog2.plugin.streams.StreamRule) Stream(org.graylog2.plugin.streams.Stream) Test(org.junit.Test)

Aggregations

StreamRule (org.graylog2.plugin.streams.StreamRule)98 Message (org.graylog2.plugin.Message)73 Test (org.junit.Test)71 Stream (org.graylog2.plugin.streams.Stream)16 ObjectId (org.bson.types.ObjectId)11 Timed (com.codahale.metrics.annotation.Timed)10 ApiOperation (io.swagger.annotations.ApiOperation)10 Output (org.graylog2.plugin.streams.Output)9 Produces (javax.ws.rs.Produces)8 AuditEvent (org.graylog2.audit.jersey.AuditEvent)8 ApiResponses (io.swagger.annotations.ApiResponses)7 Consumes (javax.ws.rs.Consumes)7 POST (javax.ws.rs.POST)7 Path (javax.ws.rs.Path)7 NotFoundException (org.graylog2.database.NotFoundException)7 URI (java.net.URI)6 Map (java.util.Map)6 AlarmCallbackConfiguration (org.graylog2.alarmcallbacks.AlarmCallbackConfiguration)6 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)6 AlertCondition (org.graylog2.plugin.alarms.AlertCondition)6