2 ==================================================================================
4 Copyright (c) 2018-2019 AT&T Intellectual Property.
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================================
20 /* Author : Ashwin Sridharan
24 #define CATCH_CONFIG_MAIN
25 #include <catch2/catch.hpp>
26 #include <json_handler.hpp>
28 void gen_message(std::string & message_string, bool enforce, int blocking_rate, int trigger_threshold, int window_length){
29 std::string enforce_string ((enforce? "true":"false"));
30 message_string = "{\"enforce\":" + enforce_string + ",";
31 message_string += std::string("\"blocking_rate\":") + std::to_string(blocking_rate) + ",";
32 message_string += std::string("\"window_length\":") + std::to_string(window_length) + ",";
33 message_string += std::string("\"trigger_threshold\":") + std::to_string(trigger_threshold) + "}";
37 TEST_CASE("JSON-schema", "Schema Check"){
38 jsonHandler json_test;
39 std::vector<TrieNode> schema_path;
41 std::string valid_schema_file = "../schemas/adm-ctrl-xapp-policy-schema.json";
42 std::string valid_sample_file = "../schemas/sample.json";
46 SECTION("Invalid Schema File"){
47 REQUIRE_THROWS( json_test.load_schema("hello there"));
48 REQUIRE_THROWS( json_test.load_schema("hello there", NULL));
49 REQUIRE_THROWS( json_test.load_schema("hello there", test_node));
53 SECTION("Invalid JSON"){
54 REQUIRE_THROWS( json_test.load_schema("invalid_json.txt"));
55 REQUIRE_THROWS( json_test.load_schema("invalid_json.txt", NULL));
56 REQUIRE_THROWS( json_test.load_schema("invalid_json.txt", test_node));
61 // SECTION("Valid File and JSON"){
62 // bool res = json_test.load_schema(valid_schema_file);
63 // REQUIRE(res == TRUE);
66 SECTION("Valid File and JSON, invalid schema root "){
68 schema_path.emplace_back("controlS");
69 schema_path.emplace_back((int)0);
70 schema_path.emplace_back("message_receives_payload_schema");
71 schema_path[0].add_child(&schema_path[1]);
72 schema_path[1].add_child(&schema_path[2]);
74 REQUIRE_THROWS(json_test.load_schema(valid_schema_file, &schema_path[0]));
77 SECTION("Valid File and JSON & root"){
79 schema_path.emplace_back("controls");
80 schema_path.emplace_back((int)0);
81 schema_path.emplace_back("message_receives_payload_schema");
82 schema_path[0].add_child(&schema_path[1]);
83 schema_path[1].add_child(&schema_path[2]);
84 std::string message_string;
87 json_test.load_schema(valid_schema_file, &schema_path[0]);
89 gen_message(message_string, 0, 10, 20, 30);
90 res = json_test.is_valid(message_string.c_str(), message_string.length(), response);
93 gen_message(message_string, 0, 10, -5, 30);
94 res = json_test.is_valid(message_string.c_str(), message_string.length(), response);
95 REQUIRE(res == false);
97 gen_message(message_string, 0, 120, 10, 5);
98 res = json_test.is_valid(message_string.c_str(), message_string.length(), response);
99 REQUIRE(res == false);
101 gen_message(message_string, 0, 20, 10, 5);
102 message_string = message_string + "}";
103 res = json_test.is_valid(message_string.c_str(), message_string.length(), response);
104 REQUIRE(res == false);
111 TEST_CASE("DataContainer", "Get/set"){
112 std::string test_string = "Hello there";
114 double test_real = 123.8;
115 long int test_long = 2839289;
116 unsigned int test_uint = 189;
117 unsigned long test_ulong = 29392329;
119 SECTION("Validate data types"){
120 TrieNode test("Test Node");
121 test.set_value(test_string);
122 const DataContainer * val = test.get_value();
123 REQUIRE(val->tag == DataContainer::Types::str);
124 REQUIRE(val->value.s == test_string);
126 test.set_value(test_int);
127 val = test.get_value();
128 REQUIRE(val->tag == DataContainer::Types::integer);
129 REQUIRE(val->value.i == test_int);
132 test.set_value(test_uint);
133 val = test.get_value();
134 REQUIRE(val->tag == DataContainer::Types::uinteger);
135 REQUIRE(val->value.ui == test_uint);
137 test.set_value(test_real);
138 val = test.get_value();
139 REQUIRE(val->tag == DataContainer::Types::real);
140 REQUIRE(val->value.f == test_real);
142 test.set_value(test_long);
143 val = test.get_value();
144 REQUIRE(val->tag == DataContainer::Types::big_integer);
145 REQUIRE(val->value.l == test_long);
148 test.set_value(test_ulong);
149 val = test.get_value();
150 REQUIRE(val->tag == DataContainer::Types::ubig_integer);
151 REQUIRE(val->value.ul == test_ulong);
156 REQUIRE(test.is_child() == true);
158 TrieNode test_child("child");
159 test.add_child(&test_child);
160 REQUIRE(test.is_child() == false);
165 TEST_CASE("JSON-getset", "Get/Set values"){
167 jsonHandler json_test;
168 std::vector<TrieNode> schema_path;
169 std::vector<TrieNode> key_path;
171 std::string valid_schema_file = "../schemas/adm-ctrl-xapp-policy-schema.json";
172 std::string valid_sample_file = "../schemas/samples.json";
173 std::string response;
176 SECTION("Invalid buffer file"){
177 REQUIRE_THROWS( json_test.load_buffer("hello there"));
178 REQUIRE_THROWS( json_test.load_buffer("hello there", NULL));
179 REQUIRE_THROWS( json_test.load_buffer("hello there", test_node));
182 SECTION("Validate buffer"){
184 schema_path.emplace_back("controls");
185 schema_path.emplace_back((int)0);
186 schema_path.emplace_back("message_receives_payload_schema");
187 schema_path[0].add_child(&schema_path[1]);
188 schema_path[1].add_child(&schema_path[2]);
189 json_test.load_schema(valid_schema_file, &schema_path[0]);
192 schema_path.emplace_back("message_receives_example");
193 json_test.load_buffer(valid_sample_file, &schema_path[0]);
195 std::string buffer = json_test.get_buffer();
196 res = json_test.is_valid(buffer.c_str(), buffer.length(), response);
197 REQUIRE(res == true);
200 SECTION("Get correct value from internal buffer"){
202 std::string message_string;
203 std::vector<TrieNode *> available_keys;
204 TrieNode test("window_length");
205 TrieNode invalid_test("Window_length");
207 // Try with no buffer loaded
208 res = json_test.get_values(response, &test, available_keys);
211 // Try with buffer loaded
213 schema_path.emplace_back("message_receives_example");
214 json_test.load_buffer(valid_sample_file, &schema_path[0]);
216 available_keys.clear();
217 res = json_test.get_values(response, &test, available_keys);
219 REQUIRE(available_keys.size() == 1);
220 REQUIRE(available_keys[0]->get_value()->value.i == 10);
222 available_keys.clear();
223 res = json_test.get_values(response, &invalid_test, available_keys);
225 REQUIRE(available_keys.size() == 0);
229 SECTION("Get values from supplied buffer"){
231 std::string message_string;
232 std::vector<TrieNode *> available_keys;
236 int blocking_rate = 10;
237 int trigger_threshold = 5000;
238 int window_length = 25;
240 TrieNode test("window_length");
241 available_keys.clear();
242 gen_message(message_string, enforce, blocking_rate, trigger_threshold, window_length);
243 res = json_test.get_values(message_string.c_str(), message_string.length(), response, &test, available_keys);
245 REQUIRE(available_keys.size() == 1);
246 REQUIRE(available_keys[0]->get_value()->value.i == 25);
248 TrieNode invalid_test("Window_length");
249 available_keys.clear();
250 gen_message(message_string, enforce, blocking_rate, trigger_threshold, window_length);
251 res = json_test.get_values(message_string.c_str(), message_string.length(), response, &invalid_test, available_keys);
253 REQUIRE(available_keys.size() == 0);
259 SECTION("Get/Set from memory"){
261 std::vector<TrieNode *> keys;
263 TrieNode test1("blocking_rate");
264 int blocking_rate = 23;
265 test1.set_value(blocking_rate);
267 TrieNode test2("window_length");
268 int window_length = 45;
269 test2.set_value(window_length);
272 schema_path.emplace_back("message_receives_example");
273 json_test.load_buffer(valid_sample_file, &schema_path[0]);
276 keys.push_back(&test1);
277 keys.push_back(&test2);
278 res = json_test.set_values(response, keys);
282 std::string message_string(response);
283 res = json_test.get_values(message_string.c_str(), message_string.length(), response, &test1, keys);
285 REQUIRE(keys.size() == 1);
286 REQUIRE(keys[0]->get_value()->value.i == blocking_rate);
289 res = json_test.get_values(message_string.c_str(), message_string.length(), response, &test2, keys);
291 REQUIRE(keys.size() == 1);
292 REQUIRE(keys[0]->get_value()->value.i == window_length);
297 SECTION("Get/Set from buffer "){
299 std::vector<TrieNode *> keys;
300 std::string message_string;
303 int blocking_rate = 10;
304 int trigger_threshold = 5000;
305 int window_length = 25;
307 gen_message(message_string, enforce, blocking_rate, trigger_threshold, window_length);
309 TrieNode test_node("blocking_rate");
311 test_node.set_value(blocking_rate);
315 keys.push_back(&test_node);
316 res = json_test.set_values(message_string.c_str(), message_string.length(), response, keys);
320 message_string = response;
321 res = json_test.get_values(message_string.c_str(), message_string.length(), response, &test_node, keys);
323 REQUIRE(keys.size() == 1);
324 REQUIRE(keys[0]->get_value()->value.i == blocking_rate);
329 SECTION("multi-level get/set"){
330 std::vector<TrieNode *> keys;
332 schema_path.emplace_back("test1");
333 schema_path.emplace_back(1);
334 schema_path[0].add_child(&schema_path[1]);
336 std::string sample_file = "test-data/test_sample.json";
337 REQUIRE_THROWS(json_test.load_buffer(sample_file, &schema_path[0]));
340 schema_path.emplace_back("test1");
341 schema_path.emplace_back(1);
342 schema_path.emplace_back("test4");
343 schema_path[0].add_child(&schema_path[1]);
344 schema_path[1].add_child(&schema_path[2]);
346 json_test.load_buffer(sample_file, &schema_path[0]);
347 std::string buffer = json_test.get_buffer();
349 TrieNode test("test5");
351 res = json_test.get_values(response, &test, keys);
353 REQUIRE(keys.size() == 1);
354 REQUIRE(keys[0]->get_value()->value.s == "new target");