# Path: pyaiot/common/messaging.py
# class Message():
#     """Utility class for generating and parsing service messages."""
# 
#     @staticmethod
#     def serialize(message):
#         return json.dumps(message, ensure_ascii=False)
# 
#     @staticmethod
#     def new_node(uid, dst="all"):
#         """Generate a text message indicating a new node."""
#         return Message.serialize({'type': 'new', 'uid': uid, 'dst': dst})
# 
#     @staticmethod
#     def out_node(uid):
#         """Generate a text message indicating a node to remove."""
#         return Message.serialize({'type': 'out', 'uid': uid})
# 
#     @staticmethod
#     def reset_node(uid):
#         """Generate a text message indicating a node reset."""
#         return Message.serialize({'type': 'reset', 'uid': uid})
# 
#     @staticmethod
#     def update_node(uid, endpoint, data, dst="all"):
#         """Generate a text message indicating a node update."""
#         return Message.serialize({'type': 'update',
#                                   'uid': uid,
#                                   'endpoint': endpoint,
#                                   'data': data,
#                                   'dst': dst})
# 
#     @staticmethod
#     def gateway_alive():
#         """Generate a text message indicating that a gateway is alive."""
#         return Message.serialize({'type': 'update', 'uid': 'alive'})
# 
#     @staticmethod
#     def discover_node():
#         """Generate a text message for websocket node discovery."""
#         return Message.serialize({'request': 'discover'})
# 
#     @staticmethod
#     def check_message(raw):
#         """Verify a received message is correctly formatted."""
#         reason = None
#         try:
#             message = json.loads(raw)
#         except TypeError as exc:
#             logger.warning(exc)
#             reason = "Invalid message '{}'.".format(raw)
#             message = None
#         except json.JSONDecodeError:
#             reason = ("Invalid message received "
#                       "'{}'. Only JSON format is supported.".format(raw))
#             message = None
# 
#         if message is not None:
#             if not hasattr(message, '__iter__'):
#                 reason = "Invalid message '{}'.".format(message)
#             elif 'type' not in message and 'data' not in message:
#                 reason = "Invalid message '{}'.".format(message)
#             elif (message['type'] != 'new' and message['type'] != 'update' and
#                   message['type'] != 'out' and message['type'] != 'reset'):
#                 reason = "Invalid message type '{}'.".format(message['type'])
# 
#         if reason is not None:
#             logger.warning(reason)
#             message = None
# 
#         return message, reason


import json
from pytest import mark
from pyaiot.common.messaging import Message

    assert serialized == json.dumps(message, ensure_ascii=False)


def test_new_node():
    serialized = Message.new_node('1234')

    assert serialized == Message.serialize(
        {'type': 'new', 'uid': '1234', 'dst': 'all'})

    serialized = Message.new_node('1234', '5678')
    assert serialized == Message.serialize(
        {'type': 'new', 'uid': '1234', 'dst': '5678'})


def test_out_node():
    serialized = Message.out_node('1234')

    assert serialized == Message.serialize({'type': 'out', 'uid': '1234'})


def test_reset_node():
    serialized = Message.reset_node('1234')

    assert serialized == Message.serialize({'type': 'reset', 'uid': '1234'})


def test_discover_node():
    serialized = Message.discover_node()


# Path: pyaiot/common/messaging.py
# class Message():
#     """Utility class for generating and parsing service messages."""
# 
#     @staticmethod
#     def serialize(message):
#         return json.dumps(message, ensure_ascii=False)
# 
#     @staticmethod
#     def new_node(uid, dst="all"):
#         """Generate a text message indicating a new node."""
#         return Message.serialize({'type': 'new', 'uid': uid, 'dst': dst})
# 
#     @staticmethod
#     def out_node(uid):
#         """Generate a text message indicating a node to remove."""
#         return Message.serialize({'type': 'out', 'uid': uid})
# 
#     @staticmethod
#     def reset_node(uid):
#         """Generate a text message indicating a node reset."""
#         return Message.serialize({'type': 'reset', 'uid': uid})
# 
#     @staticmethod
#     def update_node(uid, endpoint, data, dst="all"):
#         """Generate a text message indicating a node update."""
#         return Message.serialize({'type': 'update',
#                                   'uid': uid,
#                                   'endpoint': endpoint,
#                                   'data': data,
#                                   'dst': dst})
# 
#     @staticmethod
#     def gateway_alive():
#         """Generate a text message indicating that a gateway is alive."""
#         return Message.serialize({'type': 'update', 'uid': 'alive'})
# 
#     @staticmethod
#     def discover_node():
#         """Generate a text message for websocket node discovery."""
#         return Message.serialize({'request': 'discover'})
# 
#     @staticmethod
#     def check_message(raw):
#         """Verify a received message is correctly formatted."""
#         reason = None
#         try:
#             message = json.loads(raw)
#         except TypeError as exc:
#             logger.warning(exc)
#             reason = "Invalid message '{}'.".format(raw)
#             message = None
#         except json.JSONDecodeError:
#             reason = ("Invalid message received "
#                       "'{}'. Only JSON format is supported.".format(raw))
#             message = None
# 
#         if message is not None:
#             if not hasattr(message, '__iter__'):
#                 reason = "Invalid message '{}'.".format(message)
#             elif 'type' not in message and 'data' not in message:
#                 reason = "Invalid message '{}'.".format(message)
#             elif (message['type'] != 'new' and message['type'] != 'update' and
#                   message['type'] != 'out' and message['type'] != 'reset'):
#                 reason = "Invalid message type '{}'.".format(message['type'])
# 
#         if reason is not None:
#             logger.warning(reason)
#             message = None
# 
#         return message, reason


# Path: pyaiot/tests/test_messaging.py
import json
from pytest import mark
from pyaiot.common.messaging import Message


    assert serialized == json.dumps(message, ensure_ascii=False)


def test_new_node():
    serialized = Message.new_node('1234')

    assert serialized == Message.serialize(
        {'type': 'new', 'uid': '1234', 'dst': 'all'})

    serialized = Message.new_node('1234', '5678')
    assert serialized == Message.serialize(
        {'type': 'new', 'uid': '1234', 'dst': '5678'})


def test_out_node():
    serialized = Message.out_node('1234')

    assert serialized == Message.serialize({'type': 'out', 'uid': '1234'})


def test_reset_node():
    serialized = Message.reset_node('1234')

    assert serialized == Message.serialize({'type': 'reset', 'uid': '1234'})


def test_discover_node():
    serialized = Message.discover_node()


    assert serialized == Message.serialize({'request': 'discover'})