File indexing completed on 2024-04-28 12:29:22

0001 /*
0002     SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "backends/openjourneyplannerparser.h"
0008 #include "testhelpers.h"
0009 
0010 #include <KPublicTransport/Journey>
0011 #include <KPublicTransport/Location>
0012 #include <KPublicTransport/Stopover>
0013 
0014 #include <QFile>
0015 #include <QJsonArray>
0016 #include <QJsonDocument>
0017 #include <QLocale>
0018 #include <QTest>
0019 
0020 #define s(x) QStringLiteral(x)
0021 
0022 using namespace KPublicTransport;
0023 
0024 class OjpParserTest : public QObject
0025 {
0026     Q_OBJECT
0027 private Q_SLOTS:
0028     void initTestCase()
0029     {
0030         qputenv("TZ", "UTC");
0031     }
0032 
0033     void testParseLocations_data()
0034     {
0035         QTest::addColumn<QString>("inFileName");
0036         QTest::addColumn<QString>("refFileName");
0037 
0038         QTest::newRow("ch-location-by-coord")
0039             << s(SOURCE_DIR "/data/ojp/ch-location-by-coord.xml")
0040             << s(SOURCE_DIR "/data/ojp/ch-location-by-coord.json");
0041         QTest::newRow("ch-location-by-name")
0042             << s(SOURCE_DIR "/data/ojp/ch-location-by-name.xml")
0043             << s(SOURCE_DIR "/data/ojp/ch-location-by-name.json");
0044         QTest::newRow("vvo-location-by-name")
0045             << s(SOURCE_DIR "/data/ojp/vvo-location-by-name.xml")
0046             << s(SOURCE_DIR "/data/ojp/vvo-location-by-name.json");
0047     }
0048 
0049     void testParseLocations()
0050     {
0051         QFETCH(QString, inFileName);
0052         QFETCH(QString, refFileName);
0053 
0054         OpenJourneyPlannerParser p;
0055         p.setLocationIdentifierType(QStringLiteral("test_id"));
0056         p.setUicLocationIdentifierType(QStringLiteral("uic"));
0057         const auto res = p.parseLocationInformationResponse(Test::readFile(inFileName));
0058         QVERIFY(!p.hasError());
0059         const auto jsonRes = Location::toJson(res);
0060 
0061         const auto ref = QJsonDocument::fromJson(Test::readFile(refFileName)).array();
0062         if (jsonRes != ref) {
0063             qDebug().noquote() << QJsonDocument(jsonRes).toJson();
0064         }
0065         QVERIFY(!jsonRes.empty());
0066         QCOMPARE(jsonRes, ref);
0067     }
0068 
0069     void testParseStopover_data()
0070     {
0071         QTest::addColumn<QString>("inFileName");
0072         QTest::addColumn<QString>("refFileName");
0073 
0074         QTest::newRow("ch-stopover-departure")
0075             << s(SOURCE_DIR "/data/ojp/ch-stopover-departure.xml")
0076             << s(SOURCE_DIR "/data/ojp/ch-stopover-departure.json");
0077         QTest::newRow("vvo-stopover-departure")
0078             << s(SOURCE_DIR "/data/ojp/vvo-stopover-departure.xml")
0079             << s(SOURCE_DIR "/data/ojp/vvo-stopover-departure.json");
0080     }
0081 
0082     void testParseStopover()
0083     {
0084         QFETCH(QString, inFileName);
0085         QFETCH(QString, refFileName);
0086 
0087         OpenJourneyPlannerParser p;
0088         p.setLocationIdentifierType(QStringLiteral("test_id"));
0089         p.setUicLocationIdentifierType(QStringLiteral("uic"));
0090         const auto res = p.parseStopEventResponse(Test::readFile(inFileName));
0091         QVERIFY(!p.hasError());
0092         const auto jsonRes = Stopover::toJson(res);
0093 
0094         const auto ref = QJsonDocument::fromJson(Test::readFile(refFileName)).array();
0095         if (jsonRes != ref) {
0096             qDebug().noquote() << QJsonDocument(jsonRes).toJson();
0097         }
0098         QVERIFY(!jsonRes.empty());
0099         QCOMPARE(jsonRes, ref);
0100     }
0101 
0102     void testParseJourney_data()
0103     {
0104         QTest::addColumn<QString>("inFileName");
0105         QTest::addColumn<QString>("refFileName");
0106 
0107         QTest::newRow("ch-journey-basic")
0108             << s(SOURCE_DIR "/data/ojp/ch-journey-basic.xml")
0109             << s(SOURCE_DIR "/data/ojp/ch-journey-basic.json");
0110         QTest::newRow("vvo-journey-intermediates")
0111             << s(SOURCE_DIR "/data/ojp/vvo-journey-intermediates.xml")
0112             << s(SOURCE_DIR "/data/ojp/vvo-journey-intermediates.json");
0113     }
0114 
0115     void testParseJourney()
0116     {
0117         QFETCH(QString, inFileName);
0118         QFETCH(QString, refFileName);
0119 
0120         OpenJourneyPlannerParser p;
0121         p.setLocationIdentifierType(QStringLiteral("test_id"));
0122         p.setUicLocationIdentifierType(QStringLiteral("uic"));
0123         const auto res = p.parseTripResponse(Test::readFile(inFileName));
0124         QVERIFY(!p.hasError());
0125         const auto jsonRes = Journey::toJson(res);
0126 
0127         const auto ref = QJsonDocument::fromJson(Test::readFile(refFileName)).array();
0128         if (jsonRes != ref) {
0129             qDebug().noquote() << QJsonDocument(jsonRes).toJson();
0130         }
0131         QVERIFY(!jsonRes.empty());
0132         QCOMPARE(jsonRes, ref);
0133     }
0134 
0135     void testParseError()
0136     {
0137         {
0138             OpenJourneyPlannerParser p;
0139             p.setLocationIdentifierType(QStringLiteral("test_id"));
0140             p.setUicLocationIdentifierType(QStringLiteral("uic"));
0141             auto res = p.parseTripResponse(Test::readFile(s(SOURCE_DIR "/data/ojp/ch-error-notripfound.xml")));
0142             QVERIFY(res.empty());
0143             QVERIFY(p.hasError());
0144             QCOMPARE(p.errorMessage(), QLatin1String("TRIP_NOTRIPFOUND"));
0145         }
0146 
0147         {
0148             OpenJourneyPlannerParser p;
0149             p.setLocationIdentifierType(QStringLiteral("test_id"));
0150             p.setUicLocationIdentifierType(QStringLiteral("uic"));
0151             auto res = p.parseStopEventResponse(Test::readFile(s(SOURCE_DIR "/data/ojp/xml-error.xml")));
0152             QVERIFY(res.empty());
0153             QVERIFY(p.hasError());
0154             QCOMPARE(p.errorMessage(), QLatin1String("Premature end of document."));
0155         }
0156     }
0157 };
0158 
0159 QTEST_GUILESS_MAIN(OjpParserTest)
0160 
0161 #include "ojpparsertest.moc"