setup:
  - do:
      indices.create:
        index: airline-data
        body:
          mappings:
            properties:
              time:
                type: date
              airline:
                type: keyword
              responsetime:
                type: float
              event_rate:
                type: integer

  - do:
      transform.put_transform:
        transform_id: "updating-airline-transform"
        body: >
          {
            "source": { "index": "airline-data" },
            "dest": { "index": "airline-data-by-airline" },
            "pivot": {
              "group_by": { "airline": {"terms": {"field": "airline"}}},
              "aggs": {"avg_response": {"avg": {"field": "responsetime"}}}
            },
            "description": "yaml test transform on airline-data",
            "frequency": "60s",
            "sync": {
              "time": {
                "field": "time",
                "delay": "90m"
              }
            }
          }
---
"Test update transform with missing transform":
  - do:
      catch: /Transform with id \[missing-transform\] could not be found/
      transform.update_transform:
        transform_id: "missing-transform"
        body: >
          {
            "description": "new description"
          }
---
"Test update transform with missing pipeline":
  - do:
      catch: /Pipeline with id \[missing-transform-pipeline\] could not be found/
      transform.update_transform:
        transform_id: "updating-airline-transform"
        body: >
          {
            "dest": { "index": "airline-data-by-airline", "pipeline": "missing-transform-pipeline" }
          }
---
"Test update transform with frequency too low":
  - do:
      catch: /minimum permitted \[frequency\] is \[1s\]/
      transform.update_transform:
        transform_id: "updating-airline-transform"
        body: >
          {
            "frequency": "999ms"
          }
---
"Test update transform with frequency too high":
  - do:
      catch: /highest permitted \[frequency\] is \[1h\]/
      transform.update_transform:
        transform_id: "updating-airline-transform"
        body: >
          {
            "frequency": "3600001ms"
          }
---
"Test put transform with invalid source index":
  - do:
      catch: /.*Validation Failed.* no such index \[missing-index\]/
      transform.update_transform:
        transform_id: "updating-airline-transform"
        body: >
          {
            "source": { "index": "missing-index" }
          }
  - do:
      transform.update_transform:
        transform_id: "updating-airline-transform"
        defer_validation: true
        body: >
          {
            "source": { "index": "missing-index" }
          }
  - match: { id: "updating-airline-transform" }
  - match: { source.index.0: "missing-index" }
---
"Test update transform when it is batch":
  - do:
      transform.put_transform:
        transform_id: "batch-airline-transform"
        body: >
          {
            "source": { "index": "airline-data" },
            "dest": { "index": "airline-data-by-airline" },
            "pivot": {
              "group_by": { "airline": {"terms": {"field": "airline"}}},
              "aggs": {"avg_response": {"avg": {"field": "responsetime"}}}
            }
          }
  - do:
      transform.update_transform:
        transform_id: "batch-airline-transform"
        body: >
          {
            "description": "time to update"
          }
  - match: { description: "time to update" }

  - do:
      catch: /Cannot change the current sync configuration of transform \[batch-airline-transform\] from \[null\] to \[time\]/
      transform.update_transform:
        transform_id: "batch-airline-transform"
        body: >
          {
            "sync": {
              "time": {
                "field": "time",
                "delay": "90m"
              }
            }
          }
