libamxo  4.3.4
Object Definition Language (ODL) parsing
test_populate_behavior.c
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 #include <sys/time.h>
55 #include <sys/resource.h>
56 #include <sys/types.h>
57 #include <sys/stat.h>
58 
59 #include <string.h>
60 #include <stdlib.h>
61 #include <stdio.h>
62 #include <stdarg.h>
63 #include <stddef.h>
64 #include <setjmp.h>
65 #include <inttypes.h>
66 #include <limits.h>
67 #include <unistd.h>
68 #include <fcntl.h>
69 #include <cmocka.h>
70 
71 #include <amxc/amxc.h>
72 #include <amxp/amxp_signal.h>
73 #include <amxd/amxd_dm.h>
74 #include <amxd/amxd_object.h>
75 #include <amxd/amxd_parameter.h>
76 #include <amxd/amxd_transaction.h>
77 #include <amxo/amxo.h>
78 
79 
80 #include "test_populate_behavior.h"
81 
82 #include <amxc/amxc_macros.h>
84  amxd_dm_t dm;
85  amxo_parser_t parser;
86  const char* main_odl =
87  "%define { object MyObject { string Text; } }\n"
88  "%populate { object MyObject { parameter OtherParam = 10; } } ";
89 
90  amxd_dm_init(&dm);
91  amxo_parser_init(&parser);
92 
93  assert_int_not_equal(amxo_parser_parse_string(&parser, main_odl, amxd_dm_get_root(&dm)), 0);
94  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_parameter_not_found);
95 
96  amxd_dm_clean(&dm);
97  amxo_parser_clean(&parser);
98 }
99 
101  amxd_object_t* object = NULL;
102  amxd_param_t* param = NULL;
103  amxd_dm_t dm;
104  amxo_parser_t parser;
105  const char* main_odl =
106  "%config { populate-behavior = { unknown-parameter = \"add\" }; }\n"
107  "%define { object MyObject { string Text; object Multi[] { string Text; } } }\n"
108  "%populate { object MyObject { parameter OtherParam = 10; object Multi { parameter Other = 10; } } }\n";
109 
110  amxd_dm_init(&dm);
111  amxo_parser_init(&parser);
112 
113  assert_int_equal(amxo_parser_parse_string(&parser, main_odl, amxd_dm_get_root(&dm)), 0);
114  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_ok);
115 
116  object = amxd_dm_findf(&dm, "MyObject");
117  param = amxd_object_get_param_def(object, "OtherParam");
118  assert_ptr_not_equal(param, NULL);
119  assert_int_equal(amxd_param_get_type(param), AMXC_VAR_ID_INT64);
120 
121  assert_int_equal(amxd_object_get_value(int64_t, object, "OtherParam", NULL), 10);
122 
123  amxd_dm_clean(&dm);
124  amxo_parser_clean(&parser);
125 }
126 
128  amxd_object_t* object = NULL;
129  amxd_param_t* param1 = NULL;
130  amxd_param_t* param2 = NULL;
131  int64_t val1 = 0;
132  int64_t val2 = 0;
133  amxd_status_t s1;
134  amxd_status_t s2;
135  amxd_dm_t dm;
136  amxo_parser_t parser;
137  const char* main_odl =
138  "%config { populate-behavior = { unknown-parameter = \"add\" }; }\n"
139  "%define { object MyObject[] { int64 DefParam; } }\n"
140  "%populate { object MyObject { instance add(6,'key1') { parameter DefParam = 66; parameter OtherParam = 10; } } }\n";
141 
142  amxd_dm_init(&dm);
143  amxo_parser_init(&parser);
144 
145  assert_int_equal(amxo_parser_parse_string(&parser, main_odl, amxd_dm_get_root(&dm)), 0);
146  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_ok);
147 
148  object = amxd_dm_findf(&dm, "MyObject.key1");
149  param1 = amxd_object_get_param_def(object, "DefParam");
150  param2 = amxd_object_get_param_def(object, "OtherParam");
151  assert_ptr_not_equal(param1, NULL);
152  assert_ptr_not_equal(param2, NULL);
153  assert_int_equal(amxd_param_get_type(param1), AMXC_VAR_ID_INT64);
154  assert_int_equal(amxd_param_get_type(param2), AMXC_VAR_ID_INT64);
155 
156  val1 = amxd_object_get_value(int64_t, object, "DefParam", &s1);
157  assert_int_equal(s1, amxd_status_ok);
158  assert_int_equal(val1, 66);
159 
160  val2 = amxd_object_get_value(int64_t, object, "OtherParam", &s2);
161  assert_int_equal(s2, amxd_status_ok);
162  assert_int_equal(val2, 10);
163 
164  amxd_dm_clean(&dm);
165  amxo_parser_clean(&parser);
166 }
167 
169  amxd_object_t* object = NULL;
170  amxd_param_t* param = NULL;
171  amxd_dm_t dm;
172  amxo_parser_t parser;
173  const char* main_odl =
174  "%config { populate-behavior = { unknown-parameter = \"warning\" }; }\n"
175  "%define { object MyObject { string Text; } }\n"
176  "%populate { object MyObject { parameter OtherParam = 10; } }\n";
177 
178  amxd_dm_init(&dm);
179  amxo_parser_init(&parser);
180 
181  assert_int_equal(amxo_parser_parse_string(&parser, main_odl, amxd_dm_get_root(&dm)), 0);
182  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_ok);
183  object = amxd_dm_findf(&dm, "MyObject");
184  param = amxd_object_get_param_def(object, "OtherParam");
185  assert_ptr_equal(param, NULL);
186 
187  amxd_dm_clean(&dm);
188  amxo_parser_clean(&parser);
189 }
190 
192  amxd_dm_t dm;
193  amxo_parser_t parser;
194  const char* main_odl =
195  "%define {"
196  " object MyObject {"
197  " object MyTemplate[] {"
198  " string Text;"
199  " }"
200  " }"
201  "}"
202  "%populate {"
203  " object MyObject.MyTemplate {"
204  " instance add(1);"
205  " instance add(2);"
206  " instance add(1);"
207  " }"
208  "}";
209 
210  amxd_dm_init(&dm);
211  amxo_parser_init(&parser);
212 
213  assert_int_not_equal(amxo_parser_parse_string(&parser, main_odl, amxd_dm_get_root(&dm)), 0);
214  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_duplicate);
215 
216  amxd_dm_clean(&dm);
217  amxo_parser_clean(&parser);
218 }
219 
221  amxd_dm_t dm;
222  amxo_parser_t parser;
223  amxd_object_t* object = NULL;
224  char* value = NULL;
225  const char* main_odl =
226  "%config { populate-behavior = { duplicate-instance = \"update\" }; }\n"
227  "%define {\n"
228  " object MyObject {\n"
229  " object MyTemplate[] {\n"
230  " string Text;\n"
231  " }\n"
232  " }\n"
233  "}\n"
234  "%populate {\n"
235  " object MyObject.MyTemplate {\n"
236  " instance add(1) { parameter Text = \"Test\"; }\n"
237  " instance add(2);\n"
238  " instance add(1) {\n"
239  " parameter Text = \"HALLO\";\n"
240  " }\n"
241  " }\n"
242  "}\n";
243 
244  amxd_dm_init(&dm);
245  amxo_parser_init(&parser);
246 
247  assert_int_equal(amxo_parser_parse_string(&parser, main_odl, amxd_dm_get_root(&dm)), 0);
248  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_ok);
249  object = amxd_dm_findf(&dm, "MyObject.MyTemplate.1");
250  value = amxd_object_get_value(cstring_t, object, "Text", NULL);
251  assert_string_equal(value, "HALLO");
252 
253  free(value);
254  amxd_dm_clean(&dm);
255  amxo_parser_clean(&parser);
256 }
257 
259  amxd_dm_t dm;
260  amxo_parser_t parser;
261  amxd_object_t* object = NULL;
262  uint32_t value = 0;
263  const char* main_odl =
264  "%config { populate-behavior = { duplicate-instance = \"update\" }; }\n"
265  "%define {\n"
266  " object MyObject {\n"
267  " object MyTemplate[] {\n"
268  " %key string Text;\n"
269  " uint32 Number;"
270  " }\n"
271  " }\n"
272  "}\n"
273  "%populate {\n"
274  " object MyObject.MyTemplate {\n"
275  " instance add(Text = \"Value1\") { parameter Number = 666; }\n"
276  " instance add(Text = \"Value2\");\n"
277  " instance add(Text = \"Value1\") {\n"
278  " parameter Number = 1234;\n"
279  " }\n"
280  " }\n"
281  "}\n";
282 
283  amxd_dm_init(&dm);
284  amxo_parser_init(&parser);
285 
286  assert_int_equal(amxo_parser_parse_string(&parser, main_odl, amxd_dm_get_root(&dm)), 0);
287  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_ok);
288  object = amxd_dm_findf(&dm, "MyObject.MyTemplate.1");
289  value = amxd_object_get_value(uint32_t, object, "Number", NULL);
290  assert_int_equal(value, 1234);
291 
292  amxd_dm_clean(&dm);
293  amxo_parser_clean(&parser);
294 }
295 
296 
298  amxd_dm_t dm;
299  amxo_parser_t parser;
300  amxd_object_t* templ = NULL;
301  amxd_object_t* object = NULL;
302  amxd_status_t status;
303  const char* main_odl =
304  "%define {\n"
305  " object MyObjectKeyTest {\n"
306  " object Node[] {\n"
307  " %key uint32 Key1;\n"
308  " %key uint32 Key2;\n"
309  " }\n"
310  " }\n"
311  "}\n";
312 
313  amxd_dm_init(&dm);
314  amxo_parser_init(&parser);
315 
316  assert_int_equal(amxo_parser_parse_string(&parser, main_odl, amxd_dm_get_root(&dm)), 0);
317  assert_int_equal(amxo_parser_get_status(&parser), amxd_status_ok);
318 
319  templ = amxd_dm_findf(&dm, "MyObjectKeyTest.Node");
320  assert_ptr_not_equal(templ, NULL);
321 
322  amxd_trans_t trans;
323  amxd_trans_init(&trans);
324  amxd_trans_select_object(&trans, templ);
325  amxd_trans_add_inst(&trans, 1, "N1");
326  amxd_trans_set_value(uint32_t, &trans, "Key1", 0);
327  amxd_trans_set_value(uint32_t, &trans, "Key2", 0);
328  status = amxd_trans_apply(&trans, &dm);
329  assert_int_equal(status, amxd_status_ok);
330  amxd_trans_clean(&trans);
331 
332  object = amxd_object_findf(templ, "[Key1 == 0].");
333  assert_ptr_not_equal(object, NULL);
334 
335  amxd_trans_init(&trans);
336  amxd_trans_select_object(&trans, templ);
337  amxd_trans_add_inst(&trans, 2, "N2");
338  amxd_trans_set_value(uint32_t, &trans, "Key1", 1);
339  amxd_trans_set_value(uint32_t, &trans, "Key2", 0);
340  status = amxd_trans_apply(&trans, &dm);
341  assert_int_equal(status, amxd_status_ok);
342  amxd_trans_clean(&trans);
343 
344  object = amxd_object_findf(templ, "[Key1 == 1].");
345  assert_ptr_not_equal(object, NULL);
346 
347  amxd_dm_clean(&dm);
348  amxo_parser_clean(&parser);
349 }
Ambiorix ODL parser header file.
int amxo_parser_parse_string(amxo_parser_t *parser, const char *text, amxd_object_t *object)
Parses a string containing a valid ODL part.
void amxo_parser_clean(amxo_parser_t *parser)
Cleans up the odl parser instance.
static amxd_status_t amxo_parser_get_status(amxo_parser_t *parser)
Get the status of the odl parser.
Definition: amxo.h:414
int amxo_parser_init(amxo_parser_t *parser)
Initializes a new odl parser instance.
The ODL parser structure.
Definition: amxo_types.h:245
#define UNUSED
Definition: test_issue_48.c:84
void test_duplicate_instance_can_update(UNUSED void **state)
void test_duplicate_instance_with_keys_can_update(UNUSED void **state)
void test_duplicate_instance_default_behavior(UNUSED void **state)
void test_none_existing_param_can_add(UNUSED void **state)
void test_transactions_with_two_keys(UNUSED void **state)
void test_none_existing_param_default_behavior(UNUSED void **state)
void test_none_existing_instance_param_can_add(UNUSED void **state)
void test_none_existing_param_warning(UNUSED void **state)