1#
2# Copyright (c) 2017 Cisco and/or its affiliates.
3#
4# This file is part of Ansible
5#
6# Ansible is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# Ansible is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
18
19from __future__ import absolute_import, division, print_function
20__metaclass__ = type
21
22from ansible_collections.cisco.nso.tests.unit.compat.mock import patch
23from ansible_collections.cisco.nso.plugins.modules import nso_query
24from . import nso_module
25from .nso_module import MockResponse
26
27from ansible_collections.cisco.nso.tests.unit.plugins.modules.utils import set_module_args
28
29
30class TestNsoQuery(nso_module.TestNsoModule):
31    module = nso_query
32
33    @patch('ansible_collections.cisco.nso.plugins.module_utils.nso.open_url')
34    def test_nso_query(self, open_url_mock):
35        xpath = '/packages/package'
36        fields = ['name', 'package-version']
37        calls = [
38            MockResponse('login', {}, 200, '{}', {'set-cookie': 'id'}),
39            MockResponse('get_system_setting', {'operation': 'version'}, 200, '{"result": "4.5"}'),
40            MockResponse('new_trans', {'mode': 'read'}, 200, '{"result": {"th": 1}}'),
41            MockResponse('query',
42                         {'xpath_expr': xpath, 'selection': fields}, 200,
43                         '{"result": {"results": [["test", "1.0"]]}}'),
44            MockResponse('logout', {}, 200, '{"result": {}}'),
45        ]
46        open_url_mock.side_effect = lambda *args, **kwargs: nso_module.mock_call(calls, *args, **kwargs)
47
48        set_module_args({
49            'username': 'user', 'password': 'password',
50            'url': 'http://localhost:8080/jsonrpc',
51            'xpath': xpath,
52            'fields': fields,
53            'validate_certs': False
54        })
55        self.execute_module(changed=False, output=[["test", "1.0"]])
56
57        self.assertEqual(0, len(calls))
58