test_json_message_converter.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 #
3 # Software License Agreement (BSD License)
4 #
5 # Copyright (c) 2019-2022, Martin Günther (DFKI GmbH) and others
6 # Copyright (c) 2013-2016, Brandon Alexander
7 #
8 # All rights reserved.
9 #
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions
12 # are met:
13 #
14 # * Redistributions of source code must retain the above copyright
15 # notice, this list of conditions and the following disclaimer.
16 # * Redistributions in binary form must reproduce the above
17 # copyright notice, this list of conditions and the following
18 # disclaimer in the documentation and/or other materials provided
19 # with the distribution.
20 # * Neither the name of this project nor the names of its
21 # contributors may be used to endorse or promote products derived
22 # from this software without specific prior written permission.
23 #
24 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 # POSSIBILITY OF SUCH DAMAGE.
36 
37 import unittest
38 import rospy
39 from rospy_message_converter import json_message_converter
40 
41 
42 class TestJsonMessageConverter(unittest.TestCase):
44  from std_msgs.msg import String
45 
46  expected_json = '{"data": "Hello"}'
47  message = String(data='Hello')
48  message = serialize_deserialize(message)
49  returned_json = json_message_converter.convert_ros_message_to_json(message)
50  self.assertEqual(returned_json, expected_json)
51 
53  from std_msgs.msg import String
54 
55  expected_json = '{"data": "Hello \\u00dcnicode"}'
56  message = String(data=u'Hello \u00dcnicode')
57  message = serialize_deserialize(message)
58  returned_json = json_message_converter.convert_ros_message_to_json(message)
59  self.assertEqual(returned_json, expected_json)
60 
62  from std_msgs.msg import Header
63  from time import time
64 
65  now_time = rospy.Time(time())
66  expected_json1 = '{{"stamp": {{"secs": {0}, "nsecs": {1}}}, "frame_id": "my_frame", "seq": 3}}'.format(
67  now_time.secs, now_time.nsecs
68  )
69  expected_json2 = '{{"seq": 3, "stamp": {{"secs": {0}, "nsecs": {1}}}, "frame_id": "my_frame"}}'.format(
70  now_time.secs, now_time.nsecs
71  )
72  expected_json3 = '{{"frame_id": "my_frame", "seq": 3, "stamp": {{"secs": {0}, "nsecs": {1}}}}}'.format(
73  now_time.secs, now_time.nsecs
74  )
75  message = Header(stamp=now_time, frame_id='my_frame', seq=3)
76  message = serialize_deserialize(message)
77  returned_json = json_message_converter.convert_ros_message_to_json(message)
78  self.assertTrue(
79  returned_json == expected_json1 or returned_json == expected_json2 or returned_json == expected_json3
80  )
81 
83  from rospy_message_converter.msg import Uint8ArrayTestMessage
84 
85  input_data = [97, 98, 99, 100]
86  expected_json = '{"data": "YWJjZA=="}' # base64.b64encode("abcd") is "YWJjZA=="
87  message = Uint8ArrayTestMessage(data=input_data)
88  message = serialize_deserialize(message)
89  returned_json = json_message_converter.convert_ros_message_to_json(message)
90  self.assertEqual(returned_json, expected_json)
91 
93  from rospy_message_converter.msg import Uint8Array3TestMessage
94 
95  input_data = [97, 98, 99]
96  expected_json = '{"data": "YWJj"}' # base64.b64encode("abc") is "YWJj"
97  message = Uint8Array3TestMessage(data=input_data)
98  message = serialize_deserialize(message)
99  returned_json = json_message_converter.convert_ros_message_to_json(message)
100  self.assertEqual(returned_json, expected_json)
101 
103  from std_msgs.msg import String
104 
105  expected_message = String(data='Hello')
106  json_str = '{"data": "Hello"}'
107  message = json_message_converter.convert_json_to_ros_message('std_msgs/String', json_str)
108  expected_message = serialize_deserialize(expected_message)
109  self.assertEqual(message, expected_message)
110 
112  from std_msgs.msg import String
113 
114  expected_message = String(data=u'Hello \u00dcnicode')
115  json_str = '{"data": "Hello \\u00dcnicode"}'
116  message = json_message_converter.convert_json_to_ros_message('std_msgs/String', json_str)
117  expected_message = serialize_deserialize(expected_message)
118  self.assertEqual(message, expected_message)
119 
121  from std_msgs.msg import Header
122  from time import time
123 
124  now_time = rospy.Time(time())
125  expected_message = Header(stamp=now_time, frame_id='my_frame', seq=12)
126  json_str = '{{"stamp": {{"secs": {0}, "nsecs": {1}}}, "frame_id": "my_frame", "seq": 12}}'.format(
127  now_time.secs, now_time.nsecs
128  )
129  message = json_message_converter.convert_json_to_ros_message('std_msgs/Header', json_str)
130  expected_message = serialize_deserialize(expected_message)
131  self.assertEqual(message, expected_message)
132 
134  from std_msgs.msg import String
135 
136  expected_message = String(data='')
137  json_str = '{"data": null}'
138  message = json_message_converter.convert_json_to_ros_message('std_msgs/String', json_str)
139  expected_message = serialize_deserialize(expected_message)
140  self.assertEqual(message, expected_message)
141 
143  self.assertRaises(
144  ValueError, json_message_converter.convert_json_to_ros_message, 'std_msgs/String', '{"not_data": "Hello"}'
145  )
146 
147 
149  """
150  Serialize and then deserialize a message. This simulates sending a message
151  between ROS nodes and makes sure that the ROS messages being tested are
152  actually serializable, and are in the same format as they would be received
153  over the network. In rospy, it is possible to assign an illegal data type
154  to a message field (for example, `message = String(data=42)`), but trying
155  to publish this message will throw `SerializationError: field data must be
156  of type str`. This method will expose such bugs.
157  """
158  from io import BytesIO
159 
160  buff = BytesIO()
161  message.serialize(buff)
162  result = message.__class__() # create new instance of same class as message
163  result.deserialize(buff.getvalue())
164  return result
165 
166 
167 PKG = 'rospy_message_converter'
168 NAME = 'test_json_message_converter'
169 if __name__ == '__main__':
170  import rosunit
171 
172  rosunit.unitrun(PKG, NAME, TestJsonMessageConverter)


rospy_message_converter
Author(s): Brandon Alexander
autogenerated on Thu Dec 22 2022 03:33:21