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-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-07-20 17:16:06 +02:00
|
|
|
QJsonParseError error;
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// read and set the json schema from the resource
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
QFile schemaData(":/hyperion-schema");
|
|
|
|
|
|
|
|
if (!schemaData.open(QIODevice::ReadOnly))
|
2013-07-26 22:38:34 +02:00
|
|
|
{
|
2016-07-20 17:16:06 +02:00
|
|
|
std::stringstream error;
|
|
|
|
error << "Schema not found: " << schemaData.errorString().toStdString();
|
|
|
|
throw std::runtime_error(error.str());
|
2013-07-26 22:38:34 +02:00
|
|
|
}
|
|
|
|
|
2016-07-20 17:16:06 +02:00
|
|
|
QByteArray schema = schemaData.readAll();
|
|
|
|
QJsonDocument schemaJson = QJsonDocument::fromJson(schema, &error);
|
|
|
|
|
|
|
|
if (error.error != QJsonParseError::NoError)
|
2013-08-13 12:03:00 +02:00
|
|
|
{
|
2016-07-20 17:16:06 +02:00
|
|
|
// report to the user the failure and their locations in the document.
|
|
|
|
int errorLine(0), errorColumn(0);
|
|
|
|
|
|
|
|
for( int i=0, count=qMin( error.offset,schema.size()); i<count; ++i )
|
|
|
|
{
|
|
|
|
++errorColumn;
|
|
|
|
if(schema.at(i) == '\n' )
|
|
|
|
{
|
|
|
|
errorColumn = 0;
|
|
|
|
++errorLine;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::stringstream sstream;
|
|
|
|
sstream << "Schema error: " << error.errorString().toStdString() << " at Line: " << errorLine << ", Column: " << errorColumn;
|
2013-10-02 09:41:28 +02:00
|
|
|
|
2016-07-20 17:16:06 +02:00
|
|
|
throw std::runtime_error(sstream.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonSchemaChecker schemaChecker;
|
|
|
|
schemaChecker.setSchema(schemaJson.object());
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// read and validate the configuration file from the command line
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
const QJsonObject jsonConfig = QJsonFactory::readJson(configFile);
|
|
|
|
|
|
|
|
if (!schemaChecker.validate(jsonConfig))
|
|
|
|
{
|
|
|
|
for (std::list<std::string>::const_iterator i = schemaChecker.getMessages().begin(); i != schemaChecker.getMessages().end(); ++i)
|
|
|
|
{
|
|
|
|
std::cout << *i << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|