setup:
  - skip:
      features: headers
  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      ml.put_job:
        job_id: post-data-job
        body:  >
          {
            "job_id":"post-data-job",
            "description":"Analysis of response time by airline",
            "analysis_config" : {
                "bucket_span": "1h",
                "detectors" :[{"function":"metric","field_name":"responsetime","by_field_name":"airline"}]
            },
            "data_description" : {
                "format":"xcontent",
                "time_field":"time",
                "time_format":"epoch"
            }
          }

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      ml.put_job:
        job_id: post-data-closed-job
        body:  >
          {
            "job_id":"post-data-closed-job",
            "description":"A closed job",
            "analysis_config" : {
                "detectors" :[{"function":"metric","field_name":"responsetime","by_field_name":"airline"}]
            },
            "data_description" : {
                "format":"xcontent"
            }
          }

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      ml.open_job:
        job_id: post-data-job

---
"Test POST data job api, flush, close and verify DataCounts doc":
  - skip:
      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'
      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

  - 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:
      warnings:
        - 'Forcing any buffered data to be processed is deprecated, in a future major version it will be compulsory to use a datafeed'
      ml.flush_job:
        job_id: post-data-job
  - match: { flushed: true }
  - match: { last_finalized_bucket_end: 1403481600000 }

  - do:
      ml.close_job:
        job_id: post-data-job
  - match: { closed: true }

  - do:
      ml.get_job_stats:
        job_id: post-data-job
  - match: { jobs.0.state: "closed" }

  - do:
      get:
        index: .ml-anomalies-post-data-job
        id: post-data-job_data_counts

  - 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:
      features:
        - "warnings"
  - do:
      warnings:
        - 'Forcing any buffered data to be processed is deprecated, in a future major version it will be compulsory to use a datafeed'
      ml.flush_job:
        job_id: post-data-job
  - match: { flushed: true }
  - is_false: last_finalized_bucket_end

  - do:
      ml.close_job:
        job_id: post-data-job
  - match: { closed: true }

  - do:
      ml.get_job_stats:
        job_id: post-data-job
  - match: { jobs.0.state: "closed" }

---
"Test flush with skip_time":

  - skip:
      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'
      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

  # Skip a bucket
  - do:
      warnings:
        - 'Forcing any buffered data to be processed is deprecated, in a future major version it will be compulsory to use a datafeed'
      ml.flush_job:
        job_id: post-data-job
        skip_time: 1403488700
  - match: { flushed: true }
  - match: { last_finalized_bucket_end: 1403488800000 }

  # Send some data that should be ignored
  - 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

  # Send data that will create results for the bucket after the skipped one
  - 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

  - do:
      ml.close_job:
        job_id: post-data-job
  - match: { closed: true }

  - do:
      ml.get_buckets:
        job_id: "post-data-job"
  - 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

  - 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

  - 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

---
"Test Flush data with invalid parameters":
  - skip:
      reason: "https://github.com/elastic/elasticsearch/issues/34747"
      cluster_features: ["gte_v6.5.0"]
      features:
        - "warnings"

  - do:
      catch: missing
      warnings:
        - 'Forcing any buffered data to be processed is deprecated, in a future major version it will be compulsory to use a datafeed'
      ml.flush_job:
        job_id: not_a_job

  - do:
      catch: /parse_exception/
      warnings:
        - 'Forcing any buffered data to be processed is deprecated, in a future major version it will be compulsory to use a datafeed'
      ml.flush_job:
        job_id: post-data-job
        start: not_a_date

  - do:
      catch: /parse_exception/
      warnings:
        - 'Forcing any buffered data to be processed is deprecated, in a future major version it will be compulsory to use a datafeed'
      ml.flush_job:
        job_id: post-data-job
        end: end_not_a_date

  - do:
      catch: /parse_exception/
      warnings:
        - 'Forcing any buffered data to be processed is deprecated, in a future major version it will be compulsory to use a datafeed'
      ml.flush_job:
        job_id: post-data-job
        advance_time: advance_time_not_a_date

---
"Test open and close with non-existent job id":
  - do:
      catch: missing
      ml.open_job:
        job_id: not_a_job

  - do:
      catch: missing
      ml.close_job:
        job_id: not_a_job

---
"Test flushing and posting a closed job":

  - skip:
      features:
        - "warnings"

  - do:
      catch: /status_exception/
      warnings:
        - 'Forcing any buffered data to be processed is deprecated, in a future major version it will be compulsory to use a datafeed'
      ml.flush_job:
        job_id: post-data-closed-job

  - 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
