add remove policy tests

This commit is contained in:
Simon Stürz 2016-04-21 10:35:51 +02:00 committed by Michael Zanetti
parent 574bbcd7b6
commit 9d2ab92d42
4 changed files with 182 additions and 24 deletions

View File

@ -1712,6 +1712,12 @@ QPair<bool, QString> JsonTypes::validateVariant(const QVariant &templateVariant,
qCWarning(dcJsonRpc) << QString("Value %1 not allowed in %2").arg(variant.toString()).arg(repeatingModeRef());
return result;
}
} else if (refName == removePolicyRef()) {
QPair<bool, QString> result = validateEnum(s_removePolicy, variant);
if (!result.first) {
qCWarning(dcJsonRpc) << QString("Value %1 not allowed in %2").arg(variant.toString()).arg(removePolicyRef());
return result;
}
} else {
Q_ASSERT_X(false, "JsonTypes", QString("Unhandled ref: %1").arg(refName).toLatin1().data());
return report(false, QString("Unhandled ref %1. Server implementation incomplete.").arg(refName));

View File

@ -61,6 +61,11 @@ ActionTypeId mockActionIdAsync = ActionTypeId("fbae06d3-7666-483e-a39e-ec50fe890
ActionTypeId mockActionIdFailing = ActionTypeId("df3cf33d-26d5-4577-9132-9823bd33fad0");
ActionTypeId mockActionIdAsyncFailing = ActionTypeId("bfe89a1d-3497-4121-8318-e77c37537219");
// Parent device
EventTypeId mockParentChildEventId = EventTypeId("d24ede5f-4064-4898-bb84-cfb533b1fbc0");
ActionTypeId mockParentChildActionId = ActionTypeId("d24ede5f-4064-4898-bb84-cfb533b1fbc0");
StateTypeId mockParentChildStateId = StateTypeId("d24ede5f-4064-4898-bb84-cfb533b1fbc0");
static QHash<QString, bool> s_loggingFilters;
static void loggingCategoryFilter(QLoggingCategory *category)

View File

@ -55,6 +55,11 @@ extern EventTypeId mockEvent2Id;
extern StateTypeId mockIntStateId;
extern StateTypeId mockBoolStateId;
// Parent / Child device
extern EventTypeId mockParentChildEventId;
extern ActionTypeId mockParentChildActionId;
extern StateTypeId mockParentChildStateId;
using namespace guhserver;
class MockTcpServer;

View File

@ -42,6 +42,9 @@ private:
void cleanupMockHistory();
void cleanupRules();
QVariantMap createEventDescriptor(const DeviceId &deviceId, const EventTypeId &eventTypeId);
QVariantMap createActionWithParams(const DeviceId &deviceId);
void verifyRuleExecuted(const ActionTypeId &actionTypeId);
void verifyRuleNotExecuted();
@ -82,7 +85,8 @@ private slots:
void testEventBasedAction();
void removePolicy();
void removePolicyUpdate();
void removePolicyCascade();
void testRuleActionParams_data();
void testRuleActionParams();
@ -107,26 +111,30 @@ void TestRules::cleanupRules() {
}
}
void TestRules::cleanup() {
cleanupMockHistory();
cleanupRules();
QVariantMap TestRules::createEventDescriptor(const DeviceId &deviceId, const EventTypeId &eventTypeId)
{
QVariantMap eventDescriptor;
eventDescriptor.insert("eventTypeId", eventTypeId);
eventDescriptor.insert("deviceId", deviceId);
return eventDescriptor;
}
void TestRules::emptyRule()
QVariantMap TestRules::createActionWithParams(const DeviceId &deviceId)
{
QVariantMap params;
params.insert("name", QString());
params.insert("actions", QVariantList());
QVariant response = injectAndWait("Rules.AddRule", params);
verifyRuleError(response, RuleEngine::RuleErrorInvalidRuleFormat);
}
void TestRules::getInvalidRule()
{
QVariantMap params;
params.insert("ruleId", QUuid::createUuid());
QVariant response = injectAndWait("Rules.GetRuleDetails", params);
verifyRuleError(response, RuleEngine::RuleErrorRuleNotFound);
QVariantMap action;
QVariantList ruleActionParams;
QVariantMap param1;
param1.insert("name", "mockActionParam1");
param1.insert("value", 4);
QVariantMap param2;
param2.insert("name", "mockActionParam2");
param2.insert("value", true);
ruleActionParams.append(param1);
ruleActionParams.append(param2);
action.insert("deviceId", deviceId);
action.insert("actionTypeId", mockActionIdWithParams);
action.insert("ruleActionParams", ruleActionParams);
return action;
}
void TestRules::verifyRuleExecuted(const ActionTypeId &actionTypeId)
@ -160,6 +168,29 @@ void TestRules::verifyRuleNotExecuted()
reply->deleteLater();
}
/***********************************************************************/
void TestRules::cleanup() {
cleanupMockHistory();
cleanupRules();
}
void TestRules::emptyRule()
{
QVariantMap params;
params.insert("name", QString());
params.insert("actions", QVariantList());
QVariant response = injectAndWait("Rules.AddRule", params);
verifyRuleError(response, RuleEngine::RuleErrorInvalidRuleFormat);
}
void TestRules::getInvalidRule()
{
QVariantMap params;
params.insert("ruleId", QUuid::createUuid());
QVariant response = injectAndWait("Rules.GetRuleDetails", params);
verifyRuleError(response, RuleEngine::RuleErrorRuleNotFound);
}
QVariant TestRules::validIntStateBasedRule(const QString &name, const bool &executable, const bool &enabled)
{
@ -1563,7 +1594,7 @@ void TestRules::testEventBasedAction()
// TODO: check if this action was realy executed with the int state value 42
}
void TestRules::removePolicy()
void TestRules::removePolicyUpdate()
{
// ADD parent device
QVariantMap params;
@ -1595,12 +1626,128 @@ void TestRules::removePolicy()
QVERIFY2(!childDeviceId.isNull(), "Could not find child device");
// Add rule with child device
QVariantList eventDescriptors;
eventDescriptors.append(createEventDescriptor(childDeviceId, mockParentChildEventId));
eventDescriptors.append(createEventDescriptor(parentDeviceId, mockParentChildEventId));
eventDescriptors.append(createEventDescriptor(m_mockDeviceId, mockEvent1Id));
params.clear(); response.clear();
params.insert("name", "RemovePolicy");
params.insert("eventDescriptors", eventDescriptors);
params.insert("actions", QVariantList() << createActionWithParams(m_mockDeviceId));
response = injectAndWait("Rules.AddRule", params);
verifyRuleError(response);
RuleId ruleId = RuleId(response.toMap().value("params").toMap().value("ruleId").toString());
QVERIFY2(!ruleId.isNull(), "Could not get ruleId");
// Try to remove child device
params.clear(); response.clear();
params.insert("deviceId", childDeviceId);
response = injectAndWait("Devices.RemoveConfiguredDevice", params);
verifyDeviceError(response, DeviceManager::DeviceErrorDeviceIsChild);
// Try to remove child device
params.clear(); response.clear();
params.insert("deviceId", parentDeviceId);
response = injectAndWait("Devices.RemoveConfiguredDevice", params);
verifyDeviceError(response, DeviceManager::DeviceErrorDeviceInRule);
// Remove policy
params.clear(); response.clear();
params.insert("deviceId", parentDeviceId);
params.insert("removePolicy", "RemovePolicyUpdate");
response = injectAndWait("Devices.RemoveConfiguredDevice", params);
verifyDeviceError(response);
// get updated rule
params.clear();
params.insert("ruleId", ruleId);
response = injectAndWait("Rules.GetRuleDetails", params);
verifyRuleError(response);
QVariantMap rule = response.toMap().value("params").toMap().value("rule").toMap();
qDebug() << QJsonDocument::fromVariant(rule).toJson();
QVERIFY(rule.value("eventDescriptors").toList().count() == 1);
// REMOVE rule
QVariantMap removeParams;
removeParams.insert("ruleId", ruleId);
response = injectAndWait("Rules.RemoveRule", removeParams);
verifyRuleError(response);
}
void TestRules::removePolicyCascade()
{
// ADD parent device
QVariantMap params;
params.insert("deviceClassId", mockParentDeviceClassId);
params.insert("name", "Parent device");
QVariant response = injectAndWait("Devices.AddConfiguredDevice", params);
verifyDeviceError(response);
DeviceId parentDeviceId = DeviceId(response.toMap().value("params").toMap().value("deviceId").toString());
QVERIFY(!parentDeviceId.isNull());
// find child device
response = injectAndWait("Devices.GetConfiguredDevices");
QVariantList devices = response.toMap().value("params").toMap().value("devices").toList();
DeviceId childDeviceId;
foreach (const QVariant deviceVariant, devices) {
QVariantMap deviceMap = deviceVariant.toMap();
if (deviceMap.value("deviceClassId").toString() == mockChildDeviceClassId.toString()) {
if (deviceMap.value("parentId") == parentDeviceId.toString()) {
//qDebug() << QJsonDocument::fromVariant(deviceVariant).toJson();
childDeviceId = DeviceId(deviceMap.value("id").toString());
}
}
}
QVERIFY2(!childDeviceId.isNull(), "Could not find child device");
// Add rule with child device
QVariantList eventDescriptors;
eventDescriptors.append(createEventDescriptor(childDeviceId, mockParentChildEventId));
eventDescriptors.append(createEventDescriptor(parentDeviceId, mockParentChildEventId));
eventDescriptors.append(createEventDescriptor(m_mockDeviceId, mockEvent1Id));
params.clear(); response.clear();
params.insert("name", "RemovePolicy");
params.insert("eventDescriptors", eventDescriptors);
params.insert("actions", QVariantList() << createActionWithParams(m_mockDeviceId));
response = injectAndWait("Rules.AddRule", params);
verifyRuleError(response);
RuleId ruleId = RuleId(response.toMap().value("params").toMap().value("ruleId").toString());
QVERIFY2(!ruleId.isNull(), "Could not get ruleId");
// Try to remove child device
params.clear(); response.clear();
params.insert("deviceId", childDeviceId);
response = injectAndWait("Devices.RemoveConfiguredDevice", params);
verifyDeviceError(response, DeviceManager::DeviceErrorDeviceIsChild);
// Try to remove child device
params.clear(); response.clear();
params.insert("deviceId", parentDeviceId);
response = injectAndWait("Devices.RemoveConfiguredDevice", params);
verifyDeviceError(response, DeviceManager::DeviceErrorDeviceInRule);
// Remove policy
params.clear(); response.clear();
params.insert("deviceId", parentDeviceId);
params.insert("removePolicy", "RemovePolicyCascade");
response = injectAndWait("Devices.RemoveConfiguredDevice", params);
verifyDeviceError(response);
// get updated rule
params.clear();
params.insert("ruleId", ruleId);
response = injectAndWait("Rules.GetRuleDetails", params);
verifyRuleError(response, RuleEngine::RuleErrorRuleNotFound);
}
void TestRules::testRuleActionParams_data()
@ -1645,12 +1792,10 @@ void TestRules::testRuleActionParams_data()
void TestRules::testRuleActionParams()
{
QFETCH(QVariantMap, action);
QFETCH(QVariantMap, exitAction);
QFETCH(RuleEngine::RuleError, error);
// Add a rule
QVariantMap addRuleParams;
addRuleParams.insert("name", "TestRule");
@ -1662,9 +1807,6 @@ void TestRules::testRuleActionParams()
QVariant response = injectAndWait("Rules.AddRule", addRuleParams);
verifyRuleError(response, error);
}
#include "testrules.moc"