File indexing completed on 2024-05-12 05:10:04

0001 /***************************************************************************
0002     Copyright (C) 2023 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #undef QT_NO_CAST_FROM_ASCII
0026 
0027 #include "opdsfetchertest.h"
0028 
0029 #include "../fetch/opdsfetcher.h"
0030 #include "../collections/bookcollection.h"
0031 #include "../collectionfactory.h"
0032 #include "../entry.h"
0033 #include "../images/imagefactory.h"
0034 #include "../utils/datafileregistry.h"
0035 
0036 #include <KSharedConfig>
0037 #include <KConfigGroup>
0038 
0039 #include <QTest>
0040 
0041 QTEST_GUILESS_MAIN( OPDSFetcherTest )
0042 
0043 OPDSFetcherTest::OPDSFetcherTest() : AbstractFetcherTest() {
0044   QStandardPaths::setTestModeEnabled(true);
0045 }
0046 
0047 void OPDSFetcherTest::initTestCase() {
0048   Tellico::RegisterCollection<Tellico::Data::BookCollection> registerBook(Tellico::Data::Collection::Book, "book");
0049   Tellico::DataFileRegistry::self()->addDataLocation(QFINDTESTDATA("../../xslt/atom2tellico.xsl"));
0050   Tellico::ImageFactory::init();
0051 }
0052 
0053 void OPDSFetcherTest::testFeedbooksSearch() {
0054   KConfigGroup cg = KSharedConfig::openConfig(QString(), KConfig::SimpleConfig)->group("Feedbooks");
0055   cg.writeEntry("Catalog", "https://www.feedbooks.com/catalog.atom");
0056   cg.writeEntry("Custom Fields", "url");
0057 
0058   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Book, Tellico::Fetch::ISBN,
0059                                        "9781773231341");
0060   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::OPDSFetcher(this));
0061   QVERIFY(fetcher->canSearch(request.key()));
0062   fetcher->readConfig(cg);
0063 
0064   Tellico::Data::EntryList results = DO_FETCH(fetcher, request);
0065 
0066   QCOMPARE(results.size(), 1);
0067 
0068   Tellico::Data::EntryPtr entry = results.at(0);
0069   QCOMPARE(entry->field("title"), "First Lensman");
0070   QCOMPARE(entry->field("author"), "E. E. Smith");
0071   QCOMPARE(entry->field("isbn"), "978-1-77323-134-1");
0072   QCOMPARE(entry->field("pub_year"), "2018");
0073   QCOMPARE(entry->field("publisher"), "Reading Essentials");
0074   QCOMPARE(entry->field("genre"), "Fiction; Science fiction; Space opera and planet opera");
0075   QCOMPARE(entry->field("pages"), "226");
0076   QCOMPARE(entry->field("url"), "https://www.feedbooks.com/item/2971293");
0077   QVERIFY(!entry->field("cover").isEmpty());
0078   QVERIFY(!entry->field("cover").contains(QLatin1Char('/')));
0079   QVERIFY(!entry->field("plot").isEmpty());
0080 }
0081 
0082 void OPDSFetcherTest::testEmptyGutenberg() {
0083   KConfigGroup cg = KSharedConfig::openConfig(QString(), KConfig::SimpleConfig)->group("Feedbooks");
0084   cg.writeEntry("Catalog", "https://m.gutenberg.org/ebooks.opds/");
0085 
0086   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Book, Tellico::Fetch::Title,
0087                                        "XXXXXXXXXXXX");
0088   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::OPDSFetcher(this));
0089   QVERIFY(fetcher->canSearch(request.key()));
0090   fetcher->readConfig(cg);
0091 
0092   Tellico::Data::EntryList results = DO_FETCH(fetcher, request);
0093 
0094   // should be no results
0095   QVERIFY(results.isEmpty());
0096   QVERIFY(!fetcher->attribution().isEmpty());
0097 }
0098 
0099 void OPDSFetcherTest::testRelativeSearch() {
0100   QUrl catalog = QUrl::fromLocalFile(QFINDTESTDATA("data/opds-relative-search.xml"));
0101   QUrl search = catalog.resolved(QUrl(QLatin1String("../opensearch.xml")));
0102   Tellico::Fetch::OPDSFetcher::Reader reader(catalog);
0103 
0104   QVERIFY(reader.parse());
0105   QUrl searchUrl = reader.searchUrl;
0106   QVERIFY(!searchUrl.isEmpty());
0107   QVERIFY(!searchUrl.isRelative());
0108   QCOMPARE(searchUrl.url(), search.url());
0109 }
0110 
0111 void OPDSFetcherTest::testAcquisitionByTitle() {
0112   QUrl catalog = QUrl::fromLocalFile(QFINDTESTDATA("data/opds-acquisition.xml"));
0113   KConfigGroup cg = KSharedConfig::openConfig(QString(), KConfig::SimpleConfig)->group("Feedbooks");
0114   cg.writeEntry("Catalog", catalog.url());
0115 
0116   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Book, Tellico::Fetch::Title,
0117                                        "Pride and Prejudice");
0118   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::OPDSFetcher(this));
0119   QVERIFY(fetcher->canSearch(request.key()));
0120   fetcher->readConfig(cg);
0121 
0122   Tellico::Data::EntryList results = DO_FETCH(fetcher, request);
0123 
0124   QCOMPARE(results.size(), 1);
0125 
0126   Tellico::Data::EntryPtr entry = results.at(0);
0127   QCOMPARE(entry->field("title"), "Pride and Prejudice");
0128   QCOMPARE(entry->field("author"), "Jane Austen");
0129   QCOMPARE(entry->field("pub_year"), "1813");
0130   QCOMPARE(entry->field("genre"), "Fiction; Romance");
0131   QVERIFY(!entry->field("cover").isEmpty());
0132   QVERIFY(!entry->field("cover").contains(QLatin1String("..")));
0133   QVERIFY(!entry->field("cover").contains(QLatin1Char('/')));
0134 }
0135 
0136 void OPDSFetcherTest::testAcquisitionByTitleNegative() {
0137   QUrl catalog = QUrl::fromLocalFile(QFINDTESTDATA("data/opds-acquisition.xml"));
0138   KConfigGroup cg = KSharedConfig::openConfig(QString(), KConfig::SimpleConfig)->group("Feedbooks");
0139   cg.writeEntry("Catalog", catalog.url());
0140 
0141   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Book, Tellico::Fetch::Title,
0142                                        "Nonexistent");
0143   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::OPDSFetcher(this));
0144   QVERIFY(fetcher->canSearch(request.key()));
0145   fetcher->readConfig(cg);
0146 
0147   Tellico::Data::EntryList results = DO_FETCH(fetcher, request);
0148   QVERIFY(results.isEmpty());
0149 }
0150 
0151 void OPDSFetcherTest::testAcquisitionByKeyword() {
0152   QUrl catalog = QUrl::fromLocalFile(QFINDTESTDATA("data/opds-acquisition.xml"));
0153   KConfigGroup cg = KSharedConfig::openConfig(QString(), KConfig::SimpleConfig)->group("Feedbooks");
0154   cg.writeEntry("Catalog", catalog.url());
0155 
0156   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Book, Tellico::Fetch::Keyword,
0157                                        "fizzes");
0158   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::OPDSFetcher(this));
0159   fetcher->readConfig(cg);
0160 
0161   Tellico::Data::EntryList results = DO_FETCH(fetcher, request);
0162 
0163   QCOMPARE(results.size(), 16);
0164 }
0165 
0166 void OPDSFetcherTest::testCalibre() {
0167   QUrl catalog = QUrl::fromLocalFile(QFINDTESTDATA("data/opds-calibre.xml"));
0168   KConfigGroup cg = KSharedConfig::openConfig(QString(), KConfig::SimpleConfig)->group("Calibre");
0169   cg.writeEntry("Catalog", catalog.url());
0170 
0171   Tellico::Fetch::FetchRequest request(Tellico::Data::Collection::Book, Tellico::Fetch::Title,
0172                                        "1632");
0173   Tellico::Fetch::Fetcher::Ptr fetcher(new Tellico::Fetch::OPDSFetcher(this));
0174   fetcher->readConfig(cg);
0175 
0176   Tellico::Data::EntryList results = DO_FETCH(fetcher, request);
0177 
0178   QCOMPARE(results.size(), 1);
0179 
0180   Tellico::Data::EntryPtr entry = results.at(0);
0181   QCOMPARE(entry->field("title"), "1632");
0182   QCOMPARE(entry->field("author"), "Flint, Eric");
0183   QCOMPARE(entry->field("rating"), "4");
0184   QCOMPARE(entry->field("series"), "1632");
0185   QCOMPARE(entry->field("series_num"), "1");
0186   QVERIFY(!entry->field("plot").isEmpty());
0187   QVERIFY(!entry->field("plot").contains("SUMMARY"));
0188 }