From a7837420cfe1db73227987d3edadeb2b9a18e130 Mon Sep 17 00:00:00 2001 From: Sebastian Wojciechowski Date: Sat, 11 May 2019 19:20:13 +0200 Subject: [PATCH] Add ability to deserialize single message with message.loads Signed-off-by: Sebastian Wojciechowski --- fedora_messaging/message.py | 109 ++++++++++---------- fedora_messaging/tests/unit/test_message.py | 46 ++++----- 2 files changed, 74 insertions(+), 81 deletions(-) diff --git a/fedora_messaging/message.py b/fedora_messaging/message.py index 238606b6..98dd12c8 100644 --- a/fedora_messaging/message.py +++ b/fedora_messaging/message.py @@ -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): diff --git a/fedora_messaging/tests/unit/test_message.py b/fedora_messaging/tests/unit/test_message.py index 7e3a4e76..6f9754f2 100644 --- a/fedora_messaging/tests/unit/test_message.py +++ b/fedora_messaging/tests/unit/test_message.py @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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)