use of org.graylog2.inputs.extractors.RegexReplaceExtractor in project graylog2-server by Graylog2.
the class RegexReplaceExtractorTest method testReplacementWithReplaceAll.
@Test
public void testReplacementWithReplaceAll() throws Exception {
final Message message = new Message("Foobar 123 Foobaz 456", "source", Tools.nowUTC());
final RegexReplaceExtractor extractor = new RegexReplaceExtractor(metricRegistry, "id", "title", 0L, Extractor.CursorStrategy.COPY, "message", "message", ImmutableMap.<String, Object>of("regex", "(\\w+) (\\d+)", "replacement", "$2/$1", "replace_all", true), "user", Collections.<Converter>emptyList(), Extractor.ConditionType.NONE, null);
extractor.runExtractor(message);
assertThat(message.getMessage()).isEqualTo("123/Foobar 456/Foobaz");
}
use of org.graylog2.inputs.extractors.RegexReplaceExtractor in project graylog2-server by Graylog2.
the class RegexReplaceExtractorTest method testReplacementWithNoMatchAndDefaultReplacement.
@Test
public void testReplacementWithNoMatchAndDefaultReplacement() throws Exception {
final Message message = new Message("Test", "source", Tools.nowUTC());
final RegexReplaceExtractor extractor = new RegexReplaceExtractor(metricRegistry, "id", "title", 0L, Extractor.CursorStrategy.COPY, "message", "message", ImmutableMap.<String, Object>of("regex", "NO-MATCH"), "user", Collections.<Converter>emptyList(), Extractor.ConditionType.NONE, null);
extractor.runExtractor(message);
assertThat(message.getMessage()).isEqualTo("Test");
}
use of org.graylog2.inputs.extractors.RegexReplaceExtractor in project graylog2-server by Graylog2.
the class RegexReplaceExtractorTest method testReplacementWithOnePlaceholder.
@Test
public void testReplacementWithOnePlaceholder() throws Exception {
final Message message = new Message("Test Foobar", "source", Tools.nowUTC());
final RegexReplaceExtractor extractor = new RegexReplaceExtractor(metricRegistry, "id", "title", 0L, Extractor.CursorStrategy.COPY, "message", "message", ImmutableMap.<String, Object>of("regex", "Test (\\w+)"), "user", Collections.<Converter>emptyList(), Extractor.ConditionType.NONE, null);
extractor.runExtractor(message);
assertThat(message.getMessage()).isEqualTo("Foobar");
}
use of org.graylog2.inputs.extractors.RegexReplaceExtractor in project graylog2-server by Graylog2.
the class RegexReplaceTesterResource method testRegexReplaceExtractor.
private RegexReplaceTesterResponse testRegexReplaceExtractor(String example, String regex, String replacement, boolean replaceAll) {
final Map<String, Object> config = ImmutableMap.<String, Object>of("regex", regex, "replacement", replacement, "replace_all", replaceAll);
final RegexReplaceExtractor extractor;
try {
extractor = new RegexReplaceExtractor(new MetricRegistry(), "test", "Test", 0L, Extractor.CursorStrategy.COPY, "test", "test", config, getCurrentUser().getName(), Collections.<Converter>emptyList(), Extractor.ConditionType.NONE, "");
} catch (Extractor.ReservedFieldException e) {
throw new BadRequestException("Trying to overwrite a reserved message field", e);
} catch (ConfigurationException e) {
throw new BadRequestException("Invalid extractor configuration", e);
}
final Extractor.Result result = extractor.runExtractor(example);
final RegexReplaceTesterResponse.Match match = result == null ? null : RegexReplaceTesterResponse.Match.create(String.valueOf(result.getValue()), result.getBeginIndex(), result.getEndIndex());
return RegexReplaceTesterResponse.create(result != null, match, regex, replacement, replaceAll, example);
}
use of org.graylog2.inputs.extractors.RegexReplaceExtractor in project graylog2-server by Graylog2.
the class RegexReplaceExtractorTest method testReplacementWithoutReplaceAll.
@Test
public void testReplacementWithoutReplaceAll() throws Exception {
final Message message = new Message("Foobar 123 Foobaz 456", "source", Tools.nowUTC());
final RegexReplaceExtractor extractor = new RegexReplaceExtractor(metricRegistry, "id", "title", 0L, Extractor.CursorStrategy.COPY, "message", "message", ImmutableMap.<String, Object>of("regex", "(\\w+) (\\d+)", "replacement", "$2/$1", "replace_all", false), "user", Collections.<Converter>emptyList(), Extractor.ConditionType.NONE, null);
extractor.runExtractor(message);
assertThat(message.getMessage()).isEqualTo("123/Foobar Foobaz 456");
}
Aggregations