/* * Copyright (C) 2014 Christian Mollekopf * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . */ #include "test.h" #include #include #include #include "facade.h" #include "facadefactory.h" #include "query.h" #include "resourceconfig.h" using namespace Sink; void Sink::Test::initTest() { QStandardPaths::setTestModeEnabled(true); // qDebug() << "Removing " << QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); QDir(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)).removeRecursively(); // qDebug() << "Removing " << QStandardPaths::writableLocation(QStandardPaths::DataLocation); QDir(QStandardPaths::writableLocation(QStandardPaths::DataLocation)).removeRecursively(); // qDebug() << "Removing " << QStandardPaths::writableLocation(QStandardPaths::ConfigLocation); QDir(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation)).removeRecursively(); // qDebug() << "Removing " << QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation); QDir(QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation)).removeRecursively(); // qDebug() << "Removing " << QStandardPaths::writableLocation(QStandardPaths::CacheLocation); QDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)).removeRecursively(); // qDebug() << "Removing " << QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation); QDir(QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation)).removeRecursively(); } template class TestFacade : public Sink::StoreFacade { public: static std::shared_ptr> registerFacade(Test::TestAccount *testAccount, const QByteArray &instanceIdentifier = QByteArray()) { static QMap>> map; auto facade = std::make_shared>(); facade->mTestAccount = testAccount; map.insert(instanceIdentifier, facade); bool alwaysReturnFacade = instanceIdentifier.isEmpty(); Sink::FacadeFactory::instance().registerFacade>("testresource", [alwaysReturnFacade](const QByteArray &instanceIdentifier) { if (alwaysReturnFacade) { return map.value(QByteArray()); } return map.value(instanceIdentifier); }); return facade; } ~TestFacade(){}; KAsync::Job create(const T &domainObject) Q_DECL_OVERRIDE { mTestAccount->addEntity(T::Ptr::create(domainObject)); return KAsync::null(); }; KAsync::Job modify(const T &domainObject) Q_DECL_OVERRIDE { // mTestAccount->modifyEntity(domainObject); return KAsync::null(); }; KAsync::Job remove(const T &domainObject) Q_DECL_OVERRIDE { //FIXME // mTestAccount->removeEntity(domainObject); return KAsync::null(); }; QPair, typename Sink::ResultEmitter::Ptr> load(const Sink::Query &query) Q_DECL_OVERRIDE { auto resultProvider = new Sink::ResultProvider(); resultProvider->onDone([resultProvider]() { Trace() << "Result provider is done"; delete resultProvider; }); // We have to do it this way, otherwise we're not setting the fetcher right auto emitter = resultProvider->emitter(); resultProvider->setFetcher([query, resultProvider, this](const typename T::Ptr &parent) { if (parent) { Trace() << "Running the fetcher " << parent->identifier(); } else { Trace() << "Running the fetcher."; } Trace() << "-------------------------."; for (const auto &res : mTestAccount->entities()) { qDebug() << "Parent filter " << query.propertyFilter.value("parent").value.toByteArray() << res->identifier() << res->getProperty("parent").toByteArray(); auto parentProperty = res->getProperty("parent").toByteArray(); if ((!parent && parentProperty.isEmpty()) || (parent && parentProperty == parent->identifier()) || query.parentProperty.isEmpty()) { qDebug() << "Found a match" << res->identifier(); resultProvider->add(res.template staticCast()); } } resultProvider->initialResultSetComplete(parent); }); auto job = KAsync::start([query, resultProvider]() {}); return qMakePair(job, emitter); } Test::TestAccount *mTestAccount; }; Test::TestAccount Sink::Test::TestAccount::registerAccount() { Test::TestAccount account; account.mFacades.insert(ApplicationDomain::getTypeName(), TestFacade::registerFacade(&account)); account.mFacades.insert(ApplicationDomain::getTypeName(), TestFacade::registerFacade(&account)); account.identifier = "testresource.instance1"; ResourceConfig::addResource(account.identifier, "testresource"); QMap configuration; configuration.insert("account", account.identifier); configuration.insert("capabilities", QVariant::fromValue(QByteArrayList() << "drafts" << "storage" << "transport")); ResourceConfig::configureResource(account.identifier, configuration); return account; } template void Sink::Test::TestAccount::addEntity(const Sink::ApplicationDomain::ApplicationDomainType::Ptr &domainObject) { mEntities[ApplicationDomain::getTypeName()].append(domainObject); } template typename DomainType::Ptr Sink::Test::TestAccount::createEntity() { auto entity = DomainType::Ptr::create(ApplicationDomain::ApplicationDomainType::createEntity(identifier)); addEntity(entity); return entity; } template QList Sink::Test::TestAccount::entities() const { return mEntities.value(ApplicationDomain::getTypeName()); } #define REGISTER_TYPE(T) \ template QList Sink::Test::TestAccount::entities() const; \ template void Sink::Test::TestAccount::addEntity(const ApplicationDomain::ApplicationDomainType::Ptr &); \ template typename T::Ptr Sink::Test::TestAccount::createEntity(); REGISTER_TYPE(ApplicationDomain::Event); REGISTER_TYPE(ApplicationDomain::Mail); REGISTER_TYPE(ApplicationDomain::Folder); REGISTER_TYPE(ApplicationDomain::SinkResource); REGISTER_TYPE(ApplicationDomain::SinkAccount); REGISTER_TYPE(ApplicationDomain::Identity);