From f90ca753a4b723dbf72996bbd1261dc786c7cce6 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Tue, 31 May 2016 11:24:59 +0200 Subject: Replace the imapresourcetest with the generic mailsynctest --- examples/imapresource/tests/CMakeLists.txt | 4 +- examples/imapresource/tests/imapmailsynctest.cpp | 84 +++++++ examples/imapresource/tests/imapresourcetest.cpp | 276 ----------------------- tests/CMakeLists.txt | 2 +- tests/mailsynctest.cpp | 241 ++++++++++++++++++++ tests/mailsynctest.h | 83 +++++++ 6 files changed, 411 insertions(+), 279 deletions(-) create mode 100644 examples/imapresource/tests/imapmailsynctest.cpp delete mode 100644 examples/imapresource/tests/imapresourcetest.cpp create mode 100644 tests/mailsynctest.cpp create mode 100644 tests/mailsynctest.h 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( include(SinkTest) auto_tests ( - imapresourcetest imapserverproxytest imapmailtest + imapmailsynctest ) -target_link_libraries(imapresourcetest sink_resource_imap) target_link_libraries(imapserverproxytest sink_resource_imap) target_link_libraries(imapmailtest sink_resource_imap) +target_link_libraries(imapmailsynctest sink_resource_imap) 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 @@ +#include + +#include +#include "../imapresource.h" +#include "../imapserverproxy.h" + +#include "common/test.h" +#include "common/domain/applicationdomaintype.h" + +using namespace Sink; +using namespace Sink::ApplicationDomain; + +/** + * Test of complete system using the imap resource. + * + * This test requires the imap resource installed. + */ +class ImapMailSyncTest : public Sink::MailSyncTest +{ + Q_OBJECT + +protected: + void resetTestEnvironment() Q_DECL_OVERRIDE + { + system("resetmailbox.sh"); + } + + Sink::ApplicationDomain::SinkResource createResource() Q_DECL_OVERRIDE + { + auto resource = ApplicationDomain::ImapResource::create("account1"); + resource.setProperty("server", "localhost"); + resource.setProperty("port", 993); + resource.setProperty("user", "doe"); + resource.setProperty("password", "doe"); + return resource; + } + + Sink::ApplicationDomain::SinkResource createFaultyResource() Q_DECL_OVERRIDE + { + auto resource = ApplicationDomain::ImapResource::create("account1"); + resource.setProperty("server", "foobar"); + resource.setProperty("port", 993); + resource.setProperty("user", "doe"); + resource.setProperty("password", "doe"); + return resource; + } + + void removeResourceFromDisk(const QByteArray &identifier) Q_DECL_OVERRIDE + { + ::ImapResource::removeFromDisk(identifier); + } + + void createFolder(const QStringList &folderPath) Q_DECL_OVERRIDE + { + Imap::ImapServerProxy imap("localhost", 993); + VERIFYEXEC(imap.login("doe", "doe")); + VERIFYEXEC(imap.create("INBOX." + folderPath.join('.'))); + } + + void removeFolder(const QStringList &folderPath) Q_DECL_OVERRIDE + { + Imap::ImapServerProxy imap("localhost", 993); + VERIFYEXEC(imap.login("doe", "doe")); + VERIFYEXEC(imap.remove("INBOX." + folderPath.join('.'))); + } + + void createMessage(const QStringList &folderPath, const QByteArray &message) Q_DECL_OVERRIDE + { + Imap::ImapServerProxy imap("localhost", 993); + VERIFYEXEC(imap.login("doe", "doe")); + VERIFYEXEC(imap.append("INBOX." + folderPath.join('.'), message)); + } + + void removeMessage(const QStringList &folderPath, const QByteArray &messages) Q_DECL_OVERRIDE + { + Imap::ImapServerProxy imap("localhost", 993); + VERIFYEXEC(imap.login("doe", "doe")); + VERIFYEXEC(imap.remove("INBOX." + folderPath.join('.'), "2:*")); + } +}; + +QTEST_MAIN(ImapMailSyncTest) + +#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 @@ -#include - -#include -#include - -// #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(1, "Comparison failed.");\ -} while (0) - -#define ASYNCVERIFY(statement) \ -do {\ - if (!QTest::qVerify((statement), #statement, "", __FILE__, __LINE__))\ - return KAsync::error(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() - { - 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("user", "doe"); - resource.setProperty("password", "doe"); - 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(); - - // Ensure all local data is processed - VERIFYEXEC(Store::synchronize(query)); - ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); - - auto job = Store::fetchAll(query).then>([](const QList &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().request(); - - 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(query).then>([](const QList &folders) { - QCOMPARE(folders.size(), 3); - QHash map; - for (const auto &folder : folders) { - map.insert(folder->getName(), folder); - } - QCOMPARE(map.value("sub")->getParent(), map.value("test")->identifier()); - }); - VERIFYEXEC(job); - } - - void testListNewFolders() - { - Sink::Query query; - query.resources << "org.kde.imap.instance1"; - query.request(); - - Imap::ImapServerProxy imap("localhost", 993); - VERIFYEXEC(imap.login("doe", "doe")); - VERIFYEXEC(imap.create("INBOX.test.sub1")); - - // Ensure all local data is processed - VERIFYEXEC(Store::synchronize(query)); - ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); - - auto job = Store::fetchAll(query).then>([](const QList &folders) { - QStringList names; - for (const auto &folder : folders) { - names << folder->getName(); - } - QVERIFY(names.contains("sub1")); - }); - VERIFYEXEC(job); - } - - void testListRemovedFolders() - { - Sink::Query query; - query.resources << "org.kde.imap.instance1"; - query.request(); - - VERIFYEXEC(Store::synchronize(query)); - ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); - - Imap::ImapServerProxy imap("localhost", 993); - VERIFYEXEC(imap.login("doe", "doe")); - VERIFYEXEC(imap.remove("INBOX.test.sub1")); - - // Ensure all local data is processed - VERIFYEXEC(Store::synchronize(query)); - ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); - - auto job = Store::fetchAll(query).then>([](const QList &folders) { - QStringList names; - for (const auto &folder : folders) { - names << folder->getName(); - } - QVERIFY(!names.contains("sub1")); - }); - VERIFYEXEC(job); - } - - void testListMails() - { - Sink::Query query; - query.resources << "org.kde.imap.instance1"; - query.request().request(); - - // Ensure all local data is processed - VERIFYEXEC(Store::synchronize(query)); - ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); - - auto job = Store::fetchAll(query).then>([](const QList &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().request(); - - // Ensure all local data is processed - VERIFYEXEC(Store::synchronize(query)); - ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); - - Imap::ImapServerProxy imap("localhost", 993); - VERIFYEXEC(imap.login("doe", "doe")); - - 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(query).then>([](const QList &mails) { - QCOMPARE(mails.size(), 2); - }); - VERIFYEXEC(job); - } - - void testFetchRemovedMessages() - { - Sink::Query query; - query.resources << "org.kde.imap.instance1"; - query.request().request(); - - // Ensure all local data is processed - VERIFYEXEC(Store::synchronize(query)); - ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); - - Imap::ImapServerProxy imap("localhost", 993); - VERIFYEXEC(imap.login("doe", "doe")); - - VERIFYEXEC(imap.remove("INBOX.test", "2:*")); - - Store::synchronize(query).exec().waitForFinished(); - ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); - - auto job = Store::fetchAll(query).then>([](const QList &mails) { - QCOMPARE(mails.size(), 1); - }); - 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" 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") find_package(KF5 COMPONENTS REQUIRED Mime) -add_library(sink_test SHARED testimplementations.cpp getrssusage.cpp mailtest.cpp) +add_library(sink_test SHARED testimplementations.cpp getrssusage.cpp mailtest.cpp mailsynctest.cpp) qt5_use_modules(sink_test Core Test Concurrent) target_link_libraries(sink_test sink libhawd KF5::Mime) 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 @@ +/* + * Copyright (C) 2016 Christian Mollekopf + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#include "mailsynctest.h" + +#include + +#include +#include + +#include "store.h" +#include "resourcecontrol.h" +#include "log.h" +#include "test.h" + +using namespace Sink; +using namespace Sink::ApplicationDomain; + +void MailSyncTest::initTestCase() +{ + Test::initTest(); + Log::setDebugOutputLevel(Sink::Log::Trace); + resetTestEnvironment(); + auto resource = createResource(); + QVERIFY(!resource.identifier().isEmpty()); + + VERIFYEXEC(Store::create(resource)); + + mResourceInstanceIdentifier = resource.identifier(); + mCapabilities = resource.getProperty("capabilities").value(); +} + +void MailSyncTest::cleanup() +{ + // VERIFYEXEC(ResourceControl::shutdown(mResourceInstanceIdentifier)); + ResourceControl::shutdown(mResourceInstanceIdentifier).exec().waitForFinished(); + removeResourceFromDisk(mResourceInstanceIdentifier); +} + +void MailSyncTest::init() +{ + qDebug(); + qDebug() << "-----------------------------------------"; + qDebug(); + VERIFYEXEC(ResourceControl::start(mResourceInstanceIdentifier)); +} + +void MailSyncTest::testListFolders() +{ + Sink::Query query; + query.resources << mResourceInstanceIdentifier; + query.request(); + + // Ensure all local data is processed + VERIFYEXEC(Store::synchronize(query)); + ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); + + auto job = Store::fetchAll(query).then>([](const QList &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 MailSyncTest::testListFolderHierarchy() +{ + Sink::Query query; + query.resources << mResourceInstanceIdentifier; + query.request().request(); + + createFolder(QStringList() << "test" << "sub"); + + // Ensure all local data is processed + VERIFYEXEC(Store::synchronize(query)); + ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); + + auto job = Store::fetchAll(query).then>([](const QList &folders) { + QCOMPARE(folders.size(), 3); + QHash map; + for (const auto &folder : folders) { + map.insert(folder->getName(), folder); + } + QCOMPARE(map.value("sub")->getParent(), map.value("test")->identifier()); + }); + VERIFYEXEC(job); +} + +void MailSyncTest::testListNewFolders() +{ + Sink::Query query; + query.resources << mResourceInstanceIdentifier; + query.request(); + + createFolder(QStringList() << "test" << "sub1"); + + // Ensure all local data is processed + VERIFYEXEC(Store::synchronize(query)); + ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); + + auto job = Store::fetchAll(query).then>([](const QList &folders) { + QStringList names; + for (const auto &folder : folders) { + names << folder->getName(); + } + QVERIFY(names.contains("sub1")); + }); + VERIFYEXEC(job); +} + +void MailSyncTest::testListRemovedFolders() +{ + Sink::Query query; + query.resources << mResourceInstanceIdentifier; + query.request(); + + VERIFYEXEC(Store::synchronize(query)); + ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); + + removeFolder(QStringList() << "test" << "sub1"); + + // Ensure all local data is processed + VERIFYEXEC(Store::synchronize(query)); + ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); + + auto job = Store::fetchAll(query).then>([](const QList &folders) { + QStringList names; + for (const auto &folder : folders) { + names << folder->getName(); + } + QVERIFY(!names.contains("sub1")); + }); + VERIFYEXEC(job); +} + +void MailSyncTest::testListMails() +{ + Sink::Query query; + query.resources << mResourceInstanceIdentifier; + query.request().request(); + + // Ensure all local data is processed + VERIFYEXEC(Store::synchronize(query)); + ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); + + auto job = Store::fetchAll(query).then>([](const QList &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 MailSyncTest::testFetchNewMessages() +{ + Sink::Query query; + query.resources << mResourceInstanceIdentifier; + query.request().request(); + + // Ensure all local data is processed + VERIFYEXEC(Store::synchronize(query)); + ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); + + auto msg = KMime::Message::Ptr::create(); + msg->subject(true)->fromUnicodeString("Foobar", "utf8"); + msg->assemble(); + createMessage(QStringList() << "test", msg->encodedContent(true)); + + Store::synchronize(query).exec().waitForFinished(); + ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); + + auto job = Store::fetchAll(query).then>([](const QList &mails) { + QCOMPARE(mails.size(), 2); + }); + VERIFYEXEC(job); +} + +void MailSyncTest::testFetchRemovedMessages() +{ + Sink::Query query; + query.resources << mResourceInstanceIdentifier; + query.request().request(); + + // Ensure all local data is processed + VERIFYEXEC(Store::synchronize(query)); + ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); + + removeMessage(QStringList() << "test", "2:*"); + + Store::synchronize(query).exec().waitForFinished(); + ResourceControl::flushMessageQueue(query.resources).exec().waitForFinished(); + + auto job = Store::fetchAll(query).then>([](const QList &mails) { + QCOMPARE(mails.size(), 1); + }); + VERIFYEXEC(job); +} + +//TODO test flag sync + +void MailSyncTest::testFailingSync() +{ + auto resource = createFaultyResource(); + QVERIFY(!resource.identifier().isEmpty()); + VERIFYEXEC(Store::create(resource)); + + 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()); +} + +#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 @@ +/* + * Copyright (C) 2016 Christian Mollekopf + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#pragma once + +#include +#include + +#include + +#define ASYNCCOMPARE(actual, expected) \ +do {\ + if (!QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__))\ + return KAsync::error(1, "Comparison failed.");\ +} while (0) + +#define ASYNCVERIFY(statement) \ +do {\ + if (!QTest::qVerify((statement), #statement, "", __FILE__, __LINE__))\ + return KAsync::error(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) + +namespace Sink { + +class MailSyncTest : public QObject +{ + Q_OBJECT + +protected: + QByteArray mResourceInstanceIdentifier; + QByteArrayList mCapabilities; + + virtual void resetTestEnvironment() = 0; + virtual Sink::ApplicationDomain::SinkResource createResource() = 0; + virtual Sink::ApplicationDomain::SinkResource createFaultyResource() = 0; + virtual void removeResourceFromDisk(const QByteArray &mResourceInstanceIdentifier) = 0; + virtual void createFolder(const QStringList &folderPath) = 0; + virtual void removeFolder(const QStringList &folderPath) = 0; + virtual void createMessage(const QStringList &folderPath, const QByteArray &message) = 0; + virtual void removeMessage(const QStringList &folderPath, const QByteArray &message) = 0; + +private slots: + void initTestCase(); + void init(); + void cleanup(); + + void testListFolders(); + void testListFolderHierarchy(); + void testListNewFolders(); + void testListRemovedFolders(); + + void testListMails(); + void testFetchNewMessages(); + void testFetchRemovedMessages(); + + void testFailingSync(); +}; + +} + -- cgit v1.2.3