diff --git a/tests/auto/jsonrpc/testjsonrpc.cpp b/tests/auto/jsonrpc/testjsonrpc.cpp index afa6784e..10886413 100644 --- a/tests/auto/jsonrpc/testjsonrpc.cpp +++ b/tests/auto/jsonrpc/testjsonrpc.cpp @@ -52,6 +52,8 @@ private slots: void deviceAddedRemovedNotifications(); void ruleAddedRemovedNotifications(); + void deviceParamsChangedNotifications(); + private: QStringList extractRefs(const QVariant &variant); @@ -259,7 +261,7 @@ void TestJSONRPC::stateChangeEmitsNotifications() } -void TestJSONRPC::deviceAddedRemovedNotifications() +void TestJSONRPC::deviceParamsChangedNotifications() { // enable notificartions QVariantMap params; @@ -270,11 +272,13 @@ void TestJSONRPC::deviceAddedRemovedNotifications() // Setup connection to mock client QSignalSpy clientSpy(m_mockTcpServer, SIGNAL(outgoingData(QUuid,QByteArray))); + // ADD + // add device and wait for notification QVariantList deviceParams; QVariantMap httpportParam; httpportParam.insert("name", "httpport"); - httpportParam.insert("value", 8765); + httpportParam.insert("value", 23234); deviceParams.append(httpportParam); params.clear(); response.clear(); @@ -284,7 +288,6 @@ void TestJSONRPC::deviceAddedRemovedNotifications() // Lets wait for the notification clientSpy.wait(500); - qDebug() << "got" << clientSpy.count() << "notifications"; QCOMPARE(clientSpy.count(), 2); // wait for device added notification and response QJsonDocument jsonDocResponse = QJsonDocument::fromJson(clientSpy.at(1).at(1).toByteArray()); @@ -306,20 +309,57 @@ void TestJSONRPC::deviceAddedRemovedNotifications() } } + // EDIT + // Setup connection to mock client QSignalSpy clientSpy2(m_mockTcpServer, SIGNAL(outgoingData(QUuid,QByteArray))); + // now edit the device and check the deviceParamsChanged notification + QVariantList newDeviceParams; + QVariantMap newHttpportParam; + newHttpportParam.insert("name", "httpport"); + newHttpportParam.insert("value", 45473); + newDeviceParams.append(newHttpportParam); + + params.clear(); response.clear(); + params.insert("deviceId", deviceId); + params.insert("deviceParams", newDeviceParams); + response = injectAndWait("Devices.EditDevice", params); + + clientSpy2.wait(500); + QCOMPARE(clientSpy2.count(), 2); + + jsonDocResponse = QJsonDocument::fromJson(clientSpy2.at(1).at(1).toByteArray()); + jsonDocNotification = QJsonDocument::fromJson(clientSpy2.at(0).at(1).toByteArray()); + + verifyDeviceError(jsonDocResponse.toVariant()); + + QCOMPARE(jsonDocNotification.toVariant().toMap().value("notification").toString(), QString("Devices.DeviceParamsChanged")); + + QVariantMap editDeviceNotificationMap = jsonDocNotification.toVariant().toMap().value("params").toMap().value("device").toMap(); + QCOMPARE(editDeviceNotificationMap.value("deviceClassId").toString(), mockDeviceClassId.toString()); + QCOMPARE(editDeviceNotificationMap.value("id").toString(), deviceId.toString()); + foreach (const QVariant ¶m, editDeviceNotificationMap.value("params").toList()) { + if (param.toMap().value("name").toString() == "httpport") { + QCOMPARE(param.toMap().value("value").toInt(), newHttpportParam.value("value").toInt()); + } + } + + // REMOVE + + // Setup connection to mock client + QSignalSpy clientSpy3(m_mockTcpServer, SIGNAL(outgoingData(QUuid,QByteArray))); + // now remove the device and check the device removed notification params.clear(); response.clear(); params.insert("deviceId", deviceId); response = injectAndWait("Devices.RemoveConfiguredDevice", params); - clientSpy2.wait(500); - qDebug() << "got" << clientSpy2.count() << "notifications"; - QCOMPARE(clientSpy2.count(), 2); // wait for device removed notification and response + clientSpy3.wait(500); + QCOMPARE(clientSpy3.count(), 2); // wait for device removed notification and response - jsonDocResponse = QJsonDocument::fromJson(clientSpy2.at(1).at(1).toByteArray()); - jsonDocNotification = QJsonDocument::fromJson(clientSpy2.at(0).at(1).toByteArray()); + jsonDocResponse = QJsonDocument::fromJson(clientSpy3.at(1).at(1).toByteArray()); + jsonDocNotification = QJsonDocument::fromJson(clientSpy3.at(0).at(1).toByteArray()); verifyDeviceError(jsonDocResponse.toVariant()); @@ -328,95 +368,6 @@ 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" QTEST_MAIN(TestJSONRPC)