---
teardown:
  - do:
      ingest.delete_pipeline:
        id: "1"
        ignore: 404
  - do:
      ingest.delete_pipeline:
        id: "2"
        ignore: 404
  - do:
      ingest.delete_pipeline:
        id: "3"
        ignore: 404
  - do:
      ingest.delete_pipeline:
        id: "4"
        ignore: 404

---
"Test JSON Processor":
  - do:
      ingest.put_pipeline:
        id: "1"
        body:  >
          {
            "processors": [
              {
                "json" : {
                  "field" : "foo_object"
                }
              },
              {
                "json" : {
                  "field" : "foo_array"
                }
              },
              {
                "json" : {
                  "field" : "foo_null"
                }
              },
              {
                "json" : {
                  "field" : "foo_string"
                }
              },
              {
                "json" : {
                  "field" : "foo_number"
                }
              },
              {
                "json" : {
                  "field" : "foo_boolean"
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      index:
        index: test
        id: "1"
        pipeline: "1"
        body: {
          foo_object: "{\"hello\": \"world\"}",
          foo_array: "[1, 2, 3]",
          foo_null: null,
          foo_string: "\"bla bla\"",
          foo_number: 3,
          foo_boolean: "true"
        }

  - do:
      get:
        index: test
        id: "1"
  - match: { _source.foo_object.hello: "world" }
  - match: { _source.foo_array.0: 1 }
  - match: { _source.foo_string: "bla bla" }
  - match: { _source.foo_number: 3 }
  - is_true:  _source.foo_boolean
  - is_false: _source.foo_null

---
"Test JSON Processor duplicate keys":
  - do:
      ingest.put_pipeline:
        id: "2"
        body: {
            "processors": [
              {
                "json" : {
                  "field" : "json",
                  "add_to_root": true,
                  "allow_duplicate_keys": true
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      index:
        index: test
        id: "2"
        pipeline: "2"
        body: {
          json: "{\"dupe\": 1, \"dupe\": 2}",
        }

  - do:
      get:
        index: test
        id: "2"
  - match: { _source.dupe: 2 }

---
"Test JSON Processor recursive merge strategy":
  - do:
      ingest.put_pipeline:
        id: "3"
        body: {
            "processors": [
              {
                "json" : {
                  "field" : "json",
                  "add_to_root": true,
                  "add_to_root_conflict_strategy": "merge"
                }
              }
            ]
          }
  - match: { acknowledged: true }

  - do:
      index:
        index: test
        id: "3"
        pipeline: "3"
        body: {
          json: "{\"foo\": {\"bar\": \"baz\"} }",
          foo: {
            bar: "override_me",
            qux: "quux"
          }
        }

  - do:
      get:
        index: test
        id: "3"
  - match: { _source.foo.bar: "baz" }
  - match: { _source.foo.qux: "quux" }

---
"Test JSON Processor lenient parsing":
  - do:
      ingest.put_pipeline:
        id: "4"
        body: {
          "processors": [
            {
              "json" : {
                "field" : "message",
                "strict_json_parsing": false
              }
            }
          ]
        }
  - match: { acknowledged: true }

  - do:
      index:
        index: test
        id: "4"
        pipeline: "4"
        body: {
          message: "123 foo"
        }

  - do:
      get:
        index: test
        id: "4"
  - match: { _source.message: 123 }
