Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
SMART
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
3
Issues
3
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
UNI-KLU
SMART
Commits
443d3b35
Commit
443d3b35
authored
Sep 09, 2019
by
Alexander Lercher
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Trace Retrieval: unit tests for message handling
parent
fca98b09
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
143 additions
and
15 deletions
+143
-15
.gitignore
.gitignore
+1
-0
main.py
transaction-hub-in/trace-retrieval-microservice/app/main.py
+3
-1
MessageHandler.py
...ce-retrieval-microservice/app/messaging/MessageHandler.py
+21
-14
manage_sys_paths.py
...in/trace-retrieval-microservice/tests/manage_sys_paths.py
+8
-0
test_MessageHandler.py
...trace-retrieval-microservice/tests/test_MessageHandler.py
+110
-0
No files found.
.gitignore
View file @
443d3b35
**/__pycache__
**/__pycache__
**/.vscode
*.log
*.log
\ No newline at end of file
transaction-hub-in/trace-retrieval-microservice/app/main.py
View file @
443d3b35
...
@@ -13,11 +13,13 @@ LOGGER = logging.getLogger(__name__)
...
@@ -13,11 +13,13 @@ LOGGER = logging.getLogger(__name__)
#############################
#############################
import
connexion
import
connexion
from
db.MongoRepository
import
MongoRepository
from
messaging.MessageHandler
import
MessageHandler
from
messaging.MessageHandler
import
MessageHandler
from
messaging.ReconnectingMessageManager
import
ReconnectingMessageManager
from
messaging.ReconnectingMessageManager
import
ReconnectingMessageManager
# init message handler
# init message handler
message_handler
=
MessageHandler
()
message_handler
=
MessageHandler
(
MongoRepository
(),
ReconnectingMessageManager
.
getInstance
()
)
def
message_received_callback
(
channel
,
method
,
properties
,
body
):
def
message_received_callback
(
channel
,
method
,
properties
,
body
):
message_handler
.
handle_generic
(
body
)
message_handler
.
handle_generic
(
body
)
...
...
transaction-hub-in/trace-retrieval-microservice/app/messaging/MessageHandler.py
View file @
443d3b35
from
db.MongoRepository
import
MongoRepository
from
messaging.ReconnectingMessageManager
import
ReconnectingMessageManager
import
json
import
json
import
logging
import
logging
LOGGER
=
logging
.
getLogger
(
__name__
)
LOGGER
=
logging
.
getLogger
(
__name__
)
class
MessageHandler
:
class
MessageHandler
:
MSG_NOT_JSON
=
"Message is not in JSON format and is ignored"
MSG_NO_TYPE
=
"Message has no type field and is ignored"
MSG_NOT_PROCESSED
=
"Message Type could not be processed"
MSG_TRACE_PROCESSED
=
"Message handled as blockchain-transaction"
_mongo_repo
=
None
_mongo_repo
=
None
_message_sender
=
None
_message_sender
=
None
def
__init__
(
self
):
def
__init__
(
self
,
mongo_repo
,
message_sender
):
self
.
_mongo_repo
=
MongoRepository
()
self
.
_mongo_repo
=
mongo_repo
self
.
_init_message_sender
()
self
.
_message_sender
=
message_sender
def
_init_message_sender
(
self
):
self
.
_message_sender
=
ReconnectingMessageManager
.
getInstance
()
self
.
_message_sender
.
create_message_destination
(
'datahub'
,
'direct'
)
self
.
_message_sender
.
create_message_destination
(
'datahub'
,
'direct'
)
def
handle_generic
(
self
,
body
):
def
handle_generic
(
self
,
body
):
LOGGER
.
info
(
f
"Received message: {body}"
)
LOGGER
.
info
(
f
"Received message: {body}"
)
result
=
None
message
=
None
message
=
None
try
:
try
:
message
=
json
.
loads
(
body
)
message
=
json
.
loads
(
body
)
except
ValueError
:
except
(
ValueError
,
TypeError
):
LOGGER
.
warning
(
"Message is not in JSON format and is ignored"
)
result
=
self
.
MSG_NOT_JSON
return
LOGGER
.
warning
(
result
)
return
result
if
not
'type'
in
message
:
if
not
'type'
in
message
:
LOGGER
.
warning
(
"Message has no type field and is ignored"
)
result
=
self
.
MSG_NO_TYPE
return
LOGGER
.
warning
(
result
)
return
result
if
message
[
'type'
]
==
'blockchain-transaction'
:
if
message
[
'type'
]
==
'blockchain-transaction'
:
self
.
handle_blockchain_transaction
(
message
[
'content'
])
self
.
handle_blockchain_transaction
(
message
[
'content'
])
result
=
self
.
MSG_TRACE_PROCESSED
else
:
else
:
LOGGER
.
info
(
"Message Type could not be processed"
)
result
=
self
.
MSG_NOT_PROCESSED
LOGGER
.
info
(
result
)
return
result
def
handle_blockchain_transaction
(
self
,
transaction
):
def
handle_blockchain_transaction
(
self
,
transaction
):
self
.
_mongo_repo
.
insert_trace
(
transaction
)
self
.
_mongo_repo
.
insert_trace
(
transaction
)
...
...
transaction-hub-in/trace-retrieval-microservice/tests/manage_sys_paths.py
0 → 100644
View file @
443d3b35
# add modules folder to interpreter path
import
sys
import
os
modules_paths
=
[
'../app/'
,
'../../../modules/'
]
for
path
in
modules_paths
:
if
os
.
path
.
exists
(
path
):
sys
.
path
.
insert
(
1
,
path
)
print
(
f
"added {path}"
)
transaction-hub-in/trace-retrieval-microservice/tests/test_MessageHandler.py
0 → 100644
View file @
443d3b35
import
unittest
import
manage_sys_paths
import
json
from
messaging.MessageHandler
import
MessageHandler
class
DummyMongoRepo
:
'''Dummy class to be used for testing the MessageHandler'''
last_trace
=
None
def
insert_trace
(
self
,
trace
):
self
.
last_trace
=
trace
class
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
):
handler
=
None
repo
=
None
msg_sender
=
None
def
setUp
(
self
):
self
.
repo
=
DummyMongoRepo
()
self
.
msg_sender
=
DummyMessageSender
()
self
.
handler
=
MessageHandler
(
self
.
repo
,
self
.
msg_sender
)
def
test_handleGeneric_emptyMessage_NotJsonError
(
self
):
res
=
self
.
handler
.
handle_generic
(
''
)
self
.
assertEqual
(
self
.
handler
.
MSG_NOT_JSON
,
res
)
def
test_handleGeneric_noneMessage_NotJsonError
(
self
):
res
=
self
.
handler
.
handle_generic
(
None
)
self
.
assertEqual
(
self
.
handler
.
MSG_NOT_JSON
,
res
)
def
test_handleGeneric_emptyJson1_NoTypeFieldError
(
self
):
message
=
json
.
dumps
({})
res
=
self
.
handler
.
handle_generic
(
message
)
self
.
assertEqual
(
self
.
handler
.
MSG_NO_TYPE
,
res
)
def
test_handleGeneric_emptyJson2_NoTypeFieldError
(
self
):
message
=
json
.
dumps
(
''
)
res
=
self
.
handler
.
handle_generic
(
message
)
self
.
assertEqual
(
self
.
handler
.
MSG_NO_TYPE
,
res
)
def
test_handleGeneric_missingTypeJson_NoTypeFieldError
(
self
):
message
=
json
.
dumps
({
'test'
:
'content'
})
res
=
self
.
handler
.
handle_generic
(
message
)
self
.
assertEqual
(
self
.
handler
.
MSG_NO_TYPE
,
res
)
def
test_handleGeneric_randomType1_NotProcessed
(
self
):
message
=
json
.
dumps
({
'type'
:
''
})
res
=
self
.
handler
.
handle_generic
(
message
)
self
.
assertEqual
(
self
.
handler
.
MSG_NOT_PROCESSED
,
res
)
def
test_handleGeneric_randomType2_NotProcessed
(
self
):
message
=
json
.
dumps
({
'type'
:
'test'
})
res
=
self
.
handler
.
handle_generic
(
message
)
self
.
assertEqual
(
self
.
handler
.
MSG_NOT_PROCESSED
,
res
)
def
test_handleGeneric_randomTypeWithOtherField_NotProcessed
(
self
):
message
=
json
.
dumps
({
'type'
:
'test'
,
'other'
:
'content'
})
res
=
self
.
handler
.
handle_generic
(
message
)
self
.
assertEqual
(
self
.
handler
.
MSG_NOT_PROCESSED
,
res
)
def
_get_valid_message
(
self
)
->
str
:
message_values
=
\
{
'type'
:
'blockchain-transaction'
,
'content'
:
{
"ApplicationType"
:
"string"
,
"Metadata"
:
{},
"ResourceIds"
:
"string"
,
"ResourceMd5"
:
"string"
,
"ResourceState"
:
"string"
,
"Timestamp"
:
"2019-08-27T14:00:48.587Z"
,
"TransactionFrom"
:
"string"
,
"TransactionFromLatLng"
:
"string"
,
"TransactionId"
:
"string"
,
"TransactionTo"
:
"string"
,
"TransactionToLatLng"
:
"string"
,
"TransferredAsset"
:
"string"
}
}
return
json
.
dumps
(
message_values
)
def
test_handleGeneric_correctTraceContent_ProcessedResult
(
self
):
res
=
self
.
handler
.
handle_generic
(
self
.
_get_valid_message
())
self
.
assertEqual
(
self
.
handler
.
MSG_TRACE_PROCESSED
,
res
)
def
test_handleGeneric_correctTraceContent_AddedToRepo
(
self
):
msg
=
self
.
_get_valid_message
()
res
=
self
.
handler
.
handle_generic
(
msg
)
trace
=
json
.
loads
(
msg
)[
'content'
]
self
.
assertEqual
(
trace
,
self
.
repo
.
last_trace
)
def
test_handleGeneric_correctTraceContent_NotificationSentCorrectly
(
self
):
msg
=
self
.
_get_valid_message
()
res
=
self
.
handler
.
handle_generic
(
msg
)
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'
])
if
__name__
==
'__main__'
:
unittest
.
main
()
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment