Update tests

This commit is contained in:
Michael Zanetti 2020-09-23 20:15:24 +02:00
parent 5f1855ff44
commit 30a0469212
4 changed files with 61 additions and 79 deletions

View File

@ -301,7 +301,6 @@ void WebServer::readClient()
HttpRequest request; HttpRequest request;
if (m_incompleteRequests.contains(socket)) { if (m_incompleteRequests.contains(socket)) {
qCDebug(dcWebServer()) << "Append data to incomlete request";
request = m_incompleteRequests.take(socket); request = m_incompleteRequests.take(socket);
request.appendData(data); request.appendData(data);
} else { } else {

View File

@ -104,7 +104,7 @@ void HttpDaemon::readClient()
} else if (stateTypeId == mockDoubleStateTypeId) { } else if (stateTypeId == mockDoubleStateTypeId) {
stateValue.convert(QVariant::Double); stateValue.convert(QVariant::Double);
} }
qCDebug(dcMock()) << "Set state value" << stateValue; qCDebug(dcMock()) << "Setting state value" << stateValue;
emit setState(stateTypeId, stateValue); emit setState(stateTypeId, stateValue);
} else if (url.path() == "/generateevent") { } else if (url.path() == "/generateevent") {
emit triggerEvent(EventTypeId(query.queryItemValue("eventtypeid"))); emit triggerEvent(EventTypeId(query.queryItemValue("eventtypeid")));

View File

@ -852,22 +852,6 @@ void TestJSONRPC::ruleActiveChangedNotifications()
clientSpy.wait(); clientSpy.wait();
waitForDBSync();
// Make sure the logg notification contains all the stuff we expect
QVariantList logEntryAddedVariants = checkNotifications(clientSpy, "Logging.LogEntryAdded");
QVERIFY2(!logEntryAddedVariants.isEmpty(), "Did not get Logging.LogEntryAdded notification.");
bool found = false;
foreach (const QVariant &loggEntryAddedVariant, logEntryAddedVariants) {
if (loggEntryAddedVariant.toMap().value("params").toMap().value("logEntry").toMap().value("typeId").toUuid() == mockIntStateTypeId) {
found = true;
QCOMPARE(loggEntryAddedVariant.toMap().value("params").toMap().value("logEntry").toMap().value("source").toString(), QString("LoggingSourceStates"));
QCOMPARE(loggEntryAddedVariant.toMap().value("params").toMap().value("logEntry").toMap().value("value").toInt(), 20);
break;
}
}
QVERIFY2(found, "LogEntryAdded notification not received");
spy.clear(); clientSpy.clear(); spy.clear(); clientSpy.clear();
// set the rule inactive // set the rule inactive
@ -889,22 +873,6 @@ void TestJSONRPC::ruleActiveChangedNotifications()
clientSpy.wait(); clientSpy.wait();
} }
// Make sure the logg notification contains all the stuff we expect
logEntryAddedVariants = checkNotifications(clientSpy, "Logging.LogEntryAdded");
QVERIFY2(!logEntryAddedVariants.isEmpty(), "Did not get Logging.LogEntryAdded notification.");
found = false;
foreach (const QVariant &logEntryAddedVariant, logEntryAddedVariants) {
qCDebug(dcTests()) << "Checking log entry" << mockIntStateTypeId << qUtf8Printable(QJsonDocument::fromVariant(logEntryAddedVariant).toJson());
if (logEntryAddedVariant.toMap().value("params").toMap().value("logEntry").toMap().value("typeId").toUuid() == mockIntStateTypeId) {
found = true;
QCOMPARE(logEntryAddedVariant.toMap().value("params").toMap().value("logEntry").toMap().value("source").toString(), QString("LoggingSourceStates"));
QCOMPARE(logEntryAddedVariant.toMap().value("params").toMap().value("logEntry").toMap().value("value").toInt(), 42);
break;
}
}
QVERIFY2(found, "LogEntryAdded notification not received");
if (clientSpy.count() == 0) clientSpy.wait();
notificationVariant = checkNotification(clientSpy, "Rules.RuleActiveChanged"); notificationVariant = checkNotification(clientSpy, "Rules.RuleActiveChanged");
verifyRuleError(response); verifyRuleError(response);
@ -960,26 +928,10 @@ void TestJSONRPC::stateChangeEmitsNotifications()
// Devices.StateChanged // Devices.StateChanged
// Devices.EventTriggered // Devices.EventTriggered
// Events.EventTriggered <-- deprecated // Events.EventTriggered <-- deprecated
// Logging.LogEntryAdded while (clientSpy.count() < 3) {
while (clientSpy.count() < 4) {
clientSpy.wait(); clientSpy.wait();
} }
// Make sure the logg notification contains all the stuff we expect
QVariantList logEntryAddedVariants = checkNotifications(clientSpy, "Logging.LogEntryAdded");
QVERIFY2(!logEntryAddedVariants.isEmpty(), "Did not get Logging.LogEntryAdded notification.");
found = false;
foreach (const QVariant &loggEntryAddedVariant, logEntryAddedVariants) {
if (loggEntryAddedVariant.toMap().value("params").toMap().value("logEntry").toMap().value("typeId").toUuid() == stateTypeId) {
found = true;
QCOMPARE(loggEntryAddedVariant.toMap().value("params").toMap().value("logEntry").toMap().value("source").toString(), QString("LoggingSourceStates"));
QCOMPARE(loggEntryAddedVariant.toMap().value("params").toMap().value("logEntry").toMap().value("value").toInt(), newVal);
break;
}
}
QVERIFY2(found, "Could not find the corresponding Logging.LogEntryAdded notification");
// Make sure the notification contains all the stuff we expect // Make sure the notification contains all the stuff we expect
QVariantList eventTriggeredVariants = checkNotifications(clientSpy, "Events.EventTriggered"); QVariantList eventTriggeredVariants = checkNotifications(clientSpy, "Events.EventTriggered");
QVERIFY2(!eventTriggeredVariants.isEmpty(), "Did not get Events.EventTriggered notification."); QVERIFY2(!eventTriggeredVariants.isEmpty(), "Did not get Events.EventTriggered notification.");

