json_plugin_router_test.cpp
Go to the documentation of this file.
1 #include "../../src/plugin_router/json_plugin_router.h"
2 
3 #include <fruit/fruit.h>
4 #include <gtest/gtest.h>
5 
6 #include <algorithm>
7 #include <boost/array.hpp>
8 #include <boost/asio.hpp>
9 #include <functional>
10 #include <iostream>
11 #include <iterator>
12 #include <memory>
13 #include <unordered_map>
14 
15 #include "../../src/socket/udp_socket.h"
16 #include "std_msgs/UInt8MultiArray.h"
17 
18 using boost::asio::ip::udp;
19 using fruit::Annotated;
20 using fruit::Component;
21 using fruit::createComponent;
22 using fruit::Injector;
23 
24 class MockJsonSocket : public Socket {
25  private:
26  std::shared_ptr<std::function<void(std::vector<uint8_t>)>> callback_;
27  int counter = 0;
28 
29  public:
30  // デフォルトコンストラクタは削除
31  MockJsonSocket() = delete;
33  ASSISTED(udp::endpoint) target_socket,
34  ASSISTED(std::shared_ptr<std::function<void(std::vector<uint8_t>)>>)
35  callback))
36  : callback_(callback) {}
37 
38  virtual void Start() override {
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);
43 
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);
49  }
50 
51  virtual void SendData(std::vector<uint8_t> data) override {
52  counter += 1;
53  std::string message(data.begin(), data.end());
54  if (counter == 1) {
55  ASSERT_STREQ(message.c_str(),
56  "{\"key\":\"value\",\"bool\":true,\"num\":10}");
57  } else {
58  ASSERT_STREQ(message.c_str(),
59  "{\"key\":\"value\",\"bool\":false,\"num\":0}");
60  }
61  }
62 };
63 
64 // Mockを入れるもの
65 Component<SocketFactory> getMockJsonUdpSourceComponent() {
66  return createComponent().bind<Socket, MockJsonSocket>();
67 }
68 
69 Component<Annotated<JsonAnnotation, PluginRouterFactory>>
71  return createComponent()
72  .replace(getUdpSocketComponent)
75 }
76 
77 // XmlRpcValueが不正なケース
78 TEST(TestSuite, json_plugin_try_start_with_invalid_xml) {
79  std::shared_ptr<rapidjson::Document> config(new rapidjson::Document);
80  config->Parse("{}");
81 
82  // objectを作成し、受信スレッドを開始
83  Injector<Annotated<JsonAnnotation, PluginRouterFactory>> injector(
85  PluginRouterFactory pluginRouterFactory =
86  injector.get<Annotated<JsonAnnotation, PluginRouterFactory>>();
87 
88  // データは送信しないのでportは何でも良い
89  auto source = pluginRouterFactory(config, udp::endpoint(udp::v4(), 0));
90  auto result = source->TryStart();
91  // TryStartに失敗して、config間違いのエラーメッセージを受け取る
92  ASSERT_FALSE(result.is_success);
93  ASSERT_STREQ(result.error_message, "invalid config parameters");
94 }
95 
96 // pluginが見つからないケース
97 TEST(TestSuite, json_plugin_try_start_not_found_plugin) {
98  std::shared_ptr<rapidjson::Document> config(new rapidjson::Document);
99  config->Parse(
100  "[{\"plugin_name\":\"json_loopback::JsonLoopback\",\"param\":"
101  "\"Parameter\"},{"
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.");
104 
105  // objectを作成し、受信スレッドを開始
106  Injector<Annotated<JsonAnnotation, PluginRouterFactory>> injector(
108  PluginRouterFactory pluginRouterFactory =
109  injector.get<Annotated<JsonAnnotation, PluginRouterFactory>>();
110 
111  // データは送信しないのでportは何でも良い
112  auto source = pluginRouterFactory(config, udp::endpoint(udp::v4(), 0));
113  auto result = source->TryStart();
114  // TryStartに失敗して、pluginがない旨のメッセージを受け取る
115  ASSERT_FALSE(result.is_success);
116  std::string error_message = result.error_message;
117  ASSERT_EQ(error_message.rfind("Failed to load"), 0);
118 }
119 
120 // Loopback Pluginを使うケース
121 TEST(TestSuite, json_plugin_try_start_with_loopback_plugin) {
122  std::shared_ptr<rapidjson::Document> config(new rapidjson::Document);
123  config->Parse(
124  "[{\"plugin_name\":\"json_loopback::JsonLoopback\",\"param\":"
125  "\"Parameter\"}]");
126 
127  // objectを作成し、受信スレッドを開始
128  Injector<Annotated<JsonAnnotation, PluginRouterFactory>> injector(
130  PluginRouterFactory pluginRouterFactory =
131  injector.get<Annotated<JsonAnnotation, PluginRouterFactory>>();
132 
133  // データは送信しないのでportは何でも良い
134  auto source = pluginRouterFactory(config, udp::endpoint(udp::v4(), 0));
135  auto result = source->TryStart();
136  // TryStartに成功する
137  // データの送信と評価はMockJsonSocket内でやっている
138  ASSERT_TRUE(result.is_success);
139 }
Definition: socket.h:8
Component< fruit::Annotated< JsonAnnotation, PluginRouterFactory > > getJsonPluginRouterComponent()
virtual void Start() override
Component< Annotated< JsonAnnotation, PluginRouterFactory > > getMockJsonPluginRouterComponent()
virtual void SendData(std::vector< uint8_t > data) override
TEST(TestSuite, json_plugin_try_start_with_invalid_xml)
INJECT(MockJsonSocket(ASSISTED(udp::endpoint) target_socket, callback))
Component< SocketFactory > getMockJsonUdpSourceComponent()
#define ROS_INFO(...)
Component< SocketFactory > getUdpSocketComponent()
Definition: udp_socket.cpp:87
std::shared_ptr< std::function< void(std::vector< uint8_t >)> > callback_
std::function< std::unique_ptr< PluginRouter >(std::shared_ptr< rapidjson::Document >, udp::endpoint)> PluginRouterFactory
Definition: plugin_router.h:29
MockJsonSocket()=delete


skyway
Author(s): Toshiya Nakakura
autogenerated on Sat Apr 15 2023 02:08:21