blob: 65539f09dacc8bb15fdbb7d5a34fbca6abfecb07 [file] [log] [blame]
Michal Vaskoee3539a2024-04-25 08:40:56 +02001/**
stewegcbbae422024-04-22 11:38:25 +02002 * @file test_printer_json.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from printer_yang.c
5 *
6 * Copyright (c) 2019-2020 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14#define _UTEST_MAIN_
15#include "utests.h"
16
17static int
18setup(void **state)
19{
20 const char *schema1 = "module schema1 {namespace urn:tests:schema1;prefix schema1;yang-version 1.1;"
21 "revision 2014-05-08;"
22 "anydata data;"
23 "}";
Michal Vaskoee3539a2024-04-25 08:40:56 +020024 const char *schema2 = "module schema2 {namespace urn:tests:schema2;prefix s2;yang-version 1.1;"
25 " container a {"
26 " container b {"
27 " leaf c {"
28 " type string;"
29 " default \"dflt\";"
30 " }"
31 " }"
32 " }"
33 "}";
stewegcbbae422024-04-22 11:38:25 +020034
35 UTEST_SETUP;
36 UTEST_ADD_MODULE(schema1, LYS_IN_YANG, NULL, NULL);
Michal Vaskoee3539a2024-04-25 08:40:56 +020037 UTEST_ADD_MODULE(schema2, LYS_IN_YANG, NULL, NULL);
stewegcbbae422024-04-22 11:38:25 +020038 return 0;
39}
40
41static void
42test_container_presence(void **state)
43{
44 struct lyd_node *tree;
45 char *buffer = NULL;
46 const char *data = "{\"schema1:data\":{\"cont1\":{}}}";
47
48 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
49 assert_int_equal(LY_SUCCESS, lyd_print_mem(&buffer, tree, LYD_JSON, LYD_PRINT_SHRINK));
50 CHECK_STRING(buffer, data);
51 free(buffer);
52 lyd_free_all(tree);
53}
54
Michal Vaskoee3539a2024-04-25 08:40:56 +020055static void
56test_empty_container_wd_trim(void **state)
57{
58 struct lyd_node *tree;
59 char *buffer = NULL;
60 const char *data = "{\"schema2:a\":{\"b\":{\"c\":\"dflt\"}}}";
61
62 CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
63 assert_int_equal(LY_SUCCESS, lyd_print_mem(&buffer, tree, LYD_JSON, LYD_PRINT_SHRINK | LYD_PRINT_WD_TRIM));
64 CHECK_STRING(buffer, "{}");
65 free(buffer);
66
67 assert_int_equal(LY_SUCCESS, lyd_print_mem(&buffer, tree, LYD_JSON, LYD_PRINT_SHRINK | LYD_PRINT_WD_TRIM | LYD_PRINT_KEEPEMPTYCONT));
68 CHECK_STRING(buffer, "{\"schema2:a\":{\"b\":{}}}");
69 free(buffer);
70
71 lyd_free_all(tree);
72}
73
stewegcbbae422024-04-22 11:38:25 +020074int
75main(void)
76{
77 const struct CMUnitTest tests[] = {
78 UTEST(test_container_presence, setup),
Michal Vaskoee3539a2024-04-25 08:40:56 +020079 UTEST(test_empty_container_wd_trim, setup),
stewegcbbae422024-04-22 11:38:25 +020080 };
81
82 return cmocka_run_group_tests(tests, NULL, NULL);
83}