use of org.graylog2.plugin.inputs.Converter 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.plugin.inputs.Converter in project graylog2-server by Graylog2.
the class JsonTesterResource method testJsonExtractor.
private JsonTesterResponse testJsonExtractor(String testString, boolean flatten, String listSeparator, String keySeparator, String kvSeparator, boolean replaceKeyWhitespace, String keyWhitespaceReplacement, String keyPrefix) {
final Map<String, Object> config = ImmutableMap.<String, Object>builder().put("flatten", flatten).put("list_separator", listSeparator).put("key_separator", keySeparator).put("kv_separator", kvSeparator).put("replace_key_whitespace", replaceKeyWhitespace).put("key_whitespace_replacement", keyWhitespaceReplacement).put("key_prefix", keyPrefix).build();
final JsonExtractor extractor;
try {
extractor = new JsonExtractor(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 Map<String, Object> result = extractor.extractJson(testString);
return JsonTesterResponse.create(result, flatten, listSeparator, keySeparator, kvSeparator, testString);
}
use of org.graylog2.plugin.inputs.Converter 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.plugin.inputs.Converter in project graylog2-server by Graylog2.
the class ExtractorTest method testConvertersWithMultipleFieldsAndNull.
@Test
public void testConvertersWithMultipleFieldsAndNull() throws Exception {
final Converter converter = new TestConverter.Builder().multiple(true).callback(new Function<Object, Object>() {
@Nullable
@Override
public Object apply(Object input) {
return null;
}
}).build();
final TestExtractor extractor = new TestExtractor.Builder().converters(Collections.singletonList(converter)).callback(() -> new Result[] { new Result("1", -1, -1) }).build();
final Message msg = createMessage("the message");
extractor.runExtractor(msg);
assertThat(msg.getField("message")).isEqualTo("the message");
assertThat(extractor.getConverterExceptionCount()).isEqualTo(0L);
}
use of org.graylog2.plugin.inputs.Converter in project graylog2-server by Graylog2.
the class ExtractorTest method testConvertersAreExecutedInOrder.
@Test
public void testConvertersAreExecutedInOrder() throws Exception {
final Converter converter1 = new TestConverter.Builder().callback(new Function<Object, Object>() {
@Nullable
@Override
public Object apply(Object input) {
return ((String) input) + "1";
}
}).build();
final Converter converter2 = new TestConverter.Builder().callback(new Function<Object, Object>() {
@Nullable
@Override
public Object apply(Object input) {
return ((String) input) + "2";
}
}).build();
final Converter converter3 = new TestConverter.Builder().callback(new Function<Object, Object>() {
@Nullable
@Override
public Object apply(Object input) {
return ((String) input) + "3";
}
}).build();
final TestExtractor extractor = new TestExtractor.Builder().converters(Lists.newArrayList(converter1, converter2, converter3)).callback(new Callable<Result[]>() {
@Override
public Result[] call() throws Exception {
return new Result[] { new Result("converter", -1, -1) };
}
}).build();
final Message msg = createMessage("message");
extractor.runExtractor(msg);
assertThat(msg.getField("target")).isEqualTo("converter123");
}
Aggregations