fixed tests and bump api

This commit is contained in:
Simon Stürz 2015-03-12 15:19:35 +01:00 committed by Michael Zanetti
parent 6bd8983ec2
commit 0603b8a3b7
9 changed files with 96 additions and 23 deletions

View File

@ -1,3 +1,21 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* This file is part of guh. *
* *
* Guh is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, version 2 of the License. *
* *
* Guh is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with guh. If not, see <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "ruleaction.h"
RuleAction::RuleAction(const ActionTypeId &actionTypeId, const DeviceId &deviceId) :

View File

@ -1,3 +1,21 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* This file is part of guh. *
* *
* Guh is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, version 2 of the License. *
* *
* Guh is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with guh. If not, see <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef RULEACTION_H
#define RULEACTION_H

View File

@ -35,7 +35,8 @@
* \sa Param, */
RuleActionParam::RuleActionParam(const Param &param) :
m_name(param.name()),
m_value(param.value())
m_value(param.value()),
m_eventTypeId(EventTypeId())
{
}

View File

@ -44,7 +44,7 @@
#include <QJsonDocument>
#include <QStringList>
#define JSON_PROTOCOL_VERSION 15
#define JSON_PROTOCOL_VERSION 16
JsonRPCServer::JsonRPCServer(QObject *parent):
JsonHandler(parent),

View File

