diff --git a/src/main/java/org/elasticsearch/watcher/watch/Watch.java b/src/main/java/org/elasticsearch/watcher/watch/Watch.java index 503e3937dbb..dce963fd5d5 100644 --- a/src/main/java/org/elasticsearch/watcher/watch/Watch.java +++ b/src/main/java/org/elasticsearch/watcher/watch/Watch.java @@ -5,6 +5,7 @@ */ package org.elasticsearch.watcher.watch; +import com.google.common.collect.ImmutableList; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.bytes.BytesReference; @@ -23,6 +24,7 @@ import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.watcher.WatcherException; import org.elasticsearch.watcher.actions.ActionRegistry; +import org.elasticsearch.watcher.actions.ActionWrapper; import org.elasticsearch.watcher.actions.ExecutableActions; import org.elasticsearch.watcher.condition.ConditionRegistry; import org.elasticsearch.watcher.condition.ExecutableCondition; @@ -220,6 +222,7 @@ public class Watch implements TriggerEngine.Job, ToXContent { private final ExecutableInput defaultInput; private final ExecutableCondition defaultCondition; + private final ExecutableActions defaultActions; private final TimeValue defaultThrottleTimePeriod; @Inject @@ -239,6 +242,7 @@ public class Watch implements TriggerEngine.Job, ToXContent { this.defaultInput = new ExecutableNoneInput(logger); this.defaultCondition = new ExecutableAlwaysCondition(logger); + this.defaultActions = new ExecutableActions(ImmutableList.of()); this.defaultThrottleTimePeriod = settings.getAsTime(DEFAULT_THROTTLE_PERIOD_SETTING, DEFAULT_THROTTLE_PERIOD); } @@ -288,7 +292,7 @@ public class Watch implements TriggerEngine.Job, ToXContent { Trigger trigger = null; ExecutableInput input = defaultInput; ExecutableCondition condition = defaultCondition; - ExecutableActions actions = null; + ExecutableActions actions = defaultActions; ExecutableTransform transform = null; Map metatdata = null; Status status = null; @@ -338,10 +342,6 @@ public class Watch implements TriggerEngine.Job, ToXContent { if (trigger == null) { throw new WatcherException("could not parse watch [{}]. missing required field [{}]", id, TRIGGER_FIELD.getPreferredName()); } - if (actions == null) { - throw new WatcherException("could not parse watch [{}]. missing required field [{}]", id, ACTIONS_FIELD.getPreferredName()); - } - return new Watch(id, clock, licenseService, trigger, input, condition, transform, actions, metatdata, throttlePeriod, status); } diff --git a/src/test/java/org/elasticsearch/watcher/test/integration/BootStrapTests.java b/src/test/java/org/elasticsearch/watcher/test/integration/BootStrapTests.java index 6baa163770c..ff331a0ee07 100644 --- a/src/test/java/org/elasticsearch/watcher/test/integration/BootStrapTests.java +++ b/src/test/java/org/elasticsearch/watcher/test/integration/BootStrapTests.java @@ -75,17 +75,6 @@ public class BootStrapTests extends AbstractWatcherIntegrationTests { .endObject()) .get(); - // no actions field: - client().prepareIndex(WatchStore.INDEX, WatchStore.DOC_TYPE, "_id1") - .setSource(jsonBuilder().startObject() - .startObject(Watch.Parser.TRIGGER_FIELD.getPreferredName()) - .startObject("schedule") - .field("interval", "1s") - .endObject() - .endObject() - .endObject()) - .get(); - // invalid interval client().prepareIndex(WatchStore.INDEX, WatchStore.DOC_TYPE, "_id2") .setSource(jsonBuilder().startObject() diff --git a/src/test/java/org/elasticsearch/watcher/watch/WatchTests.java b/src/test/java/org/elasticsearch/watcher/watch/WatchTests.java index 4d397d71f9e..a95c83601d0 100644 --- a/src/test/java/org/elasticsearch/watcher/watch/WatchTests.java +++ b/src/test/java/org/elasticsearch/watcher/watch/WatchTests.java @@ -7,7 +7,6 @@ package org.elasticsearch.watcher.watch; import com.carrotsearch.ant.tasks.junit4.dependencies.com.google.common.collect.ImmutableSet; import com.carrotsearch.randomizedtesting.annotations.Repeat; -import com.carrotsearch.randomizedtesting.annotations.Seed; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.ImmutableList; import org.elasticsearch.common.collect.ImmutableMap; @@ -50,6 +49,7 @@ import org.elasticsearch.watcher.input.ExecutableInput; import org.elasticsearch.watcher.input.InputBuilders; import org.elasticsearch.watcher.input.InputFactory; import org.elasticsearch.watcher.input.InputRegistry; +import org.elasticsearch.watcher.input.none.ExecutableNoneInput; import org.elasticsearch.watcher.input.search.ExecutableSearchInput; import org.elasticsearch.watcher.input.search.SearchInput; import org.elasticsearch.watcher.input.search.SearchInputFactory; @@ -99,8 +99,8 @@ import java.util.Map; import static org.elasticsearch.watcher.input.InputBuilders.searchInput; import static org.elasticsearch.watcher.test.WatcherTestUtils.matchAllRequest; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; +import static org.elasticsearch.watcher.trigger.TriggerBuilders.schedule; +import static org.hamcrest.Matchers.*; import static org.mockito.Mockito.mock; public class WatchTests extends ElasticsearchTestCase { @@ -209,6 +209,37 @@ public class WatchTests extends ElasticsearchTestCase { } } + @Test + public void testParser_Defaults() throws Exception { + Schedule schedule = randomSchedule(); + ScheduleRegistry scheduleRegistry = registry(schedule); + TriggerEngine triggerEngine = new ParseOnlyScheduleTriggerEngine(ImmutableSettings.EMPTY, scheduleRegistry, SystemClock.INSTANCE); + TriggerService triggerService = new TriggerService(ImmutableSettings.EMPTY, ImmutableSet.of(triggerEngine)); + SecretService secretService = new SecretService.PlainText(); + + ConditionRegistry conditionRegistry = registry(new ExecutableAlwaysCondition(logger)); + InputRegistry inputRegistry = registry(new ExecutableNoneInput(logger)); + TransformRegistry transformRegistry = transformRegistry(); + ExecutableActions actions = new ExecutableActions(ImmutableList.of()); + ActionRegistry actionRegistry = registry(actions, transformRegistry); + + XContentBuilder builder = XContentFactory.jsonBuilder(); + builder.startObject(); + builder.startObject(Watch.Parser.TRIGGER_FIELD.getPreferredName()) + .field(ScheduleTrigger.TYPE, schedule(schedule).build()) + .endObject(); + builder.endObject(); + Watch.Parser watchParser = new Watch.Parser(settings, mock(LicenseService.class), conditionRegistry, triggerService, transformRegistry, actionRegistry, inputRegistry, SystemClock.INSTANCE, secretService); + Watch watch = watchParser.parse("failure", false, builder.bytes()); + assertThat(watch, notNullValue()); + assertThat(watch.trigger(), instanceOf(ScheduleTrigger.class)); + assertThat(watch.input(), instanceOf(ExecutableNoneInput.class)); + assertThat(watch.condition(), instanceOf(ExecutableAlwaysCondition.class)); + assertThat(watch.transform(), nullValue()); + assertThat(watch.actions(), notNullValue()); + assertThat(watch.actions().count(), is(0)); + } + private static Schedule randomSchedule() { String type = randomFrom(CronSchedule.TYPE, HourlySchedule.TYPE, DailySchedule.TYPE, WeeklySchedule.TYPE, MonthlySchedule.TYPE, YearlySchedule.TYPE, IntervalSchedule.TYPE); switch (type) {