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

0001 /*
0002     SPDX-FileCopyrightText: 2014 Christian Mollekopf <mollekopf@kolabsys.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 #include <QObject>
0007 
0008 #include "storage/entity.h"
0009 
0010 #include "aktest.h"
0011 #include "dbinitializer.h"
0012 #include "entities.h"
0013 #include "fakeakonadiserver.h"
0014 
0015 #include "private/scope_p.h"
0016 
0017 #include <QTest>
0018 
0019 using namespace Akonadi;
0020 using namespace Akonadi::Server;
0021 
0022 class CollectionCreateHandlerTest : public QObject
0023 {
0024     Q_OBJECT
0025 
0026     FakeAkonadiServer mAkonadi;
0027 
0028 public:
0029     CollectionCreateHandlerTest()
0030     {
0031         mAkonadi.init();
0032     }
0033 
0034 private Q_SLOTS:
0035     void testCreate_data()
0036     {
0037         DbInitializer dbInitializer;
0038 
0039         QTest::addColumn<TestScenario::List>("scenarios");
0040         QTest::addColumn<Protocol::CollectionChangeNotificationPtr>("notification");
0041 
0042         auto notificationTemplate = Protocol::CollectionChangeNotificationPtr::create();
0043         notificationTemplate->setOperation(Protocol::CollectionChangeNotification::Add);
0044         notificationTemplate->setParentCollection(3);
0045         notificationTemplate->setResource("akonadi_fake_resource_0");
0046         notificationTemplate->setSessionId(FakeAkonadiServer::instanceName().toLatin1());
0047 
0048         {
0049             auto cmd = Protocol::CreateCollectionCommandPtr::create();
0050             cmd->setName(QStringLiteral("New Name"));
0051             cmd->setParent(Scope(3));
0052             cmd->setAttributes({{"MYRANDOMATTRIBUTE", ""}});
0053 
0054             auto resp = Protocol::FetchCollectionsResponsePtr::create(8);
0055             resp->setName(QStringLiteral("New Name"));
0056             resp->setParentId(3);
0057             resp->setAttributes({{"MYRANDOMATTRIBUTE", ""}});
0058             resp->setResource(QStringLiteral("akonadi_fake_resource_0"));
0059             resp->cachePolicy().setLocalParts({QLatin1StringView("ALL")});
0060             resp->setMimeTypes({QLatin1StringView("application/octet-stream"), QLatin1StringView("inode/directory")});
0061 
0062             TestScenario::List scenarios;
0063             scenarios << FakeAkonadiServer::loginScenario() << TestScenario::create(5, TestScenario::ClientCmd, cmd)
0064                       << TestScenario::create(5, TestScenario::ServerCmd, resp)
0065                       << TestScenario::create(5, TestScenario::ServerCmd, Protocol::CreateCollectionResponsePtr::create());
0066 
0067             Protocol::FetchCollectionsResponse collection(*resp);
0068             auto notification = Protocol::CollectionChangeNotificationPtr::create(*notificationTemplate);
0069             notification->setCollection(std::move(collection));
0070 
0071             QTest::newRow("create collection") << scenarios << notification;
0072         }
0073         {
0074             auto cmd = Protocol::CreateCollectionCommandPtr::create();
0075             cmd->setName(QStringLiteral("Name 2"));
0076             cmd->setParent(Scope(3));
0077             cmd->setEnabled(false);
0078             cmd->setDisplayPref(Tristate::True);
0079             cmd->setSyncPref(Tristate::True);
0080             cmd->setIndexPref(Tristate::True);
0081 
0082             auto resp = Protocol::FetchCollectionsResponsePtr::create(9);
0083             resp->setName(QStringLiteral("Name 2"));
0084             resp->setParentId(3);
0085             resp->setEnabled(false);
0086             resp->setDisplayPref(Tristate::True);
0087             resp->setSyncPref(Tristate::True);
0088             resp->setIndexPref(Tristate::True);
0089             resp->setResource(QStringLiteral("akonadi_fake_resource_0"));
0090             resp->cachePolicy().setLocalParts({QLatin1StringView("ALL")});
0091             resp->setMimeTypes({QLatin1StringView("application/octet-stream"), QLatin1StringView("inode/directory")});
0092 
0093             TestScenario::List scenarios;
0094             scenarios << FakeAkonadiServer::loginScenario() << TestScenario::create(5, TestScenario::ClientCmd, cmd)
0095                       << TestScenario::create(5, TestScenario::ServerCmd, resp)
0096                       << TestScenario::create(5, TestScenario::ServerCmd, Protocol::CreateCollectionResponsePtr::create());
0097 
0098             Protocol::FetchCollectionsResponse collection(*resp);
0099             auto notification = Protocol::CollectionChangeNotificationPtr::create(*notificationTemplate);
0100             notification->setCollection(std::move(collection));
0101 
0102             QTest::newRow("create collection with local override") << scenarios << notification;
0103         }
0104 
0105         {
0106             auto cmd = Protocol::CreateCollectionCommandPtr::create();
0107             cmd->setName(QStringLiteral("TopLevel"));
0108             cmd->setParent(Scope(0));
0109             cmd->setMimeTypes({QLatin1StringView("inode/directory")});
0110 
0111             auto resp = Protocol::FetchCollectionsResponsePtr::create(10);
0112             resp->setName(QStringLiteral("TopLevel"));
0113             resp->setParentId(0);
0114             resp->setEnabled(true);
0115             resp->setMimeTypes({QLatin1StringView("inode/directory")});
0116             resp->cachePolicy().setLocalParts({QLatin1StringView("ALL")});
0117             resp->setResource(QStringLiteral("akonadi_fake_resource_0"));
0118 
0119             TestScenario::List scenarios;
0120             scenarios << FakeAkonadiServer::loginScenario("akonadi_fake_resource_0") << TestScenario::create(5, TestScenario::ClientCmd, cmd)
0121                       << TestScenario::create(5, TestScenario::ServerCmd, resp)
0122                       << TestScenario::create(5, TestScenario::ServerCmd, Protocol::CreateCollectionResponsePtr::create());
0123 
0124             Protocol::FetchCollectionsResponse collection(*resp);
0125             auto notification = Protocol::CollectionChangeNotificationPtr::create(*notificationTemplate);
0126             notification->setSessionId("akonadi_fake_resource_0");
0127             notification->setParentCollection(0);
0128             notification->setCollection(std::move(collection));
0129 
0130             QTest::newRow("create top-level collection") << scenarios << notification;
0131         }
0132     }
0133 
0134     void testCreate()
0135     {
0136         QFETCH(TestScenario::List, scenarios);
0137         QFETCH(Protocol::CollectionChangeNotificationPtr, notification);
0138 
0139         mAkonadi.setScenarios(scenarios);
0140         mAkonadi.runTest();
0141 
0142         auto notificationSpy = mAkonadi.notificationSpy();
0143         if (notification->operation() != Protocol::CollectionChangeNotification::InvalidOp) {
0144             QTRY_COMPARE(notificationSpy->count(), 1);
0145             const auto notifications = notificationSpy->takeFirst().first().value<Protocol::ChangeNotificationList>();
0146             QCOMPARE(notifications.count(), 1);
0147             const auto actualNtf = notifications.first().staticCast<Protocol::CollectionChangeNotification>();
0148             if (*actualNtf != *notification) {
0149                 qDebug() << "Actual:  " << Protocol::debugString(actualNtf);
0150                 qDebug() << "Expected:" << Protocol::debugString(notification);
0151             }
0152             QCOMPARE(*notifications.first().staticCast<Protocol::CollectionChangeNotification>(), *notification);
0153         } else {
0154             QVERIFY(notificationSpy->isEmpty() || notificationSpy->takeFirst().first().value<Protocol::ChangeNotificationList>().isEmpty());
0155         }
0156     }
0157 };
0158 
0159 AKTEST_FAKESERVER_MAIN(CollectionCreateHandlerTest)
0160 
0161 #include "collectioncreatehandlertest.moc"