---
teardown:
  - do:
      ingest.delete_pipeline:
        id: "my_pipeline"
        ignore: 404
  - do:
      ingest.delete_pipeline:
        id: "first_pipeline"
        ignore: 404
  - do:
      ingest.delete_pipeline:
        id: "second_pipeline"
        ignore: 404

---
"Test basic pipeline crud":
  - do:
      ingest.put_pipeline:
        id: "my_pipeline"
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "set" : {
                  "field" : "field2",
                  "value": "_value"
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      ingest.get_pipeline:
        id: "my_pipeline"
  - match: { my_pipeline.description: "_description" }

  - do:
      ingest.get_pipeline: {}
  - match: { my_pipeline.description: "_description" }

  - do:
      ingest.delete_pipeline:
        id: "my_pipeline"
  - match: { acknowledged: true }

  - do:
      catch: missing
      ingest.get_pipeline:
        id: "my_pipeline"
---
"Test wildcard pipeline delete":
  - do:
      ingest.put_pipeline:
        id: "my_pipeline"
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "set" : {
                  "field" : "field2",
                  "value": "_value"
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      ingest.get_pipeline:
        id: "my_pipeline"
  - match: { my_pipeline.description: "_description" }

  - do:
      ingest.delete_pipeline:
        id: "my_*"
  - match: { acknowledged: true }

  - do:
      catch: missing
      ingest.get_pipeline:
        id: "my_pipeline"

  - do:
      catch: missing
      ingest.delete_pipeline:
        id: "my_*"
  - match: { "error.type": "resource_not_found_exception" }
  - match: { "error.reason": "pipeline [my_*] is missing" }

---
"Test Get All Pipelines":
  - do:
      ingest.put_pipeline:
        id: "first_pipeline"
        body:  >
          {
            "description": "first",
            "processors": [
              {
                "set" : {
                  "field" : "field1",
                  "value": "_value"
                }
              }
            ]
          }
  - do:
      ingest.put_pipeline:
        id: "second_pipeline"
        body:  >
          {
            "description": "second",
            "processors": []
          }

  - do:
      ingest.get_pipeline: {}
  - match: { first_pipeline.description: "first" }
  - match: { second_pipeline.description: "second" }

---
"Test invalid config":
  - do:
      catch: /parse_exception/
      ingest.put_pipeline:
        id: "my_pipeline"
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "set" : {
                }
              }
            ]
          }

---
"Test invalid processor config":
  - do:
      catch: bad_request
      ingest.put_pipeline:
        id: "my_pipeline"
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "set" : {
                  "tag" : "fritag"
                }
              }
            ]
          }
  - match: { error.root_cause.0.type: "parse_exception" }
  - match: { error.root_cause.0.reason: "[field] required property is missing" }
  - match: { error.root_cause.0.processor_tag: "fritag" }
  - match: { error.root_cause.0.processor_type: "set" }
  - match: { error.root_cause.0.property_name: "field" }

---
"Test basic pipeline with on_failure in processor":
  - do:
      ingest.put_pipeline:
        id: "my_pipeline"
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "set" : {
                  "field" : "field2",
                  "value": "_value",
                  "on_failure": [
                    {
                      "set" : {
                        "field" : "field2",
                        "value" : "_failed_value"
                      }
                    }
                  ]
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      ingest.get_pipeline:
        id: "my_pipeline"
  - match: { my_pipeline.description: "_description" }

  - do:
      ingest.delete_pipeline:
        id: "my_pipeline"
  - match: { acknowledged: true }

  - do:
      catch: missing
      ingest.get_pipeline:
        id: "my_pipeline"

---
"Test processor with description":
  - do:
      ingest.put_pipeline:
        id: "my_pipeline"
        body:  >
          {
            "description": "_description",
            "processors": [
              {
                "set" : {
                  "description": "this processor sets the [field2] field to [_value]",
                  "field" : "field2",
                  "value": "_value"
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      ingest.get_pipeline:
        id: "my_pipeline"
  - match: { my_pipeline.processors.0.set.description: "this processor sets the [field2] field to [_value]" }
