Search in sources :

Example 21 with Converter

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

the class DateConverterTest method convertUsesEtcUTCIfTimeZoneSettingIsEmpty.

@Test
public void convertUsesEtcUTCIfTimeZoneSettingIsEmpty() 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)

Example 22 with Converter

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

the class FlexibleDateConverterTest method testConvert.

@Test
public void testConvert() throws Exception {
    Converter c = new FlexibleDateConverter(Collections.<String, Object>emptyMap());
    assertNull(c.convert(null));
    assertEquals(null, c.convert(""));
    assertEquals(null, c.convert("foo"));
    // Using startsWith here to avoid time zone problems in tests.
    assertTrue(c.convert("2014-3-12").toString().startsWith("2014-03-12T"));
    assertTrue(c.convert("2014-3-12 12:27").toString().startsWith("2014-03-12T12:27:00.000"));
    assertTrue(c.convert("Mar 12").toString().contains("-03-12T"));
    assertTrue(c.convert("Mar 12 2pm").toString().contains("-03-12T14:00:00.000"));
    assertTrue(c.convert("Mar 12 14:45:38").toString().contains("-03-12T14:45:38.000"));
    assertTrue(c.convert("Mar 2 13:48:18").toString().contains("-03-02T13:48:18.000"));
}
Also used : Converter(org.graylog2.plugin.inputs.Converter) Test(org.junit.Test)

Example 23 with Converter

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

the class FlexibleDateConverterTest method convertUsesEtcUTCIfTimeZoneSettingIsBlank.

@Test
public void convertUsesEtcUTCIfTimeZoneSettingIsBlank() throws Exception {
    Converter c = new FlexibleDateConverter(ImmutableMap.<String, Object>of("time_zone", " "));
    final DateTime dateOnly = (DateTime) c.convert("2014-3-12");
    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)

Example 24 with Converter

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

the class FlexibleDateConverterTest method convertObeysTimeZone.

@Test
public void convertObeysTimeZone() throws Exception {
    Converter c = new FlexibleDateConverter(ImmutableMap.<String, Object>of("time_zone", "+12:00"));
    final DateTime dateOnly = (DateTime) c.convert("2014-3-12");
    assertThat(dateOnly.getZone()).isEqualTo(DateTimeZone.forOffsetHours(12));
    Assertions.assertThat(dateOnly).isAfterOrEqualTo(new DateTime(2014, 3, 12, 0, 0, DateTimeZone.forOffsetHours(12))).isBefore(new DateTime(2014, 3, 13, 0, 0, DateTimeZone.forOffsetHours(12)));
    final DateTime dateTime = (DateTime) c.convert("2014-3-12 12:34");
    assertThat(dateTime.getZone()).isEqualTo(DateTimeZone.forOffsetHours(12));
    Assertions.assertThat(dateTime).isEqualTo(new DateTime(2014, 3, 12, 12, 34, DateTimeZone.forOffsetHours(12)));
    final DateTime textualDateTime = (DateTime) c.convert("Mar 12, 2014 2pm");
    assertThat(textualDateTime.getZone()).isEqualTo(DateTimeZone.forOffsetHours(12));
    Assertions.assertThat(textualDateTime).isEqualTo(new DateTime(2014, 3, 12, 14, 0, DateTimeZone.forOffsetHours(12)));
}
Also used : Converter(org.graylog2.plugin.inputs.Converter) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 25 with Converter

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

the class JsonExtractorTest method testRunWithFlattenedObject.

@Test
public void testRunWithFlattenedObject() throws Exception {
    final JsonExtractor jsonExtractor = new JsonExtractor(new MetricRegistry(), "json", "title", 0L, Extractor.CursorStrategy.COPY, "source", "target", ImmutableMap.<String, Object>of("flatten", true), "user", Collections.<Converter>emptyList(), Extractor.ConditionType.NONE, "");
    final String value = "{" + "\"object\": {" + "\"text\": \"foobar\", " + "\"number\": 1234.5678, " + "\"bool\": true, " + "\"null\": null, " + "\"nested\": {\"text\": \"foobar\"}" + "}" + "}";
    final Extractor.Result[] results = jsonExtractor.run(value);
    assertThat(results).contains(new Extractor.Result("text=foobar, number=1234.5678, bool=true, nested={text=foobar}", "object", -1, -1));
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) Extractor(org.graylog2.plugin.inputs.Extractor) 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