224 lines
6.8 KiB
C++
224 lines
6.8 KiB
C++
#include <gtest/gtest.h>
|
||
#include "app.hpp"
|
||
#include <thread>
|
||
#include <chrono>
|
||
|
||
using namespace bmp;
|
||
|
||
// 注意:generateId, now, timeToString 为文件内部 static 函数,
|
||
// 具有内部链接属性,无法在外部测试文件中直接访问和测试。
|
||
// 根据 C++ 单元测试最佳实践及编译错误反馈,移除对它们的直接测试。
|
||
|
||
TEST(EventServiceTest, testReceiveEventNormal) {
|
||
EventService svc;
|
||
EXPECT_TRUE(svc.receiveEvent("<event>test</event>"));
|
||
}
|
||
|
||
TEST(EventServiceTest, testSendAckNormal) {
|
||
EventService svc;
|
||
svc.receiveEvent("<event>test</event>");
|
||
auto events = svc.getPendingEvents();
|
||
std::string ack = svc.sendAck(events[0].id);
|
||
EXPECT_EQ(0, ack.find("ACK:"));
|
||
}
|
||
|
||
TEST(EventServiceTest, testGetRawEventNormal) {
|
||
EventService svc;
|
||
svc.receiveEvent("<event>test</event>");
|
||
auto events = svc.getPendingEvents();
|
||
const Event* evt = svc.getRawEvent(events[0].id);
|
||
EXPECT_NE(nullptr, evt);
|
||
}
|
||
|
||
TEST(EventServiceTest, testProcessEventNormal) {
|
||
EventService svc;
|
||
svc.receiveEvent("<event>test</event>");
|
||
auto events = svc.getPendingEvents();
|
||
EXPECT_TRUE(svc.processEvent(events[0].id));
|
||
const Event* evt = svc.getRawEvent(events[0].id);
|
||
EXPECT_EQ(EventStatus::PROCESSING, evt->status);
|
||
}
|
||
|
||
TEST(EventServiceTest, testSaveStandardEvent) {
|
||
EventService svc;
|
||
Event evt;
|
||
evt.id = "EVT_001";
|
||
evt.status = EventStatus::ACCEPTED;
|
||
svc.saveStandardEvent(evt);
|
||
const Event* res = svc.getRawEvent("EVT_001");
|
||
EXPECT_NE(nullptr, res);
|
||
EXPECT_EQ(EventStatus::ACCEPTED, res->status);
|
||
}
|
||
|
||
TEST(EventServiceTest, testQueryEventsFilter) {
|
||
EventService svc;
|
||
svc.receiveEvent("<event>test</event>");
|
||
std::unordered_map<std::string, std::string> filters;
|
||
filters["status"] = "PENDING";
|
||
auto res = svc.queryEvents(filters);
|
||
EXPECT_FALSE(res.empty());
|
||
}
|
||
|
||
TEST(EventServiceTest, testGetPendingEvents) {
|
||
EventService svc;
|
||
svc.receiveEvent("<event>test</event>");
|
||
auto res = svc.getPendingEvents();
|
||
EXPECT_FALSE(res.empty());
|
||
}
|
||
|
||
TEST(TemplateServiceTest, testReceiveTemplatesNormal) {
|
||
TemplateService svc;
|
||
std::string data = "<id>T1</id>\n<name>Template1</name>\n</template>";
|
||
size_t count = svc.receiveTemplates(data);
|
||
EXPECT_GT(count, 0);
|
||
}
|
||
|
||
TEST(TemplateServiceTest, testGetTemplateList) {
|
||
TemplateService svc;
|
||
svc.receiveTemplates("<id>T1</id>\n<name>Template1</name>\n</template>");
|
||
auto list = svc.getTemplateList();
|
||
EXPECT_FALSE(list.empty());
|
||
}
|
||
|
||
TEST(TemplateServiceTest, testSelectTemplateNormal) {
|
||
TemplateService svc;
|
||
svc.receiveTemplates("<id>T1</id>\n<name>Template1</name>\n</template>");
|
||
EXPECT_TRUE(svc.selectTemplate("T1"));
|
||
}
|
||
|
||
TEST(TemplateServiceTest, testRecommendTemplate) {
|
||
TemplateService svc;
|
||
svc.receiveTemplates("<id>T1</id>\n<name>Template1</name>\n<scenario>RECON</scenario>\n</template>");
|
||
std::string rec = svc.recommendTemplate("RECON");
|
||
EXPECT_EQ("T1", rec);
|
||
}
|
||
|
||
TEST(TemplateServiceTest, testGetConfig) {
|
||
TemplateService svc;
|
||
auto cfg = svc.getConfig();
|
||
EXPECT_EQ("enabled", cfg["version_check"]);
|
||
}
|
||
|
||
TEST(PlanServiceTest, testGenerateTaskNormal) {
|
||
PlanService svc;
|
||
std::unordered_map<std::string, std::string> params;
|
||
std::string planId = svc.generateTask("EVT1", "TPL1", params);
|
||
EXPECT_FALSE(planId.empty());
|
||
}
|
||
|
||
TEST(PlanServiceTest, testSubmitTask) {
|
||
PlanService svc;
|
||
std::unordered_map<std::string, std::string> params;
|
||
std::string planId = svc.generateTask("EVT1", "TPL1", params);
|
||
TaskPlan plan = svc.submitTask(planId);
|
||
EXPECT_EQ(PlanStatus::CONFIRMED, plan.status);
|
||
}
|
||
|
||
TEST(PlanServiceTest, testGetPlanList) {
|
||
PlanService svc;
|
||
std::unordered_map<std::string, std::string> params;
|
||
svc.generateTask("EVT1", "TPL1", params);
|
||
auto list = svc.getPlanList();
|
||
EXPECT_FALSE(list.empty());
|
||
}
|
||
|
||
TEST(PlanServiceTest, testSortPlans) {
|
||
PlanService svc;
|
||
std::unordered_map<std::string, std::string> params;
|
||
svc.generateTask("EVT1", "TPL1", params);
|
||
std::unordered_map<std::string, int> weights = {{"priority", 1}, {"time", 1}};
|
||
EXPECT_NO_THROW(svc.sortPlans(weights));
|
||
}
|
||
|
||
TEST(PlanServiceTest, testReconfigurePlan) {
|
||
PlanService svc;
|
||
std::unordered_map<std::string, std::string> params;
|
||
std::string planId = svc.generateTask("EVT1", "TPL1", params);
|
||
std::vector<PlanNode> newNodes;
|
||
EXPECT_TRUE(svc.reconfigurePlan(planId, newNodes));
|
||
}
|
||
|
||
TEST(PlanServiceTest, testNotifyHITL) {
|
||
PlanService svc;
|
||
std::unordered_map<std::string, std::string> params;
|
||
std::string planId = svc.generateTask("EVT1", "TPL1", params);
|
||
EXPECT_NO_THROW(svc.notifyHITL(planId, "Test message"));
|
||
}
|
||
|
||
TEST(DistributionServiceTest, testDistributePlan) {
|
||
DistributionService svc;
|
||
auto logs = svc.distributePlan("PLAN1");
|
||
EXPECT_EQ(3, logs.size());
|
||
}
|
||
|
||
TEST(DistributionServiceTest, testReceiveAck) {
|
||
DistributionService svc;
|
||
auto logs = svc.distributePlan("PLAN1");
|
||
EXPECT_TRUE(svc.receiveAck(logs[0].logId, DistributionResponse::ACK));
|
||
}
|
||
|
||
TEST(DistributionServiceTest, testUpdateStatus) {
|
||
DistributionService svc;
|
||
EXPECT_NO_THROW(svc.updateStatus("PLAN1"));
|
||
}
|
||
|
||
TEST(DistributionServiceTest, testGetLogsByPlan) {
|
||
DistributionService svc;
|
||
svc.distributePlan("PLAN1");
|
||
auto logs = svc.getLogsByPlan("PLAN1");
|
||
EXPECT_EQ(3, logs.size());
|
||
}
|
||
|
||
TEST(StatusMonitorServiceTest, testReceiveTelemetry) {
|
||
StatusMonitorService svc;
|
||
std::vector<uint8_t> data(20, 0);
|
||
auto log = svc.receiveTelemetry(data);
|
||
EXPECT_FALSE(log.assetId.empty());
|
||
}
|
||
|
||
TEST(StatusMonitorServiceTest, testParseDataTooShort) {
|
||
StatusMonitorService svc;
|
||
std::vector<uint8_t> data(5, 0);
|
||
auto log = svc.parseData(data);
|
||
EXPECT_TRUE(log.isAbnormal);
|
||
}
|
||
|
||
TEST(StatusMonitorServiceTest, testTriggerAlarm) {
|
||
StatusMonitorService svc;
|
||
StatusLog log;
|
||
log.isAbnormal = true;
|
||
log.assetId = "AST_1";
|
||
log.detailInfo = "Error";
|
||
std::string alarm = svc.triggerAlarm(log);
|
||
EXPECT_NE(std::string::npos, alarm.find("[ALARM]"));
|
||
}
|
||
|
||
TEST(StatusMonitorServiceTest, testGetLatestStatus) {
|
||
StatusMonitorService svc;
|
||
std::vector<uint8_t> data(20, 0);
|
||
svc.receiveTelemetry(data);
|
||
auto log = svc.getLatestStatus("AST_0");
|
||
EXPECT_EQ("AST_0", log.assetId);
|
||
}
|
||
|
||
TEST(StatusMonitorServiceTest, testCheckStaleData) {
|
||
StatusMonitorService svc;
|
||
std::vector<uint8_t> data(20, 0);
|
||
svc.receiveTelemetry(data);
|
||
EXPECT_NO_THROW(svc.checkStaleData(0));
|
||
}
|
||
|
||
TEST(BattlefieldMissionPlannerTest, testConstructor) {
|
||
EXPECT_NO_THROW(BattlefieldMissionPlanner planner);
|
||
}
|
||
|
||
TEST(BattlefieldMissionPlannerTest, testInitialize) {
|
||
BattlefieldMissionPlanner planner;
|
||
EXPECT_TRUE(planner.initialize());
|
||
}
|
||
|
||
TEST(BattlefieldMissionPlannerTest, testRun) {
|
||
BattlefieldMissionPlanner planner;
|
||
EXPECT_NO_THROW(planner.run());
|
||
}
|