387 lines
11 KiB
C++
387 lines
11 KiB
C++
#include <gtest/gtest.h>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <chrono>
|
|
#include "app.hpp"
|
|
|
|
// ============================================================
|
|
// Test Fixture for CmsEngine
|
|
// ============================================================
|
|
class CmsEngineTest : public ::testing::Test {
|
|
protected:
|
|
void SetUp() override {
|
|
engine = new CmsEngine();
|
|
}
|
|
|
|
void TearDown() override {
|
|
delete engine;
|
|
}
|
|
|
|
CmsEngine* engine;
|
|
};
|
|
|
|
// ============================================================
|
|
// Tests for ingestEvent
|
|
// ============================================================
|
|
|
|
/**
|
|
* @brief 正常输入测试:接入有效事件
|
|
*
|
|
* 验证 ingestEvent 能够成功接入一个包含有效 ID 和优先级的事件。
|
|
*/
|
|
TEST_F(CmsEngineTest, testIngestEventValidInput) {
|
|
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 边界值测试:优先级为 0 的事件
|
|
*
|
|
* 验证 ingestEvent 能够处理优先级为 0 的边界值。
|
|
*/
|
|
TEST_F(CmsEngineTest, testIngestEventPriorityZero) {
|
|
EventRecord event;
|
|
event.id = "EVT-002";
|
|
event.priority = 0;
|
|
event.status = EventStatus::Pending;
|
|
|
|
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 边界值测试:优先级为 255 的事件
|
|
*
|
|
* 验证 ingestEvent 能够处理优先级为 255 的边界值。
|
|
*/
|
|
TEST_F(CmsEngineTest, testIngestEventPriorityMax) {
|
|
EventRecord event;
|
|
event.id = "EVT-003";
|
|
event.priority = 255;
|
|
event.status = EventStatus::Pending;
|
|
|
|
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 异常输入测试:空 ID 事件
|
|
*
|
|
* 验证 ingestEvent 拒绝接入 ID 为空的事件。
|
|
*/
|
|
TEST_F(CmsEngineTest, testIngestEventEmptyId) {
|
|
EventRecord event;
|
|
event.id = "";
|
|
event.priority = 100;
|
|
event.status = EventStatus::Pending;
|
|
|
|
bool result = engine->ingestEvent(event);
|
|
EXPECT_FALSE(result);
|
|
|
|
// 验证事件未被存储
|
|
const EventRecord* found = engine->findEventById("");
|
|
EXPECT_EQ(found, nullptr);
|
|
}
|
|
|
|
/**
|
|
* @brief 异常输入测试:优先级超过 255 的事件
|
|
*
|
|
* 验证 ingestEvent 拒绝接入优先级超过 255 的事件。
|
|
*/
|
|
TEST_F(CmsEngineTest, testIngestEventPriorityOverflow) {
|
|
EventRecord event;
|
|
event.id = "EVT-004";
|
|
event.priority = 256;
|
|
event.status = EventStatus::Pending;
|
|
|
|
bool result = engine->ingestEvent(event);
|
|
EXPECT_FALSE(result);
|
|
|
|
const EventRecord* found = engine->findEventById("EVT-004");
|
|
EXPECT_EQ(found, nullptr);
|
|
}
|
|
|
|
/**
|
|
* @brief 特殊场景测试:接入多个事件
|
|
*
|
|
* 验证 ingestEvent 能够连续接入多个事件并正确存储。
|
|
*/
|
|
TEST_F(CmsEngineTest, testIngestEventMultipleEvents) {
|
|
EventRecord event1;
|
|
event1.id = "EVT-005";
|
|
event1.priority = 50;
|
|
event1.status = EventStatus::Pending;
|
|
|
|
EventRecord event2;
|
|
event2.id = "EVT-006";
|
|
event2.priority = 150;
|
|
event2.status = EventStatus::Pending;
|
|
|
|
EXPECT_TRUE(engine->ingestEvent(event1));
|
|
EXPECT_TRUE(engine->ingestEvent(event2));
|
|
|
|
const EventRecord* found1 = engine->findEventById("EVT-005");
|
|
ASSERT_NE(found1, nullptr);
|
|
EXPECT_EQ(found1->priority, 50);
|
|
|
|
const EventRecord* found2 = engine->findEventById("EVT-006");
|
|
ASSERT_NE(found2, nullptr);
|
|
EXPECT_EQ(found2->priority, 150);
|
|
}
|
|
|
|
// ============================================================
|
|
// Tests for processPendingEvents
|
|
// ============================================================
|
|
|
|
/**
|
|
* @brief 正常输入测试:处理待处理事件
|
|
*
|
|
* 验证 processPendingEvents 能够正确处理 Pending 状态的事件。
|
|
*/
|
|
TEST_F(CmsEngineTest, testProcessPendingEventsValid) {
|
|
// 准备待处理事件
|
|
EventRecord event;
|
|
event.id = "EVT-010";
|
|
event.priority = 100;
|
|
event.status = EventStatus::Pending;
|
|
engine->ingestEvent(event);
|
|
|
|
size_t processed = engine->processPendingEvents();
|
|
EXPECT_EQ(processed, 1);
|
|
|
|
// 验证事件状态已更新
|
|
const EventRecord* found = engine->findEventById("EVT-010");
|
|
ASSERT_NE(found, nullptr);
|
|
EXPECT_EQ(found->status, EventStatus::Generated);
|
|
|
|
// 验证自动生成了任务方案
|
|
const auto& plans = engine->getAllPlans();
|
|
ASSERT_EQ(plans.size(), 1);
|
|
EXPECT_EQ(plans[0].relatedEventId, "EVT-010");
|
|
EXPECT_EQ(plans[0].status, PlanStatus::Drafting);
|
|
EXPECT_EQ(plans[0].type, PlanType::Centralized);
|
|
}
|
|
|
|
/**
|
|
* @brief 边界值测试:无待处理事件
|
|
*
|
|
* 验证 processPendingEvents 在没有 Pending 事件时返回 0。
|
|
*/
|
|
TEST_F(CmsEngineTest, testProcessPendingEventsNoPending) {
|
|
// 添加一个非 Pending 状态的事件
|
|
EventRecord event;
|
|
event.id = "EVT-011";
|
|
event.priority = 100;
|
|
event.status = EventStatus::Generated;
|
|
engine->ingestEvent(event);
|
|
|
|
size_t processed = engine->processPendingEvents();
|
|
EXPECT_EQ(processed, 0);
|
|
|
|
// 验证事件状态未改变
|
|
const EventRecord* found = engine->findEventById("EVT-011");
|
|
ASSERT_NE(found, nullptr);
|
|
EXPECT_EQ(found->status, EventStatus::Generated);
|
|
|
|
// 验证没有生成任务方案
|
|
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);
|
|
|
|
size_t processed = engine->processPendingEvents();
|
|
EXPECT_EQ(processed, 2);
|
|
|
|
// 验证所有事件状态已更新
|
|
const EventRecord* found1 = engine->findEventById("EVT-012");
|
|
ASSERT_NE(found1, nullptr);
|
|
EXPECT_EQ(found1->status, EventStatus::Generated);
|
|
|
|
const EventRecord* found2 = engine->findEventById("EVT-013");
|
|
ASSERT_NE(found2, nullptr);
|
|
EXPECT_EQ(found2->status, EventStatus::Generated);
|
|
|
|
// 验证生成了两个任务方案
|
|
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 只处理 Pending 状态的事件,忽略其他状态。
|
|
*/
|
|
TEST_F(CmsEngineTest, testProcessPendingEventsMixedStatus) {
|
|
EventRecord pendingEvent;
|
|
pendingEvent.id = "EVT-014";
|
|
pendingEvent.priority = 100;
|
|
pendingEvent.status = EventStatus::Pending;
|
|
engine->ingestEvent(pendingEvent);
|
|
|
|
EventRecord generatedEvent;
|
|
generatedEvent.id = "EVT-015";
|
|
generatedEvent.priority = 150;
|
|
generatedEvent.status = EventStatus::Generated;
|
|
engine->ingestEvent(generatedEvent);
|
|
|
|
size_t processed = engine->processPendingEvents();
|
|
EXPECT_EQ(processed, 1);
|
|
|
|
// 验证只有 Pending 事件被处理
|
|
const EventRecord* foundPending = engine->findEventById("EVT-014");
|
|
ASSERT_NE(foundPending, nullptr);
|
|
EXPECT_EQ(foundPending->status, EventStatus::Generated);
|
|
|
|
const EventRecord* foundGenerated = engine->findEventById("EVT-015");
|
|
ASSERT_NE(foundGenerated, nullptr);
|
|
EXPECT_EQ(foundGenerated->status, EventStatus::Generated);
|
|
|
|
// 验证只生成了一个任务方案
|
|
const auto& plans = engine->getAllPlans();
|
|
ASSERT_EQ(plans.size(), 1);
|
|
EXPECT_EQ(plans[0].relatedEventId, "EVT-014");
|
|
}
|
|
|
|
// ============================================================
|
|
// Tests for findEventById
|
|
// ============================================================
|
|
|
|
/**
|
|
* @brief 正常输入测试:查找已存在的事件
|
|
*
|
|
* 验证 findEventById 能够正确返回已存储的事件指针。
|
|
*/
|
|
TEST_F(CmsEngineTest, testFindEventByIdExisting) {
|
|
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 边界值测试:查找不存在的 ID
|
|
*
|
|
* 验证 findEventById 在查找不存在的 ID 时返回 nullptr。
|
|
*/
|
|
TEST_F(CmsEngineTest, testFindEventByIdNonExistent) {
|
|
const EventRecord* found = engine->findEventById("NONEXISTENT");
|
|
EXPECT_EQ(found, nullptr);
|
|
}
|
|
|
|
/**
|
|
* @brief 边界值测试:查找空字符串 ID
|
|
*
|
|
* 验证 findEventById 在查找空字符串 ID 时返回 nullptr。
|
|
*/
|
|
TEST_F(CmsEngineTest, testFindEventByIdEmptyString) {
|
|
const EventRecord* found = engine->findEventById("");
|
|
EXPECT_EQ(found, nullptr);
|
|
}
|
|
|
|
/**
|
|
* @brief 特殊场景测试:查找多个事件中的特定事件
|
|
*
|
|
* 验证 findEventById 在多个事件中能够正确找到目标事件。
|
|
*/
|
|
TEST_F(CmsEngineTest, testFindEventByIdMultipleEvents) {
|
|
EventRecord event1;
|
|
event1.id = "EVT-021";
|
|
event1.priority = 50;
|
|
event1.status = EventStatus::Pending;
|
|
engine->ingestEvent(event1);
|
|
|
|
EventRecord event2;
|
|
event2.id = "EVT-022";
|
|
event2.priority = 150;
|
|
event2.status = EventStatus::Generated;
|
|
engine->ingestEvent(event2);
|
|
|
|
EventRecord event3;
|
|
event3.id = "EVT-023";
|
|
event3.priority = 200;
|
|
event3.status = EventStatus::Pending;
|
|
engine->ingestEvent(event3);
|
|
|
|
// 查找中间的事件
|
|
const EventRecord* found = engine->findEventById("EVT-022");
|
|
ASSERT_NE(found, nullptr);
|
|
EXPECT_EQ(found->id, "EVT-022");
|
|
EXPECT_EQ(found->priority, 150);
|
|
EXPECT_EQ(found->status, EventStatus::Generated);
|
|
|
|
// 确认其他事件仍然存在
|
|
const EventRecord* found1 = engine->findEventById("EVT-021");
|
|
ASSERT_NE(found1, nullptr);
|
|
EXPECT_EQ(found1->id, "EVT-021");
|
|
|
|
const EventRecord* found3 = engine->findEventById("EVT-023");
|
|
ASSERT_NE(found3, nullptr);
|
|
EXPECT_EQ(found3->id, "EVT-023");
|
|
}
|
|
|
|
/**
|
|
* @brief 特殊场景测试:查找已处理事件
|
|
*
|
|
* 验证 findEventById 能够找到经过 processPendingEvents 处理的事件。
|
|
*/
|
|
TEST_F(CmsEngineTest, testFindEventByIdAfterProcessing) {
|
|
EventRecord event;
|
|
event.id = "EVT-030";
|
|
event.priority = 100;
|
|
event.status = EventStatus::Pending;
|
|
engine->ingestEvent(event);
|
|
|
|
engine->processPendingEvents();
|
|
|
|
const EventRecord* found = engine->findEventById("EVT-030");
|
|
ASSERT_NE(found, nullptr);
|
|
EXPECT_EQ(found->id, "EVT-030");
|
|
EXPECT_EQ(found->status, EventStatus::Generated);
|
|
}
|