From 6175b9efda19c2466be91687a26f40958d662040 Mon Sep 17 00:00:00 2001 From: uboness Date: Thu, 28 May 2015 00:56:59 +0200 Subject: [PATCH] Disallow negative time value settings - Consolidated setting validation under `WatcherSettingsValidation` - `WatcherSettingsException` is now only used for settings errors We need this consolidation as Guice doesn't deal well with exceptions in constructors. So instead, `WatcherSettingsValidation` can be injected and used as a registry for settings errors and then, since it's a service, if there any registered errors, it'll throw `WatcherSettingsException` when it's started. Fixes elastic/elasticsearch#539 Original commit: elastic/x-pack-elasticsearch@2c1895d18c5c9559f3552d7c4abb106ab8d7b0a3 --- .../elasticsearch/watcher/WatcherModule.java | 2 + .../elasticsearch/watcher/WatcherPlugin.java | 4 +- .../watcher/WatcherSettingsException.java | 21 ------- .../watcher/actions/ActionRegistry.java | 2 +- .../watcher/execution/ExecutionService.java | 9 ++- .../watcher/history/WatchRecord.java | 4 +- .../watcher/support/WatcherDateTimeUtils.java | 21 ++++--- .../support/{ => validation}/Validation.java | 2 +- .../validation/WatcherSettingsException.java | 29 ++++++++++ .../validation/WatcherSettingsValidation.java | 55 +++++++++++++++++++ .../actions/ack/AckWatchRequest.java | 2 +- .../actions/delete/DeleteWatchRequest.java | 2 +- .../actions/execute/ExecuteWatchRequest.java | 2 +- .../actions/get/GetWatchRequest.java | 2 +- .../actions/put/PutWatchRequest.java | 2 +- .../trigger/schedule/CronSchedule.java | 11 ++-- .../trigger/schedule/DailySchedule.java | 7 +-- .../trigger/schedule/HourlySchedule.java | 13 ++--- .../trigger/schedule/IntervalSchedule.java | 7 +-- .../trigger/schedule/MonthlySchedule.java | 7 +-- .../trigger/schedule/ScheduleRegistry.java | 7 +-- .../watcher/trigger/schedule/Schedules.java | 2 +- .../trigger/schedule/WeeklySchedule.java | 7 +-- .../trigger/schedule/YearlySchedule.java | 7 +-- .../trigger/schedule/support/DayTimes.java | 6 +- .../trigger/schedule/support/MonthTimes.java | 4 +- .../trigger/schedule/support/YearTimes.java | 4 +- .../execution/ExecutionServiceTests.java | 4 +- .../support/WatcherDateTimeUtilsTests.java | 44 +++++++++++++-- .../test/AbstractWatcherIntegrationTests.java | 3 - .../test/integration/BasicWatcherTests.java | 9 ++- .../trigger/schedule/CronScheduleTests.java | 9 ++- .../trigger/schedule/DailyScheduleTests.java | 11 ++-- .../trigger/schedule/HourlyScheduleTests.java | 13 ++--- .../schedule/IntervalScheduleTests.java | 5 +- .../schedule/MonthlyScheduleTests.java | 5 +- .../trigger/schedule/WeeklyScheduleTests.java | 5 +- .../trigger/schedule/YearlyScheduleTests.java | 5 +- 38 files changed, 223 insertions(+), 131 deletions(-) delete mode 100644 src/main/java/org/elasticsearch/watcher/WatcherSettingsException.java rename src/main/java/org/elasticsearch/watcher/support/{ => validation}/Validation.java (96%) create mode 100644 src/main/java/org/elasticsearch/watcher/support/validation/WatcherSettingsException.java create mode 100644 src/main/java/org/elasticsearch/watcher/support/validation/WatcherSettingsValidation.java diff --git a/src/main/java/org/elasticsearch/watcher/WatcherModule.java b/src/main/java/org/elasticsearch/watcher/WatcherModule.java index c8486393512..f22db9af117 100644 --- a/src/main/java/org/elasticsearch/watcher/WatcherModule.java +++ b/src/main/java/org/elasticsearch/watcher/WatcherModule.java @@ -26,6 +26,7 @@ import org.elasticsearch.watcher.support.http.HttpClientModule; import org.elasticsearch.watcher.support.init.InitializingModule; import org.elasticsearch.watcher.support.secret.SecretModule; import org.elasticsearch.watcher.support.template.TemplateModule; +import org.elasticsearch.watcher.support.validation.WatcherSettingsValidation; import org.elasticsearch.watcher.transform.TransformModule; import org.elasticsearch.watcher.transport.WatcherTransportModule; import org.elasticsearch.watcher.trigger.TriggerModule; @@ -67,6 +68,7 @@ public class WatcherModule extends AbstractModule implements SpawnModules { protected void configure() { bind(WatcherLifeCycleService.class).asEagerSingleton(); bind(TemplateUtils.class).asEagerSingleton(); + bind(WatcherSettingsValidation.class).asEagerSingleton(); } } diff --git a/src/main/java/org/elasticsearch/watcher/WatcherPlugin.java b/src/main/java/org/elasticsearch/watcher/WatcherPlugin.java index 9bedcc508dc..1fa9d4760c6 100644 --- a/src/main/java/org/elasticsearch/watcher/WatcherPlugin.java +++ b/src/main/java/org/elasticsearch/watcher/WatcherPlugin.java @@ -17,6 +17,7 @@ import org.elasticsearch.watcher.history.HistoryModule; import org.elasticsearch.watcher.license.LicenseService; import org.elasticsearch.watcher.support.http.HttpClient; import org.elasticsearch.watcher.support.init.InitializingService; +import org.elasticsearch.watcher.support.validation.WatcherSettingsValidation; import java.util.Collection; @@ -67,7 +68,8 @@ public class WatcherPlugin extends AbstractPlugin { InitializingService.class, LicenseService.class, InternalEmailService.class, - HttpClient.class); + HttpClient.class, + WatcherSettingsValidation.class); } @Override diff --git a/src/main/java/org/elasticsearch/watcher/WatcherSettingsException.java b/src/main/java/org/elasticsearch/watcher/WatcherSettingsException.java deleted file mode 100644 index f6378db554f..00000000000 --- a/src/main/java/org/elasticsearch/watcher/WatcherSettingsException.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.watcher; - -/** - * - */ -public class WatcherSettingsException extends WatcherException { - - public WatcherSettingsException(String msg, Object... args) { - super(msg, args); - } - - public WatcherSettingsException(String msg, Throwable cause, Object... args) { - super(msg, cause, args); - } - -} diff --git a/src/main/java/org/elasticsearch/watcher/actions/ActionRegistry.java b/src/main/java/org/elasticsearch/watcher/actions/ActionRegistry.java index fed91e2033a..4e766047a15 100644 --- a/src/main/java/org/elasticsearch/watcher/actions/ActionRegistry.java +++ b/src/main/java/org/elasticsearch/watcher/actions/ActionRegistry.java @@ -10,7 +10,7 @@ import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.watcher.execution.Wid; import org.elasticsearch.watcher.license.LicenseService; -import org.elasticsearch.watcher.support.Validation; +import org.elasticsearch.watcher.support.validation.Validation; import org.elasticsearch.watcher.support.clock.Clock; import org.elasticsearch.watcher.transform.TransformRegistry; diff --git a/src/main/java/org/elasticsearch/watcher/execution/ExecutionService.java b/src/main/java/org/elasticsearch/watcher/execution/ExecutionService.java index eb8336a82a9..ff520b29d15 100644 --- a/src/main/java/org/elasticsearch/watcher/execution/ExecutionService.java +++ b/src/main/java/org/elasticsearch/watcher/execution/ExecutionService.java @@ -24,6 +24,7 @@ import org.elasticsearch.watcher.history.HistoryStore; import org.elasticsearch.watcher.history.WatchRecord; import org.elasticsearch.watcher.input.Input; import org.elasticsearch.watcher.support.clock.Clock; +import org.elasticsearch.watcher.support.validation.WatcherSettingsValidation; import org.elasticsearch.watcher.trigger.TriggerEvent; import org.elasticsearch.watcher.watch.Watch; import org.elasticsearch.watcher.watch.WatchLockService; @@ -53,15 +54,17 @@ public class ExecutionService extends AbstractComponent { @Inject public ExecutionService(Settings settings, HistoryStore historyStore, WatchExecutor executor, WatchStore watchStore, - WatchLockService watchLockService, Clock clock) { + WatchLockService watchLockService, Clock clock, WatcherSettingsValidation settingsValidation) { super(settings); this.historyStore = historyStore; this.executor = executor; this.watchStore = watchStore; this.watchLockService = watchLockService; this.clock = clock; - TimeValue throttlePeriod = componentSettings.getAsTime("default_throttle_period", TimeValue.timeValueSeconds(5)); - this.defaultThrottlePeriod = throttlePeriod.millis() == 0 ? null : throttlePeriod; + this.defaultThrottlePeriod = componentSettings.getAsTime("default_throttle_period", TimeValue.timeValueSeconds(5)); + if (ExecutionService.this.defaultThrottlePeriod.millis() < 0) { + settingsValidation.addError("watcher.execution.default_throttle_period", "time value cannot be negative"); + } } public void start(ClusterState state) { diff --git a/src/main/java/org/elasticsearch/watcher/history/WatchRecord.java b/src/main/java/org/elasticsearch/watcher/history/WatchRecord.java index c393285ac4e..a9c06601336 100644 --- a/src/main/java/org/elasticsearch/watcher/history/WatchRecord.java +++ b/src/main/java/org/elasticsearch/watcher/history/WatchRecord.java @@ -17,7 +17,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.watcher.WatcherException; -import org.elasticsearch.watcher.WatcherSettingsException; +import org.elasticsearch.watcher.support.validation.WatcherSettingsException; import org.elasticsearch.watcher.actions.ActionRegistry; import org.elasticsearch.watcher.condition.Condition; import org.elasticsearch.watcher.condition.ConditionRegistry; @@ -197,7 +197,7 @@ public class WatchRecord implements ToXContent { try { return valueOf(id.toUpperCase(Locale.ROOT)); } catch (IllegalArgumentException iae) { - throw new WatcherSettingsException("unknown watch record state [{}]", id); + throw new HistoryException("unknown watch record state [{}]", id); } } diff --git a/src/main/java/org/elasticsearch/watcher/support/WatcherDateTimeUtils.java b/src/main/java/org/elasticsearch/watcher/support/WatcherDateTimeUtils.java index 9ebc95362a1..61bce852986 100644 --- a/src/main/java/org/elasticsearch/watcher/support/WatcherDateTimeUtils.java +++ b/src/main/java/org/elasticsearch/watcher/support/WatcherDateTimeUtils.java @@ -12,14 +12,11 @@ import org.elasticsearch.common.joda.DateMathParser; import org.elasticsearch.common.joda.FormatDateTimeFormatter; import org.elasticsearch.common.joda.time.DateTime; import org.elasticsearch.common.joda.time.DateTimeZone; -import org.elasticsearch.common.joda.time.PeriodType; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.mapper.core.DateFieldMapper; import org.elasticsearch.watcher.WatcherException; -import org.elasticsearch.watcher.actions.ActionException; -import org.elasticsearch.watcher.rest.action.RestExecuteWatchAction; import org.elasticsearch.watcher.support.clock.Clock; import java.io.IOException; @@ -130,12 +127,20 @@ public class WatcherDateTimeUtils { return defaultValue; } if (token == XContentParser.Token.VALUE_NUMBER) { - return new TimeValue(parser.longValue(), defaultTimeUnit); - } else if (token == XContentParser.Token.VALUE_STRING) { - return TimeValue.parseTimeValue(parser.text(), defaultValue); - } else { - throw new ParseException("could not parse time value. expected either a string or a numeric value but found [{}] instead", token); + long millis = parser.longValue(); + if (millis < 0) { + throw new ParseException("could not parse milli-seconds time value [{}]. Time value cannot be negative.", millis); + } + return new TimeValue(millis, defaultTimeUnit); } + if (token == XContentParser.Token.VALUE_STRING) { + TimeValue value = TimeValue.parseTimeValue(parser.text(), defaultValue); + if (value.millis() < 0) { + throw new ParseException("could not parse time value [{}]. Time value cannot be negative.", parser.text()); + } + return value; + } + throw new ParseException("could not parse time value. expected either a string or a numeric value but found [{}] instead", token); } public static class ParseException extends WatcherException { diff --git a/src/main/java/org/elasticsearch/watcher/support/Validation.java b/src/main/java/org/elasticsearch/watcher/support/validation/Validation.java similarity index 96% rename from src/main/java/org/elasticsearch/watcher/support/Validation.java rename to src/main/java/org/elasticsearch/watcher/support/validation/Validation.java index d50ecd6504e..cb2845c9d83 100644 --- a/src/main/java/org/elasticsearch/watcher/support/Validation.java +++ b/src/main/java/org/elasticsearch/watcher/support/validation/Validation.java @@ -3,7 +3,7 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -package org.elasticsearch.watcher.support; +package org.elasticsearch.watcher.support.validation; import org.elasticsearch.common.logging.support.LoggerMessageFormat; diff --git a/src/main/java/org/elasticsearch/watcher/support/validation/WatcherSettingsException.java b/src/main/java/org/elasticsearch/watcher/support/validation/WatcherSettingsException.java new file mode 100644 index 00000000000..a9a349db9a2 --- /dev/null +++ b/src/main/java/org/elasticsearch/watcher/support/validation/WatcherSettingsException.java @@ -0,0 +1,29 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.watcher.support.validation; + +import org.elasticsearch.watcher.WatcherException; + +/** + * + */ +public class WatcherSettingsException extends WatcherException { + + public WatcherSettingsException() { + super("invalid settings"); + } + + public void addError(String error) { + addSuppressed(new InvalidSettingException(error)); + } + + static class InvalidSettingException extends WatcherException { + + public InvalidSettingException(String error) { + super(error); + } + } +} diff --git a/src/main/java/org/elasticsearch/watcher/support/validation/WatcherSettingsValidation.java b/src/main/java/org/elasticsearch/watcher/support/validation/WatcherSettingsValidation.java new file mode 100644 index 00000000000..fb9d2355c31 --- /dev/null +++ b/src/main/java/org/elasticsearch/watcher/support/validation/WatcherSettingsValidation.java @@ -0,0 +1,55 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.watcher.support.validation; + +import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.common.component.AbstractLifecycleComponent; +import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.logging.support.LoggerMessageFormat; +import org.elasticsearch.common.settings.Settings; + +import java.util.*; + +/** + * + */ +public class WatcherSettingsValidation extends AbstractLifecycleComponent { + + private List errors = new ArrayList<>(); + + @Inject + public WatcherSettingsValidation(Settings settings) { + super(settings); + } + + @Override + protected void doStart() throws ElasticsearchException { + validate(); + } + + @Override + protected void doStop() throws ElasticsearchException { + } + + @Override + protected void doClose() throws ElasticsearchException { + } + + public void addError(String setting, String reason) { + errors.add(LoggerMessageFormat.format("", "invalid [{}] setting value [{}]. {}", setting, settings.get(setting), reason)); + } + + private void validate() throws ElasticsearchException { + if (errors.isEmpty()) { + return; + } + WatcherSettingsException exception = new WatcherSettingsException(); + for (String error : errors) { + exception.addError(error); + } + throw exception; + } +} diff --git a/src/main/java/org/elasticsearch/watcher/transport/actions/ack/AckWatchRequest.java b/src/main/java/org/elasticsearch/watcher/transport/actions/ack/AckWatchRequest.java index 28fb054b9ab..9628648929b 100644 --- a/src/main/java/org/elasticsearch/watcher/transport/actions/ack/AckWatchRequest.java +++ b/src/main/java/org/elasticsearch/watcher/transport/actions/ack/AckWatchRequest.java @@ -12,7 +12,7 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.watcher.support.Validation; +import org.elasticsearch.watcher.support.validation.Validation; import java.io.IOException; diff --git a/src/main/java/org/elasticsearch/watcher/transport/actions/delete/DeleteWatchRequest.java b/src/main/java/org/elasticsearch/watcher/transport/actions/delete/DeleteWatchRequest.java index 23b59546657..9a99c5061a3 100644 --- a/src/main/java/org/elasticsearch/watcher/transport/actions/delete/DeleteWatchRequest.java +++ b/src/main/java/org/elasticsearch/watcher/transport/actions/delete/DeleteWatchRequest.java @@ -12,7 +12,7 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.lucene.uid.Versions; import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.watcher.support.Validation; +import org.elasticsearch.watcher.support.validation.Validation; import java.io.IOException; diff --git a/src/main/java/org/elasticsearch/watcher/transport/actions/execute/ExecuteWatchRequest.java b/src/main/java/org/elasticsearch/watcher/transport/actions/execute/ExecuteWatchRequest.java index 904628c2357..e2bcd480751 100644 --- a/src/main/java/org/elasticsearch/watcher/transport/actions/execute/ExecuteWatchRequest.java +++ b/src/main/java/org/elasticsearch/watcher/transport/actions/execute/ExecuteWatchRequest.java @@ -14,7 +14,7 @@ import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.watcher.support.Validation; +import org.elasticsearch.watcher.support.validation.Validation; import org.elasticsearch.watcher.trigger.TriggerEvent; import org.elasticsearch.watcher.execution.ActionExecutionMode; diff --git a/src/main/java/org/elasticsearch/watcher/transport/actions/get/GetWatchRequest.java b/src/main/java/org/elasticsearch/watcher/transport/actions/get/GetWatchRequest.java index aa0ce25204d..08e6dcd3ca8 100644 --- a/src/main/java/org/elasticsearch/watcher/transport/actions/get/GetWatchRequest.java +++ b/src/main/java/org/elasticsearch/watcher/transport/actions/get/GetWatchRequest.java @@ -12,7 +12,7 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.lucene.uid.Versions; import org.elasticsearch.index.VersionType; -import org.elasticsearch.watcher.support.Validation; +import org.elasticsearch.watcher.support.validation.Validation; import java.io.IOException; diff --git a/src/main/java/org/elasticsearch/watcher/transport/actions/put/PutWatchRequest.java b/src/main/java/org/elasticsearch/watcher/transport/actions/put/PutWatchRequest.java index 4ef589602b4..9904438d224 100644 --- a/src/main/java/org/elasticsearch/watcher/transport/actions/put/PutWatchRequest.java +++ b/src/main/java/org/elasticsearch/watcher/transport/actions/put/PutWatchRequest.java @@ -15,7 +15,7 @@ import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.watcher.support.Validation; +import org.elasticsearch.watcher.support.validation.Validation; import java.io.IOException; diff --git a/src/main/java/org/elasticsearch/watcher/trigger/schedule/CronSchedule.java b/src/main/java/org/elasticsearch/watcher/trigger/schedule/CronSchedule.java index 24edd15dc93..f149e24282c 100644 --- a/src/main/java/org/elasticsearch/watcher/trigger/schedule/CronSchedule.java +++ b/src/main/java/org/elasticsearch/watcher/trigger/schedule/CronSchedule.java @@ -7,7 +7,6 @@ package org.elasticsearch.watcher.trigger.schedule; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.watcher.WatcherSettingsException; import java.io.IOException; import java.util.ArrayList; @@ -56,24 +55,24 @@ public class CronSchedule extends CronnableSchedule { crons.add(parser.text()); break; default: - throw new WatcherSettingsException("could not parse [cron] schedule. expected a string value in the cron array but found [" + token + "]"); + throw new ScheduleTriggerException("could not parse [cron] schedule. expected a string value in the cron array but found [" + token + "]"); } } if (crons.isEmpty()) { - throw new WatcherSettingsException("could not parse [cron] schedule. no cron expression found in cron array"); + throw new ScheduleTriggerException("could not parse [cron] schedule. no cron expression found in cron array"); } return new CronSchedule(crons.toArray(new String[crons.size()])); } else { - throw new WatcherSettingsException("could not parse [cron] schedule. expected either a cron string value or an array of cron string values, but found [" + token + "]"); + throw new ScheduleTriggerException("could not parse [cron] schedule. expected either a cron string value or an array of cron string values, but found [" + token + "]"); } } catch (ValidationException ve) { - throw new WatcherSettingsException("could not parse [cron] schedule. invalid cron expression [" + ve.expression + "]", ve); + throw new ScheduleTriggerException("could not parse [cron] schedule. invalid cron expression [" + ve.expression + "]", ve); } } } - public static class ValidationException extends WatcherSettingsException { + public static class ValidationException extends ScheduleTriggerException { private String expression; diff --git a/src/main/java/org/elasticsearch/watcher/trigger/schedule/DailySchedule.java b/src/main/java/org/elasticsearch/watcher/trigger/schedule/DailySchedule.java index 7239c40a369..8aec11b0f19 100644 --- a/src/main/java/org/elasticsearch/watcher/trigger/schedule/DailySchedule.java +++ b/src/main/java/org/elasticsearch/watcher/trigger/schedule/DailySchedule.java @@ -8,7 +8,6 @@ package org.elasticsearch.watcher.trigger.schedule; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.watcher.WatcherSettingsException; import org.elasticsearch.watcher.trigger.schedule.support.DayTimes; import java.io.IOException; @@ -96,19 +95,19 @@ public class DailySchedule extends CronnableSchedule { try { times.add(DayTimes.parse(parser, token)); } catch (DayTimes.ParseException pe) { - throw new WatcherSettingsException("could not parse [daily] schedule. invalid time value for field [at] - [" + token + "]", pe); + throw new ScheduleTriggerException("could not parse [daily] schedule. invalid time value for field [at] - [" + token + "]", pe); } } else { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { try { times.add(DayTimes.parse(parser, token)); } catch (DayTimes.ParseException pe) { - throw new WatcherSettingsException("could not parse [daily] schedule. invalid time value for field [at] - [" + token + "]", pe); + throw new ScheduleTriggerException("could not parse [daily] schedule. invalid time value for field [at] - [" + token + "]", pe); } } } } else { - throw new WatcherSettingsException("could not parse [daily] schedule. unexpected field [" + currentFieldName + "]"); + throw new ScheduleTriggerException("could not parse [daily] schedule. unexpected field [" + currentFieldName + "]"); } } diff --git a/src/main/java/org/elasticsearch/watcher/trigger/schedule/HourlySchedule.java b/src/main/java/org/elasticsearch/watcher/trigger/schedule/HourlySchedule.java index 732b39f0d71..cc61e0a85d3 100644 --- a/src/main/java/org/elasticsearch/watcher/trigger/schedule/HourlySchedule.java +++ b/src/main/java/org/elasticsearch/watcher/trigger/schedule/HourlySchedule.java @@ -9,7 +9,6 @@ import org.elasticsearch.common.ParseField; import org.elasticsearch.common.primitives.Ints; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.watcher.WatcherSettingsException; import org.elasticsearch.watcher.trigger.schedule.support.DayTimes; import java.io.IOException; @@ -70,7 +69,7 @@ public class HourlySchedule extends CronnableSchedule { sb.append(","); } if (!validMinute(minutes[i])) { - throw new WatcherSettingsException("invalid hourly minute [" + minutes[i] + "]. minute must be between 0 and 59 incl."); + throw new ScheduleTriggerException("invalid hourly minute [" + minutes[i] + "]. minute must be between 0 and 59 incl."); } sb.append(minutes[i]); } @@ -100,27 +99,27 @@ public class HourlySchedule extends CronnableSchedule { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (currentFieldName == null) { - throw new WatcherSettingsException("could not parse [{}] schedule. unexpected token [{}]", TYPE, token); + throw new ScheduleTriggerException("could not parse [{}] schedule. unexpected token [{}]", TYPE, token); } else if (MINUTE_FIELD.match(currentFieldName)) { if (token.isValue()) { try { minutes.add(DayTimes.parseMinuteValue(parser, token)); } catch (DayTimes.ParseException pe) { - throw new WatcherSettingsException("could not parse [hourly] schedule. invalid value for [minute]", pe); + throw new ScheduleTriggerException("could not parse [hourly] schedule. invalid value for [minute]", pe); } } else if (token == XContentParser.Token.START_ARRAY) { while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { try { minutes.add(DayTimes.parseMinuteValue(parser, token)); } catch (DayTimes.ParseException pe) { - throw new WatcherSettingsException("could not parse [hourly] schedule. invalid value for [minute]", pe); + throw new ScheduleTriggerException("could not parse [hourly] schedule. invalid value for [minute]", pe); } } } else { - throw new WatcherSettingsException("could not parse [hourly] schedule. invalid minute value. expected either string/value or an array of string/number values, but found [" + token + "]"); + throw new ScheduleTriggerException("could not parse [hourly] schedule. invalid minute value. expected either string/value or an array of string/number values, but found [" + token + "]"); } } else { - throw new WatcherSettingsException("could not parse [hourly] schedule. unexpected field [" + currentFieldName + "]"); + throw new ScheduleTriggerException("could not parse [hourly] schedule. unexpected field [" + currentFieldName + "]"); } } diff --git a/src/main/java/org/elasticsearch/watcher/trigger/schedule/IntervalSchedule.java b/src/main/java/org/elasticsearch/watcher/trigger/schedule/IntervalSchedule.java index 95ba8bac2f2..5af018869b8 100644 --- a/src/main/java/org/elasticsearch/watcher/trigger/schedule/IntervalSchedule.java +++ b/src/main/java/org/elasticsearch/watcher/trigger/schedule/IntervalSchedule.java @@ -8,7 +8,6 @@ package org.elasticsearch.watcher.trigger.schedule; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.watcher.WatcherSettingsException; import java.io.IOException; import java.util.Locale; @@ -93,7 +92,7 @@ public class IntervalSchedule implements Schedule { String value = parser.text(); return new IntervalSchedule(Interval.parse(value)); } - throw new WatcherSettingsException("could not parse [interval] schedule. expected either a numeric value " + + throw new ScheduleTriggerException("could not parse [interval] schedule. expected either a numeric value " + "(millis) or a string value representing time value (e.g. '5s'), but found [" + token + "]"); } } @@ -133,7 +132,7 @@ public class IntervalSchedule implements Schedule { try { return Long.parseLong(num); } catch (NumberFormatException nfe) { - throw new WatcherSettingsException("could not parse [interval] schedule. could not parse [" + throw new ScheduleTriggerException("could not parse [interval] schedule. could not parse [" + num + "] as a " + name().toLowerCase(Locale.ROOT) + " duration"); } } @@ -196,7 +195,7 @@ public class IntervalSchedule implements Schedule { return new Interval(unit.parse(value), unit); } } - throw new WatcherSettingsException("could not parse [interval] schedule. unrecognized interval format [" + value + "]"); + throw new ScheduleTriggerException("could not parse [interval] schedule. unrecognized interval format [" + value + "]"); } } } diff --git a/src/main/java/org/elasticsearch/watcher/trigger/schedule/MonthlySchedule.java b/src/main/java/org/elasticsearch/watcher/trigger/schedule/MonthlySchedule.java index da9595214d8..182d8f7b5b2 100644 --- a/src/main/java/org/elasticsearch/watcher/trigger/schedule/MonthlySchedule.java +++ b/src/main/java/org/elasticsearch/watcher/trigger/schedule/MonthlySchedule.java @@ -7,7 +7,6 @@ package org.elasticsearch.watcher.trigger.schedule; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.watcher.WatcherSettingsException; import org.elasticsearch.watcher.trigger.schedule.support.MonthTimes; import java.io.IOException; @@ -83,7 +82,7 @@ public class MonthlySchedule extends CronnableSchedule { try { return new MonthlySchedule(MonthTimes.parse(parser, parser.currentToken())); } catch (MonthTimes.ParseException pe) { - throw new WatcherSettingsException("could not parse [monthly] schedule. invalid month times", pe); + throw new ScheduleTriggerException("could not parse [monthly] schedule. invalid month times", pe); } } if (parser.currentToken() == XContentParser.Token.START_ARRAY) { @@ -93,12 +92,12 @@ public class MonthlySchedule extends CronnableSchedule { try { times.add(MonthTimes.parse(parser, token)); } catch (MonthTimes.ParseException pe) { - throw new WatcherSettingsException("could not parse [monthly] schedule. invalid month times", pe); + throw new ScheduleTriggerException("could not parse [monthly] schedule. invalid month times", pe); } } return times.isEmpty() ? new MonthlySchedule() : new MonthlySchedule(times.toArray(new MonthTimes[times.size()])); } - throw new WatcherSettingsException("could not parse [monthly] schedule. expected either an object or an array " + + throw new ScheduleTriggerException("could not parse [monthly] schedule. expected either an object or an array " + "of objects representing month times, but found [" + parser.currentToken() + "] instead"); } } diff --git a/src/main/java/org/elasticsearch/watcher/trigger/schedule/ScheduleRegistry.java b/src/main/java/org/elasticsearch/watcher/trigger/schedule/ScheduleRegistry.java index 9eb2a2f300a..177a3e3b646 100644 --- a/src/main/java/org/elasticsearch/watcher/trigger/schedule/ScheduleRegistry.java +++ b/src/main/java/org/elasticsearch/watcher/trigger/schedule/ScheduleRegistry.java @@ -8,7 +8,6 @@ package org.elasticsearch.watcher.trigger.schedule; import org.elasticsearch.common.collect.ImmutableMap; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.watcher.WatcherSettingsException; import java.io.IOException; import java.util.Map; @@ -40,11 +39,11 @@ public class ScheduleRegistry { } else if (type != null) { schedule = parse(context, type, parser); } else { - throw new WatcherSettingsException("could not parse schedule. expected a schedule type field, but found [" + token + "]"); + throw new ScheduleTriggerException("could not parse schedule. expected a schedule type field, but found [" + token + "]"); } } if (schedule == null) { - throw new WatcherSettingsException("could not parse schedule. expected a schedule type field, but no fields were found"); + throw new ScheduleTriggerException("could not parse schedule. expected a schedule type field, but no fields were found"); } return schedule; } @@ -52,7 +51,7 @@ public class ScheduleRegistry { public Schedule parse(String context, String type, XContentParser parser) throws IOException { Schedule.Parser scheduleParser = parsers.get(type); if (scheduleParser == null) { - throw new WatcherSettingsException("could not parse schedule for [" + context + "]. unknown schedule type [" + type + "]"); + throw new ScheduleTriggerException("could not parse schedule for [" + context + "]. unknown schedule type [" + type + "]"); } return scheduleParser.parse(parser); } diff --git a/src/main/java/org/elasticsearch/watcher/trigger/schedule/Schedules.java b/src/main/java/org/elasticsearch/watcher/trigger/schedule/Schedules.java index ef8779f7278..fb3d7917577 100644 --- a/src/main/java/org/elasticsearch/watcher/trigger/schedule/Schedules.java +++ b/src/main/java/org/elasticsearch/watcher/trigger/schedule/Schedules.java @@ -58,7 +58,7 @@ public class Schedules { * @param minutes the minutes within the hour that the schedule should trigger at. values must be * between 0 and 59 (inclusive). * @return the newly created hourly schedule - * @throws org.elasticsearch.watcher.WatcherSettingsException if any of the provided minutes are out of valid range + * @throws ScheduleTriggerException if any of the provided minutes are out of valid range */ public static HourlySchedule hourly(int... minutes) { return new HourlySchedule(minutes); diff --git a/src/main/java/org/elasticsearch/watcher/trigger/schedule/WeeklySchedule.java b/src/main/java/org/elasticsearch/watcher/trigger/schedule/WeeklySchedule.java index 99bfa30e282..b422573e782 100644 --- a/src/main/java/org/elasticsearch/watcher/trigger/schedule/WeeklySchedule.java +++ b/src/main/java/org/elasticsearch/watcher/trigger/schedule/WeeklySchedule.java @@ -7,7 +7,6 @@ package org.elasticsearch.watcher.trigger.schedule; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.watcher.WatcherSettingsException; import org.elasticsearch.watcher.trigger.schedule.support.WeekTimes; import java.io.IOException; @@ -83,7 +82,7 @@ public class WeeklySchedule extends CronnableSchedule { try { return new WeeklySchedule(WeekTimes.parse(parser, parser.currentToken())); } catch (WeekTimes.ParseException pe) { - throw new WatcherSettingsException("could not parse [weekly] schedule. invalid weekly times", pe); + throw new ScheduleTriggerException("could not parse [weekly] schedule. invalid weekly times", pe); } } if (parser.currentToken() == XContentParser.Token.START_ARRAY) { @@ -93,12 +92,12 @@ public class WeeklySchedule extends CronnableSchedule { try { times.add(WeekTimes.parse(parser, token)); } catch (WeekTimes.ParseException pe) { - throw new WatcherSettingsException("could not parse [weekly] schedule. invalid weekly times", pe); + throw new ScheduleTriggerException("could not parse [weekly] schedule. invalid weekly times", pe); } } return times.isEmpty() ? new WeeklySchedule() : new WeeklySchedule(times.toArray(new WeekTimes[times.size()])); } - throw new WatcherSettingsException("could not parse [weekly] schedule. expected either an object or an array " + + throw new ScheduleTriggerException("could not parse [weekly] schedule. expected either an object or an array " + "of objects representing weekly times, but found [" + parser.currentToken() + "] instead"); } } diff --git a/src/main/java/org/elasticsearch/watcher/trigger/schedule/YearlySchedule.java b/src/main/java/org/elasticsearch/watcher/trigger/schedule/YearlySchedule.java index bd814fbffba..598bdac7016 100644 --- a/src/main/java/org/elasticsearch/watcher/trigger/schedule/YearlySchedule.java +++ b/src/main/java/org/elasticsearch/watcher/trigger/schedule/YearlySchedule.java @@ -7,7 +7,6 @@ package org.elasticsearch.watcher.trigger.schedule; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.watcher.WatcherSettingsException; import org.elasticsearch.watcher.trigger.schedule.support.YearTimes; import java.io.IOException; @@ -83,7 +82,7 @@ public class YearlySchedule extends CronnableSchedule { try { return new YearlySchedule(YearTimes.parse(parser, parser.currentToken())); } catch (YearTimes.ParseException pe) { - throw new WatcherSettingsException("could not parse [yearly] schedule. invalid year times", pe); + throw new ScheduleTriggerException("could not parse [yearly] schedule. invalid year times", pe); } } if (parser.currentToken() == XContentParser.Token.START_ARRAY) { @@ -93,12 +92,12 @@ public class YearlySchedule extends CronnableSchedule { try { times.add(YearTimes.parse(parser, token)); } catch (YearTimes.ParseException pe) { - throw new WatcherSettingsException("could not parse [yearly] schedule. invalid year times", pe); + throw new ScheduleTriggerException("could not parse [yearly] schedule. invalid year times", pe); } } return times.isEmpty() ? new YearlySchedule() : new YearlySchedule(times.toArray(new YearTimes[times.size()])); } - throw new WatcherSettingsException("could not parse [yearly] schedule. expected either an object or an array " + + throw new ScheduleTriggerException("could not parse [yearly] schedule. expected either an object or an array " + "of objects representing year times, but found [" + parser.currentToken() + "] instead"); } } diff --git a/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/DayTimes.java b/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/DayTimes.java index ae716ebee5b..16dcdf68f1e 100644 --- a/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/DayTimes.java +++ b/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/DayTimes.java @@ -9,7 +9,7 @@ import org.elasticsearch.common.primitives.Ints; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.watcher.WatcherException; -import org.elasticsearch.watcher.WatcherSettingsException; +import org.elasticsearch.watcher.trigger.schedule.ScheduleTriggerException; import java.io.IOException; import java.util.ArrayList; @@ -99,12 +99,12 @@ public class DayTimes implements Times { public void validate() { for (int i = 0; i < hour.length; i++) { if (!validHour(hour[i])) { - throw new WatcherSettingsException("invalid time [" + this + "]. invalid time hour value [" + hour[i] + "]. time hours must be between 0 and 23 incl."); + throw new ScheduleTriggerException("invalid time [" + this + "]. invalid time hour value [" + hour[i] + "]. time hours must be between 0 and 23 incl."); } } for (int i = 0; i < minute.length; i++) { if (!validMinute(minute[i])) { - throw new WatcherSettingsException("invalid time [" + this + "]. invalid time minute value [" + minute[i] + "]. time minutes must be between 0 and 59 incl."); + throw new ScheduleTriggerException("invalid time [" + this + "]. invalid time minute value [" + minute[i] + "]. time minutes must be between 0 and 59 incl."); } } } diff --git a/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/MonthTimes.java b/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/MonthTimes.java index f24af450813..ae5ab1f7800 100644 --- a/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/MonthTimes.java +++ b/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/MonthTimes.java @@ -11,7 +11,7 @@ import org.elasticsearch.common.primitives.Ints; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.watcher.WatcherException; -import org.elasticsearch.watcher.WatcherSettingsException; +import org.elasticsearch.watcher.trigger.schedule.ScheduleTriggerException; import java.io.IOException; import java.util.Arrays; @@ -47,7 +47,7 @@ public class MonthTimes implements Times { void validate() { for (int day : days) { if (day < 1 || day > 32) { //32 represents the last day of the month - throw new WatcherSettingsException("invalid month day [" + day + "]"); + throw new ScheduleTriggerException("invalid month day [" + day + "]"); } } for (DayTimes dayTimes : times) { diff --git a/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/YearTimes.java b/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/YearTimes.java index f141a1eb127..21aad23c901 100644 --- a/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/YearTimes.java +++ b/src/main/java/org/elasticsearch/watcher/trigger/schedule/support/YearTimes.java @@ -11,7 +11,7 @@ import org.elasticsearch.common.primitives.Ints; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.watcher.WatcherException; -import org.elasticsearch.watcher.WatcherSettingsException; +import org.elasticsearch.watcher.trigger.schedule.ScheduleTriggerException; import java.io.IOException; import java.util.*; @@ -44,7 +44,7 @@ public class YearTimes implements Times { void validate() { for (int day : days) { if (day < 1 || day > 32) { //32 represents the last day of the month - throw new WatcherSettingsException("invalid month day [" + day + "]"); + throw new ScheduleTriggerException("invalid month day [" + day + "]"); } } for (DayTimes dayTimes : times) { diff --git a/src/test/java/org/elasticsearch/watcher/execution/ExecutionServiceTests.java b/src/test/java/org/elasticsearch/watcher/execution/ExecutionServiceTests.java index 28e8fbb3231..03dcb260e2f 100644 --- a/src/test/java/org/elasticsearch/watcher/execution/ExecutionServiceTests.java +++ b/src/test/java/org/elasticsearch/watcher/execution/ExecutionServiceTests.java @@ -21,6 +21,7 @@ import org.elasticsearch.watcher.support.clock.Clock; import org.elasticsearch.watcher.support.clock.ClockMock; import org.elasticsearch.watcher.actions.throttler.ActionThrottler; import org.elasticsearch.watcher.actions.throttler.Throttler; +import org.elasticsearch.watcher.support.validation.WatcherSettingsValidation; import org.elasticsearch.watcher.transform.ExecutableTransform; import org.elasticsearch.watcher.transform.Transform; import org.elasticsearch.watcher.trigger.schedule.ScheduleTriggerEvent; @@ -58,8 +59,9 @@ public class ExecutionServiceTests extends ElasticsearchTestCase { WatchExecutor executor = mock(WatchExecutor.class); WatchStore watchStore = mock(WatchStore.class); WatchLockService watchLockService = mock(WatchLockService.class); + WatcherSettingsValidation settingsValidator = mock(WatcherSettingsValidation.class); Clock clock = new ClockMock(); - executionService = new ExecutionService(ImmutableSettings.EMPTY, historyStore, executor, watchStore, watchLockService, clock); + executionService = new ExecutionService(ImmutableSettings.EMPTY, historyStore, executor, watchStore, watchLockService, clock, settingsValidator); } @Test diff --git a/src/test/java/org/elasticsearch/watcher/support/WatcherDateTimeUtilsTests.java b/src/test/java/org/elasticsearch/watcher/support/WatcherDateTimeUtilsTests.java index c2b6a4961f7..b0027151c9a 100644 --- a/src/test/java/org/elasticsearch/watcher/support/WatcherDateTimeUtilsTests.java +++ b/src/test/java/org/elasticsearch/watcher/support/WatcherDateTimeUtilsTests.java @@ -14,6 +14,7 @@ import org.junit.Test; import java.util.concurrent.TimeUnit; +import static java.util.concurrent.TimeUnit.*; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.watcher.test.WatcherTestUtils.xContentParser; import static org.hamcrest.CoreMatchers.nullValue; @@ -26,7 +27,7 @@ import static org.hamcrest.Matchers.notNullValue; */ public class WatcherDateTimeUtilsTests extends ElasticsearchTestCase { - @Test @Repeat(iterations = 20) + @Test @Repeat(iterations = 10) public void testParseTimeValue_Numeric() throws Exception { TimeValue value = new TimeValue(randomInt(100), randomFrom(TimeUnit.values())); @@ -40,13 +41,26 @@ public class WatcherDateTimeUtilsTests extends ElasticsearchTestCase { assertThat(parsed.millis(), is(value.millis())); } + @Test(expected = WatcherDateTimeUtils.ParseException.class) @Repeat(iterations = 10) + public void testParseTimeValue_Numeric_Negative() throws Exception { + TimeValue value = new TimeValue(randomIntBetween(1, 100), randomFrom(MILLISECONDS, SECONDS, MINUTES, HOURS, DAYS)); + + XContentParser parser = xContentParser(jsonBuilder().startObject().field("value", -1 * value.getMillis()).endObject()); + parser.nextToken(); // start object + parser.nextToken(); // field name + parser.nextToken(); // value + + WatcherDateTimeUtils.parseTimeValue(parser, null); + } + @Test @Repeat(iterations = 10) public void testParseTimeValue_String() throws Exception { + int value = randomIntBetween(2, 200); ImmutableMap values = ImmutableMap.builder() - .put("5s", TimeValue.timeValueSeconds(5)) - .put("5m", TimeValue.timeValueMinutes(5)) - .put("5h", TimeValue.timeValueHours(5)) - .put("5", TimeValue.timeValueMillis(5)) + .put(value + "s", TimeValue.timeValueSeconds(value)) + .put(value + "m", TimeValue.timeValueMinutes(value)) + .put(value + "h", TimeValue.timeValueHours(value)) + .put(value + "", TimeValue.timeValueMillis(value)) .build(); String key = randomFrom(values.keySet().toArray(new String[values.size()])); @@ -61,6 +75,26 @@ public class WatcherDateTimeUtilsTests extends ElasticsearchTestCase { assertThat(parsed.millis(), is(values.get(key).millis())); } + @Test(expected = WatcherDateTimeUtils.ParseException.class) @Repeat(iterations = 10) + public void testParseTimeValue_String_Negative() throws Exception { + int value = -1 * randomIntBetween(2, 200); + ImmutableMap values = ImmutableMap.builder() + .put(value + "s", TimeValue.timeValueSeconds(value)) + .put(value + "m", TimeValue.timeValueMinutes(value)) + .put(value + "h", TimeValue.timeValueHours(value)) + .put(value + "", TimeValue.timeValueMillis(value)) + .build(); + + String key = randomFrom(values.keySet().toArray(new String[values.size()])); + + XContentParser parser = xContentParser(jsonBuilder().startObject().field("value", key).endObject()); + parser.nextToken(); // start object + parser.nextToken(); // field name + parser.nextToken(); // value + + WatcherDateTimeUtils.parseTimeValue(parser, null); + } + @Test public void testParseTimeValue_Null() throws Exception { XContentParser parser = xContentParser(jsonBuilder().startObject().nullField("value").endObject()); diff --git a/src/test/java/org/elasticsearch/watcher/test/AbstractWatcherIntegrationTests.java b/src/test/java/org/elasticsearch/watcher/test/AbstractWatcherIntegrationTests.java index fba36d53d15..cf24ddc873f 100644 --- a/src/test/java/org/elasticsearch/watcher/test/AbstractWatcherIntegrationTests.java +++ b/src/test/java/org/elasticsearch/watcher/test/AbstractWatcherIntegrationTests.java @@ -28,8 +28,6 @@ import org.elasticsearch.plugins.Plugin; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.shield.ShieldPlugin; import org.elasticsearch.shield.authc.esusers.ESUsersRealm; -import org.elasticsearch.shield.authc.support.SecuredString; -import org.elasticsearch.shield.authc.support.UsernamePasswordToken; import org.elasticsearch.shield.crypto.InternalCryptoService; import org.elasticsearch.test.ElasticsearchIntegrationTest; import org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope; @@ -51,7 +49,6 @@ import org.elasticsearch.watcher.license.LicenseService; import org.elasticsearch.watcher.support.clock.ClockMock; import org.elasticsearch.watcher.support.http.HttpClient; import org.elasticsearch.watcher.support.init.proxy.ScriptServiceProxy; -import org.elasticsearch.watcher.transport.actions.stats.WatcherStatsResponse; import org.elasticsearch.watcher.trigger.ScheduleTriggerEngineMock; import org.elasticsearch.watcher.trigger.TriggerService; import org.elasticsearch.watcher.trigger.schedule.ScheduleModule; diff --git a/src/test/java/org/elasticsearch/watcher/test/integration/BasicWatcherTests.java b/src/test/java/org/elasticsearch/watcher/test/integration/BasicWatcherTests.java index 176eee53a2d..67348e4ee7e 100644 --- a/src/test/java/org/elasticsearch/watcher/test/integration/BasicWatcherTests.java +++ b/src/test/java/org/elasticsearch/watcher/test/integration/BasicWatcherTests.java @@ -17,7 +17,6 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.test.junit.annotations.TestLogging; import org.elasticsearch.watcher.WatcherException; -import org.elasticsearch.watcher.WatcherSettingsException; import org.elasticsearch.watcher.client.WatchSourceBuilder; import org.elasticsearch.watcher.client.WatcherClient; import org.elasticsearch.watcher.condition.ConditionBuilders; @@ -392,7 +391,7 @@ public class BasicWatcherTests extends AbstractWatcherIntegrationTests { .addAction("_logger", loggingAction("executed!"))) .get(); fail("put watch should have failed"); - } catch (WatcherSettingsException e) { + } catch (ScheduleTriggerException e) { assertThat(e.getMessage(), equalTo("invalid hourly minute [-10]. minute must be between 0 and 59 incl.")); } @@ -405,7 +404,7 @@ public class BasicWatcherTests extends AbstractWatcherIntegrationTests { .addAction("_logger", loggingAction("executed!"))) .get(); fail("put watch should have failed"); - } catch (WatcherSettingsException e) { + } catch (ScheduleTriggerException e) { assertThat(e.getMessage(), equalTo("invalid time [0-10:00]. invalid time hour value [-10]. time hours must be between 0 and 23 incl.")); } @@ -418,7 +417,7 @@ public class BasicWatcherTests extends AbstractWatcherIntegrationTests { .addAction("_logger", loggingAction("executed!"))) .get(); fail("put watch should have failed"); - } catch (WatcherSettingsException e) { + } catch (ScheduleTriggerException e) { assertThat(e.getMessage(), equalTo("invalid time [0-10:00]. invalid time hour value [-10]. time hours must be between 0 and 23 incl.")); } @@ -431,7 +430,7 @@ public class BasicWatcherTests extends AbstractWatcherIntegrationTests { .addAction("_logger", loggingAction("executed!"))) .get(); fail("put watch should have failed"); - } catch (WatcherSettingsException e) { + } catch (ScheduleTriggerException e) { assertThat(e.getMessage(), equalTo("invalid time [0-10:00]. invalid time hour value [-10]. time hours must be between 0 and 23 incl.")); } } diff --git a/src/test/java/org/elasticsearch/watcher/trigger/schedule/CronScheduleTests.java b/src/test/java/org/elasticsearch/watcher/trigger/schedule/CronScheduleTests.java index 5bdd33acfb8..862f764bfd5 100644 --- a/src/test/java/org/elasticsearch/watcher/trigger/schedule/CronScheduleTests.java +++ b/src/test/java/org/elasticsearch/watcher/trigger/schedule/CronScheduleTests.java @@ -5,7 +5,6 @@ */ package org.elasticsearch.watcher.trigger.schedule; -import org.elasticsearch.watcher.WatcherSettingsException; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; @@ -64,13 +63,13 @@ public class CronScheduleTests extends ScheduleTestCase { try { new CronSchedule.Parser().parse(parser); fail("expected cron parsing to fail when using invalid cron expression"); - } catch (WatcherSettingsException ase) { + } catch (ScheduleTriggerException ase) { // expected assertThat(ase.getCause(), instanceOf(CronSchedule.ValidationException.class)); } } - @Test(expected = WatcherSettingsException.class) + @Test(expected = ScheduleTriggerException.class) public void testParse_Invalid_Empty() throws Exception { XContentBuilder builder = jsonBuilder(); BytesReference bytes = builder.bytes(); @@ -79,7 +78,7 @@ public class CronScheduleTests extends ScheduleTestCase { new CronSchedule.Parser().parse(parser); } - @Test(expected = WatcherSettingsException.class) + @Test(expected = ScheduleTriggerException.class) public void testParse_Invalid_Object() throws Exception { XContentBuilder builder = jsonBuilder().startObject().endObject(); BytesReference bytes = builder.bytes(); @@ -88,7 +87,7 @@ public class CronScheduleTests extends ScheduleTestCase { new CronSchedule.Parser().parse(parser); } - @Test(expected = WatcherSettingsException.class) + @Test(expected = ScheduleTriggerException.class) public void testParse_Invalid_EmptyArray() throws Exception { XContentBuilder builder = jsonBuilder().value(new String[0]); BytesReference bytes = builder.bytes(); diff --git a/src/test/java/org/elasticsearch/watcher/trigger/schedule/DailyScheduleTests.java b/src/test/java/org/elasticsearch/watcher/trigger/schedule/DailyScheduleTests.java index ced6b924be0..4ab16f182c4 100644 --- a/src/test/java/org/elasticsearch/watcher/trigger/schedule/DailyScheduleTests.java +++ b/src/test/java/org/elasticsearch/watcher/trigger/schedule/DailyScheduleTests.java @@ -11,7 +11,6 @@ import org.elasticsearch.common.primitives.Ints; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.json.JsonXContent; -import org.elasticsearch.watcher.WatcherSettingsException; import org.elasticsearch.watcher.trigger.schedule.support.DayTimes; import org.junit.Test; @@ -48,7 +47,7 @@ public class DailyScheduleTests extends ScheduleTestCase { fail("expected either a parse exception or an watcher settings exception on invalid time input"); } catch (DayTimes.ParseException pe) { // expected - } catch (WatcherSettingsException ase) { + } catch (ScheduleTriggerException ase) { // expected } } @@ -95,7 +94,7 @@ public class DailyScheduleTests extends ScheduleTestCase { assertThat(schedule.times()[0], is(time)); } - @Test(expected = WatcherSettingsException.class) @Repeat(iterations = 20) + @Test(expected = ScheduleTriggerException.class) @Repeat(iterations = 20) public void testParser_SingleTime_Object_Invalid() throws Exception { HourAndMinute time = invalidDayTime(); XContentBuilder builder = jsonBuilder() @@ -127,7 +126,7 @@ public class DailyScheduleTests extends ScheduleTestCase { assertThat(schedule.times()[0], is(DayTimes.parse(timeStr))); } - @Test(expected = WatcherSettingsException.class) @Repeat(iterations = 20) + @Test(expected = ScheduleTriggerException.class) @Repeat(iterations = 20) public void testParser_SingleTime_String_Invalid() throws Exception { XContentBuilder builder = jsonBuilder() .startObject() @@ -157,7 +156,7 @@ public class DailyScheduleTests extends ScheduleTestCase { } } - @Test(expected = WatcherSettingsException.class) @Repeat(iterations = 20) + @Test(expected = ScheduleTriggerException.class) @Repeat(iterations = 20) public void testParser_MultipleTimes_Objects_Invalid() throws Exception { HourAndMinute[] times = invalidDayTimes(); XContentBuilder builder = jsonBuilder() @@ -188,7 +187,7 @@ public class DailyScheduleTests extends ScheduleTestCase { } } - @Test(expected = WatcherSettingsException.class) @Repeat(iterations = 20) + @Test(expected = ScheduleTriggerException.class) @Repeat(iterations = 20) public void testParser_MultipleTimes_Strings_Invalid() throws Exception { String[] times = invalidDayTimesAsStrings(); XContentBuilder builder = jsonBuilder() diff --git a/src/test/java/org/elasticsearch/watcher/trigger/schedule/HourlyScheduleTests.java b/src/test/java/org/elasticsearch/watcher/trigger/schedule/HourlyScheduleTests.java index 4a872496f09..9e5ba23023e 100644 --- a/src/test/java/org/elasticsearch/watcher/trigger/schedule/HourlyScheduleTests.java +++ b/src/test/java/org/elasticsearch/watcher/trigger/schedule/HourlyScheduleTests.java @@ -12,7 +12,6 @@ import org.elasticsearch.common.primitives.Ints; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.json.JsonXContent; -import org.elasticsearch.watcher.WatcherSettingsException; import org.junit.Test; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; @@ -40,7 +39,7 @@ public class HourlyScheduleTests extends ScheduleTestCase { assertThat(crons, arrayContaining("0 " + minute + " * * * ?")); } - @Test(expected = WatcherSettingsException.class) @Repeat(iterations = 20) + @Test(expected = ScheduleTriggerException.class) @Repeat(iterations = 20) public void test_SingleMinute_Invalid() throws Exception { new HourlySchedule(invalidMinute()); } @@ -55,7 +54,7 @@ public class HourlyScheduleTests extends ScheduleTestCase { assertThat(crons, arrayContaining("0 " + minutesStr + " * * * ?")); } - @Test(expected = WatcherSettingsException.class) @Repeat(iterations = 20) + @Test(expected = ScheduleTriggerException.class) @Repeat(iterations = 20) public void test_MultipleMinutes_Invalid() throws Exception { int[] minutes = invalidMinutes(); new HourlySchedule(minutes); @@ -89,7 +88,7 @@ public class HourlyScheduleTests extends ScheduleTestCase { assertThat(schedule.minutes()[0], is(minute)); } - @Test(expected = WatcherSettingsException.class) @Repeat(iterations = 20) + @Test(expected = ScheduleTriggerException.class) @Repeat(iterations = 20) public void testParser_SingleMinute_Number_Invalid() throws Exception { XContentBuilder builder = jsonBuilder() .startObject() @@ -117,7 +116,7 @@ public class HourlyScheduleTests extends ScheduleTestCase { assertThat(schedule.minutes()[0], is(minute)); } - @Test(expected = WatcherSettingsException.class) @Repeat(iterations = 20) + @Test(expected = ScheduleTriggerException.class) @Repeat(iterations = 20) public void testParser_SingleMinute_String_Invalid() throws Exception { XContentBuilder builder = jsonBuilder() .startObject() @@ -147,7 +146,7 @@ public class HourlyScheduleTests extends ScheduleTestCase { } } - @Test(expected = WatcherSettingsException.class) @Repeat(iterations = 20) + @Test(expected = ScheduleTriggerException.class) @Repeat(iterations = 20) public void testParser_MultipleMinutes_Numbers_Invalid() throws Exception { int[] minutes = invalidMinutes(); XContentBuilder builder = jsonBuilder() @@ -178,7 +177,7 @@ public class HourlyScheduleTests extends ScheduleTestCase { } } - @Test(expected = WatcherSettingsException.class) @Repeat(iterations = 20) + @Test(expected = ScheduleTriggerException.class) @Repeat(iterations = 20) public void testParser_MultipleMinutes_Strings_Invalid() throws Exception { int[] minutes = invalidMinutes(); XContentBuilder builder = jsonBuilder() diff --git a/src/test/java/org/elasticsearch/watcher/trigger/schedule/IntervalScheduleTests.java b/src/test/java/org/elasticsearch/watcher/trigger/schedule/IntervalScheduleTests.java index 44da36d4436..4f6db4acc38 100644 --- a/src/test/java/org/elasticsearch/watcher/trigger/schedule/IntervalScheduleTests.java +++ b/src/test/java/org/elasticsearch/watcher/trigger/schedule/IntervalScheduleTests.java @@ -5,7 +5,6 @@ */ package org.elasticsearch.watcher.trigger.schedule; -import org.elasticsearch.watcher.WatcherSettingsException; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; @@ -60,7 +59,7 @@ public class IntervalScheduleTests extends ElasticsearchTestCase { assertThat(schedule.interval(), is(value)); } - @Test(expected = WatcherSettingsException.class) + @Test(expected = ScheduleTriggerException.class) public void testParse_Invalid_String() throws Exception { XContentBuilder builder = jsonBuilder().value("43S"); BytesReference bytes = builder.bytes(); @@ -69,7 +68,7 @@ public class IntervalScheduleTests extends ElasticsearchTestCase { new IntervalSchedule.Parser().parse(parser); } - @Test(expected = WatcherSettingsException.class) + @Test(expected = ScheduleTriggerException.class) public void testParse_Invalid_Object() throws Exception { XContentBuilder builder = jsonBuilder().startObject().endObject(); BytesReference bytes = builder.bytes(); diff --git a/src/test/java/org/elasticsearch/watcher/trigger/schedule/MonthlyScheduleTests.java b/src/test/java/org/elasticsearch/watcher/trigger/schedule/MonthlyScheduleTests.java index 78acfef4616..1db75a524ab 100644 --- a/src/test/java/org/elasticsearch/watcher/trigger/schedule/MonthlyScheduleTests.java +++ b/src/test/java/org/elasticsearch/watcher/trigger/schedule/MonthlyScheduleTests.java @@ -11,7 +11,6 @@ import org.elasticsearch.common.primitives.Ints; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.json.JsonXContent; -import org.elasticsearch.watcher.WatcherSettingsException; import org.elasticsearch.watcher.trigger.schedule.support.DayTimes; import org.elasticsearch.watcher.trigger.schedule.support.MonthTimes; import org.junit.Test; @@ -104,7 +103,7 @@ public class MonthlyScheduleTests extends ScheduleTestCase { assertThat(schedule.times()[0].times(), hasItemInArray(time)); } - @Test(expected = WatcherSettingsException.class) @Repeat(iterations = 20) + @Test(expected = ScheduleTriggerException.class) @Repeat(iterations = 20) public void testParser_SingleTime_Invalid() throws Exception { HourAndMinute time = invalidDayTime(); XContentBuilder builder = jsonBuilder() @@ -136,7 +135,7 @@ public class MonthlyScheduleTests extends ScheduleTestCase { } } - @Test(expected = WatcherSettingsException.class) @Repeat(iterations = 20) + @Test(expected = ScheduleTriggerException.class) @Repeat(iterations = 20) public void testParser_MultipleTimes_Invalid() throws Exception { HourAndMinute[] times = invalidDayTimes(); XContentBuilder builder = jsonBuilder() diff --git a/src/test/java/org/elasticsearch/watcher/trigger/schedule/WeeklyScheduleTests.java b/src/test/java/org/elasticsearch/watcher/trigger/schedule/WeeklyScheduleTests.java index c9a24ea4dbe..a8888bed515 100644 --- a/src/test/java/org/elasticsearch/watcher/trigger/schedule/WeeklyScheduleTests.java +++ b/src/test/java/org/elasticsearch/watcher/trigger/schedule/WeeklyScheduleTests.java @@ -12,7 +12,6 @@ import org.elasticsearch.common.primitives.Ints; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.json.JsonXContent; -import org.elasticsearch.watcher.WatcherSettingsException; import org.elasticsearch.watcher.trigger.schedule.support.DayOfWeek; import org.elasticsearch.watcher.trigger.schedule.support.DayTimes; import org.elasticsearch.watcher.trigger.schedule.support.WeekTimes; @@ -97,7 +96,7 @@ public class WeeklyScheduleTests extends ScheduleTestCase { assertThat(schedule.times()[0].times(), hasItemInArray(time)); } - @Test(expected = WatcherSettingsException.class) @Repeat(iterations = 20) + @Test(expected = ScheduleTriggerException.class) @Repeat(iterations = 20) public void testParser_SingleTime_Invalid() throws Exception { HourAndMinute time = invalidDayTime(); XContentBuilder builder = jsonBuilder() @@ -129,7 +128,7 @@ public class WeeklyScheduleTests extends ScheduleTestCase { } } - @Test(expected = WatcherSettingsException.class) @Repeat(iterations = 20) + @Test(expected = ScheduleTriggerException.class) @Repeat(iterations = 20) public void testParser_MultipleTimes_Objects_Invalid() throws Exception { HourAndMinute[] times = invalidDayTimes(); XContentBuilder builder = jsonBuilder() diff --git a/src/test/java/org/elasticsearch/watcher/trigger/schedule/YearlyScheduleTests.java b/src/test/java/org/elasticsearch/watcher/trigger/schedule/YearlyScheduleTests.java index 924e911c639..77d9eb1a085 100644 --- a/src/test/java/org/elasticsearch/watcher/trigger/schedule/YearlyScheduleTests.java +++ b/src/test/java/org/elasticsearch/watcher/trigger/schedule/YearlyScheduleTests.java @@ -12,7 +12,6 @@ import org.elasticsearch.common.primitives.Ints; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.json.JsonXContent; -import org.elasticsearch.watcher.WatcherSettingsException; import org.elasticsearch.watcher.trigger.schedule.support.DayTimes; import org.elasticsearch.watcher.trigger.schedule.support.YearTimes; import org.junit.Test; @@ -111,7 +110,7 @@ public class YearlyScheduleTests extends ScheduleTestCase { assertThat(schedule.times()[0].times(), hasItemInArray(time)); } - @Test(expected = WatcherSettingsException.class) @Repeat(iterations = 20) + @Test(expected = ScheduleTriggerException.class) @Repeat(iterations = 20) public void testParser_SingleTime_Invalid() throws Exception { HourAndMinute time = invalidDayTime(); XContentBuilder builder = jsonBuilder() @@ -144,7 +143,7 @@ public class YearlyScheduleTests extends ScheduleTestCase { } } - @Test(expected = WatcherSettingsException.class) @Repeat(iterations = 20) + @Test(expected = ScheduleTriggerException.class) @Repeat(iterations = 20) public void testParser_MultipleTimes_Invalid() throws Exception { HourAndMinute[] times = invalidDayTimes(); XContentBuilder builder = jsonBuilder()