Commit 4a566c1b authored by Alexander Lercher's avatar Alexander Lercher

Rest Gateway: Tests + dummy version of message-sender

parent 443d3b35
from messaging.ReconnectingMessageManager import ReconnectingMessageManager
class DummyMessageManager(ReconnectingMessageManager):
'''Dummy class to be used for testing - sets itself as singleton instance if called before ReconnectingMessageManager'''
@staticmethod
def get_instance():
if ReconnectingMessageManager._instance == None:
ReconnectingMessageManager._instance = DummyMessageManager()
assert isinstance(ReconnectingMessageManager._instance, DummyMessageManager)
return ReconnectingMessageManager._instance
last_message = {}
def _init_message_manager(self):
pass
def create_message_destination(self, exch, exch_type):
pass
def send_message(self, exch, key, mess):
self.last_message = {'ex': exch, 'key': key, 'msg': mess}
...@@ -7,7 +7,7 @@ LOGGER = logging.getLogger(__name__) ...@@ -7,7 +7,7 @@ LOGGER = logging.getLogger(__name__)
class ReconnectingMessageManager: class ReconnectingMessageManager:
'''The ReconnectingMessageManager Singleton handles connection errors by itself''' '''The ReconnectingMessageManager Singleton handles connection errors by itself'''
__instance = None _instance = None
_message_manager: MessageManager = None _message_manager: MessageManager = None
_consuming = False _consuming = False
...@@ -16,15 +16,15 @@ class ReconnectingMessageManager: ...@@ -16,15 +16,15 @@ class ReconnectingMessageManager:
@staticmethod @staticmethod
def getInstance() -> ReconnectingMessageManager: def getInstance() -> ReconnectingMessageManager:
''' Static access method. ''' ''' Static access method. '''
if ReconnectingMessageManager.__instance == None: if ReconnectingMessageManager._instance == None:
ReconnectingMessageManager.__instance = ReconnectingMessageManager() ReconnectingMessageManager._instance = ReconnectingMessageManager()
return ReconnectingMessageManager.__instance return ReconnectingMessageManager._instance
def __init__(self): def __init__(self):
if ReconnectingMessageManager.__instance != None: if ReconnectingMessageManager._instance != None:
raise Exception("This class is a singleton!") raise Exception("This class is a singleton!")
ReconnectingMessageManager.__instance = self ReconnectingMessageManager._instance = self
self._init_message_manager() self._init_message_manager()
def _init_message_manager(self): def _init_message_manager(self):
......
import unittest import unittest
import manage_sys_paths import manage_sys_paths
from messaging.DummyMessageManager import DummyMessageManager
# init dummy message manager so no connection to rabbitmq is established
_ = DummyMessageManager.get_instance()
import rest.blockchain_trace as blockchain_trace import rest.blockchain_trace as blockchain_trace
class Test_BlockchainTrace(unittest.TestCase): class Test_BlockchainTrace(unittest.TestCase):
...@@ -25,15 +29,15 @@ class Test_BlockchainTrace(unittest.TestCase): ...@@ -25,15 +29,15 @@ class Test_BlockchainTrace(unittest.TestCase):
input = self._get_valid_input() input = self._get_valid_input()
self.assertTrue(blockchain_trace.isBlockchainTraceValid(input), "Trace should be valid") self.assertTrue(blockchain_trace.isBlockchainTraceValid(input), "Trace should be valid")
def test_isBlockchainTraceValid_invalidMetadataInputType(self): # def test_isBlockchainTraceValid_invalidMetadataInputType(self):
input = self._get_valid_input() # input = self._get_valid_input()
input["Metadata"] = "string" # input["Metadata"] = "string"
self.assertFalse(blockchain_trace.isBlockchainTraceValid(input), "Metadata type should be invalid") # self.assertFalse(blockchain_trace.isBlockchainTraceValid(input), "Metadata type should be invalid")
def test_isBlockchainTraceValid_invalidTransactionFromLatLngInputType(self): # def test_isBlockchainTraceValid_invalidTransactionFromLatLngInputType(self):
input = self._get_valid_input() # input = self._get_valid_input()
input["TransactionFromLatLng"] = ["55.1", "44.1"] # input["TransactionFromLatLng"] = ["55.1", "44.1"]
self.assertFalse(blockchain_trace.isBlockchainTraceValid(input), "TransactionFromLatLng type should be invalid") # self.assertFalse(blockchain_trace.isBlockchainTraceValid(input), "TransactionFromLatLng type should be invalid")
def test_isBlockchainTraceValid_emptyInput(self): def test_isBlockchainTraceValid_emptyInput(self):
input = {} input = {}
......
...@@ -10,13 +10,7 @@ class DummyMongoRepo: ...@@ -10,13 +10,7 @@ class DummyMongoRepo:
def insert_trace(self, trace): def insert_trace(self, trace):
self.last_trace = trace self.last_trace = trace
class DummyMessageSender: from messaging.DummyMessageManager import DummyMessageManager as DummyMessageSender
'''Dummy class to be used for testing the MessageHandler'''
last_message = {}
def create_message_destination(self, exch, exch_type):
pass
def send_message(self, exch, key, mess):
self.last_message = {'ex': exch, 'key': key, 'msg': mess}
class Test_MessageHandler(unittest.TestCase): class Test_MessageHandler(unittest.TestCase):
handler = None handler = None
...@@ -25,7 +19,7 @@ class Test_MessageHandler(unittest.TestCase): ...@@ -25,7 +19,7 @@ class Test_MessageHandler(unittest.TestCase):
def setUp(self): def setUp(self):
self.repo = DummyMongoRepo() self.repo = DummyMongoRepo()
self.msg_sender = DummyMessageSender() self.msg_sender = DummyMessageSender.get_instance()
self.handler = MessageHandler(self.repo, self.msg_sender) self.handler = MessageHandler(self.repo, self.msg_sender)
def test_handleGeneric_emptyMessage_NotJsonError(self): def test_handleGeneric_emptyMessage_NotJsonError(self):
...@@ -93,14 +87,14 @@ class Test_MessageHandler(unittest.TestCase): ...@@ -93,14 +87,14 @@ class Test_MessageHandler(unittest.TestCase):
def test_handleGeneric_correctTraceContent_AddedToRepo(self): def test_handleGeneric_correctTraceContent_AddedToRepo(self):
msg = self._get_valid_message() msg = self._get_valid_message()
res = self.handler.handle_generic(msg) _ = self.handler.handle_generic(msg)
trace = json.loads(msg)['content'] trace = json.loads(msg)['content']
self.assertEqual(trace, self.repo.last_trace) self.assertEqual(trace, self.repo.last_trace)
def test_handleGeneric_correctTraceContent_NotificationSentCorrectly(self): def test_handleGeneric_correctTraceContent_NotificationSentCorrectly(self):
msg = self._get_valid_message() msg = self._get_valid_message()
res = self.handler.handle_generic(msg) _ = self.handler.handle_generic(msg)
self.assertEqual('datahub', self.msg_sender.last_message['ex']) self.assertEqual('datahub', self.msg_sender.last_message['ex'])
self.assertEqual('semantic-linking', self.msg_sender.last_message['key']) self.assertEqual('semantic-linking', self.msg_sender.last_message['key'])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment