test_json_message_converter.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 import unittest
3 import rospy
4 from rospy_message_converter import json_message_converter
5 
6 class TestJsonMessageConverter(unittest.TestCase):
7 
9  from std_msgs.msg import String
10  expected_json = '{"data": "Hello"}'
11  message = String(data = 'Hello')
12  message = serialize_deserialize(message)
13  returned_json = json_message_converter.convert_ros_message_to_json(message)
14  self.assertEqual(returned_json, expected_json)
15 
17  from std_msgs.msg import Header
18  from time import time
19  now_time = rospy.Time(time())
20  expected_json = '{{"stamp": {{"secs": {0}, "nsecs": {1}}}, "frame_id": "my_frame", "seq": 3}}'\
21  .format(now_time.secs, now_time.nsecs)
22  message = Header(stamp = now_time, frame_id = 'my_frame', seq = 3)
23  message = serialize_deserialize(message)
24  returned_json = json_message_converter.convert_ros_message_to_json(message)
25  self.assertEqual(returned_json, expected_json)
26 
28  from rospy_message_converter.msg import Uint8ArrayTestMessage
29  input_data = "".join([chr(i) for i in [97, 98, 99, 100]])
30  expected_json = '{"data": "YWJjZA=="}' # base64.standard_b64encode("abcd") is "YWJjZA=="
31  message = Uint8ArrayTestMessage(data=input_data)
32  message = serialize_deserialize(message)
33  returned_json = json_message_converter.convert_ros_message_to_json(message)
34  self.assertEqual(returned_json, expected_json)
35 
37  from rospy_message_converter.msg import Uint8Array3TestMessage
38  input_data = "".join([chr(i) for i in [97, 98, 99]])
39  expected_json = '{"data": "YWJj"}' # base64.standard_b64encode("abc") is "YWJj"
40  message = Uint8Array3TestMessage(data=input_data)
41  message = serialize_deserialize(message)
42  returned_json = json_message_converter.convert_ros_message_to_json(message)
43  self.assertEqual(returned_json, expected_json)
44 
46  from std_msgs.msg import String
47  expected_message = String(data = 'Hello')
48  json_str = '{"data": "Hello"}'
49  message = json_message_converter.convert_json_to_ros_message('std_msgs/String', json_str)
50  expected_message = serialize_deserialize(expected_message)
51  self.assertEqual(message, expected_message)
52 
54  from std_msgs.msg import Header
55  from time import time
56  now_time = rospy.Time(time())
57  expected_message = Header(
58  stamp = now_time,
59  frame_id = 'my_frame',
60  seq = 12
61  )
62  json_str = '{{"stamp": {{"secs": {0}, "nsecs": {1}}}, "frame_id": "my_frame", "seq": 12}}'\
63  .format(now_time.secs, now_time.nsecs)
64  message = json_message_converter.convert_json_to_ros_message('std_msgs/Header', json_str)
65  expected_message = serialize_deserialize(expected_message)
66  self.assertEqual(message, expected_message)
67 
69  self.assertRaises(ValueError,
70  json_message_converter.convert_json_to_ros_message,
71  'std_msgs/String',
72  '{"not_data": "Hello"}')
73 
74 
75 def serialize_deserialize(message):
76  """
77  Serialize and then deserialize a message. This simulates sending a message
78  between ROS nodes and makes sure that the ROS messages being tested are
79  actually serializable, and are in the same format as they would be received
80  over the network. In rospy, it is possible to assign an illegal data type
81  to a message field (for example, `message = String(data=42)`), but trying
82  to publish this message will throw `SerializationError: field data must be
83  of type str`. This method will expose such bugs.
84  """
85  from StringIO import StringIO
86  buff = StringIO()
87  message.serialize(buff)
88  result = message.__class__() # create new instance of same class as message
89  result.deserialize(buff.getvalue())
90  return result
91 
92 
93 PKG = 'rospy_message_converter'
94 NAME = 'test_json_message_converter'
95 if __name__ == '__main__':
96  import rosunit
97  rosunit.unitrun(PKG, NAME, TestJsonMessageConverter)


rospy_message_converter
Author(s): Brandon Alexander
autogenerated on Thu Mar 5 2020 03:12:49