summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-05-24 09:20:02 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-05-24 09:20:02 +0200
commit6434a87199b3259e9002570332beaa8caf5d2213 (patch)
tree87c5fe877e350b067b37961b9af2058693be8665
parentf47270cea65545f946ff301014cb09bd5ff40261 (diff)
downloadsink-6434a87199b3259e9002570332beaa8caf5d2213.tar.gz
sink-6434a87199b3259e9002570332beaa8caf5d2213.zip
A generic mailtest that can be applied to all resources that support
mails.
-rw-r--r--examples/imapresource/tests/CMakeLists.txt2
-rw-r--r--examples/imapresource/tests/imapmailtest.cpp45
-rw-r--r--tests/CMakeLists.txt14
-rw-r--r--tests/mailtest.cpp183
-rw-r--r--tests/mailtest.h69
5 files changed, 309 insertions, 4 deletions
diff --git a/examples/imapresource/tests/CMakeLists.txt b/examples/imapresource/tests/CMakeLists.txt
index 8482720..b10adcd 100644
--- a/examples/imapresource/tests/CMakeLists.txt
+++ b/examples/imapresource/tests/CMakeLists.txt
@@ -16,8 +16,10 @@ endmacro(auto_tests)
16auto_tests ( 16auto_tests (
17 imapresourcetest 17 imapresourcetest
18 imapserverproxytest 18 imapserverproxytest
19 imapmailtest
19) 20)
20target_link_libraries(imapresourcetest sink_resource_imap) 21target_link_libraries(imapresourcetest sink_resource_imap)
21target_link_libraries(imapserverproxytest sink_resource_imap) 22target_link_libraries(imapserverproxytest sink_resource_imap)
23target_link_libraries(imapmailtest sink_test sink_resource_imap)
22 24
23install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/resetmailbox.sh DESTINATION bin PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ GROUP_EXECUTE GROUP_READ) 25install(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/imapmailtest.cpp b/examples/imapresource/tests/imapmailtest.cpp
new file mode 100644
index 0000000..82324af
--- /dev/null
+++ b/examples/imapresource/tests/imapmailtest.cpp
@@ -0,0 +1,45 @@
1#include <QtTest>
2
3#include <tests/mailtest.h>
4#include "../imapresource.h"
5
6#include "common/test.h"
7#include "common/domain/applicationdomaintype.h"
8
9using namespace Sink;
10using namespace Sink::ApplicationDomain;
11
12/**
13 * Test of complete system using the imap resource.
14 *
15 * This test requires the imap resource installed.
16 */
17class ImapMailTest : public Sink::MailTest
18{
19 Q_OBJECT
20
21protected:
22 void resetTestEnvironment() Q_DECL_OVERRIDE
23 {
24 system("resetmailbox.sh");
25 }
26
27 Sink::ApplicationDomain::SinkResource createResource() Q_DECL_OVERRIDE
28 {
29 auto resource = ApplicationDomain::ImapResource::create("account1");
30 resource.setProperty("server", "localhost");
31 resource.setProperty("port", 993);
32 resource.setProperty("user", "doe");
33 resource.setProperty("password", "doe");
34 return resource;
35 }
36
37 void removeResourceFromDisk(const QByteArray &identifier) Q_DECL_OVERRIDE
38 {
39 ::ImapResource::removeFromDisk(identifier);
40 }
41};
42
43QTEST_MAIN(ImapMailTest)
44
45#include "imapmailtest.moc"
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index c9f7591..9de6ffa 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -10,22 +10,28 @@ include_directories(
10 10
11add_definitions(-DTESTDATAPATH="${CMAKE_CURRENT_SOURCE_DIR}/data") 11add_definitions(-DTESTDATAPATH="${CMAKE_CURRENT_SOURCE_DIR}/data")
12 12
13find_package(KF5 COMPONENTS REQUIRED Mime)
14
15add_library(sink_test SHARED testimplementations.cpp getrssusage.cpp mailtest.cpp)
16qt5_use_modules(sink_test Core Test Concurrent)
17target_link_libraries(sink_test sink libhawd KF5::Mime)
18
13macro(manual_tests) 19macro(manual_tests)
14 foreach(_testname ${ARGN}) 20 foreach(_testname ${ARGN})
15 add_executable(${_testname} ${_testname}.cpp testimplementations.cpp getrssusage.cpp) 21 add_executable(${_testname} ${_testname}.cpp)
16 generate_flatbuffers(${_testname} calendar) 22 generate_flatbuffers(${_testname} calendar)
17 qt5_use_modules(${_testname} Core Test Concurrent) 23 qt5_use_modules(${_testname} Core Test Concurrent)
18 target_link_libraries(${_testname} sink libhawd) 24 target_link_libraries(${_testname} sink libhawd sink_test)
19 endforeach(_testname) 25 endforeach(_testname)
20endmacro(manual_tests) 26endmacro(manual_tests)
21 27
22macro(auto_tests) 28macro(auto_tests)
23 foreach(_testname ${ARGN}) 29 foreach(_testname ${ARGN})
24 add_executable(${_testname} ${_testname}.cpp testimplementations.cpp getrssusage.cpp) 30 add_executable(${_testname} ${_testname}.cpp)
25 generate_flatbuffers(${_testname} calendar) 31 generate_flatbuffers(${_testname} calendar)
26 add_test(${_testname} ${_testname}) 32 add_test(${_testname} ${_testname})
27 qt5_use_modules(${_testname} Core Test Concurrent) 33 qt5_use_modules(${_testname} Core Test Concurrent)
28 target_link_libraries(${_testname} sink libhawd) 34 target_link_libraries(${_testname} sink libhawd sink_test)
29 endforeach(_testname) 35 endforeach(_testname)
30endmacro(auto_tests) 36endmacro(auto_tests)
31 37
diff --git a/tests/mailtest.cpp b/tests/mailtest.cpp
new file mode 100644
index 0000000..79a077c
--- /dev/null
+++ b/tests/mailtest.cpp
@@ -0,0 +1,183 @@
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 "mailtest.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
31using namespace Sink;
32using namespace Sink::ApplicationDomain;
33
34void MailTest::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}
46
47void MailTest::cleanup()
48{
49 VERIFYEXEC(ResourceControl::shutdown(mResourceInstanceIdentifier));
50 removeResourceFromDisk(mResourceInstanceIdentifier);
51}
52
53void MailTest::init()
54{
55 qDebug();
56 qDebug() << "-----------------------------------------";
57 qDebug();
58 VERIFYEXEC(ResourceControl::start(mResourceInstanceIdentifier));
59}
60
61void MailTest::testCreateModifyDeleteFolder()
62{
63 QString name = "name";
64 QByteArray icon = "icon";
65
66 auto folder = Folder::create(mResourceInstanceIdentifier);
67 folder.setName(name);
68 folder.setIcon(icon);
69
70 VERIFYEXEC(Store::create(folder));
71 VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mResourceInstanceIdentifier));
72 {
73 auto job = Store::fetchAll<Folder>(Query::RequestedProperties(QByteArrayList() << Folder::Name::name << Folder::Icon::name))
74 .then<void, QList<Folder::Ptr>>([=](const QList<Folder::Ptr> &folders) {
75 QCOMPARE(folders.size(), 1);
76 auto folder = *folders.first();
77 QCOMPARE(folder.getName(), name);
78 QCOMPARE(folder.getIcon(), icon);
79 });
80 VERIFYEXEC(job);
81 }
82
83 QString name2 = "name2";
84 QByteArray icon2 = "icon2";
85 folder.setName(name2);
86 folder.setIcon(icon2);
87
88 VERIFYEXEC(Store::modify(folder));
89 VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mResourceInstanceIdentifier));
90 {
91 auto job = Store::fetchAll<Folder>(Query::RequestedProperties(QByteArrayList() << Folder::Name::name << Folder::Icon::name))
92 .then<void, QList<Folder::Ptr>>([=](const QList<Folder::Ptr> &folders) {
93 QCOMPARE(folders.size(), 1);
94 auto folder = *folders.first();
95 QCOMPARE(folder.getName(), name2);
96 QCOMPARE(folder.getIcon(), icon2);
97 });
98 VERIFYEXEC(job);
99 }
100
101 VERIFYEXEC(Store::remove(folder));
102 VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mResourceInstanceIdentifier));
103 {
104 auto job = Store::fetchAll<Folder>(Query::RequestedProperties(QByteArrayList() << Folder::Name::name << Folder::Icon::name))
105 .then<void, QList<Folder::Ptr>>([=](const QList<Folder::Ptr> &folders) {
106 QCOMPARE(folders.size(), 0);
107 });
108 VERIFYEXEC(job);
109 }
110}
111
112void MailTest::testCreateModifyDeleteMail()
113{
114
115 const auto subject = QString::fromLatin1("Foobar");
116
117 Query query;
118 query.resources << mResourceInstanceIdentifier;
119 query.request<Mail::Folder>().request<Mail::Subject>();
120
121 auto folder = Folder::create(mResourceInstanceIdentifier);
122 folder.setName("folder");
123 VERIFYEXEC(Store::create(folder));
124
125 auto message = KMime::Message::Ptr::create();
126 message->subject(true)->fromUnicodeString(subject, "utf8");
127 message->assemble();
128
129 auto mail = Mail::create(mResourceInstanceIdentifier);
130 mail.setMimeMessage(message->encodedContent());
131 mail.setFolder(folder);
132
133 VERIFYEXEC(Store::create(mail));
134 VERIFYEXEC(ResourceControl::flushMessageQueue(query.resources));
135 {
136 auto job = Store::fetchAll<Mail>(Query::RequestedProperties(QByteArrayList() << Mail::Folder::name << Mail::Subject::name))
137 .then<void, QList<Mail::Ptr>>([=](const QList<Mail::Ptr> &mails) {
138 QCOMPARE(mails.size(), 1);
139 auto mail = *mails.first();
140 // QCOMPARE(mail.getSubject(), subject);
141 QCOMPARE(mail.getFolder(), folder.identifier());
142 // TODO test access to mime message
143
144 // return Store::remove(*mail)
145 // .then(ResourceControl::flushReplayQueue(query.resources)) // The change needs to be replayed already
146 // .then(ResourceControl::inspect<Mail>(ResourceControl::Inspection::ExistenceInspection(*mail, false)));
147 });
148 VERIFYEXEC(job);
149 }
150
151 const auto subject2 = QString::fromLatin1("Foobar2");
152 auto message2 = KMime::Message::Ptr::create();
153 message2->subject(true)->fromUnicodeString(subject2, "utf8");
154 message2->assemble();
155 mail.setMimeMessage(message2->encodedContent());
156
157 VERIFYEXEC(Store::modify(mail));
158 VERIFYEXEC(ResourceControl::flushMessageQueue(query.resources));
159 {
160 auto job = Store::fetchAll<Mail>(Query::RequestedProperties(QByteArrayList() << Mail::Folder::name << Mail::Subject::name))
161 .then<void, QList<Mail::Ptr>>([=](const QList<Mail::Ptr> &mails) {
162 QCOMPARE(mails.size(), 1);
163 auto mail = *mails.first();
164 QCOMPARE(mail.getFolder(), folder.identifier());
165 // QCOMPARE(mail.getSubject(), subject);
166 // TODO test access to modified mime message
167
168 });
169 VERIFYEXEC(job);
170 }
171
172 VERIFYEXEC(Store::remove(mail));
173 VERIFYEXEC(ResourceControl::flushMessageQueue(query.resources));
174 {
175 auto job = Store::fetchAll<Mail>(Query::RequestedProperties(QByteArrayList() << Mail::Folder::name << Mail::Subject::name))
176 .then<void, QList<Mail::Ptr>>([=](const QList<Mail::Ptr> &mails) {
177 QCOMPARE(mails.size(), 0);
178 });
179 VERIFYEXEC(job);
180 }
181}
182
183#include "mailtest.moc"
diff --git a/tests/mailtest.h b/tests/mailtest.h
new file mode 100644
index 0000000..0729a91
--- /dev/null
+++ b/tests/mailtest.h
@@ -0,0 +1,69 @@
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) \
27do {\
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) \
33do {\
34 if (!QTest::qVerify((statement), #statement, "", __FILE__, __LINE__))\
35 return KAsync::error<void>(1, "Verify failed.");\
36} while (0)
37
38#define VERIFYEXEC(statement) \
39do {\
40 auto result = statement.exec(); \
41 result.waitForFinished(); \
42 if (!QTest::qVerify(!result.errorCode(), #statement, "", __FILE__, __LINE__))\
43 return;\
44} while (0)
45
46namespace Sink {
47
48class MailTest : public QObject
49{
50 Q_OBJECT
51
52protected:
53 QByteArray mResourceInstanceIdentifier;
54
55 virtual void resetTestEnvironment() = 0;
56 virtual Sink::ApplicationDomain::SinkResource createResource() = 0;
57 virtual void removeResourceFromDisk(const QByteArray &mResourceInstanceIdentifier) = 0;
58
59private slots:
60 void initTestCase();
61 void init();
62 void cleanup();
63
64 void testCreateModifyDeleteFolder();
65 void testCreateModifyDeleteMail();
66};
67
68}
69