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) {
qCDebug(dcMockDevice()) << "Setting power to" << 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;
} else if (device->deviceClassId() == mockDeviceAutoDeviceClassId) {
if (action.actionTypeId() == mockDeviceAutoMockActionAsyncActionTypeId || action.actionTypeId() == mockDeviceAutoMockActionAsyncBrokenActionTypeId) {

View File

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

View File

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

View File

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