1 /*
2  * SPDX-FileCopyrightText: 2018 Daniel Vrátil <dvratil@kde.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * move it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) version 3, or any
8  * later version accepted by the membership of KDE e.V. (or its
9  * successor approved by the membership of KDE e.V.), which shall
10  * act as a proxy defined in Section 6 of version 3 of the license.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library.  If not, see <https://www.gnu.org/licenses/>.
19  */
20 
21 #include <QObject>
22 #include <QTest>
23 
24 #include "fakenetworkaccessmanagerfactory.h"
25 #include "testutils.h"
26 #include "taskstestutils.h"
27 
28 #include "types.h"
29 #include "taskmovejob.h"
30 #include "task.h"
31 #include "account.h"
32 
33 using namespace KGAPI2;
34 
35 Q_DECLARE_METATYPE(QList<FakeNetworkAccessManager::Scenario>)
36 Q_DECLARE_METATYPE(KGAPI2::TasksList)
37 
38 class TaskMoveJobTest : public QObject
39 {
40     Q_OBJECT
41 private Q_SLOTS:
initTestCase()42     void initTestCase()
43     {
44         NetworkAccessManagerFactory::setFactory(new FakeNetworkAccessManagerFactory);
45     }
46 
testMove_data()47     void testMove_data()
48     {
49         QTest::addColumn<QList<FakeNetworkAccessManager::Scenario>>("scenarios");
50         QTest::addColumn<TasksList>("tasks");
51         QTest::addColumn<QString>("newParent");
52 
53         QTest::newRow("single move to top-level")
54             << QList<FakeNetworkAccessManager::Scenario>{
55                     scenarioFromFile(QFINDTESTDATA("data/task1_move_request.txt"),
56                                      QFINDTESTDATA("data/task1_move_response.txt")),
57                 }
58             << TasksList{ taskFromFile(QFINDTESTDATA("data/task1.json")) }
59             << QString();
60 
61         QTest::newRow("single move to new parent")
62             << QList<FakeNetworkAccessManager::Scenario>{
63                     scenarioFromFile(QFINDTESTDATA("data/task2_move_with_parent_request.txt"),
64                                      QFINDTESTDATA("data/task2_move_response.txt")),
65                 }
66             << TasksList{ taskFromFile(QFINDTESTDATA("data/task2.json")) }
67             << QStringLiteral("newMockParent");
68 
69         QTest::newRow("batch move")
70             << QList<FakeNetworkAccessManager::Scenario>{
71                     scenarioFromFile(QFINDTESTDATA("data/task1_move_request.txt"),
72                                      QFINDTESTDATA("data/task1_move_response.txt")),
73                     scenarioFromFile(QFINDTESTDATA("data/task2_move_request.txt"),
74                                      QFINDTESTDATA("data/task2_move_response.txt"))
75                 }
76             << TasksList{
77                     taskFromFile(QFINDTESTDATA("data/task1.json")),
78                     taskFromFile(QFINDTESTDATA("data/task2.json"))
79                 }
80             << QString();
81     }
82 
testMove()83     void testMove()
84     {
85         QFETCH(QList<FakeNetworkAccessManager::Scenario>, scenarios);
86         QFETCH(TasksList, tasks);
87         QFETCH(QString, newParent);
88 
89         FakeNetworkAccessManagerFactory::get()->setScenarios(scenarios);
90 
91         auto account = AccountPtr::create(QStringLiteral("MockAccount"), QStringLiteral("MockToken"));
92         TaskMoveJob *job = nullptr;
93         if (tasks.count() == 1) {
94             job = new TaskMoveJob(tasks.at(0), QStringLiteral("MockAccount"), newParent, account);
95         } else {
96             job = new TaskMoveJob(tasks, QStringLiteral("MockAccount"), newParent, account);
97         }
98         QVERIFY(execJob(job));
99     }
100 };
101 
102 QTEST_GUILESS_MAIN(TaskMoveJobTest)
103 
104 #include "taskmovejobtest.moc"
105 
106 
107 
108