summaryrefslogtreecommitdiffstats
path: root/examples/imapresource/tests/imapresourcetest.cpp
blob: 8ce32c290d2b2240ad09c4931ffd1d291813b575 (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
#include <QtTest>

#include <QString>
#include <KMime/Message>

// #include "imapresource/imapresource.h"
#include "store.h"
#include "resourcecontrol.h"
#include "commands.h"
#include "entitybuffer.h"
#include "resourceconfig.h"
#include "modelresult.h"
#include "pipeline.h"
#include "log.h"
#include "test.h"
#include "../imapresource.h"
#include "../imapserverproxy.h"

#define ASYNCCOMPARE(actual, expected) \
do {\
    if (!QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__))\
        return KAsync::error<void>(1, "Comparison failed.");\
} while (0)

#define ASYNCVERIFY(statement) \
do {\
    if (!QTest::qVerify((statement), #statement, "", __FILE__, __LINE__))\
        return KAsync::error<void>(1, "Verify failed.");\
} while (0)

#define VERIFYEXEC(statement) \
do {\
    auto result = statement.exec(); \
    result.waitForFinished(); \
    if (!QTest::qVerify(!result.errorCode(), #statement, "", __FILE__, __LINE__))\
        return;\
} while (0)

using namespace Sink;
using namespace Sink::ApplicationDomain;

/**
 * Test of complete system using the imap resource.
 *
 * This test requires the imap resource installed.
 */
class ImapResourceTest : public QObject
{
    Q_OBJECT

    QTemporaryDir tempDir;
    QString targetPath;
private slots:
    void initTestCase()
    {

        //FIXME initTest only works for the current process,
        //we also have to start resources in test-mode
        // Sink::Test::initTest();
        Sink::Log::setDebugOutputLevel(Sink::Log::Trace);
        ::ImapResource::removeFromDisk("org.kde.imap.instance1");
        system("resetmailbox.sh");
        // auto resource = ApplicationDomain::ImapResource::create("account1");
        Sink::ApplicationDomain::SinkResource resource;
        resource.setProperty("identifier", "org.kde.imap.instance1");
        resource.setProperty("type", "org.kde.imap");
        resource.setProperty("server", "localhost");
        resource.setProperty("port", 993);
        Sink::Store::create(resource).exec().waitForFinished();
    }

    void cleanup()
    {
        Sink::ResourceControl::shutdown(QByteArray("org.kde.imap.instance1")).exec().waitForFinished();
        ::ImapResource::removeFromDisk("org.kde.imap.instance1");
    }

    void init()
    {
        qDebug();
        qDebug() << "-----------------------------------------";
        qDebug();
        Sink::ResourceControl::start(QByteArray("org.kde.imap.instance1")).exec().waitForFinished();
    }

    void testListFolders()
    {
        Sink::Query query;
        query.resources << "org.kde.imap.instance1";
        query.request<Folder::Name>();

        // Ensure all local data is processed
        VERIFYEXEC(Store::synchronize(query));
        ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished();

        auto job = Store::fetchAll<Folder>(query).then<void, QList<Folder::Ptr>>([](const QList<Folder::Ptr> &folders) {
            QCOMPARE(folders.size(), 2);
            QStringList names;
            for (const auto &folder : folders) {
                names << folder->getName();
            }
            QVERIFY(names.contains("INBOX"));
            QVERIFY(names.contains("test"));
        });
        VERIFYEXEC(job);
    }

    void testListFolderHierarchy()
    {
        Sink::Query query;
        query.resources << "org.kde.imap.instance1";
        query.request<Folder::Name>().request<Folder::Parent>();

        Imap::ImapServerProxy imap("localhost", 993);
        VERIFYEXEC(imap.login("doe", "doe"));
        VERIFYEXEC(imap.create("INBOX.test.sub"));

        // Ensure all local data is processed
        VERIFYEXEC(Store::synchronize(query));
        ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished();

        auto job = Store::fetchAll<Folder>(query).then<void, QList<Folder::Ptr>>([](const QList<Folder::Ptr> &folders) {
            QCOMPARE(folders.size(), 3);
            QHash<QString, Folder::Ptr> map;
            for (const auto &folder : folders) {
                map.insert(folder->getName(), folder);
            }
            QCOMPARE(map.value("sub")->getParent(), map.value("test")->identifier());
        });
        VERIFYEXEC(job);
    }

    void testListMails()
    {
        Sink::Query query;
        query.resources << "org.kde.imap.instance1";
        query.request<Mail::Subject>().request<Mail::MimeMessage>();

        // Ensure all local data is processed
        VERIFYEXEC(Store::synchronize(query));
        ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished();

        auto job = Store::fetchAll<Mail>(query).then<void, QList<Mail::Ptr>>([](const QList<Mail::Ptr> &mails) {
            QCOMPARE(mails.size(), 1);
            QVERIFY(mails.first()->getSubject().startsWith(QString("[Nepomuk] Jenkins build is still unstable")));
            const auto data = mails.first()->getMimeMessage();
            QVERIFY(!data.isEmpty());

            KMime::Message m;
            m.setContent(data);
            m.parse();
            QCOMPARE(mails.first()->getSubject(), m.subject(true)->asUnicodeString());
        });
        VERIFYEXEC(job);
    }

    void testFetchNewMessages()
    {
        Sink::Query query;
        query.resources << "org.kde.imap.instance1";
        query.request<Mail::Subject>().request<Mail::MimeMessage>();

        // Ensure all local data is processed
        VERIFYEXEC(Store::synchronize(query));
        ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished();

        ImapServerProxy imap("localhost", 993);
        imap.login("doe", "doe").exec().waitForFinished();

        auto msg = KMime::Message::Ptr::create();
        msg->subject(true)->fromUnicodeString("Foobar", "utf8");
        msg->assemble();

        VERIFYEXEC(imap.append("INBOX.test", msg->encodedContent(true)));

        Store::synchronize(query).exec().waitForFinished();
        ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished();

        auto job = Store::fetchAll<Mail>(query).then<void, QList<Mail::Ptr>>([](const QList<Mail::Ptr> &mails) {
            QCOMPARE(mails.size(), 2);
        });
        VERIFYEXEC(job);
    }

    void testFailingSync()
    {
        auto resource = ApplicationDomain::ImapResource::create("account1");
        resource.setProperty("server", "foobar");
        resource.setProperty("port", 993);
        Sink::Store::create(resource).exec().waitForFinished();
        Sink::Query query;
        query.resources << resource.identifier();

        // Ensure sync fails if resource is misconfigured
        auto future = Store::synchronize(query).exec();
        future.waitForFinished();
        QVERIFY(future.errorCode());
    }
};

QTEST_MAIN(ImapResourceTest)
#include "imapresourcetest.moc"