Updated documentation for mock a1 tool
[ric-app/admin.git] / test / unit_test_json.cc
1 /*
2 ==================================================================================
3
4         Copyright (c) 2018-2019 AT&T Intellectual Property.
5
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
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
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 ==================================================================================
18 */
19
20 /* Author : Ashwin Sridharan
21    Date   : Sept 2019
22 */
23
24 #define CATCH_CONFIG_MAIN
25 #include <catch2/catch.hpp>
26 #include <json_handler.hpp>
27
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) + "}";
34 }
35
36
37 TEST_CASE("JSON-schema", "Schema Check"){
38   jsonHandler json_test;
39   std::vector<TrieNode> schema_path;
40   TrieNode * test_node;
41   std::string valid_schema_file = "../schemas/adm-ctrl-xapp-policy-schema.json";
42   std::string valid_sample_file = "../schemas/sample.json";
43   std::string response;
44   
45   
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));
50   }
51
52   
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));
57     
58     
59   }
60
61   // SECTION("Valid File and JSON"){
62   //   bool res = json_test.load_schema(valid_schema_file);
63   //   REQUIRE(res == TRUE);
64   // }
65
66   SECTION("Valid File and JSON, invalid schema root "){
67     schema_path.clear();
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]);
73     
74     REQUIRE_THROWS(json_test.load_schema(valid_schema_file, &schema_path[0]));
75   }
76
77   SECTION("Valid File and JSON & root"){
78     schema_path.clear();
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;
85     bool res;
86     
87     json_test.load_schema(valid_schema_file, &schema_path[0]);
88
89     gen_message(message_string, 0, 10, 20, 30);
90     res = json_test.is_valid(message_string.c_str(), message_string.length(), response);
91     REQUIRE(res == true);      
92
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);      
96
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);      
100
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);      
105     
106   }
107
108
109 }
110
111 TEST_CASE("DataContainer", "Get/set"){
112   std::string test_string = "Hello there";
113   int test_int = 101;
114   double test_real = 123.8;
115   long int test_long = 2839289;
116   unsigned int test_uint = 189;
117   unsigned long test_ulong = 29392329;
118   
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);
125     
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);
130
131
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);
136
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);
141     
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);
146
147     
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);
152
153     test.print_id();
154     test.print_value();
155
156     REQUIRE(test.is_child() == true);
157
158     TrieNode test_child("child");
159     test.add_child(&test_child);
160     REQUIRE(test.is_child() == false);
161    
162   }
163
164 }
165 TEST_CASE("JSON-getset", "Get/Set values"){
166
167     jsonHandler json_test;
168     std::vector<TrieNode> schema_path;
169     std::vector<TrieNode> key_path;
170     TrieNode  *test_node;
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;
174     bool res;
175     
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));
180     }
181     
182     SECTION("Validate buffer"){
183       schema_path.clear();
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]);
190       
191       schema_path.clear();
192       schema_path.emplace_back("message_receives_example");
193       json_test.load_buffer(valid_sample_file, &schema_path[0]);
194
195       std::string buffer = json_test.get_buffer();
196       res = json_test.is_valid(buffer.c_str(), buffer.length(), response);
197       REQUIRE(res == true);
198     }
199
200     SECTION("Get correct value from internal buffer"){
201       int res;
202       std::string message_string;
203       std::vector<TrieNode *> available_keys;
204       TrieNode test("window_length");
205       TrieNode invalid_test("Window_length");
206       
207       // Try with no buffer loaded 
208       res = json_test.get_values(response, &test, available_keys);
209       REQUIRE(res == -4);
210
211       // Try with buffer loaded
212       schema_path.clear();
213       schema_path.emplace_back("message_receives_example");
214       json_test.load_buffer(valid_sample_file, &schema_path[0]);
215
216       available_keys.clear();
217       res = json_test.get_values(response, &test, available_keys);
218       REQUIRE (res == 1);
219       REQUIRE(available_keys.size() == 1);
220       REQUIRE(available_keys[0]->get_value()->value.i == 10);
221       
222       available_keys.clear();
223       res = json_test.get_values(response, &invalid_test, available_keys);
224       REQUIRE (res == 0);
225       REQUIRE(available_keys.size() == 0);
226
227       
228     }
229     SECTION("Get values from supplied buffer"){
230       int res;
231       std::string message_string;
232       std::vector<TrieNode *> available_keys;
233       
234
235       bool enforce = true;
236       int blocking_rate = 10;
237       int trigger_threshold = 5000;
238       int window_length = 25;
239
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);
244       REQUIRE(res == 0);
245       REQUIRE(available_keys.size() == 1);
246       REQUIRE(available_keys[0]->get_value()->value.i == 25);
247  
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);
252       REQUIRE(res == -3);
253       REQUIRE(available_keys.size() == 0);
254
255       
256     }
257     
258
259     SECTION("Get/Set  from memory"){
260       int res;
261       std::vector<TrieNode *> keys;
262
263       TrieNode test1("blocking_rate");
264       int blocking_rate = 23;
265       test1.set_value(blocking_rate);
266
267       TrieNode test2("window_length");
268       int window_length = 45;
269       test2.set_value(window_length);
270       
271       schema_path.clear();
272       schema_path.emplace_back("message_receives_example");
273       json_test.load_buffer(valid_sample_file, &schema_path[0]);
274
275       keys.clear();
276       keys.push_back(&test1);
277       keys.push_back(&test2);
278       res = json_test.set_values(response,  keys);
279       REQUIRE(res == 0);
280
281       keys.clear();
282       std::string message_string(response);
283       res = json_test.get_values(message_string.c_str(), message_string.length(), response, &test1, keys);
284       REQUIRE(res == 0);
285       REQUIRE(keys.size() == 1);
286       REQUIRE(keys[0]->get_value()->value.i == blocking_rate);
287
288       keys.clear();
289       res = json_test.get_values(message_string.c_str(), message_string.length(), response, &test2, keys);
290       REQUIRE(res == 0);
291       REQUIRE(keys.size() == 1);
292       REQUIRE(keys[0]->get_value()->value.i == window_length);
293
294     }
295
296
297     SECTION("Get/Set from buffer "){
298       int res;
299       std::vector<TrieNode *> keys;
300       std::string message_string;
301       
302       bool enforce = true;
303       int blocking_rate = 10;
304       int trigger_threshold = 5000;
305       int window_length = 25;
306
307       gen_message(message_string, enforce, blocking_rate, trigger_threshold, window_length);
308
309       TrieNode test_node("blocking_rate");
310       blocking_rate = 33;
311       test_node.set_value(blocking_rate);
312       
313
314       keys.clear();
315       keys.push_back(&test_node);
316       res = json_test.set_values(message_string.c_str(), message_string.length(), response,  keys);
317       REQUIRE(res == 0);
318
319       keys.clear();
320       message_string = response;
321       res = json_test.get_values(message_string.c_str(), message_string.length(), response, &test_node, keys);
322       REQUIRE(res == 0);
323       REQUIRE(keys.size() == 1);
324       REQUIRE(keys[0]->get_value()->value.i == blocking_rate);
325       
326     }
327
328
329     SECTION("multi-level get/set"){
330       std::vector<TrieNode *> keys;
331       schema_path.clear();
332       schema_path.emplace_back("test1");
333       schema_path.emplace_back(1);
334       schema_path[0].add_child(&schema_path[1]);
335       
336       std::string sample_file = "test-data/test_sample.json";
337       REQUIRE_THROWS(json_test.load_buffer(sample_file, &schema_path[0]));
338
339       schema_path.clear();
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]);
345       
346       json_test.load_buffer(sample_file, &schema_path[0]);
347       std::string buffer = json_test.get_buffer();
348       
349       TrieNode test("test5");
350       keys.clear();
351       res = json_test.get_values(response, &test, keys);
352       REQUIRE(res == 1);
353       REQUIRE(keys.size() == 1);
354       REQUIRE(keys[0]->get_value()->value.s == "new target");
355     }
356       
357 }
358
359
360
361