2013-10-02 09:41:28 +02:00
|
|
|
// QT includes
|
|
|
|
#include <QResource>
|
2017-07-30 13:32:10 +02:00
|
|
|
#include <QDebug>
|
2013-10-02 09:41:28 +02:00
|
|
|
|
2013-07-26 22:38:34 +02:00
|
|
|
// JsonSchema includes
|
2016-07-20 17:16:06 +02:00
|
|
|
#include <utils/jsonschema/QJsonFactory.h>
|
2013-07-26 22:38:34 +02:00
|
|
|
|
|
|
|
// hyperion includes
|
|
|
|
#include <hyperion/LedString.h>
|
2016-12-24 10:02:12 +01:00
|
|
|
#include "HyperionConfig.h"
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2017-07-30 13:32:10 +02:00
|
|
|
bool loadConfig(const QString & configFile, bool correct, bool ignore)
|
2013-07-26 22:38:34 +02:00
|
|
|
{
|
2013-10-02 09:41:28 +02:00
|
|
|
// make sure the resources are loaded (they may be left out after static linking)
|
|
|
|
Q_INIT_RESOURCE(resource);
|
2016-12-23 19:37:35 +01:00
|
|
|
|
2016-07-20 17:16:06 +02:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// read and set the json schema from the resource
|
|
|
|
////////////////////////////////////////////////////////////
|
2016-12-23 19:37:35 +01:00
|
|
|
|
2017-01-23 23:25:12 +01:00
|
|
|
QJsonObject schemaJson;
|
|
|
|
|
|
|
|
try
|
2013-07-26 22:38:34 +02:00
|
|
|
{
|
2017-01-23 23:25:12 +01:00
|
|
|
schemaJson = QJsonFactory::readSchema(":/hyperion-schema");
|
2013-07-26 22:38:34 +02:00
|
|
|
}
|
2017-01-23 23:25:12 +01:00
|
|
|
catch(const std::runtime_error& error)
|
2013-08-13 12:03:00 +02:00
|
|
|
{
|
2017-01-23 23:25:12 +01:00
|
|
|
throw std::runtime_error(error.what());
|
2016-07-20 17:16:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QJsonSchemaChecker schemaChecker;
|
2017-01-23 23:25:12 +01:00
|
|
|
schemaChecker.setSchema(schemaJson);
|
2016-07-20 17:16:06 +02:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// read and validate the configuration file from the command line
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
2017-07-30 13:32:10 +02:00
|
|
|
QJsonObject jsonConfig = QJsonFactory::readConfig(configFile);
|
|
|
|
|
|
|
|
if (!correct)
|
2016-07-20 17:16:06 +02:00
|
|
|
{
|
2017-07-30 13:32:10 +02:00
|
|
|
if (!schemaChecker.validate(jsonConfig).first)
|
2016-07-20 17:16:06 +02:00
|
|
|
{
|
2017-07-30 13:32:10 +02:00
|
|
|
QStringList schemaErrors = schemaChecker.getMessages();
|
|
|
|
foreach (auto & schemaError, schemaErrors)
|
|
|
|
{
|
|
|
|
qDebug() << "config write validation: " << schemaError;
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << "FAILED";
|
|
|
|
exit(1);
|
|
|
|
return false;
|
2016-07-20 17:16:06 +02:00
|
|
|
}
|
2017-07-30 13:32:10 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
jsonConfig = schemaChecker.getAutoCorrectedConfig(jsonConfig, ignore); // The second parameter is to ignore the "required" keyword in hyperion schema
|
|
|
|
QJsonFactory::writeJson(configFile, jsonConfig);
|
2016-07-20 17:16:06 +02:00
|
|
|
}
|
2013-10-02 09:41:28 +02:00
|
|
|
|
2016-07-20 17:16:06 +02:00
|
|
|
return true;
|
2013-10-02 09:41:28 +02:00
|
|
|
}
|
|
|
|
|
2017-07-30 13:32:10 +02:00
|
|
|
void usage()
|
|
|
|
{
|
|
|
|
qDebug() << "Missing required configuration file to test";
|
|
|
|
qDebug() << "Usage: test_configfile <option> [configfile]";
|
|
|
|
qDebug() << "<option>:";
|
|
|
|
qDebug() << "\t--ac - for json auto correction";
|
|
|
|
qDebug() << "\t--ac-ignore-required - for json auto correction without paying attention 'required' keyword in hyperion schema";
|
|
|
|
}
|
|
|
|
|
2013-10-02 09:41:28 +02:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2017-07-30 13:32:10 +02:00
|
|
|
if (argc < 2)
|
2013-08-13 12:03:00 +02:00
|
|
|
{
|
2017-07-30 13:32:10 +02:00
|
|
|
usage();
|
2013-10-02 09:41:28 +02:00
|
|
|
return 0;
|
2013-08-13 12:03:00 +02:00
|
|
|
}
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2017-07-30 13:32:10 +02:00
|
|
|
QString option = argv[1];
|
|
|
|
QString configFile;
|
|
|
|
|
|
|
|
if (option == "--ac" || option == "--ac-ignore-required")
|
|
|
|
if (argc > 2)
|
|
|
|
configFile = argv[2];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
usage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
configFile = argv[1];
|
|
|
|
|
|
|
|
qDebug() << "Configuration file selected: " << configFile;
|
|
|
|
qDebug() << "Attemp to load...";
|
2013-10-02 09:41:28 +02:00
|
|
|
try
|
2013-08-13 12:03:00 +02:00
|
|
|
{
|
2017-07-30 13:32:10 +02:00
|
|
|
if (loadConfig(configFile, (option == "--ac" || option == "--ac-ignore-required"), option == "--ac-ignore-required"))
|
|
|
|
qDebug() << "PASSED";
|
2016-09-05 17:26:29 +02:00
|
|
|
return 0;
|
2013-08-13 12:03:00 +02:00
|
|
|
}
|
2013-10-02 09:41:28 +02:00
|
|
|
catch (std::runtime_error exception)
|
2013-08-13 12:03:00 +02:00
|
|
|
{
|
2017-07-30 13:32:10 +02:00
|
|
|
qDebug() << "FAILED";
|
|
|
|
qDebug() << exception.what();
|
2013-08-13 12:03:00 +02:00
|
|
|
}
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2016-09-05 17:26:29 +02:00
|
|
|
return 1;
|
2013-07-26 22:38:34 +02:00
|
|
|
}
|