File indexing completed on 2025-01-05 04:59:51

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Kevin Ottens <ervin@kde.org>
0003  SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 
0006 
0007 #include <testlib/qtest_zanshin.h>
0008 
0009 #include "utils/mockobject.h"
0010 
0011 #include "testlib/akonadifakejobs.h"
0012 #include "testlib/akonadifakemonitor.h"
0013 
0014 #include "akonadi/akonadidatasourcerepository.h"
0015 #include "akonadi/akonadiserializerinterface.h"
0016 #include "akonadi/akonadistorageinterface.h"
0017 
0018 using namespace mockitopp;
0019 
0020 Q_DECLARE_METATYPE(Testlib::AkonadiFakeItemFetchJob*)
0021 
0022 class AkonadiDataSourceRepositoryTest : public QObject
0023 {
0024     Q_OBJECT
0025 private slots:
0026     void shouldUpdateExistingSources()
0027     {
0028         // GIVEN
0029 
0030         // A source and its corresponding collection already existing in storage
0031         Akonadi::Collection collection(42);
0032         auto source = Domain::DataSource::Ptr::create();
0033 
0034         // A mock modify job
0035         auto collectionModifyJob = new FakeJob(this);
0036 
0037         Utils::MockObject<Akonadi::StorageInterface> storageMock;
0038         Utils::MockObject<Akonadi::SerializerInterface> serializerMock;
0039         QScopedPointer<Akonadi::DataSourceRepository> repository(new Akonadi::DataSourceRepository(storageMock.getInstance(),
0040                                                                                                    serializerMock.getInstance()));
0041 
0042         // Storage mock returning the create job
0043         storageMock(&Akonadi::StorageInterface::updateCollection).when(collection, repository.get())
0044                                                                  .thenReturn(collectionModifyJob);
0045 
0046         // Serializer mock returning the item for the project
0047         serializerMock(&Akonadi::SerializerInterface::createCollectionFromDataSource).when(source).thenReturn(collection);
0048 
0049         // WHEN
0050         repository->update(source)->exec();
0051 
0052         // THEN
0053         QVERIFY(serializerMock(&Akonadi::SerializerInterface::createCollectionFromDataSource).when(source).exactly(1));
0054         QVERIFY(storageMock(&Akonadi::StorageInterface::updateCollection).when(collection, repository.get()).exactly(1));
0055     }
0056 };
0057 
0058 ZANSHIN_TEST_MAIN(AkonadiDataSourceRepositoryTest)
0059 
0060 #include "akonadidatasourcerepositorytest.moc"