File indexing completed on 2024-11-10 04:40:12
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 "collection.h" 0010 #include "collectionstatistics.h" 0011 #include "control.h" 0012 #include "itemcopyjob.h" 0013 #include "itemcreatejob.h" 0014 #include "itemfetchjob.h" 0015 #include "itemfetchscope.h" 0016 0017 #include "qtest_akonadi.h" 0018 0019 #include <QObject> 0020 #include <QTemporaryFile> 0021 0022 using namespace Akonadi; 0023 0024 class ItemCopyTest : public QObject 0025 { 0026 Q_OBJECT 0027 private Q_SLOTS: 0028 void initTestCase() 0029 { 0030 AkonadiTest::checkTestIsIsolated(); 0031 Control::start(); 0032 // switch target resources offline to reduce interference from them 0033 const Akonadi::AgentInstance::List agents = Akonadi::AgentManager::self()->instances(); 0034 for (Akonadi::AgentInstance agent : agents) { 0035 if (agent.identifier() == QLatin1StringView("akonadi_knut_resource_2")) { 0036 agent.setIsOnline(false); 0037 } 0038 } 0039 } 0040 0041 void testCopy() 0042 { 0043 const Collection target(AkonadiTest::collectionIdFromPath(QStringLiteral("res3"))); 0044 QVERIFY(target.isValid()); 0045 0046 auto copy = new ItemCopyJob(Item(1), target); 0047 AKVERIFYEXEC(copy); 0048 0049 Item source(1); 0050 auto sourceFetch = new ItemFetchJob(source); 0051 AKVERIFYEXEC(sourceFetch); 0052 source = sourceFetch->items().first(); 0053 0054 auto fetch = new ItemFetchJob(target); 0055 fetch->fetchScope().fetchFullPayload(); 0056 fetch->fetchScope().fetchAllAttributes(); 0057 fetch->fetchScope().setCacheOnly(true); 0058 AKVERIFYEXEC(fetch); 0059 QCOMPARE(fetch->items().count(), 1); 0060 0061 Item item = fetch->items().first(); 0062 QVERIFY(item.hasPayload()); 0063 QVERIFY(source.size() > 0); 0064 QVERIFY(item.size() > 0); 0065 QCOMPARE(item.size(), source.size()); 0066 QCOMPARE(item.attributes().count(), 1); 0067 QVERIFY(item.remoteId().isEmpty()); 0068 QEXPECT_FAIL("", "statistics are not properly updated after copy", Abort); 0069 QCOMPARE(target.statistics().count(), 1LL); 0070 } 0071 0072 void testIlleagalCopy() 0073 { 0074 // empty item list 0075 auto copy = new ItemCopyJob(Item::List(), Collection::root()); 0076 QVERIFY(!copy->exec()); 0077 0078 // non-existing target 0079 copy = new ItemCopyJob(Item(1), Collection(INT_MAX)); 0080 QVERIFY(!copy->exec()); 0081 0082 // non-existing source 0083 copy = new ItemCopyJob(Item(INT_MAX), Collection::root()); 0084 QVERIFY(!copy->exec()); 0085 } 0086 0087 void testCopyForeign() 0088 { 0089 QTemporaryFile file; 0090 QVERIFY(file.open()); 0091 file.write("123456789"); 0092 file.close(); 0093 0094 const Collection source(AkonadiTest::collectionIdFromPath(QStringLiteral("res2"))); 0095 0096 Item item(QStringLiteral("application/octet-stream")); 0097 item.setPayloadPath(file.fileName()); 0098 0099 auto create = new ItemCreateJob(item, source, this); 0100 AKVERIFYEXEC(create); 0101 item = create->item(); 0102 0103 const Collection target(AkonadiTest::collectionIdFromPath(QStringLiteral("res2/space folder"))); 0104 auto copy = new ItemCopyJob(item, target, this); 0105 AKVERIFYEXEC(copy); 0106 0107 auto fetch = new ItemFetchJob(target, this); 0108 fetch->fetchScope().fetchFullPayload(true); 0109 AKVERIFYEXEC(fetch); 0110 QCOMPARE(fetch->items().size(), 1); 0111 auto copiedItem = fetch->items().at(0); 0112 0113 // Copied payload should be completely stored inside Akonadi 0114 QVERIFY(copiedItem.payloadPath().isEmpty()); 0115 QVERIFY(copiedItem.hasPayload<QByteArray>()); 0116 QCOMPARE(copiedItem.payload<QByteArray>(), item.payload<QByteArray>()); 0117 } 0118 }; 0119 0120 QTEST_AKONADIMAIN(ItemCopyTest) 0121 0122 #include "itemcopytest.moc"