add logging tests and clear database method

This commit is contained in:
Simon Stürz 2015-12-08 21:31:58 +01:00 committed by Michael Zanetti
parent 8d979cea51
commit 572f98bc6c
7 changed files with 169 additions and 0 deletions

View File

@ -184,6 +184,20 @@ QList<LogEntry> 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);

View File

@ -41,6 +41,8 @@ public:
QList<LogEntry> logEntries(const LogFilter &filter = LogFilter()) const;
void clearDatabase();
signals:
void logEntryAdded(const LogEntry &logEntry);
void logDatabaseUpdated();

View File

@ -16,5 +16,6 @@ SUBDIRS = versioning \
restrules \
websocketserver \
coap \
logging \

View File

@ -24,6 +24,7 @@
#include "guhcore.h"
#include "guhsettings.h"
#include "devicemanager.h"
#include "logging/logengine.h"
#include "jsontypes.h"
#include <QVariantMap>
@ -343,3 +344,8 @@ void GuhTestBase::restartServer()
m_mockTcpServer = MockTcpServer::servers().first();
}
void GuhTestBase::clearLoggingDatabase()
{
GuhCore::instance()->logEngine()->clearDatabase();
}

View File

@ -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");

View File

@ -0,0 +1,5 @@
include(../../../guh.pri)
include(../autotests.pri)
TARGET = testlogging
SOURCES += testlogging.cpp

View File

@ -0,0 +1,136 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2015 Simon Stuerz <simon.stuerz@guh.guru> *
* Copyright (C) 2014 Michael Zanetti <michael_zanetti@gmx.net> *
* *
* 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 <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "guhtestbase.h"
#include "guhcore.h"
#include "devicemanager.h"
#include "guhsettings.h"
#include "plugin/deviceplugin.h"
#include <QDebug>
#include <QSignalSpy>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
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<Device*> 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)