add state/calendarItem/event tests

This commit is contained in:
Simon Stürz 2017-10-09 14:09:34 +02:00 committed by Michael Zanetti
parent bf2c94469e
commit 74397da6c1
4 changed files with 36 additions and 22 deletions

View File

@ -1372,8 +1372,11 @@ EventDescriptor JsonTypes::unpackEventDescriptor(const QVariantMap &eventDescrip
StateEvaluator JsonTypes::unpackStateEvaluator(const QVariantMap &stateEvaluatorMap)
{
StateEvaluator ret(unpackStateDescriptor(stateEvaluatorMap.value("stateDescriptor").toMap()));
if (stateEvaluatorMap.contains("operator"))
if (stateEvaluatorMap.contains("operator")) {
ret.setOperatorType((Types::StateOperator)s_stateOperator.indexOf(stateEvaluatorMap.value("operator").toString()));
} else {
ret.setOperatorType(Types::StateOperatorAnd);
}
QList<StateEvaluator> childEvaluators;
foreach (const QVariant &childEvaluator, stateEvaluatorMap.value("childEvaluators").toList())

View File

@ -492,24 +492,24 @@ QList<DeviceClass> DevicePlugin::supportedDevices() const
StateType stateType = stateTypes.findByName(stateVariant.toMap().value("name").toString());
QVariantMap stateMap = stateVariant.toMap();
if (stateType.id().isNull()) {
qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but doesn't implement state" << stateMap.value("name").toString();
//qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but doesn't implement state" << stateMap.value("name").toString();
valid = false;
continue;
}
if (QVariant::nameToType(stateMap.value("type").toByteArray().data()) != stateType.type()) {
qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but state" << stateMap.value("name").toString() << "has not matching type" << stateMap.value("type").toString();
//qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but state" << stateMap.value("name").toString() << "has not matching type" << stateMap.value("type").toString();
valid = false;
continue;
}
if (stateMap.contains("minimumValue")) {
if (stateMap.value("minimumValue").toString() == "any") {
if (stateType.minValue().isNull()) {
qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but state" << stateMap.value("name").toString() << "has no minimum value defined.";
//qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but state" << stateMap.value("name").toString() << "has no minimum value defined.";
valid = false;
continue;
}
} else if (stateMap.value("minimumValue") != stateType.minValue()) {
qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but state" << stateMap.value("name").toString() << "has not matching minimum value:" << stateMap.value("minimumValue") << "!=" << stateType.minValue();
//qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but state" << stateMap.value("name").toString() << "has not matching minimum value:" << stateMap.value("minimumValue") << "!=" << stateType.minValue();
valid = false;
continue;
}
@ -517,23 +517,23 @@ QList<DeviceClass> DevicePlugin::supportedDevices() const
if (stateMap.contains("maximumValue")) {
if (stateMap.value("maximumValue").toString() == "any") {
if (stateType.maxValue().isNull()) {
qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but state" << stateMap.value("name").toString() << "has no maximum value defined.";
//qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but state" << stateMap.value("name").toString() << "has no maximum value defined.";
valid = false;
continue;
}
} else if (stateMap.value("maximumValue") != stateType.maxValue()) {
qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but state" << stateMap.value("name").toString() << "has not matching maximum value:" << stateMap.value("maximumValue") << "!=" << stateType.minValue();
//qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but state" << stateMap.value("name").toString() << "has not matching maximum value:" << stateMap.value("maximumValue") << "!=" << stateType.minValue();
valid = false;
continue;
}
}
if (stateMap.contains("allowedValues") && stateMap.value("allowedValues") != stateType.possibleValues()) {
qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but state" << stateMap.value("name").toString() << "has not matching allowed values" << stateMap.value("allowedValues") << "!=" << stateType.possibleValues();
//qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but state" << stateMap.value("name").toString() << "has not matching allowed values" << stateMap.value("allowedValues") << "!=" << stateType.possibleValues();
valid = false;
continue;
}
if (stateMap.contains("writable") && stateMap.value("writable").toBool() && actionTypes.findById(ActionTypeId(stateType.id().toString())).id().isNull()) {
qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but state" << stateMap.value("name").toString() << "is not writable while it should be";
//qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but state" << stateMap.value("name").toString() << "is not writable while it should be";
valid = false;
continue;
}
@ -543,18 +543,18 @@ QList<DeviceClass> DevicePlugin::supportedDevices() const
QVariantMap actionMap = actionVariant.toMap();
ActionType actionType = actionTypes.findByName(actionMap.value("name").toString());
if (actionType.id().isNull()) {
qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but doesn't implement action" << actionMap.value("name").toString();
//qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but doesn't implement action" << actionMap.value("name").toString();
valid = false;
}
QVariantList params = actionMap.value("params").toList();
foreach (const QVariant &paramVariant, params) {
ParamType paramType = actionType.paramTypes().findByName(paramVariant.toMap().value("name").toString());
if (!paramType.isValid()) {
qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but doesn't implement action param" << actionMap.value("name").toString() << ":" << paramVariant.toMap().value("name").toString();
//qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but doesn't implement action param" << actionMap.value("name").toString() << ":" << paramVariant.toMap().value("name").toString();
valid = false;
} else {
if (paramType.type() != QVariant::nameToType(paramVariant.toMap().value("type").toString().toLatin1())) {
qCWarning(dcDeviceManager()) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but param" << paramType.name() << "is of wrong type:" << QVariant::typeToName(paramType.type()) << "expected:" << paramVariant.toMap().value("type").toString();
//qCWarning(dcDeviceManager()) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but param" << paramType.name() << "is of wrong type:" << QVariant::typeToName(paramType.type()) << "expected:" << paramVariant.toMap().value("type").toString();
valid = false;
}
}
@ -565,18 +565,18 @@ QList<DeviceClass> DevicePlugin::supportedDevices() const
QVariantMap eventMap = eventVariant.toMap();
EventType eventType = eventTypes.findByName(eventMap.value("name").toString());
if (eventType.isValid()) {
qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but doesn't implement event" << eventMap.value("name").toString();
//qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but doesn't implement event" << eventMap.value("name").toString();
valid = false;
}
QVariantList params = eventMap.value("params").toList();
foreach (const QVariant &paramVariant, params) {
ParamType paramType = eventType.paramTypes().findByName(paramVariant.toMap().value("name").toString());
if (!paramType.isValid()) {
qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but doesn't implement action param" << eventMap.value("name").toString() << ":" << paramVariant.toMap().value("name").toString();
//qCWarning(dcDeviceManager) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but doesn't implement action param" << eventMap.value("name").toString() << ":" << paramVariant.toMap().value("name").toString();
valid = false;
} else {
if (paramType.type() != QVariant::nameToType(paramVariant.toMap().value("type").toString().toLatin1())) {
qCWarning(dcDeviceManager()) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but param" << paramType.name() << "is of wrong type:" << QVariant::typeToName(paramType.type()) << "expected:" << paramVariant.toMap().value("type").toString();
//qCWarning(dcDeviceManager()) << "DeviceClass" << deviceClass.name() << "claims to implement interface" << value.toString() << "but param" << paramType.name() << "is of wrong type:" << QVariant::typeToName(paramType.type()) << "expected:" << paramVariant.toMap().value("type").toString();
valid = false;
}
}

View File

@ -157,6 +157,7 @@ void GuhTestBase::initTestCase()
s_loggingFilters.insert("OAuth2", true);
s_loggingFilters.insert("TimeManager", true);
QHash<QString, bool> loggingFiltersPlugins;
foreach (const QJsonObject &pluginMetadata, DeviceManager::pluginsMetadata()) {
loggingFiltersPlugins.insert(pluginMetadata.value("idName").toString(), false);

View File

@ -1213,13 +1213,14 @@ void TestTimeManager::testCalendarItemEvent()
} else {
verifyRuleNotExecuted();
}
}
void TestTimeManager::testCalendarItemStatesEvent_data()
{
initTimeManager();
GuhCore::instance()->timeManager()->setTime(QDateTime(QDate::currentDate(), QTime(7,59)));
// Action (without params)
QVariantMap action;
action.insert("actionTypeId", mockActionIdNoParams);
@ -1243,14 +1244,12 @@ void TestTimeManager::testCalendarItemStatesEvent_data()
// The rule
QVariantMap ruleMap;
ruleMap.insert("name", "Time and state based daily calendar rule");
ruleMap.insert("name", "Time and state and event based daily calendar rule");
ruleMap.insert("actions", QVariantList() << action);
ruleMap.insert("eventDescriptors", QVariantList() << eventDescriptor);
ruleMap.insert("stateEvaluator", QVariantList() << stateEvaluator);
ruleMap.insert("stateEvaluator", stateEvaluator);
ruleMap.insert("timeDescriptor", createTimeDescriptorCalendar(createCalendarItem("08:00", 10)));
GuhCore::instance()->timeManager()->setTime(QDateTime(QDate::currentDate(), QTime(7,59)));
QVariant response = injectAndWait("Rules.AddRule", ruleMap);
verifyRuleError(response);
RuleId ruleId = RuleId(response.toMap().value("params").toMap().value("ruleId").toString());
@ -1258,8 +1257,6 @@ void TestTimeManager::testCalendarItemStatesEvent_data()
QVariantMap params;
params.insert("ruleId", ruleId);
response = injectAndWait("Rules.GetRuleDetails", params);
printJson(response);
QTest::addColumn<QDateTime>("dateTime");
QTest::addColumn<bool>("boolValue");
@ -1936,11 +1933,24 @@ void TestTimeManager::triggerMockEvent1()
QNetworkAccessManager nam;
QSignalSpy spy(&nam, SIGNAL(finished(QNetworkReply*)));
QSignalSpy eventSpy(m_mockTcpServer, SIGNAL(outgoingData(QUuid,QByteArray)));
QNetworkRequest request(QUrl(QString("http://localhost:%1/generateevent?eventtypeid=%2").arg(m_mockDevice1Port).arg(mockEvent1Id.toString())));
QNetworkReply *reply = nam.get(request);
spy.wait();
QCOMPARE(spy.count(), 1);
reply->deleteLater();
eventSpy.wait(200);
QVariantList eventTriggerVariants = checkNotifications(eventSpy, "Events.EventTriggered");
QVERIFY2(eventTriggerVariants.count() == 1, "Did not get Events.EventTriggered notification.");
QVERIFY2(eventTriggerVariants.first().toMap().value("params").toMap().contains("event"), "Notification Events.EventTriggered does not contain event.");
QVariantMap eventMap = eventTriggerVariants.first().toMap().value("params").toMap().value("event").toMap();
QVERIFY2(eventMap.contains("deviceId"), "Events.EventTriggered notification does not contain deviceId");
QVERIFY2(DeviceId(eventMap.value("deviceId").toString()) == m_mockDeviceId, "Events.EventTriggered notification does not contain the correct deviceId");
QVERIFY2(eventMap.contains("eventTypeId"), "Events.EventTriggered notification does not contain eventTypeId");
QVERIFY2(EventTypeId(eventMap.value("eventTypeId").toString()) == mockEvent1Id, "Events.EventTriggered notification does not contain the correct eventTypeId");
}
QVariantMap TestTimeManager::createTimeEventItem(const QString &time, const QVariantMap &repeatingOption) const