2013-07-26 22:38:34 +02:00
|
|
|
|
|
|
|
// STL includes
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2013-10-02 09:41:28 +02:00
|
|
|
// QT includes
|
|
|
|
#include <QResource>
|
|
|
|
|
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
|
|
|
|
2016-07-20 17:16:06 +02:00
|
|
|
bool loadConfig(const QString & configFile)
|
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-01-23 23:25:12 +01:00
|
|
|
const QJsonObject jsonConfig = QJsonFactory::readConfig(configFile);
|
2016-07-20 17:16:06 +02:00
|
|
|
|
|
|
|
if (!schemaChecker.validate(jsonConfig))
|
|
|
|
{
|
2017-03-04 22:17:42 +01:00
|
|
|
QStringList schemaErrors = schemaChecker.getMessages();
|
|
|
|
foreach (auto & schemaError, schemaErrors)
|
2016-07-20 17:16:06 +02:00
|
|
|
{
|
2017-03-04 22:17:42 +01:00
|
|
|
std::cout << "config write validation: " << schemaError.toStdString() << std::endl;
|
2016-07-20 17:16:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "FAILED" << std::endl;
|
|
|
|
exit(1);
|
|
|
|
return false;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
if (argc != 2)
|
2013-08-13 12:03:00 +02:00
|
|
|
{
|
2013-10-02 09:41:28 +02:00
|
|
|
std::cerr << "Missing required configuration file to test" << std::endl;
|
|
|
|
std::cerr << "Usage: test_configfile [configfile]" << std::endl;
|
|
|
|
return 0;
|
2013-08-13 12:03:00 +02:00
|
|
|
}
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2016-07-20 17:16:06 +02:00
|
|
|
const QString configFile(argv[1]);
|
|
|
|
std::cout << "Configuration file selected: " << configFile.toStdString() << std::endl;
|
|
|
|
std::cout << "Attemp to load..." << std::endl;
|
2013-10-02 09:41:28 +02:00
|
|
|
try
|
2013-08-13 12:03:00 +02:00
|
|
|
{
|
2016-07-20 17:16:06 +02:00
|
|
|
if (loadConfig(configFile))
|
|
|
|
std::cout << "PASSED" << std::endl;
|
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
|
|
|
{
|
2013-10-02 09:41:28 +02:00
|
|
|
std::cout << "FAILED" << std::endl;
|
|
|
|
std::cout << exception.what() << std::endl;
|
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
|
|
|
}
|