| 29 | namespace utils |
| 30 | { |
| 31 | bool validatePUTReply(const std::string& path, const nlohmann::json& request, const nlohmann::json& reply) |
| 32 | { |
| 33 | std::string pathAppend = path; |
| 34 | if (pathAppend.back() != '/') |
| 35 | { |
| 36 | pathAppend.push_back('/'); |
| 37 | } |
| 38 | bool success = false; |
| 39 | for (auto it = reply.begin(); it != reply.end(); ++it) |
| 40 | { |
| 41 | success = it.value().count("success"); |
| 42 | if (success) |
| 43 | { |
| 44 | // Traverse through first object |
| 45 | nlohmann::json successObject = it.value()["success"]; |
| 46 | for (auto successIt = successObject.begin(); successIt != successObject.end(); ++successIt) |
| 47 | { |
| 48 | const std::string successPath = successIt.key(); |
| 49 | if (successPath.find(pathAppend) == 0) |
| 50 | { |
| 51 | const std::string valueKey = successPath.substr(pathAppend.size()); |
| 52 | auto requestIt = request.find(valueKey); |
| 53 | success = requestIt != request.end(); |
| 54 | if (success) |
| 55 | { |
| 56 | if (valueKey == "xy") |
| 57 | { |
| 58 | success = std::abs(requestIt.value()[0].get<float>() - successIt.value()[0].get<float>()) |
| 59 | <= 1E-4f |
| 60 | && std::abs(requestIt.value()[1].get<float>() - successIt.value()[1].get<float>()) |
| 61 | <= 1E-4f; |
| 62 | } |
| 63 | else |
| 64 | { |
| 65 | success = requestIt.value() == successIt.value() |
| 66 | || (successIt.value().is_string() && successIt.value() == "Updated."); |
| 67 | } |
| 68 | if (!success) |
| 69 | { |
| 70 | std::cout << "Value " << requestIt.value() << " does not match reply " << successIt.value() |
| 71 | << std::endl; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | success = false; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | if (!success) // Fail fast |
| 82 | { |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | return success; |
| 87 | } |
| 88 | |