1 /*
2  * SPDX-FileCopyrightText: 2018 Daniel Vrátil <dvratil@kde.org>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5  */
6 
7 #include <QObject>
8 #include <QTest>
9 
10 #include "fakenetworkaccessmanagerfactory.h"
11 #include "testutils.h"
12 #include "contactstestutils.h"
13 
14 #include "types.h"
15 #include "contactsgroupfetchjob.h"
16 #include "contactsgroup.h"
17 #include "account.h"
18 
19 using namespace KGAPI2;
20 
21 Q_DECLARE_METATYPE(QList<FakeNetworkAccessManager::Scenario>)
22 Q_DECLARE_METATYPE(KGAPI2::ContactsGroupPtr)
23 
24 class ContactsGroupFetchJobTest : public QObject
25 {
26     Q_OBJECT
27 private Q_SLOTS:
initTestCase()28     void initTestCase()
29     {
30         NetworkAccessManagerFactory::setFactory(new FakeNetworkAccessManagerFactory);
31     }
32 
testFetchAll()33     void testFetchAll()
34     {
35         FakeNetworkAccessManagerFactory::get()->setScenarios({
36             scenarioFromFile(QFINDTESTDATA("data/contacts_groups_fetch_page1_request.txt"),
37                              QFINDTESTDATA("data/contacts_groups_fetch_page1_response.txt")),
38             scenarioFromFile(QFINDTESTDATA("data/contacts_groups_fetch_page2_request.txt"),
39                              QFINDTESTDATA("data/contacts_groups_fetch_page2_response.txt"))
40         });
41         const ContactsGroupsList groups = {
42             contactsGroupFromFile(QFINDTESTDATA("data/contacts_group1.xml")),
43             contactsGroupFromFile(QFINDTESTDATA("data/contacts_group2.xml"))
44         };
45 
46         auto account = AccountPtr::create(QStringLiteral("MockAccount"), QStringLiteral("MockToken"));
47         auto job = new ContactsGroupFetchJob(account);
48         QVERIFY(execJob(job));
49         const auto items = job->items();
50         QCOMPARE(items.count(), groups.count());
51         for (int i = 0; i < groups.count(); ++i) {
52             const auto returnedGroup =  items.at(i).dynamicCast<ContactsGroup>();
53             QVERIFY(returnedGroup);
54             QCOMPARE(*returnedGroup, *groups.at(i));
55         }
56     }
57 
testFetchSingle_data()58     void testFetchSingle_data()
59     {
60         FakeNetworkAccessManagerFactory::get()->setScenarios({
61             scenarioFromFile(QFINDTESTDATA("data/contacts_group2_fetch_request.txt"),
62                              QFINDTESTDATA("data/contacts_group1_fetch_response.txt"))
63         });
64         const auto group = contactsGroupFromFile(QFINDTESTDATA("data/contacts_group2.xml"));
65 
66         auto account = AccountPtr::create(QStringLiteral("MockAccount"), QStringLiteral("MockToken"));
67         auto job = new ContactsGroupFetchJob(group->id(), account);
68         QVERIFY(execJob(job));
69         const auto items = job->items();
70         QCOMPARE(items.count(), 1);
71         const auto returnedGroup =  items.at(0).dynamicCast<ContactsGroup>();
72         QVERIFY(returnedGroup);
73         QCOMPARE(*returnedGroup, *group);
74     }
75 };
76 
77 QTEST_GUILESS_MAIN(ContactsGroupFetchJobTest)
78 
79 #include "contactsgroupfetchjobtest.moc"
80