File indexing completed on 2024-07-21 12:41:46

0001 /*
0002     SPDX-FileCopyrightText: 2020 Elvis Angelaccio <elvis.angelaccio@kde.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "../src/s3url.h"
0007 
0008 #include <QTest>
0009 
0010 class S3UrlTest : public QObject
0011 {
0012     Q_OBJECT
0013 
0014 private Q_SLOTS:
0015     void testS3Url_data();
0016     void testS3Url();
0017 };
0018 
0019 QTEST_GUILESS_MAIN(S3UrlTest)
0020 
0021 void S3UrlTest::testS3Url_data()
0022 {
0023     QTest::addColumn<QUrl>("url");
0024     QTest::addColumn<bool>("expectedIsBucket");
0025     QTest::addColumn<bool>("expectedIsKey");
0026     QTest::addColumn<QString>("expectedBucketName");
0027     QTest::addColumn<QString>("expectedKey");
0028     QTest::addColumn<QString>("expectedPrefix");
0029 
0030     QTest::newRow("root url")
0031             << QUrl(QStringLiteral("s3:"))
0032             << false
0033             << false
0034             << QString()
0035             << QString()
0036             << QString();
0037 
0038     QTest::newRow("bucket url")
0039             << QUrl(QStringLiteral("s3://foo-bucket"))
0040             << true
0041             << false
0042             << QStringLiteral("foo-bucket")
0043             << QString()
0044             << QString();
0045 
0046     QTest::newRow("root key url")
0047             << QUrl(QStringLiteral("s3://foo-bucket/"))
0048             << false
0049             << true
0050             << QStringLiteral("foo-bucket")
0051             << QStringLiteral("/")
0052             << QString();
0053 
0054     QTest::newRow("top-level file")
0055             << QUrl(QStringLiteral("s3://foo-bucket/bar.txt"))
0056             << false
0057             << true
0058             << QStringLiteral("foo-bucket")
0059             << QStringLiteral("/bar.txt")
0060             << QStringLiteral("bar.txt/");
0061 
0062     QTest::newRow("top-level folder")
0063             << QUrl(QStringLiteral("s3://foo-bucket/bar/"))
0064             << false
0065             << true
0066             << QStringLiteral("foo-bucket")
0067             << QStringLiteral("/bar/")
0068             << QStringLiteral("bar/");
0069 
0070     QTest::newRow("top-level folder without trailing slash")
0071             << QUrl(QStringLiteral("s3://foo-bucket/bar"))
0072             << false
0073             << true
0074             << QStringLiteral("foo-bucket")
0075             << QStringLiteral("/bar")
0076             << QStringLiteral("bar/");
0077 
0078     QTest::newRow("file-in-toplevel-folder")
0079             << QUrl(QStringLiteral("s3://foo-bucket/bar/foo.txt"))
0080             << false
0081             << true
0082             << QStringLiteral("foo-bucket")
0083             << QStringLiteral("/bar/foo.txt")
0084             << QStringLiteral("bar/foo.txt/");
0085 
0086     QTest::newRow("subfolder")
0087             << QUrl(QStringLiteral("s3://foo-bucket/bar/baz/"))
0088             << false
0089             << true
0090             << QStringLiteral("foo-bucket")
0091             << QStringLiteral("/bar/baz/")
0092             << QStringLiteral("bar/baz/");
0093 
0094     QTest::newRow("file-in-subfolder")
0095             << QUrl(QStringLiteral("s3://foo-bucket/bar/baz/foo.txt"))
0096             << false
0097             << true
0098             << QStringLiteral("foo-bucket")
0099             << QStringLiteral("/bar/baz/foo.txt")
0100             << QStringLiteral("bar/baz/foo.txt/");
0101 }
0102 
0103 void S3UrlTest::testS3Url()
0104 {
0105     QFETCH(QUrl, url);
0106     QVERIFY(url.isValid());
0107 
0108     const auto s3url = S3Url(url);
0109 
0110     QFETCH(bool, expectedIsBucket);
0111     QCOMPARE(s3url.isBucket(), expectedIsBucket);
0112 
0113     QFETCH(bool, expectedIsKey);
0114     QCOMPARE(s3url.isKey(), expectedIsKey);
0115 
0116     QFETCH(QString, expectedBucketName);
0117     QFETCH(QString, expectedKey);
0118     QFETCH(QString, expectedPrefix);
0119 
0120     QCOMPARE(s3url.bucketName(), expectedBucketName);
0121     QCOMPARE(s3url.key(), expectedKey);
0122     QCOMPARE(s3url.prefix(), expectedPrefix);
0123 }
0124 
0125 #include "s3urltest.moc"