Skip to content

Commit

Permalink
Add ability to deserialize single message with message.loads
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Wojciechowski <s.wojciechowski89@gmail.com>
  • Loading branch information
sebwoj committed May 15, 2019
1 parent be3e885 commit a783742
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 81 deletions.
109 changes: 53 additions & 56 deletions fedora_messaging/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,84 +244,81 @@ def dumps(messages):

def loads(serialized_messages):
"""
Deserialize messages from a JSON formatted str
Deserialize message from a JSON formatted str.
Args:
serialized_messages (JSON str):
serialized_message (JSON str): JSON string message to deserialize.
Returns:
list: Deserialized message objects.
Message: Deserialized message object.
Raises:
ValidationError: If deserialized message validation failed.
KeyError: If serialized_messages aren't properly serialized.
ValueError: If serialized_messages is not valid JSON
KeyError: If serialized_message aren't properly serialized.
ValueError: If serialized_message is not valid JSON
"""
try:
messages_dicts = json.loads(serialized_messages)
message_dict = json.loads(serialized_messages)
except ValueError:
_log.error("Loading serialized messages failed.")
raise

messages = []
for message_dict in messages_dicts:
try:
headers = message_dict["headers"]
except KeyError:
_log.error("Message saved without headers.")
raise
try:
headers = message_dict["headers"]
except KeyError:
_log.error("Message saved without headers.")
raise

try:
MessageClass = get_class(headers["fedora_messaging_schema"])
except KeyError:
_log.error("Message (headers=%r) saved without a schema header.", headers)
raise
try:
MessageClass = get_class(headers["fedora_messaging_schema"])
except KeyError:
_log.error("Message (headers=%r) saved without a schema header.", headers)
raise

try:
body = message_dict["body"]
except KeyError:
_log.error("Message saved without body.")
raise
try:
body = message_dict["body"]
except KeyError:
_log.error("Message saved without body.")
raise

try:
id = message_dict["id"]
except KeyError:
_log.error("Message saved without id.")
raise
try:
id = message_dict["id"]
except KeyError:
_log.error("Message saved without id.")
raise

try:
queue = message_dict["queue"]
except KeyError:
_log.warning("Message saved without queue.")
queue = None
try:
queue = message_dict["queue"]
except KeyError:
_log.warning("Message saved without queue.")
queue = None

try:
topic = message_dict["topic"]
except KeyError:
_log.error("Message saved without topic.")
raise
try:
topic = message_dict["topic"]
except KeyError:
_log.error("Message saved without topic.")
raise

try:
severity = headers["fedora_messaging_severity"]
except KeyError:
_log.error("Message saved without a severity.")
raise
try:
severity = headers["fedora_messaging_severity"]
except KeyError:
_log.error("Message saved without a severity.")
raise

message = MessageClass(
body=body, topic=topic, headers=headers, severity=severity
)
try:
message.validate()
_log.debug("Successfully validated message %r", message)
except jsonschema.exceptions.ValidationError as e:
_log.error("Message validation of %r failed: %r", message, e)
raise ValidationError(e)
message = MessageClass(
body=body, topic=topic, headers=headers, severity=severity
)
try:
message.validate()
_log.debug("Successfully validated message %r", message)
except jsonschema.exceptions.ValidationError as e:
_log.error("Message validation of %r failed: %r", message, e)
raise ValidationError(e)

message.queue = queue
message.id = id
messages.append(message)
message.queue = queue
message.id = id

return messages
return message


class Message(object):
Expand Down
46 changes: 21 additions & 25 deletions fedora_messaging/tests/unit/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,14 @@ def test_improper_messages(self):
class MessageLoadsTests(unittest.TestCase):
"""Tests for the :func:`fedora_messaging.message.loads` function."""

