Merge pull request #15803 from talevy/fix/daterror

[Ingest] throw exception when invalid locale is provided to the date processor
This commit is contained in:
Tal Levy 2016-01-11 10:56:59 -08:00
commit 7600afc82f
2 changed files with 39 additions and 2 deletions

View File

@ -27,6 +27,7 @@ import org.joda.time.DateTimeZone;
import org.joda.time.format.ISODateTimeFormat;
import java.util.ArrayList;
import java.util.IllformedLocaleException;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@ -121,7 +122,14 @@ public final class DateProcessor implements Processor {
String timezoneString = ConfigurationUtils.readOptionalStringProperty(config, "timezone");
DateTimeZone timezone = timezoneString == null ? DateTimeZone.UTC : DateTimeZone.forID(timezoneString);
String localeString = ConfigurationUtils.readOptionalStringProperty(config, "locale");
Locale locale = localeString == null ? Locale.ENGLISH : Locale.forLanguageTag(localeString);
Locale locale = Locale.ENGLISH;
if (localeString != null) {
try {
locale = (new Locale.Builder()).setLanguageTag(localeString).build();
} catch (IllformedLocaleException e) {
throw new IllegalArgumentException("Invalid language tag specified: " + localeString);
}
}
List<String> matchFormats = ConfigurationUtils.readList(config, "match_formats");
return new DateProcessor(timezone, locale, matchField, matchFormats, targetField);
}

View File

@ -19,7 +19,6 @@
package org.elasticsearch.ingest.processor;
import org.elasticsearch.ingest.processor.DateProcessor;
import org.elasticsearch.test.ESTestCase;
import org.joda.time.DateTimeZone;
@ -95,6 +94,21 @@ public class DateProcessorFactoryTests extends ESTestCase {
assertThat(processor.getLocale().toLanguageTag(), equalTo(locale.toLanguageTag()));
}
public void testParseInvalidLocale() throws Exception {
DateProcessor.Factory factory = new DateProcessor.Factory();
Map<String, Object> config = new HashMap<>();
String sourceField = randomAsciiOfLengthBetween(1, 10);
config.put("match_field", sourceField);
config.put("match_formats", Collections.singletonList("dd/MM/yyyyy"));
config.put("locale", "invalid_locale");
try {
factory.create(config);
fail("should fail with invalid locale");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), equalTo("Invalid language tag specified: invalid_locale"));
}
}
public void testParseTimezone() throws Exception {
DateProcessor.Factory factory = new DateProcessor.Factory();
Map<String, Object> config = new HashMap<>();
@ -108,6 +122,21 @@ public class DateProcessorFactoryTests extends ESTestCase {
assertThat(processor.getTimezone(), equalTo(timezone));
}
public void testParseInvalidTimezone() throws Exception {
DateProcessor.Factory factory = new DateProcessor.Factory();
Map<String, Object> config = new HashMap<>();
String sourceField = randomAsciiOfLengthBetween(1, 10);
config.put("match_field", sourceField);
config.put("match_formats", Collections.singletonList("dd/MM/yyyyy"));
config.put("timezone", "invalid_timezone");
try {
factory.create(config);
fail("invalid timezone should fail");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), equalTo("The datetime zone id 'invalid_timezone' is not recognised"));
}
}
//we generate a timezone out of the available ones in joda, some available in the jdk are not available in joda by default
private static DateTimeZone randomTimezone() {
List<String> ids = new ArrayList<>(DateTimeZone.getAvailableIDs());