From d15933ac320ab7eb5d2bede2a2acdcf34c28ba02 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Tue, 13 Dec 2016 12:13:24 +0100 Subject: [PATCH] test: move java rest test to yaml rest test. These test are just checking response / status code. It is cleaner to have these tests as yaml tests. Original commit: elastic/x-pack-elasticsearch@61c323059e4c46d8b05b988d77888beb7787b2c1 --- .../prelert/integration/ScheduledJobIT.java | 57 ------------ .../test/start_stop_scheduler.yaml | 90 +++++++++++++++++++ 2 files changed, 90 insertions(+), 57 deletions(-) create mode 100644 elasticsearch/src/test/resources/rest-api-spec/test/start_stop_scheduler.yaml diff --git a/elasticsearch/src/test/java/org/elasticsearch/xpack/prelert/integration/ScheduledJobIT.java b/elasticsearch/src/test/java/org/elasticsearch/xpack/prelert/integration/ScheduledJobIT.java index f0585e7a8ac..019355b3ad7 100644 --- a/elasticsearch/src/test/java/org/elasticsearch/xpack/prelert/integration/ScheduledJobIT.java +++ b/elasticsearch/src/test/java/org/elasticsearch/xpack/prelert/integration/ScheduledJobIT.java @@ -31,52 +31,6 @@ import static org.hamcrest.Matchers.equalTo; public class ScheduledJobIT extends ESRestTestCase { - public void testStartJobScheduler_GivenMissingJob() { - ResponseException e = expectThrows(ResponseException.class, - () -> client().performRequest("post", PrelertPlugin.BASE_PATH + "schedulers/invalid-job/_start")); - assertThat(e.getResponse().getStatusLine().getStatusCode(), equalTo(404)); - } - - public void testStartJobScheduler_GivenNonScheduledJob() throws Exception { - String jobId = "_id1"; - createNonScheduledJob(jobId); - - ResponseException e = expectThrows(ResponseException.class, - () -> client().performRequest("post", PrelertPlugin.BASE_PATH + "schedulers/" + jobId + "/_start")); - assertThat(e.getResponse().getStatusLine().getStatusCode(), equalTo(400)); - String responseAsString = responseEntityToString(e.getResponse()); - assertThat(responseAsString, containsString("\"reason\":\"job [" + jobId + "] is not a scheduled job\"")); - } - - public void testStartJobScheduler_jobNotOpened() throws Exception { - String jobId = "_id1"; - createScheduledJob(jobId); - - ResponseException e = expectThrows(ResponseException.class, - () -> client().performRequest("post", PrelertPlugin.BASE_PATH + "schedulers/" + jobId + "/_start")); - assertThat(e.getResponse().getStatusLine().getStatusCode(), equalTo(409)); - String responseAsString = responseEntityToString(e.getResponse()); - assertThat(responseAsString, containsString("\"reason\":\"cannot start scheduler, expected job status [OPENED], " + - "but got [CLOSED]\"")); - } - - public void testStartJobScheduler_schedulerAlreadyStarted() throws Exception { - String jobId = "_id1"; - createScheduledJob(jobId); - openJob(client(), jobId); - Response startSchedulerRequest = client().performRequest("post", - PrelertPlugin.BASE_PATH + "schedulers/" + jobId + "/_start?start=2016-06-01T00:00:00Z"); - assertThat(startSchedulerRequest.getStatusLine().getStatusCode(), equalTo(200)); - assertThat(responseEntityToString(startSchedulerRequest), containsString("{\"task\":\"")); - - ResponseException e = expectThrows(ResponseException.class, - () -> client().performRequest("post", PrelertPlugin.BASE_PATH + "schedulers/" + jobId + "/_start")); - assertThat(e.getResponse().getStatusLine().getStatusCode(), equalTo(409)); - String responseAsString = responseEntityToString(e.getResponse()); - assertThat(responseAsString, containsString("\"reason\":\"scheduler already started, expected scheduler status " + - "[STOPPED], but got [STARTED]\"")); - } - public void testStartJobScheduler_GivenLookbackOnly() throws Exception { String jobId = "_id2"; createAirlineDataIndex(); @@ -152,17 +106,6 @@ public class ScheduledJobIT extends ESRestTestCase { client().performRequest("post", "airline-data/_refresh"); } - private Response createNonScheduledJob(String id) throws Exception { - String job = "{\n" + " \"job_id\":\"" + id + "\",\n" + " \"description\":\"Analysis of response time by airline\",\n" - + " \"analysis_config\" : {\n" + " \"bucket_span\":3600,\n" - + " \"detectors\" :[{\"function\":\"mean\",\"field_name\":\"responsetime\",\"by_field_name\":\"airline\"}]\n" - + " },\n" + " \"data_description\" : {\n" + " \"field_delimiter\":\",\",\n" + " " + - "\"time_field\":\"time\",\n" - + " \"time_format\":\"yyyy-MM-dd'T'HH:mm:ssX\"\n" + " }\n" + "}"; - - return client().performRequest("put", PrelertPlugin.BASE_PATH + "anomaly_detectors", Collections.emptyMap(), new StringEntity(job)); - } - private Response createScheduledJob(String id) throws Exception { HttpHost httpHost = getClusterHosts().get(0); logger.info("Http host = " + httpHost.toURI()); diff --git a/elasticsearch/src/test/resources/rest-api-spec/test/start_stop_scheduler.yaml b/elasticsearch/src/test/resources/rest-api-spec/test/start_stop_scheduler.yaml new file mode 100644 index 00000000000..7f152789d45 --- /dev/null +++ b/elasticsearch/src/test/resources/rest-api-spec/test/start_stop_scheduler.yaml @@ -0,0 +1,90 @@ +setup: + - do: + xpack.prelert.put_job: + body: > + { + "job_id":"non-scheduled-job", + "description":"Analysis of response time by airline", + "analysis_config" : { + "bucket_span":3600, + "detectors" :[{"function":"metric","field_name":"responsetime","by_field_name":"airline"}] + }, + "data_description" : { + "format":"JSON", + "time_field":"time", + "time_format":"epoch" + } + } + - do: + xpack.prelert.put_job: + body: > + { + "job_id":"scheduled-job", + "description":"Analysis of response time by airline", + "analysis_config" : { + "bucket_span":3600, + "detectors" :[{"function":"metric","field_name":"responsetime","by_field_name":"airline"}] + }, + "data_description" : { + "format":"ELASTICSEARCH", + "time_field":"time", + "time_format":"epoch" + }, + "scheduler_config" : { + "indexes" : "airline-data", + "types" : "response" + } + } + +--- +"Test start and stop scheduler happy path": + - do: + xpack.prelert.open_job: + job_id: "scheduled-job" + - do: + xpack.prelert.start_job_scheduler: + "job_id": "scheduled-job" + "start": 0 + - do: + xpack.prelert.stop_job_scheduler: + "job_id": "scheduled-job" + + +--- +"Test start non existing job": + - do: + catch: missing + xpack.prelert.start_job_scheduler: + "job_id": "non-existing-job" + "start": 0 + +--- +"Test start non scheduled job": + - do: + catch: request + xpack.prelert.start_job_scheduler: + "job_id": "non-scheduled-job" + "start": 0 + +--- +"Test start scheduled job, but not open": + - do: + catch: conflict + xpack.prelert.start_job_scheduler: + "job_id": "scheduled-job" + "start": 0 + +--- +"Test start already started scheduled job": + - do: + xpack.prelert.open_job: + job_id: "scheduled-job" + - do: + xpack.prelert.start_job_scheduler: + "job_id": "scheduled-job" + "start": 0 + - do: + catch: conflict + xpack.prelert.start_job_scheduler: + "job_id": "scheduled-job" + "start": 0