# -*- coding: utf-8 -*-

# FOGLAMP_BEGIN
# See: http://foglamp.readthedocs.io/
# FOGLAMP_END

from unittest.mock import MagicMock, patch
import pytest

from foglamp.services.core.service_registry.service_registry import ServiceRegistry as Service
from foglamp.services.core.service_registry.exceptions import DoesNotExist
from foglamp.services.core import connect
from foglamp.common.storage_client.storage_client import StorageClient

__author__ = "Ashish Jabble"
__copyright__ = "Copyright (c) 2017 OSIsoft, LLC"
__license__ = "Apache 2.0"
__version__ = "${VERSION}"


@pytest.allure.feature("unit")
@pytest.allure.story("services", "core")
class TestConnect:
    """ Storage connection"""
    
    def setup_method(self):
        Service._registry = []

    def teardown_method(self):
        Service._registry = []

    def test_get_storage(self):
        service_reg = MagicMock(spec=Service)
        service_idx = Service.register("FogLAMP Storage", "Storage", "127.0.0.1", 37449, 37843)
        with patch.object(service_reg, 'get', return_value=service_idx):
            storage_client = connect.get_storage()
            assert isinstance(storage_client, StorageClient)

    def test_get_storage_exception(self):
        service_reg = MagicMock(spec=Service)
        with patch.object(service_reg, 'get', side_effect=Exception()):
            with pytest.raises(DoesNotExist) as excinfo:
                with patch.object(connect._logger, 'exception') as logger_exception:
                    connect.get_storage()
                assert str(excinfo).endswith('DoesNotExist')
                logger_exception.assert_called_once_with()

@@ -116,19 +116,8 @@
 port
- MagicMock,
  pat
@@ -221,19 +221,8 @@
 stry
- as Service
 %0Afro
@@ -667,13 +667,8 @@
 %22%22%22%0A
-    %0A
     
@@ -698,32 +698,40 @@
 %0A        Service
+Registry
 ._registry = %5B%5D%0A
@@ -777,16 +777,24 @@
  Service
+Registry
 ._regist
@@ -846,75 +846,23 @@
     
-s
+S
 ervice
-_reg = MagicMock(spec=Service)%0A        service_idx = Service
+Registry
 .reg
@@ -928,85 +928,8 @@
 43)%0A
-        with patch.object(service_reg, 'get', return_value=service_idx):%0A    
     
@@ -963,36 +963,32 @@
 t.get_storage()%0A
-    
         assert i
@@ -1037,314 +1037,557 @@
     
-def test_get_storage_exception(self):%0A        service_reg = MagicMock(spec=Service)%0A        with patch.object(service_reg, 'get', side_effect=Exception()):%0A            with pytest.raises(DoesNotExist) as excinfo:%0A                with patch.object(connect._logger, 'exception') as logger_exception:%0A        
+@patch('foglamp.services.core.connect._logger')%0A    def test_exception_when_no_storage(self, mock_logger):%0A        with pytest.raises(DoesNotExist) as excinfo:%0A            connect.get_storage()%0A        assert str(excinfo).endswith('DoesNotExist')%0A        mock_logger.exception.assert_called_once_with('')%0A%0A    @patch('foglamp.services.core.connect._logger')%0A    def test_exception_when_non_foglamp_storage(self, mock_logger):%0A        ServiceRegistry.register(%22foo%22, %22Storage%22, %22127.0.0.1%22, 1, 2)%0A        with pytest.raises(DoesNotExist) as excinfo:%0A
     
@@ -1624,24 +1624,16 @@
         
-    
-    
 assert s
@@ -1681,23 +1681,20 @@
     
-        
+mock_
 logger
-_
+.
 exce
@@ -1723,10 +1723,12 @@
 ce_with(
+''
 )%0A

patch refactoring and 1 more test exception added for connect