# -*- coding: utf-8 -*-
'''
    :codeauthor: :email:`Anthony Shaw <anthonyshaw@apache.org>`
'''

# Import Python Libs
from __future__ import absolute_import

# Import Salt Testing Libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import TestCase, skipIf
from tests.support.mock import (
    patch,
    MagicMock,
    NO_MOCK,
    NO_MOCK_REASON
)
from functools import wraps


# Test data
TEST_FACTS = {
    'uptime': 'Forever',
    'UP': True
}

TEST_ENVIRONMENT = {
    'hot': 'yes'
}


class MockNapalmDevice(object):
    '''Setup a mock device for our tests'''
    def get_facts():
        return TEST_FACTS

    def get_environment():
        return TEST_ENVIRONMENT

    def get(key):
        return TEST_FACTS[key]


def mock_proxy_napalm_wrap(func):
    '''
    The proper decorator checks for proxy minions. We don't care
    so just pass back to the origination function
    '''

    @wraps(func)
    def func_wrapper(*args, **kwargs):
        func.__globals__['napalm_device'] = MockNapalmDevice()
        return func(*args, **kwargs)
    return func_wrapper


import salt.utils.napalm as napalm_utils
napalm_utils.proxy_napalm_wrap = mock_proxy_napalm_wrap

import salt.modules.napalm_network as napalm_network


@skipIf(NO_MOCK, NO_MOCK_REASON)
class NapalmNetworkModuleTestCase(TestCase, LoaderModuleMockMixin):

    def setup_loader_modules(self):
        # TODO: Determine configuration best case
        module_globals = {
            '__salt__': {
                'config.option': MagicMock(return_value={
                    'test': {
                        'driver': 'test',
                        'key': '2orgk34kgk34g'
                    }
                })
            }
        }

        return {napalm_network: module_globals}

    def test_init(self):
        with patch('salt.utils.compat.pack_dunder', return_value=False) as dunder:
            napalm_network.__init__("test")
            dunder.assert_called_with('salt.modules.napalm_network')

    def test_connected_pass(self):
        ret = napalm_network.connected()
        assert ret['out'] is True

    def test_facts(self):
        ret = napalm_network.facts()
        assert ret == TEST_FACTS

    def test_environment(self):
        ret = napalm_network.environment()
        assert ret == TEST_ENVIRONMENT

    def test_cli_single_command(self):
        '''
        Test that CLI works with 1 arg
        '''
        ret = napalm_network.cli("show run")
        assert ret == TEST_COMMAND_RESPONSE

@@ -440,85 +440,215 @@
    '
-uptime': 'Forever',%0A    'UP': True%0A%7D%0A%0ATEST_ENVIRONMENT = %7B%0A    'hot': 'yes'%0A%7D
+__opts__': %7B%7D,%0A    'OPTIONAL_ARGS': %7B%7D,%0A    'uptime': 'Forever',%0A    'UP': True,%0A    'HOSTNAME': 'test-device.com'%0A%7D%0A%0ATEST_ENVIRONMENT = %7B%0A    'hot': 'yes'%0A%7D%0A%0ATEST_COMMAND_RESPONSE = 'all the command output'
 %0A%0A%0Ac
@@ -740,16 +740,20 @@
 t_facts(
+self
 ):%0A     
@@ -798,16 +798,20 @@
 ronment(
+self
 ):%0A     
@@ -854,44 +854,335 @@
 get(
-key):%0A        return TEST_FACTS%5Bkey%5D
+self, key, default=None, *args, **kwargs):%0A        try:%0A            if key == 'DRIVER':%0A                return self%0A            return TEST_FACTS%5Bkey%5D%0A        except KeyError:%0A            return default%0A%0A    def cli(self, commands, *args, **kwargs):%0A        assert commands%5B0%5D == 'show run'%0A        return TEST_COMMAND_RESPONSE
 %0A%0A%0Ad
@@ -2218,230 +2218,8 @@
 s%7D%0A%0A
-    def test_init(self):%0A        with patch('salt.utils.compat.pack_dunder', return_value=False) as dunder:%0A            napalm_network.__init__(%22test%22)%0A            dunder.assert_called_with('salt.modules.napalm_network')%0A%0A
     
@@ -2398,32 +2398,39 @@
       assert ret
+%5B'out'%5D
  == TEST_FACTS%0A%0A
@@ -2514,32 +2514,39 @@
       assert ret
+%5B'out'%5D
  == TEST_ENVIRON
@@ -2716,16 +2716,23 @@
 sert ret
+%5B'out'%5D
  == TEST

update tests with correct assertions and mock methods on device instance