---
"Test basic transform crud":
  - do:
      indices.create:
        index: other-airline-data
        body:
          mappings:
            properties:
              time:
                type: date
              airline:
                type: keyword
              responsetime:
                type: float
              event_rate:
                type: integer
  - do:
      transform.get_transform:
        transform_id: "updating-airline-transform"
  - match: { count: 1 }
  - match: { transforms.0.id: "updating-airline-transform" }
  - match: { transforms.0.source.index.0: "airline-data" }
  - match: { transforms.0.dest.index: "airline-data-by-airline" }
  - is_true: transforms.0.source.query.match_all
  - is_true: transforms.0.create_time
  - is_true: transforms.0.version
  - match: { transforms.0.pivot.group_by.airline.terms.field: "airline" }
  - match: { transforms.0.pivot.aggregations.avg_response.avg.field: "responsetime" }
  - match: { transforms.0.description: "yaml test transform on airline-data" }
  - match: { transforms.0.sync.time.field: "time" }
  - match: { transforms.0.sync.time.delay: "90m" }
  - match: { transforms.0.frequency: "60s" }

  - do:
      transform.update_transform:
        transform_id: "updating-airline-transform"
        body: >
          {
            "source": { "index": "other-airline-data" },
            "dest": { "index": "new-destination" },
            "description": "updated description",
            "sync": {
              "time": { "field": "time", "delay": "120m"}
            },
            "frequency": "5s"
          }
  - match: { id: "updating-airline-transform" }
  - match: { source.index.0: "other-airline-data" }
  - match: { dest.index: "new-destination" }
  - is_true: source.query.match_all
  - is_true: create_time
  - is_true: version
  - match: { pivot.group_by.airline.terms.field: "airline" }
  - match: { pivot.aggregations.avg_response.avg.field: "responsetime" }
  - match: { description: "updated description" }
  - match: { sync.time.field: "time" }
  - match: { sync.time.delay: "120m" }
  - match: { frequency: "5s" }
  - is_true: authorization.roles

  - do:
      transform.get_transform:
        transform_id: "updating-airline-transform"
  - match: { count: 1 }
  - match: { transforms.0.id: "updating-airline-transform" }
  - match: { transforms.0.source.index.0: "other-airline-data" }
  - match: { transforms.0.dest.index: "new-destination" }
  - is_true: transforms.0.source.query.match_all
  - is_true: transforms.0.create_time
  - is_true: transforms.0.version
  - match: { transforms.0.pivot.group_by.airline.terms.field: "airline" }
  - match: { transforms.0.pivot.aggregations.avg_response.avg.field: "responsetime" }
  - match: { transforms.0.description: "updated description" }
  - match: { transforms.0.sync.time.field: "time" }
  - match: { transforms.0.sync.time.delay: "120m" }
  - match: { transforms.0.frequency: "5s" }
  - is_true: transforms.0.authorization.roles

---
"Test update retention policy":
  - do:
      transform.get_transform:
        transform_id: "updating-airline-transform"
  - match: { count: 1 }
  - match: { transforms.0.id: "updating-airline-transform" }
  - match: { transforms.0.retention_policy: null }

  - do:
      transform.update_transform:
        transform_id: "updating-airline-transform"
        body: >
          {
            "retention_policy": {
              "time": {
                "field": "time",
                "max_age": "24h"
              }
            }
          }

  - do:
      transform.get_transform:
        transform_id: "updating-airline-transform"
  - match: { count: 1 }
  - match: { transforms.0.id: "updating-airline-transform" }
  - match: { transforms.0.retention_policy.time.field: "time" }
  - match: { transforms.0.retention_policy.time.max_age: "24h" }
  - is_true: transforms.0.authorization.roles

  - do:
      transform.update_transform:
        transform_id: "updating-airline-transform"
        body: >
          {
            "retention_policy": null
          }

  - do:
      transform.get_transform:
        transform_id: "updating-airline-transform"
  - match: { count: 1 }
  - match: { transforms.0.id: "updating-airline-transform" }
  - match: { transforms.0.retention_policy: null }

