start fixing rules
This commit is contained in:
parent
aa5cba8189
commit
e454351865
@ -48,6 +48,8 @@ private:
|
|||||||
void verifyRuleExecuted(const ActionTypeId &actionTypeId);
|
void verifyRuleExecuted(const ActionTypeId &actionTypeId);
|
||||||
void verifyRuleNotExecuted();
|
void verifyRuleNotExecuted();
|
||||||
|
|
||||||
|
void triggerMockEvent();
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void addRemoveRules_data();
|
void addRemoveRules_data();
|
||||||
void addRemoveRules();
|
void addRemoveRules();
|
||||||
@ -140,6 +142,18 @@ void TestRestRules::verifyRuleNotExecuted()
|
|||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TestRestRules::triggerMockEvent()
|
||||||
|
{
|
||||||
|
// trigger event in mock device
|
||||||
|
QNetworkAccessManager nam;
|
||||||
|
QSignalSpy spy(&nam, SIGNAL(finished(QNetworkReply*)));
|
||||||
|
QNetworkRequest request = QNetworkRequest(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();
|
||||||
|
}
|
||||||
|
|
||||||
void TestRestRules::addRemoveRules_data()
|
void TestRestRules::addRemoveRules_data()
|
||||||
{
|
{
|
||||||
// RuleAction
|
// RuleAction
|
||||||
@ -806,92 +820,43 @@ void TestRestRules::enableDisableRule()
|
|||||||
actions.append(action);
|
actions.append(action);
|
||||||
addRuleParams.insert("actions", actions);
|
addRuleParams.insert("actions", actions);
|
||||||
|
|
||||||
QNetworkAccessManager *nam = new QNetworkAccessManager(this);
|
|
||||||
QSignalSpy clientSpy(nam, SIGNAL(finished(QNetworkReply*)));
|
|
||||||
|
|
||||||
// ADD rule
|
// ADD rule
|
||||||
QNetworkRequest request = QNetworkRequest(QUrl(QString("http://localhost:3333/api/v1/rules")));
|
QNetworkRequest request = QNetworkRequest(QUrl(QString("http://localhost:3333/api/v1/rules")));
|
||||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "text/json");
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "text/json");
|
||||||
QNetworkReply *reply = nam->post(request, QJsonDocument::fromVariant(addRuleParams).toJson(QJsonDocument::Compact));
|
|
||||||
clientSpy.wait();
|
|
||||||
QVERIFY2(clientSpy.count() == 1, "expected exactly 1 response from webserver");
|
|
||||||
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
||||||
QCOMPARE(statusCode, 200);
|
|
||||||
QByteArray data = reply->readAll();
|
|
||||||
reply->deleteLater();
|
|
||||||
|
|
||||||
QJsonParseError error;
|
QVariant response = postAndWait(request, addRuleParams);
|
||||||
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &error);
|
RuleId ruleId = RuleId(response.toMap().value("id").toString());
|
||||||
QCOMPARE(error.error, QJsonParseError::NoError);
|
|
||||||
|
|
||||||
RuleId ruleId = RuleId(jsonDoc.toVariant().toMap().value("id").toString());
|
|
||||||
QVERIFY(!ruleId.isNull());
|
QVERIFY(!ruleId.isNull());
|
||||||
|
|
||||||
// ENABLE rule
|
// ENABLE rule
|
||||||
clientSpy.clear();
|
|
||||||
request = QNetworkRequest(QUrl(QString("http://localhost:3333/api/v1/rules/%1/enable").arg(ruleId.toString())));
|
request = QNetworkRequest(QUrl(QString("http://localhost:3333/api/v1/rules/%1/enable").arg(ruleId.toString())));
|
||||||
reply = nam->post(request, QByteArray());
|
response = postAndWait(request, QVariant());
|
||||||
clientSpy.wait();
|
QVERIFY2(!response.isNull(), "Could not read response");
|
||||||
QVERIFY2(clientSpy.count() == 1, "expected exactly 1 response from webserver");
|
|
||||||
statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
||||||
QCOMPARE(statusCode, 200);
|
|
||||||
reply->deleteLater();
|
|
||||||
|
|
||||||
// Trigger an event
|
// Trigger an event
|
||||||
|
triggerMockEvent();
|
||||||
// trigger event in mock device
|
|
||||||
clientSpy.clear();
|
|
||||||
request = QNetworkRequest(QUrl(QString("http://localhost:%1/generateevent?eventtypeid=%2").arg(m_mockDevice1Port).arg(mockEvent1Id.toString())));
|
|
||||||
reply = nam->get(request);
|
|
||||||
clientSpy.wait();
|
|
||||||
QCOMPARE(clientSpy.count(), 1);
|
|
||||||
reply->deleteLater();
|
|
||||||
|
|
||||||
verifyRuleExecuted(mockActionIdNoParams);
|
verifyRuleExecuted(mockActionIdNoParams);
|
||||||
|
|
||||||
cleanupMockHistory();
|
cleanupMockHistory();
|
||||||
|
|
||||||
// DISABLE the rule
|
// DISABLE the rule
|
||||||
clientSpy.clear();
|
|
||||||
request = QNetworkRequest(QUrl(QString("http://localhost:3333/api/v1/rules/%1/disable").arg(ruleId.toString())));
|
request = QNetworkRequest(QUrl(QString("http://localhost:3333/api/v1/rules/%1/disable").arg(ruleId.toString())));
|
||||||
reply = nam->post(request, QByteArray());
|
response = postAndWait(request, QVariant());
|
||||||
clientSpy.wait();
|
QVERIFY2(!response.isNull(), "Could not read response");
|
||||||
QVERIFY2(clientSpy.count() == 1, "expected exactly 1 response from webserver");
|
|
||||||
statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
||||||
QCOMPARE(statusCode, 200);
|
|
||||||
reply->deleteLater();
|
|
||||||
|
|
||||||
|
|
||||||
// trigger event in mock device
|
// trigger event in mock device
|
||||||
clientSpy.clear();
|
triggerMockEvent();
|
||||||
request = QNetworkRequest(QUrl(QString("http://localhost:%1/generateevent?eventtypeid=%2").arg(m_mockDevice1Port).arg(mockEvent1Id.toString())));
|
|
||||||
reply = nam->get(request);
|
|
||||||
clientSpy.wait();
|
|
||||||
QCOMPARE(clientSpy.count(), 1);
|
|
||||||
reply->deleteLater();
|
|
||||||
|
|
||||||
verifyRuleNotExecuted();
|
verifyRuleNotExecuted();
|
||||||
|
|
||||||
cleanupMockHistory();
|
cleanupMockHistory();
|
||||||
|
|
||||||
// ENABLE again
|
// ENABLE again
|
||||||
clientSpy.clear();
|
|
||||||
request = QNetworkRequest(QUrl(QString("http://localhost:3333/api/v1/rules/%1/enable").arg(ruleId.toString())));
|
request = QNetworkRequest(QUrl(QString("http://localhost:3333/api/v1/rules/%1/enable").arg(ruleId.toString())));
|
||||||
reply = nam->post(request, QByteArray());
|
response = postAndWait(request, QVariant());
|
||||||
clientSpy.wait();
|
QVERIFY2(!response.isNull(), "Could not read response");
|
||||||
QVERIFY2(clientSpy.count() == 1, "expected exactly 1 response from webserver");
|
|
||||||
statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
||||||
QCOMPARE(statusCode, 200);
|
|
||||||
reply->deleteLater();
|
|
||||||
|
|
||||||
// trigger event in mock device
|
// trigger event in mock device
|
||||||
clientSpy.clear();
|
triggerMockEvent();
|
||||||
request = QNetworkRequest(QUrl(QString("http://localhost:%1/generateevent?eventtypeid=%2").arg(m_mockDevice1Port).arg(mockEvent1Id.toString())));
|
|
||||||
reply = nam->get(request);
|
|
||||||
clientSpy.wait();
|
|
||||||
QCOMPARE(clientSpy.count(), 1);
|
|
||||||
reply->deleteLater();
|
|
||||||
|
|
||||||
verifyRuleExecuted(mockActionIdNoParams);
|
verifyRuleExecuted(mockActionIdNoParams);
|
||||||
|
|
||||||
cleanupRules();
|
cleanupRules();
|
||||||
@ -902,7 +867,7 @@ void TestRestRules::getRules()
|
|||||||
// Get all rules
|
// Get all rules
|
||||||
QVariant response = getAndWait(QNetworkRequest(QUrl("http://localhost:3333/api/v1/rules")));
|
QVariant response = getAndWait(QNetworkRequest(QUrl("http://localhost:3333/api/v1/rules")));
|
||||||
QVariantList rulesList = response.toList();
|
QVariantList rulesList = response.toList();
|
||||||
QVERIFY2(rulesList.count() == 0, "there should be at least one vendor.");
|
QVERIFY2(rulesList.count() == 0, "Not enought rules");
|
||||||
|
|
||||||
foreach (const QVariant &rule, rulesList) {
|
foreach (const QVariant &rule, rulesList) {
|
||||||
QVariantMap ruleMap = rule.toMap();
|
QVariantMap ruleMap = rule.toMap();
|
||||||
|
|||||||
Reference in New Issue
Block a user