File indexing completed on 2024-11-10 04:40:09

0001 /*
0002     SPDX-FileCopyrightText: 2008 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "agentinstance.h"
0008 #include "agentmanager.h"
0009 #include "collectioncopyjob.h"
0010 #include "collectionfetchjob.h"
0011 #include "control.h"
0012 #include "item.h"
0013 #include "itemfetchjob.h"
0014 #include "itemfetchscope.h"
0015 #include "qtest_akonadi.h"
0016 
0017 #include <QObject>
0018 
0019 using namespace Akonadi;
0020 
0021 class CollectionCopyTest : public QObject
0022 {
0023     Q_OBJECT
0024 private Q_SLOTS:
0025     void initTestCase()
0026     {
0027         AkonadiTest::checkTestIsIsolated();
0028 
0029         Control::start();
0030         // switch target resources offline to reduce interference from them
0031         const Akonadi::AgentInstance::List agents = Akonadi::AgentManager::self()->instances();
0032         for (Akonadi::AgentInstance agent : agents) {
0033             if (agent.identifier() == QLatin1StringView("akonadi_knut_resource_2")) {
0034                 agent.setIsOnline(false);
0035             }
0036         }
0037     }
0038 
0039     void testCopy()
0040     {
0041         const Collection target(AkonadiTest::collectionIdFromPath(QStringLiteral("res3")));
0042         Collection source(AkonadiTest::collectionIdFromPath(QStringLiteral("res1/foo")));
0043         QVERIFY(target.isValid());
0044         QVERIFY(source.isValid());
0045 
0046         // obtain reference listing
0047         auto fetch = new CollectionFetchJob(source, CollectionFetchJob::Base);
0048         AKVERIFYEXEC(fetch);
0049         QCOMPARE(fetch->collections().count(), 1);
0050         source = fetch->collections().first();
0051         QVERIFY(source.isValid());
0052 
0053         fetch = new CollectionFetchJob(source, CollectionFetchJob::Recursive);
0054         AKVERIFYEXEC(fetch);
0055         QMap<Collection, Item::List> referenceData;
0056         Collection::List cols = fetch->collections();
0057         cols << source;
0058         for (const Collection &c : std::as_const(cols)) {
0059             auto job = new ItemFetchJob(c, this);
0060             AKVERIFYEXEC(job);
0061             referenceData.insert(c, job->items());
0062         }
0063 
0064         // actually copy the collection
0065         auto copy = new CollectionCopyJob(source, target);
0066         AKVERIFYEXEC(copy);
0067 
0068         // list destination and check if everything has arrived
0069         auto list = new CollectionFetchJob(target, CollectionFetchJob::Recursive);
0070         AKVERIFYEXEC(list);
0071         cols = list->collections();
0072         QCOMPARE(cols.count(), referenceData.count());
0073         for (QMap<Collection, Item::List>::ConstIterator it = referenceData.constBegin(), end = referenceData.constEnd(); it != end; ++it) {
0074             QVERIFY(!cols.contains(it.key()));
0075             Collection col;
0076             for (const Collection &c : std::as_const(cols)) {
0077                 if (it.key().name() == c.name()) {
0078                     col = c;
0079                 }
0080             }
0081 
0082             QVERIFY(col.isValid());
0083             QCOMPARE(col.resource(), QStringLiteral("akonadi_knut_resource_2"));
0084             QVERIFY(col.remoteId().isEmpty());
0085             auto job = new ItemFetchJob(col, this);
0086             job->fetchScope().fetchFullPayload();
0087             job->fetchScope().setCacheOnly(true);
0088             AKVERIFYEXEC(job);
0089             QCOMPARE(job->items().count(), it.value().count());
0090             const Item::List items = job->items();
0091             for (const Item &item : items) {
0092                 QVERIFY(!it.value().contains(item));
0093                 QVERIFY(item.remoteId().isEmpty());
0094                 QVERIFY(item.hasPayload());
0095             }
0096         }
0097     }
0098 
0099     void testIlleagalCopy()
0100     {
0101         // invalid source
0102         auto copy = new CollectionCopyJob(Collection(), Collection(1));
0103         QVERIFY(!copy->exec());
0104 
0105         // non-existing source
0106         copy = new CollectionCopyJob(Collection(INT_MAX), Collection(1));
0107         QVERIFY(!copy->exec());
0108 
0109         // invalid target
0110         copy = new CollectionCopyJob(Collection(1), Collection());
0111         QVERIFY(!copy->exec());
0112 
0113         // non-existing target
0114         copy = new CollectionCopyJob(Collection(1), Collection(INT_MAX));
0115         QVERIFY(!copy->exec());
0116     }
0117 };
0118 
0119 QTEST_AKONADIMAIN(CollectionCopyTest)
0120 
0121 #include "collectioncopytest.moc"