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_json1 = '{{"stamp": {{"secs": {0}, "nsecs": {1}}}, "frame_id": "my_frame", "seq": 3}}'\
21  .format(now_time.secs, now_time.nsecs)
22  expected_json2 = '{{"seq": 3, "stamp": {{"secs": {0}, "nsecs": {1}}}, "frame_id": "my_frame"}}'\
23  .format(now_time.secs, now_time.nsecs)
24  expected_json3 = '{{"frame_id": "my_frame", "seq": 3, "stamp": {{"secs": {0}, "nsecs": {1}}}}}'\
25  .format(now_time.secs, now_time.nsecs)
26  message = Header(stamp = now_time, frame_id = 'my_frame', seq = 3)
27  message = serialize_deserialize(message)
28  returned_json = json_message_converter.convert_ros_message_to_json(message)
29  self.assertTrue(returned_json == expected_json1 or
30  returned_json == expected_json2 or
31  returned_json == expected_json3)
32 
34  from rospy_message_converter.msg import Uint8ArrayTestMessage
35  input_data = [97, 98, 99, 100]
36  expected_json = '{"data": "YWJjZA=="}' # base64.standard_b64encode("abcd") is "YWJjZA=="
37  message = Uint8ArrayTestMessage(data=input_data)
38  message = serialize_deserialize(message)
39  returned_json = json_message_converter.convert_ros_message_to_json(message)
40  self.assertEqual(returned_json, expected_json)
41 
43  from rospy_message_converter.msg import Uint8Array3TestMessage
44  input_data = [97, 98, 99]
45  expected_json = '{"data": "YWJj"}' # base64.standard_b64encode("abc") is "YWJj"
46  message = Uint8Array3TestMessage(data=input_data)
47  message = serialize_deserialize(message)
48  returned_json = json_message_converter.convert_ros_message_to_json(message)
49  self.assertEqual(returned_json, expected_json)
50 
52  from std_msgs.msg import String
53  expected_message = String(data = 'Hello')
54  json_str = '{"data": "Hello"}'
55  message = json_message_converter.convert_json_to_ros_message('std_msgs/String', json_str)
56  expected_message = serialize_deserialize(expected_message)
57  self.assertEqual(message, expected_message)
58 
60  from std_msgs.msg import Header
61  from time import time
62  now_time = rospy.Time(time())
63  expected_message = Header(
64  stamp = now_time,
65  frame_id = 'my_frame',
66  seq = 12
67  )
68  json_str = '{{"stamp": {{"secs": {0}, "nsecs": {1}}}, "frame_id": "my_frame", "seq": 12}}'\
69  .format(now_time.secs, now_time.nsecs)
70  message = json_message_converter.convert_json_to_ros_message('std_msgs/Header', json_str)
71  expected_message = serialize_deserialize(expected_message)
72  self.assertEqual(message, expected_message)
73 
75  self.assertRaises(ValueError,
76  json_message_converter.convert_json_to_ros_message,
77  'std_msgs/String',
78  '{"not_data": "Hello"}')
79 
80 
81 def serialize_deserialize(message):
82  """
83  Serialize and then deserialize a message. This simulates sending a message
84  between ROS nodes and makes sure that the ROS messages being tested are
85  actually serializable, and are in the same format as they would be received
86  over the network. In rospy, it is possible to assign an illegal data type
87  to a message field (for example, `message = String(data=42)`), but trying
88  to publish this message will throw `SerializationError: field data must be
89  of type str`. This method will expose such bugs.
90  """
91  from io import BytesIO
92  buff = BytesIO()
93  message.serialize(buff)
94  result = message.__class__() # create new instance of same class as message
95  result.deserialize(buff.getvalue())
96  return result
97 
98 
99 PKG = 'rospy_message_converter'
100 NAME = 'test_json_message_converter'
101 if __name__ == '__main__':
102  import rosunit
103  rosunit.unitrun(PKG, NAME, TestJsonMessageConverter)


rospy_message_converter
Author(s): Brandon Alexander
autogenerated on Tue Jun 2 2020 03:05:31