mirror of https://github.com/nymea/nymea.git
add JSON RPC rule execution tests
parent
882395472b
commit
79e0ff2dd6
|
|
@ -45,6 +45,8 @@ private:
|
|||
void verifyRuleExecuted(const ActionTypeId &actionTypeId);
|
||||
void verifyRuleNotExecuted();
|
||||
|
||||
QVariant validIntStateBasedRule(const QString &name, const bool &executable, const bool &enabled);
|
||||
|
||||
private slots:
|
||||
|
||||
void cleanup();
|
||||
|
|
@ -57,6 +59,9 @@ private slots:
|
|||
void editRules_data();
|
||||
void editRules();
|
||||
|
||||
void executeRuleActions_data();
|
||||
void executeRuleActions();
|
||||
|
||||
void removeInvalidRule();
|
||||
|
||||
void loadStoreConfig();
|
||||
|
|
@ -153,6 +158,54 @@ void TestRules::verifyRuleNotExecuted()
|
|||
|
||||
}
|
||||
|
||||
|
||||
QVariant TestRules::validIntStateBasedRule(const QString &name, const bool &executable, const bool &enabled)
|
||||
{
|
||||
QVariantMap params;
|
||||
|
||||
// StateDescriptor
|
||||
QVariantMap stateDescriptor;
|
||||
stateDescriptor.insert("stateTypeId", mockIntStateId);
|
||||
stateDescriptor.insert("deviceId", m_mockDeviceId);
|
||||
stateDescriptor.insert("operator", JsonTypes::valueOperatorToString(Types::ValueOperatorLess));
|
||||
stateDescriptor.insert("value", 25);
|
||||
|
||||
// StateEvaluator
|
||||
QVariantMap stateEvaluator;
|
||||
stateEvaluator.insert("stateDescriptor", stateDescriptor);
|
||||
stateEvaluator.insert("operator", JsonTypes::stateOperatorToString(Types::StateOperatorAnd));
|
||||
|
||||
// RuleAction
|
||||
QVariantMap action;
|
||||
action.insert("actionTypeId", mockActionIdWithParams);
|
||||
QVariantList actionParams;
|
||||
QVariantMap param1;
|
||||
param1.insert("name", "mockActionParam1");
|
||||
param1.insert("value", 5);
|
||||
actionParams.append(param1);
|
||||
QVariantMap param2;
|
||||
param2.insert("name", "mockActionParam2");
|
||||
param2.insert("value", true);
|
||||
actionParams.append(param2);
|
||||
action.insert("deviceId", m_mockDeviceId);
|
||||
action.insert("ruleActionParams", actionParams);
|
||||
|
||||
// RuleExitAction
|
||||
QVariantMap exitAction;
|
||||
exitAction.insert("actionTypeId", mockActionIdNoParams);
|
||||
exitAction.insert("deviceId", m_mockDeviceId);
|
||||
exitAction.insert("ruleActionParams", QVariantList());
|
||||
|
||||
params.insert("name", name);
|
||||
params.insert("enabled", enabled);
|
||||
params.insert("executable", executable);
|
||||
params.insert("stateEvaluator", stateEvaluator);
|
||||
params.insert("actions", QVariantList() << action);
|
||||
params.insert("exitActions", QVariantList() << exitAction);
|
||||
|
||||
return params;
|
||||
}
|
||||
|
||||
void TestRules::addRemoveRules_data()
|
||||
{
|
||||
// RuleAction
|
||||
|
|
@ -164,12 +217,8 @@ void TestRules::addRemoveRules_data()
|
|||
QVariantMap validActionWithParams;
|
||||
validActionWithParams.insert("actionTypeId", mockActionIdNoParams);
|
||||
validActionWithParams.insert("deviceId", m_mockDeviceId);
|
||||
QVariantList actionParams;
|
||||
|
||||
|
||||
validActionWithParams.insert("ruleActionParams", QVariantList());
|
||||
|
||||
|
||||
QVariantMap invalidAction;
|
||||
invalidAction.insert("actionTypeId", ActionTypeId());
|
||||
invalidAction.insert("deviceId", m_mockDeviceId);
|
||||
|
|
@ -741,6 +790,64 @@ void TestRules::editRules()
|
|||
QVERIFY2(rules.count() == 0, "There should be no rules.");
|
||||
}
|
||||
|
||||
void TestRules::executeRuleActions_data()
|
||||
{
|
||||
QTest::addColumn<QVariantMap>("params");
|
||||
QTest::addColumn<RuleEngine::RuleError>("ruleError");
|
||||
|
||||
QTest::newRow("executable rule, enabled") << validIntStateBasedRule("Executeable", true, true).toMap() << RuleEngine::RuleErrorNoError;
|
||||
QTest::newRow("executable rule, disabled") << validIntStateBasedRule("Executeable", true, false).toMap() << RuleEngine::RuleErrorNoError;
|
||||
QTest::newRow("not executable rule, enabled") << validIntStateBasedRule("Not Executable", false, true).toMap() << RuleEngine::RuleErrorNotExecutable;
|
||||
QTest::newRow("not executable rule, disabled") << validIntStateBasedRule("Not Executable", false, false).toMap() << RuleEngine::RuleErrorNotExecutable;
|
||||
}
|
||||
|
||||
void TestRules::executeRuleActions()
|
||||
{
|
||||
QFETCH(QVariantMap, params);
|
||||
QFETCH(RuleEngine::RuleError, ruleError);
|
||||
|
||||
// ADD rule
|
||||
QVariant response = injectAndWait("Rules.AddRule", params);
|
||||
verifyRuleError(response);
|
||||
|
||||
RuleId ruleId = RuleId(response.toMap().value("params").toMap().value("ruleId").toString());
|
||||
QVERIFY(!ruleId.isNull());
|
||||
|
||||
cleanupMockHistory();
|
||||
|
||||
// EXECUTE actions
|
||||
QVariantMap executeParams;
|
||||
executeParams.insert("ruleId", ruleId.toString());
|
||||
response = injectAndWait("Rules.ExecuteActions", executeParams);
|
||||
verifyRuleError(response, ruleError);
|
||||
|
||||
if (ruleError == RuleEngine::RuleErrorNoError) {
|
||||
verifyRuleExecuted(mockActionIdWithParams);
|
||||
} else {
|
||||
verifyRuleNotExecuted();
|
||||
}
|
||||
|
||||
cleanupMockHistory();
|
||||
|
||||
// EXECUTE exit actions
|
||||
response = injectAndWait("Rules.ExecuteExitActions", executeParams);
|
||||
verifyRuleError(response, ruleError);
|
||||
|
||||
if (ruleError == RuleEngine::RuleErrorNoError) {
|
||||
verifyRuleExecuted(mockActionIdNoParams);
|
||||
} else {
|
||||
verifyRuleNotExecuted();
|
||||
}
|
||||
|
||||
cleanupMockHistory();
|
||||
|
||||
// REMOVE rule
|
||||
QVariantMap removeParams;
|
||||
removeParams.insert("ruleId", ruleId);
|
||||
response = injectAndWait("Rules.RemoveRule", removeParams);
|
||||
verifyRuleError(response);
|
||||
}
|
||||
|
||||
void TestRules::removeInvalidRule()
|
||||
{
|
||||
QVariantMap params;
|
||||
|
|
|
|||
Loading…
Reference in New Issue