TestConfigFile.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // QT includes
  2. #include <QResource>
  3. #include <QDebug>
  4. // JsonSchema includes
  5. #include <utils/jsonschema/QJsonFactory.h>
  6. // hyperion includes
  7. #include <hyperion/LedString.h>
  8. #include "HyperionConfig.h"
  9. bool loadConfig(const QString & configFile, bool correct, bool ignore)
  10. {
  11. // make sure the resources are loaded (they may be left out after static linking)
  12. Q_INIT_RESOURCE(resource);
  13. ////////////////////////////////////////////////////////////
  14. // read and set the json schema from the resource
  15. ////////////////////////////////////////////////////////////
  16. QJsonObject schemaJson;
  17. try
  18. {
  19. schemaJson = QJsonFactory::readSchema(":/hyperion-schema");
  20. }
  21. catch(const std::runtime_error& error)
  22. {
  23. throw std::runtime_error(error.what());
  24. }
  25. QJsonSchemaChecker schemaChecker;
  26. schemaChecker.setSchema(schemaJson);
  27. ////////////////////////////////////////////////////////////
  28. // read and validate the configuration file from the command line
  29. ////////////////////////////////////////////////////////////
  30. QJsonObject jsonConfig = QJsonFactory::readConfig(configFile);
  31. if (!correct)
  32. {
  33. if (!schemaChecker.validate(jsonConfig).first)
  34. {
  35. QStringList schemaErrors = schemaChecker.getMessages();
  36. for (auto & schemaError : schemaErrors)
  37. {
  38. qDebug() << "config write validation: " << schemaError;
  39. }
  40. qDebug() << "FAILED";
  41. exit(1);
  42. return false;
  43. }
  44. }
  45. else
  46. {
  47. jsonConfig = schemaChecker.getAutoCorrectedConfig(jsonConfig, ignore); // The second parameter is to ignore the "required" keyword in hyperion schema
  48. QJsonFactory::writeJson(configFile, jsonConfig);
  49. }
  50. return true;
  51. }
  52. void usage()
  53. {
  54. qDebug() << "Missing required configuration file to test";
  55. qDebug() << "Usage: test_configfile <option> [configfile]";
  56. qDebug() << "<option>:";
  57. qDebug() << "\t--ac - for json auto correction";
  58. qDebug() << "\t--ac-ignore-required - for json auto correction without paying attention 'required' keyword in hyperion schema";
  59. }
  60. int main(int argc, char** argv)
  61. {
  62. if (argc < 2)
  63. {
  64. usage();
  65. return 0;
  66. }
  67. QString option = argv[1];
  68. QString configFile;
  69. if (option == "--ac" || option == "--ac-ignore-required")
  70. {
  71. if (argc > 2)
  72. configFile = argv[2];
  73. else
  74. {
  75. usage();
  76. return 0;
  77. }
  78. }
  79. else configFile = argv[1];
  80. qDebug() << "Configuration file selected: " << configFile;
  81. qDebug() << "Attemp to load...";
  82. try
  83. {
  84. if (loadConfig(configFile, (option == "--ac" || option == "--ac-ignore-required"), option == "--ac-ignore-required"))
  85. qDebug() << "PASSED";
  86. return 0;
  87. }
  88. catch (std::runtime_error& exception)
  89. {
  90. qDebug() << "FAILED";
  91. qDebug() << exception.what();
  92. }
  93. return 1;
  94. }