Commit 6d7ef4dd authored by Manuel's avatar Manuel

[TraceRetrieval] fixed testcases

parent 72f33272
...@@ -25,16 +25,17 @@ class Test_Pipeline(unittest.TestCase): ...@@ -25,16 +25,17 @@ class Test_Pipeline(unittest.TestCase):
return { return {
"type": "new-trace", "type": "new-trace",
"content": { "content": {
'use_case': 'debug', "use_case": "debug",
'table': 'pizza', "table": "pizza",
'id': '9e1e0983ada4e79de324a80b5f35f681c3e5016b08e1d6612c17892ade17e2c0', "id": "9e1e0983ada4e79de324a80b5f35f681c3e5016b08e1d6612c17892ade17e2c0",
'properties': { 'properties': {
'UniqueID': '9e1e0983ada4e79de324a80b5f35f681c3e5016b08e1d6612c17892ade17e2c0', "UniqueID": "9e1e0983ada4e79de324a80b5f35f681c3e5016b08e1d6612c17892ade17e2c0",
'cheese': False, "doughType": "wheat",
'dough': 'wheat', "firstTopping": "Tomato Sauce",
'name': 'Margherita', "firstToppingPrice": 1.00,
'sauce': 'tomato', "hasSpinach": False,
'sugar': 0.0 "name": "Diavolo",
"toppingInfo": "Tomato Sauce1.00",
} }
} }
} }
......
...@@ -2,10 +2,12 @@ import unittest ...@@ -2,10 +2,12 @@ import unittest
# import manage_sys_paths # import manage_sys_paths
from db.entities.layer_adapter import LayerAdapter from db.entities.layer_adapter import LayerAdapter
class Test_Layer_Adapter(unittest.TestCase): class Test_Layer_Adapter(unittest.TestCase):
def test_valid_adapter(self): def test_valid_adapter(self):
adapter1 = LayerAdapter("layer1", {"a":"b", "c":"d"}, ["a"]) adapter1 = LayerAdapter("layer1", "use_case", "table", ["a", "c"], ["a"])
print(adapter1.to_serializable_dict) print(adapter1.to_serializable_dict)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
...@@ -2,6 +2,7 @@ from security.token_manager import TokenManager ...@@ -2,6 +2,7 @@ from security.token_manager import TokenManager
import network_constants import network_constants
from database.entities.transaction import Transaction from database.entities.transaction import Transaction
from database.repository import Repository from database.repository import Repository
from messaging.rest_fetcher import RestFetcher
import json import json
import hashlib import hashlib
...@@ -20,11 +21,13 @@ class MessageHandler: ...@@ -20,11 +21,13 @@ class MessageHandler:
_mongo_repo = None _mongo_repo = None
_message_sender = None _message_sender = None
_rest_fetcher = None
def __init__(self, mongo_repo, message_sender): def __init__(self, mongo_repo, message_sender, rest_fetcher:RestFetcher):
self._mongo_repo = mongo_repo self._mongo_repo = mongo_repo
self._message_sender = message_sender self._message_sender = message_sender
self._message_sender.create_message_destination('datahub', 'direct') self._message_sender.create_message_destination('datahub', 'direct')
self._rest_fetcher = rest_fetcher
def handle_generic(self, body): def handle_generic(self, body):
LOGGER.info(f"Received message: {body}") LOGGER.info(f"Received message: {body}")
...@@ -119,30 +122,6 @@ class MessageHandler: ...@@ -119,30 +122,6 @@ class MessageHandler:
return flattened return flattened
def _fetch_schema_information(self, use_case: str) -> Dict:
'''
Fetches the schema of the use-case from the business-logic microservice per REST.
@params
use_case - Required: string identifier for the use-case
'''
jwt_token = TokenManager.getInstance().getToken()
# query tables for use-case
url = f'https://{network_constants.BUSINESS_LOGIC_HOSTNAME}:{network_constants.BUSINESS_LOGIC_REST_PORT}/api/use-cases/{use_case}/tables'
response = requests.get(
url,
verify = False,
proxies = { "http":None, "https":None },
headers = { "Authorization": f"Bearer {jwt_token}"}
)
if response.status_code != 200:
raise ValueError("Error while retrieving schema information")
tables = json.loads(response.text)
return tables
def handle_blockchain_transaction(self, transaction_message: Dict): def handle_blockchain_transaction(self, transaction_message: Dict):
''' '''
Proccesses a blockchain transaction. The schema gets fetched from the business-logic microservice and a flattened Proccesses a blockchain transaction. The schema gets fetched from the business-logic microservice and a flattened
...@@ -168,7 +147,7 @@ class MessageHandler: ...@@ -168,7 +147,7 @@ class MessageHandler:
docType = transaction_message["docType"] docType = transaction_message["docType"]
try: try:
tables = self._fetch_schema_information(use_case) tables = self._rest_fetcher.fetch_schema_information(use_case)
except ValueError as e: except ValueError as e:
print(f"{e}") print(f"{e}")
self._mongo_repo.add_failed_transaction(transaction_message) self._mongo_repo.add_failed_transaction(transaction_message)
......
from messaging.rest_fetcher import RestFetcher
class DummyRestFetcher(RestFetcher):
def fetch_schema_information(self, use_case: str):
return [
{
"name": "string",
"use_case": "string",
"mappings": {
"UniqueID": "ResourceIds",
"RIds": "ResourceIds"
}
}
]
from security.token_manager import TokenManager
import network_constants
from typing import List
import json, requests
class RestFetcher:
def fetch_schema_information(self, use_case: str) -> List:
jwt_token = TokenManager.getInstance().getToken()
# query tables for use-case
url = f'https://{network_constants.BUSINESS_LOGIC_HOSTNAME}:{network_constants.BUSINESS_LOGIC_REST_PORT}/api/use-cases/{use_case}/tables'
response = requests.get(
url,
verify = False,
proxies = { "http":None, "https":None },
headers = { "Authorization": f"Bearer {jwt_token}"}
)
if response.status_code != 200:
raise ValueError("Error while retrieving schema information")
tables = json.loads(response.text)
return tables
...@@ -7,20 +7,30 @@ from messaging.MessageHandler import MessageHandler ...@@ -7,20 +7,30 @@ from messaging.MessageHandler import MessageHandler
class DummyMongoRepo: class DummyMongoRepo:
'''Dummy class to be used for testing the MessageHandler''' '''Dummy class to be used for testing the MessageHandler'''
last_trace = None last_trace = None
def __init__(self):
self.added_transactions = []
def insert_trace(self, trace): def insert_trace(self, trace):
self.last_trace = trace self.last_trace = trace
def add_transaction(self, transaction):
self.added_transactions.append(transaction)
from messaging.DummyMessageManager import DummyMessageManager as DummyMessageSender from messaging.DummyMessageManager import DummyMessageManager as DummyMessageSender
from messaging.dummy_rest_fetcher import DummyRestFetcher
class Test_MessageHandler(unittest.TestCase): class Test_MessageHandler(unittest.TestCase):
handler = None handler = None
repo = None repo = None
msg_sender = None msg_sender = None
rest_fetcher = None
def setUp(self): def setUp(self):
self.repo = DummyMongoRepo() self.repo = DummyMongoRepo()
self.msg_sender = DummyMessageSender.get_instance() self.msg_sender = DummyMessageSender.get_instance()
self.handler = MessageHandler(self.repo, self.msg_sender) self.rest_fetcher = DummyRestFetcher()
self.handler = MessageHandler(self.repo, self.msg_sender, self.rest_fetcher)
def _get_valid_message(self) -> str: def _get_valid_message(self) -> str:
message_values = \ message_values = \
...@@ -28,6 +38,7 @@ class Test_MessageHandler(unittest.TestCase): ...@@ -28,6 +38,7 @@ class Test_MessageHandler(unittest.TestCase):
'content': 'content':
{ {
"ApplicationType": "string", "ApplicationType": "string",
"docType": "string",
"Metadata": {}, "Metadata": {},
"ResourceIds": "string", "ResourceIds": "string",
"ResourceMd5": "string", "ResourceMd5": "string",
...@@ -86,12 +97,10 @@ class Test_MessageHandler(unittest.TestCase): ...@@ -86,12 +97,10 @@ class Test_MessageHandler(unittest.TestCase):
self.assertEqual(self.handler.MSG_TRACE_PROCESSED, res) self.assertEqual(self.handler.MSG_TRACE_PROCESSED, res)
def test_handleGeneric_correctTraceContent_AddedToRepo(self): def test_handleGeneric_correctTraceContent_AddedToRepo(self):
# TODO repo should contain processed datapoint
msg = self._get_valid_message() msg = self._get_valid_message()
_ = self.handler.handle_generic(msg) _ = self.handler.handle_generic(msg)
trace = json.loads(msg)['content'] self.assertEqual(self.repo.added_transactions[-1].to_serializable_dict()["properties"]["RIds"], "string")
self.assertEqual(trace, self.repo.last_trace)
def test_handleGeneric_correctTraceContent_NotificationSentCorrectly(self): def test_handleGeneric_correctTraceContent_NotificationSentCorrectly(self):
# TODO message queue should contain new message with datapoint as content # TODO message queue should contain new message with datapoint as content
...@@ -100,7 +109,7 @@ class Test_MessageHandler(unittest.TestCase): ...@@ -100,7 +109,7 @@ class Test_MessageHandler(unittest.TestCase):
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'])
self.assertEqual(json.dumps({'type': 'new-traces-available'}), self.msg_sender.last_message['msg']) self.assertEqual('new-trace', json.loads(self.msg_sender.last_message['msg'])["type"])
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
\ No newline at end of file
...@@ -12,41 +12,22 @@ class DummyMongoRepo: ...@@ -12,41 +12,22 @@ class DummyMongoRepo:
self.last_trace = trace self.last_trace = trace
def add_transaction(self, transaction: Transaction): def add_transaction(self, transaction: Transaction):
print(str(transaction.to_serializable_dict())) self.last_transaction = transaction
from messaging.DummyMessageManager import DummyMessageManager as DummyMessageSender from messaging.DummyMessageManager import DummyMessageManager as DummyMessageSender
from messaging.rest_fetcher import RestFetcher
class Test_MessageHandler(unittest.TestCase): class Test_MessageHandler(unittest.TestCase):
handler = None handler = None
repo = None repo = None
msg_sender = None msg_sender = None
rest_fetcher = None
def setUp(self): def setUp(self):
self.repo = DummyMongoRepo() self.repo = DummyMongoRepo()
self.msg_sender = DummyMessageSender.get_instance() self.msg_sender = DummyMessageSender.get_instance()
self.handler = MessageHandler(self.repo, self.msg_sender) self.rest_fetcher = RestFetcher()
self.handler = MessageHandler(self.repo, self.msg_sender, self.rest_fetcher)
def _get_valid_message(self) -> str:
message_values = \
{
'type': 'blockchain-transaction',
'content':
{
"ApplicationType": "smart-energy",
"Customer": 13,
"Postcode": 2261,
"Timestamp": "01.07.2012 00:30",
"Solar_Production_kWh": 0.0,
"Energy_Consumption_kWh": 0.23399999999999999,
"Heating_Consumption_kWh": 0.23399999999999999,
"Price_AUD/MWh": 57.04,
"Total_Demand_MWh": 8097.93,
"Latitude": -33.362679,
"Longitude": 151.447302,
}
}
return json.dumps(message_values)
def _get_order_message(self) -> str: def _get_order_message(self) -> str:
message_values = \ message_values = \
...@@ -96,18 +77,9 @@ class Test_MessageHandler(unittest.TestCase): ...@@ -96,18 +77,9 @@ class Test_MessageHandler(unittest.TestCase):
} }
return json.dumps(message_values) return json.dumps(message_values)
# def test_handleGeneric_correctTraceContent_NotificationSentCorrectly(self):
# msg = self._get_valid_message()
# _ = self.handler.handle_blockchain_transaction(msg)
def test_HandlePizzaMessage(self): def test_HandlePizzaMessage(self):
print("STARTING THE TEST...")
msg = self._get_pizza_message() msg = self._get_pizza_message()
print(f"msg: {msg}")
_ = self.handler.handle_blockchain_transaction(json.loads(msg)["content"]) _ = self.handler.handle_blockchain_transaction(json.loads(msg)["content"])
if __name__ == '__main__': if __name__ == '__main__':
......
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