---
setup:
- skip:
    features:
    - "headers"
    - "allowed_warnings_regex"
- do:
    headers:
      Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA=="
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    ml.put_job:
      job_id: "post-data-job"
      body: "{\n  \"job_id\":\"post-data-job\",\n  \"description\":\"Analysis of response\
        \ time by airline\",\n  \"analysis_config\" : {\n      \"bucket_span\": \"\
        1h\",\n      \"detectors\" :[{\"function\":\"metric\",\"field_name\":\"responsetime\"\
        ,\"by_field_name\":\"airline\"}]\n  },\n  \"data_description\" : {\n     \
        \ \"format\":\"xcontent\",\n      \"time_field\":\"time\",\n      \"time_format\"\
        :\"epoch\"\n  }\n}\n"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    headers:
      Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA=="
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    ml.put_job:
      job_id: "post-data-closed-job"
      body: "{\n  \"job_id\":\"post-data-closed-job\",\n  \"description\":\"A closed\
        \ job\",\n  \"analysis_config\" : {\n      \"detectors\" :[{\"function\":\"\
        metric\",\"field_name\":\"responsetime\",\"by_field_name\":\"airline\"}]\n\
        \  },\n  \"data_description\" : {\n      \"format\":\"xcontent\"\n  }\n}\n"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    headers:
      Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA=="
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    ml.open_job:
      job_id: "post-data-job"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
---
Test POST data job api, flush, close and verify DataCounts doc:
- skip:
    features:
    - "warnings"
    awaits_fix: "Flush API is deprecated"
- do:
    warnings:
    - "Posting data directly to anomaly detection jobs is deprecated, in a future\
      \ major version it will be compulsory to use a datafeed"
    ml.post_data:
      job_id: "post-data-job"
      body:
      - airline: "AAL"
        responsetime: 132.2046
        sourcetype: "post-data-job"
        time: 1403481600
      - airline: "JZA"
        responsetime: 990.4628
        sourcetype: "post-data-job"
        time: 1403481700
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    processed_record_count: 2
- match:
    processed_field_count: 4
- gte:
    input_bytes: 140
- lte:
    input_bytes: 180
- match:
    input_field_count: 6
- match:
    invalid_date_count: 0
- match:
    missing_field_count: 0
- match:
    out_of_order_timestamp_count: 0
- match:
    earliest_record_timestamp: 1403481600000
- match:
    latest_record_timestamp: 1403481700000
- do:
    ml.flush_job:
      job_id: "post-data-job"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    flushed: true
- match:
    last_finalized_bucket_end: 1403481600000
- do:
    ml.close_job:
      job_id: "post-data-job"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    closed: true
- do:
    ml.get_job_stats:
      job_id: "post-data-job"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    jobs.0.state: "closed"
- do:
    get:
      index: ".ml-anomalies-post-data-job"
      id: "post-data-job_data_counts"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    _source.processed_record_count: 2
- match:
    _source.processed_field_count: 4
- gte:
    _source.input_bytes: 140
- lte:
    _source.input_bytes: 180
- match:
    _source.input_field_count: 6
- match:
    _source.invalid_date_count: 0
- match:
    _source.missing_field_count: 0
- match:
    _source.out_of_order_timestamp_count: 0
- match:
    _source.earliest_record_timestamp: 1403481600000
- match:
    _source.latest_record_timestamp: 1403481700000
---
Test flush and close job WITHOUT sending any data:
- skip:
    awaits_fix: "Flush API is deprecated"
- do:
    ml.flush_job:
      job_id: "post-data-job"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    flushed: true
- is_false: "last_finalized_bucket_end"
- do:
    ml.close_job:
      job_id: "post-data-job"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    closed: true
- do:
    ml.get_job_stats:
      job_id: "post-data-job"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    jobs.0.state: "closed"
---
Test flush with skip_time:
- skip:
    features:
    - "warnings"
    awaits_fix: "Flush API is deprecated"
- do:
    warnings:
    - "Posting data directly to anomaly detection jobs is deprecated, in a future\
      \ major version it will be compulsory to use a datafeed"
    ml.post_data:
      job_id: "post-data-job"
      body:
      - airline: "AAL"
        responsetime: 132.2046
        sourcetype: "post-data-job"
        time: 1403481600
      - airline: "AAL"
        responsetime: 990.4628
        sourcetype: "post-data-job"
        time: 1403485200
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    ml.flush_job:
      job_id: "post-data-job"
      skip_time: 1403488700
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    flushed: true
- match:
    last_finalized_bucket_end: 1403488800000
- do:
    warnings:
    - "Posting data directly to anomaly detection jobs is deprecated, in a future\
      \ major version it will be compulsory to use a datafeed"
    ml.post_data:
      job_id: "post-data-job"
      body:
      - airline: "AAL"
        responsetime: 132.2046
        sourcetype: "post-data-job"
        time: 1403488600
      - airline: "AAL"
        responsetime: 990.4628
        sourcetype: "post-data-job"
        time: 1403488700
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    warnings:
    - "Posting data directly to anomaly detection jobs is deprecated, in a future\
      \ major version it will be compulsory to use a datafeed"
    ml.post_data:
      job_id: "post-data-job"
      body:
      - airline: "AAL"
        responsetime: 132.2046
        sourcetype: "post-data-job"
        time: 1403488900
      - airline: "AAL"
        responsetime: 132.2046
        sourcetype: "post-data-job"
        time: 1403492400
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    ml.close_job:
      job_id: "post-data-job"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    closed: true
- do:
    ml.get_buckets:
      job_id: "post-data-job"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    count: 2
- match:
    buckets.0.timestamp: 1403481600000
- match:
    buckets.0.event_count: 1
- match:
    buckets.1.timestamp: 1403488800000
- match:
    buckets.1.event_count: 1
---
Test POST data with invalid parameters:
- skip:
    reason: "https://github.com/elastic/elasticsearch/issues/34747"
    cluster_features:
    - "gte_v6.5.0"
- requires:
    test_runner_features:
    - "warnings"
- do:
    warnings:
    - "Posting data directly to anomaly detection jobs is deprecated, in a future\
      \ major version it will be compulsory to use a datafeed"
    catch: "missing"
    ml.post_data:
      job_id: "not_a_job"
      body:
      - airline: "AAL"
        responsetime: 132.2046
        sourcetype: "farequote"
        time: 1403481600
      - airline: "JZA"
        responsetime: 990.4628
        sourcetype: "farequote"
        time: 1403481700
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    warnings:
    - "Posting data directly to anomaly detection jobs is deprecated, in a future\
      \ major version it will be compulsory to use a datafeed"
    catch: "/parse_exception/"
    ml.post_data:
      job_id: "post-data-job"
      reset_start: "not_a_date"
      body:
      - airline: "AAL"
        responsetime: 132.2046
        sourcetype: "farequote"
        time: 1403481600
      - airline: "JZA"
        responsetime: 990.4628
        sourcetype: "farequote"
        time: 1403481700
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    warnings:
    - "Posting data directly to anomaly detection jobs is deprecated, in a future\
      \ major version it will be compulsory to use a datafeed"
    catch: "/parse_exception/"
    ml.post_data:
      job_id: "post-data-job"
      reset_end: "end_not_a_date"
      body:
      - airline: "AAL"
        responsetime: 132.2046
        sourcetype: "farequote"
        time: 1403481600
      - airline: "JZA"
        responsetime: 990.4628
        sourcetype: "farequote"
        time: 1403481700
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
---
Test Flush data with invalid parameters:
- skip:
    reason: "https://github.com/elastic/elasticsearch/issues/34747"
    cluster_features:
    - "gte_v6.5.0"
- do:
    catch: "missing"
    ml.flush_job:
      job_id: "not_a_job"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    catch: "/parse_exception/"
    ml.flush_job:
      job_id: "post-data-job"
      start: "not_a_date"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    catch: "/parse_exception/"
    ml.flush_job:
      job_id: "post-data-job"
      end: "end_not_a_date"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    catch: "/parse_exception/"
    ml.flush_job:
      job_id: "post-data-job"
      advance_time: "advance_time_not_a_date"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
---
Test open and close with non-existent job id:
- do:
    catch: "missing"
    ml.open_job:
      job_id: "not_a_job"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    catch: "missing"
    ml.close_job:
      job_id: "not_a_job"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
---
Test flushing and posting a closed job:
- skip:
    features:
    - "warnings"
- do:
    catch: "/status_exception/"
    ml.flush_job:
      job_id: "post-data-closed-job"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    warnings:
    - "Posting data directly to anomaly detection jobs is deprecated, in a future\
      \ major version it will be compulsory to use a datafeed"
    catch: "/status_exception/"
    ml.post_data:
      job_id: "post-data-closed-job"
      body:
      - airline: "AAL"
        responsetime: 132.2046
        sourcetype: "farequote"
        time: 1403481600
      - airline: "JZA"
        responsetime: 990.4628
        sourcetype: "farequote"
        time: 1403481700
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
