---
"Test execute watch api":

  - do:
      cluster.health:
          wait_for_status: green

  - do:
      watcher.put_watch:
        id: "my_exe_watch"
        body:  >
          {
            "trigger" : {
              "schedule" : { "cron" : "0 0 0 1 * ? 2099" }
            },
            "input" : {
              "chain" : {
                "inputs" : [
                  {
                    "first" : {
                      "search" : {
                        "request" : {
                          "indices" : [ "logstash*" ],
                          "rest_total_hits_as_int": true,
                          "body" : {
                            "query" : {
                              "bool": {
                                "must" : {
                                  "match": {
                                    "response": 404
                                  }
                                },
                                "filter": {
                                  "range": {
                                    "@timestamp" : {
                                      "from": "{{ctx.trigger.scheduled_time}}||-5m",
                                      "to": "{{ctx.trigger.triggered_time}}"
                                    }
                                  }
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  },
                  {
                    "second" : {
                      "transform" : {
                        "script" : {
                          "source": "return [ 'hits' : [ 'total' : ctx.payload.first.hits.total ]]"
                        }
                      }
                    }
                  }
                ]
              }
            },
            "condition" : {
              "script" : {
                "source" : "ctx.payload.hits.total > 1",
                "lang" : "painless"
              }
            },
            "actions" : {
              "email_admin" : {
                "transform" : {
                  "script" : {
                    "source" : "return ['foo': 'bar']",
                    "lang" : "painless"
                  }
                },
                "email" : {
                  "to" : "someone@domain.host.com",
                  "subject" : "404 recently encountered"
                }
              }
            }
          }
  - match: { _id: "my_exe_watch" }

  - do:
      watcher.get_watch:
        id: "my_exe_watch"

  - match: { _id: "my_exe_watch" }
  - match: { watch.actions.email_admin.transform.script.source: "return ['foo': 'bar']" }
  - match: { watch.input.chain.inputs.1.second.transform.script.source: "return [ 'hits' : [ 'total' : ctx.payload.first.hits.total ]]" }

  - do:
      watcher.execute_watch:
        id: "my_exe_watch"
        body: >
          {
            "trigger_data" : {
              "scheduled_time" : "2015-05-05T20:58:02.443Z",
              "triggered_time" : "2015-05-05T20:58:02.443Z"
            },
            "alternative_input" : {
              "foo" : "bar"
            },
            "ignore_condition" : true,
            "action_modes" : {
              "_all" : "force_simulate"
            },
            "record_execution" : true
          }
  - match: { "watch_record.watch_id": "my_exe_watch" }
  - match: { "watch_record.state": "executed" }
  - match: { "watch_record.trigger_event.type": "manual" }
  - match: { "watch_record.trigger_event.triggered_time": "2015-05-05T20:58:02.443Z" }
  - match: { "watch_record.trigger_event.manual.schedule.scheduled_time": "2015-05-05T20:58:02.443Z" }
  - match: { "watch_record.result.input.type": "simple" }
  - match: { "watch_record.result.input.status": "success" }
  - match: { "watch_record.result.input.payload.foo": "bar" }
  - match: { "watch_record.result.condition.type": "always" }
  - match: { "watch_record.result.condition.status": "success" }
  - match: { "watch_record.result.condition.met": true }
  - match: { "watch_record.result.actions.0.id" : "email_admin" }
  - match: { "watch_record.result.actions.0.status" : "simulated" }
  - match: { "watch_record.result.actions.0.type" : "email" }
  - match: { "watch_record.result.actions.0.email.message.subject" : "404 recently encountered" }

---
"Test execute watch api with rest_total_hits_as_int":

  - do:
      cluster.health:
        wait_for_status: green

  - do:
      watcher.put_watch:
        id: "my_exe_watch"
        body:  >
          {
            "trigger" : {
              "schedule" : { "cron" : "0 0 0 1 * ? 2099" }
            },
            "input" : {
              "chain" : {
                "inputs" : [
                  {
                    "first" : {
                      "search" : {
                        "request" : {
                          "indices" : [ "logstash*" ],
                          "rest_total_hits_as_int": true,
                          "body" : {
                            "query" : {
                              "bool": {
                                "must" : {
                                  "match": {
                                    "response": 404
                                  }
                                },
                                "filter": {
                                  "range": {
                                    "@timestamp" : {
                                      "from": "{{ctx.trigger.scheduled_time}}||-5m",
                                      "to": "{{ctx.trigger.triggered_time}}"
                                    }
                                  }
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  },
                  {
                    "second" : {
                      "transform" : {
                        "script" : {
                          "source": "return [ 'hits' : [ 'total' : ctx.payload.first.hits.total ]]"
                        }
                      }
                    }
                  }
                ]
              }
            },
            "condition" : {
              "script" : {
                "source" : "ctx.payload.hits.total > 1",
                "lang" : "painless"
              }
            },
            "actions" : {
              "email_admin" : {
                "transform" : {
                  "script" : {
                    "source" : "return ['foo': 'bar']",
                    "lang" : "painless"
                  }
                },
                "email" : {
                  "to" : "someone@domain.host.com",
                  "subject" : "404 recently encountered"
                }
              }
            }
          }
  - match: { _id: "my_exe_watch" }

  - do:
      watcher.get_watch:
        id: "my_exe_watch"

  - match: { _id: "my_exe_watch" }
  - match: { watch.actions.email_admin.transform.script.source: "return ['foo': 'bar']" }
  - match: { watch.input.chain.inputs.1.second.transform.script.source: "return [ 'hits' : [ 'total' : ctx.payload.first.hits.total ]]" }

  - do:
      watcher.execute_watch:
        id: "my_exe_watch"
        body: >
          {
            "trigger_data" : {
              "scheduled_time" : "2015-05-05T20:58:02.443Z",
              "triggered_time" : "2015-05-05T20:58:02.443Z"
            },
            "alternative_input" : {
              "foo" : "bar"
            },
            "ignore_condition" : true,
            "action_modes" : {
              "_all" : "force_simulate"
            },
            "record_execution" : true
          }
  - match: { "watch_record.watch_id": "my_exe_watch" }
  - match: { "watch_record.state": "executed" }
  - match: { "watch_record.trigger_event.type": "manual" }
  - match: { "watch_record.trigger_event.triggered_time": "2015-05-05T20:58:02.443Z" }
  - match: { "watch_record.trigger_event.manual.schedule.scheduled_time": "2015-05-05T20:58:02.443Z" }
  - match: { "watch_record.result.input.type": "simple" }
  - match: { "watch_record.result.input.status": "success" }
  - match: { "watch_record.result.input.payload.foo": "bar" }
  - match: { "watch_record.result.condition.type": "always" }
  - match: { "watch_record.result.condition.status": "success" }
  - match: { "watch_record.result.condition.met": true }
  - match: { "watch_record.result.actions.0.id" : "email_admin" }
  - match: { "watch_record.result.actions.0.status" : "simulated" }
  - match: { "watch_record.result.actions.0.type" : "email" }
  - match: { "watch_record.result.actions.0.email.message.subject" : "404 recently encountered" }

---
"Test executing logging action using scripts with params":
  - do:
      cluster.health:
        wait_for_status: yellow

  - do:
      put_script:
        id: log-action
        body: >
          {
            "script":
            {
              "lang": "painless",
              "source": "params.color + \" alert\""
            }
          }

  - do:
      watcher.put_watch:
        id: "test_watch"
        body:  >
          {
            "trigger": {
              "schedule" : { "interval": "1m" }
            },
            "input": {
              "simple": {
              }
            },
            "condition": {
              "always": {}
            },
            "actions": {
              "log" : {
                "logging" : {
                  "text" : {
                    "id": "log-action",
                    "params": {
                      "color": "yellow"
                    }
                  }
                }
              }
            }
          }
  - match: { _id: "test_watch" }
  - match: { created: true }

  - do:
      watcher.execute_watch:
        id: "test_watch"

  - match: { watch_record.watch_id: "test_watch" }
  - match: { watch_record.result.actions.0.id: "log" }
  - match: { watch_record.result.actions.0.type: "logging" }
  - match: { watch_record.result.actions.0.status: "success" }
  - match: { watch_record.result.actions.0.logging.logged_text: "yellow alert" }
