247 lines
9.7 KiB
C++
247 lines
9.7 KiB
C++
#include "network/HTTPDataSource.h"
|
|
#include <gtest/gtest.h>
|
|
#include <gmock/gmock.h>
|
|
#include "utils/Logger.h"
|
|
#include <set>
|
|
#include <memory>
|
|
#include <thread>
|
|
|
|
class HTTPDataSourceTest : public ::testing::Test {
|
|
protected:
|
|
void SetUp() override {}
|
|
void TearDown() override {}
|
|
};
|
|
|
|
TEST_F(HTTPDataSourceTest, BasicFunctionality) {
|
|
DataSourceConfig config;
|
|
config.position.host = "localhost";
|
|
config.position.port = 8081;
|
|
config.position.aircraft_path = "/openApi/getCurrentFlightPositions";
|
|
config.position.vehicle_path = "/openApi/getCurrentVehiclePositions";
|
|
config.position.auth.auth_required = true;
|
|
config.position.auth.auth_path = "/login";
|
|
config.position.auth.username = "dianxin";
|
|
config.position.auth.password = "dianxin@123";
|
|
config.position.timeout_ms = 5000;
|
|
config.position.read_timeout_ms = 2000;
|
|
config.position.refresh_interval_ms = 100;
|
|
|
|
HTTPDataSource source(config);
|
|
ASSERT_TRUE(source.connect()) << "连接失败";
|
|
|
|
std::vector<Aircraft> aircraft;
|
|
bool success = source.fetchPositionAircraftData(aircraft);
|
|
ASSERT_TRUE(success) << "获取航空器数据失败";
|
|
ASSERT_FALSE(aircraft.empty()) << "航空器数据为空";
|
|
|
|
std::vector<Vehicle> vehicles;
|
|
ASSERT_TRUE(source.fetchPositionVehicleData(vehicles)) << "获取车辆数据失败";
|
|
ASSERT_FALSE(vehicles.empty()) << "车辆数据为空";
|
|
}
|
|
|
|
TEST_F(HTTPDataSourceTest, AuthenticationFailure) {
|
|
DataSourceConfig config;
|
|
config.position.host = "localhost";
|
|
config.position.port = 8081;
|
|
config.position.aircraft_path = "/openApi/getCurrentFlightPositions";
|
|
config.position.vehicle_path = "/openApi/getCurrentVehiclePositions";
|
|
config.position.refresh_interval_ms = 1000;
|
|
config.position.timeout_ms = 1000;
|
|
config.position.read_timeout_ms = 500;
|
|
|
|
config.position.auth.auth_required = true;
|
|
config.position.auth.username = "wrong_user";
|
|
config.position.auth.password = "wrong_pass";
|
|
config.position.auth.auth_path = "/login";
|
|
|
|
HTTPDataSource source(config);
|
|
std::vector<Aircraft> aircraft;
|
|
EXPECT_FALSE(source.fetchPositionAircraftData(aircraft));
|
|
}
|
|
|
|
TEST_F(HTTPDataSourceTest, Reconnection) {
|
|
DataSourceConfig config;
|
|
config.position.host = "localhost";
|
|
config.position.port = 8081;
|
|
config.position.aircraft_path = "/openApi/getCurrentFlightPositions";
|
|
config.position.vehicle_path = "/openApi/getCurrentVehiclePositions";
|
|
config.position.refresh_interval_ms = 1000;
|
|
config.position.timeout_ms = 1000;
|
|
config.position.read_timeout_ms = 500;
|
|
|
|
config.position.auth.auth_required = true;
|
|
config.position.auth.username = "dianxin";
|
|
config.position.auth.password = "dianxin@123";
|
|
config.position.auth.auth_path = "/login";
|
|
|
|
HTTPDataSource source(config);
|
|
EXPECT_TRUE(source.connect());
|
|
|
|
std::vector<Aircraft> aircraft;
|
|
EXPECT_TRUE(source.fetchPositionAircraftData(aircraft));
|
|
|
|
source.disconnect();
|
|
EXPECT_TRUE(source.connect());
|
|
|
|
aircraft.clear();
|
|
EXPECT_TRUE(source.fetchPositionAircraftData(aircraft));
|
|
}
|
|
|
|
TEST_F(HTTPDataSourceTest, AuthenticationOnly) {
|
|
DataSourceConfig config;
|
|
config.position.host = "localhost";
|
|
config.position.port = 8081;
|
|
config.position.aircraft_path = "/openApi/getCurrentFlightPositions";
|
|
config.position.vehicle_path = "/openApi/getCurrentVehiclePositions";
|
|
config.position.refresh_interval_ms = 1000;
|
|
config.position.timeout_ms = 1000;
|
|
config.position.read_timeout_ms = 500;
|
|
|
|
config.position.auth.auth_required = true;
|
|
config.position.auth.username = "dianxin";
|
|
config.position.auth.password = "dianxin@123";
|
|
config.position.auth.auth_path = "/login";
|
|
|
|
HTTPDataSource source(config);
|
|
EXPECT_TRUE(source.connect());
|
|
|
|
std::vector<Aircraft> aircraft;
|
|
EXPECT_TRUE(source.fetchPositionAircraftData(aircraft));
|
|
}
|
|
|
|
TEST_F(HTTPDataSourceTest, TrafficLightSignals) {
|
|
DataSourceConfig config;
|
|
config.position.host = "localhost";
|
|
config.position.port = 8081;
|
|
config.traffic_light.host = "localhost";
|
|
config.traffic_light.port = 8081;
|
|
config.traffic_light.signal_path = "/api/VehicleCommandInfo";
|
|
config.vehicle.host = "localhost";
|
|
config.vehicle.port = 8081;
|
|
config.vehicle.command_path = "/api/VehicleCommandInfo";
|
|
|
|
HTTPDataSource source(config);
|
|
ASSERT_TRUE(source.connect());
|
|
|
|
// 先发送 GREEN 指令解除 RED 状态
|
|
VehicleCommand green_command;
|
|
green_command.vehicleId = "QN001";
|
|
green_command.type = CommandType::SIGNAL;
|
|
green_command.reason = CommandReason::TRAFFIC_LIGHT;
|
|
green_command.signalState = SignalState::GREEN;
|
|
green_command.intersectionId = "INTERSECTION_001";
|
|
green_command.timestamp = std::chrono::duration_cast<std::chrono::microseconds>(
|
|
std::chrono::system_clock::now().time_since_epoch()).count();
|
|
ASSERT_TRUE(source.sendUnmannedVehicleCommand(green_command.vehicleId, green_command)) << "发送绿灯指令失败";
|
|
|
|
// 等待一小段时间确保 GREEN 指令生效
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
|
// 再发送 RESUME 指令解除其他可能的状态
|
|
VehicleCommand resume_command;
|
|
resume_command.vehicleId = "QN001";
|
|
resume_command.type = CommandType::RESUME;
|
|
resume_command.reason = CommandReason::RESUME_TRAFFIC;
|
|
resume_command.timestamp = std::chrono::duration_cast<std::chrono::microseconds>(
|
|
std::chrono::system_clock::now().time_since_epoch()).count();
|
|
ASSERT_TRUE(source.sendUnmannedVehicleCommand(resume_command.vehicleId, resume_command)) << "发送恢复指令失败";
|
|
|
|
// 等待一小段时间确保 RESUME 指令生效
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
|
// 发送红绿灯信号指令
|
|
VehicleCommand command;
|
|
command.vehicleId = "QN001";
|
|
command.type = CommandType::SIGNAL;
|
|
command.reason = CommandReason::TRAFFIC_LIGHT;
|
|
command.signalState = SignalState::RED;
|
|
command.intersectionId = "INTERSECTION_001";
|
|
command.latitude = 36.36;
|
|
command.longitude = 120.08;
|
|
command.timestamp = std::chrono::duration_cast<std::chrono::microseconds>(
|
|
std::chrono::system_clock::now().time_since_epoch()).count();
|
|
|
|
ASSERT_TRUE(source.sendUnmannedVehicleCommand(command.vehicleId, command)) << "获取红绿灯信号失败";
|
|
}
|
|
|
|
TEST_F(HTTPDataSourceTest, UnmannedVehicleCommand) {
|
|
DataSourceConfig config;
|
|
config.vehicle.host = "localhost";
|
|
config.vehicle.port = 8081;
|
|
config.vehicle.command_path = "/api/VehicleCommandInfo";
|
|
|
|
HTTPDataSource source(config);
|
|
ASSERT_TRUE(source.connect());
|
|
|
|
// 先发送 GREEN 指令解除 RED 状态
|
|
VehicleCommand green_command;
|
|
green_command.vehicleId = "QN001";
|
|
green_command.type = CommandType::SIGNAL;
|
|
green_command.reason = CommandReason::TRAFFIC_LIGHT;
|
|
green_command.signalState = SignalState::GREEN;
|
|
green_command.intersectionId = "INTERSECTION_001";
|
|
green_command.timestamp = std::chrono::duration_cast<std::chrono::microseconds>(
|
|
std::chrono::system_clock::now().time_since_epoch()).count();
|
|
ASSERT_TRUE(source.sendUnmannedVehicleCommand(green_command.vehicleId, green_command)) << "发送绿灯指令失败";
|
|
|
|
// 等待一小段时间确保 GREEN 指令生效
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
|
// 再发送 RESUME 指令解除其他可能的状态
|
|
VehicleCommand resume_command;
|
|
resume_command.vehicleId = "QN001";
|
|
resume_command.type = CommandType::RESUME;
|
|
resume_command.reason = CommandReason::RESUME_TRAFFIC;
|
|
resume_command.timestamp = std::chrono::duration_cast<std::chrono::microseconds>(
|
|
std::chrono::system_clock::now().time_since_epoch()).count();
|
|
ASSERT_TRUE(source.sendUnmannedVehicleCommand(resume_command.vehicleId, resume_command)) << "发送恢复指令失败";
|
|
|
|
// 等待一小段时间确保 RESUME 指令生效
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
|
// 发送告警指令
|
|
VehicleCommand command;
|
|
command.vehicleId = "QN001";
|
|
command.type = CommandType::ALERT;
|
|
command.reason = CommandReason::AIRCRAFT_CROSSING;
|
|
command.relativeSpeed = 100;
|
|
command.relativeMotionX = 100;
|
|
command.relativeMotionY = 100;
|
|
command.minDistance = 100;
|
|
command.latitude = 36.36;
|
|
command.longitude = 120.08;
|
|
command.timestamp = std::chrono::duration_cast<std::chrono::microseconds>(
|
|
std::chrono::system_clock::now().time_since_epoch()).count();
|
|
|
|
ASSERT_TRUE(source.sendUnmannedVehicleCommand(command.vehicleId, command)) << "发送无人车指令失败";
|
|
}
|
|
|
|
TEST_F(HTTPDataSourceTest, TrafficLightAndUnmannedAuthFailure) {
|
|
DataSourceConfig config;
|
|
config.traffic_light.host = "localhost";
|
|
config.traffic_light.port = 8081;
|
|
config.traffic_light.signal_path = "/api/VehicleCommandInfo";
|
|
config.traffic_light.auth.auth_required = true;
|
|
config.traffic_light.auth.username = "wrong_user";
|
|
config.traffic_light.auth.password = "wrong_pass";
|
|
|
|
config.vehicle.host = "localhost";
|
|
config.vehicle.port = 8081;
|
|
config.vehicle.command_path = "/api/VehicleCommandInfo";
|
|
config.vehicle.auth.auth_required = true;
|
|
config.vehicle.auth.username = "wrong_user";
|
|
config.vehicle.auth.password = "wrong_pass";
|
|
|
|
HTTPDataSource source(config);
|
|
|
|
std::vector<TrafficLightSignal> signals;
|
|
EXPECT_FALSE(source.fetchTrafficLightSignals(signals));
|
|
|
|
VehicleCommand command;
|
|
command.vehicleId = "TEST_VEHICLE_001";
|
|
command.type = CommandType::SIGNAL;
|
|
command.reason = CommandReason::RESUME_TRAFFIC;
|
|
command.signalState = SignalState::GREEN;
|
|
command.timestamp = 0;
|
|
EXPECT_FALSE(source.sendUnmannedVehicleCommand(command.vehicleId, command));
|
|
}
|