Files
qt5-legatus-legacy/Lts/Services/ExerciseManagement/ModuleTests/exm_statemachine_test.cpp
T
2026-08-13 18:33:10 +02:00

176 lines
5.1 KiB
C++
Executable File

/**
* Unittest for testing the statemachine for aes objects
* $Id$
* $HeadURL$
* $Author$
*/
#include "exm_statemachine_test.h"
// Generated
#include <ExerciseManagement/exm_enums.hpp>
#include <ExerciseManagement/exm_db.h>
#include <DBAdministration/exercise_table_generator.h>
#include <ExerciseManagement/exercise_state_machine.h>
#include <ExerciseManagement/dds_handler.h>
#include <ExerciseManagement/object_exercise.h>
#include <ExerciseManagement/settings.h>
#include <ExerciseManagement/logger.h>
#include <QCoreApplication>
#include <QDebug>
#include <QSignalSpy>
#include <QTest>
#include <nglapplication/nglcoreapplication.h>
#include <ngltools/ngltools_filesystem.h>
using namespace ExerciseManagement;
namespace QTest
{
template<>
char *toString(const ExerciseState &s) { std::ostringstream str; str << s; return strdup(str.str().c_str()); }
}
StateMachineTest::StateMachineTest()
: m_exmObject(nullptr)
{
}
StateMachineTest::~StateMachineTest()
{
delete m_exmObject;
}
void StateMachineTest::initTestCase()
{
NGL::INGLLogger &logger = getLogger("StateMachineTest");
m_dbHandler = std::make_shared<DbHandler>();
QVERIFY2(m_dbHandler.get(), "Reset of dbHandler failed");
bool bOk = m_dbHandler->init(0, Settings::settings());
// Initialize database
QVERIFY2(bOk, "Init dbHandler/DbInitialisation failed");
// initialize DDS
int domainId = 0;
m_ddsHandler = std::make_shared<DdsHandler>(domainId);
QVERIFY2(m_ddsHandler.get(), "Reset of ddsInterface failed");
QVERIFY2(m_ddsHandler->init(), "Ininitialisation of ddsInterface failed");
// Initialise ExerciseTableGenerator
const QString xmlPath = NGL::directory(NGL::FileType::ConfigFile, QLatin1String("xml"));
m_exTblGen = std::make_shared<DBAdministration::ExerciseTableGenerator>(logger, xmlPath);
QVERIFY2(m_exTblGen, "new object creation of ExerciseTableGenerator failed");
QVERIFY2(m_exTblGen->init(m_dbHandler->db()->database(), m_dbHandler->db()->schemaName(), false), "Initialisation of ExerciseTableGenerator failed");
HandlerExercise *exHandler = nullptr;
m_exmObject = new ObjectExercise(m_ddsHandler, m_dbHandler->db(), m_exTblGen, exHandler);
QVERIFY2(m_exmObject, "Initialisation of exerciseObject failed");
#if 1
Database::ExmExercise data;
data.name = QLatin1String("Test Statemachine");
data.trainingAreaId = 6; // Bremen
data.beginTime = QDateTime::currentDateTimeUtc();
data.endTime = data.beginTime.addDays(2);
#else
QVector<Database::ExmExercise> dataVec;
// read all exercises from database
QVERIFY2(m_dbHandler->db()->select(dataVec), "Error retrieving exercises from db");
DatabaseInterface::ExmExercise data;
if (dataVec.isEmpty() )
{
data.name = QLatin1String("Test Statemachine");
data.beginTime = QDateTime::currentDateTimeUtc().toMSecsSinceEpoch();
data.endTime = QDateTime::currentDateTimeUtc().toMSecsSinceEpoch() + 2000;
data.created = QDateTime::currentDateTimeUtc();
}
else
{
data = dataVec.first();
qDebug() << "First existing exerciseId: " << data.id << ", name = " << data.name;
}
#endif
Core::FunctionReplyObject ret = m_exmObject->createObject(data);
QVERIFY2(!ret.error(), "Creation of new exerciseObject failed");
QTest::qWait(100);
}
void StateMachineTest::staticTests()
{
// newly created, nothing set -> Status should be ExerciseState::Created
QCOMPARE(m_exmObject->exerciseState().underlying(), ExerciseState::Created);
}
void StateMachineTest::dynamicTests()
{
QSignalSpy spy(m_exmObject->getStateMachine(), &ExerciseStateMachine::exerciseStateChanged);
// Switch to STARTED
m_exmObject->switchToState(ExerciseState::Started, 0, 0);
QTRY_COMPARE(m_exmObject->exerciseState().underlying(), ExerciseState::Started);
QCOMPARE(spy.count(), 1); // Started
spy.clear();
// => PAUSED
m_exmObject->switchToState(ExerciseState::Paused, 99, 10);
QTRY_COMPARE(m_exmObject->exerciseState().underlying(), ExerciseState::Paused);
QCOMPARE(spy.count(), 1); // PAUSED
spy.clear();
// Try to (re)start
m_exmObject->switchToState(ExerciseState::Started, 99, 10);
QTRY_COMPARE(m_exmObject->exerciseState().underlying(), ExerciseState::Started);
QCOMPARE(spy.count(), 1); // Started (Resumed)
spy.clear();
// => Stopped
m_exmObject->switchToState(ExerciseState::Stopped, 99, 10);
QTRY_COMPARE(m_exmObject->exerciseState().underlying(), ExerciseState::Stopped);
QCOMPARE(spy.count(), 1); // Stopped
spy.clear();
// => Terminated
m_exmObject->switchToState(ExerciseState::Terminated, 99, 10);
QTRY_COMPARE(m_exmObject->exerciseState().underlying(), ExerciseState::Terminated);
QCOMPARE(spy.count(), 1); // Terminated
spy.clear();
}
void StateMachineTest::cleanupTestCase()
{
delete m_exmObject;
m_exmObject = nullptr;
}
// ------------------------------------------------------------------
int main(int argc, char **argv)
{
NGLCoreApplication app(argc, argv);
app.createLogger("EXM", 0);
STARTUP_LOG(app.baseLogger());
ExerciseManagement::StateMachineTest *test = new ExerciseManagement::StateMachineTest;
int retCode = QTest::qExec(test, 0, argv);
delete test;
SHUTDOWN_LOG(app.baseLogger());
return retCode;
}