View File

@ -71,6 +71,7 @@ private slots:
void invalidFilter_data(); void invalidFilter_data();
void invalidFilter(); void invalidFilter();
void eventLogs_data();
void eventLogs(); void eventLogs();
void actionLog(); void actionLog();
@ -261,24 +262,53 @@ void TestLogging::invalidFilter()
qDebug() << response.toMap().value("error").toString(); qDebug() << response.toMap().value("error").toString();
} }
void TestLogging::eventLogs_data()
{
QTest::addColumn<StateTypeId>("stateTypeId");
QTest::addColumn<QVariant>("initValue");
QTest::addColumn<QVariant>("newValue");
QTest::addColumn<bool>("expectLogEntry");
QTest::newRow("logged event") << mockConnectedStateTypeId << QVariant(false) << QVariant(true) << true;
QTest::newRow("not logged event") << mockSignalStrengthStateTypeId << QVariant(10) << QVariant(20) << false;
}
void TestLogging::eventLogs() void TestLogging::eventLogs()
{ {
QFETCH(StateTypeId, stateTypeId);
QFETCH(QVariant, initValue);
QFETCH(QVariant, newValue);
QFETCH(bool, expectLogEntry);
QList<Thing*> devices = NymeaCore::instance()->thingManager()->findConfiguredThings(mockThingClassId); QList<Thing*> devices = NymeaCore::instance()->thingManager()->findConfiguredThings(mockThingClassId);
QVERIFY2(devices.count() > 0, "There needs to be at least one configured Mock Device for this test"); QVERIFY2(devices.count() > 0, "There needs to be at least one configured Mock Device for this test");
Thing *device = devices.first(); Thing *device = devices.first();
enableNotifications({"Events", "Logging"});
// Setup connection to mock client // Setup connection to mock client
QNetworkAccessManager nam; QNetworkAccessManager nam;
int port = device->paramValue(mockThingHttpportParamTypeId).toInt();
// init state in mock device
QNetworkRequest request(QUrl(QString("http://localhost:%1/setstate?%2=%3").arg(port).arg(stateTypeId.toString()).arg(initValue.toString())));
QNetworkReply *reply = nam.get(request);
{
QSignalSpy finishedSpy(reply, &QNetworkReply::finished);
finishedSpy.wait();
}
// Now snoop in for the events
clearLoggingDatabase();
enableNotifications({"Events", "Logging"});
QSignalSpy clientSpy(m_mockTcpServer, SIGNAL(outgoingData(QUuid,QByteArray))); QSignalSpy clientSpy(m_mockTcpServer, SIGNAL(outgoingData(QUuid,QByteArray)));
// trigger event in mock device // trigger state change in mock device
int port = device->paramValue(mockThingHttpportParamTypeId).toInt(); request = QNetworkRequest(QUrl(QString("http://localhost:%1/setstate?%2=%3").arg(port).arg(stateTypeId.toString()).arg(newValue.toString())));
QNetworkRequest request(QUrl(QString("http://localhost:%1/generateevent?eventtypeid=%2").arg(port).arg(mockEvent1EventTypeId.toString()))); reply = nam.get(request);
QNetworkReply *reply = nam.get(request); {
QSignalSpy finishedSpy(reply, &QNetworkReply::finished);
finishedSpy.wait();
}
// Lets wait for the notification // Lets wait for the notification
QTest::qWait(200); QTest::qWait(200);
@ -286,41 +316,42 @@ void TestLogging::eventLogs()
reply->deleteLater(); reply->deleteLater();
// Make sure the logg notification contains all the stuff we expect // Make sure the logg notification contains all the stuff we expect
QVariantList loggEntryAddedVariants = checkNotifications(clientSpy, "Logging.LogEntryAdded"); QVariantList logEntryAddedVariants = checkNotifications(clientSpy, "Logging.LogEntryAdded");
QVERIFY2(!loggEntryAddedVariants.isEmpty(), "Did not get Logging.LogEntryAdded notification."); qDebug() << "got" << logEntryAddedVariants.count() << "Logging.LogEntryAdded notifications";
qDebug() << "got" << loggEntryAddedVariants.count() << "Logging.LogEntryAdded notifications";
bool found = false; bool found = false;
qDebug() << "got" << loggEntryAddedVariants.count() << "Logging.LogEntryAdded"; qDebug() << "got" << logEntryAddedVariants.count() << "Logging.LogEntryAdded";
foreach (const QVariant &loggEntryAddedVariant, loggEntryAddedVariants) { foreach (const QVariant &logEntryAddedVariant, logEntryAddedVariants) {
QVariantMap logEntry = loggEntryAddedVariant.toMap().value("params").toMap().value("logEntry").toMap(); QVariantMap logEntry = logEntryAddedVariant.toMap().value("params").toMap().value("logEntry").toMap();
if (logEntry.value("thingId").toUuid() == device->id()) { if (logEntry.value("thingId").toUuid() == device->id()) {
found = true; found = true;
// Make sure the notification contains all the stuff we expect // Make sure the notification contains all the stuff we expect
QCOMPARE(logEntry.value("typeId").toUuid().toString(), mockEvent1EventTypeId.toString()); QCOMPARE(logEntry.value("typeId").toUuid().toString(), stateTypeId.toString());
QCOMPARE(logEntry.value("eventType").toString(), enumValueName(Logging::LoggingEventTypeTrigger)); QCOMPARE(logEntry.value("eventType").toString(), enumValueName(Logging::LoggingEventTypeTrigger));
QCOMPARE(logEntry.value("source").toString(), enumValueName(Logging::LoggingSourceEvents)); QCOMPARE(logEntry.value("source").toString(), enumValueName(Logging::LoggingSourceStates));
QCOMPARE(logEntry.value("loggingLevel").toString(), enumValueName(Logging::LoggingLevelInfo)); QCOMPARE(logEntry.value("loggingLevel").toString(), enumValueName(Logging::LoggingLevelInfo));
break; break;
} }
} }
if (!found)
qDebug() << QJsonDocument::fromVariant(loggEntryAddedVariants).toJson();
QVERIFY2(found, "Could not find the corresponding Logging.LogEntryAdded notification"); QVERIFY2(found == expectLogEntry, "Could not find the corresponding Logging.LogEntryAdded notification");
// get this logentry with filter if (expectLogEntry) {
QVariantMap params; // get this logentry with filter
params.insert("thingIds", QVariantList() << device->id()); QVariantMap params;
params.insert("loggingSources", QVariantList() << enumValueName(Logging::LoggingSourceEvents)); params.insert("thingIds", QVariantList() << device->id());
params.insert("eventTypes", QVariantList() << enumValueName(Logging::LoggingEventTypeTrigger)); params.insert("loggingSources", QVariantList() << enumValueName(Logging::LoggingSourceStates));
params.insert("typeIds", QVariantList() << mockEvent1EventTypeId); params.insert("eventTypes", QVariantList() << enumValueName(Logging::LoggingEventTypeTrigger));
params.insert("typeIds", QVariantList() << stateTypeId);
QVariant response = injectAndWait("Logging.GetLogEntries", params); QVariant response = injectAndWait("Logging.GetLogEntries", params);
verifyLoggingError(response); verifyLoggingError(response);
QVariantList logEntries = response.toMap().value("params").toMap().value("logEntries").toList();
qCDebug(dcTests()) << qUtf8Printable(QJsonDocument::fromVariant(logEntries).toJson());
QCOMPARE(logEntries.count(), 1);
}
QVariantList logEntries = response.toMap().value("params").toMap().value("logEntries").toList();
QVERIFY(logEntries.count() == 1);
// disable notifications // disable notifications
QCOMPARE(disableNotifications(), true); QCOMPARE(disableNotifications(), true);
@ -629,7 +660,7 @@ void TestLogging::testHouseKeeping()
// Trigger something that creates a logging entry // Trigger something that creates a logging entry
QNetworkAccessManager nam; QNetworkAccessManager nam;
QSignalSpy spy(&nam, SIGNAL(finished(QNetworkReply*))); QSignalSpy spy(&nam, SIGNAL(finished(QNetworkReply*)));
QNetworkRequest request(QUrl(QString("http://localhost:%1/setstate?%2=%3").arg(6667).arg(mockIntStateTypeId.toString()).arg(4321))); QNetworkRequest request(QUrl(QString("http://localhost:%1/setstate?%2=%3").arg(6667).arg(mockConnectedStateTypeId.toString()).arg(false)));
QNetworkReply *reply = nam.get(request); QNetworkReply *reply = nam.get(request);
connect(reply, SIGNAL(finished()), reply, SLOT(deleteLater())); connect(reply, SIGNAL(finished()), reply, SLOT(deleteLater()));
spy.wait(); spy.wait();