From 74397da6c1c11b18e47ace1e6872fce8a0c99281 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Mon, 9 Oct 2017 14:09:34 +0200 Subject: [PATCH] add state/calendarItem/event tests --- libguh-core/jsonrpc/jsontypes.cpp | 5 +++- libguh/plugin/deviceplugin.cpp | 28 +++++++++++----------- tests/auto/guhtestbase.cpp | 1 + tests/auto/timemanager/testtimemanager.cpp | 24 +++++++++++++------ 4 files changed, 36 insertions(+), 22 deletions(-) diff --git a/libguh-core/jsonrpc/jsontypes.cpp b/libguh-core/jsonrpc/jsontypes.cpp index d5be8e99..b530288f 100644 --- a/libguh-core/jsonrpc/jsontypes.cpp +++ b/libguh-core/jsonrpc/jsontypes.cpp @@ -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 childEvaluators; foreach (const QVariant &childEvaluator, stateEvaluatorMap.value("childEvaluators").toList()) diff --git a/libguh/plugin/deviceplugin.cpp b/libguh/plugin/deviceplugin.cpp index 19847554..ff6531e9 100644 --- a/libguh/plugin/deviceplugin.cpp +++ b/libguh/plugin/deviceplugin.cpp @@ -492,24 +492,24 @@ QList 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 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 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 ¶mVariant, 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 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 ¶mVariant, 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; } } diff --git a/tests/auto/guhtestbase.cpp b/tests/auto/guhtestbase.cpp index b8c09b7e..7ca064ba 100644 --- a/tests/auto/guhtestbase.cpp +++ b/tests/auto/guhtestbase.cpp @@ -157,6 +157,7 @@ void GuhTestBase::initTestCase() s_loggingFilters.insert("OAuth2", true); s_loggingFilters.insert("TimeManager", true); + QHash loggingFiltersPlugins; foreach (const QJsonObject &pluginMetadata, DeviceManager::pluginsMetadata()) { loggingFiltersPlugins.insert(pluginMetadata.value("idName").toString(), false); diff --git a/tests/auto/timemanager/testtimemanager.cpp b/tests/auto/timemanager/testtimemanager.cpp index 30f2df9f..ef06c96a 100644 --- a/tests/auto/timemanager/testtimemanager.cpp +++ b/tests/auto/timemanager/testtimemanager.cpp @@ -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("dateTime"); QTest::addColumn("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