---
"Test transform where dest is included in source":
  - do:
      catch: /Destination index \[airline-data-by-airline\] is included in source expression \[airline-data/
      transform.update_transform:
        transform_id: "updating-airline-transform"
        body: >
          {
            "source": {
              "index": ["airline-data*"]
            },
            "dest": { "index": "airline-data-by-airline" }
          }

  - do:
      transform.update_transform:
        transform_id: "updating-airline-transform"
        defer_validation: true
        body: >
          {
            "source": {
              "index": ["airline-data*"]
            },
            "dest": { "index": "airline-data-by-airline" }
          }
---
"Test alias scenarios":
  - do:
      indices.create:
        index: created-destination-index
  - do:
      indices.create:
        index: second-created-destination-index
  - do:
      indices.put_alias:
        index: airline-data
        name: source-index
  - do:
      indices.put_alias:
        index: created-destination-index
        name: dest-index
  - do:
      transform.update_transform:
        transform_id: "updating-airline-transform"
        body: >
          {
            "source": {
              "index": "source-index"
            },
            "dest": { "index": "dest-index" }
          }
  - match: { id: "updating-airline-transform" }

  - do:
      indices.put_alias:
        index: created-destination-index
        name: source2-index

  - do:
      catch: /.*Validation Failed.* Destination index \[created-destination-index\] is included in source expression \[created-destination-index\]/
      transform.update_transform:
        transform_id: "updating-airline-transform"
        body: >
          {
            "source": {
              "index": "source2-index"
            },
            "dest": { "index": "dest-index" }
          }

  - do:
      indices.delete_alias:
        index: created-destination-index
        name: source2-index

  - do:
      indices.put_alias:
        index: second-created-destination-index
        name: dest2-index
  - do:
      indices.put_alias:
        index: created-destination-index
        name: dest2-index
  - do:
      catch: /.*Validation Failed.* no write index is defined for alias [dest2-index].*/
      transform.update_transform:
        transform_id: "updating-airline-transform"
        body: >
          {
            "dest": { "index": "dest2-index" }
          }
---
"Test invalid destination index name":
  - do:
      catch: /.*Validation Failed.* Destination index \[DeStInAtIoN\] must be lowercase/
      transform.update_transform:
        transform_id: "updating-airline-transform"
        body: >
          {
            "dest": { "index": "DeStInAtIoN" }
          }
  - do:
      catch: /.*Validation Failed.* Invalid index name \[destination#dest\], must not contain \'#\'/
      transform.update_transform:
        transform_id: "updating-airline-transform"
        body: >
          {
            "dest": { "index": "destination#dest" }
          }

---
"Test update transform metadata":
  - do:
      transform.get_transform:
        transform_id: "updating-airline-transform"
  - match: { count: 1 }
  - match: { transforms.0.id: "updating-airline-transform" }
  - is_false: transforms.0._meta  # no metadata exists yet

  - do:
      transform.update_transform:
        transform_id: "updating-airline-transform"
        body: >
          {
            "_meta": {
              "foo": 123,
              "bar": 456,
              "baz": {
                "a1": 11,
                "a2": 22
              }
            }
          }
  - match: { id: "updating-airline-transform" }
  - match: { _meta.foo: 123 }
  - match: { _meta.bar: 456 }
  - match: { _meta.baz.a1: 11 }
  - match: { _meta.baz.a2: 22 }
  - is_false: _meta.baz.a3
  - is_true: authorization.roles

  - do:
      transform.get_transform:
        transform_id: "updating-airline-transform"
  - match: { count: 1 }
  - match: { transforms.0.id: "updating-airline-transform" }
  - match: { transforms.0._meta.foo: 123 }
  - match: { transforms.0._meta.bar: 456 }
  - match: { transforms.0._meta.baz.a1: 11 }
  - match: { transforms.0._meta.baz.a2: 22 }
  - is_false: transforms.0._meta.baz.a3
  - is_true: transforms.0.authorization.roles

  - do:
      transform.update_transform:
        transform_id: "updating-airline-transform"
        body: >
          {
            "_meta": {
              "bar": "some bar note",
              "baz": {
                "a2": 222,
                "a3": 333
              }
            }
          }
  - match: { id: "updating-airline-transform" }
  - is_false: _meta.foo  # "foo" disappeared as the metadata update is implemented as full replace
  - match: { _meta.bar: "some bar note" }  # "bar" value type has changed from int to string
  - is_false: _meta.baz.a1  # "baz.a1" disappeared as the metadata update is implemented as full replace
  - match: { _meta.baz.a2: 222 }
  - match: { _meta.baz.a3: 333 }
  - is_true: authorization.roles

  - do:
      transform.get_transform:
        transform_id: "updating-airline-transform"
  - match: { count: 1 }
  - match: { transforms.0.id: "updating-airline-transform" }
  - is_false: transforms.0._meta.foo
  - match: { transforms.0._meta.bar: "some bar note" }
  - is_false: transforms.0._meta.baz.a1
  - match: { transforms.0._meta.baz.a2: 222 }
  - match: { transforms.0._meta.baz.a3: 333 }
  - is_true: transforms.0.authorization.roles
