Skip to content

Commit

Permalink
Storing string for matching type, adding unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisoelkers committed Jul 13, 2015
1 parent 87809db commit c0fbfe7
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
*/
package org.graylog2.plugin.streams;

import com.fasterxml.jackson.annotation.JsonValue;
import org.graylog2.plugin.database.Persisted;

import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.google.common.base.Strings.emptyToNull;

public interface Stream extends Persisted {
enum MatchingType {
AND,
Expand All @@ -36,7 +39,7 @@ enum MatchingType {
public static final MatchingType DEFAULT = AND;

public static MatchingType valueOfOrDefault(String name) {
return (name == null ? DEFAULT : valueOf(name));
return (emptyToNull(name) == null ? DEFAULT : valueOf(name));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.graylog2.plugin.streams;

import org.junit.Test;

import static org.junit.Assert.*;

public class MatchingTypeTest {

@Test
public void testValueOfOrDefault() throws Exception {
assertEquals(Stream.MatchingType.valueOfOrDefault("AND"), Stream.MatchingType.AND);
assertEquals(Stream.MatchingType.valueOfOrDefault("OR"), Stream.MatchingType.OR);
assertEquals(Stream.MatchingType.valueOfOrDefault(null), Stream.MatchingType.DEFAULT);
assertEquals(Stream.MatchingType.valueOfOrDefault(""), Stream.MatchingType.DEFAULT);
}

@Test(expected = IllegalArgumentException.class)
public void testValueOfOrDefaultThrowsExceptionForUnknownEnumName() {
Stream.MatchingType.valueOfOrDefault("FOO");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public Stream create(CreateStreamRequest cr, String userId) {
streamData.put(StreamImpl.FIELD_CREATOR_USER_ID, userId);
streamData.put(StreamImpl.FIELD_CREATED_AT, Tools.iso8601());
streamData.put(StreamImpl.FIELD_CONTENT_PACK, cr.contentPack());
streamData.put(StreamImpl.FIELD_MATCHING_TYPE, Stream.MatchingType.valueOfOrDefault(cr.matchingType()));
streamData.put(StreamImpl.FIELD_MATCHING_TYPE, Stream.MatchingType.valueOfOrDefault(cr.matchingType()).toString());

return create(streamData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.codahale.metrics.MetricRegistry;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.bson.types.ObjectId;
import org.graylog2.plugin.Message;
import org.graylog2.plugin.streams.Stream;
Expand All @@ -37,10 +38,13 @@
import java.util.Map;
import java.util.concurrent.Executors;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
Expand Down Expand Up @@ -443,6 +447,40 @@ public void testGetFingerprint() {
assertNotEquals(engine1.getFingerprint(), engine3.getFingerprint());
}

@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 = mock(StreamRule.class);
when(streamRule1.getId()).thenReturn("StreamRule1Id");
when(streamRule1.getType()).thenReturn(StreamRuleType.EXACT);
when(streamRule1.getField()).thenReturn(dummyField);
when(streamRule1.getValue()).thenReturn(dummyValue);

final StreamRule streamRule2 = mock(StreamRule.class);
when(streamRule2.getId()).thenReturn("StreamRule2Id");
when(streamRule2.getType()).thenReturn(StreamRuleType.EXACT);
when(streamRule2.getField()).thenReturn(dummyField);
when(streamRule2.getValue()).thenReturn("not" + dummyValue);

when(stream.getStreamRules()).thenReturn(Lists.<StreamRule>newArrayList(streamRule1, streamRule2));

final Message message = mock(Message.class);
when(message.hasField(eq(dummyField))).thenReturn(true);
when(message.getField(eq(dummyField))).thenReturn(dummyValue);
when(message.getFieldNames()).thenReturn(Sets.<String>newHashSet(dummyField));

final StreamRouterEngine engine = newEngine(Lists.<Stream>newArrayList(stream));

final List<Stream> result = engine.match(message);

assertThat(result).hasSize(1);
assertThat(result).contains(stream);
}

private StreamMock getStreamMock(String title) {
return new StreamMock(ImmutableMap.<String, Object>of("_id", new ObjectId(), "title", title));
}
Expand Down

0 comments on commit c0fbfe7

Please sign in to comment.