File indexing completed on 2024-05-12 05:26:23

0001 /*
0002  * Copyright (C) 2016 Christian Mollekopf <mollekopf@kolabsys.com>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) version 3, or any
0008  * later version accepted by the membership of KDE e.V. (or its
0009  * successor approved by the membership of KDE e.V.), which shall
0010  * act as a proxy defined in Section 6 of version 3 of the license.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0019  */
0020 #include <QTest>
0021 
0022 #include <QString>
0023 
0024 #include "dummyresource/resourcefactory.h"
0025 #include "store.h"
0026 #include "resourceconfig.h"
0027 #include "resourcecontrol.h"
0028 #include "log.h"
0029 #include "test.h"
0030 #include "test.h"
0031 #include <KMime/Message>
0032 
0033 using namespace Sink;
0034 using namespace Sink::ApplicationDomain;
0035 
0036 /**
0037  * Test of complete system using the dummy resource.
0038  *
0039  * This test requires the dummy resource installed.
0040  */
0041 class InterResourceMoveTest : public QObject
0042 {
0043     Q_OBJECT
0044 
0045     QByteArray message(const QByteArray &uid, const QString &subject)
0046     {
0047         KMime::Message m;
0048         m.subject(true)->fromUnicodeString(subject, "utf8");
0049         m.messageID(true)->setIdentifier(uid);
0050         m.assemble();
0051         return m.encodedContent(true);
0052     }
0053 
0054 private slots:
0055     void initTestCase()
0056     {
0057         Sink::Test::initTest();
0058         auto factory = Sink::ResourceFactory::load("sink.dummy");
0059         QVERIFY(factory);
0060         ::DummyResource::removeFromDisk("instance1");
0061         ::DummyResource::removeFromDisk("instance2");
0062         ResourceConfig::addResource("instance1", "sink.dummy");
0063         ResourceConfig::addResource("instance2", "sink.dummy");
0064     }
0065 
0066     void init()
0067     {
0068     }
0069 
0070     void cleanup()
0071     {
0072         VERIFYEXEC(Sink::Store::removeDataFromDisk(QByteArray("instance1")));
0073         VERIFYEXEC(Sink::Store::removeDataFromDisk(QByteArray("instance2")));
0074     }
0075 
0076     void testMove()
0077     {
0078         QByteArray testuid = "testuid@test.test";
0079         QString subject = "summaryValue";
0080         auto mimeMessage = message(testuid, subject);
0081 
0082         Mail mail("instance1");
0083         mail.setMimeMessage(mimeMessage);
0084         VERIFYEXEC(Sink::Store::create<Mail>(mail));
0085 
0086         Mail createdmail;
0087         // Ensure all local data is processed
0088         VERIFYEXEC(Sink::ResourceControl::flushMessageQueue("instance1"));
0089         {
0090             auto query = Query().resourceFilter("instance1") ;
0091             auto list = Sink::Store::read<Mail>(query.filter<Mail::MessageId>(testuid));
0092             QCOMPARE(list.size(), 1);
0093             createdmail = list.first();
0094         }
0095 
0096         VERIFYEXEC(Sink::Store::move<Mail>(createdmail, "instance2"));
0097 
0098         //FIXME we can't guarantee that that the create command arrives at instance2 before the flush command, so we'll just wait for a little bit.
0099         QTest::qWait(1000);
0100         //Ensure the move has been processed
0101         VERIFYEXEC(Sink::ResourceControl::flushMessageQueue("instance1"));
0102         //Ensure the create in the target resource has been processed
0103         VERIFYEXEC(Sink::ResourceControl::flushMessageQueue("instance2"));
0104         {
0105             auto query = Query().resourceFilter("instance2") ;
0106             auto list = Sink::Store::read<Mail>(query.filter<Mail::MessageId>(testuid));
0107             QCOMPARE(list.size(), 1);
0108             const auto mail = list.first();
0109             QCOMPARE(mail.getSubject(), subject);
0110             QCOMPARE(mail.getMimeMessage(), mimeMessage);
0111         }
0112 
0113         VERIFYEXEC(Sink::ResourceControl::flushMessageQueue("instance1"));
0114         {
0115             auto query = Query().resourceFilter("instance1") ;
0116             auto list = Sink::Store::read<Mail>(query.filter<Mail::MessageId>(testuid));
0117             QCOMPARE(list.size(), 0);
0118         }
0119     }
0120 
0121     void testCopy()
0122     {
0123         QByteArray testuid = "testuid@test.test";
0124         QString subject = "summaryValue";
0125         auto mimeMessage = message(testuid, subject);
0126 
0127         Mail mail("instance1");
0128         mail.setMimeMessage(mimeMessage);
0129         VERIFYEXEC(Sink::Store::create<Mail>(mail));
0130 
0131 
0132         Mail createdMail;
0133         // Ensure all local data is processed
0134         VERIFYEXEC(Sink::ResourceControl::flushMessageQueue(QByteArrayList() << "instance1"));
0135         {
0136             auto query = Query().resourceFilter("instance1") ;
0137             auto list = Sink::Store::read<Mail>(query.filter<Mail::MessageId>(testuid));
0138             QCOMPARE(list.size(), 1);
0139             createdMail = list.first();
0140         }
0141 
0142         VERIFYEXEC(Sink::Store::copy<Mail>(createdMail, "instance2"));
0143 
0144         //FIXME we can't guarantee that that the create command arrives at instance2 before the flush command, so we'll just wait for a little bit.
0145         QTest::qWait(100);
0146         //Ensure the copy has been processed
0147         VERIFYEXEC(Sink::ResourceControl::flushMessageQueue(QByteArrayList() << "instance1"));
0148         //Ensure the create in the target resource has been processed
0149         VERIFYEXEC(Sink::ResourceControl::flushMessageQueue(QByteArrayList() << "instance2"));
0150         {
0151             auto query = Query().resourceFilter("instance2") ;
0152             auto list = Sink::Store::read<Mail>(query.filter<Mail::MessageId>(testuid));
0153             QCOMPARE(list.size(), 1);
0154             const auto mail = list.first();
0155             QCOMPARE(mail.getSubject(), subject);
0156             QCOMPARE(mail.getMimeMessage(), mimeMessage);
0157         }
0158 
0159         VERIFYEXEC(Sink::ResourceControl::flushMessageQueue(QByteArrayList() << "instance1"));
0160         {
0161             auto query = Query().resourceFilter("instance1") ;
0162             auto list = Sink::Store::read<Mail>(query.filter<Mail::MessageId>(testuid));
0163             QCOMPARE(list.size(), 1);
0164             const auto mail = list.first();
0165             QCOMPARE(mail.getSubject(), subject);
0166             QCOMPARE(mail.getMimeMessage(), mimeMessage);
0167         }
0168     }
0169 
0170 };
0171 
0172 QTEST_MAIN(InterResourceMoveTest)
0173 #include "interresourcemovetest.moc"