From b7b286e5990479af06935516314ee192e917fa05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Thu, 7 May 2015 12:13:54 +0200 Subject: [PATCH] added tests for RuleAdded and RuleRemoved notifications fixed notification bug in rulehandler --- server/jsonrpc/ruleshandler.cpp | 2 +- tests/auto/jsonrpc/testjsonrpc.cpp | 90 +++++++++++++++++++++++++++++- 2 files changed, 90 insertions(+), 2 deletions(-) diff --git a/server/jsonrpc/ruleshandler.cpp b/server/jsonrpc/ruleshandler.cpp index 98e55787..6070f571 100644 --- a/server/jsonrpc/ruleshandler.cpp +++ b/server/jsonrpc/ruleshandler.cpp @@ -363,5 +363,5 @@ void RulesHandler::ruleAddedNotification(const Rule &rule) QVariantMap params; params.insert("rule", JsonTypes::packRule(rule)); - emit RuleRemoved(params); + emit RuleAdded(params); } diff --git a/tests/auto/jsonrpc/testjsonrpc.cpp b/tests/auto/jsonrpc/testjsonrpc.cpp index 731e4bda..afa6784e 100644 --- a/tests/auto/jsonrpc/testjsonrpc.cpp +++ b/tests/auto/jsonrpc/testjsonrpc.cpp @@ -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"