Updated test for configfile for verification against schema.

Former-commit-id: 97ecb9e26d266f925c60462de91af9f062ebf3cf
This commit is contained in:
T. van der Zwan 2013-10-02 07:41:28 +00:00
parent 352d9f5652
commit fc3361f760

View File

@ -2,50 +2,64 @@
// STL includes // STL includes
#include <cstdlib> #include <cstdlib>
// QT includes
#include <QResource>
// JsonSchema includes // JsonSchema includes
#include <utils/jsonschema/JsonFactory.h> #include <utils/jsonschema/JsonFactory.h>
// hyperion includes // hyperion includes
#include <hyperion/LedString.h> #include <hyperion/LedString.h>
int main() Json::Value loadConfig(const std::string & configFile)
{ {
std::string homeDir = getenv("RASPILIGHT_HOME"); // make sure the resources are loaded (they may be left out after static linking)
Q_INIT_RESOURCE(resource);
const std::string schemaFile = homeDir + "/hyperion.schema.json"; // read the json schema from the resource
const std::string configFile = homeDir + "/hyperion.config.json"; QResource schemaData(":/hyperion-schema");
if (!schemaData.isValid()) \
Json::Value config;
if (JsonFactory::load(schemaFile, configFile, config) < 0)
{ {
std::cerr << "UNABLE TO LOAD CONFIGURATION" << std::endl; throw std::runtime_error("Schema not found");
return -1;
} }
const Json::Value& deviceConfig = config["device"]; Json::Reader jsonReader;
if (deviceConfig.empty()) Json::Value schemaJson;
if (!jsonReader.parse(reinterpret_cast<const char *>(schemaData.data()), reinterpret_cast<const char *>(schemaData.data()) + schemaData.size(), schemaJson, false))
{ {
std::cout << "Missing DEVICE configuration 'device'" << std::endl; throw std::runtime_error("Schema error: " + jsonReader.getFormattedErrorMessages()) ;
} }
const Json::Value& ledConfig = config["leds"]; JsonSchemaChecker schemaChecker;
if (ledConfig.empty()) schemaChecker.setSchema(schemaJson);
{
std::cout << "Missing LEDS configuration 'leds'" << std::endl; const Json::Value jsonConfig = JsonFactory::readJson(configFile);
schemaChecker.validate(jsonConfig);
return jsonConfig;
} }
const Json::Value& colorConfig = config["color"]; int main(int argc, char** argv)
if (colorConfig.empty())
{ {
std::cout << "Missing COLORS configuration 'colors'" << std::endl; if (argc != 2)
{
std::cerr << "Missing required configuration file to test" << std::endl;
std::cerr << "Usage: test_configfile [configfile]" << std::endl;
return 0;
} }
else
{
std::cout << "COLOR CONFIGURATION: " << colorConfig.toStyledString() << std::endl;
const Json::Value& redConfig = colorConfig["red"]; const std::string configFile(argv[1]);
double redGamma = redConfig["gamma"].asDouble(); std::cout << "Configuration file selected: " << configFile.c_str() << std::endl;
std::cout << "RED GAMMA = " << redGamma << std::endl; std::cout << "Attemp to load...\t";
std::cout << "RED GAMMA = " << colorConfig["red.gamma"].asDouble() << std::endl; try
{
Json::Value value = loadConfig(configFile);
(void)value;
std::cout << "PASSED" << std::endl;
}
catch (std::runtime_error exception)
{
std::cout << "FAILED" << std::endl;
std::cout << exception.what() << std::endl;
} }
return 0; return 0;