# Copyright (c) 2014 Hewlett-Packard Development Company, L.P.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import contextlib

import mock
from oslo_vmware import api
from oslo_vmware import vim

from networking_vsphere.tests import base
from networking_vsphere.utils import vim_session


class TestVmwareApiSession(base.TestCase):

    def setUp(self):
        super(TestVmwareApiSession, self).setUp()
        self.host_ip = "192.168.1.3"
        self.host_username = "user"
        self.host_password = "password"
        self.api_retry_count = 2
        self.wsdl_url = "fake_url"
        self.vm_session = vim_session.VMWareAPISession(self.host_ip,
                                                       self.host_username,
                                                       self.host_password,
                                                       self.api_retry_count,
                                                       self.wsdl_url,
                                                       create_session=False)

    def test_call_method(self):
        with contextlib.nested(
            mock.patch.object(api.VMwareAPISession,
                              "invoke_api"),
            mock.patch.object(self.vm_session,
                              "_is_vim_object",
                              return_value=True)
            ) as (invoke_ob, is_vim_ob):
            self.vm_session._call_method("fake_module",
                                         "get_objects",
                                         "HostSystem", ['name'])
            self.assertTrue(invoke_ob.called)

    def test_call_method_with_vim_object_false(self):
        vim.Vim = mock.Mock()
        with contextlib.nested(
            mock.patch.object(api.VMwareAPISession,
                              "invoke_api"),
            mock.patch.object(api.VMwareAPISession,
                              "vim",
                              return_value=vim.Vim),
            mock.patch.object(self.vm_session,
                              "_is_vim_object",
                              return_value=False)
            ) as (invoke_ob, vim_prop, is_vim_ob):
            self.vm_session._call_method("fake_module",
                                         "get_objects",
                                         "HostSystem", ['name'])
            self.assertTrue(invoke_ob.called)

    def test_get_vim(self):
        vim.Vim = mock.Mock(return_value="fake_vim")
        with contextlib.nested(
            mock.patch.object(api.VMwareAPISession,
                              "vim",
                              return_value=vim.Vim)):
            new_vim = self.vm_session._get_vim()
            self.assertEqual(new_vim, self.vm_session.vim)


class TestConnectionHandler(base.TestCase):

    def setUp(self):
        super(TestConnectionHandler, self).setUp()
        self.host_ip = "192.168.1.3"
        self.host_username = "user"
        self.host_password = "password"
        self.api_retry_count = 2
        self.wsdl_url = "fake_url"

    def test_create_connection(self):
        vim_session.ConnectionHandler.set_vc_details(self.host_ip,
                                                     self.host_username,
                                                     self.host_password,
                                                     self.api_retry_count,
                                                     self.wsdl_url)
        vim_session.ConnectionHandler.create_session = False
        vm_session = vim_session.ConnectionHandler.create_connection()
        self.assertEqual(vim_session.ConnectionHandler.host_ip,
                         self.host_ip)
        self.assertEqual(vim_session.ConnectionHandler.host_username,
                         self.host_username)
        self.assertEqual(vim_session.ConnectionHandler.host_password,
                         self.host_password)
        self.assertEqual(vim_session.ConnectionHandler.api_retry_count, 2)
        self.assertEqual(vim_session.ConnectionHandler.wsdl_url,
                         self.wsdl_url)
        self.assertTrue(vm_session)

    def test_connection_handler_stop(self):
        vim_session.ConnectionHandler.set_vc_details(self.host_ip,
                                                     self.host_username,
                                                     self.host_password,
                                                     self.api_retry_count,
                                                     self.wsdl_url)
        vim_session.ConnectionHandler.create_session = False
        vim_session.ConnectionHandler.create_connection()
        with mock.patch.object(api.VMwareAPISession,
                               "logout") as log_ob:
            vim_session.ConnectionHandler.stop()
            self.assertTrue(log_ob.called)
            self.assertTrue(vim_session.ConnectionHandler.stopped)

    def test_connection_handler_start(self):
        vim_session.ConnectionHandler.start()
        self.assertFalse(vim_session.ConnectionHandler.stopped)

    def test_get_connection(self):
        vim_session.ConnectionHandler.set_vc_details(self.host_ip,
                                                     self.host_username,
                                                     self.host_password,
                                                     self.api_retry_count,
                                                     self.wsdl_url)
        vim_session.ConnectionHandler.create_session = False
        vm_session = vim_session.ConnectionHandler.create_connection()
        new_session = vim_session.ConnectionHandler.get_connection()
        self.assertEqual(vm_session, new_session)
