File indexing completed on 2024-05-12 05:22:07

0001 /*
0002  * Createright (C) 2018  Daniel Vrátil <dvratil@kde.org>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) version 3, or any
0008  * later version accepted by the membership of KDE e.V. (or its
0009  * successor approved by the membership of KDE e.V.), which shall
0010  * act as a proxy defined in Section 6 of version 3 of the license.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0015  * GNU Lesser General Public License for more details.
0016  *
0017  * You should have received a create of the GNU Lesser General Public
0018  * License along with this library.  If not, see <https://www.gnu.org/licenses/>.
0019  */
0020 
0021 #include <QObject>
0022 #include <QTest>
0023 
0024 #include "drivetestutils.h"
0025 #include "fakenetworkaccessmanagerfactory.h"
0026 #include "testutils.h"
0027 
0028 #include "account.h"
0029 #include "file.h"
0030 #include "filecreatejob.h"
0031 #include "types.h"
0032 
0033 using namespace KGAPI2;
0034 
0035 Q_DECLARE_METATYPE(QList<FakeNetworkAccessManager::Scenario>)
0036 Q_DECLARE_METATYPE(KGAPI2::Drive::FilePtr)
0037 
0038 class FileCreateJobTest : public QObject
0039 {
0040     Q_OBJECT
0041 private Q_SLOTS:
0042     void initTestCase()
0043     {
0044         NetworkAccessManagerFactory::setFactory(new FakeNetworkAccessManagerFactory);
0045     }
0046 
0047     void testCreate_data()
0048     {
0049         QTest::addColumn<QList<FakeNetworkAccessManager::Scenario>>("scenarios");
0050         QTest::addColumn<Drive::FilePtr>("sourceFile");
0051         QTest::addColumn<QString>("uploadFilePath");
0052         QTest::addColumn<Drive::FilePtr>("expectedResult");
0053 
0054         QTest::newRow("metadata only") << QList<FakeNetworkAccessManager::Scenario>{scenarioFromFile(QFINDTESTDATA("data/file1_create_request.txt"),
0055                                                                                                      QFINDTESTDATA("data/file1_create_response.txt"))}
0056                                        << fileFromFile(QFINDTESTDATA("data/file1.json")) << QString() << fileFromFile(QFINDTESTDATA("data/file1.json"));
0057 
0058         // NOTE: The scenarios are reversed due use of QMap, which orders the files
0059         // by ID
0060         QTest::newRow("upload") << QList<FakeNetworkAccessManager::Scenario>{scenarioFromFile(QFINDTESTDATA("data/file2_create_request.txt"),
0061                                                                                               QFINDTESTDATA("data/file2_create_response.txt"))}
0062                                 << fileFromFile(QFINDTESTDATA("data/file2.json")) << QFINDTESTDATA("data/DSC_1287.JPG")
0063                                 << fileFromFile(QFINDTESTDATA("data/file2.json"));
0064     }
0065 
0066     void testCreate()
0067     {
0068         QFETCH(QList<FakeNetworkAccessManager::Scenario>, scenarios);
0069         QFETCH(Drive::FilePtr, sourceFile);
0070         QFETCH(QString, uploadFilePath);
0071         QFETCH(Drive::FilePtr, expectedResult);
0072 
0073         FakeNetworkAccessManagerFactory::get()->setScenarios(scenarios);
0074 
0075         auto account = AccountPtr::create(QStringLiteral("MockAccount"), QStringLiteral("MockToken"));
0076         Drive::FileCreateJob *job = nullptr;
0077         if (uploadFilePath.isNull()) {
0078             job = new Drive::FileCreateJob(sourceFile, account);
0079         } else {
0080             job = new Drive::FileCreateJob(uploadFilePath, sourceFile, account);
0081         }
0082 
0083         QVERIFY(execJob(job));
0084         const auto items = job->files();
0085         QCOMPARE(items.count(), 1);
0086         QVERIFY(*items.cbegin());
0087         QCOMPARE(**items.cbegin(), *expectedResult);
0088     }
0089 };
0090 
0091 QTEST_GUILESS_MAIN(FileCreateJobTest)
0092 
0093 #include "filecreatejobtest.moc"