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
|
|
|
|
#include <utils/jsonschema/JsonFactory.h>
|
|
|
|
|
|
|
|
// hyperion includes
|
|
|
|
#include <hyperion/LedString.h>
|
|
|
|
|
2013-10-02 09:41:28 +02:00
|
|
|
Json::Value loadConfig(const std::string & 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);
|
2013-07-26 22:38:34 +02:00
|
|
|
|
2013-10-02 09:41:28 +02:00
|
|
|
// read the json schema from the resource
|
|
|
|
QResource schemaData(":/hyperion-schema");
|
|
|
|
if (!schemaData.isValid()) \
|
2013-07-26 22:38:34 +02:00
|
|
|
{
|
2013-10-02 09:41:28 +02:00
|
|
|
throw std::runtime_error("Schema not found");
|
2013-07-26 22:38:34 +02:00
|
|
|
}
|
|
|
|
|
2013-10-02 09:41:28 +02:00
|
|
|
Json::Reader jsonReader;
|
|
|
|
Json::Value schemaJson;
|
|
|
|
if (!jsonReader.parse(reinterpret_cast<const char *>(schemaData.data()), reinterpret_cast<const char *>(schemaData.data()) + schemaData.size(), schemaJson, false))
|
2013-08-13 12:03:00 +02:00
|
|
|
{
|
2013-10-02 09:41:28 +02:00
|
|
|
throw std::runtime_error("Schema error: " + jsonReader.getFormattedErrorMessages()) ;
|
2013-08-13 12:03:00 +02:00
|
|
|
}
|
2013-10-02 09:41:28 +02:00
|
|
|
JsonSchemaChecker schemaChecker;
|
|
|
|
schemaChecker.setSchema(schemaJson);
|
|
|
|
|
|
|
|
const Json::Value jsonConfig = JsonFactory::readJson(configFile);
|
|
|
|
schemaChecker.validate(jsonConfig);
|
|
|
|
|
|
|
|
return jsonConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-10-02 09:41:28 +02:00
|
|
|
const std::string configFile(argv[1]);
|
|
|
|
std::cout << "Configuration file selected: " << configFile.c_str() << std::endl;
|
|
|
|
std::cout << "Attemp to load...\t";
|
|
|
|
try
|
2013-08-13 12:03:00 +02:00
|
|
|
{
|
2013-10-02 09:41:28 +02:00
|
|
|
Json::Value value = loadConfig(configFile);
|
|
|
|
(void)value;
|
|
|
|
std::cout << "PASSED" << std::endl;
|
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
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|