added tests for RuleAdded and RuleRemoved notifications

fixed notification bug in rulehandler
This commit is contained in:
Simon Stürz 2015-05-07 12:13:54 +02:00 committed by Michael Zanetti
parent 26ac948f98
commit b7b286e599
2 changed files with 90 additions and 2 deletions

View File

@ -363,5 +363,5 @@ void RulesHandler::ruleAddedNotification(const Rule &rule)
QVariantMap params;
params.insert("rule", JsonTypes::packRule(rule));
emit RuleRemoved(params);
emit RuleAdded(params);
}

View File

@ -50,7 +50,7 @@ private slots:
void stateChangeEmitsNotifications();
void deviceAddedRemovedNotifications();
void ruleAddedRemovedNotifications();
private:
QStringList extractRefs(const QVariant &variant);
@ -328,6 +328,94 @@ void TestJSONRPC::deviceAddedRemovedNotifications()
QCOMPARE(jsonDocNotification.toVariant().toMap().value("params").toMap().value("deviceId").toString(), deviceId.toString());
}
void TestJSONRPC::ruleAddedRemovedNotifications()
{
// enable notificartions
QVariantMap params;
params.insert("enabled", true);
QVariant response = injectAndWait("JSONRPC.SetNotificationStatus", params);
QCOMPARE(response.toMap().value("params").toMap().value("enabled").toBool(), true);
// Add rule and wait for notification
// StateDescriptor
QVariantMap stateDescriptor;
stateDescriptor.insert("stateTypeId", mockIntStateId);
stateDescriptor.insert("deviceId", m_mockDeviceId);
stateDescriptor.insert("operator", JsonTypes::valueOperatorToString(Types::ValueOperatorLess));
stateDescriptor.insert("value", "20");
QVariantMap stateEvaluator;
stateEvaluator.insert("stateDescriptor", stateDescriptor);
// RuleAction
QVariantMap actionNoParams;
actionNoParams.insert("actionTypeId", mockActionIdNoParams);
actionNoParams.insert("deviceId", m_mockDeviceId);
actionNoParams.insert("ruleActionParams", QVariantList());
// EventDescriptor
QVariantMap eventDescriptor;
eventDescriptor.insert("eventTypeId", mockEvent1Id);
eventDescriptor.insert("deviceId", m_mockDeviceId);
eventDescriptor.insert("paramDescriptors", QVariantList());
params.clear(); response.clear();
params.insert("name", "Test Rule notifications");
params.insert("actions", QVariantList() << actionNoParams);
params.insert("eventDescriptor", eventDescriptor);
params.insert("stateEvaluator", stateEvaluator);
// Setup connection to mock client
QSignalSpy clientSpy(m_mockTcpServer, SIGNAL(outgoingData(QUuid,QByteArray)));
response = injectAndWait("Rules.AddRule", params);
clientSpy.wait(500);
qDebug() << "got" << clientSpy.count() << "notifications";
QCOMPARE(clientSpy.count(), 2);
QJsonDocument jsonDocResponse = QJsonDocument::fromJson(clientSpy.at(1).at(1).toByteArray());
QJsonDocument jsonDocNotification = QJsonDocument::fromJson(clientSpy.at(0).at(1).toByteArray());
verifyRuleError(jsonDocResponse.toVariant());
RuleId ruleId = RuleId(jsonDocResponse.toVariant().toMap().value("params").toMap().value("ruleId").toString());
QVERIFY(!ruleId.isNull());
// check the DeviceAdded notification
QCOMPARE(jsonDocNotification.toVariant().toMap().value("notification").toString(), QString("Rules.RuleAdded"));
QVariantMap notificationRuleMap = jsonDocNotification.toVariant().toMap().value("params").toMap().value("rule").toMap();
QCOMPARE(notificationRuleMap.value("enabled").toBool(), true);
QCOMPARE(notificationRuleMap.value("name").toString(), params.value("name").toString());
QCOMPARE(notificationRuleMap.value("id").toString(), ruleId.toString());
QCOMPARE(notificationRuleMap.value("actions").toList(), QVariantList() << actionNoParams);
QCOMPARE(notificationRuleMap.value("stateEvaluator").toMap().value("stateDescriptor").toMap(), stateDescriptor);
QCOMPARE(notificationRuleMap.value("eventDescriptors").toList(), QVariantList() << eventDescriptor);
QCOMPARE(notificationRuleMap.value("exitActions").toList(), QVariantList());
// Setup connection to mock client
QSignalSpy clientSpy2(m_mockTcpServer, SIGNAL(outgoingData(QUuid,QByteArray)));
// now remove the rule and check the RuleRemoved notification
params.clear(); response.clear();
params.insert("ruleId", ruleId);
response = injectAndWait("Rules.RemoveRule", params);
clientSpy2.wait(500);
qDebug() << "got" << clientSpy2.count() << "notifications";
QCOMPARE(clientSpy2.count(), 2); // wait for RuleRemoved notification and response
jsonDocResponse = QJsonDocument::fromJson(clientSpy2.at(1).at(1).toByteArray());
jsonDocNotification = QJsonDocument::fromJson(clientSpy2.at(0).at(1).toByteArray());
verifyRuleError(jsonDocResponse.toVariant());
// check the DeviceRemoved notification
QCOMPARE(jsonDocNotification.toVariant().toMap().value("notification").toString(), QString("Rules.RuleRemoved"));
QCOMPARE(jsonDocNotification.toVariant().toMap().value("params").toMap().value("ruleId").toString(), ruleId.toString());
}
#include "testjsonrpc.moc"