---
setup:
  - do:
      cluster.health:
          wait_for_status: yellow
  - do: {watcher.stats:{}}
  - do:
      index:
        index: idx
        id: "1"
        body: >
          {
            "date" : "2015-01-01T00:00:00",
            "value" : "val_1"
          }
  - do:
      index:
        index: idx
        id: "2"
        body: >
          {
            "date" : "2015-01-02T00:00:00",
            "value" : "val_2"
          }
  - do:
      index:
        index: idx
        id: "3"
        body: >
          {
            "date" : "2015-01-03T00:00:00",
            "value" : "val_3"
          }
  - do:
      index:
        index: idx
        id: "4"
        body: >
          {
            "date" : "2015-01-04T00:00:00",
            "value" : "val_4"
          }
  - do:
      indices.refresh:
        index: idx

---
"Test search transform mustache integration (using request body)":
  - do:
      watcher.execute_watch:
        body: >
          {
            "trigger_data" : {
              "scheduled_time" : "2015-01-04T00:00:00"
            },
            "watch" : {
              "trigger" : { "schedule" : { "interval" : "10s" } },
              "input" : { "simple" : { "value" : "val_3" } },
              "actions" : {
                "dummy" : {
                  "logging" : {
                    "text" : "executed!"
                  }
                }
              },
              "transform" : {
                "search" : {
                  "request" : {
                    "indices" : "idx",
                    "rest_total_hits_as_int": true,
                    "body" : {
                      "query" : {
                        "bool" : {
                          "filter" : [
                            {
                              "range" : {
                                "date" : {
                                  "lte" : "{{ctx.trigger.scheduled_time}}",
                                  "gte" : "{{ctx.trigger.scheduled_time}}||-1d"
                                }
                              }
                            },
                            {
                              "term" : {
                                "value" : "{{ctx.payload.value}}"
                              }
                            }
                          ]
                        }
                      }
                    }
                  }
                }
              }
            }
          }
  - match: { "watch_record.result.transform.type": "search" }
  - match: { "watch_record.result.transform.status": "success" }
  - match: { "watch_record.result.transform.payload.hits.total": 1 }
  - match: { "watch_record.result.transform.payload.hits.hits.0._id": "3" }
  # makes sure that the mustache template snippets have been resolved  correctly:
  - match: { "watch_record.result.transform.search.request.body.query.bool.filter.0.range.date.gte": "2015-01-04T00:00Z||-1d" }
  - match: { "watch_record.result.transform.search.request.body.query.bool.filter.0.range.date.lte": "2015-01-04T00:00Z" }
  - match: { "watch_record.result.transform.search.request.body.query.bool.filter.1.term.value": "val_3" }

---
"Test search transform mustache integration (using request template)":
  - do:
      watcher.execute_watch:
        body: >
          {
            "trigger_data" : {
              "scheduled_time" : "2015-01-04T00:00:00"
            },
            "watch" : {
              "trigger" : { "schedule" : { "interval" : "10s" } },
              "input" : { "simple" : { "number" : 2 } },
              "actions" : {
                "dummy" : {
                  "logging" : {
                    "text" : "executed!"
                  }
                }
              },
              "transform" : {
                "search" : {
                  "request" : {
                    "rest_total_hits_as_int": true,
                    "indices" : "idx",
                    "template" : {
                      "source" : "{\"query\": {\"bool\" : { \"must\": [{\"term\": {\"value\": \"val_{{ctx.payload.number}}\"}}]}}}"
                    }
                  }
                }
              }
            }
          }
  - match: { "watch_record.result.transform.type": "search" }
  - match: { "watch_record.result.transform.status": "success" }
  - match: { "watch_record.result.transform.payload.hits.total": 1 }
  - match: { "watch_record.result.transform.payload.hits.hits.0._id": "2" }
  # makes sure that the mustache template snippets have been resolved  correctly:
  - match: { "watch_record.result.transform.search.request.body.query.bool.must.0.term.value": "val_2" }
  - match: { "watch_record.result.transform.search.request.template.lang": "mustache" }
