libamxd  6.4.1
Data Model Manager
test_amxd_transaction_sub.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 
55 #include <stdlib.h>
56 #include <stdarg.h>
57 #include <stddef.h>
58 #include <string.h>
59 #include <setjmp.h>
60 #include <fcntl.h>
61 #include <unistd.h>
62 #include <signal.h>
63 #include <cmocka.h>
64 
65 #include <amxc/amxc.h>
66 #include <amxp/amxp_signal.h>
67 #include <amxp/amxp_slot.h>
68 
69 #include <amxd/amxd_common.h>
70 #include <amxd/amxd_dm.h>
71 #include <amxd/amxd_object.h>
72 #include <amxd/amxd_parameter.h>
73 #include <amxd/amxd_function.h>
74 #include <amxd/amxd_action.h>
75 #include <amxd/amxd_transaction.h>
76 
78 
79 #include <amxc/amxc_macros.h>
80 static amxd_dm_t dm;
81 
82 static void test_check_change_event(const char* const sig_name,
83  const amxc_var_t* const data,
84  UNUSED void* const priv) {
85  const char* path = GET_CHAR(data, "path");
86  const char* param_from = GETP_CHAR(data, "parameters.Text.from");
87  const char* param_to = GETP_CHAR(data, "parameters.Text.to");
88  amxc_var_dump(data, STDOUT_FILENO);
89 
90  check_expected(sig_name);
91  check_expected(path);
92  check_expected(param_from);
93  check_expected(param_to);
94 }
95 
96 static void test_check_event(const char* const sig_name,
97  UNUSED const amxc_var_t* const data,
98  UNUSED void* const priv) {
99  amxc_var_dump(data, STDOUT_FILENO);
100 
101  check_expected(sig_name);
102 }
103 
104 void test_build_dm(void) {
105  amxd_object_t* object = NULL;
106  amxd_object_t* templ = NULL;
107  amxd_object_t* child = NULL;
108  amxd_param_t* param = NULL;
109 
110  assert_int_equal(amxd_dm_init(&dm), 0);
111 
112  // object Top1 {
113  assert_int_equal(amxd_object_new(&object, amxd_object_singleton, "Top1"), 0);
114  assert_int_equal(amxd_dm_add_root_object(&dm, object), 0);
115  // string Text;
116  assert_int_equal(amxd_param_new(&param, "Text", AMXC_VAR_ID_CSTRING), 0);
117  assert_int_equal(amxd_object_add_param(object, param), 0);
118 
119  // object Child {
120  assert_int_equal(amxd_object_new(&child, amxd_object_singleton, "Child"), 0);
121  assert_int_equal(amxd_object_add_object(object, child), 0);
122  // string Text;
123  assert_int_equal(amxd_param_new(&param, "Text", AMXC_VAR_ID_CSTRING), 0);
124  assert_int_equal(amxd_object_add_param(child, param), 0);
125  // }
126  // }
127 
128  // object Top2 {
129  assert_int_equal(amxd_object_new(&object, amxd_object_singleton, "Top2"), 0);
130  assert_int_equal(amxd_dm_add_root_object(&dm, object), 0);
131  // string Text;
132  assert_int_equal(amxd_param_new(&param, "Text", AMXC_VAR_ID_CSTRING), 0);
133  assert_int_equal(amxd_object_add_param(object, param), 0);
134 
135  // object Child[] {
136  assert_int_equal(amxd_object_new(&templ, amxd_object_template, "Child"), 0);
137  assert_int_equal(amxd_object_add_object(object, templ), 0);
138  // string Text;
139  assert_int_equal(amxd_param_new(&param, "Text", AMXC_VAR_ID_CSTRING), 0);
140  assert_int_equal(amxd_object_add_param(templ, param), 0);
141  // uint32 Number;
142  assert_int_equal(amxd_param_new(&param, "Number", AMXC_VAR_ID_UINT32), 0);
143  assert_int_equal(amxd_object_add_param(templ, param), 0);
144  // object Child {
145  assert_int_equal(amxd_object_new(&child, amxd_object_singleton, "Child"), 0);
146  assert_int_equal(amxd_object_add_object(templ, child), 0);
147  // string Text;
148  assert_int_equal(amxd_param_new(&param, "Text", AMXC_VAR_ID_CSTRING), 0);
149  assert_int_equal(amxd_object_add_param(child, param), 0);
150  // uint32 Number;
151  assert_int_equal(amxd_param_new(&param, "Number", AMXC_VAR_ID_UINT32), 0);
152  assert_int_equal(amxd_object_add_param(child, param), 0);
153  // }
154  // }
155  // }
156 }
157 
159  return &dm;
160 }
161 
162 void test_clean_dm(void) {
163  amxd_dm_clean(&dm);
164 }
165 
166 void test_amxd_can_set_sub_object_params(UNUSED void** state) {
167  amxd_trans_t transaction;
168  amxd_object_t* obj = NULL;
169  char* text = NULL;
171 
172  test_build_dm();
173 
174  // read all pending events
175  while(amxp_signal_read() == 0) {
176  }
177 
178  assert_int_equal(amxd_trans_init(&transaction), 0);
179  assert_int_equal(amxd_trans_select_pathf(&transaction, "Top1"), 0);
180  assert_int_equal(amxd_trans_set_value(cstring_t, &transaction, "Text", "Hello"), 0);
181  assert_int_equal(amxd_trans_set_value(cstring_t, &transaction, "Child.Text", "World"), 0);
182 
183  amxd_trans_dump(&transaction, STDOUT_FILENO, false);
184  assert_int_equal(amxd_trans_apply(&transaction, test_get_dm()), 0);
185  amxd_trans_clean(&transaction);
186 
187  // check Top1.Text == "Hello"
188  obj = amxd_dm_findf(test_get_dm(), "Top1.");
189  assert_non_null(obj);
190  text = amxd_object_get_value(cstring_t, obj, "Text", &status);
191  assert_non_null(text);
192  assert_string_equal(text, "Hello");
193  free(text);
194 
195  // check Top1.Child.Text == "World"
196  obj = amxd_dm_findf(test_get_dm(), "Top1.Child.");
197  assert_non_null(obj);
198  text = amxd_object_get_value(cstring_t, obj, "Text", &status);
199  assert_non_null(text);
200  assert_string_equal(text, "World");
201  free(text);
202 }
203 
205  amxd_dm_t* dm = NULL;
206  dm = test_get_dm();
207 
208  assert_int_equal(amxp_slot_connect(&dm->sigmngr,
209  "dm:object-changed",
210  NULL,
212  NULL), 0);
213 
214  expect_string(test_check_change_event, sig_name, "dm:object-changed");
215  expect_string(test_check_change_event, path, "Top1.Child.");
216  expect_string(test_check_change_event, param_from, "");
217  expect_string(test_check_change_event, param_to, "World");
218 
219  expect_string(test_check_change_event, sig_name, "dm:object-changed");
220  expect_string(test_check_change_event, path, "Top1.");
221  expect_string(test_check_change_event, param_from, "");
222  expect_string(test_check_change_event, param_to, "Hello");
223 
224  while(amxp_signal_read() == 0) {
225  }
226 
227  test_clean_dm();
228 }
229 
231  amxd_trans_t transaction;
232  amxd_object_t* obj = NULL;
233  char* text = NULL;
235 
236  test_build_dm();
237 
238  // read all pending events
239  while(amxp_signal_read() == 0) {
240  }
241 
242  assert_int_equal(amxd_trans_init(&transaction), 0);
243  assert_int_equal(amxd_trans_select_pathf(&transaction, "Top2.Child."), 0);
244  assert_int_equal(amxd_trans_add_inst(&transaction, 0, NULL), 0);
245  assert_int_equal(amxd_trans_set_value(cstring_t, &transaction, "Text", "Hello"), 0);
246  assert_int_equal(amxd_trans_set_value(cstring_t, &transaction, "Child.Text", "World"), 0);
247  assert_int_equal(amxd_trans_set_value(uint32_t, &transaction, "Number", 100), 0);
248  assert_int_equal(amxd_trans_set_value(uint32_t, &transaction, "Child.Number", 101), 0);
249 
250  amxd_trans_dump(&transaction, STDOUT_FILENO, false);
251  assert_int_equal(amxd_trans_apply(&transaction, test_get_dm()), 0);
252  amxd_trans_clean(&transaction);
253 
254  obj = amxd_dm_findf(test_get_dm(), "Top2.Child.1.");
255  assert_non_null(obj);
256  text = amxd_object_get_value(cstring_t, obj, "Text", &status);
257  assert_non_null(text);
258  assert_string_equal(text, "Hello");
259  assert_int_equal(amxd_object_get_value(uint32_t, obj, "Number", &status), 100);
260  free(text);
261 
262  obj = amxd_dm_findf(test_get_dm(), "Top2.Child.1.Child.");
263  assert_non_null(obj);
264  text = amxd_object_get_value(cstring_t, obj, "Text", &status);
265  assert_non_null(text);
266  assert_string_equal(text, "World");
267  assert_int_equal(amxd_object_get_value(uint32_t, obj, "Number", &status), 101);
268  free(text);
269 }
270 
271 void test_amxd_check_two_events_are_send(UNUSED void** state) {
272  amxd_dm_t* dm = NULL;
273  dm = test_get_dm();
274 
275  assert_int_equal(amxp_slot_connect(&dm->sigmngr,
276  "dm:instance-added",
277  NULL,
279  NULL), 0);
280 
281  assert_int_equal(amxp_slot_connect(&dm->sigmngr,
282  "dm:object-added",
283  NULL,
285  NULL), 0);
286 
287  expect_string(test_check_event, sig_name, "dm:instance-added");
288  expect_string(test_check_event, sig_name, "dm:object-added");
289 
290  while(amxp_signal_read() == 0) {
291  }
292 
293  test_clean_dm();
294 }
Ambiorix Data Model Default actions header file.
Ambiorix Data Model API header file.
amxd_object_t * amxd_dm_findf(amxd_dm_t *const dm, const char *abs_path,...) __attribute__((format(printf
Ambiorix Data Model RPC methods API header file.
Ambiorix Data Model API header file.
amxd_status_t amxd_param_new(amxd_param_t **param, const char *name, const uint32_t type)
Ambiorix Data Model API header file.
enum _amxd_status amxd_status_t
@ amxd_status_ok
Definition: amxd_types.h:78
@ amxd_object_template
Definition: amxd_types.h:183
@ amxd_object_singleton
Definition: amxd_types.h:181
amxd_status_t amxd_dm_add_root_object(amxd_dm_t *const dm, amxd_object_t *const object)
Adds an object to the root of the data model.
Definition: amxd_dm.c:418
amxd_status_t amxd_dm_init(amxd_dm_t *dm)
Initializes a data model structure.
Definition: amxd_dm.c:334
void amxd_dm_clean(amxd_dm_t *dm)
Cleans a data model structure.
Definition: amxd_dm.c:365
amxd_status_t amxd_object_add_object(amxd_object_t *const parent, amxd_object_t *const child)
Adds an object in the data model tree.
Definition: amxd_object.c:207
#define amxd_object_get_value(type, object, name, status)
Helper macro for getting a value.
amxd_status_t amxd_object_add_param(amxd_object_t *const object, amxd_param_t *const param)
Adds a parameter definition to an object.
amxd_status_t amxd_object_new(amxd_object_t **object, const amxd_object_type_t type, const char *name)
Data model object constructor function.
Definition: amxd_object.c:185
void amxd_trans_dump(const amxd_trans_t *const trans, const int fd, const bool reverse)
Dumps the transaction to a file descriptor.
void amxd_trans_clean(amxd_trans_t *const trans)
Cleans the transaction object.
amxd_status_t amxd_trans_apply(amxd_trans_t *const trans, amxd_dm_t *const dm)
Applies all previously added actions.
#define amxd_trans_set_value(type, trans, name, value)
Helper macro for setting a value.
amxd_status_t amxd_trans_add_inst(amxd_trans_t *const trans, const uint32_t index, const char *name)
Adds an instance add action to a transaction.
amxd_status_t amxd_trans_select_pathf(amxd_trans_t *const trans, const char *path,...) __attribute__((format(printf
Selects an object using a absolute or relative path.
amxd_status_t amxd_trans_init(amxd_trans_t *const trans)
Initializes a transaction object.
amxp_signal_mngr_t sigmngr
Definition: amxd_types.h:261
static amxd_status_t status
static void test_check_event(const char *const sig_name, UNUSED const amxc_var_t *const data, UNUSED void *const priv)
void test_build_dm(void)
static amxd_dm_t dm
void test_amxd_check_two_change_events_are_send(UNUSED void **state)
static void test_check_change_event(const char *const sig_name, const amxc_var_t *const data, UNUSED void *const priv)
void test_amxd_check_two_events_are_send(UNUSED void **state)
amxd_dm_t * test_get_dm(void)
void test_amxd_can_set_sub_object_params(UNUSED void **state)
void test_clean_dm(void)
void test_amxd_can_create_instance_and_set_sub_object_params(UNUSED void **state)