File: testmovabletype.cpp

package info (click to toggle)
kblog 4%3A18.08.3-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 952 kB
  • sloc: cpp: 6,537; makefile: 5; sh: 1
file content (443 lines) | stat: -rw-r--r-- 15,100 bytes parent folder | download
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
  This file is part of the kblog library.

  Copyright (c) 2006-2007 Christian Weilbach <christian_weilbach@web.de>

  This library 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 library 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 Library General Public License
  along with this library; see the file COPYING.LIB.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA 02110-1301, USA.
*/

#include "data.h"

#include "kblog/movabletype.h"
#include "kblog/blogpost.h"
#include "kblog/blogmedia.h"

#include <QTest>
#include <QDateTime>
#include <QTimeZone>
#include <QTimer>
#include <QDebug>

#include <unistd.h>

#define TIMEOUT 10000
#define GLOBALTIMEOUT 70000
#define DOWNLOADCOUNT 5

using namespace KBlog;

class TestMovableType : public QObject
{
    Q_OBJECT
public Q_SLOTS:
    // use this functions as a chain to go through network traffic.
    void fetchUserInfo(const QMap<QString, QString> &);
    void listBlogs(const QList<QMap<QString, QString> > &);
    void listCategories(const QList<QMap<QString, QString> > &categories);
    void listRecentPosts(const QList<KBlog::BlogPost> &posts);
    void createPost(KBlog::BlogPost *post);
    void modifyPost(KBlog::BlogPost *post);
    void fetchPost(KBlog::BlogPost *post);
    void removePost(KBlog::BlogPost *post);
    // end chain
    void errorPost(KBlog::Blog::ErrorType type, const QString &errStr, KBlog::BlogPost *);

private Q_SLOTS:
    void testValidity();
    void testNetwork();

private:
    void dumpPost(const KBlog::BlogPost *);
    KBlog::MovableType *b;
    KBlog::BlogPost *p;
    QEventLoop *eventLoop;
    QTimer *fetchUserInfoTimer;
    QTimer *listBlogsTimer;
    QTimer *listRecentPostsTimer;
    QTimer *listCategoriesTimer;
    QTimer *fetchPostTimer;
    QTimer *modifyPostTimer;
    QTimer *createPostTimer;
    QTimer *removePostTimer;
};

class TestMovableTypeWarnings : public QObject
{
    Q_OBJECT
private Q_SLOTS:
    void fetchUserInfoTimeoutWarning();
    void listBlogsTimeoutWarning();
    void listRecentPostsTimeoutWarning();
    void listCategoriesTimeoutWarning();
    void fetchPostTimeoutWarning();
    void modifyPostTimeoutWarning();
    void createPostTimeoutWarning();
    void removePostTimeoutWarning();

};

#include "testmovabletype.moc"

void TestMovableType::dumpPost(const BlogPost *post)
{
    qDebug() << "########### post ############";
    qDebug() << "# postId: " << post->postId();
    qDebug() << "# title: " << post->title();
    qDebug() << "# content: " << post->content();
    qDebug() << "# private: " << post->isPrivate();
    qDebug() << "# categories: " << post->categories().join(QLatin1String(" "));
    qDebug() << "# error: " << post->error();
    qDebug() << "# journalId: " << post->journalId();
    qDebug() << "# allowTrackBack: " << post->isTrackBackAllowed();
    qDebug() << "# allowComment: " << post->isCommentAllowed();
    qDebug() << "# summary: " << post->summary();
    qDebug() << "# tags: " << post->tags();
    qDebug() << "# link: " << post->link().url();
    qDebug() << "# permalink: " << post->permaLink().url();
    switch (post->status()) {
    case BlogPost::New:
        qDebug() << "# status: New";
        break;
    case BlogPost::Fetched:
        qDebug() << "# status: Fetched";
        break;
    case BlogPost::Created:
        qDebug() << "# status: Created";
        break;
    case BlogPost::Modified:
        qDebug() << "# status: Modified";
        break;
    case BlogPost::Removed:
        qDebug() << "# status: Removed";
        break;
    case BlogPost::Error:
        qDebug() << "# status: Error";
        break;
    };
    qDebug() << "# creationDateTime(UTC): "
             << post->creationDateTime().toUTC().toString();
    qDebug() << "# modificationDateTime(UTC): "
             << post->modificationDateTime().toUTC().toString();
    qDebug() << "###########################";
}

// the chain starts here

void TestMovableType::fetchUserInfo(const QMap<QString, QString> &userInfo)
{
    fetchUserInfoTimer->stop();
    qDebug() << "########### fetchUserInfo ###########";
    qDebug() << "# nickname: " << userInfo[QLatin1String("nickname")];
    qDebug() << "# userid: "  << userInfo[QLatin1String("userid")];
    qDebug() << "# url: " <<  userInfo[QLatin1String("url")];
    qDebug() << "# email: " <<  userInfo[QLatin1String("email")];
    qDebug() << "# lastname: " << userInfo[QLatin1String("lastname")];
    qDebug() << "# firstname: " <<  userInfo[QLatin1String("firstname")];
    qDebug() << "##############################\n";

    connect(b, SIGNAL(listedBlogs(QList<QMap<QString,QString> >)),
            this, SLOT(listBlogs(QList<QMap<QString,QString> >)));
    b->listBlogs();
    listBlogsTimer->start(TIMEOUT);
}

void TestMovableType::listBlogs(const QList<QMap<QString, QString> > &listedBlogs)
{
    listBlogsTimer->stop();
    qDebug() << "########### listBlogs ###########";
    QList<QMap<QString, QString> >::ConstIterator it = listedBlogs.begin();
    QList<QMap<QString, QString> >::ConstIterator end = listedBlogs.end();
    for (; it != end; ++it) {
        qDebug() << "# " << (*it).cbegin().key() << ": " << (*it).cbegin().value();
    }
    qDebug() << "###########################\n";

    connect(b, SIGNAL(listedRecentPosts(QList<KBlog::BlogPost>)),
            this, SLOT(listRecentPosts(QList<KBlog::BlogPost>)));
    b->listRecentPosts(DOWNLOADCOUNT);
    listRecentPostsTimer->start(TIMEOUT);
}

void TestMovableType::listRecentPosts(
    const QList<KBlog::BlogPost> &posts)
{
    listRecentPostsTimer->stop();
    qDebug() << "########### listRecentPosts ###########";
    QList<KBlog::BlogPost>::ConstIterator it = posts.begin();
    QList<KBlog::BlogPost>::ConstIterator end = posts.end();
    for (; it != end; ++it) {
        dumpPost(&(*it));
    }
    qDebug() << "#################################\n";

    connect(b, SIGNAL(listedCategories(QList<QMap<QString,QString> >)),
            this, SLOT(listCategories(QList<QMap<QString,QString> >)));
    b->listCategories(); // start chain
    listCategoriesTimer->start(TIMEOUT);
}

void TestMovableType::listCategories(
    const QList<QMap<QString, QString> > &categories)
{
    listCategoriesTimer->stop();
    qDebug() << "########### listCategories ###########";
    QList<QMap<QString, QString> >::ConstIterator it = categories.begin();
    QList<QMap<QString, QString> >::ConstIterator end = categories.end();
    for (; it != end; ++it) {
        qDebug() << "# category name: " << (*it)[QLatin1String("name")];
    }
    qDebug() << "###############################\n";

    connect(b, SIGNAL(createdPost(KBlog::BlogPost*)),
            this, SLOT(createPost(KBlog::BlogPost*)));
    b->createPost(p);   // start chain
    createPostTimer->start(TIMEOUT);
}

void TestMovableType::createPost(KBlog::BlogPost *post)
{
    createPostTimer->stop();
    qDebug() << "########### createPost ############";
    dumpPost(post);
    qDebug() << "################################\n";
    QVERIFY(post->status() == BlogPost::Created);

    connect(b, SIGNAL(modifiedPost(KBlog::BlogPost*)),
            this, SLOT(modifyPost(KBlog::BlogPost*)));
    p->setContent(mModifiedContent);
    b->modifyPost(p);
    modifyPostTimer->start(TIMEOUT);
}

