diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-05-22 13:10:39 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-05-22 13:10:39 +0200 |
commit | 6864e4accaafa4fa90332719bff5a85a0e92b242 (patch) | |
tree | 31c0d6df98112674e55ea5ad636c2ad780add49c /examples/imapresource/tests/imapresourcetest.cpp | |
parent | de52c17a7a08e72affc4c182fb1650d18d8b3b2b (diff) | |
download | sink-6864e4accaafa4fa90332719bff5a85a0e92b242.tar.gz sink-6864e4accaafa4fa90332719bff5a85a0e92b242.zip |
ImapResource prototype
Diffstat (limited to 'examples/imapresource/tests/imapresourcetest.cpp')
-rw-r--r-- | examples/imapresource/tests/imapresourcetest.cpp | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/examples/imapresource/tests/imapresourcetest.cpp b/examples/imapresource/tests/imapresourcetest.cpp new file mode 100644 index 0000000..27d7d6d --- /dev/null +++ b/examples/imapresource/tests/imapresourcetest.cpp | |||
@@ -0,0 +1,162 @@ | |||
1 | #include <QtTest> | ||
2 | |||
3 | #include <QString> | ||
4 | #include <KMime/Message> | ||
5 | |||
6 | // #include "imapresource/imapresource.h" | ||
7 | #include "store.h" | ||
8 | #include "resourcecontrol.h" | ||
9 | #include "commands.h" | ||
10 | #include "entitybuffer.h" | ||
11 | #include "resourceconfig.h" | ||
12 | #include "modelresult.h" | ||
13 | #include "pipeline.h" | ||
14 | #include "log.h" | ||
15 | #include "test.h" | ||
16 | #include "../imapresource.h" | ||
17 | #include "../imapserverproxy.h" | ||
18 | |||
19 | #define ASYNCCOMPARE(actual, expected) \ | ||
20 | do {\ | ||
21 | if (!QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__))\ | ||
22 | return KAsync::error<void>(1, "Comparison failed.");\ | ||
23 | } while (0) | ||
24 | |||
25 | #define ASYNCVERIFY(statement) \ | ||
26 | do {\ | ||
27 | if (!QTest::qVerify((statement), #statement, "", __FILE__, __LINE__))\ | ||
28 | return KAsync::error<void>(1, "Verify failed.");\ | ||
29 | } while (0) | ||
30 | |||
31 | #define VERIFYEXEC(statement) \ | ||
32 | do {\ | ||
33 | auto result = statement.exec(); \ | ||
34 | result.waitForFinished(); \ | ||
35 | if (!QTest::qVerify(!result.errorCode(), #statement, "", __FILE__, __LINE__))\ | ||
36 | return;\ | ||
37 | } while (0) | ||
38 | |||
39 | using namespace Sink; | ||
40 | using namespace Sink::ApplicationDomain; | ||
41 | |||
42 | /** | ||
43 | * Test of complete system using the imap resource. | ||
44 | * | ||
45 | * This test requires the imap resource installed. | ||
46 | */ | ||
47 | class ImapResourceTest : public QObject | ||
48 | { | ||
49 | Q_OBJECT | ||
50 | |||
51 | QTemporaryDir tempDir; | ||
52 | QString targetPath; | ||
53 | private slots: | ||
54 | void initTestCase() | ||
55 | { | ||
56 | |||
57 | //FIXME initTest only works for the current process, | ||
58 | //we also have to start resources in test-mode | ||
59 | // Sink::Test::initTest(); | ||
60 | Sink::Log::setDebugOutputLevel(Sink::Log::Trace); | ||
61 | ::ImapResource::removeFromDisk("org.kde.imap.instance1"); | ||
62 | system("resetmailbox.sh"); | ||
63 | // auto resource = ApplicationDomain::ImapResource::create("account1"); | ||
64 | Sink::ApplicationDomain::SinkResource resource; | ||
65 | resource.setProperty("identifier", "org.kde.imap.instance1"); | ||
66 | resource.setProperty("type", "org.kde.imap"); | ||
67 | resource.setProperty("server", "localhost"); | ||
68 | resource.setProperty("port", 993); | ||
69 | Sink::Store::create(resource).exec().waitForFinished(); | ||
70 | } | ||
71 | |||
72 | void cleanup() | ||
73 | { | ||
74 | Sink::ResourceControl::shutdown(QByteArray("org.kde.imap.instance1")).exec().waitForFinished(); | ||
75 | ::ImapResource::removeFromDisk("org.kde.imap.instance1"); | ||
76 | } | ||
77 | |||
78 | void init() | ||
79 | { | ||
80 | qDebug(); | ||
81 | qDebug() << "-----------------------------------------"; | ||
82 | qDebug(); | ||
83 | Sink::ResourceControl::start(QByteArray("org.kde.imap.instance1")).exec().waitForFinished(); | ||
84 | } | ||
85 | |||
86 | void testListFolders() | ||
87 | { | ||
88 | Sink::Query query; | ||
89 | query.resources << "org.kde.imap.instance1"; | ||
90 | query.request<Folder::Name>(); | ||
91 | |||
92 | // Ensure all local data is processed | ||
93 | VERIFYEXEC(Store::synchronize(query)); | ||
94 | ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); | ||
95 | |||
96 | auto job = Store::fetchAll<Folder>(query).then<void, QList<Folder::Ptr>>([](const QList<Folder::Ptr> &folders) { | ||
97 | QCOMPARE(folders.size(), 2); | ||
98 | QStringList names; | ||
99 | for (const auto &folder : folders) { | ||
100 | names << folder->getName(); | ||
101 | } | ||
102 | QVERIFY(names.contains("INBOX")); | ||
103 | QVERIFY(names.contains("INBOX.test")); | ||
104 | }); | ||
105 | VERIFYEXEC(job); | ||
106 | } | ||
107 | |||
108 | void testListMails() | ||
109 | { | ||
110 | Sink::Query query; | ||
111 | query.resources << "org.kde.imap.instance1"; | ||
112 | query.request<Mail::Subject>().request<Mail::MimeMessage>(); | ||
113 | |||
114 | // Ensure all local data is processed | ||
115 | VERIFYEXEC(Store::synchronize(query)); | ||
116 | ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); | ||
117 | |||
118 | auto job = Store::fetchAll<Mail>(query).then<void, QList<Mail::Ptr>>([](const QList<Mail::Ptr> &mails) { | ||
119 | QCOMPARE(mails.size(), 1); | ||
120 | QVERIFY(mails.first()->getSubject().startsWith(QString("[Nepomuk] Jenkins build is still unstable"))); | ||
121 | const auto data = mails.first()->getMimeMessage(); | ||
122 | QVERIFY(!data.isEmpty()); | ||
123 | |||
124 | KMime::Message m; | ||
125 | m.setContent(data); | ||
126 | m.parse(); | ||
127 | QCOMPARE(mails.first()->getSubject(), m.subject(true)->asUnicodeString()); | ||
128 | }); | ||
129 | VERIFYEXEC(job); | ||
130 | } | ||
131 | |||
132 | void testFetchNewMessages() | ||
133 | { | ||
134 | Sink::Query query; | ||
135 | query.resources << "org.kde.imap.instance1"; | ||
136 | query.request<Mail::Subject>().request<Mail::MimeMessage>(); | ||
137 | |||
138 | // Ensure all local data is processed | ||
139 | VERIFYEXEC(Store::synchronize(query)); | ||
140 | ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); | ||
141 | |||
142 | ImapServerProxy imap("localhost", 993); | ||
143 | imap.login("doe", "doe").exec().waitForFinished(); | ||
144 | |||
145 | auto msg = KMime::Message::Ptr::create(); | ||
146 | msg->subject(true)->fromUnicodeString("Foobar", "utf8"); | ||
147 | msg->assemble(); | ||
148 | |||
149 | VERIFYEXEC(imap.append("INBOX.test", msg->encodedContent(true))); | ||
150 | |||
151 | Store::synchronize(query).exec().waitForFinished(); | ||
152 | ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); | ||
153 | |||
154 | auto job = Store::fetchAll<Mail>(query).then<void, QList<Mail::Ptr>>([](const QList<Mail::Ptr> &mails) { | ||
155 | QCOMPARE(mails.size(), 2); | ||
156 | }); | ||
157 | VERIFYEXEC(job); | ||
158 | } | ||
159 | }; | ||
160 | |||
161 | QTEST_MAIN(ImapResourceTest) | ||
162 | #include "imapresourcetest.moc" | ||