/** * Unittest for testing the statemachine for aes objects * $Id$ * $HeadURL$ * $Author$ */ #include "exm_statemachine_test.h" // Generated #include #include #include #include #include #include #include #include #include #include #include #include #include #include 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(); 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(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(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 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; }