fix mock device and introduce tests logging category

This commit is contained in:
Michael Zanetti 2018-12-16 13:43:59 +01:00
parent 15938cafb3
commit 635a4e3ef2
4 changed files with 24 additions and 8 deletions

View File

@ -236,10 +236,9 @@ DeviceManager::DeviceError DevicePluginMock::executeAction(Device *device, const
if (action.actionTypeId() == mockPowerActionTypeId) { if (action.actionTypeId() == mockPowerActionTypeId) {
qCDebug(dcMockDevice()) << "Setting power to" << action.param(mockPowerActionPowerParamTypeId).value().toBool(); qCDebug(dcMockDevice()) << "Setting power to" << action.param(mockPowerActionPowerParamTypeId).value().toBool();
device->setStateValue(mockPowerStateTypeId, action.param(mockPowerActionPowerParamTypeId).value().toBool()); device->setStateValue(mockPowerStateTypeId, action.param(mockPowerActionPowerParamTypeId).value().toBool());
return DeviceManager::DeviceErrorNoError;
} }
m_daemons.value(device)->actionExecuted(action.actionTypeId());
m_daemons.value(device)->actionExecuted(action.actionTypeId());
return DeviceManager::DeviceErrorNoError; return DeviceManager::DeviceErrorNoError;
} else if (device->deviceClassId() == mockDeviceAutoDeviceClassId) { } else if (device->deviceClassId() == mockDeviceAutoDeviceClassId) {
if (action.actionTypeId() == mockDeviceAutoMockActionAsyncActionTypeId || action.actionTypeId() == mockDeviceAutoMockActionAsyncBrokenActionTypeId) { if (action.actionTypeId() == mockDeviceAutoMockActionAsyncActionTypeId || action.actionTypeId() == mockDeviceAutoMockActionAsyncBrokenActionTypeId) {

View File

@ -26,6 +26,8 @@
using namespace nymeaserver; using namespace nymeaserver;
Q_LOGGING_CATEGORY(dcTests, "Tests")
PluginId mockPluginId = PluginId("727a4a9a-c187-446f-aadf-f1b2220607d1"); PluginId mockPluginId = PluginId("727a4a9a-c187-446f-aadf-f1b2220607d1");
VendorId guhVendorId = VendorId("2062d64d-3232-433c-88bc-0d33c0ba2ba6"); VendorId guhVendorId = VendorId("2062d64d-3232-433c-88bc-0d33c0ba2ba6");
DeviceClassId mockDeviceClassId = DeviceClassId("753f0d32-0468-4d08-82ed-1964aab03298"); DeviceClassId mockDeviceClassId = DeviceClassId("753f0d32-0468-4d08-82ed-1964aab03298");
@ -117,7 +119,7 @@ void NymeaTestBase::initTestCase()
NymeaSettings nymeadSettings(NymeaSettings::SettingsRoleGlobal); NymeaSettings nymeadSettings(NymeaSettings::SettingsRoleGlobal);
nymeadSettings.clear(); nymeadSettings.clear();
QLoggingCategory::setFilterRules("*.debug=false"); QLoggingCategory::setFilterRules("*.debug=false\nTests.debug=true");
// Start the server // Start the server
NymeaCore::instance(); NymeaCore::instance();

View File

@ -29,6 +29,8 @@
#include <QNetworkRequest> #include <QNetworkRequest>
#include <QNetworkReply> #include <QNetworkReply>
Q_DECLARE_LOGGING_CATEGORY(dcTests)
extern DeviceClassId mockDeviceClassId; extern DeviceClassId mockDeviceClassId;
extern DeviceClassId mockDeviceAutoClassId; extern DeviceClassId mockDeviceAutoClassId;
extern DeviceClassId mockPushButtonDeviceClassId; extern DeviceClassId mockPushButtonDeviceClassId;

View File

@ -263,8 +263,7 @@ void TestRules::verifyRuleExecuted(const ActionTypeId &actionTypeId)
QCOMPARE(spy.count(), 1); QCOMPARE(spy.count(), 1);
QByteArray actionHistory = reply->readAll(); QByteArray actionHistory = reply->readAll();
qDebug() << "have action history" << actionHistory; QVERIFY2(actionTypeId == ActionTypeId(actionHistory), "Action not triggered. Current action history: \"" + actionHistory + "\"");
QVERIFY2(actionTypeId == ActionTypeId(actionHistory), "Action not triggered");
reply->deleteLater(); reply->deleteLater();
} }
@ -278,8 +277,7 @@ void TestRules::verifyRuleNotExecuted()
QCOMPARE(spy.count(), 1); QCOMPARE(spy.count(), 1);
QByteArray actionHistory = reply->readAll(); QByteArray actionHistory = reply->readAll();
qDebug() << "have action history" << actionHistory; QVERIFY2(actionHistory.isEmpty(), "Action is triggered while it should not have been. Current action history: \"" + actionHistory + "\"");
QVERIFY2(actionHistory.isEmpty(), "Action is triggered while it should not have been.");
reply->deleteLater(); reply->deleteLater();
} }
@ -373,7 +371,7 @@ void TestRules::generateEvent(const EventTypeId &eventTypeId)
void TestRules::initTestCase() void TestRules::initTestCase()
{ {
NymeaTestBase::initTestCase(); NymeaTestBase::initTestCase();
QLoggingCategory::setFilterRules("*.debug=false\nRuleEngine.debug=true\nRuleEngineDebug.debug=true\nMockDevice.*=true"); QLoggingCategory::setFilterRules("*.debug=false\nTests.debug=true\nRuleEngine.debug=true\nRuleEngineDebug.debug=true\nMockDevice.*=true");
} }
void TestRules::addRemoveRules_data() void TestRules::addRemoveRules_data()
@ -2682,12 +2680,17 @@ void TestRules::testInterfaceBasedEventRule()
addRuleParams.insert("actions", QVariantList() << powerAction); addRuleParams.insert("actions", QVariantList() << powerAction);
addRuleParams.insert("eventDescriptors", QVariantList() << lowBatteryEvent); addRuleParams.insert("eventDescriptors", QVariantList() << lowBatteryEvent);
qDebug(dcTests) << "Inserting rule";
QVariant response = injectAndWait("Rules.AddRule", addRuleParams); QVariant response = injectAndWait("Rules.AddRule", addRuleParams);
QCOMPARE(response.toMap().value("status").toString(), QString("success")); QCOMPARE(response.toMap().value("status").toString(), QString("success"));
QCOMPARE(response.toMap().value("params").toMap().value("ruleError").toString(), QString("RuleErrorNoError")); QCOMPARE(response.toMap().value("params").toMap().value("ruleError").toString(), QString("RuleErrorNoError"));
QVariantMap getRuleParams; QVariantMap getRuleParams;
getRuleParams.insert("ruleId", response.toMap().value("params").toMap().value("ruleId")); getRuleParams.insert("ruleId", response.toMap().value("params").toMap().value("ruleId"));
qDebug(dcTests) << "Getting rule details";
response = injectAndWait("Rules.GetRuleDetails", getRuleParams); response = injectAndWait("Rules.GetRuleDetails", getRuleParams);
QCOMPARE(response.toMap().value("params").toMap().value("ruleError").toString(), QString("RuleErrorNoError")); QCOMPARE(response.toMap().value("params").toMap().value("ruleError").toString(), QString("RuleErrorNoError"));
@ -2700,23 +2703,33 @@ void TestRules::testInterfaceBasedEventRule()
QCOMPARE(response.toMap().value("params").toMap().value("rule").toMap().value("actions").toList().first().toMap().value("ruleActionParams").toList().first().toMap().value("paramName").toString(), QString("power")); QCOMPARE(response.toMap().value("params").toMap().value("rule").toMap().value("actions").toList().first().toMap().value("ruleActionParams").toList().first().toMap().value("paramName").toString(), QString("power"));
QCOMPARE(response.toMap().value("params").toMap().value("rule").toMap().value("actions").toList().first().toMap().value("ruleActionParams").toList().first().toMap().value("value").toString(), QString("true")); QCOMPARE(response.toMap().value("params").toMap().value("rule").toMap().value("actions").toList().first().toMap().value("ruleActionParams").toList().first().toMap().value("value").toString(), QString("true"));
qDebug(dcTests) << "Clearing action history";
// Change the state to true, action should trigger // Change the state to true, action should trigger
spy.clear(); spy.clear();
request = QNetworkRequest(QUrl(QString("http://localhost:%1/clearactionhistory").arg(m_mockDevice1Port))); request = QNetworkRequest(QUrl(QString("http://localhost:%1/clearactionhistory").arg(m_mockDevice1Port)));
reply = nam.get(request); reply = nam.get(request);
qDebug(dcTests) << "Changing battery state -> true";
spy.wait(); spy.clear(); spy.wait(); spy.clear();
request = QNetworkRequest(QUrl(QString("http://localhost:%1/setstate?%2=%3").arg(m_mockDevice1Port).arg(mockBatteryCriticalStateId.toString()).arg(true))); request = QNetworkRequest(QUrl(QString("http://localhost:%1/setstate?%2=%3").arg(m_mockDevice1Port).arg(mockBatteryCriticalStateId.toString()).arg(true)));
reply = nam.get(request); reply = nam.get(request);
spy.wait(); spy.wait();
QCOMPARE(spy.count(), 1); QCOMPARE(spy.count(), 1);
reply->deleteLater(); reply->deleteLater();
verifyRuleExecuted(mockActionIdPower); verifyRuleExecuted(mockActionIdPower);
qDebug(dcTests) << "Clearing action history";
// Change the state to false, action should not trigger // Change the state to false, action should not trigger
spy.clear(); spy.clear();
request = QNetworkRequest(QUrl(QString("http://localhost:%1/clearactionhistory").arg(m_mockDevice1Port))); request = QNetworkRequest(QUrl(QString("http://localhost:%1/clearactionhistory").arg(m_mockDevice1Port)));
reply = nam.get(request); reply = nam.get(request);
qDebug(dcTests) << "Changing battery state -> false";
spy.wait(); spy.clear(); spy.wait(); spy.clear();
request = QNetworkRequest(QUrl(QString("http://localhost:%1/setstate?%2=%3").arg(m_mockDevice1Port).arg(mockBatteryCriticalStateId.toString()).arg(false))); request = QNetworkRequest(QUrl(QString("http://localhost:%1/setstate?%2=%3").arg(m_mockDevice1Port).arg(mockBatteryCriticalStateId.toString()).arg(false)));
reply = nam.get(request); reply = nam.get(request);