diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-05-31 11:24:59 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-05-31 11:24:59 +0200 |
commit | f90ca753a4b723dbf72996bbd1261dc786c7cce6 (patch) | |
tree | 13916b2f69e728af3883e63e23f71a464ef3573b | |
parent | 08c368700c57fc1214aab34a24c921756b3200f0 (diff) | |
download | sink-f90ca753a4b723dbf72996bbd1261dc786c7cce6.tar.gz sink-f90ca753a4b723dbf72996bbd1261dc786c7cce6.zip |
Replace the imapresourcetest with the generic mailsynctest
-rw-r--r-- | examples/imapresource/tests/CMakeLists.txt | 4 | ||||
-rw-r--r-- | examples/imapresource/tests/imapmailsynctest.cpp | 84 | ||||
-rw-r--r-- | examples/imapresource/tests/imapresourcetest.cpp | 276 | ||||
-rw-r--r-- | tests/CMakeLists.txt | 2 | ||||
-rw-r--r-- | tests/mailsynctest.cpp | 241 | ||||
-rw-r--r-- | tests/mailsynctest.h | 83 |
6 files changed, 411 insertions, 279 deletions
diff --git a/examples/imapresource/tests/CMakeLists.txt b/examples/imapresource/tests/CMakeLists.txt index 0604b91..29ae918 100644 --- a/examples/imapresource/tests/CMakeLists.txt +++ b/examples/imapresource/tests/CMakeLists.txt | |||
@@ -6,12 +6,12 @@ include_directories( | |||
6 | include(SinkTest) | 6 | include(SinkTest) |
7 | 7 | ||
8 | auto_tests ( | 8 | auto_tests ( |
9 | imapresourcetest | ||
10 | imapserverproxytest | 9 | imapserverproxytest |
11 | imapmailtest | 10 | imapmailtest |
11 | imapmailsynctest | ||
12 | ) | 12 | ) |
13 | target_link_libraries(imapresourcetest sink_resource_imap) | ||
14 | target_link_libraries(imapserverproxytest sink_resource_imap) | 13 | target_link_libraries(imapserverproxytest sink_resource_imap) |
15 | target_link_libraries(imapmailtest sink_resource_imap) | 14 | target_link_libraries(imapmailtest sink_resource_imap) |
15 | target_link_libraries(imapmailsynctest sink_resource_imap) | ||
16 | 16 | ||
17 | install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/resetmailbox.sh DESTINATION bin PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_EXECUTE GROUP_READ) | 17 | install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/resetmailbox.sh DESTINATION bin PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_EXECUTE GROUP_READ) |
diff --git a/examples/imapresource/tests/imapmailsynctest.cpp b/examples/imapresource/tests/imapmailsynctest.cpp new file mode 100644 index 0000000..c73c840 --- /dev/null +++ b/examples/imapresource/tests/imapmailsynctest.cpp | |||
@@ -0,0 +1,84 @@ | |||
1 | #include <QtTest> | ||
2 | |||
3 | #include <tests/mailsynctest.h> | ||
4 | #include "../imapresource.h" | ||
5 | #include "../imapserverproxy.h" | ||
6 | |||
7 | #include "common/test.h" | ||
8 | #include "common/domain/applicationdomaintype.h" | ||
9 | |||
10 | using namespace Sink; | ||
11 | using namespace Sink::ApplicationDomain; | ||
12 | |||
13 | /** | ||
14 | * Test of complete system using the imap resource. | ||
15 | * | ||
16 | * This test requires the imap resource installed. | ||
17 | */ | ||
18 | class ImapMailSyncTest : public Sink::MailSyncTest | ||
19 | { | ||
20 | Q_OBJECT | ||
21 | |||
22 | protected: | ||
23 | void resetTestEnvironment() Q_DECL_OVERRIDE | ||
24 | { | ||
25 | system("resetmailbox.sh"); | ||
26 | } | ||
27 | |||
28 | Sink::ApplicationDomain::SinkResource createResource() Q_DECL_OVERRIDE | ||
29 | { | ||
30 | auto resource = ApplicationDomain::ImapResource::create("account1"); | ||
31 | resource.setProperty("server", "localhost"); | ||
32 | resource.setProperty("port", 993); | ||
33 | resource.setProperty("user", "doe"); | ||
34 | resource.setProperty("password", "doe"); | ||
35 | return resource; | ||
36 | } | ||
37 | |||
38 | Sink::ApplicationDomain::SinkResource createFaultyResource() Q_DECL_OVERRIDE | ||
39 | { | ||
40 | auto resource = ApplicationDomain::ImapResource::create("account1"); | ||
41 | resource.setProperty("server", "foobar"); | ||
42 | resource.setProperty("port", 993); | ||
43 | resource.setProperty("user", "doe"); | ||
44 | resource.setProperty("password", "doe"); | ||
45 | return resource; | ||
46 | } | ||
47 | |||
48 | void removeResourceFromDisk(const QByteArray &identifier) Q_DECL_OVERRIDE | ||
49 | { | ||
50 | ::ImapResource::removeFromDisk(identifier); | ||
51 | } | ||
52 | |||
53 | void createFolder(const QStringList &folderPath) Q_DECL_OVERRIDE | ||
54 | { | ||
55 | Imap::ImapServerProxy imap("localhost", 993); | ||
56 | VERIFYEXEC(imap.login("doe", "doe")); | ||
57 | VERIFYEXEC(imap.create("INBOX." + folderPath.join('.'))); | ||
58 | } | ||
59 | |||
60 | void removeFolder(const QStringList &folderPath) Q_DECL_OVERRIDE | ||
61 | { | ||
62 | Imap::ImapServerProxy imap("localhost", 993); | ||
63 | VERIFYEXEC(imap.login("doe", "doe")); | ||
64 | VERIFYEXEC(imap.remove("INBOX." + folderPath.join('.'))); | ||
65 | } | ||
66 | |||
67 | void createMessage(const QStringList &folderPath, const QByteArray &message) Q_DECL_OVERRIDE | ||
68 | { | ||
69 | Imap::ImapServerProxy imap("localhost", 993); | ||
70 | VERIFYEXEC(imap.login("doe", "doe")); | ||
71 | VERIFYEXEC(imap.append("INBOX." + folderPath.join('.'), message)); | ||
72 | } | ||
73 | |||
74 | void removeMessage(const QStringList &folderPath, const QByteArray &messages) Q_DECL_OVERRIDE | ||
75 | { | ||
76 | Imap::ImapServerProxy imap("localhost", 993); | ||
77 | VERIFYEXEC(imap.login("doe", "doe")); | ||
78 | VERIFYEXEC(imap.remove("INBOX." + folderPath.join('.'), "2:*")); | ||
79 | } | ||
80 | }; | ||
81 | |||
82 | QTEST_MAIN(ImapMailSyncTest) | ||
83 | |||
84 | #include "imapmailsynctest.moc" | ||
diff --git a/examples/imapresource/tests/imapresourcetest.cpp b/examples/imapresource/tests/imapresourcetest.cpp deleted file mode 100644 index fa3caa7..0000000 --- a/examples/imapresource/tests/imapresourcetest.cpp +++ /dev/null | |||
@@ -1,276 +0,0 @@ | |||
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 | Sink::Test::initTest(); | ||
57 | Sink::Log::setDebugOutputLevel(Sink::Log::Trace); | ||
58 | ::ImapResource::removeFromDisk("org.kde.imap.instance1"); | ||
59 | system("resetmailbox.sh"); | ||
60 | // auto resource = ApplicationDomain::ImapResource::create("account1"); | ||
61 | Sink::ApplicationDomain::SinkResource resource; | ||
62 | resource.setProperty("identifier", "org.kde.imap.instance1"); | ||
63 | resource.setProperty("type", "org.kde.imap"); | ||
64 | resource.setProperty("server", "localhost"); | ||
65 | resource.setProperty("user", "doe"); | ||
66 | resource.setProperty("password", "doe"); | ||
67 | resource.setProperty("port", 993); | ||
68 | Sink::Store::create(resource).exec().waitForFinished(); | ||
69 | } | ||
70 | |||
71 | void cleanup() | ||
72 | { | ||
73 | Sink::ResourceControl::shutdown(QByteArray("org.kde.imap.instance1")).exec().waitForFinished(); | ||
74 | ::ImapResource::removeFromDisk("org.kde.imap.instance1"); | ||
75 | } | ||
76 | |||
77 | void init() | ||
78 | { | ||
79 | qDebug(); | ||
80 | qDebug() << "-----------------------------------------"; | ||
81 | qDebug(); | ||
82 | Sink::ResourceControl::start(QByteArray("org.kde.imap.instance1")).exec().waitForFinished(); | ||
83 | } | ||
84 | |||
85 | void testListFolders() | ||
86 | { | ||
87 | Sink::Query query; | ||
88 | query.resources << "org.kde.imap.instance1"; | ||
89 | query.request<Folder::Name>(); | ||
90 | |||
91 | // Ensure all local data is processed | ||
92 | VERIFYEXEC(Store::synchronize(query)); | ||
93 | ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); | ||
94 | |||
95 | auto job = Store::fetchAll<Folder>(query).then<void, QList<Folder::Ptr>>([](const QList<Folder::Ptr> &folders) { | ||
96 | QCOMPARE(folders.size(), 2); | ||
97 | QStringList names; | ||
98 | for (const auto &folder : folders) { | ||
99 | names << folder->getName(); | ||
100 | } | ||
101 | QVERIFY(names.contains("INBOX")); | ||
102 | QVERIFY(names.contains("test")); | ||
103 | }); | ||
104 | VERIFYEXEC(job); | ||
105 | } | ||
106 | |||
107 | void testListFolderHierarchy() | ||
108 | { | ||
109 | Sink::Query query; | ||
110 | query.resources << "org.kde.imap.instance1"; | ||
111 | query.request<Folder::Name>().request<Folder::Parent>(); | ||
112 | |||
113 | Imap::ImapServerProxy imap("localhost", 993); | ||
114 | VERIFYEXEC(imap.login("doe", "doe")); | ||
115 | VERIFYEXEC(imap.create("INBOX.test.sub")); | ||
116 | |||
117 | // Ensure all local data is processed | ||
118 | VERIFYEXEC(Store::synchronize(query)); | ||
119 | ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); | ||
120 | |||
121 | auto job = Store::fetchAll<Folder>(query).then<void, QList<Folder::Ptr>>([](const QList<Folder::Ptr> &folders) { | ||
122 | QCOMPARE(folders.size(), 3); | ||
123 | QHash<QString, Folder::Ptr> map; | ||
124 | for (const auto &folder : folders) { | ||
125 | map.insert(folder->getName(), folder); | ||
126 | } | ||
127 | QCOMPARE(map.value("sub")->getParent(), map.value("test")->identifier()); | ||
128 | }); | ||
129 | VERIFYEXEC(job); | ||
130 | } | ||
131 | |||
132 | void testListNewFolders() | ||
133 | { | ||
134 | Sink::Query query; | ||
135 | query.resources << "org.kde.imap.instance1"; | ||
136 | query.request<Folder::Name>(); | ||
137 | |||
138 | Imap::ImapServerProxy imap("localhost", 993); | ||
139 | VERIFYEXEC(imap.login("doe", "doe")); | ||
140 | VERIFYEXEC(imap.create("INBOX.test.sub1")); | ||
141 | |||
142 | // Ensure all local data is processed | ||
143 | VERIFYEXEC(Store::synchronize(query)); | ||
144 | ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); | ||
145 | |||
146 | auto job = Store::fetchAll<Folder>(query).then<void, QList<Folder::Ptr>>([](const QList<Folder::Ptr> &folders) { | ||
147 | QStringList names; | ||
148 | for (const auto &folder : folders) { | ||
149 | names << folder->getName(); | ||
150 | } | ||
151 | QVERIFY(names.contains("sub1")); | ||
152 | }); | ||
153 | VERIFYEXEC(job); | ||
154 | } | ||
155 | |||
156 | void testListRemovedFolders() | ||
157 | { | ||
158 | Sink::Query query; | ||
159 | query.resources << "org.kde.imap.instance1"; | ||
160 | query.request<Folder::Name>(); | ||
161 | |||
162 | VERIFYEXEC(Store::synchronize(query)); | ||
163 | ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); | ||
164 | |||
165 | Imap::ImapServerProxy imap("localhost", 993); | ||
166 | VERIFYEXEC(imap.login("doe", "doe")); | ||
167 | VERIFYEXEC(imap.remove("INBOX.test.sub1")); | ||
168 | |||
169 | // Ensure all local data is processed | ||
170 | VERIFYEXEC(Store::synchronize(query)); | ||
171 | ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); | ||
172 | |||
173 | auto job = Store::fetchAll<Folder>(query).then<void, QList<Folder::Ptr>>([](const QList<Folder::Ptr> &folders) { | ||
174 | QStringList names; | ||
175 | for (const auto &folder : folders) { | ||
176 | names << folder->getName(); | ||
177 | } | ||
178 | QVERIFY(!names.contains("sub1")); | ||
179 | }); | ||
180 | VERIFYEXEC(job); | ||
181 | } | ||
182 | |||
183 | void testListMails() | ||
184 | { | ||
185 | Sink::Query query; | ||
186 | query.resources << "org.kde.imap.instance1"; | ||
187 | query.request<Mail::Subject>().request<Mail::MimeMessage>(); | ||
188 | |||
189 | // Ensure all local data is processed | ||
190 | VERIFYEXEC(Store::synchronize(query)); | ||
191 | ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); | ||
192 | |||
193 | auto job = Store::fetchAll<Mail>(query).then<void, QList<Mail::Ptr>>([](const QList<Mail::Ptr> &mails) { | ||
194 | QCOMPARE(mails.size(), 1); | ||
195 | QVERIFY(mails.first()->getSubject().startsWith(QString("[Nepomuk] Jenkins build is still unstable"))); | ||
196 | const auto data = mails.first()->getMimeMessage(); | ||
197 | QVERIFY(!data.isEmpty()); | ||
198 | |||
199 | KMime::Message m; | ||
200 | m.setContent(data); | ||
201 | m.parse(); | ||
202 | QCOMPARE(mails.first()->getSubject(), m.subject(true)->asUnicodeString()); | ||
203 | }); | ||
204 | VERIFYEXEC(job); | ||
205 | } | ||
206 | |||
207 | void testFetchNewMessages() | ||
208 | { | ||
209 | Sink::Query query; | ||
210 | query.resources << "org.kde.imap.instance1"; | ||
211 | query.request<Mail::Subject>().request<Mail::MimeMessage>(); | ||
212 | |||
213 | // Ensure all local data is processed | ||
214 | VERIFYEXEC(Store::synchronize(query)); | ||
215 | ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); | ||
216 | |||
217 | Imap::ImapServerProxy imap("localhost", 993); | ||
218 | VERIFYEXEC(imap.login("doe", "doe")); | ||
219 | |||
220 | auto msg = KMime::Message::Ptr::create(); | ||
221 | msg->subject(true)->fromUnicodeString("Foobar", "utf8"); | ||
222 | msg->assemble(); | ||
223 | |||
224 | VERIFYEXEC(imap.append("INBOX.test", msg->encodedContent(true))); | ||
225 | |||
226 | Store::synchronize(query).exec().waitForFinished(); | ||
227 | ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); | ||
228 | |||
229 | auto job = Store::fetchAll<Mail>(query).then<void, QList<Mail::Ptr>>([](const QList<Mail::Ptr> &mails) { | ||
230 | QCOMPARE(mails.size(), 2); | ||
231 | }); | ||
232 | VERIFYEXEC(job); | ||
233 | } | ||
234 | |||
235 | void testFetchRemovedMessages() | ||
236 | { | ||
237 | Sink::Query query; | ||
238 | query.resources << "org.kde.imap.instance1"; | ||
239 | query.request<Mail::Subject>().request<Mail::MimeMessage>(); | ||
240 | |||
241 | // Ensure all local data is processed | ||
242 | VERIFYEXEC(Store::synchronize(query)); | ||
243 | ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); | ||
244 | |||
245 | Imap::ImapServerProxy imap("localhost", 993); | ||
246 | VERIFYEXEC(imap.login("doe", "doe")); | ||
247 | |||
248 | VERIFYEXEC(imap.remove("INBOX.test", "2:*")); | ||
249 | |||
250 | Store::synchronize(query).exec().waitForFinished(); | ||
251 | ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); | ||
252 | |||
253 | auto job = Store::fetchAll<Mail>(query).then<void, QList<Mail::Ptr>>([](const QList<Mail::Ptr> &mails) { | ||
254 | QCOMPARE(mails.size(), 1); | ||
255 | }); | ||
256 | VERIFYEXEC(job); | ||
257 | } | ||
258 | |||
259 | void testFailingSync() | ||
260 | { | ||
261 | auto resource = ApplicationDomain::ImapResource::create("account1"); | ||
262 | resource.setProperty("server", "foobar"); | ||
263 | resource.setProperty("port", 993); | ||
264 | Sink::Store::create(resource).exec().waitForFinished(); | ||
265 | Sink::Query query; | ||
266 | query.resources << resource.identifier(); | ||
267 | |||
268 | // Ensure sync fails if resource is misconfigured | ||
269 | auto future = Store::synchronize(query).exec(); | ||
270 | future.waitForFinished(); | ||
271 | QVERIFY(future.errorCode()); | ||
272 | } | ||
273 | }; | ||
274 | |||
275 | QTEST_MAIN(ImapResourceTest) | ||
276 | #include "imapresourcetest.moc" | ||
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7146220..c9ad41f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt | |||
@@ -12,7 +12,7 @@ add_definitions(-DTESTDATAPATH="${CMAKE_CURRENT_SOURCE_DIR}/data") | |||
12 | 12 | ||
13 | find_package(KF5 COMPONENTS REQUIRED Mime) | 13 | find_package(KF5 COMPONENTS REQUIRED Mime) |
14 | 14 | ||
15 | add_library(sink_test SHARED testimplementations.cpp getrssusage.cpp mailtest.cpp) | 15 | add_library(sink_test SHARED testimplementations.cpp getrssusage.cpp mailtest.cpp mailsynctest.cpp) |
16 | qt5_use_modules(sink_test Core Test Concurrent) | 16 | qt5_use_modules(sink_test Core Test Concurrent) |
17 | target_link_libraries(sink_test sink libhawd KF5::Mime) | 17 | target_link_libraries(sink_test sink libhawd KF5::Mime) |
18 | 18 | ||
diff --git a/tests/mailsynctest.cpp b/tests/mailsynctest.cpp new file mode 100644 index 0000000..b86fbae --- /dev/null +++ b/tests/mailsynctest.cpp | |||
@@ -0,0 +1,241 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2016 Christian Mollekopf <chrigi_1@fastmail.fm> | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify | ||
5 | * it under the terms of the GNU General Public License as published by | ||
6 | * the Free Software Foundation; either version 2 of the License, or | ||
7 | * (at your option) any later version. | ||
8 | * | ||
9 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with this program; if not, write to the | ||
16 | * Free Software Foundation, Inc., | ||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
18 | */ | ||
19 | #include "mailsynctest.h" | ||
20 | |||
21 | #include <QtTest> | ||
22 | |||
23 | #include <QString> | ||
24 | #include <KMime/Message> | ||
25 | |||
26 | #include "store.h" | ||
27 | #include "resourcecontrol.h" | ||
28 | #include "log.h" | ||
29 | #include "test.h" | ||
30 | |||
31 | using namespace Sink; | ||
32 | using namespace Sink::ApplicationDomain; | ||
33 | |||
34 | void MailSyncTest::initTestCase() | ||
35 | { | ||
36 | Test::initTest(); | ||
37 | Log::setDebugOutputLevel(Sink::Log::Trace); | ||
38 | resetTestEnvironment(); | ||
39 | auto resource = createResource(); | ||
40 | QVERIFY(!resource.identifier().isEmpty()); | ||
41 | |||
42 | VERIFYEXEC(Store::create(resource)); | ||
43 | |||
44 | mResourceInstanceIdentifier = resource.identifier(); | ||
45 | mCapabilities = resource.getProperty("capabilities").value<QByteArrayList>(); | ||
46 | } | ||
47 | |||
48 | void MailSyncTest::cleanup() | ||
49 | { | ||
50 | // VERIFYEXEC(ResourceControl::shutdown(mResourceInstanceIdentifier)); | ||
51 | ResourceControl::shutdown(mResourceInstanceIdentifier).exec().waitForFinished(); | ||
52 | removeResourceFromDisk(mResourceInstanceIdentifier); | ||
53 | } | ||
54 | |||
55 | void MailSyncTest::init() | ||
56 | { | ||
57 | qDebug(); | ||
58 | qDebug() << "-----------------------------------------"; | ||
59 | qDebug(); | ||
60 | VERIFYEXEC(ResourceControl::start(mResourceInstanceIdentifier)); | ||
61 | } | ||
62 | |||
63 | void MailSyncTest::testListFolders() | ||
64 | { | ||
65 | Sink::Query query; | ||
66 | query.resources << mResourceInstanceIdentifier; | ||
67 | query.request<Folder::Name>(); | ||
68 | |||
69 | // Ensure all local data is processed | ||
70 | VERIFYEXEC(Store::synchronize(query)); | ||
71 | ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); | ||
72 | |||
73 | auto job = Store::fetchAll<Folder>(query).then<void, QList<Folder::Ptr>>([](const QList<Folder::Ptr> &folders) { | ||
74 | QCOMPARE(folders.size(), 2); | ||
75 | QStringList names; | ||
76 | for (const auto &folder : folders) { | ||
77 | names << folder->getName(); | ||
78 | } | ||
79 | QVERIFY(names.contains("INBOX")); | ||
80 | QVERIFY(names.contains("test")); | ||
81 | }); | ||
82 | VERIFYEXEC(job); | ||
83 | } | ||
84 | |||
85 | void MailSyncTest::testListFolderHierarchy() | ||
86 | { | ||
87 | Sink::Query query; | ||
88 | query.resources << mResourceInstanceIdentifier; | ||
89 | query.request<Folder::Name>().request<Folder::Parent>(); | ||
90 | |||
91 | createFolder(QStringList() << "test" << "sub"); | ||
92 | |||
93 | // Ensure all local data is processed | ||
94 | VERIFYEXEC(Store::synchronize(query)); | ||
95 | ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); | ||
96 | |||
97 | auto job = Store::fetchAll<Folder>(query).then<void, QList<Folder::Ptr>>([](const QList<Folder::Ptr> &folders) { | ||
98 | QCOMPARE(folders.size(), 3); | ||
99 | QHash<QString, Folder::Ptr> map; | ||
100 | for (const auto &folder : folders) { | ||
101 | map.insert(folder->getName(), folder); | ||
102 | } | ||
103 | QCOMPARE(map.value("sub")->getParent(), map.value("test")->identifier()); | ||
104 | }); | ||
105 | VERIFYEXEC(job); | ||
106 | } | ||
107 | |||
108 | void MailSyncTest::testListNewFolders() | ||
109 | { | ||
110 | Sink::Query query; | ||
111 | query.resources << mResourceInstanceIdentifier; | ||
112 | query.request<Folder::Name>(); | ||
113 | |||
114 | createFolder(QStringList() << "test" << "sub1"); | ||
115 | |||
116 | // Ensure all local data is processed | ||
117 | VERIFYEXEC(Store::synchronize(query)); | ||
118 | ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); | ||
119 | |||
120 | auto job = Store::fetchAll<Folder>(query).then<void, QList<Folder::Ptr>>([](const QList<Folder::Ptr> &folders) { | ||
121 | QStringList names; | ||
122 | for (const auto &folder : folders) { | ||
123 | names << folder->getName(); | ||
124 | } | ||
125 | QVERIFY(names.contains("sub1")); | ||
126 | }); | ||
127 | VERIFYEXEC(job); | ||
128 | } | ||
129 | |||
130 | void MailSyncTest::testListRemovedFolders() | ||
131 | { | ||
132 | Sink::Query query; | ||
133 | query.resources << mResourceInstanceIdentifier; | ||
134 | query.request<Folder::Name>(); | ||
135 | |||
136 | VERIFYEXEC(Store::synchronize(query)); | ||
137 | ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); | ||
138 | |||
139 | removeFolder(QStringList() << "test" << "sub1"); | ||
140 | |||
141 | // Ensure all local data is processed | ||
142 | VERIFYEXEC(Store::synchronize(query)); | ||
143 | ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); | ||
144 | |||
145 | auto job = Store::fetchAll<Folder>(query).then<void, QList<Folder::Ptr>>([](const QList<Folder::Ptr> &folders) { | ||
146 | QStringList names; | ||
147 | for (const auto &folder : folders) { | ||
148 | names << folder->getName(); | ||
149 | } | ||
150 | QVERIFY(!names.contains("sub1")); | ||
151 | }); | ||
152 | VERIFYEXEC(job); | ||
153 | } | ||
154 | |||
155 | void MailSyncTest::testListMails() | ||
156 | { | ||
157 | Sink::Query query; | ||
158 | query.resources << mResourceInstanceIdentifier; | ||
159 | query.request<Mail::Subject>().request<Mail::MimeMessage>(); | ||
160 | |||
161 | // Ensure all local data is processed | ||
162 | VERIFYEXEC(Store::synchronize(query)); | ||
163 | ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); | ||
164 | |||
165 | auto job = Store::fetchAll<Mail>(query).then<void, QList<Mail::Ptr>>([](const QList<Mail::Ptr> &mails) { | ||
166 | QCOMPARE(mails.size(), 1); | ||
167 | QVERIFY(mails.first()->getSubject().startsWith(QString("[Nepomuk] Jenkins build is still unstable"))); | ||
168 | const auto data = mails.first()->getMimeMessage(); | ||
169 | QVERIFY(!data.isEmpty()); | ||
170 | |||
171 | KMime::Message m; | ||
172 | m.setContent(data); | ||
173 | m.parse(); | ||
174 | QCOMPARE(mails.first()->getSubject(), m.subject(true)->asUnicodeString()); | ||
175 | }); | ||
176 | VERIFYEXEC(job); | ||
177 | } | ||
178 | |||
179 | void MailSyncTest::testFetchNewMessages() | ||
180 | { | ||
181 | Sink::Query query; | ||
182 | query.resources << mResourceInstanceIdentifier; | ||
183 | query.request<Mail::Subject>().request<Mail::MimeMessage>(); | ||
184 | |||
185 | // Ensure all local data is processed | ||
186 | VERIFYEXEC(Store::synchronize(query)); | ||
187 | ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); | ||
188 | |||
189 | auto msg = KMime::Message::Ptr::create(); | ||
190 | msg->subject(true)->fromUnicodeString("Foobar", "utf8"); | ||
191 | msg->assemble(); | ||
192 | createMessage(QStringList() << "test", msg->encodedContent(true)); | ||
193 | |||
194 | Store::synchronize(query).exec().waitForFinished(); | ||
195 | ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); | ||
196 | |||
197 | auto job = Store::fetchAll<Mail>(query).then<void, QList<Mail::Ptr>>([](const QList<Mail::Ptr> &mails) { | ||
198 | QCOMPARE(mails.size(), 2); | ||
199 | }); | ||
200 | VERIFYEXEC(job); | ||
201 | } | ||
202 | |||
203 | void MailSyncTest::testFetchRemovedMessages() | ||
204 | { | ||
205 | Sink::Query query; | ||
206 | query.resources << mResourceInstanceIdentifier; | ||
207 | query.request<Mail::Subject>().request<Mail::MimeMessage>(); | ||
208 | |||
209 | // Ensure all local data is processed | ||
210 | VERIFYEXEC(Store::synchronize(query)); | ||
211 | ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); | ||
212 | |||
213 | removeMessage(QStringList() << "test", "2:*"); | ||
214 | |||
215 | Store::synchronize(query).exec().waitForFinished(); | ||
216 | ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); | ||
217 | |||
218 | auto job = Store::fetchAll<Mail>(query).then<void, QList<Mail::Ptr>>([](const QList<Mail::Ptr> &mails) { | ||
219 | QCOMPARE(mails.size(), 1); | ||
220 | }); | ||
221 | VERIFYEXEC(job); | ||
222 | } | ||
223 | |||
224 | //TODO test flag sync | ||
225 | |||
226 | void MailSyncTest::testFailingSync() | ||
227 | { | ||
228 | auto resource = createFaultyResource(); | ||
229 | QVERIFY(!resource.identifier().isEmpty()); | ||
230 | VERIFYEXEC(Store::create(resource)); | ||
231 | |||
232 | Sink::Query query; | ||
233 | query.resources << resource.identifier(); | ||
234 | |||
235 | // Ensure sync fails if resource is misconfigured | ||
236 | auto future = Store::synchronize(query).exec(); | ||
237 | future.waitForFinished(); | ||
238 | QVERIFY(future.errorCode()); | ||
239 | } | ||
240 | |||
241 | #include "mailsynctest.moc" | ||
diff --git a/tests/mailsynctest.h b/tests/mailsynctest.h new file mode 100644 index 0000000..f9ee7be --- /dev/null +++ b/tests/mailsynctest.h | |||
@@ -0,0 +1,83 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2016 Christian Mollekopf <chrigi_1@fastmail.fm> | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify | ||
5 | * it under the terms of the GNU General Public License as published by | ||
6 | * the Free Software Foundation; either version 2 of the License, or | ||
7 | * (at your option) any later version. | ||
8 | * | ||
9 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with this program; if not, write to the | ||
16 | * Free Software Foundation, Inc., | ||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
18 | */ | ||
19 | #pragma once | ||
20 | |||
21 | #include <QObject> | ||
22 | #include <QByteArray> | ||
23 | |||
24 | #include <applicationdomaintype.h> | ||
25 | |||
26 | #define ASYNCCOMPARE(actual, expected) \ | ||
27 | do {\ | ||
28 | if (!QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__))\ | ||
29 | return KAsync::error<void>(1, "Comparison failed.");\ | ||
30 | } while (0) | ||
31 | |||
32 | #define ASYNCVERIFY(statement) \ | ||
33 | do {\ | ||
34 | if (!QTest::qVerify((statement), #statement, "", __FILE__, __LINE__))\ | ||
35 | return KAsync::error<void>(1, "Verify failed.");\ | ||
36 | } while (0) | ||
37 | |||
38 | #define VERIFYEXEC(statement) \ | ||
39 | do {\ | ||
40 | auto result = statement.exec(); \ | ||
41 | result.waitForFinished(); \ | ||
42 | if (!QTest::qVerify(!result.errorCode(), #statement, "", __FILE__, __LINE__))\ | ||
43 | return;\ | ||
44 | } while (0) | ||
45 | |||
46 | namespace Sink { | ||
47 | |||
48 | class MailSyncTest : public QObject | ||
49 | { | ||
50 | Q_OBJECT | ||
51 | |||
52 | protected: | ||
53 | QByteArray mResourceInstanceIdentifier; | ||
54 | QByteArrayList mCapabilities; | ||
55 | |||
56 | virtual void resetTestEnvironment() = 0; | ||
57 | virtual Sink::ApplicationDomain::SinkResource createResource() = 0; | ||
58 | virtual Sink::ApplicationDomain::SinkResource createFaultyResource() = 0; | ||
59 | virtual void removeResourceFromDisk(const QByteArray &mResourceInstanceIdentifier) = 0; | ||
60 | virtual void createFolder(const QStringList &folderPath) = 0; | ||
61 | virtual void removeFolder(const QStringList &folderPath) = 0; | ||
62 | virtual void createMessage(const QStringList &folderPath, const QByteArray &message) = 0; | ||
63 | virtual void removeMessage(const QStringList &folderPath, const QByteArray &message) = 0; | ||
64 | |||
65 | private slots: | ||
66 | void initTestCase(); | ||
67 | void init(); | ||
68 | void cleanup(); | ||
69 | |||
70 | void testListFolders(); | ||
71 | void testListFolderHierarchy(); | ||
72 | void testListNewFolders(); | ||
73 | void testListRemovedFolders(); | ||
74 | |||
75 | void testListMails(); | ||
76 | void testFetchNewMessages(); | ||
77 | void testFetchRemovedMessages(); | ||
78 | |||
79 | void testFailingSync(); | ||
80 | }; | ||
81 | |||
82 | } | ||
83 | |||