Go to the documentation of this file.00001
00002 import unittest
00003 import rospy
00004 from rospy_message_converter import json_message_converter
00005
00006 class TestJsonMessageConverter(unittest.TestCase):
00007
00008 def test_ros_message_with_string(self):
00009 from std_msgs.msg import String
00010 expected_json = '{"data": "Hello"}'
00011 message = String(data = 'Hello')
00012 message = serialize_deserialize(message)
00013 returned_json = json_message_converter.convert_ros_message_to_json(message)
00014 self.assertEqual(returned_json, expected_json)
00015
00016 def test_ros_message_with_header(self):
00017 from std_msgs.msg import Header
00018 from time import time
00019 now_time = rospy.Time(time())
00020 expected_json = '{{"stamp": {{"secs": {0}, "nsecs": {1}}}, "frame_id": "my_frame", "seq": 3}}'\
00021 .format(now_time.secs, now_time.nsecs)
00022 message = Header(stamp = now_time, frame_id = 'my_frame', seq = 3)
00023 message = serialize_deserialize(message)
00024 returned_json = json_message_converter.convert_ros_message_to_json(message)
00025 self.assertEqual(returned_json, expected_json)
00026
00027 def test_ros_message_with_uint8_array(self):
00028 from rospy_message_converter.msg import Uint8ArrayTestMessage
00029 input_data = "".join([chr(i) for i in [97, 98, 99, 100]])
00030 expected_json = '{"data": "YWJjZA=="}'
00031 message = Uint8ArrayTestMessage(data=input_data)
00032 message = serialize_deserialize(message)
00033 returned_json = json_message_converter.convert_ros_message_to_json(message)
00034 self.assertEqual(returned_json, expected_json)
00035
00036 def test_ros_message_with_3uint8_array(self):
00037 from rospy_message_converter.msg import Uint8Array3TestMessage
00038 input_data = "".join([chr(i) for i in [97, 98, 99]])
00039 expected_json = '{"data": "YWJj"}'
00040 message = Uint8Array3TestMessage(data=input_data)
00041 message = serialize_deserialize(message)
00042 returned_json = json_message_converter.convert_ros_message_to_json(message)
00043 self.assertEqual(returned_json, expected_json)
00044
00045 def test_json_with_string(self):
00046 from std_msgs.msg import String
00047 expected_message = String(data = 'Hello')
00048 json_str = '{"data": "Hello"}'
00049 message = json_message_converter.convert_json_to_ros_message('std_msgs/String', json_str)
00050 expected_message = serialize_deserialize(expected_message)
00051 self.assertEqual(message, expected_message)
00052
00053 def test_json_with_header(self):
00054 from std_msgs.msg import Header
00055 from time import time
00056 now_time = rospy.Time(time())
00057 expected_message = Header(
00058 stamp = now_time,
00059 frame_id = 'my_frame',
00060 seq = 12
00061 )
00062 json_str = '{{"stamp": {{"secs": {0}, "nsecs": {1}}}, "frame_id": "my_frame", "seq": 12}}'\
00063 .format(now_time.secs, now_time.nsecs)
00064 message = json_message_converter.convert_json_to_ros_message('std_msgs/Header', json_str)
00065 expected_message = serialize_deserialize(expected_message)
00066 self.assertEqual(message, expected_message)
00067
00068 def test_json_with_invalid_message_fields(self):
00069 self.assertRaises(ValueError,
00070 json_message_converter.convert_json_to_ros_message,
00071 'std_msgs/String',
00072 '{"not_data": "Hello"}')
00073
00074
00075 def serialize_deserialize(message):
00076 """
00077 Serialize and then deserialize a message. This simulates sending a message
00078 between ROS nodes and makes sure that the ROS messages being tested are
00079 actually serializable, and are in the same format as they would be received
00080 over the network. In rospy, it is possible to assign an illegal data type
00081 to a message field (for example, `message = String(data=42)`), but trying
00082 to publish this message will throw `SerializationError: field data must be
00083 of type str`. This method will expose such bugs.
00084 """
00085 from StringIO import StringIO
00086 buff = StringIO()
00087 message.serialize(buff)
00088 result = message.__class__()
00089 result.deserialize(buff.getvalue())
00090 return result
00091
00092
00093 PKG = 'rospy_message_converter'
00094 NAME = 'test_json_message_converter'
00095 if __name__ == '__main__':
00096 import rosunit
00097 rosunit.unitrun(PKG, NAME, TestJsonMessageConverter)