use of org.graylog2.plugin.inputs.Converter in project graylog2-server by Graylog2.
the class SyslogPriFacilityConverterTest method testConvert.
@Test
public void testConvert() throws Exception {
Converter hc = new SyslogPriFacilityConverter(new HashMap<String, Object>());
assertNull(hc.convert(null));
assertEquals("", hc.convert(""));
assertEquals("lol no number", hc.convert("lol no number"));
// user-level
assertEquals("user-level", hc.convert("14"));
// kernel
assertEquals("kernel", hc.convert("5"));
// security/authorization
assertEquals("security/authorization", hc.convert("87"));
}
use of org.graylog2.plugin.inputs.Converter in project graylog2-server by Graylog2.
the class InputServiceImpl method getConvertersOfExtractor.
@SuppressWarnings("unchecked")
private List<Converter> getConvertersOfExtractor(DBObject extractor) {
final ImmutableList.Builder<Converter> listBuilder = ImmutableList.builder();
final BasicDBList converters = (BasicDBList) extractor.get(Extractor.FIELD_CONVERTERS);
for (final Object element : converters) {
final DBObject c = (BasicDBObject) element;
try {
listBuilder.add(ConverterFactory.factory(Converter.Type.valueOf(((String) c.get(Extractor.FIELD_CONVERTER_TYPE)).toUpperCase(Locale.ENGLISH)), (Map<String, Object>) c.get(Extractor.FIELD_CONVERTER_CONFIG)));
} catch (ConverterFactory.NoSuchConverterException e1) {
LOG.error("Cannot build converter from persisted data. No such converter.", e1);
} catch (Exception e) {
LOG.error("Cannot build converter from persisted data.", e);
}
}
return listBuilder.build();
}
use of org.graylog2.plugin.inputs.Converter in project graylog2-server by Graylog2.
the class ExtractorTest method testConvertersWithMultipleFields.
@Test
public void testConvertersWithMultipleFields() throws Exception {
final Converter converter = new TestConverter.Builder().multiple(true).callback(new Function<Object, Object>() {
@Nullable
@Override
public Object apply(Object input) {
return ImmutableMap.builder().put("one", 1).put("two", "2").put("message", // Try to overwrite reserved field.
"message should not be overwritten").build();
}
}).build();
final TestExtractor extractor = new TestExtractor.Builder().converters(Lists.newArrayList(converter)).callback(new Callable<Result[]>() {
@Override
public Result[] call() throws Exception {
return new Result[] { new Result("1", -1, -1) };
}
}).build();
final Message msg = createMessage("the message");
extractor.runExtractor(msg);
// With a "multiple fields" converter the target field is not touched, only the additional fields are added.
assertThat(msg.getField("target")).isEqualTo("1");
assertThat(msg.getField("one")).isEqualTo(1);
assertThat(msg.getField("two")).isEqualTo("2");
// Reserved fields are not overwritten!
assertThat(msg.getField("message")).isEqualTo("the message");
// Attempts to overwrite a reserved field are recorded as converter exception.
assertThat(extractor.getConverterExceptionCount()).isEqualTo(1);
}
use of org.graylog2.plugin.inputs.Converter in project graylog2-server by Graylog2.
the class ExtractorTest method testConvertersWithExceptions.
@Test
public void testConvertersWithExceptions() throws Exception {
final Converter converter1 = new TestConverter.Builder().callback(new Function<Object, Object>() {
@Nullable
@Override
public Object apply(Object input) {
throw new NullPointerException("EEK");
}
}).build();
final Converter converter2 = new TestConverter.Builder().callback(new Function<Object, Object>() {
@Nullable
@Override
public Object apply(Object input) {
return input + "2";
}
}).build();
final Converter converter3 = new TestConverter.Builder().callback(new Function<Object, Object>() {
@Nullable
@Override
public Object apply(Object input) {
throw new NullPointerException("EEK");
}
}).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);
// The two exceptions should have been recorded.
assertThat(extractor.getConverterExceptionCount()).isEqualTo(2);
// It ignores all converters which throw an exception but executes the ones that don't.
// TODO: Is this really the expected behaviour? The converters are executed in order and basically depend on the output of the previous. This might not work for all converters.
assertThat(msg.getField("target")).isEqualTo("converter2");
}
use of org.graylog2.plugin.inputs.Converter in project graylog2-server by Graylog2.
the class ExtractorTest method testMultipleConvertersWithFirstReturningNullValue.
@Test
public void testMultipleConvertersWithFirstReturningNullValue() throws Exception {
final Converter converter1 = new TestConverter.Builder().callback(new Function<Object, Object>() {
@Nullable
@Override
public Object apply(Object input) {
return null;
}
}).build();
final Converter converter2 = new TestConverter.Builder().callback(new Function<Object, Object>() {
@Nullable
@Override
public Object apply(Object input) {
return input + "2";
}
}).build();
final TestExtractor extractor = new TestExtractor.Builder().converters(Lists.newArrayList(converter1, converter2)).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);
// If the first converter returns null, the second will not be executed because the value is not a string anymore.
assertThat(msg.getField("target")).isNull();
}
Aggregations