add tests
This commit is contained in:
parent
85ba7af29c
commit
44bdfbcb13
@ -277,7 +277,7 @@ JsonReply *RulesHandler::EditRule(const QVariantMap ¶ms)
|
|||||||
RuleEngine::RuleError status = GuhCore::instance()->editRule(ruleId, name, eventDescriptorList, stateEvaluator, actions, exitActions, enabled);
|
RuleEngine::RuleError status = GuhCore::instance()->editRule(ruleId, name, eventDescriptorList, stateEvaluator, actions, exitActions, enabled);
|
||||||
QVariantMap returns;
|
QVariantMap returns;
|
||||||
if (status == RuleEngine::RuleErrorNoError) {
|
if (status == RuleEngine::RuleErrorNoError) {
|
||||||
returns.insert("ruleId", ruleId.toString());
|
returns.insert("rule", JsonTypes::packRule(GuhCore::instance()->findRule(ruleId)));
|
||||||
}
|
}
|
||||||
returns.insert("ruleError", JsonTypes::ruleErrorToString(status));
|
returns.insert("ruleError", JsonTypes::ruleErrorToString(status));
|
||||||
return createReply(returns);
|
return createReply(returns);
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
26
|
27
|
||||||
{
|
{
|
||||||
"methods": {
|
"methods": {
|
||||||
"Actions.ExecuteAction": {
|
"Actions.ExecuteAction": {
|
||||||
@ -309,7 +309,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"Rules.DisableRule": {
|
"Rules.DisableRule": {
|
||||||
"description": "Disable a rule. The rule won't be triggered by it's events or state changes while it is disabled.",
|
"description": "Disable a rule. The rule won't be triggered by it's events or state changes while it is disabled. If successfull, the notification \"Rule.RuleConfigurationChanged\" will be emitted.",
|
||||||
"params": {
|
"params": {
|
||||||
"ruleId": "Uuid"
|
"ruleId": "Uuid"
|
||||||
},
|
},
|
||||||
@ -317,8 +317,31 @@
|
|||||||
"ruleError": "$ref:RuleError"
|
"ruleError": "$ref:RuleError"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"Rules.EditRule": {
|
||||||
|
"description": "Edit the parameters of a rule. The configuration of the rule with the given ruleId will be replaced with the new given configuration. In ordert to enable or disable a Rule, please use the methods \"Rules.EnableRule\" and \"Rules.DisableRule\". If successfull, the notification \"Rule.RuleConfigurationChanged\" will be emitted.",
|
||||||
|
"params": {
|
||||||
|
"actions": [
|
||||||
|
"$ref:RuleAction"
|
||||||
|
],
|
||||||
|
"name": "String",
|
||||||
|
"o:enabled": "Bool",
|
||||||
|
"o:eventDescriptor": "$ref:EventDescriptor",
|
||||||
|
"o:eventDescriptorList": [
|
||||||
|
"$ref:EventDescriptor"
|
||||||
|
],
|
||||||
|
"o:exitActions": [
|
||||||
|
"$ref:RuleAction"
|
||||||
|
],
|
||||||
|
"o:stateEvaluator": "$ref:StateEvaluator",
|
||||||
|
"ruleId": "Uuid"
|
||||||
|
},
|
||||||
|
"returns": {
|
||||||
|
"o:rule": "$ref:Rule",
|
||||||
|
"ruleError": "$ref:RuleError"
|
||||||
|
}
|
||||||
|
},
|
||||||
"Rules.EnableRule": {
|
"Rules.EnableRule": {
|
||||||
"description": "Enabled a rule that has previously been disabled.",
|
"description": "Enabled a rule that has previously been disabled.If successfull, the notification \"Rule.RuleConfigurationChanged\" will be emitted.",
|
||||||
"params": {
|
"params": {
|
||||||
"ruleId": "Uuid"
|
"ruleId": "Uuid"
|
||||||
},
|
},
|
||||||
@ -429,6 +452,12 @@
|
|||||||
"rule": "$ref:Rule"
|
"rule": "$ref:Rule"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"Rules.RuleConfigurationChanged": {
|
||||||
|
"description": "Emitted whenever the configuration of a Rule changed.",
|
||||||
|
"params": {
|
||||||
|
"rule": "$ref:Rule"
|
||||||
|
}
|
||||||
|
},
|
||||||
"Rules.RuleRemoved": {
|
"Rules.RuleRemoved": {
|
||||||
"description": "Emitted whenever a Rule was removed.",
|
"description": "Emitted whenever a Rule was removed.",
|
||||||
"params": {
|
"params": {
|
||||||
|
|||||||
@ -27,6 +27,7 @@
|
|||||||
|
|
||||||
#include <QVariantMap>
|
#include <QVariantMap>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
|
#include <QJsonParseError>
|
||||||
#include <QSignalSpy>
|
#include <QSignalSpy>
|
||||||
#include <QSettings>
|
#include <QSettings>
|
||||||
#include <QtTest>
|
#include <QtTest>
|
||||||
@ -111,7 +112,7 @@ void GuhTestBase::cleanupTestCase()
|
|||||||
QVariant GuhTestBase::injectAndWait(const QString &method, const QVariantMap ¶ms)
|
QVariant GuhTestBase::injectAndWait(const QString &method, const QVariantMap ¶ms)
|
||||||
{
|
{
|
||||||
QVariantMap call;
|
QVariantMap call;
|
||||||
call.insert("id", m_commandId++);
|
call.insert("id", m_commandId);
|
||||||
call.insert("method", method);
|
call.insert("method", method);
|
||||||
call.insert("params", params);
|
call.insert("params", params);
|
||||||
|
|
||||||
@ -124,11 +125,59 @@ QVariant GuhTestBase::injectAndWait(const QString &method, const QVariantMap &pa
|
|||||||
spy.wait();
|
spy.wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure the response it a valid JSON string
|
for (int i = 0; i < spy.count(); i++) {
|
||||||
QJsonParseError error;
|
// Make sure the response it a valid JSON string
|
||||||
jsonDoc = QJsonDocument::fromJson(spy.takeFirst().last().toByteArray(), &error);
|
QJsonParseError error;
|
||||||
|
jsonDoc = QJsonDocument::fromJson(spy.at(i).last().toByteArray(), &error);
|
||||||
|
if (error.error != QJsonParseError::NoError) {
|
||||||
|
qWarning() << "JSON parser error" << error.errorString();
|
||||||
|
return QVariant();
|
||||||
|
}
|
||||||
|
QVariantMap response = jsonDoc.toVariant().toMap();
|
||||||
|
if (response.value("id").toInt() == m_commandId) {
|
||||||
|
m_commandId++;
|
||||||
|
return jsonDoc.toVariant();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_commandId++;
|
||||||
|
return QVariant();
|
||||||
|
}
|
||||||
|
|
||||||
return jsonDoc.toVariant();
|
QVariant GuhTestBase::checkNotification(const QSignalSpy &spy, const QString ¬ification)
|
||||||
|
{
|
||||||
|
qDebug() << "Got" << spy.count() << "notifications while waiting for" << notification;
|
||||||
|
for (int i = 0; i < spy.count(); i++) {
|
||||||
|
// Make sure the response it a valid JSON string
|
||||||
|
QJsonParseError error;
|
||||||
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(spy.at(i).last().toByteArray(), &error);
|
||||||
|
if (error.error != QJsonParseError::NoError) {
|
||||||
|
qWarning() << "JSON parser error" << error.errorString();
|
||||||
|
return QVariant();
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariantMap response = jsonDoc.toVariant().toMap();
|
||||||
|
if (response.value("notification").toString() == notification) {
|
||||||
|
return jsonDoc.toVariant();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return QVariant();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GuhTestBase::enableNotifications()
|
||||||
|
{
|
||||||
|
QVariantMap notificationParams;
|
||||||
|
notificationParams.insert("enabled", true);
|
||||||
|
QVariant response = injectAndWait("JSONRPC.SetNotificationStatus", notificationParams);
|
||||||
|
|
||||||
|
return response.toMap().value("params").toMap().value("enabled").toBool();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GuhTestBase::disableNotifications()
|
||||||
|
{
|
||||||
|
QVariantMap notificationParams;
|
||||||
|
notificationParams.insert("enabled", true);
|
||||||
|
QVariant response = injectAndWait("JSONRPC.SetNotificationStatus", notificationParams);
|
||||||
|
return !response.toMap().value("params").toMap().value("enabled").toBool();
|
||||||
}
|
}
|
||||||
|
|
||||||
void GuhTestBase::restartServer()
|
void GuhTestBase::restartServer()
|
||||||
|
|||||||
@ -31,6 +31,7 @@
|
|||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QUuid>
|
#include <QUuid>
|
||||||
#include <QVariantMap>
|
#include <QVariantMap>
|
||||||
|
#include <QSignalSpy>
|
||||||
#include <QtTest>
|
#include <QtTest>
|
||||||
|
|
||||||
extern DeviceClassId mockDeviceClassId;
|
extern DeviceClassId mockDeviceClassId;
|
||||||
@ -63,6 +64,10 @@ protected slots:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
QVariant injectAndWait(const QString &method, const QVariantMap ¶ms = QVariantMap());
|
QVariant injectAndWait(const QString &method, const QVariantMap ¶ms = QVariantMap());
|
||||||
|
QVariant checkNotification(const QSignalSpy &spy, const QString ¬ification);
|
||||||
|
|
||||||
|
bool enableNotifications();
|
||||||
|
bool disableNotifications();
|
||||||
|
|
||||||
inline void verifyError(const QVariant &response, const QString &fieldName, const QString &error)
|
inline void verifyError(const QVariant &response, const QString &fieldName, const QString &error)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -50,6 +50,9 @@ private slots:
|
|||||||
void addRemoveRules_data();
|
void addRemoveRules_data();
|
||||||
void addRemoveRules();
|
void addRemoveRules();
|
||||||
|
|
||||||
|
void editRules_data();
|
||||||
|
void editRules();
|
||||||
|
|
||||||
void removeInvalidRule();
|
void removeInvalidRule();
|
||||||
|
|
||||||
void loadStoreConfig();
|
void loadStoreConfig();
|
||||||
@ -373,6 +376,338 @@ void TestRules::addRemoveRules()
|
|||||||
QVERIFY2(rules.count() == 0, "There should be no rules.");
|
QVERIFY2(rules.count() == 0, "There should be no rules.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TestRules::editRules_data()
|
||||||
|
{
|
||||||
|
// RuleAction
|
||||||
|
QVariantMap validActionNoParams;
|
||||||
|
validActionNoParams.insert("actionTypeId", mockActionIdNoParams);
|
||||||
|
validActionNoParams.insert("deviceId", m_mockDeviceId);
|
||||||
|
validActionNoParams.insert("ruleActionParams", QVariantList());
|
||||||
|
|
||||||
|
QVariantMap invalidAction;
|
||||||
|
invalidAction.insert("actionTypeId", ActionTypeId());
|
||||||
|
invalidAction.insert("deviceId", m_mockDeviceId);
|
||||||
|
invalidAction.insert("ruleActionParams", QVariantList());
|
||||||
|
|
||||||
|
// RuleExitAction
|
||||||
|
QVariantMap validExitActionNoParams;
|
||||||
|
validExitActionNoParams.insert("actionTypeId", mockActionIdNoParams);
|
||||||
|
validExitActionNoParams.insert("deviceId", m_mockDeviceId);
|
||||||
|
validExitActionNoParams.insert("ruleActionParams", QVariantList());
|
||||||
|
|
||||||
|
QVariantMap invalidExitAction;
|
||||||
|
invalidExitAction.insert("actionTypeId", ActionTypeId());
|
||||||
|
invalidExitAction.insert("deviceId", m_mockDeviceId);
|
||||||
|
invalidExitAction.insert("ruleActionParams", QVariantList());
|
||||||
|
|
||||||
|
// StateDescriptor
|
||||||
|
QVariantMap stateDescriptor;
|
||||||
|
stateDescriptor.insert("stateTypeId", mockIntStateId);
|
||||||
|
stateDescriptor.insert("deviceId", m_mockDeviceId);
|
||||||
|
stateDescriptor.insert("operator", JsonTypes::valueOperatorToString(Types::ValueOperatorLess));
|
||||||
|
stateDescriptor.insert("value", "20");
|
||||||
|
|
||||||
|
// StateEvaluator
|
||||||
|
QVariantMap validStateEvaluator;
|
||||||
|
validStateEvaluator.insert("stateDescriptor", stateDescriptor);
|
||||||
|
validStateEvaluator.insert("operator", JsonTypes::stateOperatorToString(Types::StateOperatorAnd));
|
||||||
|
|
||||||
|
QVariantMap invalidStateEvaluator;
|
||||||
|
stateDescriptor.remove("deviceId");
|
||||||
|
invalidStateEvaluator.insert("stateDescriptor", stateDescriptor);
|
||||||
|
|
||||||
|
// EventDescriptor
|
||||||
|
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("name", "mockParamInt");
|
||||||
|
param1.insert("value", 3);
|
||||||
|
param1.insert("operator", JsonTypes::valueOperatorToString(Types::ValueOperatorEquals));
|
||||||
|
params.append(param1);
|
||||||
|
validEventDescriptor2.insert("paramDescriptors", params);
|
||||||
|
|
||||||
|
QVariantMap validEventDescriptor3;
|
||||||
|
validEventDescriptor3.insert("eventTypeId", mockEvent2Id);
|
||||||
|
validEventDescriptor3.insert("deviceId", m_mockDeviceId);
|
||||||
|
validEventDescriptor3.insert("paramDescriptors", QVariantList());
|
||||||
|
|
||||||
|
// EventDescriptorList
|
||||||
|
QVariantList eventDescriptorList;
|
||||||
|
eventDescriptorList.append(validEventDescriptor1);
|
||||||
|
eventDescriptorList.append(validEventDescriptor2);
|
||||||
|
|
||||||
|
QVariantMap invalidEventDescriptor;
|
||||||
|
invalidEventDescriptor.insert("eventTypeId", mockEvent1Id);
|
||||||
|
invalidEventDescriptor.insert("deviceId", DeviceId());
|
||||||
|
invalidEventDescriptor.insert("paramDescriptors", QVariantList());
|
||||||
|
|
||||||
|
// RuleAction event based
|
||||||
|
QVariantMap validActionEventBased;
|
||||||
|
validActionEventBased.insert("actionTypeId", mockActionIdWithParams);
|
||||||
|
validActionEventBased.insert("deviceId", m_mockDeviceId);
|
||||||
|
QVariantMap validActionEventBasedParam1;
|
||||||
|
validActionEventBasedParam1.insert("name", "mockActionParam1");
|
||||||
|
validActionEventBasedParam1.insert("eventTypeId", mockEvent2Id);
|
||||||
|
validActionEventBasedParam1.insert("eventParamName", "mockParamInt");
|
||||||
|
QVariantMap validActionEventBasedParam2;
|
||||||
|
validActionEventBasedParam2.insert("name", "mockActionParam2");
|
||||||
|
validActionEventBasedParam2.insert("value", false);
|
||||||
|
validActionEventBased.insert("ruleActionParams", QVariantList() << validActionEventBasedParam1 << validActionEventBasedParam2);
|
||||||
|
|
||||||
|
QVariantMap invalidActionEventBased;
|
||||||
|
invalidActionEventBased.insert("actionTypeId", mockActionIdNoParams);
|
||||||
|
invalidActionEventBased.insert("deviceId", m_mockDeviceId);
|
||||||
|
validActionEventBasedParam1.insert("value", 10);
|
||||||
|
invalidActionEventBased.insert("ruleActionParams", QVariantList() << validActionEventBasedParam1);
|
||||||
|
|
||||||
|
QVariantMap invalidActionEventBased2;
|
||||||
|
invalidActionEventBased2.insert("actionTypeId", mockActionIdWithParams);
|
||||||
|
invalidActionEventBased2.insert("deviceId", m_mockDeviceId);
|
||||||
|
QVariantMap invalidActionEventBasedParam2;
|
||||||
|
invalidActionEventBasedParam2.insert("name", "mockActionParam1");
|
||||||
|
invalidActionEventBasedParam2.insert("eventTypeId", mockEvent1Id);
|
||||||
|
invalidActionEventBasedParam2.insert("eventParamName", "value");
|
||||||
|
QVariantMap invalidActionEventBasedParam3;
|
||||||
|
invalidActionEventBasedParam3.insert("name", "mockActionParam2");
|
||||||
|
invalidActionEventBasedParam3.insert("value", 2);
|
||||||
|
invalidActionEventBased2.insert("ruleActionParams", QVariantList() << invalidActionEventBasedParam2 << invalidActionEventBasedParam3);
|
||||||
|
|
||||||
|
QVariantMap invalidActionEventBased3;
|
||||||
|
invalidActionEventBased3.insert("actionTypeId", mockActionIdWithParams);
|
||||||
|
invalidActionEventBased3.insert("deviceId", m_mockDeviceId);
|
||||||
|
QVariantMap invalidActionEventBasedParam4;
|
||||||
|
invalidActionEventBasedParam4.insert("name", "mockActionParam1");
|
||||||
|
invalidActionEventBasedParam4.insert("eventTypeId", mockEvent1Id);
|
||||||
|
invalidActionEventBasedParam4.insert("eventParamName", "mockParamInt");
|
||||||
|
invalidActionEventBased3.insert("ruleActionParams", QVariantList() << invalidActionEventBasedParam4);
|
||||||
|
|
||||||
|
QTest::addColumn<bool>("enabled");
|
||||||
|
QTest::addColumn<QVariantMap>("action");
|
||||||
|
QTest::addColumn<QVariantMap>("exitAction");
|
||||||
|
QTest::addColumn<QVariantMap>("eventDescriptor");
|
||||||
|
QTest::addColumn<QVariantList>("eventDescriptorList");
|
||||||
|
QTest::addColumn<QVariantMap>("stateEvaluator");
|
||||||
|
QTest::addColumn<RuleEngine::RuleError>("error");
|
||||||
|
QTest::addColumn<QString>("name");
|
||||||
|
|
||||||
|
// Rules with event based actions
|
||||||
|
QTest::newRow("valid rule. enabled, 1 Action (eventBased), 1 EventDescriptor, name") << true << validActionEventBased << QVariantMap() << validEventDescriptor3 << QVariantList() << QVariantMap() << RuleEngine::RuleErrorNoError << "ActionEventRule1";
|
||||||
|
QTest::newRow("invalid rule. enabled, 1 Action (eventBased), 1 EventDescriptor, name") << true << invalidActionEventBased2 << QVariantMap() << validEventDescriptor3 << QVariantList() << QVariantMap() << RuleEngine::RuleErrorInvalidRuleActionParameter << "TestRule";
|
||||||
|
|
||||||
|
QTest::newRow("invalid rule. enabled, 1 Action (eventBased), types not matching, name") << true << invalidActionEventBased3 << QVariantMap() << validEventDescriptor1 << QVariantList() << QVariantMap() << RuleEngine::RuleErrorTypesNotMatching << "TestRule";
|
||||||
|
|
||||||
|
QTest::newRow("invalid rule. enabled, 1 Action (eventBased), 1 EventDescriptor, name") << true << invalidActionEventBased << QVariantMap() << validEventDescriptor2 << QVariantList() << QVariantMap() << RuleEngine::RuleErrorInvalidRuleActionParameter << "TestRule";
|
||||||
|
QTest::newRow("invalid rule. enabled, 1 Action (eventBased), 1 StateEvaluator, name") << true << validActionEventBased << QVariantMap() << QVariantMap() << QVariantList() << validStateEvaluator << RuleEngine::RuleErrorInvalidRuleActionParameter << "TestRule";
|
||||||
|
QTest::newRow("invalid rule. enabled, 1 Action (eventBased), 1 EventDescriptor, name") << true << validActionEventBased << validActionEventBased << validEventDescriptor2 << QVariantList() << QVariantMap() << RuleEngine::RuleErrorInvalidRuleFormat << "TestRule";
|
||||||
|
QTest::newRow("invalid rule. enabled, 1 Action, 1 ExitAction (EventBased), name") << true << validActionNoParams << validActionEventBased << validEventDescriptor2 << QVariantList() << QVariantMap() << RuleEngine::RuleErrorInvalidRuleFormat << "TestRule";
|
||||||
|
|
||||||
|
// Rules with exit actions
|
||||||
|
QTest::newRow("valid rule. enabled, 1 Action, 1 Exit Action, 1 StateEvaluator, name") << true << validActionNoParams << validExitActionNoParams << QVariantMap() << QVariantList() << validStateEvaluator << RuleEngine::RuleErrorNoError << "TestRule";
|
||||||
|
QTest::newRow("valid rule. disabled, 1 Action, 1 Exit Action, 1 StateEvaluator, name") << false << validActionNoParams << validExitActionNoParams << QVariantMap() << QVariantList() << validStateEvaluator << RuleEngine::RuleErrorNoError << "TestRule";
|
||||||
|
QTest::newRow("invalid rule. 1 Action, 1 Exit Action, 1 EventDescriptor, 1 StateEvaluator, name") << true << validActionNoParams << validExitActionNoParams << validEventDescriptor1 << QVariantList() << validStateEvaluator << RuleEngine::RuleErrorInvalidRuleFormat << "TestRule";
|
||||||
|
QTest::newRow("invalid rule. 1 Action, 1 Exit Action, eventDescriptorList, 1 StateEvaluator, name") << true << validActionNoParams << validExitActionNoParams << QVariantMap() << eventDescriptorList << validStateEvaluator << RuleEngine::RuleErrorInvalidRuleFormat << "TestRule";
|
||||||
|
|
||||||
|
// Rules without exit actions
|
||||||
|
QTest::newRow("valid rule. enabled, 1 EventDescriptor, StateEvaluator, 1 Action, name") << true << validActionNoParams << QVariantMap() << validEventDescriptor1 << QVariantList() << validStateEvaluator << RuleEngine::RuleErrorNoError << "TestRule";
|
||||||
|
QTest::newRow("valid rule. diabled, 1 EventDescriptor, StateEvaluator, 1 Action, name") << false << validActionNoParams << QVariantMap() << validEventDescriptor1 << QVariantList() << validStateEvaluator << RuleEngine::RuleErrorNoError << "TestRule";
|
||||||
|
QTest::newRow("valid rule. 2 EventDescriptors, 1 Action, name") << true << validActionNoParams << QVariantMap() << QVariantMap() << eventDescriptorList << validStateEvaluator << RuleEngine::RuleErrorNoError << "TestRule";
|
||||||
|
QTest::newRow("invalid rule: eventDescriptor and eventDescriptorList used") << true << validActionNoParams << QVariantMap() << validEventDescriptor1 << eventDescriptorList << validStateEvaluator << RuleEngine::RuleErrorInvalidParameter << "TestRule";
|
||||||
|
}
|
||||||
|
|
||||||
|
void TestRules::editRules()
|
||||||
|
{
|
||||||
|
QFETCH(bool, enabled);
|
||||||
|
QFETCH(QVariantMap, action);
|
||||||
|
QFETCH(QVariantMap, exitAction);
|
||||||
|
QFETCH(QVariantMap, eventDescriptor);
|
||||||
|
QFETCH(QVariantList, eventDescriptorList);
|
||||||
|
QFETCH(QVariantMap, stateEvaluator);
|
||||||
|
QFETCH(RuleEngine::RuleError, error);
|
||||||
|
QFETCH(QString, name);
|
||||||
|
|
||||||
|
// Add the rule we want to edit
|
||||||
|
QVariantList eventParamDescriptors;
|
||||||
|
QVariantMap eventDescriptor1;
|
||||||
|
eventDescriptor1.insert("eventTypeId", mockEvent1Id);
|
||||||
|
eventDescriptor1.insert("deviceId", m_mockDeviceId);
|
||||||
|
eventDescriptor1.insert("paramDescriptors", QVariantList());
|
||||||
|
QVariantMap eventDescriptor2;
|
||||||
|
eventDescriptor2.insert("eventTypeId", mockEvent2Id);
|
||||||
|
eventDescriptor2.insert("deviceId", m_mockDeviceId);
|
||||||
|
eventDescriptor2.insert("paramDescriptors", QVariantList());
|
||||||
|
QVariantMap eventParam1;
|
||||||
|
eventParam1.insert("name", "mockParamInt");
|
||||||
|
eventParam1.insert("value", 3);
|
||||||
|
eventParam1.insert("operator", JsonTypes::valueOperatorToString(Types::ValueOperatorEquals));
|
||||||
|
eventParamDescriptors.append(eventParam1);
|
||||||
|
eventDescriptor2.insert("paramDescriptors", eventParamDescriptors);
|
||||||
|
|
||||||
|
QVariantList eventDescriptorList1;
|
||||||
|
eventDescriptorList1.append(eventDescriptor1);
|
||||||
|
eventDescriptorList1.append(eventDescriptor2);
|
||||||
|
|
||||||
|
QVariantMap stateEvaluator0;
|
||||||
|
QVariantMap stateDescriptor1;
|
||||||
|
stateDescriptor1.insert("deviceId", m_mockDeviceId);
|
||||||
|
stateDescriptor1.insert("operator", JsonTypes::valueOperatorToString(Types::ValueOperatorEquals));
|
||||||
|
stateDescriptor1.insert("stateTypeId", mockIntStateId);
|
||||||
|
stateDescriptor1.insert("value", 1);
|
||||||
|
QVariantMap stateDescriptor2;
|
||||||
|
stateDescriptor2.insert("deviceId", m_mockDeviceId);
|
||||||
|
stateDescriptor2.insert("operator", JsonTypes::valueOperatorToString(Types::ValueOperatorEquals));
|
||||||
|
stateDescriptor2.insert("stateTypeId", mockBoolStateId);
|
||||||
|
stateDescriptor2.insert("value", true);
|
||||||
|
QVariantMap stateEvaluator1;
|
||||||
|
stateEvaluator1.insert("stateDescriptor", stateDescriptor1);
|
||||||
|
stateEvaluator1.insert("operator", JsonTypes::stateOperatorToString(Types::StateOperatorAnd));
|
||||||
|
QVariantMap stateEvaluator2;
|
||||||
|
stateEvaluator2.insert("stateDescriptor", stateDescriptor2);
|
||||||
|
stateEvaluator2.insert("operator", JsonTypes::stateOperatorToString(Types::StateOperatorAnd));
|
||||||
|
QVariantList childEvaluators;
|
||||||
|
childEvaluators.append(stateEvaluator1);
|
||||||
|
childEvaluators.append(stateEvaluator2);
|
||||||
|
stateEvaluator0.insert("childEvaluators", childEvaluators);
|
||||||
|
stateEvaluator0.insert("operator", JsonTypes::stateOperatorToString(Types::StateOperatorAnd));
|
||||||
|
|
||||||
|
QVariantMap action1;
|
||||||
|
action1.insert("actionTypeId", mockActionIdNoParams);
|
||||||
|
action1.insert("deviceId", m_mockDeviceId);
|
||||||
|
action1.insert("ruleActionParams", QVariantList());
|
||||||
|
QVariantMap action2;
|
||||||
|
action2.insert("actionTypeId", mockActionIdWithParams);
|
||||||
|
qDebug() << "got action id" << mockActionIdWithParams;
|
||||||
|
action2.insert("deviceId", m_mockDeviceId);
|
||||||
|
QVariantList action2Params;
|
||||||
|
QVariantMap action2Param1;
|
||||||
|
action2Param1.insert("name", "mockActionParam1");
|
||||||
|
action2Param1.insert("value", 5);
|
||||||
|
action2Params.append(action2Param1);
|
||||||
|
QVariantMap action2Param2;
|
||||||
|
action2Param2.insert("name", "mockActionParam2");
|
||||||
|
action2Param2.insert("value", true);
|
||||||
|
action2Params.append(action2Param2);
|
||||||
|
action2.insert("ruleActionParams", action2Params);
|
||||||
|
|
||||||
|
// RuleAction event based
|
||||||
|
QVariantMap validActionEventBased;
|
||||||
|
validActionEventBased.insert("actionTypeId", mockActionIdWithParams);
|
||||||
|
validActionEventBased.insert("deviceId", m_mockDeviceId);
|
||||||
|
QVariantMap validActionEventBasedParam1;
|
||||||
|
validActionEventBasedParam1.insert("name", "mockActionParam1");
|
||||||
|
validActionEventBasedParam1.insert("eventTypeId", mockEvent2Id);
|
||||||
|
validActionEventBasedParam1.insert("eventParamName", "mockParamInt");
|
||||||
|
QVariantMap validActionEventBasedParam2;
|
||||||
|
validActionEventBasedParam2.insert("name", "mockActionParam2");
|
||||||
|
validActionEventBasedParam2.insert("value", false);
|
||||||
|
validActionEventBased.insert("ruleActionParams", QVariantList() << validActionEventBasedParam1 << validActionEventBasedParam2);
|
||||||
|
|
||||||
|
QVariantList validEventDescriptors3;
|
||||||
|
QVariantMap validEventDescriptor3;
|
||||||
|
validEventDescriptor3.insert("eventTypeId", mockEvent2Id);
|
||||||
|
validEventDescriptor3.insert("deviceId", m_mockDeviceId);
|
||||||
|
validEventDescriptor3.insert("paramDescriptors", QVariantList());
|
||||||
|
validEventDescriptors3.append(validEventDescriptor3);
|
||||||
|
|
||||||
|
QVariantMap params;
|
||||||
|
QVariantList actions;
|
||||||
|
actions.append(action1);
|
||||||
|
actions.append(action2);
|
||||||
|
params.insert("actions", actions);
|
||||||
|
params.insert("eventDescriptorList", eventDescriptorList1);
|
||||||
|
params.insert("stateEvaluator", stateEvaluator0);
|
||||||
|
params.insert("name", "TestRule");
|
||||||
|
QVariant response = injectAndWait("Rules.AddRule", params);
|
||||||
|
|
||||||
|
RuleId ruleId = RuleId(response.toMap().value("params").toMap().value("ruleId").toString());
|
||||||
|
verifyRuleError(response);
|
||||||
|
|
||||||
|
// enable notifications
|
||||||
|
QCOMPARE(enableNotifications(), true);
|
||||||
|
|
||||||
|
// now create the new rule and edit the original one
|
||||||
|
params.clear();
|
||||||
|
params.insert("ruleId", ruleId.toString());
|
||||||
|
params.insert("name", name);
|
||||||
|
|
||||||
|
if (!eventDescriptor.isEmpty()) {
|
||||||
|
params.insert("eventDescriptor", eventDescriptor);
|
||||||
|
}
|
||||||
|
if (!eventDescriptorList.isEmpty()) {
|
||||||
|
params.insert("eventDescriptorList", eventDescriptorList);
|
||||||
|
}
|
||||||
|
actions.clear();
|
||||||
|
actions.append(action);
|
||||||
|
params.insert("actions", actions);
|
||||||
|
|
||||||
|
QVariantList exitActions;
|
||||||
|
if (!exitAction.isEmpty()) {
|
||||||
|
exitActions.append(exitAction);
|
||||||
|
params.insert("exitActions", exitActions);
|
||||||
|
}
|
||||||
|
params.insert("stateEvaluator", stateEvaluator);
|
||||||
|
if (!enabled) {
|
||||||
|
params.insert("enabled", enabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setup connection to mock client
|
||||||
|
QSignalSpy clientSpy(m_mockTcpServer, SIGNAL(outgoingData(QUuid,QByteArray)));
|
||||||
|
response.clear();
|
||||||
|
response = injectAndWait("Rules.EditRule", params);
|
||||||
|
verifyRuleError(response, error);
|
||||||
|
if (error == RuleEngine::RuleErrorNoError){
|
||||||
|
QVariant notification = checkNotification(clientSpy, "Rules.RuleConfigurationChanged");
|
||||||
|
QVERIFY2(notification != QVariant(), "not received \"Rules.RuleConfigurationChanged\" notification");
|
||||||
|
|
||||||
|
|
||||||
|
// now check if the received rule matches the our new rule
|
||||||
|
QVariantMap rule = response.toMap().value("params").toMap().value("rule").toMap();
|
||||||
|
|
||||||
|
QVERIFY2(rule.value("enabled").toBool() == enabled, "Rule enabled state doesn't match");
|
||||||
|
QVariantList eventDescriptors = rule.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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
QVERIFY2(found, "Missing event descriptor");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariantList replyActions = rule.value("actions").toList();
|
||||||
|
QVERIFY2(actions == replyActions, "Actions don't match");
|
||||||
|
|
||||||
|
QVariantList replyExitActions = rule.value("exitActions").toList();
|
||||||
|
QVERIFY2(exitActions == replyExitActions, "ExitActions don't match");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove th rule
|
||||||
|
params.clear();
|
||||||
|
params.insert("ruleId", ruleId);
|
||||||
|
response = injectAndWait("Rules.RemoveRule", params);
|
||||||
|
verifyRuleError(response);
|
||||||
|
|
||||||
|
// check if removed
|
||||||
|
response = injectAndWait("Rules.GetRules");
|
||||||
|
QVariantList rules = response.toMap().value("params").toMap().value("rules").toList();
|
||||||
|
QVERIFY2(rules.count() == 0, "There should be no rules.");
|
||||||
|
}
|
||||||
|
|
||||||
void TestRules::removeInvalidRule()
|
void TestRules::removeInvalidRule()
|
||||||
{
|
{
|
||||||
QVariantMap params;
|
QVariantMap params;
|
||||||
|
|||||||
Reference in New Issue
Block a user