test_json_message_converter.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 import unittest
00003 import rospy
00004 import rostest
00005 from rospy_message_converter import json_message_converter
00006 
00007 class TestJsonMessageConverter(unittest.TestCase):
00008 
00009     def test_ros_message_with_string(self):
00010         from std_msgs.msg import String
00011         expected_json = '{"data": "Hello"}'
00012         message = String(data = 'Hello')
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         rospy.init_node('time_node')
00019         now_time = rospy.Time.now()
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         returned_json = json_message_converter.convert_ros_message_to_json(message)
00024         self.assertEqual(returned_json, expected_json)
00025 
00026     def test_ros_message_with_uint8_array(self):
00027         from rospy_message_converter.msg import Uint8ArrayTestMessage
00028         input_data = "".join([chr(i) for i in [97, 98, 99, 100]])
00029         expected_json = '{"data": "YWJjZA=="}'  # base64encode("abcd") is YWJjZA==
00030         message = Uint8ArrayTestMessage(data=input_data)
00031         returned_json = json_message_converter.convert_ros_message_to_json(message)
00032         self.assertEqual(returned_json, expected_json)
00033 
00034     def test_ros_message_with_uint8_array(self):
00035         from rospy_message_converter.msg import Uint8Array3TestMessage
00036         input_data = "".join([chr(i) for i in [97, 98, 99, 100]])
00037         expected_json = '{"data": "YWJjZA=="}'  # base64encode("abcd") YWJjZA==
00038         message = Uint8Array3TestMessage(data=input_data)
00039         returned_json = json_message_converter.convert_ros_message_to_json(message)
00040         self.assertEqual(returned_json, expected_json)
00041 
00042     def test_json_with_string(self):
00043         from std_msgs.msg import String
00044         expected_message = String(data = 'Hello')
00045         json_str = '{"data": "Hello"}'
00046         message = json_message_converter.convert_json_to_ros_message('std_msgs/String', json_str)
00047         self.assertEqual(message, expected_message)
00048 
00049     def test_json_with_header(self):
00050         from std_msgs.msg import Header
00051         rospy.init_node('time_node')
00052         now_time = rospy.Time.now()
00053         expected_message = Header(
00054             stamp = now_time,
00055             frame_id = 'my_frame',
00056             seq = 12
00057         )
00058         json_str = '{{"stamp": {{"secs": {0}, "nsecs": {1}}}, "frame_id": "my_frame", "seq": 12}}'\
00059             .format(now_time.secs, now_time.nsecs)
00060         message = json_message_converter.convert_json_to_ros_message('std_msgs/Header', json_str)
00061         self.assertEqual(message, expected_message)
00062 
00063     def test_json_with_invalid_message_fields(self):
00064         self.assertRaises(ValueError,
00065                           json_message_converter.convert_json_to_ros_message,
00066                           'std_msgs/String',
00067                           '{"not_data": "Hello"}')
00068 
00069 
00070 PKG = 'rospy_message_converter'
00071 NAME = 'test_json_message_converter'
00072 if __name__ == '__main__':
00073     rostest.unitrun(PKG, NAME, TestJsonMessageConverter)


rospy_message_converter
Author(s): Brandon Alexander
autogenerated on Sat May 6 2017 02:31:54