added a test for triggering a rule

This commit is contained in:
Michael Zanetti 2014-06-15 02:02:11 +02:00
parent 576e67f9f3
commit 011a0afa48
2 changed files with 48 additions and 1 deletions

View File

@ -38,6 +38,8 @@ private slots:
void addRules();
void loadStoreConfig();
void evaluateEvent();
};
void TestRules::addRules_data()
@ -139,6 +141,7 @@ void TestRules::addRules()
QVERIFY2(eventDescriptorVariant == replyEventDescriptorVariant, "Event descriptor doesn't match");
}
}
QVERIFY2(found, "Missing event descriptor");
}
}
@ -241,5 +244,49 @@ void TestRules::loadStoreConfig()
QVERIFY2(rules.count() == 0, "There should be no rules.");
}
void TestRules::evaluateEvent()
{
// Add a rule
QVariantMap addRuleParams;
QVariantList events;
QVariantMap event1;
event1.insert("eventTypeId", mockEvent1Id);
event1.insert("deviceId", m_mockDeviceId);
events.append(event1);
addRuleParams.insert("eventDescriptorList", 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);
verifySuccess(response, true);
// Trigger an event
QNetworkAccessManager nam;
QSignalSpy spy(&nam, SIGNAL(finished(QNetworkReply*)));
// trigger event in mock device
QNetworkRequest request(QUrl(QString("http://localhost:%1/generateevent?eventtypeid=%2").arg(m_mockDevice1Port).arg(mockEvent1Id.toString())));
QNetworkReply *reply = nam.get(request);
spy.wait();
QCOMPARE(spy.count(), 1);
reply->deleteLater();
// Verify rule got executed
spy.clear();
request.setUrl(QUrl(QString("http://localhost:%1/actionhistory").arg(m_mockDevice1Port)));
reply = nam.get(request);
spy.wait();
QCOMPARE(spy.count(), 1);
reply->deleteLater();
QByteArray actionHistory = reply->readAll();
QVERIFY2(mockActionIdNoParams == ActionTypeId(actionHistory), "Action not triggered");
}
#include "testrules.moc"
QTEST_MAIN(TestRules)

View File

@ -3,7 +3,7 @@
if test -z $5; then
echo "usage: $0 host sourceDevice eventTypeId targetDeviceId actionTypeId [paramname paramvalue]"
elif test -z $6; then
(echo '{"id":1, "method":"Rules.AddRule", "params":{"event": {"eventTypeId": "$3", "deviceId":"'$2'"}, "actions": [ { "deviceId":"'$4'", "actionTypeId":"'$5'"}]}}'; sleep 1) | nc $1 1234
(echo '{"id":1, "method":"Rules.AddRule", "params":{"eventDescriptor": {"eventTypeId": "'$3'", "deviceId":"'$2'"}, "actions": [ { "deviceId":"'$4'", "actionTypeId":"'$5'"}]}}'; sleep 1) | nc $1 1234
elif test -z $7; then
echo "usage: $0 host sourceDevice eventTypeId targetDeviceId actionTypeId [paramname paramvalue]"
else