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

0001 /*
0002     SPDX-FileCopyrightText: 2014 Christian Mollekopf <mollekopf@kolabsys.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "tagsync.h"
0008 #include "agentinstance.h"
0009 #include "agentmanager.h"
0010 #include "control.h"
0011 #include "itemcreatejob.h"
0012 #include "itemfetchjob.h"
0013 #include "itemfetchscope.h"
0014 #include "resourceselectjob_p.h"
0015 #include "tag.h"
0016 #include "tagcreatejob.h"
0017 #include "tagdeletejob.h"
0018 #include "tagfetchjob.h"
0019 #include "tagfetchscope.h"
0020 
0021 #include "qtest_akonadi.h"
0022 
0023 #include <QObject>
0024 
0025 using namespace Akonadi;
0026 
0027 class TagSyncTest : public QObject
0028 {
0029     Q_OBJECT
0030 private Q_SLOTS:
0031     void initTestCase()
0032     {
0033         AkonadiTest::checkTestIsIsolated();
0034         Control::start();
0035         AkonadiTest::setAllResourcesOffline();
0036         cleanTags();
0037     }
0038 
0039     Tag::List getTags()
0040     {
0041         auto fetchJob = new TagFetchJob();
0042         fetchJob->fetchScope().setFetchRemoteId(true);
0043         bool ret = fetchJob->exec();
0044         Q_ASSERT(ret);
0045         return fetchJob->tags();
0046     }
0047 
0048     Tag::List getTagsWithRid()
0049     {
0050         Tag::List tags;
0051         const auto allTags = getTags();
0052         for (const Tag &t : allTags) {
0053             if (!t.remoteId().isEmpty()) {
0054                 tags << t;
0055                 qDebug() << t.remoteId();
0056             }
0057         }
0058         return tags;
0059     }
0060 
0061     void cleanTags()
0062     {
0063         const auto tags = getTags();
0064         for (const Tag &t : tags) {
0065             auto job = new TagDeleteJob(t);
0066             bool ret = job->exec();
0067             Q_ASSERT(ret);
0068         }
0069     }
0070 
0071     void newTag()
0072     {
0073         auto select = new ResourceSelectJob(QStringLiteral("akonadi_knut_resource_0"));
0074         AKVERIFYEXEC(select);
0075 
0076         Tag::List remoteTags;
0077 
0078         Tag tag1(QStringLiteral("tag1"));
0079         tag1.setRemoteId("rid1");
0080         remoteTags << tag1;
0081 
0082         auto syncer = new TagSync(this);
0083         syncer->setFullTagList(remoteTags);
0084         syncer->setTagMembers(QHash<QString, Item::List>());
0085         AKVERIFYEXEC(syncer);
0086 
0087         Tag::List resultTags = getTags();
0088         QCOMPARE(resultTags.count(), remoteTags.count());
0089         QCOMPARE(resultTags, remoteTags);
0090         cleanTags();
0091     }
0092 
0093     void newTagWithItems()
0094     {
0095         const Collection res3 = Collection(AkonadiTest::collectionIdFromPath(QStringLiteral("res3")));
0096         auto select = new ResourceSelectJob(QStringLiteral("akonadi_knut_resource_2"));
0097         AKVERIFYEXEC(select);
0098 
0099         Tag::List remoteTags;
0100 
0101         Tag tag1(QStringLiteral("tag1"));
0102         tag1.setRemoteId("rid1");
0103         remoteTags << tag1;
0104 
0105         Item item1;
0106         {
0107             item1.setMimeType(QStringLiteral("application/octet-stream"));
0108             item1.setRemoteId(QStringLiteral("item1"));
0109             auto append = new ItemCreateJob(item1, res3, this);
0110             AKVERIFYEXEC(append);
0111             item1 = append->item();
0112         }
0113 
0114         QHash<QString, Item::List> tagMembers;
0115         tagMembers.insert(QString::fromLatin1(tag1.remoteId()), {item1});
0116 
0117         auto syncer = new TagSync(this);
0118         syncer->setFullTagList(remoteTags);
0119         syncer->setTagMembers(tagMembers);
0120         AKVERIFYEXEC(syncer);
0121 
0122         Tag::List resultTags = getTags();
0123         QCOMPARE(resultTags.count(), remoteTags.count());
0124         QCOMPARE(resultTags, remoteTags);
0125 
0126         // We need the id of the fetch
0127         tag1 = resultTags.first();
0128 
0129         auto fetchJob = new ItemFetchJob(tag1);
0130         fetchJob->fetchScope().setFetchTags(true);
0131         fetchJob->fetchScope().tagFetchScope().setFetchRemoteId(true);
0132         AKVERIFYEXEC(fetchJob);
0133         QCOMPARE(fetchJob->items().count(), tagMembers.value(QString::fromLatin1(tag1.remoteId())).count());
0134         QCOMPARE(fetchJob->items(), tagMembers.value(QString::fromLatin1(tag1.remoteId())));
0135 
0136         cleanTags();
0137     }
0138 
0139     void existingTag()
0140     {
0141         auto select = new ResourceSelectJob(QStringLiteral("akonadi_knut_resource_0"));
0142         AKVERIFYEXEC(select);
0143 
0144         Tag tag1(QStringLiteral("tag1"));
0145         tag1.setRemoteId("rid1");
0146 
0147         auto createJob = new TagCreateJob(tag1, this);
0148         AKVERIFYEXEC(createJob);
0149 
0150         Tag::List remoteTags;
0151         remoteTags << tag1;
0152 
0153         auto syncer = new TagSync(this);
0154         syncer->setFullTagList(remoteTags);
0155         syncer->setTagMembers(QHash<QString, Item::List>());
0156         AKVERIFYEXEC(syncer);
0157 
0158         Tag::List resultTags = getTags();
0159         QCOMPARE(resultTags.count(), remoteTags.count());
0160         QCOMPARE(resultTags, remoteTags);
0161         cleanTags();
0162     }
0163 
0164     void existingTagWithItems()
0165     {
0166         const Collection res3 = Collection(AkonadiTest::collectionIdFromPath(QStringLiteral("res3")));
0167 
0168         auto select = new ResourceSelectJob(QStringLiteral("akonadi_knut_resource_2"));
0169         AKVERIFYEXEC(select);
0170 
0171         Tag tag1(QStringLiteral("tag1"));
0172         tag1.setRemoteId("rid1");
0173 
0174         auto createJob = new TagCreateJob(tag1, this);
0175         AKVERIFYEXEC(createJob);
0176 
0177         Tag::List remoteTags;
0178         remoteTags << tag1;
0179 
0180         Item item1;
0181         {
0182             item1.setMimeType(QStringLiteral("application/octet-stream"));
0183             item1.setRemoteId(QStringLiteral("item1"));
0184             auto append = new ItemCreateJob(item1, res3, this);
0185             AKVERIFYEXEC(append);
0186             item1 = append->item();
0187         }
0188 
0189         Item item2;
0190         {
0191             item2.setMimeType(QStringLiteral("application/octet-stream"));
0192             item2.setRemoteId(QStringLiteral("item2"));
0193             item2.setTag(tag1);
0194             auto append = new ItemCreateJob(item2, res3, this);
0195             AKVERIFYEXEC(append);
0196             item2 = append->item();
0197         }
0198 
0199         QHash<QString, Item::List> tagMembers;
0200         tagMembers.insert(QString::fromLatin1(tag1.remoteId()), Item::List() << item1);
0201 
0202         auto syncer = new TagSync(this);
0203         syncer->setFullTagList(remoteTags);
0204         syncer->setTagMembers(tagMembers);
0205         AKVERIFYEXEC(syncer);
0206 
0207         Tag::List resultTags = getTags();
0208         QCOMPARE(resultTags.count(), remoteTags.count());
0209         QCOMPARE(resultTags, remoteTags);
0210         {
0211             auto fetchJob = new ItemFetchJob(item1, this);
0212             fetchJob->fetchScope().setFetchTags(true);
0213             AKVERIFYEXEC(fetchJob);
0214             QCOMPARE(fetchJob->items().first().tags().count(), 1);
0215         }
0216         {
0217             auto fetchJob = new ItemFetchJob(item2, this);
0218             fetchJob->fetchScope().setFetchTags(true);
0219             AKVERIFYEXEC(fetchJob);
0220             QCOMPARE(fetchJob->items().first().tags().count(), 0);
0221         }
0222 
0223         cleanTags();
0224     }
0225 
0226     void removeTag()
0227     {
0228         auto select = new ResourceSelectJob(QStringLiteral("akonadi_knut_resource_0"));
0229         AKVERIFYEXEC(select);
0230 
0231         Tag tag1(QStringLiteral("tag1"));
0232         tag1.setRemoteId("rid1");
0233 
0234         auto createJob = new TagCreateJob(tag1, this);
0235         AKVERIFYEXEC(createJob);
0236 
0237         Tag::List remoteTags;
0238 
0239         auto syncer = new TagSync(this);
0240         syncer->setFullTagList(remoteTags);
0241         syncer->setTagMembers(QHash<QString, Item::List>());
0242         AKVERIFYEXEC(syncer);
0243 
0244         Tag::List resultTags = getTagsWithRid();
0245         QCOMPARE(resultTags.count(), remoteTags.count());
0246         QCOMPARE(resultTags, remoteTags);
0247         cleanTags();
0248     }
0249 };
0250 
0251 QTEST_AKONADIMAIN(TagSyncTest)
0252 
0253 #include "tagsynctest.moc"