string_plugin_router_test.cpp
Go to the documentation of this file.
1 #include "../../src/plugin_router/string_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 MockStringSocket : 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  MockStringSocket() = 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 = "first message";
40  std::vector<uint8_t> first_data(first_message.begin(), first_message.end());
41  (*callback_)(first_data);
42  }
43 
44  virtual void SendData(std::vector<uint8_t> data) override {
45  std::string message(data.begin(), data.end());
46 
47  counter += 1;
48  switch (counter) {
49  case 1:
50  ASSERT_STREQ(message.c_str(), "first message");
51  break;
52  case 2:
53  ASSERT_STREQ(message.c_str(), "second message");
54  break;
55  case 3:
56  ASSERT_STREQ(message.c_str(), "third message");
57  break;
58  }
59  }
60 };
61 
62 // Mockを入れるもの
63 Component<SocketFactory> getMockStringUdpSourceComponent() {
64  return createComponent().bind<Socket, MockStringSocket>();
65 }
66 
67 Component<Annotated<StringAnnotation, PluginRouterFactory>>
69  return createComponent()
70  .replace(getUdpSocketComponent)
73 }
74 
75 // XmlRpcValueが不正なケース
76 TEST(TestSuite, string_plugin_try_start_with_invalid_xml) {
77  std::shared_ptr<rapidjson::Document> config(new rapidjson::Document);
78  config->Parse("{}");
79 
80  // objectを作成し、受信スレッドを開始
81  Injector<Annotated<StringAnnotation, PluginRouterFactory>> injector(
83  PluginRouterFactory pluginRouterFactory =
84  injector.get<fruit::Annotated<StringAnnotation, PluginRouterFactory>>();
85  // データは送信しないのでportは何でも良い
86  auto source = pluginRouterFactory(config, udp::endpoint(udp::v4(), 0));
87  auto result = source->TryStart();
88  // TryStartに失敗して、config間違いのエラーメッセージを受け取る
89  ASSERT_FALSE(result.is_success);
90  ASSERT_STREQ(result.error_message, "invalid config parameters");
91 }
92 
93 // pluginが見つからないケース
94 TEST(TestSuite, string_plugin_try_start_not_found_plugin) {
95  std::shared_ptr<rapidjson::Document> config(new rapidjson::Document);
96  config->Parse(
97  "[{\"plugin_name\":\"string_loopback::StringLoopback\",\"param\":"
98  "\"Parameter\"},{"
99  "\"plugin_name\":\"not_found_plugin::NotFoundPlugin\"}]");
100  ROS_INFO("This test produces a ROS_ERROR message because of a loading error in the plugin, but the behavior is as expected.");
101 
102  // objectを作成し、受信スレッドを開始
103  Injector<Annotated<StringAnnotation, PluginRouterFactory>> injector(
105  PluginRouterFactory pluginRouterFactory =
106  injector.get<fruit::Annotated<StringAnnotation, PluginRouterFactory>>();
107  // データは送信しないのでportは何でも良い
108  auto source = pluginRouterFactory(config, udp::endpoint(udp::v4(), 0));
109  auto result = source->TryStart();
110  // TryStartに失敗して、pluginがない旨のメッセージを受け取る
111  ASSERT_FALSE(result.is_success);
112  std::string error_message = result.error_message;
113  ASSERT_EQ(error_message.rfind("Failed to load"), 0);
114 }
115 
116 // Loopback Pluginを使うケース
117 TEST(TestSuite, string_plugin_try_start_with_loopback_plugin) {
118  std::shared_ptr<rapidjson::Document> config(new rapidjson::Document);
119  config->Parse(
120  "[{\"plugin_name\":\"string_loopback::StringLoopback\",\"param\":"
121  "\"Parameter\"}]");
122 
123  // objectを作成し、受信スレッドを開始
124  Injector<Annotated<StringAnnotation, PluginRouterFactory>> injector(
126  PluginRouterFactory pluginRouterFactory =
127  injector.get<fruit::Annotated<StringAnnotation, PluginRouterFactory>>();
128  // データは送信しないのでportは何でも良い
129  auto source = pluginRouterFactory(config, udp::endpoint(udp::v4(), 0));
130  auto result = source->TryStart();
131  // TryStartに成功する
132  // データの送信と評価はMockStringSocket内でやっている
133  ASSERT_TRUE(result.is_success);
134 }
MockStringSocket
Definition: string_plugin_router_test.cpp:24
getUdpSocketComponent
Component< SocketFactory > getUdpSocketComponent()
Definition: udp_socket.cpp:87
Socket
Definition: socket.h:8
MockStringSocket::MockStringSocket
MockStringSocket()=delete
MockStringSocket::counter
int counter
Definition: string_plugin_router_test.cpp:27
MockStringSocket::Start
virtual void Start() override
Definition: string_plugin_router_test.cpp:38
getStringPluginRouterComponent
Component< fruit::Annotated< StringAnnotation, PluginRouterFactory > > getStringPluginRouterComponent()
Definition: string_plugin_router.cpp:89
MockStringSocket::SendData
virtual void SendData(std::vector< uint8_t > data) override
Definition: string_plugin_router_test.cpp:44
TEST
TEST(TestSuite, string_plugin_try_start_with_invalid_xml)
Definition: string_plugin_router_test.cpp:76
PluginRouterFactory
std::function< std::unique_ptr< PluginRouter >(std::shared_ptr< rapidjson::Document >, udp::endpoint)> PluginRouterFactory
Definition: plugin_router.h:29
getMockStringPluginRouterComponent
Component< Annotated< StringAnnotation, PluginRouterFactory > > getMockStringPluginRouterComponent()
Definition: string_plugin_router_test.cpp:68
MockStringSocket::callback_
std::shared_ptr< std::function< void(std::vector< uint8_t >)> > callback_
Definition: string_plugin_router_test.cpp:26
getMockStringUdpSourceComponent
Component< SocketFactory > getMockStringUdpSourceComponent()
Definition: string_plugin_router_test.cpp:63
ROS_INFO
#define ROS_INFO(...)
MockStringSocket::INJECT
INJECT(MockStringSocket(ASSISTED(udp::endpoint) target_socket, callback))
Definition: string_plugin_router_test.cpp:32


skyway
Author(s): Toshiya Nakakura
autogenerated on Thu Oct 26 2023 02:42:21