-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: Native SDK unit tests support framework #219
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ca3cdcd
Native sdk unit tests framework initial commit
parag-pv 86d5300
Fixing the buffer overflow in rpc dereferencing
parag-pv 1e17527
Updated schema validator to loop through params in request
parag-pv d120d22
Removing commented code, adding ifdef conditions
parag-pv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
#include<iostream> | ||
#include <fstream> | ||
|
||
#include "gtest/gtest.h" | ||
#include "gmock/gmock.h" | ||
|
||
#include <nlohmann/json.hpp> | ||
#include <nlohmann/json-schema.hpp> | ||
|
||
using nlohmann::json; | ||
using nlohmann::json_schema::json_validator; | ||
using namespace ::testing; | ||
|
||
#define REMOVE_QUOTES(s) (s.substr(1, s.length() - 2)) | ||
#define STRING_TO_BOOL(s) (s == "true" ? true : false) | ||
|
||
|
||
inline std::string capitalizeFirstChar(std::string str) { | ||
if (!str.empty()) { | ||
str[0] = std::toupper(str[0]); | ||
} | ||
return str; | ||
} | ||
|
||
|
||
class JsonEngine | ||
{ | ||
private: | ||
std::ifstream _file; | ||
nlohmann::json _data; | ||
|
||
public: | ||
|
||
JsonEngine() | ||
{ | ||
_data = read_json_from_file("../firebolt-core-open-rpc.json"); | ||
} | ||
|
||
~JsonEngine(){ | ||
if (_file.is_open()) | ||
_file.close(); | ||
} | ||
|
||
std::string get_value(const std::string& method_name) | ||
{ | ||
for (const auto &method : _data["methods"]) | ||
{ | ||
if (method.contains("name") && (method["name"] == method_name)) | ||
{ | ||
auto value = method["examples"][0]["result"]["value"]; | ||
return value.dump(); | ||
} | ||
} | ||
return ""; | ||
} | ||
|
||
json read_json_from_file(const std::string &filename) | ||
{ | ||
std::ifstream file(filename); | ||
if (!file.is_open()) | ||
{ | ||
throw std::runtime_error("Could not open file: " + filename); | ||
} | ||
|
||
json j; | ||
file >> j; | ||
return j; | ||
} | ||
|
||
json resolve_reference(const json &full_schema, const std::string &ref) | ||
{ | ||
if (ref.find("#/") != 0) | ||
{ | ||
throw std::invalid_argument("Only internal references supported"); | ||
} | ||
|
||
std::string path = ref.substr(2); | ||
std::istringstream ss(path); | ||
std::string token; | ||
json current = full_schema; | ||
|
||
while (std::getline(ss, token, '/')) | ||
{ | ||
if (current.contains(token)) | ||
{ | ||
current = current[token]; | ||
} | ||
else | ||
{ | ||
throw std::invalid_argument("Invalid reference path: " + ref); | ||
} | ||
} | ||
|
||
return current; | ||
} | ||
|
||
json process_schema(json schema, const json &full_schema) | ||
{ | ||
if (schema.is_object()) | ||
{ | ||
if (schema.contains("$ref")) | ||
{ | ||
std::string ref = schema["$ref"]; | ||
schema = resolve_reference(full_schema, ref); | ||
} | ||
|
||
for (auto &el : schema.items()) | ||
{ | ||
el.value() = process_schema(el.value(), full_schema); | ||
} | ||
} | ||
else if (schema.is_array()) | ||
{ | ||
for (auto &el : schema) | ||
{ | ||
el = process_schema(el, full_schema); | ||
} | ||
} | ||
|
||
return schema; | ||
} | ||
|
||
|
||
#ifndef UNIT_TEST | ||
|
||
// template <typename RESPONSE> | ||
void MockRequest(const WPEFramework::Core::JSONRPC::Message* message) | ||
{ | ||
std::cout << "Inside JSON engine MockRequest function" << std::endl; | ||
std::string methodName = capitalizeFirstChar(message->Designator.Value().c_str()); | ||
|
||
/* TODO: Add a flag here that will be set to true if the method name is found in the rpc block, u | ||
Use the flag to validate "Method not found" or other errors from SDK if applicable */ | ||
for (const auto &method : _data["methods"]) | ||
{ | ||
if (method.contains("name") && (method["name"] == methodName)) | ||
{ | ||
// Method name validation | ||
EXPECT_EQ(methodName, method["name"]); | ||
|
||
// ID Validation | ||
// TODO: Check if id gets incremented by 1 for each request | ||
std::cout << "MockRequest actual message.Id.Value(): " << message->Id.Value() << std::endl; | ||
EXPECT_THAT(message->Id, AllOf(Ge(1),Le(std::numeric_limits<int>::max()))); | ||
|
||
// Schema validation | ||
const json requestParams = json::parse(message->Parameters.Value()); | ||
std::cout << "Schema validator requestParams JSON: " << requestParams.dump() << std::endl; | ||
if(method["params"].empty()) { | ||
std::cout << "Params is empty" << std::endl; | ||
EXPECT_EQ(requestParams, "{}"_json); | ||
} | ||
else { | ||
std::cout << "Params is NOT empty" << std::endl; | ||
json_validator validator; | ||
const json openRPCParams = method["params"]; | ||
for (auto& item : openRPCParams.items()) { | ||
std::string key = item.key(); | ||
json currentSchema = item.value(); | ||
std::string paramName = currentSchema["name"]; | ||
std::cout << "paramName: " << paramName << std::endl; | ||
if (requestParams.contains(paramName)) { | ||
std::cout << "RequestParams contain paramName in rpc" << std::endl; | ||
json dereferenced_schema = process_schema(currentSchema, _data); | ||
std::cout << "schema JSON after $ref: " << dereferenced_schema.dump() << std::endl; | ||
try{ | ||
validator.set_root_schema(dereferenced_schema["schema"]); | ||
validator.validate(requestParams[paramName]); | ||
std::cout << "Schema validation succeeded" << std::endl; | ||
} | ||
catch (const std::exception &e){ | ||
FAIL() << "Schema validation error: " << e.what() << std::endl; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
template <typename RESPONSE> | ||
Firebolt::Error MockResponse(WPEFramework::Core::JSONRPC::Message &message, RESPONSE &response) | ||
{ | ||
std::cout << "Inside JSON engine MockResponse function" << std::endl; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This debug log can be removed. |
||
std::string methodName = capitalizeFirstChar(message.Designator.Value().c_str()); | ||
|
||
// Loop through the methods to find the one with the given name | ||
for (const auto &method : _data["methods"]) | ||
{ | ||
if (method.contains("name") && (method["name"] == methodName)) | ||
{ | ||
message.Result = method["examples"][0]["result"]["value"].dump(); | ||
} | ||
} | ||
return Firebolt::Error::None; | ||
} | ||
#endif | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This debug log can be removed.