def test_should_not_enable_when_it_is_already_enabled(self):
        self.resource.get_by_uri().data = DEFAULT_RANGE_TEMPLATE

        self.mock_ansible_module.params = PARAMS_FOR_ENABLE_NO_CHANGE

        IdPoolsIpv4RangeModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=False,
            msg=IdPoolsIpv4RangeModule.MSG_ALREADY_PRESENT,
            ansible_facts=dict(id_pools_ipv4_range=DEFAULT_RANGE_TEMPLATE))
    def test_should_not_update_when_data_is_the_same(self):
        self.resource.get_by_uri().data = DEFAULT_RANGE_TEMPLATE
        self.resource.data = DEFAULT_RANGE_TEMPLATE
        self.mock_ansible_module.params = PARAMS_FOR_PRESENT_WITH_URI

        IdPoolsIpv4RangeModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=False,
            msg=IdPoolsIpv4RangeModule.MSG_ALREADY_PRESENT,
            ansible_facts=dict(id_pools_ipv4_range=DEFAULT_RANGE_TEMPLATE))
    def test_should_not_delete_when_id_pools_ipv4_range_do_not_exist(self):
        self.resource.get_by_uri.return_value = self.resource
        self.resource.data = DEFAULT_SUBNET_TEMPLATE
        self.resource.get_by_uri.side_effect = [
            DEFAULT_NOT_RANGE_TEMPLATE, DEFAULT_NOT_RANGE_TEMPLATE
        ]
        self.mock_ansible_module.params = PARAMS_FOR_ABSENT

        IdPoolsIpv4RangeModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=False, msg=IdPoolsIpv4RangeModule.MSG_ALREADY_ABSENT)
    def test_should_delete_the_ipv4_range_when_it_exists(self):
        self.resource.get_by_uri.return_value = self.resource
        self.resource.data = DEFAULT_SUBNET_TEMPLATE
        obj = mock.Mock()
        obj.data = DEFAULT_RANGE_TEMPLATE
        self.mock_ov_client.id_pools_ipv4_subnets.get_by_uri.return_value = self.resource
        self.mock_ansible_module.params = PARAMS_FOR_ABSENT

        IdPoolsIpv4RangeModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True, msg=IdPoolsIpv4RangeModule.MSG_DELETED)
    def test_should_update_when_data_is_different(self):
        new_data = DEFAULT_RANGE_TEMPLATE.copy()
        new_data['name'] = 'newRangeName'
        self.resource.get_by_uri().data = DEFAULT_RANGE_TEMPLATE
        self.resource.data = new_data
        self.resource.update.return_value = self.resource
        self.mock_ansible_module.params = PARAMS_WITH_CHANGES

        IdPoolsIpv4RangeModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=IdPoolsIpv4RangeModule.MSG_UPDATED,
            ansible_facts=dict(id_pools_ipv4_range=DEFAULT_RANGE_TEMPLATE))
    def test_should_create_new_id_pools_ipv4_range(self):

        self.mock_ov_client.id_pools_ipv4_subnets.get.return_value = DEFAULT_SUBNET_TEMPLATE
        self.mock_ov_client.id_pools_ipv4_ranges.get_by_uri(
        ).data = DEFAULT_NOT_RANGE_TEMPLATE
        self.resource.data = DEFAULT_RANGE_TEMPLATE
        self.resource.create.return_value = self.resource

        self.mock_ansible_module.params = PARAMS_FOR_PRESENT_NAME_SUBNET

        IdPoolsIpv4RangeModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=IdPoolsIpv4RangeModule.MSG_CREATED,
            ansible_facts=dict(id_pools_ipv4_range=DEFAULT_RANGE_TEMPLATE))
    def test_should_allocate_id_to_ip_range(self):
        self.resource.get_by_uri(
        ).data = DEFAULT_RANGE_TEMPLATE_Alocator_and_Collector
        self.resource.update_allocator.return_value = DEFAULT_RANGE_TEMPLATE_Alocator_and_Collector
        self.mock_ansible_module.params = PARAMS_FOR_ALLOCATOR

        IdPoolsIpv4RangeModule().run()

        self.resource.update_allocator.assert_called_once_with(
            dict(idList=['172.0.0.0', '172.1.1.1'], count=2),
            DEFAULT_RANGE_TEMPLATE['uri'])

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=IdPoolsIpv4RangeModule.MSG_UPDATED,
            ansible_facts=dict(id_pools_ipv4_range=
                               DEFAULT_RANGE_TEMPLATE_Alocator_and_Collector))
    def test_should_disable_when_it_is_enabled(self):
        self.resource.get_by_uri().data = DEFAULT_RANGE_TEMPLATE

        new_data = DEFAULT_RANGE_TEMPLATE.copy()
        new_data['enabled'] = False

        self.resource.enable.return_value = new_data

        self.mock_ansible_module.params = PARAMS_FOR_ENABLE_EDIT

        IdPoolsIpv4RangeModule().run()

        self.resource.enable.assert_called_once_with(
            dict(type='Range', enabled=False), DEFAULT_RANGE_TEMPLATE['uri'])

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=IdPoolsIpv4RangeModule.MSG_UPDATED,
            ansible_facts=dict(id_pools_ipv4_range=new_data))