From 572f98bc6c63f7a66f4607d67a54e54d3a998589 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Tue, 8 Dec 2015 21:31:58 +0100 Subject: [PATCH] add logging tests and clear database method --- server/logging/logengine.cpp | 14 +++ server/logging/logengine.h | 2 + tests/auto/auto.pro | 1 + tests/auto/guhtestbase.cpp | 6 ++ tests/auto/guhtestbase.h | 5 ++ tests/auto/logging/logging.pro | 5 ++ tests/auto/logging/testlogging.cpp | 136 +++++++++++++++++++++++++++++ 7 files changed, 169 insertions(+) create mode 100644 tests/auto/logging/logging.pro create mode 100644 tests/auto/logging/testlogging.cpp diff --git a/server/logging/logengine.cpp b/server/logging/logengine.cpp index 8577af33..9823584d 100644 --- a/server/logging/logengine.cpp +++ b/server/logging/logengine.cpp @@ -184,6 +184,20 @@ QList LogEngine::logEntries(const LogFilter &filter) const return results; } +/*! Removes all entries from the database. This method will be used for the tests. */ +void LogEngine::clearDatabase() +{ + qCWarning(dcLogEngine) << "Clear logging database."; + + QSqlQuery query; + QString queryDeleteString = QString("DELETE FROM entries;"); + if (!query.exec(queryDeleteString)) { + qCWarning(dcLogEngine) << "Could not clear logging database. Driver error:" << query.lastError().driverText() << "Database error:" << query.lastError().databaseText(); + } + + emit logDatabaseUpdated(); +} + void LogEngine::logSystemEvent(bool active, Logging::LoggingLevel level) { LogEntry entry(level, Logging::LoggingSourceSystem); diff --git a/server/logging/logengine.h b/server/logging/logengine.h index 4a41b1b0..09781596 100644 --- a/server/logging/logengine.h +++ b/server/logging/logengine.h @@ -41,6 +41,8 @@ public: QList logEntries(const LogFilter &filter = LogFilter()) const; + void clearDatabase(); + signals: void logEntryAdded(const LogEntry &logEntry); void logDatabaseUpdated(); diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index b077c910..449d6ef6 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -16,5 +16,6 @@ SUBDIRS = versioning \ restrules \ websocketserver \ coap \ + logging \ diff --git a/tests/auto/guhtestbase.cpp b/tests/auto/guhtestbase.cpp index a7425e44..3b1cc693 100644 --- a/tests/auto/guhtestbase.cpp +++ b/tests/auto/guhtestbase.cpp @@ -24,6 +24,7 @@ #include "guhcore.h" #include "guhsettings.h" #include "devicemanager.h" +#include "logging/logengine.h" #include "jsontypes.h" #include @@ -343,3 +344,8 @@ void GuhTestBase::restartServer() m_mockTcpServer = MockTcpServer::servers().first(); } +void GuhTestBase::clearLoggingDatabase() +{ + GuhCore::instance()->logEngine()->clearDatabase(); +} + diff --git a/tests/auto/guhtestbase.h b/tests/auto/guhtestbase.h index 026bd075..997cbb2d 100644 --- a/tests/auto/guhtestbase.h +++ b/tests/auto/guhtestbase.h @@ -106,6 +106,10 @@ protected: verifyError(response, "deviceError", JsonTypes::deviceErrorToString(error)); } + inline void verifyLoggingError(const QVariant &response, Logging::LoggingError error = Logging::LoggingErrorNoError) { + verifyError(response, "loggingError", JsonTypes::loggingErrorToString(error)); + } + inline void verifyParams(const QVariantList &requestList, const QVariantList &responseList, bool allRequired = true) { if (allRequired) @@ -131,6 +135,7 @@ protected: } void restartServer(); + void clearLoggingDatabase(); protected: PluginId mockPluginId = PluginId("727a4a9a-c187-446f-aadf-f1b2220607d1"); diff --git a/tests/auto/logging/logging.pro b/tests/auto/logging/logging.pro new file mode 100644 index 00000000..84a8fc5e --- /dev/null +++ b/tests/auto/logging/logging.pro @@ -0,0 +1,5 @@ +include(../../../guh.pri) +include(../autotests.pri) + +TARGET = testlogging +SOURCES += testlogging.cpp diff --git a/tests/auto/logging/testlogging.cpp b/tests/auto/logging/testlogging.cpp new file mode 100644 index 00000000..7cc8d594 --- /dev/null +++ b/tests/auto/logging/testlogging.cpp @@ -0,0 +1,136 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2015 Simon Stuerz * + * Copyright (C) 2014 Michael Zanetti * + * * + * This file is part of guh. * + * * + * Guh is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, version 2 of the License. * + * * + * Guh is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with guh. If not, see . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#include "guhtestbase.h" +#include "guhcore.h" +#include "devicemanager.h" +#include "guhsettings.h" +#include "plugin/deviceplugin.h" + +#include +#include +#include +#include +#include + +using namespace guhserver; + +class TestLogging : public GuhTestBase +{ + Q_OBJECT + +private: + +private slots: + void initLogs(); + + void eventLogs(); + + void actionLog_data(); + void actionLog(); + + +}; + + + +void TestLogging::initLogs() +{ + QVariant response = injectAndWait("Logging.GetLogEntries"); + verifyLoggingError(response); + + QVariantList logEntries = response.toMap().value("params").toMap().value("logEntries").toList(); + qDebug() << "Got" << logEntries.count() << "logs"; + QVERIFY(logEntries.count() > 0); + +// foreach (const QVariant &logEntryVariant, logEntries) { +// qDebug() << QJsonDocument::fromVariant(logEntryVariant).toJson(); +// } + + clearLoggingDatabase(); + + response = injectAndWait("Logging.GetLogEntries"); + verifyLoggingError(response); + + logEntries = response.toMap().value("params").toMap().value("logEntries").toList(); + QVERIFY(logEntries.count() == 0); +} + +void TestLogging::eventLogs() +{ + QList devices = GuhCore::instance()->findConfiguredDevices(mockDeviceClassId); + QVERIFY2(devices.count() > 0, "There needs to be at least one configured Mock Device for this test"); + Device *device = devices.first(); + + // enable notifications + QCOMPARE(enableNotifications(), true); + + // Setup connection to mock client + QNetworkAccessManager nam; + + QSignalSpy clientSpy(m_mockTcpServer, SIGNAL(outgoingData(QUuid,QByteArray))); + + // trigger event in mock device + int port = device->paramValue("httpport").toInt(); + QNetworkRequest request(QUrl(QString("http://localhost:%1/generateevent?eventtypeid=%2").arg(port).arg(mockEvent1Id.toString()))); + QNetworkReply *reply = nam.get(request); + + // Lets wait for the notification + clientSpy.wait(1000); + QVariant notification = checkNotification(clientSpy, "Logging.LogEntryAdded"); + QVERIFY(!notification.isNull()); + reply->deleteLater(); + + QVariantMap logEntry = notification.toMap().value("params").toMap().value("logEntry").toMap(); + + // Make sure the event contains all the stuff we expect + QCOMPARE(logEntry.value("typeId").toString(), mockEvent1Id.toString()); + QCOMPARE(logEntry.value("deviceId").toString(), device->id().toString()); + QCOMPARE(logEntry.value("eventType").toString(), JsonTypes::loggingEventTypeToString(Logging::LoggingEventTypeTrigger)); + QCOMPARE(logEntry.value("source").toString(), JsonTypes::loggingSourceToString(Logging::LoggingSourceEvents)); + QCOMPARE(logEntry.value("loggingLevel").toString(), JsonTypes::loggingLevelToString(Logging::LoggingLevelInfo)); + + // get this logentry with filter + QVariantMap params; + params.insert("deviceIds", QVariantList() << device->id()); + params.insert("loggingSources", QVariantList() << JsonTypes::loggingSourceToString(Logging::LoggingSourceEvents)); + + QVariant response = injectAndWait("Logging.GetLogEntries", params); + verifyLoggingError(response); + + QVariantList logEntries = response.toMap().value("params").toMap().value("logEntries").toList(); + QVERIFY(logEntries.count() == 1); + +} + +void TestLogging::actionLog_data() +{ + +} + +void TestLogging::actionLog() +{ + +} + +#include "testlogging.moc" +QTEST_MAIN(TestLogging) +