@ -104,7 +104,7 @@ void JsonTypes::init()
// RuleActionParam
s_ruleActionParam.insert("name", basicTypeToString(String));
s_ruleActionParam.insert("o:value", basicTypeRef());
s_ruleActionParam.insert("o:eventId", basicTypeToString(Uuid));
s_ruleActionParam.insert("o:eventTypeId", basicTypeToString(Uuid));
// ParamDescriptor
s_paramDescriptor.insert("name", basicTypeToString(String));
@ -355,8 +355,13 @@ QVariantMap JsonTypes::packRuleActionParam(const RuleActionParam &ruleActionPara
{
QVariantMap variantMap;
variantMap.insert("name", ruleActionParam.name());
variantMap.insert("value", ruleActionParam.value());
variantMap.insert("eventTypeId", ruleActionParam.eventTypeId());
// if this ruleaction param has a valid EventTypeId, there is no value
if (ruleActionParam.eventTypeId() != EventTypeId()) {
variantMap.insert("eventTypeId", ruleActionParam.eventTypeId());
} else {
variantMap.insert("value", ruleActionParam.value());
}
return variantMap;
}
@ -843,6 +848,18 @@ QPair<bool, QString> JsonTypes::validateVariant(const QVariant &templateVariant,
qDebug() << "param types not matching";
return result;
}
} else if (refName == ruleActionRef()) {
QPair<bool, QString> result = validateMap(ruleActionDescription(), variant.toMap());
if (!result.first) {
qDebug() << "ruleAction type not matching";
return result;
}
} else if (refName == ruleActionParamRef()) {
QPair<bool, QString> result = validateMap(ruleActionParamDescription(), variant.toMap());
if (!result.first) {
qDebug() << "ruleActionParam type not matching";
return result;
}
} else if (refName == actionTypeRef()) {
QPair<bool, QString> result = validateMap(actionTypeDescription(), variant.toMap());
if (!result.first) {

View File

@ -50,11 +50,11 @@ RulesHandler::RulesHandler(QObject *parent) :
params.insert("o:eventDescriptor", JsonTypes::eventDescriptorRef());
params.insert("o:eventDescriptorList", QVariantList() << JsonTypes::eventDescriptorRef());
params.insert("o:stateEvaluator", JsonTypes::stateEvaluatorRef());
params.insert("o:exitActions", QVariantList() << JsonTypes::actionRef());
params.insert("o:exitActions", QVariantList() << JsonTypes::ruleActionRef());
params.insert("o:enabled", JsonTypes::basicTypeToString(JsonTypes::Bool));
params.insert("name", JsonTypes::basicTypeToString(JsonTypes::String));
QVariantList actions;
actions.append(JsonTypes::actionRef());
actions.append(JsonTypes::ruleActionRef());
params.insert("actions", actions);
setParams("AddRule", params);
returns.insert("ruleError", JsonTypes::ruleErrorRef());
@ -173,13 +173,14 @@ JsonReply* RulesHandler::AddRule(const QVariantMap &params)
// We have an eventTypeId
if (eventDescriptorList.isEmpty()) {
QVariantMap returns;
qWarning() << "RuleAction" << ruleAction.actionTypeId() << "contains an eventTypeId, but there areno eventDescriptors.";
qWarning() << "RuleAction" << ruleAction.actionTypeId() << "contains an eventTypeId, but there are no eventDescriptors.";
returns.insert("ruleErorr", JsonTypes::ruleErrorToString(RuleEngine::RuleErrorInvalidRuleActionPatameter));
return createReply(returns);
}
// now check if this eventType is in the eventDescriptorList of this rule
foreach (const EventDescriptor eventDescriptor, eventDescriptorList) {
if (eventDescriptor.eventTypeId() == ruleActionParam.eventTypeId()) {
// TODO: check if they match
continue;
} else {
// the given eventTypeId is not in the eventDescriptorList
@ -207,7 +208,7 @@ JsonReply* RulesHandler::AddRule(const QVariantMap &params)
foreach (const QVariant &actionVariant, exitActionList) {
QVariantMap actionMap = actionVariant.toMap();
RuleAction action(ActionTypeId(actionMap.value("actionTypeId").toString()), DeviceId(actionMap.value("deviceId").toString()));
qDebug() << "params from json" << actionMap.value("ruleActionParams");
qDebug() << "params from json" << actionMap.value("ruleActionParams");
action.setRuleActionParams(JsonTypes::unpackRuleActionParams(actionMap.value("ruleActionParams").toList()));
qDebug() << "params in action" << action.ruleActionParams();
exitActions.append(action);

View File

@ -139,7 +139,7 @@ RuleEngine::RuleEngine(QObject *parent) :
foreach (QString paramNameString, settings.childGroups()) {
if (paramNameString.startsWith("RuleActionParam-")) {
settings.beginGroup(paramNameString);
RuleActionParam param(paramNameString.remove(QRegExp("^RuleActionParam-")), settings.value("value"));
RuleActionParam param(paramNameString.remove(QRegExp("^RuleActionParam-")), settings.value("value",QVariant()), EventTypeId(settings.value("eventTypeId", EventTypeId()).toString()));
params.append(param);
settings.endGroup();
}
@ -354,6 +354,7 @@ RuleEngine::RuleError RuleEngine::addRule(const RuleId &ruleId, const QString &n
settings.setValue("actionTypeId", action.actionTypeId());
foreach (const RuleActionParam &param, action.ruleActionParams()) {
settings.beginGroup("RuleActionParam-" + param.name());
settings.setValue("eventTypeId", param.eventTypeId());
settings.setValue("value", param.value());
settings.endGroup();
}
@ -362,7 +363,7 @@ RuleEngine::RuleError RuleEngine::addRule(const RuleId &ruleId, const QString &n
settings.endGroup();
settings.beginGroup("exitActions");
settings.beginGroup("ruleExitActions");
foreach (const RuleAction &action, rule.exitActions()) {
settings.beginGroup(action.actionTypeId().toString());
settings.setValue("deviceId", action.deviceId());

View File

@ -1,4 +1,4 @@
15
16
{
"methods": {
"Actions.ExecuteAction": {
@ -277,7 +277,7 @@
"description": "Add a rule. You can describe rules by one or many EventDesciptors and a StateEvaluator. Note that onlyone of either eventDescriptor or eventDescriptorList may be passed at a time. A rule can be created but left disabled,meaning it won't actually be executed until set to enabled. If not given, enabled defaults to true.",
"params": {
"actions": [
"$ref:Action"
"$ref:RuleAction"
],
"name": "String",
"o:enabled": "Bool",
@ -286,7 +286,7 @@
"$ref:EventDescriptor"
],
"o:exitActions": [
"$ref:Action"
"$ref:RuleAction"
],
"o:stateEvaluator": "$ref:StateEvaluator"
},
@ -572,19 +572,31 @@
],
"Rule": {
"actions": [
"$ref:Action"
"$ref:RuleAction"
],
"enabled": "Bool",
"eventDescriptors": [
"$ref:EventDescriptor"
],
"exitActions": [
"$ref:Action"
"$ref:RuleAction"
],
"id": "Uuid",
"name": "String",
"stateEvaluator": "$ref:StateEvaluator"
},
"RuleAction": {
"actionTypeId": "Uuid",
"deviceId": "Uuid",
"o:ruleActionParams": [
"$ref:RuleActionParam"
]
},
"RuleActionParam": {
"name": "String",
"o:eventTypeId": "Uuid",
"o:value": "$ref:BasicType"
},
"RuleError": [
"RuleErrorNoError",
"RuleErrorInvalidRuleId",
@ -594,7 +606,8 @@
"RuleErrorActionTypeNotFound",
"RuleErrorInvalidParameter",
"RuleErrorInvalidRuleFormat",
"RuleErrorMissingParameter"
"RuleErrorMissingParameter",
"RuleErrorInvalidRuleActionPatameter"
],
"SetupMethod": [
"SetupMethodJustAdd",

View File

@ -126,22 +126,22 @@ void TestRules::addRemoveRules_data()
QVariantMap validActionNoParams;
validActionNoParams.insert("actionTypeId", mockActionIdNoParams);
validActionNoParams.insert("deviceId", m_mockDeviceId);
validActionNoParams.insert("params", QVariantList());
validActionNoParams.insert("ruleActionParams", QVariantList());
QVariantMap invalidAction;
invalidAction.insert("actionTypeId", ActionTypeId());
invalidAction.insert("deviceId", m_mockDeviceId);
invalidAction.insert("params", QVariantList());
invalidAction.insert("ruleActionParams", QVariantList());
QVariantMap validExitActionNoParams;
validExitActionNoParams.insert("actionTypeId", mockActionIdNoParams);
validExitActionNoParams.insert("deviceId", m_mockDeviceId);
validExitActionNoParams.insert("params", QVariantList());
validExitActionNoParams.insert("ruleActionParams", QVariantList());
QVariantMap invalidExitAction;
invalidExitAction.insert("actionTypeId", ActionTypeId());
invalidExitAction.insert("deviceId", m_mockDeviceId);
invalidExitAction.insert("params", QVariantList());
invalidExitAction.insert("ruleActionParams", QVariantList());
QVariantMap stateDescriptor;
stateDescriptor.insert("stateTypeId", mockIntStateId);
@ -363,7 +363,7 @@ void TestRules::loadStoreConfig()
QVariantMap action1;
action1.insert("actionTypeId", mockActionIdNoParams);
action1.insert("deviceId", m_mockDeviceId);
action1.insert("params", QVariantList());
action1.insert("ruleActionParams", QVariantList());
QVariantMap action2;
action2.insert("actionTypeId", mockActionIdWithParams);
@ -378,7 +378,7 @@ void TestRules::loadStoreConfig()
action2Param2.insert("name", "mockActionParam2");
action2Param2.insert("value", true);
action2Params.append(action2Param2);
action2.insert("params", action2Params);
action2.insert("ruleActionParams", action2Params);
// First rule
QVariantMap params;
@ -454,6 +454,10 @@ void TestRules::loadStoreConfig()
}
QVariantList replyActions = rule1.value("actions").toList();
qDebug() << "----------------------------------------------";
qDebug() << rule1;
qDebug() << "----------------------------------------------";
foreach (const QVariant &actionVariant, actions) {
bool found = false;
foreach (const QVariant &replyActionVariant, replyActions) {