def test_proper_json(self):
def test_proper_json_with_dict(self):
"""Assert loading message from json work."""
message_json = (
'[{"topic": "test topic", "headers": {"fedora_messaging_schema": "base.message", '
'{"topic": "test topic", "headers": {"fedora_messaging_schema": "base.message", '
'"fedora_messaging_severity": 30}, "id": "test id", "body": '
'{"test_key": "test_value"}, "queue": "test queue"}]'
'{"test_key": "test_value"}, "queue": "test queue"}'
)
messages = message.loads(message_json)
test_message = messages[0]
self.assertEqual(len(messages), 1)
test_message = message.loads(message_json)
self.assertIsInstance(test_message, message.Message)
self.assertEqual("test topic", test_message.topic)
self.assertEqual("test id", test_message.id)
Expand All @@ -134,8 +132,8 @@ def test_improper_json(self):
def test_missing_headers(self):
"""Assert proper exception is raised when headers are missing."""
message_json = (
'[{"topic": "test topic", "id": "test id",'
'"body": {"test_key": "test_value"}, "queue": "test queue"}]'
'{"topic": "test topic", "id": "test id",'
'"body": {"test_key": "test_value"}, "queue": "test queue"}'
)
with mock.patch("fedora_messaging.message._log") as mock_log:
self.assertRaises(KeyError, message.loads, message_json)
Expand All @@ -144,8 +142,8 @@ def test_missing_headers(self):
def test_missing_messaging_schema(self):
"""Assert proper exception is raised when messaging schema missing."""
message_json = (
'[{"topic": "test topic", "headers": {"fedora_messaging_severity": 30}, '
'"id": "test id", "body": {"test_key": "test_value"}, "queue": "test queue"}]'
'{"topic": "test topic", "headers": {"fedora_messaging_severity": 30}, '
'"id": "test id", "body": {"test_key": "test_value"}, "queue": "test queue"}'
)
with mock.patch("fedora_messaging.message._log") as mock_log:
self.assertRaises(KeyError, message.loads, message_json)
Expand All @@ -157,9 +155,9 @@ def test_missing_messaging_schema(self):
def test_missing_body(self):
"""Assert proper exception is raised when body is missing."""
message_json = (
'[{"topic": "test topic", "headers": {"fedora_messaging_schema": '
'{"topic": "test topic", "headers": {"fedora_messaging_schema": '
'"base.message", "fedora_messaging_severity": 30}, "id": "test id", '
'"queue": "test queue"}]'
'"queue": "test queue"}'
)
with mock.patch("fedora_messaging.message._log") as mock_log:
self.assertRaises(KeyError, message.loads, message_json)
Expand All @@ -168,9 +166,9 @@ def test_missing_body(self):
def test_missing_id(self):
"""Assert proper exception is raised when id is missing."""
message_json = (
'[{"topic": "test topic", "headers": {"fedora_messaging_schema": '
'{"topic": "test topic", "headers": {"fedora_messaging_schema": '
'"base.message", "fedora_messaging_severity": 30}, '
'"body": {"test_key": "test_value"}, "queue": "test queue"}]'
'"body": {"test_key": "test_value"}, "queue": "test queue"}'
)
with mock.patch("fedora_messaging.message._log") as mock_log:
self.assertRaises(KeyError, message.loads, message_json)
Expand All @@ -179,15 +177,13 @@ def test_missing_id(self):
def test_missing_queue(self):
"""Assert message without queue is returned and warning is printed."""
message_json = (
'[{"topic": "test topic", "headers": {"fedora_messaging_schema": '
'{"topic": "test topic", "headers": {"fedora_messaging_schema": '
'"base.message", "fedora_messaging_severity": 30}, "id": "test id", '
'"body": {"test_key": "test_value"}}]'
'"body": {"test_key": "test_value"}}'
)
with mock.patch("fedora_messaging.message._log") as mock_log:
messages = message.loads(message_json)
test_message = message.loads(message_json)
mock_log.warning.assert_called_once_with("Message saved without queue.")
test_message = messages[0]
self.assertEqual(len(messages), 1)
self.assertEqual("test topic", test_message.topic)
self.assertEqual("test id", test_message.id)
self.assertEqual({"test_key": "test_value"}, test_message.body)
Expand All @@ -202,9 +198,9 @@ def test_missing_queue(self):
def test_missing_topic(self):
"""Assert proper exception is raised when topic is missing."""
message_json = (
'[{"headers": {"fedora_messaging_schema": "base.message", '
'{"headers": {"fedora_messaging_schema": "base.message", '
'"fedora_messaging_severity": 30}, "id": "test id", '
'"body": {"test_key": "test_value"}, "queue": "test queue"}]'
'"body": {"test_key": "test_value"}, "queue": "test queue"}'
)
with mock.patch("fedora_messaging.message._log") as mock_log:
self.assertRaises(KeyError, message.loads, message_json)
Expand All @@ -213,9 +209,9 @@ def test_missing_topic(self):
def test_missing_severity(self):
"""Assert proper exception is raised when topic severity missing."""
message_json = (
'[{"topic": "test topic", "headers": {"fedora_messaging_schema": '
'{"topic": "test topic", "headers": {"fedora_messaging_schema": '
'"base.message"}, "id": "test id", "body": {"test_key": "test_value"},'
'"queue": "test queue"}]'
'"queue": "test queue"}'
)
with mock.patch("fedora_messaging.message._log") as mock_log:
self.assertRaises(KeyError, message.loads, message_json)
Expand All @@ -224,9 +220,9 @@ def test_missing_severity(self):
def test_validation_failure(self):
"""Assert proper exception is raised when message validation failed."""
message_json = (
'[{"topic": "test topic", "headers": {"fedora_messaging_schema": "base.message", '
'{"topic": "test topic", "headers": {"fedora_messaging_schema": "base.message", '
'"fedora_messaging_severity": 41}, "id": "test id", '
'"body": {"test_key": "test_value"}, "queue": "test queue"}]'
'"body": {"test_key": "test_value"}, "queue": "test queue"}'
)
with mock.patch("fedora_messaging.message._log") as mock_log:
self.assertRaises(exceptions.ValidationError, message.loads, message_json)
Expand Down

0 comments on commit a783742

Please sign in to comment.