363 lines
10 KiB
C++
363 lines
10 KiB
C++
|
|
#include <gtest/gtest.h>
|
|||
|
|
#include "app.hpp"
|
|||
|
|
|
|||
|
|
// ============================================================================
|
|||
|
|
// 测试夹具:CmsEngineTest
|
|||
|
|
// ============================================================================
|
|||
|
|
class CmsEngineTest : public ::testing::Test {
|
|||
|
|
protected:
|
|||
|
|
void SetUp() override {
|
|||
|
|
// 每个测试用例开始前重置引擎
|
|||
|
|
engine = new CmsEngine();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void TearDown() override {
|
|||
|
|
delete engine;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
CmsEngine* engine;
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// ============================================================================
|
|||
|
|
// ingestEvent 测试
|
|||
|
|
// ============================================================================
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 测试 ingestEvent 正常输入:有效事件应成功接入
|
|||
|
|
*/
|
|||
|
|
TEST_F(CmsEngineTest, testIngestEventNormal) {
|
|||
|
|
EventRecord event;
|
|||
|
|
event.id = "EVT-001";
|
|||
|
|
event.priority = 100;
|
|||
|
|
event.status = EventStatus::Pending;
|
|||
|
|
|
|||
|
|
bool result = engine->ingestEvent(event);
|
|||
|
|
EXPECT_TRUE(result);
|
|||
|
|
|
|||
|
|
// 验证事件已存储
|
|||
|
|
const EventRecord* found = engine->findEventById("EVT-001");
|
|||
|
|
ASSERT_NE(found, nullptr);
|
|||
|
|
EXPECT_EQ(found->id, "EVT-001");
|
|||
|
|
EXPECT_EQ(found->priority, 100);
|
|||
|
|
EXPECT_EQ(found->status, EventStatus::Pending);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 测试 ingestEvent 空 ID:应返回 false
|
|||
|
|
*/
|
|||
|
|
TEST_F(CmsEngineTest, testIngestEventEmptyId) {
|
|||
|
|
EventRecord event;
|
|||
|
|
event.id = "";
|
|||
|
|
event.priority = 100;
|
|||
|
|
|
|||
|
|
bool result = engine->ingestEvent(event);
|
|||
|
|
EXPECT_FALSE(result);
|
|||
|
|
|
|||
|
|
// 验证事件未被存储
|
|||
|
|
const EventRecord* found = engine->findEventById("");
|
|||
|
|
EXPECT_EQ(found, nullptr);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 测试 ingestEvent 优先级边界:优先级为 0 应成功
|
|||
|
|
*/
|
|||
|
|
TEST_F(CmsEngineTest, testIngestEventPriorityMin) {
|
|||
|
|
EventRecord event;
|
|||
|
|
event.id = "EVT-002";
|
|||
|
|
event.priority = 0;
|
|||
|
|
|
|||
|
|
bool result = engine->ingestEvent(event);
|
|||
|
|
EXPECT_TRUE(result);
|
|||
|
|
|
|||
|
|
const EventRecord* found = engine->findEventById("EVT-002");
|
|||
|
|
ASSERT_NE(found, nullptr);
|
|||
|
|
EXPECT_EQ(found->priority, 0);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 测试 ingestEvent 优先级边界:优先级为 255 应成功
|
|||
|
|
*/
|
|||
|
|
TEST_F(CmsEngineTest, testIngestEventPriorityMax) {
|
|||
|
|
EventRecord event;
|
|||
|
|
event.id = "EVT-003";
|
|||
|
|
event.priority = 255;
|
|||
|
|
|
|||
|
|
bool result = engine->ingestEvent(event);
|
|||
|
|
EXPECT_TRUE(result);
|
|||
|
|
|
|||
|
|
const EventRecord* found = engine->findEventById("EVT-003");
|
|||
|
|
ASSERT_NE(found, nullptr);
|
|||
|
|
EXPECT_EQ(found->priority, 255);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 测试 ingestEvent 优先级越界:优先级 > 255 应返回 false
|
|||
|
|
*/
|
|||
|
|
TEST_F(CmsEngineTest, testIngestEventPriorityOverflow) {
|
|||
|
|
EventRecord event;
|
|||
|
|
event.id = "EVT-004";
|
|||
|
|
event.priority = 256;
|
|||
|
|
|
|||
|
|
bool result = engine->ingestEvent(event);
|
|||
|
|
EXPECT_FALSE(result);
|
|||
|
|
|
|||
|
|
const EventRecord* found = engine->findEventById("EVT-004");
|
|||
|
|
EXPECT_EQ(found, nullptr);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 测试 ingestEvent 多次接入:多个事件应全部成功
|
|||
|
|
*/
|
|||
|
|
TEST_F(CmsEngineTest, testIngestEventMultiple) {
|
|||
|
|
EventRecord event1;
|
|||
|
|
event1.id = "EVT-005";
|
|||
|
|
event1.priority = 50;
|
|||
|
|
|
|||
|
|
EventRecord event2;
|
|||
|
|
event2.id = "EVT-006";
|
|||
|
|
event2.priority = 150;
|
|||
|
|
|
|||
|
|
EXPECT_TRUE(engine->ingestEvent(event1));
|
|||
|
|
EXPECT_TRUE(engine->ingestEvent(event2));
|
|||
|
|
|
|||
|
|
const EventRecord* found1 = engine->findEventById("EVT-005");
|
|||
|
|
const EventRecord* found2 = engine->findEventById("EVT-006");
|
|||
|
|
ASSERT_NE(found1, nullptr);
|
|||
|
|
ASSERT_NE(found2, nullptr);
|
|||
|
|
EXPECT_EQ(found1->priority, 50);
|
|||
|
|
EXPECT_EQ(found2->priority, 150);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ============================================================================
|
|||
|
|
// processPendingEvents 测试
|
|||
|
|
// ============================================================================
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 测试 processPendingEvents 正常处理:Pending 事件应被处理
|
|||
|
|
*/
|
|||
|
|
TEST_F(CmsEngineTest, testProcessPendingEventsNormal) {
|
|||
|
|
EventRecord event;
|
|||
|
|
event.id = "EVT-010";
|
|||
|
|
event.priority = 100;
|
|||
|
|
event.status = EventStatus::Pending;
|
|||
|
|
engine->ingestEvent(event);
|
|||
|
|
|
|||
|
|
size_t count = engine->processPendingEvents();
|
|||
|
|
EXPECT_EQ(count, 1);
|
|||
|
|
|
|||
|
|
// 验证事件状态已更新
|
|||
|
|
const EventRecord* found = engine->findEventById("EVT-010");
|
|||
|
|
ASSERT_NE(found, nullptr);
|
|||
|
|
EXPECT_EQ(found->status, EventStatus::Generated);
|
|||
|
|
|
|||
|
|
// 验证生成了对应的 TaskPlan
|
|||
|
|
const auto& plans = engine->getAllPlans();
|
|||
|
|
ASSERT_EQ(plans.size(), 1);
|
|||
|
|
EXPECT_EQ(plans[0].relatedEventId, "EVT-010");
|
|||
|
|
EXPECT_EQ(plans[0].name, "AutoPlan-EVT-010");
|
|||
|
|
EXPECT_EQ(plans[0].type, PlanType::Centralized);
|
|||
|
|
EXPECT_EQ(plans[0].status, PlanStatus::Drafting);
|
|||
|
|
EXPECT_EQ(plans[0].resourceQuota, 0.5);
|
|||
|
|
EXPECT_EQ(plans[0].constraints, "自动生成约束");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 测试 processPendingEvents 无 Pending 事件:应返回 0
|
|||
|
|
*/
|
|||
|
|
TEST_F(CmsEngineTest, testProcessPendingEventsNoPending) {
|
|||
|
|
EventRecord event;
|
|||
|
|
event.id = "EVT-011";
|
|||
|
|
event.priority = 100;
|
|||
|
|
event.status = EventStatus::Generated; // 非 Pending 状态
|
|||
|
|
engine->ingestEvent(event);
|
|||
|
|
|
|||
|
|
size_t count = engine->processPendingEvents();
|
|||
|
|
EXPECT_EQ(count, 0);
|
|||
|
|
|
|||
|
|
// 验证事件状态未改变
|
|||
|
|
const EventRecord* found = engine->findEventById("EVT-011");
|
|||
|
|
ASSERT_NE(found, nullptr);
|
|||
|
|
EXPECT_EQ(found->status, EventStatus::Generated);
|
|||
|
|
|
|||
|
|
// 验证未生成 TaskPlan
|
|||
|
|
const auto& plans = engine->getAllPlans();
|
|||
|
|
EXPECT_EQ(plans.size(), 0);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 测试 processPendingEvents 空事件库:应返回 0
|
|||
|
|
*/
|
|||
|
|
TEST_F(CmsEngineTest, testProcessPendingEventsEmpty) {
|
|||
|
|
size_t count = engine->processPendingEvents();
|
|||
|
|
EXPECT_EQ(count, 0);
|
|||
|
|
|
|||
|
|
const auto& plans = engine->getAllPlans();
|
|||
|
|
EXPECT_EQ(plans.size(), 0);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 测试 processPendingEvents 多个 Pending 事件:应全部处理
|
|||
|
|
*/
|
|||
|
|
TEST_F(CmsEngineTest, testProcessPendingEventsMultiple) {
|
|||
|
|
EventRecord event1;
|
|||
|
|
event1.id = "EVT-012";
|
|||
|
|
event1.priority = 100;
|
|||
|
|
event1.status = EventStatus::Pending;
|
|||
|
|
engine->ingestEvent(event1);
|
|||
|
|
|
|||
|
|
EventRecord event2;
|
|||
|
|
event2.id = "EVT-013";
|
|||
|
|
event2.priority = 200;
|
|||
|
|
event2.status = EventStatus::Pending;
|
|||
|
|
engine->ingestEvent(event2);
|
|||
|
|
|
|||
|
|
EventRecord event3;
|
|||
|
|
event3.id = "EVT-014";
|
|||
|
|
event3.priority = 50;
|
|||
|
|
event3.status = EventStatus::Generated; // 非 Pending
|
|||
|
|
engine->ingestEvent(event3);
|
|||
|
|
|
|||
|
|
size_t count = engine->processPendingEvents();
|
|||
|
|
EXPECT_EQ(count, 2);
|
|||
|
|
|
|||
|
|
// 验证事件状态
|
|||
|
|
const EventRecord* found1 = engine->findEventById("EVT-012");
|
|||
|
|
const EventRecord* found2 = engine->findEventById("EVT-013");
|
|||
|
|
const EventRecord* found3 = engine->findEventById("EVT-014");
|
|||
|
|
ASSERT_NE(found1, nullptr);
|
|||
|
|
ASSERT_NE(found2, nullptr);
|
|||
|
|
ASSERT_NE(found3, nullptr);
|
|||
|
|
EXPECT_EQ(found1->status, EventStatus::Generated);
|
|||
|
|
EXPECT_EQ(found2->status, EventStatus::Generated);
|
|||
|
|
EXPECT_EQ(found3->status, EventStatus::Generated); // 未改变
|
|||
|
|
|
|||
|
|
// 验证生成了 2 个 TaskPlan
|
|||
|
|
const auto& plans = engine->getAllPlans();
|
|||
|
|
ASSERT_EQ(plans.size(), 2);
|
|||
|
|
EXPECT_EQ(plans[0].relatedEventId, "EVT-012");
|
|||
|
|
EXPECT_EQ(plans[1].relatedEventId, "EVT-013");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 测试 processPendingEvents 生成 ID 自增:ID 应连续
|
|||
|
|
*/
|
|||
|
|
TEST_F(CmsEngineTest, testProcessPendingEventsIdAutoIncrement) {
|
|||
|
|
EventRecord event1;
|
|||
|
|
event1.id = "EVT-015";
|
|||
|
|
event1.priority = 100;
|
|||
|
|
event1.status = EventStatus::Pending;
|
|||
|
|
engine->ingestEvent(event1);
|
|||
|
|
|
|||
|
|
EventRecord event2;
|
|||
|
|
event2.id = "EVT-016";
|
|||
|
|
event2.priority = 100;
|
|||
|
|
event2.status = EventStatus::Pending;
|
|||
|
|
engine->ingestEvent(event2);
|
|||
|
|
|
|||
|
|
engine->processPendingEvents();
|
|||
|
|
|
|||
|
|
const auto& plans = engine->getAllPlans();
|
|||
|
|
ASSERT_EQ(plans.size(), 2);
|
|||
|
|
// ID 格式为 "ID-xxxxx",自增
|
|||
|
|
EXPECT_EQ(plans[0].id, "ID-00001");
|
|||
|
|
EXPECT_EQ(plans[1].id, "ID-00002");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ============================================================================
|
|||
|
|
// findEventById 测试
|
|||
|
|
// ============================================================================
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 测试 findEventById 正常查找:应返回正确的事件指针
|
|||
|
|
*/
|
|||
|
|
TEST_F(CmsEngineTest, testFindEventByIdNormal) {
|
|||
|
|
EventRecord event;
|
|||
|
|
event.id = "EVT-020";
|
|||
|
|
event.priority = 100;
|
|||
|
|
event.status = EventStatus::Pending;
|
|||
|
|
engine->ingestEvent(event);
|
|||
|
|
|
|||
|
|
const EventRecord* found = engine->findEventById("EVT-020");
|
|||
|
|
ASSERT_NE(found, nullptr);
|
|||
|
|
EXPECT_EQ(found->id, "EVT-020");
|
|||
|
|
EXPECT_EQ(found->priority, 100);
|
|||
|
|
EXPECT_EQ(found->status, EventStatus::Pending);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 测试 findEventById 查找不存在的 ID:应返回 nullptr
|
|||
|
|
*/
|
|||
|
|
TEST_F(CmsEngineTest, testFindEventByIdNotFound) {
|
|||
|
|
EventRecord event;
|
|||
|
|
event.id = "EVT-021";
|
|||
|
|
event.priority = 100;
|
|||
|
|
engine->ingestEvent(event);
|
|||
|
|
|
|||
|
|
const EventRecord* found = engine->findEventById("NONEXISTENT");
|
|||
|
|
EXPECT_EQ(found, nullptr);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 测试 findEventById 空字符串查找:应返回 nullptr
|
|||
|
|
*/
|
|||
|
|
TEST_F(CmsEngineTest, testFindEventByIdEmptyString) {
|
|||
|
|
EventRecord event;
|
|||
|
|
event.id = "EVT-022";
|
|||
|
|
event.priority = 100;
|
|||
|
|
engine->ingestEvent(event);
|
|||
|
|
|
|||
|
|
const EventRecord* found = engine->findEventById("");
|
|||
|
|
EXPECT_EQ(found, nullptr);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 测试 findEventById 空事件库:应返回 nullptr
|
|||
|
|
*/
|
|||
|
|
TEST_F(CmsEngineTest, testFindEventByIdEmptyStore) {
|
|||
|
|
const EventRecord* found = engine->findEventById("ANY_ID");
|
|||
|
|
EXPECT_EQ(found, nullptr);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 测试 findEventById 多个事件中查找:应返回正确的事件
|
|||
|
|
*/
|
|||
|
|
TEST_F(CmsEngineTest, testFindEventByIdMultiple) {
|
|||
|
|
EventRecord event1;
|
|||
|
|
event1.id = "EVT-023";
|
|||
|
|
event1.priority = 50;
|
|||
|
|
engine->ingestEvent(event1);
|
|||
|
|
|
|||
|
|
EventRecord event2;
|
|||
|
|
event2.id = "EVT-024";
|
|||
|
|
event2.priority = 200;
|
|||
|
|
engine->ingestEvent(event2);
|
|||
|
|
|
|||
|
|
EventRecord event3;
|
|||
|
|
event3.id = "EVT-025";
|
|||
|
|
event3.priority = 100;
|
|||
|
|
engine->ingestEvent(event3);
|
|||
|
|
|
|||
|
|
const EventRecord* found = engine->findEventById("EVT-024");
|
|||
|
|
ASSERT_NE(found, nullptr);
|
|||
|
|
EXPECT_EQ(found->id, "EVT-024");
|
|||
|
|
EXPECT_EQ(found->priority, 200);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 测试 findEventById 查找已处理事件:应返回正确的事件
|
|||
|
|
*/
|
|||
|
|
TEST_F(CmsEngineTest, testFindEventByIdProcessedEvent) {
|
|||
|
|
EventRecord event;
|
|||
|
|
event.id = "EVT-026";
|
|||
|
|
event.priority = 100;
|
|||
|
|
event.status = EventStatus::Pending;
|
|||
|
|
engine->ingestEvent(event);
|
|||
|
|
|
|||
|
|
engine->processPendingEvents();
|
|||
|
|
|
|||
|
|
const EventRecord* found = engine->findEventById("EVT-026");
|
|||
|
|
ASSERT_NE(found, nullptr);
|
|||
|
|
EXPECT_EQ(found->id, "EVT-026");
|
|||
|
|
EXPECT_EQ(found->status, EventStatus::Generated);
|
|||
|
|
}
|