/** * opm Function Topic test executable * $Id$ * $HeadURL$ * $Author$ * Created by hjl, SLSE, 2016-04-18 */ #include "opm_tests.h" #include #include #include #include #include #include #include #include #include using namespace OperatorManagement; //----------------------------------------------------------------------------- OpmTests::OpmTests(QObject *parent) : QObject(parent) , m_exerciseId(0) , m_testId(0) , m_operatorId(0) , m_createdId(0) , m_eTestStep(eUnknown) , m_bTestsSuccessful(true) , m_logger(getLogger("OpmTests")) { qRegisterMetaType("Core::FunctionReply"); } // end of ctor() //----------------------------------------------------------------------------- OpmTests::~OpmTests() { } // end of dtor() //----------------------------------------------------------------------------- bool OpmTests::init(int domainId, int exerciseId, int testId, int operatorId, int createdId) { INFO_LOG(m_logger) << "OpmTests::init() " << " domainId:" << domainId << " exerciseId:" << exerciseId << " testId: " << testId << " operatorId: " << operatorId << " createdId: " << createdId; m_testId = testId; m_exerciseId = exerciseId; m_operatorId = operatorId; m_createdId = createdId; // initialize DDS m_ddsConnector.reset(new DDSConnector::DdsConnector(domainId, IDL::PartitionType::Exercise, exerciseId)); if (!m_ddsConnector->init()) return false; m_ddsConnector->initPublisherSubscriber(); // Access to FTC DEBUG_LOG(m_logger) << "create ftc"; m_ftc = OperatorManagement::Interface::Ftc::create(m_ddsConnector); DEBUG_LOG(m_logger) << "init ftc"; bool bRet = m_ftc->enableAll(); DEBUG_LOG(m_logger) << "init end - bRet = " << bRet; // Access to DTC DEBUG_LOG(m_logger) << "create dtc"; m_dtc = OperatorManagement::Interface::Dtc::create(m_ddsConnector); DEBUG_LOG(m_logger) << "init dtc"; bRet = m_dtc->enableAll(); DEBUG_LOG(m_logger) << "init end - bRet = " << bRet; connect(m_dtc.get(), &Interface::Dtc::userChanged, this, &OpmTests::onReceiveOperatorChanged); connect(m_dtc.get(), &Interface::Dtc::groupChanged, this, &OpmTests::onReceiveGroupChanged); return bRet; } // end of init() //----------------------------------------------------------------------------- void OpmTests::startTest() { INFO_LOG(m_logger) << "startTest() exerciseId:" << m_exerciseId << " testId:" << m_testId << " operatorId:" << m_operatorId ; if (m_testId <= 1) QTimer::singleShot(0, this, &OpmTests::tryCreate); else if (m_testId == 2) QTimer::singleShot(0, this, &OpmTests::tryModify); else if (m_testId == 3) QTimer::singleShot(0, this, &OpmTests::tryRemove); else if (m_testId == 4) QTimer::singleShot(0, this, &OpmTests::tryLogin); else if (m_testId == 5) QTimer::singleShot(0, this, &OpmTests::tryLogout); else if (m_testId == 6) QTimer::singleShot(0, this, &OpmTests::tryLoginForce); else if (m_testId == 7) QTimer::singleShot(0, this, &OpmTests::tryGetOperatorsByOperatorRight); else if (m_testId == 10) QTimer::singleShot(0, this, &OpmTests::tryGetAllOperators); else if (m_testId == 11) QTimer::singleShot(0, this, &OpmTests::tryGetOperator); QTimer::singleShot(0, qApp, &QCoreApplication::quit); } // end of startTest() //----------------------------------------------------------------------------- void OpmTests::tryCreate() { m_eTestStep = eCreate; OperatorCreateModifyRequest data; getOperatorCreateRequestData( data); INFO_LOG(m_logger) << "trying to send OperatorCreateRequest"; if (!m_ftc->operatorCreateModifyRequest(data, this)) { ERROR_LOG(m_logger) << "Error requesting create data"; failExit(); } QTest::qWait(1000); } // end of tryCreate() //----------------------------------------------------------------------------- void OpmTests::tryModify() { m_eTestStep = eModify; OperatorCreateModifyRequest data; getOperatorModifyRequestData(data); INFO_LOG(m_logger) << "trying to send OperatorCreateModifyRequest"; if (!m_ftc->operatorCreateModifyRequest(data, this)) { ERROR_LOG(m_logger) << "Error requesting modify data"; failExit(); } QTest::qWait(1000); } // end of tryModify() //----------------------------------------------------------------------------- void OpmTests::tryRemove() { m_eTestStep = eRemove; OperatorRemoveRequest data; data.id(m_operatorId); INFO_LOG(m_logger) << "trying to send remove request"; if (!m_ftc->operatorRemoveRequest(data, this)) { ERROR_LOG(m_logger) << "Error requesting remove data"; failExit(); } QTest::qWait(1000); } // end of tryRemove() //----------------------------------------------------------------------------- void OpmTests::tryLogin() { NOTICE_LOG(m_logger) << "tryLogin"; m_eTestStep = eLogin; OperatorLoginRequest data; data.id(m_operatorId); std::string pass("ctc"); const QByteArray ba(QByteArray::fromRawData(pass.c_str(), pass.size())); //pass = QCryptographicHash::hash(ba, QCryptographicHash::Md5).toHex().data(); data.password(pass); data.workstationIp(-1); data.workstationId(301); data.force(false); NOTICE_LOG(m_logger) << "trying to send OperatorLoginRequest" << ", operatorId:" << m_operatorId << ", pass:" << pass << ", workstationIp:" << data.workstationIp() << ", workstationId:" << data.workstationId() << ", force:" << data.force(); if (!m_ftc->operatorLoginRequest(data, this)) { ERROR_LOG(m_logger) << "Error login operator id " << m_operatorId << " on workstation 1"; failExit(); } QTest::qWait(1000); } //----------------------------------------------------------------------------- void OpmTests::tryLoginForce() { m_eTestStep = eLogin; OperatorLoginRequest data; data.id(m_operatorId); std::string pass("password"); const QByteArray ba(QByteArray::fromRawData(pass.c_str(), pass.size())); pass = QCryptographicHash::hash(ba, QCryptographicHash::Md5).toHex().data(); data.password(pass); data.workstationIp(-1); data.workstationId(4); data.force(true); INFO_LOG(m_logger) << "trying to send OperatorLoginRequest"; if (!m_ftc->operatorLoginRequest(data, this)) { ERROR_LOG(m_logger) << "Error login operator id " << m_operatorId << " on workstation 1"; failExit(); } QTest::qWait(1000); } //----------------------------------------------------------------------------- void OpmTests::tryLogout() { m_eTestStep = eLogout; OperatorLogoutRequest data; data.id(m_operatorId); INFO_LOG(m_logger) << "trying to send OperatorLogoutRequest"; if (!m_ftc->operatorLogoutRequest(data, this)) { ERROR_LOG(m_logger) << "Error logout operator id " << m_operatorId; failExit(); } QTest::qWait(1000); } void OpmTests::tryGetOperatorsByOperatorRight() { m_eTestStep = eGetOperatoryByOperatorRight; int operatorRight = OperatorRights::CContaminationsEdit; m_dtc->waitForHistoricalDataUser(); INFO_LOG(m_logger) << "trying to send get operators by operatorRight"; QVector operators = m_dtc->getOperatorsByOperatorRight(operatorRight); if (operators.empty()) { ERROR_LOG(m_logger) << "Error no operators for operatorRight: " << operatorRight; failExit(); } else { for (int operatorId : operators) { INFO_LOG(m_logger) << "Operator " << operatorId << " found."; } } QTest::qWait(1000); } //----------------------------------------------------------------------------- void OpmTests::tryGetAllOperators() { m_eTestStep = eGetAllOperators; DEBUG_LOG(m_logger) << "trying to get all operators"; m_dtc->waitForHistoricalDataUser(); const QMap opMap = m_dtc->users(); if(opMap.isEmpty()) { ERROR_LOG(m_logger) << "Error requesting all operator data"; failExit(); } else { INFO_LOG(m_logger) << "Amout of existing operators = " << opMap.size(); m_operatorId = opMap.firstKey(); for(auto it = opMap.begin(); it != opMap.end(); ++it) { INFO_LOG(m_logger) << "opm(" << it.key() << ") :" << " name:" << it.value().name() // << " password:" << it.value().password() // << " effectiveACLs:" << it.value().effectiveAcls() ; } } QTest::qWait(1000); } // end of tryGetAllOperators() //----------------------------------------------------------------------------- void OpmTests::tryGetOperator() { DEBUG_LOG(m_logger) << "trying to get operator with id:" << m_operatorId; if(m_testId > 1) { m_eTestStep = eGetOperator; } if(m_operatorId <= 0) { ERROR_LOG(m_logger) << "invalid operatorId - test aborted."; failExit(); } m_dtc->waitForHistoricalDataUser(); OperatorManagement::User op = m_dtc->user(m_operatorId); if(op.id() <= 0) { ERROR_LOG(m_logger) << "Error receiving data for operatorId " << m_operatorId << " returnVal:" << op.id(); failExit(); } else { INFO_LOG(m_logger) << "opm(" << op.id() << ") :" << " name: " << op.name() // << " password: " << data.password() // << " effectiveACLs:" << it.value().effectiveAcls() ; } if(m_testId == 0) { if(m_eTestStep == eCreate) { if(! onCompareCreatedData(op)) { ERROR_LOG(m_logger) << "Compare created data failed!"; failExit(); } else { //QTimer::singleShot(0, this, &OpmTests::tryChangeName); // not implemented... return; } } else if(m_eTestStep == eModify) { if(! onCompareModifiedData(op)) { ERROR_LOG(m_logger) << "Compare modified data failed!"; failExit(); } else { QTimer::singleShot(0, this, &OpmTests::tryRemove); return; } } } else QTimer::singleShot(0, qApp, &QCoreApplication::quit); QTest::qWait(1000); } // end of tryGetOperator() //----------------------------------------------------------------------------- void OpmTests::onOperatorCreateModifyReply(const Core::FunctionReplyObject &replyObject) { INFO_LOG(m_logger) << "Got create/modifyReply with id " << replyObject.id() << " and errorString " << replyObject.translated(); m_operatorId = replyObject.id(); if(replyObject.error()) { ERROR_LOG(m_logger) << "Error requesting create/modify data"; failExit(); return; } else { DEBUG_LOG(m_logger) << "Requesting create/modify data ok. Got id:" << replyObject.id() << " ... ok."; } if(m_testId == 0) { if(m_eTestStep == eCreate) QTimer::singleShot(0, this, &OpmTests::tryModify); else if (m_eTestStep == eModify) QTimer::singleShot(0, this, &OpmTests::tryGetOperator); } else QTimer::singleShot(0, qApp, &QCoreApplication::quit); } // end of onOperatorCreateModifyReply() //----------------------------------------------------------------------------- void OpmTests::onOperatorRemoveReply(const Core::FunctionReplyObject &replyObject) { INFO_LOG(m_logger) << "Got removeReply with id " << replyObject.id() << " and errorString " << replyObject.translated(); if(replyObject.error()) { ERROR_LOG(m_logger) << "Error requesting remove data for id:" << replyObject.id(); failExit(); return; } else { DEBUG_LOG(m_logger) << "Requesting remove data for id:" << replyObject.id() << " ... ok."; } if(m_testId == 0) { INFO_LOG(m_logger) << "***** FTC/DTC test successfully finished. (used id:" << replyObject.id() << ") *****"; // ansonsten verheriger Ausstieg mit Errorlog. } QTimer::singleShot(0, qApp, &QCoreApplication::quit); } // end of onOperatorRemoveReply() //----------------------------------------------------------------------------- void OpmTests::onOperatorLoginReply(const Core::FunctionReplyObject &replyObject) { DEBUG_LOG(m_logger) << "Got loginReply with id " << replyObject.id() << " and errorString " << replyObject.translated(); if(replyObject.error()) { ERROR_LOG(m_logger) << "Error requesting login for id:" << replyObject.id(); failExit(); return; } QTimer::singleShot(0, qApp, &QCoreApplication::quit); } //----------------------------------------------------------------------------- void OpmTests::onOperatorLogoutReply(const Core::FunctionReplyObject &replyObject) { INFO_LOG(m_logger) << "Got logoutReply with id " << replyObject.id() << " and errorString " << replyObject.translated(); if(replyObject.error()) { ERROR_LOG(m_logger) << "Error requesting logout for id:" << replyObject.id(); failExit(); return; } QTimer::singleShot(0, qApp, &QCoreApplication::quit); } //----------------------------------------------------------------------------- void OpmTests::onReceiveOperatorChanged(const User &data, DataModification modType) { // Check signal/slot connection if (m_operatorId == data.id()) { TRACE_LOG(m_logger) << "Check signal/slot connection for DTC. Data for id = " << data.id() << " received"; TRACE_LOG(m_logger) << "ModificationType: = " << (int)modType << " received"; } } // end of onReceiveOperatorData() //----------------------------------------------------------------------------- void OpmTests::onReceiveGroupChanged(const Group &data, DataModification modType) { // Check signal/slot connection // if (m_operatorId == data.id()) // { TRACE_LOG(m_logger) << "Check signal/slot connection for DTC. Data for id = " << data.id() << " received"; TRACE_LOG(m_logger) << "ModificationType: = " << (int)modType << " received"; // } } // end of onReceiveOperatorData() //----------------------------------------------------------------------------- void OpmTests::getOperatorCreateRequestData(OperatorCreateModifyRequest &data) const { time_t currentTime = time(nullptr); if(m_testId == 0) currentTime = currentTime/10; data.name(std::string("TestOperatorCreate" + std::to_string(currentTime))); std::string pass("password"); const QByteArray ba(QByteArray::fromRawData(pass.c_str(), pass.size())); pass = QCryptographicHash::hash(ba, QCryptographicHash::Md5).toHex().data(); data.password(pass); data.description(std::string("Description")); // Todo: test anpassen PartiesByExercise pbe; pbe.exerciseId(0); pbe.visibleParties().set({0,1,2,3,4,5,6,7,8,9,10,11,12}); data.visiblePartiesByExercise().push_back(pbe); data.rolePresetId(1001); } // end of getOperatorCreateRequestData() //----------------------------------------------------------------------------- void OpmTests::getOperatorModifyRequestData(OperatorCreateModifyRequest &data) const { time_t currentTime = time(nullptr); if(m_testId == 0) currentTime = currentTime/10; data.id(m_operatorId); // data.name(std::string("Operator"+std::to_string(m_operatorId))); // only changing the description data.description(std::string("Description" + std::to_string(m_operatorId))); } // end of getOperatorChangeNameRequestData() //----------------------------------------------------------------------------- bool OpmTests::onCompareCreatedData(User &data) { DEBUG_LOG(m_logger) << "start onCompareCreatedData() for id :" << data.id(); OperatorCreateModifyRequest createData; getOperatorCreateRequestData(createData); if (createData.name() != data.name()) return false; // if(createData.password() != data.password()) // return false; if (data.description() != createData.description()) return false; DEBUG_LOG(m_logger) << "successfull finished onCompareCreatedData() for id :" << data.id(); return true; } // end of onCompareCreatedData() //----------------------------------------------------------------------------- bool OpmTests::onCompareModifiedData(User &data) { DEBUG_LOG(m_logger) << "start onCompareModifiedData() for id :" << data.id(); OperatorCreateModifyRequest modifyData; getOperatorModifyRequestData(modifyData); if (modifyData.id() != data.id()) return false; if (modifyData.name() != data.name()) return false; if (data.description() != modifyData.description()) return false; DEBUG_LOG(m_logger) << "successfully finished onCompareModifiedData() for id :" << data.id(); return true; } // end of onCompareModifiedData() // ------------------------------------------------------------------------------ void OpmTests::failExit() { m_bTestsSuccessful = false; QTimer::singleShot(0, qApp, &QCoreApplication::quit); } //----------------------------------------------------------------------------- class CmdLineParser : public LtsCommandLineParser::LtsCommandLineParser { public: CmdLineParser() : LtsCommandLineParser() { addOption( { QStringList() << QLatin1String("t") << QLatin1String("testId") , QLatin1String("TestId.") , QLatin1String("Default: 0 (all FTC/DTC tests), 1 create, 2 modify, 3 remove, 10 getAllOperators, 11 getOperator") , QLatin1String("0") } ); addOption( { QStringList() << QLatin1String("o") << QLatin1String("operatorId") , QLatin1String("OperatorId.") , QLatin1String("Used for tests 2, 3 & 11 only to select the operator") , QLatin1String("0") } ); addOption( { QStringList() << QLatin1String("c") << QLatin1String("createdId") , QLatin1String("CreatedId.") , QLatin1String("Used to create unique names for operators in test 1") , QLatin1String("0") } ); } int testId() const { return isSet(QLatin1String("t")) ? value(QLatin1String("t")).toInt() : -1; } int operatorId() const { return isSet(QLatin1String("o")) ? value(QLatin1String("o")).toInt() : -1; } int createdId() const { return isSet(QLatin1String("c")) ? value(QLatin1String("c")).toInt() : -1; } }; //----------------------------------------------------------------------------- int main(int argc, char **argv) { NGLCoreApplication app(argc, argv); app.createLogger("OPM", "log4cxx_opm.props"); int iRet = -1; CmdLineParser options; if (!options.init()) return iRet; std::unique_ptr OpmTests(new OperatorManagement::OpmTests); if(!OpmTests->init(options.domainId(), options.exerciseId(), options.testId(), options.operatorId(), options.createdId() )) { return iRet; } QTimer::singleShot(0, OpmTests.get(), &OperatorManagement::OpmTests::startTest); STARTUP_LOG(app.baseLogger()); app.exec(); iRet = OpmTests->testsSuccessful() ? 0 : 1; fprintf(stderr, "%d\n", iRet); DEBUG_LOG(app.baseLogger()) << "** TEST result = " << iRet; SHUTDOWN_LOG(app.baseLogger()); return iRet; } //-----------------------------------------------------------------------------