From 6c6dcafd0e8ab7ada5fb21d74839e5102697b4fe Mon Sep 17 00:00:00 2001 From: Dimitris Athanasiou Date: Mon, 15 Jan 2018 18:13:50 +0000 Subject: [PATCH] [ML] Improve error message when post-calendar-events-request is empty (elastic/x-pack-elasticsearch#3572) Relates elastic/x-pack-elasticsearch#3016 Original commit: elastic/x-pack-elasticsearch@3d25ed47232a7a1a0310c4af09adf3fc1efa5b24 --- .../xpack/ml/action/PostCalendarEventsAction.java | 4 ++++ .../rest-api-spec/test/ml/calendar_crud.yml | 12 ++++++++++++ qa/smoke-test-ml-with-security/build.gradle | 1 + 3 files changed, 17 insertions(+) diff --git a/plugin/core/src/main/java/org/elasticsearch/xpack/ml/action/PostCalendarEventsAction.java b/plugin/core/src/main/java/org/elasticsearch/xpack/ml/action/PostCalendarEventsAction.java index 0f897d2ed7c..2c97516b3f5 100644 --- a/plugin/core/src/main/java/org/elasticsearch/xpack/ml/action/PostCalendarEventsAction.java +++ b/plugin/core/src/main/java/org/elasticsearch/xpack/ml/action/PostCalendarEventsAction.java @@ -82,6 +82,10 @@ public class PostCalendarEventsAction extends Action scheduledEvents) { this.calendarId = ExceptionsHelper.requireNonNull(calendarId, Calendar.ID.getPreferredName()); this.scheduledEvents = ExceptionsHelper.requireNonNull(scheduledEvents, EVENTS.getPreferredName()); + + if (scheduledEvents.isEmpty()) { + throw ExceptionsHelper.badRequestException("at least 1 event is required"); + } } public String getCalendarId() { diff --git a/plugin/src/test/resources/rest-api-spec/test/ml/calendar_crud.yml b/plugin/src/test/resources/rest-api-spec/test/ml/calendar_crud.yml index 5084b3574ee..9879103316d 100644 --- a/plugin/src/test/resources/rest-api-spec/test/ml/calendar_crud.yml +++ b/plugin/src/test/resources/rest-api-spec/test/ml/calendar_crud.yml @@ -584,3 +584,15 @@ - length: { events: 2 } - match: { events.0.description: ski } - match: { events.1.description: snow } + +--- +"Test post calendar events given empty events": + + - do: + catch: /at least 1 event is required/ + xpack.ml.post_calendar_events: + calendar_id: "foo" + body: > + { + "events": [] + } diff --git a/qa/smoke-test-ml-with-security/build.gradle b/qa/smoke-test-ml-with-security/build.gradle index 240a220706f..46708519a2f 100644 --- a/qa/smoke-test-ml-with-security/build.gradle +++ b/qa/smoke-test-ml-with-security/build.gradle @@ -22,6 +22,7 @@ integTestRunner { // know whether to expect an authorization exception or a validation exception 'ml/calendar_crud/Test cannot create calendar with name _all', 'ml/calendar_crud/Test PageParams with ID is invalid', + 'ml/calendar_crud/Test post calendar events given empty events', 'ml/custom_all_field/Test querying custom all field', 'ml/datafeeds_crud/Test delete datafeed with missing id', 'ml/datafeeds_crud/Test put datafeed referring to missing job_id',