From 26ac948f982829e021dd4e2945f19eb3d6710acb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Thu, 7 May 2015 11:38:13 +0200 Subject: [PATCH] added notification tests for DeviceRemoved and DeviceAdded --- tests/auto/jsonrpc/testjsonrpc.cpp | 73 ++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/tests/auto/jsonrpc/testjsonrpc.cpp b/tests/auto/jsonrpc/testjsonrpc.cpp index a199eb07..731e4bda 100644 --- a/tests/auto/jsonrpc/testjsonrpc.cpp +++ b/tests/auto/jsonrpc/testjsonrpc.cpp @@ -49,6 +49,8 @@ private slots: void enableDisableNotifications(); void stateChangeEmitsNotifications(); + void deviceAddedRemovedNotifications(); + private: QStringList extractRefs(const QVariant &variant); @@ -187,6 +189,7 @@ void TestJSONRPC::enableDisableNotifications() QCOMPARE(response.toMap().value("params").toMap().value("enabled").toString(), enabled); } + void TestJSONRPC::stateChangeEmitsNotifications() { QVariantMap params; @@ -256,6 +259,76 @@ void TestJSONRPC::stateChangeEmitsNotifications() } +void TestJSONRPC::deviceAddedRemovedNotifications() +{ + // enable notificartions + QVariantMap params; + params.insert("enabled", true); + QVariant response = injectAndWait("JSONRPC.SetNotificationStatus", params); + QCOMPARE(response.toMap().value("params").toMap().value("enabled").toBool(), true); + + // Setup connection to mock client + QSignalSpy clientSpy(m_mockTcpServer, SIGNAL(outgoingData(QUuid,QByteArray))); + + // add device and wait for notification + QVariantList deviceParams; + QVariantMap httpportParam; + httpportParam.insert("name", "httpport"); + httpportParam.insert("value", 8765); + deviceParams.append(httpportParam); + + params.clear(); response.clear(); + params.insert("deviceClassId", mockDeviceClassId); + params.insert("deviceParams", deviceParams); + response = injectAndWait("Devices.AddConfiguredDevice", params); + + // 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()); + QJsonDocument jsonDocNotification = QJsonDocument::fromJson(clientSpy.at(0).at(1).toByteArray()); + + verifyDeviceError(jsonDocResponse.toVariant()); + DeviceId deviceId = DeviceId(jsonDocResponse.toVariant().toMap().value("params").toMap().value("deviceId").toString()); + QVERIFY(!deviceId.isNull()); + + // check the DeviceAdded notification + QCOMPARE(jsonDocNotification.toVariant().toMap().value("notification").toString(), QString("Devices.DeviceAdded")); + QVariantMap notificationDeviceMap = jsonDocNotification.toVariant().toMap().value("params").toMap().value("device").toMap(); + + QCOMPARE(notificationDeviceMap.value("deviceClassId").toString(), mockDeviceClassId.toString()); + QCOMPARE(notificationDeviceMap.value("id").toString(), deviceId.toString()); + foreach (const QVariant ¶m, notificationDeviceMap.value("params").toList()) { + if (param.toMap().value("name").toString() == "httpport") { + QCOMPARE(param.toMap().value("value").toInt(), httpportParam.value("value").toInt()); + } + } + + // Setup connection to mock client + QSignalSpy clientSpy2(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 + + jsonDocResponse = QJsonDocument::fromJson(clientSpy2.at(1).at(1).toByteArray()); + jsonDocNotification = QJsonDocument::fromJson(clientSpy2.at(0).at(1).toByteArray()); + + verifyDeviceError(jsonDocResponse.toVariant()); + + // check the DeviceRemoved notification + QCOMPARE(jsonDocNotification.toVariant().toMap().value("notification").toString(), QString("Devices.DeviceRemoved")); + QCOMPARE(jsonDocNotification.toVariant().toMap().value("params").toMap().value("deviceId").toString(), deviceId.toString()); +} + + #include "testjsonrpc.moc" QTEST_MAIN(TestJSONRPC)