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: force-delete-job
        body:  >
          {
            "analysis_config" : {
                "bucket_span": "1h",
                "detectors" :[{"function":"count"}]
            },
            "data_description" : {
            }
          }

  - do:
      headers:
        Authorization: "Basic eF9wYWNrX3Jlc3RfdXNlcjp4LXBhY2stdGVzdC1wYXNzd29yZA==" # run as x_pack_rest_user, i.e. the test setup superuser
      indices.create:
        index: airline-data
        body:
          mappings:
            properties:
              time:
                type: date
              airline:
                type: keyword
              airport:
                type: text
              responsetime:
                type: float

---
"Test force delete a closed job":
  - do:
      ml.delete_job:
        force: true
        delete_user_annotations: true
        job_id: force-delete-job
  - match: { acknowledged: true }

  - do:
      ml.get_jobs:
        job_id: "_all"
  - match: { count: 0 }

---
"Test force delete an open job":

  - do:
      ml.open_job:
        job_id: force-delete-job

  - do:
      ml.delete_job:
        force: true
        job_id: force-delete-job
  - match: { acknowledged: true }

  - do:
      ml.get_jobs:
        job_id: "_all"
  - match: { count: 0 }

---
"Test cannot force delete a non-existent job":

  - do:
      catch: /resource_not_found_exception/
      ml.delete_job:
        force: true
        job_id: inexistent-job

---
"Test force delete job that is referred by a datafeed":

  - do:
      ml.put_datafeed:
        datafeed_id: force-delete-job-datafeed
        body:  >
          {
            "job_id":"force-delete-job",
            "indices":["index-foo"]
          }
  - match: { datafeed_id: force-delete-job-datafeed }

  - do:
      ml.delete_job:
        job_id: force-delete-job
  - match: { acknowledged: true }

  - do:
      ml.get_jobs:
        job_id: "_all"
  - match: { count: 0 }

  - do:
      ml.get_datafeeds:
        datafeed_id: "_all"
  - match: { count: 0 }

---
"Test force delete an open job that is referred by a started datafeed":

  - do:
      ml.open_job:
        job_id: force-delete-job

  - do:
      ml.put_datafeed:
        datafeed_id: force-delete-job-started-datafeed
        body:  >
          {
            "job_id":"force-delete-job",
            "indices":["airline-data"]
          }
  - match: { datafeed_id: force-delete-job-started-datafeed }

  - do:
      ml.start_datafeed:
        datafeed_id: force-delete-job-started-datafeed
        start: 0

  - do:
      ml.delete_job:
        force: true
        job_id: force-delete-job
  - match: { acknowledged: true }

  - do:
      ml.get_jobs:
        job_id: "_all"
  - match: { count: 0 }

  - do:
      ml.get_datafeeds:
        datafeed_id: "_all"
  - match: { count: 0 }