void TestMovableType::modifyPost(KBlog::BlogPost *post)
{
    modifyPostTimer->stop();
    qDebug() << "########### modifyPost ############";
    dumpPost(post);
    qDebug() << "################################\n";
    QVERIFY(post->status() == BlogPost::Modified);

    connect(b, SIGNAL(fetchedPost(KBlog::BlogPost*)),
            this, SLOT(fetchPost(KBlog::BlogPost*)));
    p->setContent(QLatin1String("TestMovableType: created content."));
    b->fetchPost(p);
    fetchPostTimer->start(TIMEOUT);
}

void TestMovableType::fetchPost(KBlog::BlogPost *post)
{
    fetchPostTimer->stop();
    qDebug() << "########### fetchPost ############";
    dumpPost(post);
    qDebug() << "###############################\n";
    QVERIFY(post->status() == BlogPost::Fetched);
//   QVERIFY( post->content() == mModifiedContent );

    connect(b, SIGNAL(removedPost(KBlog::BlogPost*)),
            this, SLOT(removePost(KBlog::BlogPost*)));
    b->removePost(p);
    removePostTimer->start(TIMEOUT);
}

void TestMovableType::removePost(KBlog::BlogPost *post)
{
    removePostTimer->stop();
    qDebug() << "########### removePost ###########";
    dumpPost(post);
    qDebug() << "################################\n";
    QVERIFY(post->status() == BlogPost::Removed);
    eventLoop->quit();
}

void TestMovableType::errorPost(KBlog::Blog::ErrorType type, const QString &errStr,
                                KBlog::BlogPost *post)
{
    qDebug() << "############ error #############";
    switch (type) {
    case Blog::Atom:
        qDebug() << "type: Atom";
        break;
    case Blog::XmlRpc:
        qDebug() << "type: xmlRpc";
        break;
    case Blog::ParsingError:
        qDebug() << "type: ParsingError";
        break;
    case Blog::AuthenticationError:
        qDebug() << "type: AuthenticationError";
        break;
    case Blog::NotSupported:
        qDebug() << "type: NotSupported";
        break;
    case Blog::Other:
        qDebug() << "type: Other";
        break;
    };
    qDebug() << "error: " << errStr;
    if (post != nullptr) {
        dumpPost(post);
    }
    qDebug() << "#############################\n";
}

// Warnings for Timouts:

void TestMovableTypeWarnings::fetchUserInfoTimeoutWarning()
{
    QWARN("fetchUserInfo() timeout. This can be caused by an error, too. Any following calls will fail.");
}

void TestMovableTypeWarnings::listBlogsTimeoutWarning()
{
    QWARN("listBlogs()  timeout. This can be caused by an error, too. Any following calls will fail.");
}

void TestMovableTypeWarnings::listRecentPostsTimeoutWarning()
{
    QWARN("listRecentPosts() timeout. This can be caused by an error, too. Any following calls will fail.");
}

void TestMovableTypeWarnings::listCategoriesTimeoutWarning()
{
    QWARN("listCategories() timeout. This can be caused by an error, too. Any following calls will fail.");
}

void TestMovableTypeWarnings::fetchPostTimeoutWarning()
{
    QWARN("fetchPost() timeout. This can be caused by an error, too. Any following calls will fail.");
}

void TestMovableTypeWarnings::modifyPostTimeoutWarning()
{
    QWARN("modifyPost() timeout. This can be caused by an error, too. Any following calls will fail.");
}

void TestMovableTypeWarnings::createPostTimeoutWarning()
{
    QWARN("createPost() timeout. This can be caused by an error, too. Any following calls will fail.");
}

void TestMovableTypeWarnings::removePostTimeoutWarning()
{
    QWARN("removePost() timeout. This can be caused by an error, too. Any following calls will fail.");
}

void TestMovableType::testValidity()
{
    eventLoop = new QEventLoop(this);

    // no need to delete later ;-):
    b = new MovableType(QUrl(QLatin1String("http://wrong.url.org/somegateway")));
    QVERIFY(b->url() == QUrl(QLatin1String("http://wrong.url.org/somegateway")));
    QTimeZone mTimeZone("UTC");
    b->setUrl(mUrl);
    b->setUsername(mUsername);
    b->setPassword(mPassword);
    b->setBlogId(mBlogId);
    b->setTimeZone(mTimeZone);
    QVERIFY(b->url() == mUrl);
    QVERIFY(b->blogId() == mBlogId);
    QVERIFY(b->username() == mUsername);
    QVERIFY(b->password() == mPassword);
    QVERIFY(b->interfaceName() == QLatin1String("Movable Type"));
    QVERIFY(b->timeZone().id() == mTimeZone.id());
}

