-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathtest_gateway_node.cpp
More file actions
184 lines (151 loc) · 5.6 KB
/
Copy pathtest_gateway_node.cpp
File metadata and controls
184 lines (151 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2025 bburda
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <gtest/gtest.h>
#include <chrono>
#include <memory>
#include <thread>
#include <httplib.h> // NOLINT(build/include_order)
#include <nlohmann/json.hpp>
#include <rclcpp/rclcpp.hpp>
using namespace std::chrono_literals;
static constexpr char VERSION[] = "0.1.0";
// Simple GatewayNode class for testing
class GatewayNode : public rclcpp::Node {
public:
GatewayNode()
: Node("gateway_node"), http_server_(std::make_unique<httplib::Server>()),
node_name_(this->get_name()) {
this->declare_parameter<int>("port", 8080);
this->declare_parameter<std::string>("host", "0.0.0.0");
port_ = this->get_parameter("port").as_int();
host_ = this->get_parameter("host").as_string();
setup_endpoints();
server_thread_ =
std::thread([this]() { http_server_->listen(host_.c_str(), port_); });
// Wait for the server to be ready by polling
wait_for_server_ready();
}
~GatewayNode() {
http_server_->stop();
if (server_thread_.joinable()) {
server_thread_.join();
}
}
int get_port() const { return port_; }
std::string get_host() const { return host_; }
private:
void wait_for_server_ready() {
const auto start = std::chrono::steady_clock::now();
const auto timeout = std::chrono::seconds(2);
httplib::Client client(host_.c_str(), port_);
while (std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - start) < timeout) {
if (auto res = client.Get("/health")) {
if (res->status == 200) {
return;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
throw std::runtime_error("HTTP server failed to start within timeout");
}
void setup_endpoints() {
http_server_->Get(
"/health", [this](const httplib::Request &req, httplib::Response &res) {
(void)req;
nlohmann::json health_json = {{"status", "ok"},
{"node", node_name_},
{"timestamp", this->now().seconds()}};
res.set_content(health_json.dump(), "application/json");
res.status = 200;
});
http_server_->Get(
"/", [this](const httplib::Request &req, httplib::Response &res) {
(void)req;
nlohmann::json info_json = {
{"name", "ROS 2 Medkit Gateway"},
{"version", VERSION},
{"endpoints", nlohmann::json::array({
"/health",
"/version-info",
"/areas",
"/components",
"/areas/{area_id}/components",
"/components/{component_id}/data",
"/components/{component_id}/data/{topic_name}"
})},
{"capabilities", {
{"discovery", true},
{"data_access", true}
}}
};
res.set_content(info_json.dump(), "application/json");
res.status = 200;
});
}
std::unique_ptr<httplib::Server> http_server_;
std::thread server_thread_;
int port_;
std::string host_;
std::string node_name_;
};
class TestGatewayNode : public ::testing::Test {
protected:
static void SetUpTestSuite() { rclcpp::init(0, nullptr); }
static void TearDownTestSuite() { rclcpp::shutdown(); }
};
TEST_F(TestGatewayNode, test_health_endpoint) {
// @verifies REQ_INTEROP_001
auto node = std::make_shared<GatewayNode>();
// Create HTTP client
httplib::Client client("localhost", node->get_port());
// Call /health endpoint
auto res = client.Get("/health");
// Verify response
ASSERT_TRUE(res);
EXPECT_EQ(res->status, 200);
EXPECT_EQ(res->get_header_value("Content-Type"), "application/json");
// Parse and verify JSON
auto json_response = nlohmann::json::parse(res->body);
EXPECT_EQ(json_response["status"], "ok");
EXPECT_EQ(json_response["node"], "gateway_node");
EXPECT_TRUE(json_response.contains("timestamp"));
EXPECT_TRUE(json_response["timestamp"].is_number());
}
TEST_F(TestGatewayNode, test_root_endpoint) {
// @verifies REQ_INTEROP_001, REQ_INTEROP_010
auto node = std::make_shared<GatewayNode>();
// Create HTTP client
httplib::Client client("localhost", node->get_port());
// Call / endpoint
auto res = client.Get("/");
// Verify response
ASSERT_TRUE(res);
EXPECT_EQ(res->status, 200);
EXPECT_EQ(res->get_header_value("Content-Type"), "application/json");
// Parse and verify JSON
auto json_response = nlohmann::json::parse(res->body);
EXPECT_EQ(json_response["name"], "ROS 2 Medkit Gateway");
EXPECT_EQ(json_response["version"], "0.1.0");
EXPECT_TRUE(json_response.contains("endpoints"));
EXPECT_TRUE(json_response["endpoints"].is_array());
EXPECT_EQ(json_response["endpoints"].size(), 7);
EXPECT_TRUE(json_response.contains("capabilities"));
EXPECT_TRUE(json_response["capabilities"]["discovery"]);
EXPECT_TRUE(json_response["capabilities"]["data_access"]);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}