---
- name: validate data in json format using jsonschema (invalid data)
  ansible.builtin.set_fact:
    data_criteria_checks: "{{ lookup('ansible.utils.validate', show_interfaces, [oper_status, enable_check, crc_error_check], engine='ansible.utils.jsonschema') }}"
  vars:
    ansible_validate_jsonschema_draft: draft7

- ansible.builtin.assert:
    that:
      - "data_criteria_checks[0].data_path == 'GigabitEthernet0/0/0/0.oper_status'"
      - "data_criteria_checks[1].data_path == 'GigabitEthernet0/0/0/1.enabled'"
      - "data_criteria_checks[2].data_path == 'GigabitEthernet0/0/0/1.counters.in_crc_errors'"

- name: validate data in json format using jsonschema (invalid data)
  ansible.builtin.set_fact:
    data_criteria_checks: "{{ lookup('ansible.utils.validate', show_interfaces, in_rate_check) }}"
  vars:
    ansible_validate_jsonschema_draft: draft7

- ansible.builtin.assert:
    that:
      - "data_criteria_checks == []"

- name: test invalid plugin configuration option, passed within lookup plugin
  ansible.builtin.set_fact:
    data_criteria_checks: "{{ lookup('ansible.utils.validate', show_interfaces, in_rate_check, draft='draft0') }}"
  ignore_errors: true
  register: result
  vars:
    ansible_validate_jsonschema_draft: draft7

- ansible.builtin.assert:
    that:
      - "'value of draft must be one of: draft3, draft4, draft6, draft7, 2019-09, 2020-12, got: draft0' in result.msg"

- name: test invalid plugin configuration option, passed as task varaible
  ansible.builtin.set_fact:
    data_criteria_checks: "{{ lookup('ansible.utils.validate', show_interfaces, in_rate_check) }}"
  ignore_errors: true
  register: result
  vars:
    ansible_validate_jsonschema_draft: draft0

- ansible.builtin.assert:
    that:
      - "'value of draft must be one of: draft3, draft4, draft6, draft7, 2019-09, 2020-12, got: draft0' in result.msg"

- name: invalid engine value
  ansible.builtin.set_fact:
    data_criteria_checks: "{{ lookup('ansible.utils.validate', show_interfaces, in_rate_check, engine='ansible.utils.sample') }}"
  ignore_errors: true
  register: result

- ansible.builtin.assert:
    that:
      - "'errors' not in result"
      - "result['failed'] == true"
      - "'For engine \\'ansible.utils.sample\\' error loading the corresponding validate plugin' in result.msg"

- name: invalid data value
  ansible.builtin.set_fact:
    data_criteria_checks: "{{ lookup('ansible.utils.validate', 'invalid data', in_rate_check, engine='ansible.utils.jsonschema') }}"
  ignore_errors: true
  register: result

- ansible.builtin.assert:
    that:
      - "result['failed'] == true"
      - "'\\'data\\' option value is invalid' in result.msg"

- name: invalid criteria value
  ansible.builtin.set_fact:
    data_criteria_checks: "{{ lookup('ansible.utils.validate', show_interfaces, 'invalid criteria', engine='ansible.utils.jsonschema') }}"
  ignore_errors: true
  register: result

- ansible.builtin.assert:
    that:
      - "result['failed'] == true"
      - "'\\'criteria\\' option value is invalid' in result.msg"

- name: read data and criteria from file
  ansible.builtin.set_fact:
    data: "{{ lookup('ansible.builtin.file', 'data/show_interface.json') }}"
    oper_status_up_criteria: "{{ lookup('ansible.builtin.file', 'criteria/oper_status_up.json') }}"
    enabled_check_criteria: "{{ lookup('ansible.builtin.file', 'criteria/enabled_check.json') }}"
    crc_error_check_criteria: "{{ lookup('ansible.builtin.file', 'criteria/crc_error_check.json') }}"
    in_rate_check_criteria: "{{ lookup('ansible.builtin.file', 'criteria/in_rate_check.json') }}"

- name: validate data using jsonschema engine (invalid data read from file)
  ansible.builtin.set_fact:
    data_criteria_checks: "{{ lookup('ansible.utils.validate', data, [oper_status_up_criteria, enabled_check_criteria, crc_error_check_criteria], engine='ansible.utils.jsonschema') }}"

- ansible.builtin.assert:
    that:
      - "data_criteria_checks[0].data_path == 'GigabitEthernet0/0/0/0.oper_status'"
      - "data_criteria_checks[1].data_path == 'GigabitEthernet0/0/0/1.enabled'"
      - "data_criteria_checks[2].data_path == 'GigabitEthernet0/0/0/1.counters.in_crc_errors'"

- name: validate data using jsonschema engine (valid data read from file)
  ansible.builtin.set_fact:
    data_criteria_checks: "{{ lookup('ansible.utils.validate', data, in_rate_check_criteria, engine='ansible.utils.jsonschema') }}"

- ansible.builtin.assert:
    that:
      - "data_criteria_checks == []"
