1 #include "../../src/plugin_router/json_plugin_router.h"
3 #include <fruit/fruit.h>
4 #include <gtest/gtest.h>
7 #include <boost/array.hpp>
8 #include <boost/asio.hpp>
13 #include <unordered_map>
15 #include "../../src/socket/udp_socket.h"
16 #include "std_msgs/UInt8MultiArray.h"
18 using boost::asio::ip::udp;
19 using fruit::Annotated;
20 using fruit::Component;
21 using fruit::createComponent;
22 using fruit::Injector;
26 std::shared_ptr<std::function<void(std::vector<uint8_t>)>>
callback_;
33 ASSISTED(udp::endpoint) target_socket,
34 ASSISTED(std::shared_ptr<std::function<
void(std::vector<uint8_t>)>>)
39 std::string first_message =
40 "{\"key\": \"value\", \"bool\": true, \"num\": 10}";
41 std::vector<uint8_t> first_data(first_message.begin(), first_message.end());
42 (*callback_)(first_data);
44 std::string second_message =
45 "{\"key\": \"value\", \"bool\": false, \"num\": 0}";
46 std::vector<uint8_t> second_data(second_message.begin(),
47 second_message.end());
48 (*callback_)(second_data);
51 virtual void SendData(std::vector<uint8_t> data)
override {
53 std::string message(data.begin(), data.end());
55 ASSERT_STREQ(message.c_str(),
56 "{\"key\":\"value\",\"bool\":true,\"num\":10}");
58 ASSERT_STREQ(message.c_str(),
59 "{\"key\":\"value\",\"bool\":false,\"num\":0}");
69 Component<Annotated<JsonAnnotation, PluginRouterFactory>>
71 return createComponent()
78 TEST(TestSuite, json_plugin_try_start_with_invalid_xml) {
79 std::shared_ptr<rapidjson::Document> config(
new rapidjson::Document);
83 Injector<Annotated<JsonAnnotation, PluginRouterFactory>> injector(
86 injector.get<Annotated<JsonAnnotation, PluginRouterFactory>>();
89 auto source = pluginRouterFactory(config, udp::endpoint(udp::v4(), 0));
90 auto result = source->TryStart();
92 ASSERT_FALSE(result.is_success);
93 ASSERT_STREQ(result.error_message,
"invalid config parameters");
97 TEST(TestSuite, json_plugin_try_start_not_found_plugin) {
98 std::shared_ptr<rapidjson::Document> config(
new rapidjson::Document);
100 "[{\"plugin_name\":\"json_loopback::JsonLoopback\",\"param\":"
102 "\"plugin_name\":\"not_found_plugin::NotFoundPlugin\"}]");
103 ROS_INFO(
"This test produces a ROS_ERROR message because of a loading error in the plugin, but the behavior is as expected.");
106 Injector<Annotated<JsonAnnotation, PluginRouterFactory>> injector(
109 injector.get<Annotated<JsonAnnotation, PluginRouterFactory>>();
112 auto source = pluginRouterFactory(config, udp::endpoint(udp::v4(), 0));
113 auto result = source->TryStart();
115 ASSERT_FALSE(result.is_success);
116 std::string error_message = result.error_message;
117 ASSERT_EQ(error_message.rfind(
"Failed to load"), 0);
121 TEST(TestSuite, json_plugin_try_start_with_loopback_plugin) {
122 std::shared_ptr<rapidjson::Document> config(
new rapidjson::Document);
124 "[{\"plugin_name\":\"json_loopback::JsonLoopback\",\"param\":"
128 Injector<Annotated<JsonAnnotation, PluginRouterFactory>> injector(
131 injector.get<Annotated<JsonAnnotation, PluginRouterFactory>>();
134 auto source = pluginRouterFactory(config, udp::endpoint(udp::v4(), 0));
135 auto result = source->TryStart();
138 ASSERT_TRUE(result.is_success);