/**************************************************************************** * * * This file is part of guh. * * * * Guh is free software: you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation, version 2 of the License. * * * * Guh is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with guh. If not, see . * * * ***************************************************************************/ #include "guhtestbase.h" #include "guhcore.h" #include "devicemanager.h" #include "mocktcpserver.h" #include #include #include #include #include #include #include class TestRules: public GuhTestBase { Q_OBJECT private slots: void addRules_data(); void addRules(); void loadStoreConfig(); }; void TestRules::addRules_data() { QVariantMap validAction; validAction.insert("actionTypeId", mockActionIdNoParams); validAction.insert("deviceId", m_mockDeviceId); validAction.insert("params", QVariantList()); QVariantMap invalidAction; invalidAction.insert("actionTypeId", ActionTypeId()); invalidAction.insert("deviceId", m_mockDeviceId); invalidAction.insert("params", QVariantList()); QVariantMap validEventDescriptor1; validEventDescriptor1.insert("eventTypeId", mockEvent1Id); validEventDescriptor1.insert("deviceId", m_mockDeviceId); validEventDescriptor1.insert("paramDescriptors", QVariantList()); QVariantMap validEventDescriptor2; validEventDescriptor2.insert("eventTypeId", mockEvent2Id); validEventDescriptor2.insert("deviceId", m_mockDeviceId); QVariantList params; QVariantMap param1; param1.insert("mockParamInt", 3); params.append(param1); validEventDescriptor2.insert("paramDescriptors", param1); QVariantList eventDescriptorList; eventDescriptorList.append(validEventDescriptor1); eventDescriptorList.append(validEventDescriptor2); QVariantMap invalidEventDescriptor; invalidEventDescriptor.insert("eventTypeId", mockEvent1Id); invalidEventDescriptor.insert("deviceId", DeviceId()); invalidEventDescriptor.insert("paramDescriptors", QVariantList()); QTest::addColumn("action1"); QTest::addColumn("eventDescriptor"); QTest::addColumn("eventDescriptorList"); QTest::addColumn("success"); QTest::newRow("valid rule. 1 EventDescriptor, 1 Action") << validAction << validEventDescriptor1 << QVariantList() << true; QTest::newRow("valid rule. 2 EventDescriptors, 1 Action") << validAction << QVariantMap() << eventDescriptorList << true; QTest::newRow("invalid rule: eventDescriptor and eventDescriptorList used") << validAction << validEventDescriptor1 << eventDescriptorList << false; QTest::newRow("invalid action") << invalidAction << validEventDescriptor1 << QVariantList() << false; QTest::newRow("invalid event descriptor") << validAction << invalidEventDescriptor << QVariantList() << false; } void TestRules::addRules() { QFETCH(QVariantMap, action1); QFETCH(QVariantMap, eventDescriptor); QFETCH(QVariantList, eventDescriptorList); QFETCH(bool, success); QVariantMap params; QVariantList actions; actions.append(action1); params.insert("actions", actions); if (!eventDescriptor.isEmpty()) { params.insert("eventDescriptor", eventDescriptor); } if (!eventDescriptorList.isEmpty()) { params.insert("eventDescriptorList", eventDescriptorList); } QVariant response = injectAndWait("Rules.AddRule", params); RuleId newRuleId = RuleId(response.toMap().value("params").toMap().value("ruleId").toString()); verifySuccess(response, success); response = injectAndWait("Rules.GetRules"); QVariantList rules = response.toMap().value("params").toMap().value("rules").toList(); if (!success) { QVERIFY2(rules.count() == 0, "There should be no rules."); return; } QVERIFY2(rules.count() == 1, "There should be exactly one rule"); QCOMPARE(RuleId(rules.first().toMap().value("id").toString()), newRuleId); QVariantList eventDescriptors = rules.first().toMap().value("eventDescriptors").toList(); if (!eventDescriptor.isEmpty()) { QVERIFY2(eventDescriptors.count() == 1, "There shoud be exactly one eventDescriptor"); QVERIFY2(eventDescriptors.first().toMap() == eventDescriptor, "Event descriptor doesn't match"); } else if (eventDescriptorList.isEmpty()){ QVERIFY2(eventDescriptors.count() == eventDescriptorList.count(), QString("There shoud be exactly %1 eventDescriptor").arg(eventDescriptorList.count()).toLatin1().data()); foreach (const QVariant &eventDescriptorVariant, eventDescriptorList) { bool found = false; foreach (const QVariant &replyEventDescriptorVariant, eventDescriptors) { if (eventDescriptorVariant.toMap().value("deviceId") == replyEventDescriptorVariant.toMap().value("deviceId") && eventDescriptorVariant.toMap().value("eventTypeId") == replyEventDescriptorVariant.toMap().value("eventTypeId")) { found = true; QVERIFY2(eventDescriptorVariant == replyEventDescriptorVariant, "Event descriptor doesn't match"); } } } } QVariantList replyActions = rules.first().toMap().value("actions").toList(); QVERIFY2(actions == replyActions, "Actions don't match"); params.clear(); params.insert("ruleId", newRuleId); response = injectAndWait("Rules.RemoveRule", params); verifySuccess(response, true); response = injectAndWait("Rules.GetRules"); rules = response.toMap().value("params").toMap().value("rules").toList(); QVERIFY2(rules.count() == 0, "There should be no rules."); } void TestRules::loadStoreConfig() { QVariantMap eventDescriptor1; eventDescriptor1.insert("eventTypeId", mockEvent1Id); eventDescriptor1.insert("deviceId", m_mockDeviceId); eventDescriptor1.insert("paramDescriptors", QVariantList()); QVariantMap eventDescriptor2; eventDescriptor2.insert("eventTypeId", mockEvent1Id); eventDescriptor2.insert("deviceId", m_mockDeviceId); eventDescriptor2.insert("paramDescriptors", QVariantList()); QVariantMap action1; action1.insert("actionTypeId", mockActionIdNoParams); action1.insert("deviceId", m_mockDeviceId); action1.insert("params", QVariantList()); QVariantMap action2; action2.insert("actionTypeId", mockActionIdWithParams); qDebug() << "got action id" << mockActionIdWithParams; action2.insert("deviceId", m_mockDeviceId); QVariantList action2Params; QVariantMap action2Param1; action2Param1.insert("mockActionParam1", 5); action2Params.append(action2Param1); QVariantMap action2Param2; action2Param2.insert("mockActionParam2", true); action2Params.append(action2Param2); action2.insert("params", action2Params); QVariantMap params; QVariantList actions; actions.append(action1); actions.append(action2); params.insert("actions", actions); params.insert("eventDescriptor", eventDescriptor1); QVariant response = injectAndWait("Rules.AddRule", params); RuleId newRuleId = RuleId(response.toMap().value("params").toMap().value("ruleId").toString()); verifySuccess(response, true); restartServer(); response = injectAndWait("Rules.GetRules"); QVariantList rules = response.toMap().value("params").toMap().value("rules").toList(); QVERIFY2(rules.count() == 1, "There should be exactly one rule"); QCOMPARE(RuleId(rules.first().toMap().value("id").toString()), newRuleId); QVariantList eventDescriptors = rules.first().toMap().value("eventDescriptors").toList(); QVERIFY2(eventDescriptors.count() == 1, "There shoud be exactly one eventDescriptor"); QVERIFY2(eventDescriptors.first().toMap() == eventDescriptor1, "Event descriptor doesn't match"); QVariantList replyActions = rules.first().toMap().value("actions").toList(); foreach (const QVariant &actionVariant, actions) { bool found = false; foreach (const QVariant &replyActionVariant, replyActions) { if (actionVariant.toMap().value("actionTypeId") == replyActionVariant.toMap().value("actionTypeId") && actionVariant.toMap().value("deviceId") == replyActionVariant.toMap().value("deviceId")) { found = true; QVERIFY2(actionVariant == replyActionVariant, "Action doesn't match after loading from config."); } } QVERIFY2(found, "Action not found after loading from config."); } params.clear(); params.insert("ruleId", newRuleId); response = injectAndWait("Rules.RemoveRule", params); verifySuccess(response, true); restartServer(); response = injectAndWait("Rules.GetRules"); rules = response.toMap().value("params").toMap().value("rules").toList(); QVERIFY2(rules.count() == 0, "There should be no rules."); } #include "testrules.moc" QTEST_MAIN(TestRules)