Commit 6d7ef4dd authored by Manuel's avatar Manuel

[TraceRetrieval] fixed testcases

parent 72f33272
......@@ -25,16 +25,17 @@ class Test_Pipeline(unittest.TestCase):
return {
"type": "new-trace",
"content": {
'use_case': 'debug',
'table': 'pizza',
'id': '9e1e0983ada4e79de324a80b5f35f681c3e5016b08e1d6612c17892ade17e2c0',
"use_case": "debug",
"table": "pizza",
"id": "9e1e0983ada4e79de324a80b5f35f681c3e5016b08e1d6612c17892ade17e2c0",
'properties': {
'UniqueID': '9e1e0983ada4e79de324a80b5f35f681c3e5016b08e1d6612c17892ade17e2c0',
'cheese': False,
'dough': 'wheat',
'name': 'Margherita',
'sauce': 'tomato',
'sugar': 0.0
"UniqueID": "9e1e0983ada4e79de324a80b5f35f681c3e5016b08e1d6612c17892ade17e2c0",
"doughType": "wheat",
"firstTopping": "Tomato Sauce",
"firstToppingPrice": 1.00,
"hasSpinach": False,
"name": "Diavolo",
"toppingInfo": "Tomato Sauce1.00",
}
}
}
......
......@@ -2,10 +2,12 @@ import unittest
# import manage_sys_paths
from db.entities.layer_adapter import LayerAdapter
class Test_Layer_Adapter(unittest.TestCase):
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)
if __name__ == '__main__':
unittest.main()
......@@ -2,6 +2,7 @@ from security.token_manager import TokenManager
import network_constants
from database.entities.transaction import Transaction
from database.repository import Repository
from messaging.rest_fetcher import RestFetcher
import json
import hashlib
......@@ -20,11 +21,13 @@ class MessageHandler:
_mongo_repo = 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._message_sender = message_sender
self._message_sender.create_message_destination('datahub', 'direct')
self._rest_fetcher = rest_fetcher
def handle_generic(self, body):
LOGGER.info(f"Received message: {body}")
......@@ -119,30 +122,6 @@ class MessageHandler:
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):
'''
Proccesses a blockchain transaction. The schema gets fetched from the business-logic microservice and a flattened
......@@ -168,7 +147,7 @@ class MessageHandler:
docType = transaction_message["docType"]
try:
tables = self._fetch_schema_information(use_case)
tables = self._rest_fetcher.fetch_schema_information(use_case)
except ValueError as e:
print(f"{e}")
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
class DummyMongoRepo:
'''Dummy class to be used for testing the MessageHandler'''
last_trace = None
def __init__(self):
self.added_transactions = []
def insert_trace(self, trace):
self.last_trace = trace
def add_transaction(self, transaction):
self.added_transactions.append(transaction)
from messaging.DummyMessageManager import DummyMessageManager as DummyMessageSender
from messaging.dummy_rest_fetcher import DummyRestFetcher
class Test_MessageHandler(unittest.TestCase):
handler = None
repo = None
msg_sender = None
rest_fetcher = None
def setUp(self):
self.repo = DummyMongoRepo()
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:
message_values = \
......@@ -28,6 +38,7 @@ class Test_MessageHandler(unittest.TestCase):
'content':
{
"ApplicationType": "string",
"docType": "string",
"Metadata": {},
"ResourceIds": "string",
"ResourceMd5": "string",
......@@ -86,12 +97,10 @@ class Test_MessageHandler(unittest.TestCase):
self.assertEqual(self.handler.MSG_TRACE_PROCESSED, res)
def test_handleGeneric_correctTraceContent_AddedToRepo(self):
# TODO repo should contain processed datapoint
msg = self._get_valid_message()
_ = self.handler.handle_generic(msg)
trace = json.loads(msg)['content']
self.assertEqual(trace, self.repo.last_trace)
self.assertEqual(self.repo.added_transactions[-1].to_serializable_dict()["properties"]["RIds"], "string")
def test_handleGeneric_correctTraceContent_NotificationSentCorrectly(self):
# TODO message queue should contain new message with datapoint as content
......@@ -100,7 +109,7 @@ class Test_MessageHandler(unittest.TestCase):
self.assertEqual('datahub', self.msg_sender.last_message['ex'])
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__':
unittest.main()
\ No newline at end of file
......@@ -12,41 +12,22 @@ class DummyMongoRepo:
self.last_trace = trace
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.rest_fetcher import RestFetcher
class Test_MessageHandler(unittest.TestCase):
handler = None
repo = None
msg_sender = None
rest_fetcher = None
def setUp(self):
self.repo = DummyMongoRepo()
self.msg_sender = DummyMessageSender.get_instance()
self.handler = MessageHandler(self.repo, self.msg_sender)
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)
self.rest_fetcher = RestFetcher()
self.handler = MessageHandler(self.repo, self.msg_sender, self.rest_fetcher)
def _get_order_message(self) -> str:
message_values = \
......@@ -96,18 +77,9 @@ class Test_MessageHandler(unittest.TestCase):
}
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):
print("STARTING THE TEST...")
msg = self._get_pizza_message()
print(f"msg: {msg}")
_ = self.handler.handle_blockchain_transaction(json.loads(msg)["content"])
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