surveyapitest.cpp 5.73 KB
Newer Older
Volker Krause's avatar
Volker Krause committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
    Copyright (C) 2016 Volker Krause <vkrause@kde.org>

    This program is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This program 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 Library General Public
    License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

18
19
#include "servercontroller.h"

Volker Krause's avatar
Volker Krause committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <rest/restapi.h>
#include <rest/restclient.h>

#include <core/product.h>
#include <core/survey.h>

#include <QDebug>
#include <QtTest/qtest.h>
#include <QNetworkReply>
#include <QObject>
#include <QSignalSpy>
#include <QStandardPaths>

#include <limits>

using namespace UserFeedback::Analyzer;

class SurveyApiTest : public QObject
{
    Q_OBJECT
private:
41
42
    ServerController m_server;

Volker Krause's avatar
Volker Krause committed
43
44
45
    ServerInfo testServer() const
    {
        ServerInfo s;
46
        s.setUrl(m_server.url());
Volker Krause's avatar
Volker Krause committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
        return s;
    }

    bool waitForFinished(QNetworkReply *reply)
    {
        Q_ASSERT(reply);
        QSignalSpy spy(reply, &QNetworkReply::finished);
        Q_ASSERT(spy.isValid());
        return spy.wait();
    }

private slots:
    void initTestCase()
    {
        QStandardPaths::setTestModeEnabled(true);
62
        QVERIFY(m_server.start());
Volker Krause's avatar
Volker Krause committed
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
    }

    void init()
    {
        RESTClient client;
        client.connectToServer(testServer());
        Product p;
        p.setName(QStringLiteral("org.kde.UserFeedback.UnitTestProduct"));
        auto reply = RESTApi::createProduct(&client, p);
        waitForFinished(reply);
    }

    void cleanup()
    {
        RESTClient client;
        client.connectToServer(testServer());
        Product p;
        p.setName(QStringLiteral("org.kde.UserFeedback.UnitTestProduct"));
        auto reply = RESTApi::deleteProduct(&client, p);
        waitForFinished(reply);
    }

    void testSurveyCRUD()
    {
        RESTClient client;
        client.connectToServer(testServer());
        QVERIFY(client.isConnected());
        Product p;
        p.setName(QStringLiteral("org.kde.UserFeedback.UnitTestProduct"));

        // list surveys
        auto reply = RESTApi::listSurveys(&client, p);
        QVERIFY(waitForFinished(reply));
        QCOMPARE(reply->error(), QNetworkReply::NoError);
97
        QVERIFY(reply->header(QNetworkRequest::ContentTypeHeader).toString().startsWith(QLatin1String("text/json")));
Volker Krause's avatar
Volker Krause committed
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
        auto surveys = Survey::fromJson(reply->readAll());
        QVERIFY(surveys.isEmpty());

        // add new survey
        Survey s;
        s.setName(QStringLiteral("unitTestSurvey"));
        s.setUrl(QUrl(QStringLiteral("http://www.kde.org")));
        s.setActive(false);
        reply = RESTApi::createSurvey(&client, p, s);
        QVERIFY(waitForFinished(reply));
        QCOMPARE(reply->error(), QNetworkReply::NoError);

        // verify the new product is there
        reply = RESTApi::listSurveys(&client, p);
        QVERIFY(waitForFinished(reply));
        QCOMPARE(reply->error(), QNetworkReply::NoError);
        surveys = Survey::fromJson(reply->readAll());
        QCOMPARE(surveys.size(), 1);
        QVERIFY(surveys.at(0) != s); // id is different
        s.setId(surveys.at(0).id());
        QCOMPARE(surveys.at(0), s);

        // update a survey
        s.setActive(true);
        reply = RESTApi::updateSurvey(&client, s);
        QVERIFY(waitForFinished(reply));
        QCOMPARE(reply->error(), QNetworkReply::NoError);

        // verify the update worked
        reply = RESTApi::listSurveys(&client, p);
        QVERIFY(waitForFinished(reply));
        QCOMPARE(reply->error(), QNetworkReply::NoError);
        surveys = Survey::fromJson(reply->readAll());
        QCOMPARE(surveys.size(), 1);
        QCOMPARE(s, surveys.at(0));

        // delete a survey
        reply = RESTApi::deleteSurvey(&client, s);
        QVERIFY(waitForFinished(reply));
        QCOMPARE(reply->error(), QNetworkReply::NoError);

        // verify it's gone
        reply = RESTApi::listSurveys(&client, p);
        QVERIFY(waitForFinished(reply));
        QCOMPARE(reply->error(), QNetworkReply::NoError);
        surveys = Survey::fromJson(reply->readAll());
        QVERIFY(surveys.isEmpty());
    }

    void testInvalidSurveyOperations()
    {
        RESTClient client;
        client.connectToServer(testServer());
        QVERIFY(client.isConnected());

        Product invalidProduct;
        invalidProduct.setName(QStringLiteral("org.kde.UserFeedback.Invalid"));

        // create survey for non-existing product
        Survey s;
        s.setName(QStringLiteral("invalidTestSurvey"));
        s.setUrl(QUrl(QStringLiteral("http://www.kde.org")));
        auto reply = RESTApi::createSurvey(&client, invalidProduct, s);
        QVERIFY(waitForFinished(reply));
        QVERIFY(reply->error() != QNetworkReply::NoError);

        // update a non-existing survey
        s.setId(std::numeric_limits<int>::max());
        reply = RESTApi::updateSurvey(&client, s);
        QVERIFY(waitForFinished(reply));
        qDebug() << reply->readAll();
        QEXPECT_FAIL("", "check not implemented on the server", Continue);
        QVERIFY(reply->error() != QNetworkReply::NoError);

        // delete a non-existing survey
        reply = RESTApi::deleteSurvey(&client, s);
        QVERIFY(waitForFinished(reply));
        QEXPECT_FAIL("", "check not implemented on the server", Continue);
        QVERIFY(reply->error() != QNetworkReply::NoError);
    }
};

QTEST_MAIN(SurveyApiTest)

#include "surveyapitest.moc"