Michal Vasko | ee3539a | 2024-04-25 08:40:56 +0200 | [diff] [blame] | 1 | /** |
steweg | cbbae42 | 2024-04-22 11:38:25 +0200 | [diff] [blame] | 2 | * @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 | |
| 17 | static int |
| 18 | setup(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 Vasko | ee3539a | 2024-04-25 08:40:56 +0200 | [diff] [blame] | 24 | 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 | "}"; |
steweg | cbbae42 | 2024-04-22 11:38:25 +0200 | [diff] [blame] | 34 | |
| 35 | UTEST_SETUP; |
| 36 | UTEST_ADD_MODULE(schema1, LYS_IN_YANG, NULL, NULL); |
Michal Vasko | ee3539a | 2024-04-25 08:40:56 +0200 | [diff] [blame] | 37 | UTEST_ADD_MODULE(schema2, LYS_IN_YANG, NULL, NULL); |
steweg | cbbae42 | 2024-04-22 11:38:25 +0200 | [diff] [blame] | 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | static void |
| 42 | test_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 Vasko | ee3539a | 2024-04-25 08:40:56 +0200 | [diff] [blame] | 55 | static void |
| 56 | test_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 | |
steweg | cbbae42 | 2024-04-22 11:38:25 +0200 | [diff] [blame] | 74 | int |
| 75 | main(void) |
| 76 | { |
| 77 | const struct CMUnitTest tests[] = { |
| 78 | UTEST(test_container_presence, setup), |
Michal Vasko | ee3539a | 2024-04-25 08:40:56 +0200 | [diff] [blame] | 79 | UTEST(test_empty_container_wd_trim, setup), |
steweg | cbbae42 | 2024-04-22 11:38:25 +0200 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 83 | } |