void TestMovableType::testNetwork()
{
    QDateTime mCDateTime(mCreationDateTime);
    QDateTime mMDateTime(mModificationDateTime);
    p = new BlogPost(); // no need to delete later ;-)
    p->setTitle(mTitle);
    p->setContent(mContent);
    p->setPrivate(mPrivate);
    p->setPostId(mPostId);
    p->setCreationDateTime(mCDateTime);
    p->setModificationDateTime(mMDateTime);
    p->setCommentAllowed(mCommentAllowed);
    p->setTrackBackAllowed(mTrackBackAllowed);
    p->setSummary(mSummary);
    p->setTags(mTags);
    p->setCategories(mCategories << QLatin1String("Blogroll"));

    BlogMedia *m = new BlogMedia();
    m->setName(QLatin1String("testMovableType.txt"));
    m->setMimetype(QLatin1String("text/plain"));
    m->setData("YTM0NZomIzI2OTsmIzM0NTueYQ==");
    QVERIFY(m->mimetype() == QLatin1String("text/plain"));
    QVERIFY(m->data() == "YTM0NZomIzI2OTsmIzM0NTueYQ==");
    QVERIFY(m->name() == QLatin1String("testMovableType.txt"));

    connect(b, SIGNAL(errorPost(KBlog::Blog::ErrorType,QString,KBlog::BlogPost*)),
            this, SLOT(errorPost(KBlog::Blog::ErrorType,QString,KBlog::BlogPost*)));

    TestMovableTypeWarnings *warnings = new TestMovableTypeWarnings();

    fetchUserInfoTimer = new QTimer(this);
    fetchUserInfoTimer->setSingleShot(true);
    connect(fetchUserInfoTimer, SIGNAL(timeout()),
            warnings, SLOT(fetchUserInfoTimeoutWarning()));

    listBlogsTimer = new QTimer(this);
    listBlogsTimer->setSingleShot(true);
    connect(listBlogsTimer, SIGNAL(timeout()),
            warnings, SLOT(listBlogsTimeoutWarning()));

    listRecentPostsTimer = new QTimer(this);
    listRecentPostsTimer->setSingleShot(true);
    connect(listRecentPostsTimer, SIGNAL(timeout()),
            warnings, SLOT(listRecentPostsTimeoutWarning()));

    listCategoriesTimer = new QTimer(this);
    listCategoriesTimer->setSingleShot(true);
    connect(listCategoriesTimer, SIGNAL(timeout()),
            warnings, SLOT(listCategoriesTimeoutWarning()));

    fetchPostTimer = new QTimer(this);
    fetchPostTimer->setSingleShot(true);
    connect(fetchPostTimer, SIGNAL(timeout()),
            warnings, SLOT(fetchPostTimeoutWarning()));

    modifyPostTimer = new QTimer(this);
    modifyPostTimer->setSingleShot(true);
    connect(modifyPostTimer, SIGNAL(timeout()),
            warnings, SLOT(modifyPostTimeoutWarning()));

    createPostTimer = new QTimer(this);
    createPostTimer->setSingleShot(true);
    connect(createPostTimer, SIGNAL(timeout()),
            warnings, SLOT(createPostTimeoutWarning()));

    removePostTimer = new QTimer(this);
    removePostTimer->setSingleShot(true);
    connect(removePostTimer, SIGNAL(timeout()),
            warnings, SLOT(removePostTimeoutWarning()));

    // start the chain
    connect(b, SIGNAL(fetchedUserInfo(QMap<QString,QString>)),
            this, SLOT(fetchUserInfo(QMap<QString,QString>)));
    b->fetchUserInfo();
    fetchUserInfoTimer->start(TIMEOUT);

// wait for all jobs to finish

    QTimer::singleShot(GLOBALTIMEOUT, eventLoop, SLOT(quit()));
    eventLoop->exec();
    delete b;
    delete p;
}

QTEST_GUILESS_MAIN(TestMovableType)