summaryrefslogtreecommitdiffstats
path: root/tests/manual/eventsubscription/tst_eventsubscription.cpp
blob: 49ab75b12fa096af03bfca0e1b953f19010a3258 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/****************************************************************************
**
** Copyright (C) 2018 basysKom GmbH, opensource@basyskom.com
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtOpcUa module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL3$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QString>
#include <QtTest>
#include <QCoreApplication>
#include <QtOpcUa>

class OpcuaConnector
{
public:
    OpcuaConnector(QOpcUaClient *client, const QString &endPoint)
        : opcuaClient(client)
    {
        QVERIFY(opcuaClient != nullptr);
        QSignalSpy connectedSpy(opcuaClient, &QOpcUaClient::connected);
        QSignalSpy disconnectedSpy(opcuaClient, &QOpcUaClient::disconnected);
        QSignalSpy stateSpy(opcuaClient, &QOpcUaClient::stateChanged);

        QTest::qWait(500);
        opcuaClient->connectToEndpoint(QUrl(endPoint));
        QTRY_VERIFY2(opcuaClient->state() == QOpcUaClient::Connected, "Could not connect to server");

        QCOMPARE(connectedSpy.count(), 1); // one connected signal fired
        QCOMPARE(disconnectedSpy.count(), 0); // zero disconnected signals fired
        QCOMPARE(stateSpy.count(), 2);

        QCOMPARE(stateSpy.at(0).at(0).value<QOpcUaClient::ClientState>(),
                 QOpcUaClient::ClientState::Connecting);
        QCOMPARE(stateSpy.at(1).at(0).value<QOpcUaClient::ClientState>(),
                 QOpcUaClient::ClientState::Connected);

        stateSpy.clear();
        connectedSpy.clear();
        disconnectedSpy.clear();

        QVERIFY(opcuaClient->url() == QUrl(endPoint));
    }

    ~OpcuaConnector()
    {
        QSignalSpy connectedSpy(opcuaClient, &QOpcUaClient::connected);
        QSignalSpy disconnectedSpy(opcuaClient, &QOpcUaClient::disconnected);
        QSignalSpy stateSpy(opcuaClient, &QOpcUaClient::stateChanged);
        QSignalSpy errorSpy(opcuaClient, &QOpcUaClient::errorChanged);

        QVERIFY(opcuaClient != nullptr);
        if (opcuaClient->state() == QOpcUaClient::Connected) {

            opcuaClient->disconnectFromEndpoint();

            QTRY_VERIFY(opcuaClient->state() == QOpcUaClient::Disconnected);

            QCOMPARE(connectedSpy.count(), 0);
            QCOMPARE(disconnectedSpy.count(), 1);
            QCOMPARE(stateSpy.count(), 2);
            QCOMPARE(stateSpy.at(0).at(0).value<QOpcUaClient::ClientState>(),
                     QOpcUaClient::ClientState::Closing);
            QCOMPARE(stateSpy.at(1).at(0).value<QOpcUaClient::ClientState>(),
                     QOpcUaClient::ClientState::Disconnected);
            QCOMPARE(errorSpy.size(), 0);
        }

        opcuaClient = nullptr;
    }

    QOpcUaClient *opcuaClient;
};

class EventsubscriptionTest : public QObject
{
    Q_OBJECT

public:
    EventsubscriptionTest();

private Q_SLOTS:
    void initTestCase();
    void cleanupTestCase();
    void eventSubscription_data();
    void eventSubscription();

private:
    QVector<QOpcUaClient *> m_clients;
    QString m_serverUrl;
};

EventsubscriptionTest::EventsubscriptionTest()
{
    m_serverUrl = qEnvironmentVariable("TESTSERVER_URL",
            QStringLiteral("opc.tcp://localhost:4840"));
}

void EventsubscriptionTest::initTestCase()
{
    QOpcUaProvider provider;
    const QStringList backends = provider.availableBackends();

    for (auto it : backends) {
        QOpcUaClient *temp = provider.createClient(it);
        if (temp)
            m_clients.append(temp);
        else
            QFAIL(QStringLiteral("Failed to create client for backend %1").arg(it).toUtf8().constData());
    }
}

void EventsubscriptionTest::cleanupTestCase()
{
    qDeleteAll(m_clients);
    m_clients.clear();
}

void EventsubscriptionTest::eventSubscription_data()
{
    QTest::addColumn<QOpcUaClient *>("client");
    for (auto it : m_clients)
        QTest::newRow(it->backend().toLatin1().constData()) << it;
}

/*
    This manual test requires the open62541 tutorial_server_events.c example
    for the server side
*/
void EventsubscriptionTest::eventSubscription()
{
    QFETCH(QOpcUaClient *, client);
    OpcuaConnector connector(client, m_serverUrl);

    QScopedPointer<QOpcUaNode> serverNode(client->node("ns=0;i=2253")); // Server object
    QVERIFY(serverNode != nullptr);

    QScopedPointer<QOpcUaNode> objectsNode(client->node("ns=0;i=85")); // Objects folder
    QVERIFY(objectsNode != nullptr);

    qDebug() << "Node created";

    QSignalSpy enabledSpy(serverNode.data(), &QOpcUaNode::enableMonitoringFinished);
    QSignalSpy eventSpy(serverNode.data(), &QOpcUaNode::eventOccurred);

    QOpcUaMonitoringParameters::EventFilter filter;
    filter << QOpcUa::QSimpleAttributeOperand("Severity");
    filter << QOpcUa::QSimpleAttributeOperand("Message");

//    Where clause is not yet supported in the open62541 server
//    // Only events with severity >= 700
//    QOpcUa::QContentFilterElement whereElement;
//    whereElement << QOpcUa::QContentFilterElement::FilterOperator::GreaterThanOrEqual << QOpcUa::QSimpleAttributeOperand("Severity") <<
//                    QOpcUa::QLiteralOperand(quint16(700), QOpcUa::Types::UInt16);
//    filter << whereElement;

    QOpcUaMonitoringParameters p(0);
    p.setFilter(filter);

    serverNode->enableMonitoring(QOpcUa::NodeAttribute::EventNotifier, p);
    enabledSpy.wait();
    QCOMPARE(enabledSpy.size(), 1);
    QCOMPARE(enabledSpy.at(0).at(0).value<QOpcUa::NodeAttribute>(), QOpcUa::NodeAttribute::EventNotifier);
    QCOMPARE(enabledSpy.at(0).at(1).value<QOpcUa::UaStatusCode>(), QOpcUa::UaStatusCode::Good);

    QCOMPARE(serverNode->monitoringStatus(QOpcUa::NodeAttribute::EventNotifier).filter().value<QOpcUaMonitoringParameters::EventFilter>(),
             filter);

//    Disabled because the open62541 server does not currently return an EventFilterResult
//    QVERIFY(serverNode->monitoringStatus(QOpcUa::NodeAttribute::EventNotifier).filterResult().isValid());
//    QOpcUa::QEventFilterResult res = serverNode->monitoringStatus(QOpcUa::NodeAttribute::EventNotifier).filterResult().value<QOpcUa::QEventFilterResult>();
//    QVERIFY(res.isGood() == true);

    qDebug() << "Monitoring enabled, waiting for event...";

    objectsNode->callMethod(QStringLiteral("ns=1;i=62541")); // Trigger event
    eventSpy.wait();
    QCOMPARE(eventSpy.size(), 1);
    QCOMPARE(eventSpy.at(0).at(0).toList().size(), 2);
    quint16 severity = eventSpy.at(0).at(0).toList().at(0).value<quint16>();
    QOpcUa::QLocalizedText message = eventSpy.at(0).at(0).toList().at(1).value<QOpcUa::QLocalizedText>();

    qDebug() << "Event received";
    qDebug() << "Message:" << message.locale() << message.text();
    qDebug() << "Severity:" << severity;

    QCOMPARE(severity, 100);
    QCOMPARE(message, QOpcUa::QLocalizedText("en-US", "An event has been generated."));

    qDebug() << "Modifying event filter...";

    eventSpy.clear();

    QSignalSpy modifySpy(serverNode.data(), &QOpcUaNode::monitoringStatusChanged);
    filter << QOpcUa::QSimpleAttributeOperand("SourceNode");
    serverNode->modifyEventFilter(filter);
    modifySpy.wait();
    QCOMPARE(modifySpy.size(), 1);
    QCOMPARE(modifySpy.at(0).at(0).value<QOpcUa::NodeAttribute>(), QOpcUa::NodeAttribute::EventNotifier);
    QVERIFY(modifySpy.at(0).at(1).value<QOpcUaMonitoringParameters::Parameters>().testFlag(QOpcUaMonitoringParameters::Parameter::Filter) == true);
    QCOMPARE(modifySpy.at(0).at(2).value<QOpcUa::UaStatusCode>(), QOpcUa::UaStatusCode::Good);
    QCOMPARE(serverNode->monitoringStatus(QOpcUa::NodeAttribute::EventNotifier).filter().value<QOpcUaMonitoringParameters::EventFilter>(),
             filter);
    qDebug() << "EventFilter modified, waiting for event with additional SourceNode field...";

    objectsNode->callMethod(QStringLiteral("ns=1;i=62541")); // Trigger event
    eventSpy.wait();
    QCOMPARE(eventSpy.size(), 1);
    QCOMPARE(eventSpy.at(0).at(0).toList().size(), 3);
    severity = eventSpy.at(0).at(0).toList().at(0).value<quint16>();
    message = eventSpy.at(0).at(0).toList().at(1).value<QOpcUa::QLocalizedText>();
    QString sourceNode = eventSpy.at(0).at(0).toList().at(2).toString();

    qDebug() << "Event received";
    qDebug() << "Message:" << message.locale() << message.text();
    qDebug() << "Severity:" << severity;
    qDebug() << "SourceNode:" << sourceNode;

    QCOMPARE(severity, 100);
    QCOMPARE(message, QOpcUa::QLocalizedText("en-US", "An event has been generated."));
    QCOMPARE(sourceNode, QStringLiteral("ns=0;i=2253"));

    QSignalSpy disabledSpy(serverNode.data(), &QOpcUaNode::disableMonitoringFinished);
    serverNode->disableMonitoring(QOpcUa::NodeAttribute::EventNotifier);
    disabledSpy.wait();
    QCOMPARE(disabledSpy.size(), 1);
    QCOMPARE(disabledSpy.at(0).at(0).value<QOpcUa::NodeAttribute>(), QOpcUa::NodeAttribute::EventNotifier);
    QCOMPARE(disabledSpy.at(0).at(1).value<QOpcUa::UaStatusCode>(), QOpcUa::UaStatusCode::Good);
}

QTEST_MAIN(EventsubscriptionTest)

#include "tst_eventsubscription.moc"