/** * whg Function Topic test executable * $Id$ * $HeadURL$ * $Author$ * Created by hjl, SLSE, 2016-04-18 */ #include "whg_tests.h" #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace WarehouseGuez; using namespace WsBiteInterface; //----------------------------------------------------------------------------- WhgTests::WhgTests(QObject *parent) : QObject(parent) , m_testId(0) , m_equipmentTypeWhId(-1) , m_injuryModelWhId(0) , m_equipNo(0) , m_boxId(0) , m_eTestStep(eUnknown) , m_bTestsSuccessful(true) { qRegisterMetaType("Core::FunctionReply"); } // end of ctor() //----------------------------------------------------------------------------- WhgTests::~WhgTests() { } // end of dtor() //----------------------------------------------------------------------------- bool WhgTests::init(int domainId, int testId, int eqtTypeWhId, int injuryModelWhId, int equipNo, int boxId) { INFO_LOG(m_logger) << "WhgTests::init() " << " domainId:" << domainId << " testId: " << testId << " equipmentTypeWhId: " << eqtTypeWhId << " equipNo: " << equipNo << " boxId: " << boxId; ; m_testId = testId; m_equipmentTypeWhId = eqtTypeWhId; m_injuryModelWhId = injuryModelWhId; m_equipNo = equipNo; m_boxId = boxId; // initialize DDS m_ddsConnector.reset(new DDSConnector::DdsConnector(domainId, IDL::PartitionType::Global, 0)); if (!m_ddsConnector->init()) return false; m_ddsConnector->initPublisherSubscriber(); // Access to FTC DEBUG_LOG(m_logger) << "create ftc"; m_ftc = WarehouseGuez::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 = WarehouseGuez::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_wsClient, &QWebSocket::connected, this, &WhgTests::onConnected); connect(&m_wsClient, &QWebSocket::textMessageReceived, this, &WhgTests::onTextMessageReceived); int port = NGL::Connections::port(NGL::Connections::eConnection::LVS); ERROR_LOG(m_logger) << "Port:_ " << port; //QUrl url(QStringLiteral("ws://%1:%2").arg(QStringLiteral("localhost"), QString::number(9094))); QUrl url(QStringLiteral("ws://%1:%2").arg(QStringLiteral("localhost"), QString::number(port))); m_wsClient.open(url); return bRet; } // end of init() void WhgTests::onConnected() { ERROR_LOG(m_logger) << "wsClient connected"; startTest(); } void WhgTests::onTextMessageReceived(const QString &msg) { INFO_LOG(m_logger) << "new text message: " << msg; //QTimer::singleShot(0, qApp, &QCoreApplication::quit); } //----------------------------------------------------------------------------- void WhgTests::startTest() { INFO_LOG(m_logger) << "startTest()" << " testId:" << m_testId << " eqtTypeWhId:" << m_equipmentTypeWhId ; connect(m_dtc.get(), &Interface::Dtc::equipmentTypeWhChanged, this, &WhgTests::onReceiveEqtTypeChanged); if (m_testId <= 1) QTimer::singleShot(0, this, &WhgTests::tryCreate); else if (m_testId == 2) QTimer::singleShot(0, this, &WhgTests::tryModify); else if (m_testId == 3) QTimer::singleShot(0, this, &WhgTests::tryRemove); else if (m_testId == 4) QTimer::singleShot(0, this, &WhgTests::tryCreateInjuryModel); else if (m_testId == 5) QTimer::singleShot(0, this, &WhgTests::tryModifyInjuryModel); else if (m_testId == 6) QTimer::singleShot(0, this, &WhgTests::tryRemoveInjuryModel); else if (m_testId == 10) QTimer::singleShot(0, this, &WhgTests::tryGetAllEquipmentTypesWh); else if (m_testId == 11) QTimer::singleShot(0, this, &WhgTests::tryGetEquipmentTypeWh); else if (m_testId == 12) QTimer::singleShot(0, this, &WhgTests::tryGetBiteData); else { ERROR_LOG(m_logger) << "Unknown test id " << m_testId; failExit(); } //QTimer::singleShot(0, qApp, &QCoreApplication::quit); } // end of startTest() //----------------------------------------------------------------------------- void WhgTests::tryCreate() { m_eTestStep = eCreate; EquipmentTypeWh data; getEqtTypeWhCreateRequestData(data); INFO_LOG(m_logger) << "trying to send create EquipmentTypeWh Request"; if (!m_ftc->modifyEquipmentTypeWhRequest(data, this)) { ERROR_LOG(m_logger) << "Error requesting create data"; failExit(); } } // end of tryCreate() //----------------------------------------------------------------------------- void WhgTests::tryModify() { m_eTestStep = eModify; EquipmentTypeWh data; getEqtTypeWhModifyRequestData(data); INFO_LOG(m_logger) << "trying to send ModifyEquipementTypeWhRequest for eqtTypeWhId:" << data.id(); if (!m_ftc->modifyEquipmentTypeWhRequest(data, this)) { ERROR_LOG(m_logger) << "Error requesting modify data for eqtTypeWhId:" << data.id(); failExit(); } } // end of tryModify() //----------------------------------------------------------------------------- void WhgTests::tryRemove() { m_eTestStep = eRemove; RemoveEquipmentTypeWhRequest data; data.id(m_equipmentTypeWhId); INFO_LOG(m_logger) << "trying to send remove request for eqtTypeWhId:" << data.id(); if (!m_ftc->removeEquipmentTypeWhRequest(data, this)) { ERROR_LOG(m_logger) << "Error requesting remove data for eqtTypeWhId:" << data.id(); failExit(); } } // end of tryRemove() //----------------------------------------------------------------------------- void WhgTests::tryGetAllEquipmentTypesWh() { m_eTestStep = eGetAllEquipmentTypesWh; DEBUG_LOG(m_logger) << "trying to get all eqtTypesWh"; m_dtc->waitForHistoricalDataEquipmentTypeWh(); const QMap map = m_dtc->equipmentTypeWhs(); if (map.isEmpty()) { ERROR_LOG(m_logger) << "Error requesting all eqtTypesWh data"; QTimer::singleShot(0, qApp, &QCoreApplication::quit); return; } INFO_LOG(m_logger) << "Amout of existing eqtTypesWh:" << map.size(); m_equipmentTypeWhId = map.firstKey(); for (auto it = map.begin(); it != map.end(); ++it) { INFO_LOG(m_logger) << "whg(" << it.key() << ") :" << " name:" << it.value().name() << " minEquipNo:" << it.value().minEquipNo() << " maxEquipNo:" << it.value().maxEquipNo() << " vbAgdus:" << it.value().vbAgdusId(); } QTimer::singleShot(0, qApp, &QCoreApplication::quit); } // end of tryGetAllEquipmentTypesWh() //----------------------------------------------------------------------------- void WhgTests::tryGetEquipmentTypeWh() { DEBUG_LOG(m_logger) << "trying to get eqtTypeWh with id:" << m_equipmentTypeWhId; if (m_testId > 1) m_eTestStep = eGetEquipmentTypeWh; if (m_equipmentTypeWhId < 0) { ERROR_LOG(m_logger) << "invalid eqtTypeWhId - test aborted."; failExit(); } m_dtc->waitForHistoricalDataEquipmentTypeWh(); WarehouseGuez::EquipmentTypeWh data = m_dtc->equipmentTypeWh(m_equipmentTypeWhId); if (data.id() < 0) { ERROR_LOG(m_logger) << "Error receiving data for equipmentTypeId " << m_equipmentTypeWhId << " returnVal:" << data.id(); failExit(); } else { INFO_LOG(m_logger) << "whg(" << data.id() << ") :" << " name:" << data.name() << " minEquipNo:" << data.minEquipNo() << " maxEquipNo:" << data.maxEquipNo() << " vbAgdus:" << data.vbAgdusId(); } if (m_testId == 0) { if (m_eTestStep == eCreate) { if (! onCompareCreatedData(data)) { ERROR_LOG(m_logger) << "Compare created data failed!"; failExit(); } else { QTimer::singleShot(0, this, &WhgTests::tryModify); return; } } else if (m_eTestStep == eModify) { if (! onCompareModifiedData(data)) { ERROR_LOG(m_logger) << "Compare modified data failed!"; failExit(); } else { QTimer::singleShot(0, this, &WhgTests::tryRemove); return; } } } else QTimer::singleShot(0, qApp, &QCoreApplication::quit); } // end of tryGetEquipmentTypeWh() //----------------------------------------------------------------------------- void WhgTests::onModifyEquipmentTypeWhReply(const Core::FunctionReplyObject &replyObject) { INFO_LOG(m_logger) << "Got onModifyEquipmentTypeWhReply with id " << replyObject.id() << " and errorString " << replyObject.translated(); m_equipmentTypeWhId = replyObject.id(); if (replyObject.error()) { ERROR_LOG(m_logger) << "Error requesting create data"; failExit(); return; } else INFO_LOG(m_logger) << "Requesting create data ok. Got new id:" << replyObject.id() << " ... ok."; if (m_testId == 0) // QTimer::singleShot(0, this, &WhgTests::tryModify())); QTimer::singleShot(0, this, &WhgTests::tryGetEquipmentTypeWh); else QTimer::singleShot(0, qApp, &QCoreApplication::quit); } // end of onModifyEquipmentTypeWhReply() //----------------------------------------------------------------------------- void WhgTests::onRemoveEquipmentTypeWhReply(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 eqtTypeWhId:" << replyObject.id(); failExit(); return; } else DEBUG_LOG(m_logger) << "Requesting remove data for eqtTypeWhId:" << replyObject.id() << " ... ok."; QTimer::singleShot(0, qApp, &QCoreApplication::quit); } // end of onRemoveEquipmentTypeWhReply() //----------------------------------------------------------------------------- void WhgTests::onModifyInjuryModelAgdusWhReply(const Core::FunctionReplyObject &replyObject) { INFO_LOG(m_logger) << "Got onModifyInjuryModelAgdusWhReply with id " << replyObject.id() << " and errorString " << replyObject.translated(); m_injuryModelWhId = replyObject.id(); if (replyObject.error()) { ERROR_LOG(m_logger) << "Error requesting create data"; failExit(); return; } else DEBUG_LOG(m_logger) << "Requesting create data ok. Got new id:" << replyObject.id() << " ... ok."; QTimer::singleShot(0, qApp, &QCoreApplication::quit); } // end of onModifyInjuryModelAgdusWhReply() //----------------------------------------------------------------------------- void WhgTests::onRemoveInjuryModelAgdusWhReply(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 injuryModelWhId:" << replyObject.id(); failExit(); return; } else DEBUG_LOG(m_logger) << "Requesting remove data for injuryModelWhId:" << replyObject.id() << " ... ok."; QTimer::singleShot(0, qApp, &QCoreApplication::quit); } // end of onRemoveInjuryModelAgdusWhReply() //----------------------------------------------------------------------------- void WhgTests::onReceiveEqtTypeChanged(const EquipmentTypeWh &data, DataModification modType) { INFO_LOG(m_logger) << "onReceiveEqtTypeChanged() eqtTypeWhId:" << data.id() << " modificationType: = " << (int)modType<< " received"; switch (modType) { case DataModification::eAdded: INFO_LOG(m_logger) << "*** EquipmentTypeWh added received. eqtTypeWhId:" << data.id(); break; case DataModification::eModified: INFO_LOG(m_logger) << "*** EquipmentTypeWh modified received. eqtTypeWhId:" << data.id(); break; case DataModification::eRemoved: INFO_LOG(m_logger) << "*** EquipmentTypeWh removed received. eqtTypeWhId:" << data.id(); break; default: INFO_LOG(m_logger) << "DataModification::" << modType << " not implemented"; } } // end of onReceiveEqtTypeChanged() //----------------------------------------------------------------------------- void WhgTests::getEqtTypeWhCreateRequestData(EquipmentTypeWh &data) { data.id(); // -1 for add, 0 is a valid id // 0 should be valid though?!? data.name("TestEqtTypeCreate"); data.minEquipNo(1000); data.maxEquipNo(3000); data.vbAgdusId(1); data.configurationAgdusHV(ConfigurationAgdusHV_def::AgdusHWNoHelmetNoWest); } // end of getEqtTypeWhCreateRequestData() //----------------------------------------------------------------------------- void WhgTests::getEqtTypeWhModifyRequestData(EquipmentTypeWh &data) { data.id(m_equipmentTypeWhId); data.name("TestEqtTypeModify"); data.maxEquipNo(3100); data.minEquipNo(1100); data.vbAgdusId(2); data.configurationAgdusHV(ConfigurationAgdusHV_def::AgdusHWWithHelmetWithVest); } // end of getEqtTypeWhModifyRequestData() //----------------------------------------------------------------------------- void WhgTests::getInjuryModelWhCreateRequestData(InjuryModelAgdusWh &data) { data.name("TestInjuryModelCreate"); data.version("1.0"); data.description("This is the description"); data.id(0); } // end of getInjuryModelWhCreateRequestData() //----------------------------------------------------------------------------- void WhgTests::getInjuryModelWhModifyRequestData(InjuryModelAgdusWh &data) { data.id(m_injuryModelWhId); data.name("TestInjuryModelWhModify"); data.version("2.0"); data.description("Description Modify"); } // end of getInjuryModelWhModifyRequestData() //----------------------------------------------------------------------------- bool WhgTests::onCompareCreatedData(EquipmentTypeWh &data) { DEBUG_LOG(m_logger) << "start onCompareCreatedData() for id :" << data.id(); // Vergleicht die zurueckgelesen Datan mit den statischen create Daten EquipmentTypeWh createData; getEqtTypeWhCreateRequestData(createData); if (createData.name() != data.name()) return false; if (createData.minEquipNo() != data.minEquipNo()) return false; if (createData.maxEquipNo() != data.maxEquipNo()) return false; if (createData.vbAgdusId() != data.vbAgdusId()) return false; DEBUG_LOG(m_logger) << "successfull finished onCompareCreatedData() for id :" << data.id(); return true; } // end of onCompareCreatedData() bool WhgTests::onCompareCreatedData(InjuryModelAgdusWh &data) { DEBUG_LOG(m_logger) << "start onCompareCreatedData() for id :" << data.id(); // Vergleicht die zurueckgelesen Datan mit den statischen create Daten InjuryModelAgdusWh createData; getInjuryModelWhCreateRequestData(createData); if (createData.name() != data.name()) return false; if (createData.version() != data.version()) return false; if (createData.description() != data.description()) return false; DEBUG_LOG(m_logger) << "successfull finished onCompareCreatedData() for id :" << data.id(); return true; } // end of onCompareCreatedData() //----------------------------------------------------------------------------- bool WhgTests::onCompareModifiedData(EquipmentTypeWh &data) { DEBUG_LOG(m_logger) << "start onCompareModifiedData() for id :" << data.id(); EquipmentTypeWh modifyData; getEqtTypeWhModifyRequestData(modifyData); if (modifyData.id() != data.id()) return false; if (modifyData.name() != data.name()) return false; if (modifyData.minEquipNo() != data.minEquipNo()) return false; if (modifyData.maxEquipNo() != data.maxEquipNo()) return false; if (modifyData.vbAgdusId() != data.vbAgdusId()) return false; DEBUG_LOG(m_logger) << "successfully finished onCompareModifiedData() for id :" << data.id(); return true; } // end of onCompareModifiedData() //----------------------------------------------------------------------------- bool WhgTests::onCompareModifiedData(InjuryModelAgdusWh &data) { DEBUG_LOG(m_logger) << "start onCompareModifiedData() for id :" << data.id(); InjuryModelAgdusWh modifyData; getInjuryModelWhModifyRequestData(modifyData); if (modifyData.id() != data.id()) return false; if (modifyData.name() != data.name()) return false; if (modifyData.version() != data.version()) return false; if (modifyData.description() != data.description()) return false; DEBUG_LOG(m_logger) << "successfull finished onCompareModifiedData() for id :" << data.id(); return true; } // end of onCompareModifiedData() //----------------------------------------------------------------------------- void WhgTests::tryCreateInjuryModel() { m_eTestStep = eCreateInjuryModel; InjuryModelAgdusWh data; getInjuryModelWhCreateRequestData(data); INFO_LOG(m_logger) << "trying to send modifyInjuryModelAgdusWhRequest with id = 0 (create)"; if (!m_ftc->modifyInjuryModelAgdusWhRequest(data, this)) { ERROR_LOG(m_logger) << "Error requesting create data"; failExit(); } } // end of tryCreateInjuryModel() //----------------------------------------------------------------------------- void WhgTests::tryModifyInjuryModel() { m_eTestStep = eModifyInjuryModel; InjuryModelAgdusWh data; getInjuryModelWhModifyRequestData(data); ERROR_LOG(m_logger) << "trying to send modifyInjuryModelAgdusWhRequest for injuryModelWhId:" << data.id(); if (!m_ftc->modifyInjuryModelAgdusWhRequest(data, this)) { ERROR_LOG(m_logger) << "Error requesting modify data for injuryModelWhId:" << data.id(); failExit(); } } // end of tryModifyInjuryModel() //----------------------------------------------------------------------------- void WhgTests::tryRemoveInjuryModel() { m_eTestStep = eRemoveInjuryModel; Core::RemoveElementRequest data; data.id(m_injuryModelWhId); INFO_LOG(m_logger) << "trying to send remove request for injuryModelWhId:" << data.id(); if (!m_ftc->removeInjuryModelAgdusWhRequest(data, this)) { ERROR_LOG(m_logger) << "Error requesting remove data for injuryModelWhId:" << data.id(); failExit(); } } // end of tryRemoveInjuryModel() void WhgTests::tryGetBiteData() { QJsonObject data; data.insert(JsonKeys::EQUIP_NO(), m_equipNo); data.insert(JsonKeys::BOX(), m_boxId); QJsonObject obj; obj.insert(JsonKeys::UUID_(), QStringLiteral("7737")); obj.insert(JsonKeys::TYPE(), QStringLiteral("getBiteData")); obj.insert(JsonKeys::DATA(), data); m_wsClient.sendTextMessage(QString::fromUtf8(QJsonDocument(obj).toJson())); } //----------------------------------------------------------------------------- void WhgTests::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 getAllEqtTypesWh, 11 getEqtTypeWh") , QLatin1String("0") }); addOption({ QStringList() << QLatin1String("w") << QLatin1String("eqtTypeWhId") , QLatin1String("EquipmentTypeWhId.") , QLatin1String("Used for tests 2, 3 & 11 only to select the eqtTypeWh") , QLatin1String("0") }); addOption({ QStringList() << QLatin1String("i") << QLatin1String("injuryModelWhId") , QLatin1String("InjuryModelWhId.") , QLatin1String("Used for tests 5 and 6 & only to select the injuryModelWh") , QLatin1String("0") }); addOption({ QStringList() << QLatin1String("e") << QLatin1String("equipNo") , QLatin1String("EquipNo") , QLatin1String("Used for test 12") , QLatin1String("0") }); addOption({ QStringList() << QLatin1String("b") << QLatin1String("boxId") , QLatin1String("BoxId") , QLatin1String("Used for test 12") , QLatin1String("0") }); } int testId() const { return isSet(QLatin1String("t")) ? value(QLatin1String("t")).toInt() : -1; } int eqtTypeWhId() const { return isSet(QLatin1String("w")) ? value(QLatin1String("w")).toInt() : -1; } int injuryModelWhId() const { return isSet(QLatin1String("i")) ? value(QLatin1String("i")).toInt() : -1; } int equipNo() const { return isSet(QLatin1String("e")) ? value(QLatin1String("e")).toInt() : -1; } int boxId() const { return isSet(QLatin1String("b")) ? value(QLatin1String("b")).toInt() : -1; } }; //----------------------------------------------------------------------------- int main(int argc, char **argv) { NGLCoreApplication app(argc, argv); app.createLogger("WHG", 0); int iRet = -1; CmdLineParser options; if (!options.init()) return iRet; std::unique_ptr WhgTests(new WarehouseGuez::WhgTests); if (!WhgTests->init(options.domainId(), options.testId(), options.eqtTypeWhId(), options.injuryModelWhId(), options.equipNo(), options.boxId())) return iRet; // QTimer::singleShot(2000, WhgTests.get(), &WarehouseGuez::WhgTests::startTest); STARTUP_LOG(app.baseLogger()); app.exec(); iRet = WhgTests->testsSuccessful() ? 0 : 1; fprintf(stderr, "%d\n", iRet); DEBUG_LOG(app.baseLogger()) << "** TEST result = " << iRet; SHUTDOWN_LOG(app.baseLogger()); return iRet; } //-----------------------------------------------------------------------------