setup:
  - requires:
      test_runner_features: capabilities
      reason: Reindex GET is added as part of reindex management feature
      capabilities:
        - method: GET
          path: /_reindex/{task_id}
          capabilities: [reindex_management_api]

  - do:
      index:
        index:   source
        refresh: true
        body:    { "test": "test" }
---
"Get reindex by id":
  - do:
      reindex:
        # need to be async to get the task id
        wait_for_completion: false
        body:
          source:
            index: source
          dest:
            index: dest
  - is_true: task
  - set: { task: taskId }

  - do:
      reindex_get:
        task_id: $taskId
        # wait for task to complete to ensure we get consistent result
        wait_for_completion: true
        human: true
  - is_true: completed
  - match: { id: $taskId }
  - match: { cancelled: false }
  - exists: start_time
  - exists: start_time_in_millis
  - exists: running_time
  - exists: running_time_in_nanos
  - exists: status
  - is_false: error
  - match: { response.total: 1 }
  - gt: { response.took: 0 }
  - match: { response.batches: 1 }
  - match: { response.failures: [ ] }
  - match: { response.timed_out: false }
---
"Invalid task id":
    - do:
        reindex_get:
            task_id: invalid_task_id
        catch: bad_request
    - is_true: error
    - match: { error.type: 'illegal_argument_exception' }
    - match: { error.reason: 'malformed task id invalid_task_id' }
---
"Task does not exist":
  - do:
      reindex_get:
        task_id: "node0:123"
      catch: missing
  - is_true: error
  - match: { error.type: 'resource_not_found_exception' }
  - match: { error.reason: 'Reindex operation [node0:123] not found' }
---
"Task exists but it is not a reindex":
  # create a task that is not a reindex
  - do:
      delete_by_query:
        wait_for_completion: false
        index: source
        body:
          query:
            match_all: {}
  - set: {task: task}

  # verify the task exists
  - do:
      tasks.get:
        task_id: $task
  - is_true: task

  # verify the reindex does not exist
  - do:
      reindex_get:
          task_id: $task
      catch: missing
  - is_true: error
  - match: { error.type: 'resource_not_found_exception' }

