1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
/**
* @file test_printer_json.c
* @author: Radek Krejci <rkrejci@cesnet.cz>
* @author Michal Vasko <mvasko@cesnet.cz>
* @brief unit tests for functions from printer_yang.c
*
* Copyright (c) 2019 - 2025 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*/
#define _UTEST_MAIN_
#include "utests.h"
static int
setup(void **state)
{
const char *schema1 = "module schema1 {namespace urn:tests:schema1;prefix schema1;yang-version 1.1;"
"revision 2014-05-08;"
"anydata data;"
"}";
const char *schema2 = "module schema2 {namespace urn:tests:schema2;prefix s2;yang-version 1.1;"
" container a {"
" container b {"
" leaf c {"
" type string;"
" default \"dflt\";"
" }"
" list l {"
" key \"k\";"
" leaf k {"
" type string;"
" }"
" }"
" list l2 {"
" key \"k2\";"
" leaf k2 {"
" type string;"
" }"
" }"
" }"
" leaf-list ll {"
" type string;"
" }"
" }"
" list tl {"
" key \"tk\";"
" leaf tk {"
" type string;"
" }"
" }"
"}";
UTEST_SETUP;
UTEST_ADD_MODULE(schema1, LYS_IN_YANG, NULL, NULL);
UTEST_ADD_MODULE(schema2, LYS_IN_YANG, NULL, NULL);
return 0;
}
static void
test_container_presence(void **state)
{
struct lyd_node *tree;
char *buffer = NULL;
const char *data = "{\"schema1:data\":{\"cont1\":{}}}";
CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
assert_int_equal(LY_SUCCESS, lyd_print_mem(&buffer, tree, LYD_JSON, LYD_PRINT_SHRINK));
CHECK_STRING(buffer, data);
free(buffer);
lyd_free_all(tree);
}
static void
test_empty_container_wd_trim(void **state)
{
struct lyd_node *tree;
char *buffer = NULL;
const char *data = "{\"schema2:a\":{\"b\":{\"c\":\"dflt\"}}}";
CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
assert_int_equal(LY_SUCCESS, lyd_print_mem(&buffer, tree, LYD_JSON, LYD_PRINT_SHRINK | LYD_PRINT_WD_TRIM));
CHECK_STRING(buffer, "{}");
free(buffer);
assert_int_equal(LY_SUCCESS, lyd_print_mem(&buffer, tree, LYD_JSON, LYD_PRINT_SHRINK | LYD_PRINT_WD_TRIM | LYD_PRINT_EMPTY_CONT));
CHECK_STRING(buffer, "{\"schema2:a\":{\"b\":{}}}");
free(buffer);
lyd_free_all(tree);
}
static void
test_empty_leaf_list(void **state)
{
struct lyd_node *tree;
char *buffer = NULL;
const char *data;
data = "{\"schema2:a\":{\"b\":{\"c\":\"val\"}}}";
CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
assert_int_equal(LY_SUCCESS, lyd_print_mem(&buffer, tree, LYD_JSON, LYD_PRINT_SHRINK | LYD_PRINT_EMPTY_LEAF_LIST));
CHECK_STRING(buffer, "{\"schema2:a\":{\"b\":{\"c\":\"val\",\"l\":[],\"l2\":[]},\"ll\":[]},\"schema2:tl\":[]}");
free(buffer);
lyd_free_all(tree);
data = "{\"schema2:a\":{\"b\":{\"l\":[{\"k\":\"key1\"},{\"k\":\"key2\"}]}}}";
CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
assert_int_equal(LY_SUCCESS, lyd_print_mem(&buffer, tree, LYD_JSON, LYD_PRINT_SHRINK | LYD_PRINT_EMPTY_LEAF_LIST));
CHECK_STRING(buffer, "{\"schema2:a\":{\"b\":{\"l\":[{\"k\":\"key1\"},{\"k\":\"key2\"}],\"l2\":[]},\"ll\":[]},\"schema2:tl\":[]}");
free(buffer);
lyd_free_all(tree);
data = "{\"schema2:a\":{}}";
CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
assert_int_equal(LY_SUCCESS, lyd_print_mem(&buffer, tree, LYD_JSON, LYD_PRINT_EMPTY_CONT | LYD_PRINT_EMPTY_LEAF_LIST));
CHECK_STRING(buffer, "{\n"
" \"schema2:a\": {\n"
" \"b\": {\n"
" \"l\": [\n"
" ],\n"
" \"l2\": [\n"
" ]\n"
" },\n"
" \"ll\": [\n"
" ]\n"
" },\n"
" \"schema2:tl\": [\n"
" ]\n"
"}\n");
free(buffer);
lyd_free_all(tree);
}
static void
test_no_json_nested_prefix(void **state)
{
struct lyd_node *tree;
char *buffer = NULL;
const char *data = "{\"schema2:a\":{\"b\":{\"c\":\"dflt\"}}}";
CHECK_PARSE_LYD_PARAM(data, LYD_JSON, 0, LYD_VALIDATE_PRESENT, LY_SUCCESS, tree);
assert_int_equal(LY_SUCCESS, lyd_print_mem(&buffer, tree, LYD_JSON, LYD_PRINT_SHRINK | LYD_PRINT_WD_ALL |
LYD_PRINT_JSON_NO_NESTED_PREFIX));
CHECK_STRING(buffer, "{\"schema2:a\":{\"b\":{\"c\":\"dflt\"}}}");
free(buffer);
assert_int_equal(LY_SUCCESS, lyd_print_mem(&buffer, lyd_child(tree), LYD_JSON, LYD_PRINT_SHRINK | LYD_PRINT_WD_ALL |
LYD_PRINT_JSON_NO_NESTED_PREFIX));
CHECK_STRING(buffer, "{\"b\":{\"c\":\"dflt\"}}");
free(buffer);
lyd_free_all(tree);
}
int
main(void)
{
const struct CMUnitTest tests[] = {
UTEST(test_container_presence, setup),
UTEST(test_empty_container_wd_trim, setup),
UTEST(test_empty_leaf_list, setup),
UTEST(test_no_json_nested_prefix, setup),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
|