# Path: data_catalog/notifier.py
# class CFNotifier(object):
# 
#     """
#     Class responsible for notifying NATS service of Data Catalog's actions.
#     """
# 
#     def __init__(self, config):
#         self._connection = pynats.Connection(url=config.services_url.nats_url, verbose=True)
#         self._subject = config.services_url.nats_subject
#         self._log = logging.getLogger(type(self).__name__)
# 
#         self._log.info(
#             'CloudFoundry notifier will talk to NATS at %s on subject %s',
#             config.services_url.nats_url,
#             config.services_url.nats_subject
#         )
# 
#     def notify(self, message, org_guid):
#         """
#         send message to NAT's service
#         :param message: message to send
#         :param org_guid: organization guid to which this message is connected
#         """
#         self._connection.connect()
#         nats_message = self._create_message(message, org_guid)
#         self._connection.publish(self._subject, json.dumps(nats_message))
# 
#     @staticmethod
#     def _create_message(message, org_guid):
#         return {'OrgGuid': org_guid,
#                 'Message': message,
#                 'Timestamp': CFNotifier._get_timestamp()}
# 
#     @staticmethod
#     def _get_timestamp():
#         return int(time.time()*1000)
# 
# Path: tests/base_test.py
# class DataCatalogTestCase(unittest.TestCase):
#     def setUp(self):
#         setup_fake_env()
#         self._config = DCConfig()
# 
#         self.app = data_catalog.app._create_app(self._config)
#         self.app.config['TESTING'] = True
#         self.client = self.app.test_client()
# 
#         self.mocked_authenticate = disable_authentication(self.client)
# 
#     def tearDown(self):
#         clean_fake_env()


import unittest
import os.path
import sys
import json
from mock import patch, MagicMock
from data_catalog.notifier import (CFNotifier)
from tests.base_test import DataCatalogTestCase
#
#    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.
#


#path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
#path = os.path.join(path, 'pynats')
#sys.path.insert(1, path)


class CFNotifierTests(DataCatalogTestCase):

    @patch('pynats.Connection')
    def test_notify_correctMessgeSend(self, mock_connection):
        message = 'message'
        guid = 'guid'
        mock_con_val = mock_connection.return_value

        notifier = CFNotifier(self._config)
        notifier.notify(message, guid)

        self.assertTrue(mock_con_val.connect.called)
        self.assertTrue(mock_con_val.publish.called)


# Path: data_catalog/notifier.py
# class CFNotifier(object):
# 
#     """
#     Class responsible for notifying NATS service of Data Catalog's actions.
#     """
# 
#     def __init__(self, config):
#         self._connection = pynats.Connection(url=config.services_url.nats_url, verbose=True)
#         self._subject = config.services_url.nats_subject
#         self._log = logging.getLogger(type(self).__name__)
# 
#         self._log.info(
#             'CloudFoundry notifier will talk to NATS at %s on subject %s',
#             config.services_url.nats_url,
#             config.services_url.nats_subject
#         )
# 
#     def notify(self, message, org_guid):
#         """
#         send message to NAT's service
#         :param message: message to send
#         :param org_guid: organization guid to which this message is connected
#         """
#         self._connection.connect()
#         nats_message = self._create_message(message, org_guid)
#         self._connection.publish(self._subject, json.dumps(nats_message))
# 
#     @staticmethod
#     def _create_message(message, org_guid):
#         return {'OrgGuid': org_guid,
#                 'Message': message,
#                 'Timestamp': CFNotifier._get_timestamp()}
# 
#     @staticmethod
#     def _get_timestamp():
#         return int(time.time()*1000)
# 
# Path: tests/base_test.py
# class DataCatalogTestCase(unittest.TestCase):
#     def setUp(self):
#         setup_fake_env()
#         self._config = DCConfig()
# 
#         self.app = data_catalog.app._create_app(self._config)
#         self.app.config['TESTING'] = True
#         self.client = self.app.test_client()
# 
#         self.mocked_authenticate = disable_authentication(self.client)
# 
#     def tearDown(self):
#         clean_fake_env()


# Path: tests/test_notifier.py
import unittest
import os.path
import sys
import json
from mock import patch, MagicMock
from data_catalog.notifier import (CFNotifier)
from tests.base_test import DataCatalogTestCase

#
#    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.
#


#path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
#path = os.path.join(path, 'pynats')
#sys.path.insert(1, path)


class CFNotifierTests(DataCatalogTestCase):

    @patch('pynats.Connection')
    def test_notify_correctMessgeSend(self, mock_connection):
        message = 'message'
        guid = 'guid'
        mock_con_val = mock_connection.return_value

        notifier = CFNotifier(self._config)
        notifier.notify(message, guid)

        self.assertTrue(mock_con_val.connect.called)
        self.assertTrue(mock_con_val.publish.called)


        subject = mock_con_val.publish.call_args[0][0]