fix comparison of eventDescriptor params in rule evaluation
This commit is contained in:
parent
8862e357a6
commit
a1880c1be9
@ -1101,42 +1101,45 @@ bool RuleEngine::containsEvent(const Rule &rule, const Event &event, const Devic
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Ok, either device/eventTypeId or interface/interfaceEvent are matching. Compare the paramdescriptor
|
// Ok, either device/eventTypeId or interface/interfaceEvent are matching. Compare the paramdescriptor
|
||||||
|
bool allOK = true;
|
||||||
foreach (const ParamDescriptor ¶mDescriptor, eventDescriptor.paramDescriptors()) {
|
foreach (const ParamDescriptor ¶mDescriptor, eventDescriptor.paramDescriptors()) {
|
||||||
switch (paramDescriptor.operatorType()) {
|
switch (paramDescriptor.operatorType()) {
|
||||||
case Types::ValueOperatorEquals:
|
case Types::ValueOperatorEquals:
|
||||||
if (event.param(paramDescriptor.paramTypeId()).value() != paramDescriptor.value()) {
|
if (event.param(paramDescriptor.paramTypeId()).value() != paramDescriptor.value()) {
|
||||||
continue;
|
allOK = false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case Types::ValueOperatorNotEquals:
|
case Types::ValueOperatorNotEquals:
|
||||||
if (event.param(paramDescriptor.paramTypeId()).value() == paramDescriptor.value()) {
|
if (event.param(paramDescriptor.paramTypeId()).value() == paramDescriptor.value()) {
|
||||||
continue;
|
allOK = false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case Types::ValueOperatorGreater:
|
case Types::ValueOperatorGreater:
|
||||||
if (event.param(paramDescriptor.paramTypeId()).value() <= paramDescriptor.value()) {
|
if (event.param(paramDescriptor.paramTypeId()).value() <= paramDescriptor.value()) {
|
||||||
continue;
|
allOK = false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case Types::ValueOperatorGreaterOrEqual:
|
case Types::ValueOperatorGreaterOrEqual:
|
||||||
if (event.param(paramDescriptor.paramTypeId()).value() < paramDescriptor.value()) {
|
if (event.param(paramDescriptor.paramTypeId()).value() < paramDescriptor.value()) {
|
||||||
continue;
|
allOK = false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case Types::ValueOperatorLess:
|
case Types::ValueOperatorLess:
|
||||||
if (event.param(paramDescriptor.paramTypeId()).value() >= paramDescriptor.value()) {
|
if (event.param(paramDescriptor.paramTypeId()).value() >= paramDescriptor.value()) {
|
||||||
continue;
|
allOK = false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case Types::ValueOperatorLessOrEqual:
|
case Types::ValueOperatorLessOrEqual:
|
||||||
if (event.param(paramDescriptor.paramTypeId()).value() < paramDescriptor.value()) {
|
if (event.param(paramDescriptor.paramTypeId()).value() < paramDescriptor.value()) {
|
||||||
continue;
|
allOK = false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// All matching!
|
// All matching!
|
||||||
return true;
|
if (allOK) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@ -78,6 +78,8 @@ private slots:
|
|||||||
|
|
||||||
void evaluateEvent();
|
void evaluateEvent();
|
||||||
|
|
||||||
|
void evaluateEventParams();
|
||||||
|
|
||||||
void testStateEvaluator_data();
|
void testStateEvaluator_data();
|
||||||
void testStateEvaluator();
|
void testStateEvaluator();
|
||||||
|
|
||||||
@ -1403,6 +1405,78 @@ void TestRules::evaluateEvent()
|
|||||||
verifyRuleExecuted(mockActionIdNoParams);
|
verifyRuleExecuted(mockActionIdNoParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TestRules::evaluateEventParams()
|
||||||
|
{
|
||||||
|
// Init bool state to true
|
||||||
|
QNetworkAccessManager nam;
|
||||||
|
QSignalSpy spy(&nam, SIGNAL(finished(QNetworkReply*)));
|
||||||
|
QNetworkRequest request(QUrl(QString("http://localhost:%1/setstate?%2=%3").arg(m_mockDevice1Port).arg(mockBoolStateId.toString()).arg("true")));
|
||||||
|
QNetworkReply *reply = nam.get(request);
|
||||||
|
spy.wait();
|
||||||
|
QCOMPARE(spy.count(), 1);
|
||||||
|
reply->deleteLater();
|
||||||
|
|
||||||
|
|
||||||
|
// Add a rule
|
||||||
|
QVariantMap addRuleParams;
|
||||||
|
addRuleParams.insert("name", "TestRule");
|
||||||
|
|
||||||
|
QVariantList params;
|
||||||
|
QVariantMap boolParam;
|
||||||
|
boolParam.insert("paramTypeId", mockBoolStateId);
|
||||||
|
boolParam.insert("operator", "ValueOperatorEquals");
|
||||||
|
boolParam.insert("value", true);
|
||||||
|
params.append(boolParam);
|
||||||
|
|
||||||
|
QVariantMap event1;
|
||||||
|
event1.insert("eventTypeId", mockBoolStateId);
|
||||||
|
event1.insert("deviceId", m_mockDeviceId);
|
||||||
|
event1.insert("paramDescriptors", params);
|
||||||
|
|
||||||
|
QVariantList events;
|
||||||
|
events.append(event1);
|
||||||
|
addRuleParams.insert("eventDescriptors", events);
|
||||||
|
|
||||||
|
QVariantList actions;
|
||||||
|
QVariantMap action;
|
||||||
|
action.insert("actionTypeId", mockActionIdNoParams);
|
||||||
|
action.insert("deviceId", m_mockDeviceId);
|
||||||
|
actions.append(action);
|
||||||
|
addRuleParams.insert("actions", actions);
|
||||||
|
QVariant response = injectAndWait("Rules.AddRule", addRuleParams);
|
||||||
|
verifyRuleError(response);
|
||||||
|
|
||||||
|
|
||||||
|
// Trigger a non matching param
|
||||||
|
spy.clear();
|
||||||
|
request = QNetworkRequest(QUrl(QString("http://localhost:%1/setstate?%2=%3").arg(m_mockDevice1Port).arg(mockBoolStateId.toString()).arg("false")));
|
||||||
|
reply = nam.get(request);
|
||||||
|
spy.wait();
|
||||||
|
QCOMPARE(spy.count(), 1);
|
||||||
|
reply->deleteLater();
|
||||||
|
|
||||||
|
verifyRuleNotExecuted();
|
||||||
|
|
||||||
|
// Trigger a matching param
|
||||||
|
spy.clear();
|
||||||
|
request = QNetworkRequest(QUrl(QString("http://localhost:%1/setstate?%2=%3").arg(m_mockDevice1Port).arg(mockBoolStateId.toString()).arg("true")));
|
||||||
|
reply = nam.get(request);
|
||||||
|
spy.wait();
|
||||||
|
QCOMPARE(spy.count(), 1);
|
||||||
|
reply->deleteLater();
|
||||||
|
|
||||||
|
verifyRuleExecuted(mockActionIdNoParams);
|
||||||
|
|
||||||
|
// Reset back to false to not mess with other tests
|
||||||
|
spy.clear();
|
||||||
|
request = QNetworkRequest(QUrl(QString("http://localhost:%1/setstate?%2=%3").arg(m_mockDevice1Port).arg(mockBoolStateId.toString()).arg("false")));
|
||||||
|
reply = nam.get(request);
|
||||||
|
spy.wait();
|
||||||
|
QCOMPARE(spy.count(), 1);
|
||||||
|
reply->deleteLater();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void TestRules::testStateChange() {
|
void TestRules::testStateChange() {
|
||||||
// Add a rule
|
// Add a rule
|
||||||
QVariantMap addRuleParams;
|
QVariantMap addRuleParams;
|
||||||
|
|||||||
Reference in New Issue
Block a user