File indexing completed on 2025-01-05 04:58:36

0001 /*
0002  *   Copyright (C) 2018 Christian Mollekopf <chrigi_1@fastmail.fm>
0003  *
0004  *   This program is free software; you can redistribute it and/or modify
0005  *   it under the terms of the GNU General Public License as published by
0006  *   the Free Software Foundation; either version 2 of the License, or
0007  *   (at your option) any later version.
0008  *
0009  *   This program is distributed in the hope that it will be useful,
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *   GNU General Public License for more details.
0013  *
0014  *   You should have received a copy of the GNU General Public License
0015  *   along with this program; if not, write to the
0016  *   Free Software Foundation, Inc.,
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
0018  */
0019 #include <QTest>
0020 #include <QTcpSocket>
0021 
0022 #include <KDAV2/DavCollectionsFetchJob>
0023 #include <KDAV2/DavCollectionCreateJob>
0024 #include <KDAV2/DavCollectionDeleteJob>
0025 #include <KDAV2/DavItemFetchJob>
0026 #include <KDAV2/DavItemModifyJob>
0027 #include <KDAV2/DavItemCreateJob>
0028 #include <KDAV2/DavItemsListJob>
0029 
0030 #include <KCalendarCore/Event>
0031 #include <KCalendarCore/ICalFormat>
0032 
0033 #include "../caldavresource.h"
0034 
0035 #include "common/test.h"
0036 #include "common/domain/applicationdomaintype.h"
0037 #include "common/store.h"
0038 #include "common/resourcecontrol.h"
0039 #include "common/secretstore.h"
0040 
0041 #include <tests/hawd/dataset.h>
0042 #include <tests/hawd/formatter.h>
0043 
0044 using namespace Sink;
0045 using namespace Sink::ApplicationDomain;
0046 
0047 /**
0048  * Test of complete system using the caldav resource.
0049  *
0050  * This test requires the caldav resource installed.
0051  */
0052 class CalDavSyncBenchmark : public QObject
0053 {
0054     Q_OBJECT
0055 
0056     void resetTestEnvironment()
0057     {
0058         system("populatecalendar.sh");
0059     }
0060 
0061     const QString baseUrl = "http://localhost/dav/calendars/user/doe";
0062     const QString username = "doe";
0063     const QString password = "doe";
0064 
0065     Sink::ApplicationDomain::SinkResource createResource()
0066     {
0067         auto resource = ApplicationDomain::CalDavResource::create("account1");
0068         resource.setProperty("server", baseUrl);
0069         resource.setProperty("username", username);
0070         Sink::SecretStore::instance().insert(resource.identifier(), password);
0071         return resource;
0072     }
0073 
0074     void removeResourceFromDisk(const QByteArray &identifier)
0075     {
0076         ::CalDavResource::removeFromDisk(identifier);
0077     }
0078 
0079     void createEvents(const QString &subject, const QString &collectionName, int num)
0080     {
0081         QUrl mainUrl{baseUrl};
0082         mainUrl.setUserName(username);
0083         mainUrl.setPassword(password);
0084 
0085         KDAV2::DavUrl davUrl(mainUrl, KDAV2::CalDav);
0086 
0087         auto *job = new KDAV2::DavCollectionsFetchJob(davUrl);
0088         job->exec();
0089 
0090         const auto collectionUrl = [&] {
0091             for (const auto &col : job->collections()) {
0092                 qWarning() << "Looking for " << collectionName << col.displayName();
0093                 if (col.displayName() == collectionName) {
0094                     return col.url().url();
0095                 }
0096             }
0097             return QUrl{};
0098         }();
0099         Q_ASSERT(!collectionUrl.isEmpty());
0100 
0101 
0102         for (int i = 0; i < num; i++) {
0103             QUrl url{collectionUrl.toString() + subject + QString::number(i) + ".ical"};
0104             url.setUserInfo(mainUrl.userInfo());
0105 
0106             KDAV2::DavUrl testItemUrl(url, KDAV2::CardDav);
0107             auto event = QSharedPointer<KCalendarCore::Event>::create();
0108             event->setSummary(subject);
0109             event->setDtStart(QDateTime::currentDateTime());
0110             event->setDtEnd(QDateTime::currentDateTime().addSecs(3600));
0111             event->setCreated(QDateTime::currentDateTime());
0112             event->setUid(subject + QString::number(i));
0113             KDAV2::DavItem item(testItemUrl, QStringLiteral("text/calendar"), KCalendarCore::ICalFormat().toICalString(event).toUtf8(), QString());
0114             auto createJob = new KDAV2::DavItemCreateJob(item);
0115             createJob->exec();
0116             if (createJob->error()) {
0117                 qWarning() << createJob->errorString();
0118             }
0119             Q_ASSERT(!createJob->error());
0120         }
0121     }
0122 
0123     QByteArray mResourceInstanceIdentifier;
0124     HAWD::State mHawdState;
0125 
0126 private slots:
0127 
0128     void initTestCase()
0129     {
0130         Test::initTest();
0131         resetTestEnvironment();
0132         auto resource = createResource();
0133         QVERIFY(!resource.identifier().isEmpty());
0134 
0135         VERIFYEXEC(Store::create(resource));
0136 
0137         mResourceInstanceIdentifier = resource.identifier();
0138     }
0139 
0140     void cleanup()
0141     {
0142         VERIFYEXEC(ResourceControl::shutdown(mResourceInstanceIdentifier));
0143         removeResourceFromDisk(mResourceInstanceIdentifier);
0144     }
0145 
0146     void init()
0147     {
0148         createEvents("test", "personal", 100);
0149         VERIFYEXEC(ResourceControl::start(mResourceInstanceIdentifier));
0150     }
0151 
0152     void testSync()
0153     {
0154         Sink::Query query;
0155         query.resourceFilter(mResourceInstanceIdentifier);
0156 
0157         QTime time;
0158         time.start();
0159 
0160         // Ensure all local data is processed
0161         VERIFYEXEC(Store::synchronize(query));
0162         auto sync = time.elapsed();
0163         SinkLog() << "Sync took: " << Sink::Log::TraceTime(sync);
0164 
0165         VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mResourceInstanceIdentifier));
0166         auto total = time.elapsed();
0167         SinkLog() << "Total took: " << Sink::Log::TraceTime(total);
0168 
0169         time.start();
0170 
0171         VERIFYEXEC(Store::synchronize(query));
0172         auto resync = time.elapsed();
0173         SinkLog() << "ReSync took: " << Sink::Log::TraceTime(resync);
0174 
0175         VERIFYEXEC(ResourceControl::flushMessageQueue(QByteArrayList() << mResourceInstanceIdentifier));
0176         auto resynctotal = time.elapsed();
0177         SinkLog() << "Total resync took: " << Sink::Log::TraceTime(resynctotal);
0178 
0179         // HAWD::Dataset dataset("caldav_sync", mHawdState);
0180         // HAWD::Dataset::Row row = dataset.row();
0181         // row.setValue("sync", sync);
0182         // row.setValue("total", total);
0183         // row.setValue("resync", resync);
0184         // row.setValue("resynctotal", resynctotal);
0185         // dataset.insertRow(row);
0186         // HAWD::Formatter::print(dataset);
0187     }
0188 };
0189 
0190 QTEST_MAIN(CalDavSyncBenchmark)
0191 
0192 #include "caldavsyncbenchmark.moc"