From 9d079a832ec9c70fccb3446843bd8d7579e3aafd Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Tue, 3 May 2016 14:38:26 +0200 Subject: An in memory testaccount that can be used for application testing. --- common/test.cpp | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ common/test.h | 26 ++++++++++++ 2 files changed, 150 insertions(+) (limited to 'common') diff --git a/common/test.cpp b/common/test.cpp index 0864f1a..c4dac89 100644 --- a/common/test.cpp +++ b/common/test.cpp @@ -23,6 +23,12 @@ #include #include #include +#include "facade.h" +#include "facadefactory.h" +#include "query.h" +#include "resourceconfig.h" + +using namespace Sink; void Sink::Test::initTest() { @@ -40,3 +46,121 @@ void Sink::Test::initTest() // 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); + 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); diff --git a/common/test.h b/common/test.h index 146b4ac..e2bd1ea 100644 --- a/common/test.h +++ b/common/test.h @@ -21,6 +21,9 @@ #pragma once #include "sink_export.h" +#include "applicationdomaintype.h" + +#include namespace Sink { namespace Test { @@ -31,5 +34,28 @@ namespace Test { * and clears all data directories. */ void SINK_EXPORT initTest(); + +class SINK_EXPORT TestAccount { +public: + QByteArray identifier; + static TestAccount registerAccount(); + + template + void addEntity(const ApplicationDomain::ApplicationDomainType::Ptr &domainObject); + + template + typename DomainType::Ptr createEntity(); + + template + QList entities() const; + +private: + TestAccount(){}; + TestAccount(const TestAccount &); + TestAccount &operator=(const TestAccount &); + QHash > mEntities; + QHash > mFacades; +}; + } } -- cgit v1.2.3