Search in sources :

Example 16 with Converter

use of org.graylog2.plugin.inputs.Converter in project graylog2-server by Graylog2.

the class ExtractorTest method testConverters.

@Test
public void testConverters() throws Exception {
    final Converter converter = new TestConverter.Builder().callback(new Function<Object, Object>() {

        @Nullable
        @Override
        public Object apply(Object input) {
            return "converted";
        }
    }).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("message");
    extractor.runExtractor(msg);
    assertThat(msg.getField("target")).isEqualTo("converted");
}
Also used : Function(com.google.common.base.Function) Message(org.graylog2.plugin.Message) Callable(java.util.concurrent.Callable) Result(org.graylog2.plugin.inputs.Extractor.Result) Test(org.junit.Test)

Example 17 with Converter

use of org.graylog2.plugin.inputs.Converter in project graylog2-server by Graylog2.

the class ExtractorTest method testConvertersWithNonStringFieldValue.

@Test
public void testConvertersWithNonStringFieldValue() throws Exception {
    final Converter converter = new TestConverter.Builder().callback(new Function<Object, Object>() {

        @Nullable
        @Override
        public Object apply(Object input) {
            return "converted";
        }
    }).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(123, "target", -1, -1) };
        }
    }).build();
    final Message msg = createMessage("message");
    extractor.runExtractor(msg);
    // Only string values will be converted.
    assertThat(msg.getField("target")).isEqualTo(123);
}
Also used : Function(com.google.common.base.Function) Message(org.graylog2.plugin.Message) Callable(java.util.concurrent.Callable) Result(org.graylog2.plugin.inputs.Extractor.Result) Test(org.junit.Test)

Example 18 with Converter

use of org.graylog2.plugin.inputs.Converter in project graylog2-server by Graylog2.

the class ExtractorTest method testConvertersThatReturnNullValue.

@Test
public void testConvertersThatReturnNullValue() throws Exception {
    final Converter converter = new TestConverter.Builder().callback(new Function<Object, Object>() {

        @Nullable
        @Override
        public Object apply(Object input) {
            return null;
        }
    }).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("message");
    extractor.runExtractor(msg);
    assertThat(msg.getField("target")).isNull();
}
Also used : Function(com.google.common.base.Function) Message(org.graylog2.plugin.Message) Callable(java.util.concurrent.Callable) Result(org.graylog2.plugin.inputs.Extractor.Result) Test(org.junit.Test)

Example 19 with Converter

use of org.graylog2.plugin.inputs.Converter in project graylog2-server by Graylog2.

the class DateConverterTest method convertObeysTimeZone.

@Test
public void convertObeysTimeZone() throws Exception {
    final DateTimeZone timeZone = DateTimeZone.forOffsetHours(12);
    final Converter c = new DateConverter(config("YYYY-MM-dd HH:mm:ss", timeZone.toString()));
    final DateTime dateOnly = (DateTime) c.convert("2014-03-12 10:00:00");
    assertThat(dateOnly.getZone()).isEqualTo(timeZone);
    Assertions.assertThat(dateOnly).isEqualTo(new DateTime(2014, 3, 12, 10, 0, 0, timeZone)).isBefore(new DateTime(2014, 3, 13, 10, 0, 0, timeZone));
    final DateTime dateTime = (DateTime) c.convert("2014-03-12 12:34:00");
    assertThat(dateTime.getZone()).isEqualTo(timeZone);
    Assertions.assertThat(dateTime).isEqualTo(new DateTime(2014, 3, 12, 12, 34, 0, timeZone));
}
Also used : Converter(org.graylog2.plugin.inputs.Converter) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 20 with Converter

use of org.graylog2.plugin.inputs.Converter in project graylog2-server by Graylog2.

the class DateConverterTest method convertUsesEtcUTCIfTimeZoneSettingIsBlank.

@Test
public void convertUsesEtcUTCIfTimeZoneSettingIsBlank() throws Exception {
    final Converter c = new DateConverter(config("YYYY-MM-dd HH:mm:ss", " "));
    final DateTime dateOnly = (DateTime) c.convert("2014-03-12 10:00:00");
    assertThat(dateOnly.getZone()).isEqualTo(DateTimeZone.forID("Etc/UTC"));
}
Also used : Converter(org.graylog2.plugin.inputs.Converter) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)37 Converter (org.graylog2.plugin.inputs.Converter)22 Message (org.graylog2.plugin.Message)14 DateTime (org.joda.time.DateTime)10 Function (com.google.common.base.Function)8 Result (org.graylog2.plugin.inputs.Extractor.Result)8 Callable (java.util.concurrent.Callable)7 MetricRegistry (com.codahale.metrics.MetricRegistry)6 Extractor (org.graylog2.plugin.inputs.Extractor)6 BadRequestException (javax.ws.rs.BadRequestException)2 ConfigurationException (org.graylog2.ConfigurationException)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 BasicDBList (com.mongodb.BasicDBList)1 BasicDBObject (com.mongodb.BasicDBObject)1 DBObject (com.mongodb.DBObject)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 NotFoundException (org.graylog2.database.NotFoundException)1 ConverterFactory (org.graylog2.inputs.converters.ConverterFactory)1