#
# Tests to explore and cover the /v2/meters section of the
# Ceilometer API.
#

fixtures:
    - ConfigFixture

tests:

# Generic HTTP health explorations of all meters.

    - name: empty meters list
      url: /v2/meters
      response_headers:
          content-type: /application/json/
      response_strings:
          - "[]"

    - name: meters list bad accept
      url: /v2/meters
      request_headers:
          accept: text/plain
      status: 406

    - name: meters list bad method
      url: /v2/meters
      method: POST
      status: 405
      response_headers:
          allow: GET

    - name: try to delete meters
      url: /v2/meters
      method: DELETE
      status: 405
      response_headers:
          allow: GET

# Generic HTTP health explorations of single meter.

    - name: get non exist meter
      url: /v2/meters/noexist
      response_strings:
          - "[]"

    - name: meter bad accept
      url: /v2/meters/noexist?direct=True
      request_headers:
          accept: text/plain
      status: 406

    - name: meter delete noexist
      url: /v2/meters/noexist
      method: DELETE
      status: "404 || 405"

    - name: post meter no data
      url: /v2/meters/apples?direct=True
      method: POST
      request_headers:
          content-type: application/json
      data: ""
      status: 400

    - name: post meter error is JSON
      url: /v2/meters/apples?direct=True
      method: POST
      request_headers:
          content-type: application/json
      data: ""
      status: 400
      response_headers:
          content-type: /application/json/
      response_json_paths:
          $.error_message.faultstring: "Samples should be included in request body"

    - name: post meter bad content-type
      url: /v2/meters/apples?direct=True
      method: POST
      request_headers:
          content-type: text/plain
      data: hello
      status: 415

    - name: post bad samples to meter
      url: /v2/meters/apples?direct=True
      method: POST
      request_headers:
          content-type: application/json
      data:
          samples:
              - red
              - blue
              - yellow
      status: 400

# POST variations on a malformed sample

    - name: post limited counter to meter
      url: /v2/meters/apples?direct=True
      method: POST
      request_headers:
          content-type: application/json
      data:
          - counter_unit: instance
            counter_volume: 1
            resource_id: bd9431c1-8d69-4ad3-803a-8d4a6b89fd36
      status: 400
      response_strings:
          - "Invalid input for field/attribute counter_name"

    - name: post mismatched counter name to meter
      url: /v2/meters/apples?direct=True
      method: POST
      request_headers:
          content-type: application/json
      data:
          - counter_name: cars
            counter_type: gauge
            counter_unit: instance
            counter_volume: 1
            resource_id: bd9431c1-8d69-4ad3-803a-8d4a6b89fd36
      status: 400
      response_strings:
          - "Invalid input for field/attribute counter_name"
          - "should be apples"

    - name: post counter no resource to meter
      url: /v2/meters/apples?direct=True
      method: POST
      request_headers:
          content-type: application/json
      data:
          - counter_name: apples
            counter_type: gauge
            counter_unit: instance
            counter_volume: 1
      status: 400
      response_strings:
          - "Invalid input for field/attribute resource_id"
          - "Mandatory field missing."

    - name: post counter bad type to meter
      url: /v2/meters/apples?direct=True
      method: POST
      request_headers:
          content-type: application/json
      data:
          - counter_name: apples
            counter_type: elevation
            counter_unit: instance
            counter_volume: 1
            resource_id: bd9431c1-8d69-4ad3-803a-8d4a6b89fd36
      status: 400
      response_strings:
          - "Invalid input for field/attribute counter_type."
          - "The counter type must be: gauge, delta, cumulative"

# Manipulate samples

    - name: post counter to meter
      url: /v2/meters/apples?direct=True
      method: POST
      request_headers:
          content-type: application/json
      data:
          - counter_name: apples
            counter_type: gauge
            counter_unit: instance
            counter_volume: 1
            resource_id: bd9431c1-8d69-4ad3-803a-8d4a6b89fd36
      status: 201

    - name: list apple samples
      url: /v2/meters/apples
      response_json_paths:
          $[0].counter_volume: 1.0
          $[0].counter_name: apples
          $[0].resource_id: bd9431c1-8d69-4ad3-803a-8d4a6b89fd36

    - name: list meters
      url: /v2/meters
      response_json_paths:
          $[0].name: apples
          $[0].resource_id: bd9431c1-8d69-4ad3-803a-8d4a6b89fd36
          $[0].type: gauge
          $[-1].name: apples

    - name: negative limit on meter list
      url: /v2/meters/apples?limit=-5
      status: 400
      response_strings:
          - Limit must be positive

    - name: nan limit on meter list
      url: /v2/meters/apples?limit=NaN
      status: 400
      response_strings:
          - unable to convert to int

    - name: post counter to meter different resource
      url: /v2/meters/apples?direct=True
      method: POST
      status: 201
      request_headers:
          content-type: application/json
      data:
          - counter_name: apples
            counter_type: gauge
            counter_unit: instance
            counter_volume: 2
            resource_id: aa9431c1-8d69-4ad3-803a-8d4a6b89fdaa

    - name: query for resource
      url: /v2/meters/apples?q.field=resource_id&q.value=aa9431c1-8d69-4ad3-803a-8d4a6b89fdaa&q.op=eq
      response_json_paths:
          $[0].resource_id: aa9431c1-8d69-4ad3-803a-8d4a6b89fdaa
          $[-1].resource_id: aa9431c1-8d69-4ad3-803a-8d4a6b89fdaa

