File indexing completed on 2024-12-01 04:35:27

0001 /*
0002    SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "rolesmanagertest.h"
0008 #include "roles/rolesmanager.h"
0009 #include "ruqola_autotest_helper.h"
0010 #include <QJsonArray>
0011 #include <QSignalSpy>
0012 
0013 QTEST_MAIN(RolesManagerTest)
0014 RolesManagerTest::RolesManagerTest(QObject *parent)
0015     : QObject{parent}
0016 {
0017 }
0018 
0019 void RolesManagerTest::shouldHaveDefaultValues()
0020 {
0021     RolesManager m;
0022     QVERIFY(m.roleInfo().isEmpty());
0023 }
0024 
0025 void RolesManagerTest::shouldLoadRoles_data()
0026 {
0027     QTest::addColumn<QString>("name");
0028     QTest::addColumn<int>("numberOfRoles");
0029     {
0030         QTest::addRow("test1") << QStringLiteral("test1") << 12;
0031     }
0032 }
0033 
0034 void RolesManagerTest::shouldLoadRoles()
0035 {
0036     QFETCH(QString, name);
0037     QFETCH(int, numberOfRoles);
0038     const QString originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/rolesmanager/") + name + QLatin1String(".json");
0039     const QJsonObject obj = AutoTestHelper::loadJsonObject(originalJsonFile);
0040 
0041     RolesManager m;
0042     m.parseRoles(obj);
0043     QCOMPARE(m.roleInfo().count(), numberOfRoles);
0044 }
0045 
0046 void RolesManagerTest::shouldUpdateRoles_data()
0047 {
0048     QTest::addColumn<QString>("name");
0049     QTest::addColumn<QString>("updateName");
0050     QTest::addColumn<int>("numberOfRolesBefore");
0051     QTest::addColumn<int>("numberOfRoles");
0052     QTest::addColumn<int>("numberOfEmitSignal");
0053     {
0054         // Add new element
0055         QTest::addRow("add-element") << QStringLiteral("initialstate1") << QStringLiteral("update1") << 12 << 13 << 1;
0056     }
0057     {
0058         // Update an element
0059         QTest::addRow("update-element") << QStringLiteral("initialstate1") << QStringLiteral("update2") << 12 << 12 << 1;
0060     }
0061     {
0062         // Remove an element
0063         QTest::addRow("remove-element") << QStringLiteral("initialstate1") << QStringLiteral("remove1") << 12 << 11 << 1;
0064     }
0065     {
0066         // Invalid remove element
0067         QTest::addRow("remove-element-not-existing") << QStringLiteral("initialstate1") << QStringLiteral("remove-not-existing") << 12 << 12 << 0;
0068     }
0069 }
0070 
0071 void RolesManagerTest::shouldUpdateRoles()
0072 {
0073     QFETCH(QString, name);
0074     QFETCH(QString, updateName);
0075     QFETCH(int, numberOfRolesBefore);
0076     QFETCH(int, numberOfRoles);
0077     QFETCH(int, numberOfEmitSignal);
0078 
0079     const QString originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/rolesmanager/") + name + QLatin1String(".json");
0080     const QJsonObject obj = AutoTestHelper::loadJsonObject(originalJsonFile);
0081 
0082     RolesManager m;
0083     QSignalSpy spy(&m, &RolesManager::rolesChanged);
0084     m.parseRoles(obj);
0085     QCOMPARE(m.roleInfo().count(), numberOfRolesBefore);
0086 
0087     const QString updateJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/rolesmanager/") + updateName + QLatin1String(".json");
0088     const QJsonArray array = AutoTestHelper::loadJsonArrayObject(updateJsonFile);
0089     m.updateRoles(array);
0090     QCOMPARE(m.roleInfo().count(), numberOfRoles);
0091 
0092     QCOMPARE(spy.count(), numberOfEmitSignal);
0093 }
0094 
0095 #include "moc_rolesmanagertest.cpp"