# Explore posting samples with less than perfect data.

    - name: post counter with bad timestamp
      url: /v2/meters/apples?direct=True
      method: POST
      request_headers:
          content-type: application/json
      data:
          - counter_name: apples
            counter_type: gauge
            counter_unit: instance
            counter_volume: 3
            resource_id: aa9431c1-8d69-4ad3-803a-8d4a6b89fdaa
            timestamp: "2013-01-bad 23:23:20"
      status: 400
      response_strings:
          - 'Invalid input for field/attribute samples'

    - name: post counter with good timestamp
      url: /v2/meters/apples?direct=True
      method: POST
      status: 201
      request_headers:
          content-type: application/json
      data:
          - counter_name: apples
            counter_type: gauge
            counter_unit: instance
            counter_volume: 3
            resource_id: aa9431c1-8d69-4ad3-803a-8d4a6b89fdaa
            timestamp: "2013-01-01 23:23:20"

    - name: post counter with wrong metadata
      url: /v2/meters/apples?direct=True
      method: POST
      request_headers:
          content-type: application/json
      data:
          - counter_name: apples
            counter_type: gauge
            counter_unit: instance
            counter_volume: 3
            resource_id: aa9431c1-8d69-4ad3-803a-8d4a6b89fdaa
            timestamp: "2013-01-01 23:23:20"
            resource_metadata: "a string"
      status: 400
      response_strings:
          - "Invalid input for field/attribute samples"

    - name: post counter with empty metadata
      url: /v2/meters/apples?direct=True
      method: POST
      status: 201
      request_headers:
          content-type: application/json
      data:
          - counter_name: apples
            counter_type: gauge
            counter_unit: instance
            counter_volume: 3
            resource_id: aa9431c1-8d69-4ad3-803a-8d4a6b89fdaa
            timestamp: "2013-01-01 23:23:20"
            resource_metadata: {}

# Statistics

    - name: get sample statistics
      url: /v2/meters/apples/statistics
      response_json_paths:
          $[0].groupby: null
          $[0].unit: instance
          $[0].sum: 9.0
          $[0].min: 1.0
          $[0].max: 3.0
          $[0].count: 4

    - name: get incorrectly grouped sample statistics
      url: /v2/meters/apples/statistics?groupby=house_id
      status: 400
      response_strings:
          - Invalid groupby fields

    - name: get grouped sample statistics
      url: /v2/meters/apples/statistics?groupby=resource_id
      response_json_paths:
          $[1].max: 3.0
          $[0].max: 1.0

    - name: get sample statistics bad period
      url: /v2/meters/apples/statistics?period=seven
      status: 400
      response_strings:
          - unable to convert to int

    - name: get sample statistics negative period
      url: /v2/meters/apples/statistics?period=-7
      status: 400
      response_strings:
          - Period must be positive.

    - name: get sample statistics 600 period
      url: /v2/meters/apples/statistics?period=600
      response_json_paths:
          $[0].period: 600

    - name: get sample statistics time limit not time
      url: /v2/meters/apples/statistics?q.field=timestamp&q.op=gt&q.value=Remember%20Remember
      status: 400
      response_strings:
          - invalid timestamp format

    - name: get sample statistics time limit gt
      url: /v2/meters/apples/statistics?q.field=timestamp&q.op=gt&q.value=2014-01-01
      response_json_paths:
          $[0].count: 2

    - name: get sample statistics time limit lt
      url: /v2/meters/apples/statistics?q.field=timestamp&q.op=lt&q.value=2014-01-01
      response_json_paths:
          $[0].count: 2

    - name: get sample statistics time limit bounded
      url: /v2/meters/apples/statistics?q.field=timestamp&q.op=gt&q.value=2013-06-01&q.field=timestamp&q.op=lt&q.value=2014-01-01
      response_strings:
          - "[]"

    - name: get sample statistics select aggregate bad format
      url: /v2/meters/apples/statistics?aggregate=max
      status: 400

    - name: get sample statistics select aggregate
      url: /v2/meters/apples/statistics?aggregate.func=max
      response_json_paths:
          $[0].aggregate.max: 3.0

# limit meters results

    - name: get meters ulimited
      url: /v2/meters
      response_json_paths:
          $.`len`: 2

    - name: get meters limited
      url: /v2/meters?limit=1
      response_json_paths:
          $.`len`: 1

    - name: get meters double limit
      url: /v2/meters?limit=1&limit=1
      status: 400

    - name: get meters filter limit
      desc: expressing limit this way is now disallowed
      url: /v2/meters?q.field=limit&q.op=eq&q.type=&q.value=1
      status: 400
      response_strings:
          - 'Unknown argument: \"limit\": unrecognized field in query'

    - name: get meters filter limit and limit
      url: /v2/meters?q.field=limit&q.op=eq&q.type=&q.value=1&limit=1
      status: 400
      response_strings:
          - 'Unknown argument: \"limit\": unrecognized field in query'
