Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/ |
| 3 | * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/ |
| 4 | * |
| 5 | * Written by Václav Kubernát <kubervac@fit.cvut.cz> |
| 6 | * |
| 7 | */ |
| 8 | |
Václav Kubernát | 26b5608 | 2020-02-03 18:28:56 +0100 | [diff] [blame] | 9 | #include "trompeloeil_doctest.hpp" |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 10 | #include <sysrepo-cpp/Session.hpp> |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 11 | #include "proxy_datastore.hpp" |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 12 | #include "yang_schema.hpp" |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 13 | |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 14 | #ifdef sysrepo_BACKEND |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 15 | #include "sysrepo_access.hpp" |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 16 | using OnInvalidSchemaPathCreate = DatastoreException; |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 17 | using OnInvalidSchemaPathDelete = DatastoreException; |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 18 | using OnInvalidSchemaPathMove = sysrepo::sysrepo_exception; |
Václav Kubernát | 4077613 | 2021-02-03 08:47:33 +0100 | [diff] [blame] | 19 | using OnInvalidRpcPath = std::runtime_error; |
Václav Kubernát | 2edfe54 | 2021-02-03 08:08:29 +0100 | [diff] [blame] | 20 | using OnInvalidRpcInput = sysrepo::sysrepo_exception; |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 21 | using OnKeyNotFound = void; |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 22 | using OnExec = void; |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 23 | #elif defined(netconf_BACKEND) |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 24 | using OnInvalidSchemaPathCreate = std::runtime_error; |
| 25 | using OnInvalidSchemaPathDelete = std::runtime_error; |
| 26 | using OnInvalidSchemaPathMove = std::runtime_error; |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 27 | using OnInvalidRpcPath = std::runtime_error; |
Václav Kubernát | 2edfe54 | 2021-02-03 08:08:29 +0100 | [diff] [blame] | 28 | using OnInvalidRpcInput = std::runtime_error; |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 29 | using OnKeyNotFound = std::runtime_error; |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 30 | using OnExec = void; |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 31 | #include "netconf_access.hpp" |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 32 | #elif defined(yang_BACKEND) |
| 33 | #include <fstream> |
| 34 | #include "yang_access.hpp" |
| 35 | #include "yang_access_test_vars.hpp" |
| 36 | using OnInvalidSchemaPathCreate = DatastoreException; |
| 37 | using OnInvalidSchemaPathDelete = DatastoreException; |
| 38 | using OnInvalidSchemaPathMove = DatastoreException; |
| 39 | using OnInvalidRpcPath = DatastoreException; |
Václav Kubernát | 2edfe54 | 2021-02-03 08:08:29 +0100 | [diff] [blame] | 40 | using OnInvalidRpcInput = std::logic_error; |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 41 | using OnKeyNotFound = DatastoreException; |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 42 | using OnExec = std::logic_error; |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 43 | #else |
| 44 | #error "Unknown backend" |
| 45 | #endif |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 46 | #include "pretty_printers.hpp" |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 47 | #include "sysrepo_subscription.hpp" |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 48 | #include "utils.hpp" |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 49 | |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 50 | using namespace std::literals::string_literals; |
| 51 | |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 52 | class MockRecorder : public trompeloeil::mock_interface<Recorder> { |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 53 | public: |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 54 | IMPLEMENT_MOCK3(write); |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 55 | }; |
| 56 | |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 57 | class MockDataSupplier : public trompeloeil::mock_interface<DataSupplier> { |
| 58 | public: |
| 59 | IMPLEMENT_CONST_MOCK1(get_data); |
| 60 | }; |
| 61 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 62 | namespace { |
| 63 | template <class ...> constexpr std::false_type always_false [[maybe_unused]] {}; |
| 64 | template <class Exception, typename Callable> void catching(const Callable& what) { |
| 65 | |
| 66 | if constexpr (std::is_same_v<Exception, void>) { |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 67 | what(); |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 68 | } else if constexpr (std::is_same<Exception, std::runtime_error>()) { |
| 69 | // cannot use REQUIRE_THROWS_AS(..., Exception) directly because that one |
| 70 | // needs an extra `typename` deep in the bowels of doctest |
| 71 | REQUIRE_THROWS_AS(what(), std::runtime_error); |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 72 | } else if constexpr (std::is_same<Exception, std::logic_error>()) { |
| 73 | REQUIRE_THROWS_AS(what(), std::logic_error); |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 74 | } else if constexpr (std::is_same<Exception, DatastoreException>()) { |
| 75 | REQUIRE_THROWS_AS(what(), DatastoreException); |
| 76 | } else if constexpr (std::is_same<Exception, sysrepo::sysrepo_exception>()) { |
| 77 | REQUIRE_THROWS_AS(what(), sysrepo::sysrepo_exception); |
| 78 | } else { |
| 79 | static_assert(always_false<Exception>); // https://stackoverflow.com/a/53945549/2245623 |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 80 | } |
| 81 | } |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 82 | } |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 83 | |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 84 | #if defined(yang_BACKEND) |
| 85 | class TestYangAccess : public YangAccess { |
| 86 | public: |
| 87 | void commitChanges() override |
| 88 | { |
| 89 | YangAccess::commitChanges(); |
| 90 | dumpToSysrepo(); |
| 91 | } |
| 92 | |
| 93 | void copyConfig(const Datastore source, const Datastore destination) override |
| 94 | { |
| 95 | YangAccess::copyConfig(source, destination); |
| 96 | dumpToSysrepo(); |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | void dumpToSysrepo() |
| 101 | { |
| 102 | { |
| 103 | std::ofstream of(testConfigFile); |
Václav Kubernát | 70d7f7a | 2020-06-23 14:40:40 +0200 | [diff] [blame] | 104 | of << dump(DataFormat::Xml); |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 105 | } |
Jan Kundrát | 86840ec | 2021-01-27 15:38:08 +0100 | [diff] [blame] | 106 | auto command = std::string(sysrepocfgExecutable) + " --import=" + testConfigFile + " --format=xml --datastore=running --module=example-schema"; |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 107 | REQUIRE(std::system(command.c_str()) == 0); |
| 108 | } |
| 109 | }; |
| 110 | #endif |
| 111 | |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 112 | TEST_CASE("setting/getting values") |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 113 | { |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 114 | sr_log_stderr(SR_LL_DBG); |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 115 | trompeloeil::sequence seq1; |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 116 | MockRecorder mockRunning; |
| 117 | MockRecorder mockStartup; |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 118 | { |
| 119 | auto conn = std::make_shared<sysrepo::Connection>(); |
| 120 | auto sess = std::make_shared<sysrepo::Session>(conn); |
| 121 | sess->copy_config(SR_DS_STARTUP, "example-schema", 1000, true); |
| 122 | } |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 123 | SysrepoSubscription subRunning("example-schema", &mockRunning); |
| 124 | SysrepoSubscription subStartup("example-schema", &mockStartup, SR_DS_STARTUP); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 125 | |
| 126 | #ifdef sysrepo_BACKEND |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 127 | SysrepoAccess datastore; |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 128 | #elif defined(netconf_BACKEND) |
Václav Kubernát | d1beedc | 2020-09-07 12:09:05 +0200 | [diff] [blame] | 129 | const auto NETOPEER_SOCKET = getenv("NETOPEER_SOCKET"); |
| 130 | NetconfAccess datastore(NETOPEER_SOCKET); |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 131 | #elif defined(yang_BACKEND) |
| 132 | TestYangAccess datastore; |
| 133 | datastore.addSchemaDir(schemaDir); |
| 134 | datastore.addSchemaFile(exampleSchemaFile); |
Václav Kubernát | c31bd60 | 2019-03-07 11:44:48 +0100 | [diff] [blame] | 135 | #else |
| 136 | #error "Unknown backend" |
| 137 | #endif |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 138 | |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 139 | |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 140 | SECTION("set leafInt8 to -128") |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 141 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 142 | REQUIRE_CALL(mockRunning, write("/example-schema:leafInt8", std::nullopt, "-128"s)); |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 143 | datastore.setLeaf("/example-schema:leafInt8", int8_t{-128}); |
| 144 | datastore.commitChanges(); |
| 145 | } |
| 146 | |
| 147 | SECTION("set leafInt16 to -32768") |
| 148 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 149 | REQUIRE_CALL(mockRunning, write("/example-schema:leafInt16", std::nullopt, "-32768"s)); |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 150 | datastore.setLeaf("/example-schema:leafInt16", int16_t{-32768}); |
| 151 | datastore.commitChanges(); |
| 152 | } |
| 153 | |
| 154 | SECTION("set leafInt32 to -2147483648") |
| 155 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 156 | REQUIRE_CALL(mockRunning, write("/example-schema:leafInt32", std::nullopt, "-2147483648"s)); |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 157 | datastore.setLeaf("/example-schema:leafInt32", int32_t{-2147483648}); |
| 158 | datastore.commitChanges(); |
| 159 | } |
| 160 | |
| 161 | SECTION("set leafInt64 to -50000000000") |
| 162 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 163 | REQUIRE_CALL(mockRunning, write("/example-schema:leafInt64", std::nullopt, "-50000000000"s)); |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 164 | datastore.setLeaf("/example-schema:leafInt64", int64_t{-50000000000}); |
| 165 | datastore.commitChanges(); |
| 166 | } |
| 167 | |
| 168 | SECTION("set leafUInt8 to 255") |
| 169 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 170 | REQUIRE_CALL(mockRunning, write("/example-schema:leafUInt8", std::nullopt, "255"s)); |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 171 | datastore.setLeaf("/example-schema:leafUInt8", uint8_t{255}); |
| 172 | datastore.commitChanges(); |
| 173 | } |
| 174 | |
| 175 | SECTION("set leafUInt16 to 65535") |
| 176 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 177 | REQUIRE_CALL(mockRunning, write("/example-schema:leafUInt16", std::nullopt, "65535"s)); |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 178 | datastore.setLeaf("/example-schema:leafUInt16", uint16_t{65535}); |
| 179 | datastore.commitChanges(); |
| 180 | } |
| 181 | |
| 182 | SECTION("set leafUInt32 to 4294967295") |
| 183 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 184 | REQUIRE_CALL(mockRunning, write("/example-schema:leafUInt32", std::nullopt, "4294967295"s)); |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 185 | datastore.setLeaf("/example-schema:leafUInt32", uint32_t{4294967295}); |
| 186 | datastore.commitChanges(); |
| 187 | } |
| 188 | |
| 189 | SECTION("set leafUInt64 to 50000000000") |
| 190 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 191 | REQUIRE_CALL(mockRunning, write("/example-schema:leafUInt64", std::nullopt, "50000000000"s)); |
Václav Kubernát | 134d78f | 2019-09-03 16:42:29 +0200 | [diff] [blame] | 192 | datastore.setLeaf("/example-schema:leafUInt64", uint64_t{50000000000}); |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 193 | datastore.commitChanges(); |
| 194 | } |
| 195 | |
| 196 | SECTION("set leafEnum to coze") |
| 197 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 198 | REQUIRE_CALL(mockRunning, write("/example-schema:leafEnum", std::nullopt, "coze"s)); |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 199 | datastore.setLeaf("/example-schema:leafEnum", enum_{"coze"}); |
| 200 | datastore.commitChanges(); |
| 201 | } |
| 202 | |
| 203 | SECTION("set leafDecimal to 123.544") |
| 204 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 205 | REQUIRE_CALL(mockRunning, write("/example-schema:leafDecimal", std::nullopt, "123.544"s)); |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 206 | datastore.setLeaf("/example-schema:leafDecimal", 123.544); |
| 207 | datastore.commitChanges(); |
| 208 | } |
| 209 | |
Jan Kundrát | d287286 | 2020-06-18 21:02:00 +0200 | [diff] [blame] | 210 | SECTION("set a string, then delete it") |
| 211 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 212 | REQUIRE_CALL(mockRunning, write("/example-schema:leafString", std::nullopt, "blah"s)); |
Jan Kundrát | d287286 | 2020-06-18 21:02:00 +0200 | [diff] [blame] | 213 | datastore.setLeaf("/example-schema:leafString", "blah"s); |
| 214 | datastore.commitChanges(); |
| 215 | DatastoreAccess::Tree expected{{"/example-schema:leafString", "blah"s}}; |
| 216 | REQUIRE(datastore.getItems("/example-schema:leafString") == expected); |
| 217 | |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 218 | REQUIRE_CALL(mockRunning, write("/example-schema:leafString", "blah"s, std::nullopt)); |
Jan Kundrát | d287286 | 2020-06-18 21:02:00 +0200 | [diff] [blame] | 219 | datastore.deleteItem("/example-schema:leafString"); |
| 220 | datastore.commitChanges(); |
| 221 | expected.clear(); |
| 222 | REQUIRE(datastore.getItems("/example-schema:leafString") == expected); |
| 223 | } |
| 224 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 225 | SECTION("set a non-existing leaf") |
| 226 | { |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 227 | catching<OnInvalidSchemaPathCreate>([&] { |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 228 | datastore.setLeaf("/example-schema:non-existing", "what"s); |
| 229 | }); |
| 230 | } |
| 231 | |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 232 | SECTION("create presence container") |
| 233 | { |
Václav Kubernát | 70d7f7a | 2020-06-23 14:40:40 +0200 | [diff] [blame] | 234 | REQUIRE(datastore.dump(DataFormat::Json).find("example-schema:pContainer") == std::string::npos); |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 235 | REQUIRE_CALL(mockRunning, write("/example-schema:pContainer", std::nullopt, ""s)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 236 | datastore.createItem("/example-schema:pContainer"); |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 237 | datastore.commitChanges(); |
Václav Kubernát | 70d7f7a | 2020-06-23 14:40:40 +0200 | [diff] [blame] | 238 | REQUIRE(datastore.dump(DataFormat::Json).find("example-schema:pContainer") != std::string::npos); |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 239 | } |
| 240 | |
Václav Kubernát | cc7a93f | 2020-02-04 11:48:15 +0100 | [diff] [blame] | 241 | SECTION("create/delete a list instance") |
Václav Kubernát | 45f4a82 | 2019-05-29 21:10:50 +0200 | [diff] [blame] | 242 | { |
Václav Kubernát | cc7a93f | 2020-02-04 11:48:15 +0100 | [diff] [blame] | 243 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 244 | REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Nguyen']", std::nullopt, ""s)); |
| 245 | REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Nguyen']/name", std::nullopt, "Nguyen"s)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 246 | datastore.createItem("/example-schema:person[name='Nguyen']"); |
Václav Kubernát | cc7a93f | 2020-02-04 11:48:15 +0100 | [diff] [blame] | 247 | datastore.commitChanges(); |
| 248 | } |
| 249 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 250 | REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Nguyen']", ""s, std::nullopt)); |
| 251 | REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Nguyen']/name", "Nguyen"s, std::nullopt)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 252 | datastore.deleteItem("/example-schema:person[name='Nguyen']"); |
Václav Kubernát | cc7a93f | 2020-02-04 11:48:15 +0100 | [diff] [blame] | 253 | datastore.commitChanges(); |
| 254 | } |
Václav Kubernát | 45f4a82 | 2019-05-29 21:10:50 +0200 | [diff] [blame] | 255 | } |
| 256 | |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 257 | SECTION("deleting non-existing list keys") |
| 258 | { |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 259 | catching<OnKeyNotFound>([&] { |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 260 | datastore.deleteItem("/example-schema:person[name='non existing']"); |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 261 | datastore.commitChanges(); |
| 262 | }); |
| 263 | } |
| 264 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 265 | SECTION("accessing non-existing schema nodes as a list") |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 266 | { |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 267 | catching<OnInvalidSchemaPathCreate>([&] { |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 268 | datastore.createItem("/example-schema:non-existing-list[xxx='blah']"); |
| 269 | datastore.commitChanges(); |
| 270 | }); |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 271 | catching<OnInvalidSchemaPathDelete>([&] { |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 272 | datastore.deleteItem("/example-schema:non-existing-list[xxx='non existing']"); |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 273 | datastore.commitChanges(); |
| 274 | }); |
| 275 | } |
| 276 | |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 277 | SECTION("leafref pointing to a key of a list") |
| 278 | { |
| 279 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 280 | REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Dan']", std::nullopt, ""s)); |
| 281 | REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Dan']/name", std::nullopt, "Dan"s)); |
| 282 | REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Elfi']", std::nullopt, ""s)); |
| 283 | REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Elfi']/name", std::nullopt, "Elfi"s)); |
| 284 | REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Kolafa']", std::nullopt, ""s)); |
| 285 | REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Kolafa']/name", std::nullopt, "Kolafa"s)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 286 | datastore.createItem("/example-schema:person[name='Dan']"); |
| 287 | datastore.createItem("/example-schema:person[name='Elfi']"); |
| 288 | datastore.createItem("/example-schema:person[name='Kolafa']"); |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 289 | datastore.commitChanges(); |
| 290 | } |
| 291 | |
Václav Kubernát | 7b191ce | 2020-06-30 16:22:53 +0200 | [diff] [blame] | 292 | std::string value; |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 293 | SECTION("Dan") |
| 294 | { |
Václav Kubernát | 7b191ce | 2020-06-30 16:22:53 +0200 | [diff] [blame] | 295 | value = "Dan"; |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | SECTION("Elfi") |
| 299 | { |
Václav Kubernát | 7b191ce | 2020-06-30 16:22:53 +0200 | [diff] [blame] | 300 | value = "Elfi"; |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | SECTION("Kolafa") |
| 304 | { |
Václav Kubernát | 7b191ce | 2020-06-30 16:22:53 +0200 | [diff] [blame] | 305 | value = "Kolafa"; |
| 306 | } |
| 307 | |
| 308 | datastore.setLeaf("/example-schema:bossPerson", value); |
| 309 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 310 | REQUIRE_CALL(mockRunning, write("/example-schema:bossPerson", std::nullopt, value)); |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 311 | datastore.commitChanges(); |
| 312 | } |
Václav Kubernát | 7b191ce | 2020-06-30 16:22:53 +0200 | [diff] [blame] | 313 | REQUIRE(datastore.getItems("/example-schema:bossPerson") == DatastoreAccess::Tree{{"/example-schema:bossPerson", value}}); |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 314 | } |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 315 | SECTION("bool values get correctly represented as bools") |
| 316 | { |
| 317 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 318 | REQUIRE_CALL(mockRunning, write("/example-schema:down", std::nullopt, "true"s)); |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 319 | datastore.setLeaf("/example-schema:down", bool{true}); |
| 320 | datastore.commitChanges(); |
| 321 | } |
| 322 | |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 323 | DatastoreAccess::Tree expected{{"/example-schema:down", bool{true}}}; |
Václav Kubernát | 8e121ff | 2019-10-15 15:47:45 +0200 | [diff] [blame] | 324 | REQUIRE(datastore.getItems("/example-schema:down") == expected); |
| 325 | } |
Václav Kubernát | 3efb5ca | 2019-10-09 20:07:40 +0200 | [diff] [blame] | 326 | |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 327 | SECTION("getting items from the whole module") |
| 328 | { |
| 329 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 330 | REQUIRE_CALL(mockRunning, write("/example-schema:up", std::nullopt, "true"s)); |
| 331 | REQUIRE_CALL(mockRunning, write("/example-schema:down", std::nullopt, "false"s)); |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 332 | datastore.setLeaf("/example-schema:up", bool{true}); |
| 333 | datastore.setLeaf("/example-schema:down", bool{false}); |
| 334 | datastore.commitChanges(); |
| 335 | } |
| 336 | |
Václav Kubernát | cf9224f | 2020-06-02 09:55:29 +0200 | [diff] [blame] | 337 | DatastoreAccess::Tree expected{ |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 338 | {"/example-schema:up", bool{true}}, |
| 339 | {"/example-schema:down", bool{false}} |
| 340 | }; |
Václav Kubernát | 9456b5c | 2019-10-02 21:14:52 +0200 | [diff] [blame] | 341 | REQUIRE(datastore.getItems("/example-schema:*") == expected); |
| 342 | } |
| 343 | |
Václav Kubernát | 152ce22 | 2019-12-19 12:23:32 +0100 | [diff] [blame] | 344 | SECTION("getItems returns correct datatypes") |
| 345 | { |
| 346 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 347 | REQUIRE_CALL(mockRunning, write("/example-schema:leafEnum", std::nullopt, "lol"s)); |
Václav Kubernát | 152ce22 | 2019-12-19 12:23:32 +0100 | [diff] [blame] | 348 | datastore.setLeaf("/example-schema:leafEnum", enum_{"lol"}); |
| 349 | datastore.commitChanges(); |
| 350 | } |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 351 | DatastoreAccess::Tree expected{{"/example-schema:leafEnum", enum_{"lol"}}}; |
Václav Kubernát | 152ce22 | 2019-12-19 12:23:32 +0100 | [diff] [blame] | 352 | |
| 353 | REQUIRE(datastore.getItems("/example-schema:leafEnum") == expected); |
| 354 | } |
| 355 | |
Václav Kubernát | d812cfb | 2020-01-07 17:30:20 +0100 | [diff] [blame] | 356 | SECTION("getItems on a list") |
| 357 | { |
| 358 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 359 | REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Jan']", std::nullopt, ""s)); |
| 360 | REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Jan']/name", std::nullopt, "Jan"s)); |
| 361 | REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Michal']", std::nullopt, ""s)); |
| 362 | REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Michal']/name", std::nullopt, "Michal"s)); |
| 363 | REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Petr']", std::nullopt, ""s)); |
| 364 | REQUIRE_CALL(mockRunning, write("/example-schema:person[name='Petr']/name", std::nullopt, "Petr"s)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 365 | datastore.createItem("/example-schema:person[name='Jan']"); |
| 366 | datastore.createItem("/example-schema:person[name='Michal']"); |
| 367 | datastore.createItem("/example-schema:person[name='Petr']"); |
Václav Kubernát | d812cfb | 2020-01-07 17:30:20 +0100 | [diff] [blame] | 368 | datastore.commitChanges(); |
| 369 | } |
Jan Kundrát | b331b55 | 2020-01-23 15:25:29 +0100 | [diff] [blame] | 370 | DatastoreAccess::Tree expected{ |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 371 | {"/example-schema:person[name='Jan']", special_{SpecialValue::List}}, |
Václav Kubernát | d812cfb | 2020-01-07 17:30:20 +0100 | [diff] [blame] | 372 | {"/example-schema:person[name='Jan']/name", std::string{"Jan"}}, |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 373 | {"/example-schema:person[name='Michal']", special_{SpecialValue::List}}, |
Václav Kubernát | d812cfb | 2020-01-07 17:30:20 +0100 | [diff] [blame] | 374 | {"/example-schema:person[name='Michal']/name", std::string{"Michal"}}, |
Václav Kubernát | 144729d | 2020-01-08 15:20:35 +0100 | [diff] [blame] | 375 | {"/example-schema:person[name='Petr']", special_{SpecialValue::List}}, |
Václav Kubernát | d812cfb | 2020-01-07 17:30:20 +0100 | [diff] [blame] | 376 | {"/example-schema:person[name='Petr']/name", std::string{"Petr"}} |
| 377 | }; |
| 378 | |
| 379 | REQUIRE(datastore.getItems("/example-schema:person") == expected); |
| 380 | } |
| 381 | |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 382 | SECTION("presence containers") |
| 383 | { |
| 384 | DatastoreAccess::Tree expected; |
| 385 | // Make sure it's not there before we create it |
| 386 | REQUIRE(datastore.getItems("/example-schema:pContainer") == expected); |
| 387 | |
| 388 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 389 | REQUIRE_CALL(mockRunning, write("/example-schema:pContainer", std::nullopt, ""s)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 390 | datastore.createItem("/example-schema:pContainer"); |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 391 | datastore.commitChanges(); |
| 392 | } |
| 393 | expected = { |
| 394 | {"/example-schema:pContainer", special_{SpecialValue::PresenceContainer}} |
| 395 | }; |
| 396 | REQUIRE(datastore.getItems("/example-schema:pContainer") == expected); |
| 397 | |
| 398 | // Make sure it's not there after we delete it |
| 399 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 400 | REQUIRE_CALL(mockRunning, write("/example-schema:pContainer", ""s, std::nullopt)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 401 | datastore.deleteItem("/example-schema:pContainer"); |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 402 | datastore.commitChanges(); |
| 403 | } |
| 404 | expected = {}; |
| 405 | REQUIRE(datastore.getItems("/example-schema:pContainer") == expected); |
Václav Kubernát | da8e4b9 | 2020-02-04 11:56:30 +0100 | [diff] [blame] | 406 | } |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 407 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 408 | SECTION("creating a non-existing schema node as a container") |
| 409 | { |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 410 | catching<OnInvalidSchemaPathCreate>([&] { |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 411 | datastore.createItem("/example-schema:non-existing-presence-container"); |
| 412 | datastore.commitChanges(); |
| 413 | }); |
| 414 | } |
| 415 | |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 416 | SECTION("deleting a non-existing schema node as a container or leaf") |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 417 | { |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 418 | catching<OnInvalidSchemaPathDelete>([&] { |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 419 | datastore.deleteItem("/example-schema:non-existing-presence-container"); |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 420 | datastore.commitChanges(); |
| 421 | }); |
| 422 | } |
| 423 | |
Václav Kubernát | da8e4b9 | 2020-02-04 11:56:30 +0100 | [diff] [blame] | 424 | SECTION("nested presence container") |
| 425 | { |
| 426 | DatastoreAccess::Tree expected; |
| 427 | // Make sure it's not there before we create it |
| 428 | REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected); |
| 429 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 430 | REQUIRE_CALL(mockRunning, write("/example-schema:inventory/stuff", std::nullopt, ""s)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 431 | datastore.createItem("/example-schema:inventory/stuff"); |
Václav Kubernát | da8e4b9 | 2020-02-04 11:56:30 +0100 | [diff] [blame] | 432 | datastore.commitChanges(); |
| 433 | } |
| 434 | expected = { |
| 435 | {"/example-schema:inventory/stuff", special_{SpecialValue::PresenceContainer}} |
| 436 | }; |
| 437 | REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected); |
| 438 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 439 | REQUIRE_CALL(mockRunning, write("/example-schema:inventory/stuff", ""s, std::nullopt)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 440 | datastore.deleteItem("/example-schema:inventory/stuff"); |
Václav Kubernát | da8e4b9 | 2020-02-04 11:56:30 +0100 | [diff] [blame] | 441 | datastore.commitChanges(); |
| 442 | } |
| 443 | expected = {}; |
| 444 | REQUIRE(datastore.getItems("/example-schema:inventory/stuff") == expected); |
Václav Kubernát | 69aabe9 | 2020-01-24 16:53:12 +0100 | [diff] [blame] | 445 | } |
| 446 | |
Jan Kundrát | bd3169c | 2020-02-03 19:31:34 +0100 | [diff] [blame] | 447 | SECTION("floats") |
| 448 | { |
| 449 | datastore.setLeaf("/example-schema:leafDecimal", 123.4); |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 450 | REQUIRE_CALL(mockRunning, write("/example-schema:leafDecimal", std::nullopt, "123.4"s)); |
Jan Kundrát | bd3169c | 2020-02-03 19:31:34 +0100 | [diff] [blame] | 451 | datastore.commitChanges(); |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 452 | DatastoreAccess::Tree expected{ |
Jan Kundrát | bd3169c | 2020-02-03 19:31:34 +0100 | [diff] [blame] | 453 | {"/example-schema:leafDecimal", 123.4}, |
| 454 | }; |
| 455 | REQUIRE(datastore.getItems("/example-schema:leafDecimal") == expected); |
| 456 | } |
| 457 | |
Jan Kundrát | 3ff5012 | 2020-05-07 00:37:50 +0200 | [diff] [blame] | 458 | SECTION("unions") |
| 459 | { |
| 460 | datastore.setLeaf("/example-schema:unionIntString", int32_t{10}); |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 461 | REQUIRE_CALL(mockRunning, write("/example-schema:unionIntString", std::nullopt, "10"s)); |
Jan Kundrát | 3ff5012 | 2020-05-07 00:37:50 +0200 | [diff] [blame] | 462 | datastore.commitChanges(); |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 463 | DatastoreAccess::Tree expected{ |
Jan Kundrát | 3ff5012 | 2020-05-07 00:37:50 +0200 | [diff] [blame] | 464 | {"/example-schema:unionIntString", int32_t{10}}, |
| 465 | }; |
| 466 | REQUIRE(datastore.getItems("/example-schema:unionIntString") == expected); |
| 467 | } |
| 468 | |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 469 | SECTION("identityref") |
| 470 | { |
Jan Kundrát | 0d8abd1 | 2020-05-07 02:00:14 +0200 | [diff] [blame] | 471 | datastore.setLeaf("/example-schema:beast", identityRef_{"example-schema", "Mammal"}); |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 472 | REQUIRE_CALL(mockRunning, write("/example-schema:beast", std::nullopt, "example-schema:Mammal"s)); |
Jan Kundrát | 0d8abd1 | 2020-05-07 02:00:14 +0200 | [diff] [blame] | 473 | datastore.commitChanges(); |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 474 | DatastoreAccess::Tree expected{ |
Jan Kundrát | 0d8abd1 | 2020-05-07 02:00:14 +0200 | [diff] [blame] | 475 | {"/example-schema:beast", identityRef_{"example-schema", "Mammal"}}, |
| 476 | }; |
| 477 | REQUIRE(datastore.getItems("/example-schema:beast") == expected); |
| 478 | |
| 479 | datastore.setLeaf("/example-schema:beast", identityRef_{"Whale"}); |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 480 | REQUIRE_CALL(mockRunning, write("/example-schema:beast", "example-schema:Mammal", "example-schema:Whale"s)); |
Jan Kundrát | 0d8abd1 | 2020-05-07 02:00:14 +0200 | [diff] [blame] | 481 | datastore.commitChanges(); |
| 482 | expected = { |
| 483 | {"/example-schema:beast", identityRef_{"example-schema", "Whale"}}, |
| 484 | }; |
| 485 | REQUIRE(datastore.getItems("/example-schema:beast") == expected); |
| 486 | } |
| 487 | |
Jan Kundrát | 6898544 | 2020-05-07 02:15:34 +0200 | [diff] [blame] | 488 | SECTION("binary") |
| 489 | { |
| 490 | datastore.setLeaf("/example-schema:blob", binary_{"cHduegByIQ=="s}); |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 491 | REQUIRE_CALL(mockRunning, write("/example-schema:blob", std::nullopt, "cHduegByIQ=="s)); |
Jan Kundrát | 6898544 | 2020-05-07 02:15:34 +0200 | [diff] [blame] | 492 | datastore.commitChanges(); |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 493 | DatastoreAccess::Tree expected{ |
Jan Kundrát | 6898544 | 2020-05-07 02:15:34 +0200 | [diff] [blame] | 494 | {"/example-schema:blob", binary_{"cHduegByIQ=="s}}, |
| 495 | }; |
| 496 | REQUIRE(datastore.getItems("/example-schema:blob") == expected); |
| 497 | } |
| 498 | |
Jan Kundrát | 379bb57 | 2020-05-07 03:23:13 +0200 | [diff] [blame] | 499 | SECTION("empty") |
| 500 | { |
| 501 | datastore.setLeaf("/example-schema:dummy", empty_{}); |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 502 | REQUIRE_CALL(mockRunning, write("/example-schema:dummy", std::nullopt, ""s)); |
Jan Kundrát | 379bb57 | 2020-05-07 03:23:13 +0200 | [diff] [blame] | 503 | datastore.commitChanges(); |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 504 | DatastoreAccess::Tree expected{ |
Jan Kundrát | 379bb57 | 2020-05-07 03:23:13 +0200 | [diff] [blame] | 505 | {"/example-schema:dummy", empty_{}}, |
| 506 | }; |
| 507 | REQUIRE(datastore.getItems("/example-schema:dummy") == expected); |
| 508 | } |
| 509 | |
Václav Kubernát | 19097f3 | 2020-10-05 10:08:29 +0200 | [diff] [blame] | 510 | SECTION("bits") |
| 511 | { |
| 512 | datastore.setLeaf("/example-schema:flags", bits_{{"sign", "carry"}}); |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 513 | REQUIRE_CALL(mockRunning, write("/example-schema:flags", std::nullopt, "carry sign"s)); |
Václav Kubernát | 19097f3 | 2020-10-05 10:08:29 +0200 | [diff] [blame] | 514 | datastore.commitChanges(); |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 515 | DatastoreAccess::Tree expected{ |
Václav Kubernát | 19097f3 | 2020-10-05 10:08:29 +0200 | [diff] [blame] | 516 | {"/example-schema:flags", bits_{{"carry", "sign"}}}, |
| 517 | }; |
| 518 | REQUIRE(datastore.getItems("/example-schema:flags") == expected); |
| 519 | } |
| 520 | |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 521 | #if not defined(yang_BACKEND) |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 522 | SECTION("operational data") |
| 523 | { |
| 524 | MockDataSupplier mockOpsData; |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 525 | OperationalDataSubscription opsDataSub("example-schema", "/example-schema:temperature", mockOpsData); |
Václav Kubernát | f90a0b5 | 2020-11-06 05:53:03 +0100 | [diff] [blame] | 526 | OperationalDataSubscription opsDataSub2("example-schema", "/example-schema:users", mockOpsData); |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 527 | DatastoreAccess::Tree expected; |
| 528 | std::string xpath; |
Václav Kubernát | f90a0b5 | 2020-11-06 05:53:03 +0100 | [diff] [blame] | 529 | |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 530 | SECTION("temperature") |
| 531 | { |
| 532 | expected = {{"/example-schema:temperature", int32_t{22}}}; |
| 533 | xpath = "/example-schema:temperature"; |
| 534 | } |
| 535 | |
Václav Kubernát | f90a0b5 | 2020-11-06 05:53:03 +0100 | [diff] [blame] | 536 | SECTION("key-less lists") |
| 537 | { |
| 538 | expected = { |
| 539 | {"/example-schema:users/userList[1]", special_{SpecialValue::List}}, |
| 540 | {"/example-schema:users/userList[1]/name", std::string{"John"}}, |
| 541 | {"/example-schema:users/userList[1]/otherfield", std::string{"LOL"}}, |
| 542 | {"/example-schema:users/userList[2]", special_{SpecialValue::List}}, |
| 543 | {"/example-schema:users/userList[2]/name", std::string{"Foo"}}, |
| 544 | {"/example-schema:users/userList[2]/otherfield", std::string{"Bar"}}, |
| 545 | }; |
| 546 | xpath = "/example-schema:users"; |
| 547 | } |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 548 | REQUIRE_CALL(mockOpsData, get_data(xpath)).RETURN(expected); |
| 549 | REQUIRE(datastore.getItems(xpath) == expected); |
| 550 | } |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 551 | #endif |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 552 | |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 553 | SECTION("leaf list") |
| 554 | { |
| 555 | DatastoreAccess::Tree expected; |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 556 | REQUIRE_CALL(mockRunning, write("/example-schema:addresses", std::nullopt, "0.0.0.0"s)); |
| 557 | REQUIRE_CALL(mockRunning, write("/example-schema:addresses", std::nullopt, "127.0.0.1"s)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 558 | datastore.createItem("/example-schema:addresses[.='0.0.0.0']"); |
| 559 | datastore.createItem("/example-schema:addresses[.='127.0.0.1']"); |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 560 | datastore.commitChanges(); |
| 561 | expected = { |
| 562 | {"/example-schema:addresses", special_{SpecialValue::LeafList}}, |
| 563 | {"/example-schema:addresses[.='0.0.0.0']", "0.0.0.0"s}, |
| 564 | {"/example-schema:addresses[.='127.0.0.1']", "127.0.0.1"s}, |
| 565 | }; |
| 566 | REQUIRE(datastore.getItems("/example-schema:addresses") == expected); |
| 567 | |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 568 | REQUIRE_CALL(mockRunning, write("/example-schema:addresses", "0.0.0.0"s, std::nullopt)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 569 | datastore.deleteItem("/example-schema:addresses[.='0.0.0.0']"); |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 570 | datastore.commitChanges(); |
| 571 | expected = { |
| 572 | {"/example-schema:addresses", special_{SpecialValue::LeafList}}, |
| 573 | {"/example-schema:addresses[.='127.0.0.1']", "127.0.0.1"s}, |
| 574 | }; |
| 575 | REQUIRE(datastore.getItems("/example-schema:addresses") == expected); |
| 576 | |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 577 | REQUIRE_CALL(mockRunning, write("/example-schema:addresses", "127.0.0.1"s, std::nullopt)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 578 | datastore.deleteItem("/example-schema:addresses[.='127.0.0.1']"); |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 579 | datastore.commitChanges(); |
| 580 | expected = {}; |
| 581 | REQUIRE(datastore.getItems("/example-schema:addresses") == expected); |
| 582 | } |
Jan Kundrát | bb525b4 | 2020-02-04 11:56:59 +0100 | [diff] [blame] | 583 | |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 584 | SECTION("deleting a non-existing leaf-list") |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 585 | { |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 586 | catching<OnKeyNotFound>([&] { |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 587 | datastore.deleteItem("/example-schema:addresses[.='non-existing']"); |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 588 | datastore.commitChanges(); |
| 589 | }); |
| 590 | } |
| 591 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 592 | SECTION("accessing a non-existing schema node as a leaf-list") |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 593 | { |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 594 | catching<OnInvalidSchemaPathCreate>([&] { |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 595 | datastore.createItem("/example-schema:non-existing[.='non-existing']"); |
| 596 | datastore.commitChanges(); |
| 597 | }); |
| 598 | |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 599 | catching<OnInvalidSchemaPathDelete>([&] { |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 600 | datastore.deleteItem("/example-schema:non-existing[.='non-existing']"); |
Jan Kundrát | 3867c9e | 2020-06-18 20:26:45 +0200 | [diff] [blame] | 601 | datastore.commitChanges(); |
| 602 | }); |
| 603 | } |
| 604 | |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 605 | SECTION("copying data from startup refreshes the data") |
| 606 | { |
| 607 | { |
| 608 | REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{}); |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 609 | REQUIRE_CALL(mockRunning, write("/example-schema:leafInt16", std::nullopt, "123"s)); |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 610 | datastore.setLeaf("/example-schema:leafInt16", int16_t{123}); |
| 611 | datastore.commitChanges(); |
| 612 | } |
| 613 | REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{{"/example-schema:leafInt16", int16_t{123}}}); |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 614 | REQUIRE_CALL(mockRunning, write("/example-schema:leafInt16", "123"s, std::nullopt)); |
Václav Kubernát | 7160a13 | 2020-04-03 02:11:01 +0200 | [diff] [blame] | 615 | datastore.copyConfig(Datastore::Startup, Datastore::Running); |
| 616 | REQUIRE(datastore.getItems("/example-schema:leafInt16") == DatastoreAccess::Tree{}); |
| 617 | } |
| 618 | |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 619 | SECTION("moving leaflist instances") |
| 620 | { |
| 621 | DatastoreAccess::Tree expected; |
| 622 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 623 | REQUIRE_CALL(mockRunning, write("/example-schema:protocols", std::nullopt, "http"s)); |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 624 | // FIXME: Why no notifications for these?? |
| 625 | // ... possibly because my subscription doesn't extract it properly? |
| 626 | // REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "ftp"s)); |
| 627 | // REQUIRE_CALL(mock, write("/example-schema:protocols", std::nullopt, "pop3"s)); |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 628 | REQUIRE_CALL(mockRunning, write("/example-schema:protocols", "http"s, "ftp"s)); |
| 629 | REQUIRE_CALL(mockRunning, write("/example-schema:protocols", "ftp"s, "pop3"s)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 630 | datastore.createItem("/example-schema:protocols[.='http']"); |
| 631 | datastore.createItem("/example-schema:protocols[.='ftp']"); |
| 632 | datastore.createItem("/example-schema:protocols[.='pop3']"); |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 633 | datastore.commitChanges(); |
| 634 | expected = { |
| 635 | {"/example-schema:protocols", special_{SpecialValue::LeafList}}, |
| 636 | {"/example-schema:protocols[.='http']", "http"s}, |
| 637 | {"/example-schema:protocols[.='ftp']", "ftp"s}, |
| 638 | {"/example-schema:protocols[.='pop3']", "pop3"s}, |
| 639 | }; |
| 640 | REQUIRE(datastore.getItems("/example-schema:protocols") == expected); |
| 641 | } |
| 642 | |
| 643 | std::string sourcePath; |
| 644 | SECTION("begin") |
| 645 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 646 | REQUIRE_CALL(mockRunning, write("/example-schema:protocols", std::nullopt, "pop3"s)); |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 647 | sourcePath = "/example-schema:protocols[.='pop3']"; |
| 648 | datastore.moveItem(sourcePath, yang::move::Absolute::Begin); |
| 649 | datastore.commitChanges(); |
| 650 | expected = { |
| 651 | {"/example-schema:protocols", special_{SpecialValue::LeafList}}, |
| 652 | {"/example-schema:protocols[.='pop3']", "pop3"s}, |
| 653 | {"/example-schema:protocols[.='http']", "http"s}, |
| 654 | {"/example-schema:protocols[.='ftp']", "ftp"s}, |
| 655 | }; |
| 656 | REQUIRE(datastore.getItems("/example-schema:protocols") == expected); |
| 657 | } |
| 658 | |
| 659 | SECTION("end") |
| 660 | { |
| 661 | sourcePath = "/example-schema:protocols[.='http']"; |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 662 | REQUIRE_CALL(mockRunning, write("/example-schema:protocols", "pop3"s, "http"s)); |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 663 | datastore.moveItem(sourcePath, yang::move::Absolute::End); |
| 664 | datastore.commitChanges(); |
| 665 | expected = { |
| 666 | {"/example-schema:protocols", special_{SpecialValue::LeafList}}, |
| 667 | {"/example-schema:protocols[.='ftp']", "ftp"s}, |
| 668 | {"/example-schema:protocols[.='pop3']", "pop3"s}, |
| 669 | {"/example-schema:protocols[.='http']", "http"s}, |
| 670 | }; |
| 671 | REQUIRE(datastore.getItems("/example-schema:protocols") == expected); |
| 672 | } |
| 673 | |
| 674 | SECTION("after") |
| 675 | { |
| 676 | sourcePath = "/example-schema:protocols[.='http']"; |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 677 | REQUIRE_CALL(mockRunning, write("/example-schema:protocols", "ftp"s, "http"s)); |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 678 | datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::After, {{".", "ftp"s}}}); |
| 679 | datastore.commitChanges(); |
| 680 | expected = { |
| 681 | {"/example-schema:protocols", special_{SpecialValue::LeafList}}, |
| 682 | {"/example-schema:protocols[.='ftp']", "ftp"s}, |
| 683 | {"/example-schema:protocols[.='http']", "http"s}, |
| 684 | {"/example-schema:protocols[.='pop3']", "pop3"s}, |
| 685 | }; |
| 686 | REQUIRE(datastore.getItems("/example-schema:protocols") == expected); |
| 687 | } |
| 688 | |
| 689 | SECTION("before") |
| 690 | { |
| 691 | sourcePath = "/example-schema:protocols[.='http']"; |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 692 | REQUIRE_CALL(mockRunning, write("/example-schema:protocols", "ftp"s, "http"s)); |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 693 | datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::Before, {{".", "pop3"s}}}); |
| 694 | datastore.commitChanges(); |
| 695 | expected = { |
| 696 | {"/example-schema:protocols", special_{SpecialValue::LeafList}}, |
| 697 | {"/example-schema:protocols[.='ftp']", "ftp"s}, |
| 698 | {"/example-schema:protocols[.='http']", "http"s}, |
| 699 | {"/example-schema:protocols[.='pop3']", "pop3"s}, |
| 700 | }; |
| 701 | REQUIRE(datastore.getItems("/example-schema:protocols") == expected); |
| 702 | } |
| 703 | } |
| 704 | |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 705 | SECTION("moving non-existing schema nodes") |
| 706 | { |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 707 | catching<OnInvalidSchemaPathMove>([&] { |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 708 | datastore.moveItem("/example-schema:non-existing", yang::move::Absolute::Begin); |
| 709 | datastore.commitChanges(); |
| 710 | }); |
| 711 | } |
| 712 | |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 713 | SECTION("moving list instances") |
| 714 | { |
| 715 | DatastoreAccess::Tree expected; |
| 716 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 717 | REQUIRE_CALL(mockRunning, write("/example-schema:players[name='John']", std::nullopt, ""s)); |
| 718 | REQUIRE_CALL(mockRunning, write("/example-schema:players[name='John']/name", std::nullopt, "John"s)); |
| 719 | REQUIRE_CALL(mockRunning, write("/example-schema:players[name='Eve']", ""s, ""s)); |
| 720 | REQUIRE_CALL(mockRunning, write("/example-schema:players[name='Eve']/name", std::nullopt, "Eve"s)); |
| 721 | REQUIRE_CALL(mockRunning, write("/example-schema:players[name='Adam']", ""s, ""s)); |
| 722 | REQUIRE_CALL(mockRunning, write("/example-schema:players[name='Adam']/name", std::nullopt, "Adam"s)); |
Jan Kundrát | cbf288b | 2020-06-18 20:44:39 +0200 | [diff] [blame] | 723 | datastore.createItem("/example-schema:players[name='John']"); |
| 724 | datastore.createItem("/example-schema:players[name='Eve']"); |
| 725 | datastore.createItem("/example-schema:players[name='Adam']"); |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 726 | datastore.commitChanges(); |
| 727 | expected = { |
| 728 | {"/example-schema:players[name='John']", special_{SpecialValue::List}}, |
| 729 | {"/example-schema:players[name='John']/name", "John"s}, |
| 730 | {"/example-schema:players[name='Eve']", special_{SpecialValue::List}}, |
| 731 | {"/example-schema:players[name='Eve']/name", "Eve"s}, |
| 732 | {"/example-schema:players[name='Adam']", special_{SpecialValue::List}}, |
| 733 | {"/example-schema:players[name='Adam']/name", "Adam"s}, |
| 734 | }; |
| 735 | REQUIRE(datastore.getItems("/example-schema:players") == expected); |
| 736 | } |
| 737 | |
| 738 | std::string sourcePath; |
| 739 | SECTION("begin") |
| 740 | { |
| 741 | sourcePath = "/example-schema:players[name='Adam']"; |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 742 | REQUIRE_CALL(mockRunning, write("/example-schema:players[name='Adam']", std::nullopt, ""s)); |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 743 | datastore.moveItem(sourcePath, yang::move::Absolute::Begin); |
| 744 | datastore.commitChanges(); |
| 745 | expected = { |
| 746 | {"/example-schema:players[name='Adam']", special_{SpecialValue::List}}, |
| 747 | {"/example-schema:players[name='Adam']/name", "Adam"s}, |
| 748 | {"/example-schema:players[name='John']", special_{SpecialValue::List}}, |
| 749 | {"/example-schema:players[name='John']/name", "John"s}, |
| 750 | {"/example-schema:players[name='Eve']", special_{SpecialValue::List}}, |
| 751 | {"/example-schema:players[name='Eve']/name", "Eve"s}, |
| 752 | }; |
| 753 | REQUIRE(datastore.getItems("/example-schema:players") == expected); |
| 754 | } |
| 755 | |
| 756 | SECTION("end") |
| 757 | { |
| 758 | sourcePath = "/example-schema:players[name='John']"; |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 759 | REQUIRE_CALL(mockRunning, write("/example-schema:players[name='John']", ""s, ""s)); |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 760 | datastore.moveItem(sourcePath, yang::move::Absolute::End); |
| 761 | datastore.commitChanges(); |
| 762 | expected = { |
| 763 | {"/example-schema:players[name='Eve']", special_{SpecialValue::List}}, |
| 764 | {"/example-schema:players[name='Eve']/name", "Eve"s}, |
| 765 | {"/example-schema:players[name='Adam']", special_{SpecialValue::List}}, |
| 766 | {"/example-schema:players[name='Adam']/name", "Adam"s}, |
| 767 | {"/example-schema:players[name='John']", special_{SpecialValue::List}}, |
| 768 | {"/example-schema:players[name='John']/name", "John"s}, |
| 769 | }; |
| 770 | REQUIRE(datastore.getItems("/example-schema:players") == expected); |
| 771 | } |
| 772 | |
| 773 | SECTION("after") |
| 774 | { |
| 775 | sourcePath = "/example-schema:players[name='John']"; |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 776 | REQUIRE_CALL(mockRunning, write("/example-schema:players[name='John']", ""s, ""s)); |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 777 | datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::After, {{"name", "Eve"s}}}); |
| 778 | datastore.commitChanges(); |
| 779 | expected = { |
| 780 | {"/example-schema:players[name='Eve']", special_{SpecialValue::List}}, |
| 781 | {"/example-schema:players[name='Eve']/name", "Eve"s}, |
| 782 | {"/example-schema:players[name='John']", special_{SpecialValue::List}}, |
| 783 | {"/example-schema:players[name='John']/name", "John"s}, |
| 784 | {"/example-schema:players[name='Adam']", special_{SpecialValue::List}}, |
| 785 | {"/example-schema:players[name='Adam']/name", "Adam"s}, |
| 786 | }; |
| 787 | REQUIRE(datastore.getItems("/example-schema:players") == expected); |
| 788 | } |
| 789 | |
| 790 | SECTION("before") |
| 791 | { |
| 792 | sourcePath = "/example-schema:players[name='John']"; |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 793 | REQUIRE_CALL(mockRunning, write("/example-schema:players[name='John']", ""s, ""s)); |
Václav Kubernát | bf65dd7 | 2020-05-28 02:32:31 +0200 | [diff] [blame] | 794 | datastore.moveItem(sourcePath, yang::move::Relative{yang::move::Relative::Position::Before, {{"name", "Adam"s}}}); |
| 795 | datastore.commitChanges(); |
| 796 | expected = { |
| 797 | {"/example-schema:players[name='Eve']", special_{SpecialValue::List}}, |
| 798 | {"/example-schema:players[name='Eve']/name", "Eve"s}, |
| 799 | {"/example-schema:players[name='John']", special_{SpecialValue::List}}, |
| 800 | {"/example-schema:players[name='John']/name", "John"s}, |
| 801 | {"/example-schema:players[name='Adam']", special_{SpecialValue::List}}, |
| 802 | {"/example-schema:players[name='Adam']/name", "Adam"s}, |
| 803 | }; |
| 804 | REQUIRE(datastore.getItems("/example-schema:players") == expected); |
| 805 | } |
| 806 | } |
| 807 | |
Václav Kubernát | fa96ac2 | 2020-06-18 17:03:52 +0200 | [diff] [blame] | 808 | SECTION("getting /") |
| 809 | { |
| 810 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 811 | REQUIRE_CALL(mockRunning, write("/example-schema:leafInt32", std::nullopt, "64"s)); |
Václav Kubernát | fa96ac2 | 2020-06-18 17:03:52 +0200 | [diff] [blame] | 812 | datastore.setLeaf("/example-schema:leafInt32", 64); |
| 813 | datastore.commitChanges(); |
| 814 | } |
| 815 | |
| 816 | DatastoreAccess::Tree expected{ |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 817 | {"/example-schema:leafInt32", 64} |
| 818 | }; |
Václav Kubernát | fa96ac2 | 2020-06-18 17:03:52 +0200 | [diff] [blame] | 819 | // This tests if we at least get the data WE added. |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 820 | REQUIRE(std::all_of(expected.begin(), expected.end(), [items = datastore.getItems("/")] (const auto& item) { |
| 821 | return std::find(items.begin(), items.end(), item) != items.end(); |
| 822 | })); |
Václav Kubernát | fa96ac2 | 2020-06-18 17:03:52 +0200 | [diff] [blame] | 823 | } |
| 824 | |
Václav Kubernát | 36986c5 | 2020-06-25 10:30:05 +0200 | [diff] [blame] | 825 | SECTION("setting and removing without commit") |
| 826 | { |
| 827 | datastore.setLeaf("/example-schema:leafInt32", 64); |
| 828 | datastore.deleteItem("/example-schema:leafInt32"); |
| 829 | } |
| 830 | |
Václav Kubernát | 70d7f7a | 2020-06-23 14:40:40 +0200 | [diff] [blame] | 831 | SECTION("two key lists") |
| 832 | { |
Václav Kubernát | eaf5668 | 2021-02-22 17:15:41 +0100 | [diff] [blame] | 833 | REQUIRE_CALL(mockRunning, write("/example-schema:point[x='12'][y='10']", std::nullopt, ""s)); |
| 834 | REQUIRE_CALL(mockRunning, write("/example-schema:point[x='12'][y='10']/x", std::nullopt, "12"s)); |
| 835 | REQUIRE_CALL(mockRunning, write("/example-schema:point[x='12'][y='10']/y", std::nullopt, "10"s)); |
Václav Kubernát | 70d7f7a | 2020-06-23 14:40:40 +0200 | [diff] [blame] | 836 | datastore.createItem("/example-schema:point[x='12'][y='10']"); |
| 837 | datastore.commitChanges(); |
| 838 | REQUIRE(datastore.dump(DataFormat::Json).find("example-schema:point") != std::string::npos); |
| 839 | } |
| 840 | |
Václav Kubernát | 7310938 | 2018-09-14 19:52:03 +0200 | [diff] [blame] | 841 | waitForCompletionAndBitMore(seq1); |
| 842 | } |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 843 | |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 844 | struct ActionCb { |
| 845 | int operator()(sysrepo::S_Session session, |
| 846 | const char* xpath, |
| 847 | [[maybe_unused]] const sysrepo::S_Vals input, |
| 848 | [[maybe_unused]] sr_event_t event, |
| 849 | [[maybe_unused]] uint32_t request_id, |
| 850 | sysrepo::S_Vals_Holder output) |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 851 | { |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 852 | if (session->get_context()->get_node(nullptr, xpath)->path(LYS_PATH_FIRST_PREFIX) == "/example-schema:ports/shutdown") { |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 853 | auto buf = output->allocate(1); |
| 854 | buf->val(0)->set(joinPaths(xpath, "success").c_str(), true); |
| 855 | return SR_ERR_OK; |
| 856 | } |
| 857 | throw std::runtime_error("unrecognized RPC"); |
| 858 | } |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 859 | }; |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 860 | |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 861 | struct RpcCb { |
| 862 | int operator()([[maybe_unused]] sysrepo::S_Session session, |
| 863 | const char* xpath, |
| 864 | const sysrepo::S_Vals input, |
| 865 | [[maybe_unused]] sr_event_t event, |
| 866 | [[maybe_unused]] uint32_t request_id, |
| 867 | sysrepo::S_Vals_Holder output) |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 868 | { |
| 869 | const auto nukes = "/example-schema:launch-nukes"s; |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 870 | if (xpath == "/example-schema:noop"s || xpath == "/example-schema:fire"s) { |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 871 | return SR_ERR_OK; |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | if (xpath == nukes) { |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 875 | uint64_t kilotons = 0; |
| 876 | bool hasCities = false; |
| 877 | for (size_t i = 0; i < input->val_cnt(); ++i) { |
| 878 | const auto& val = input->val(i); |
Václav Kubernát | f4b6a93 | 2020-07-09 10:34:19 +0200 | [diff] [blame] | 879 | if (val->xpath() == nukes + "/payload") { |
| 880 | continue; // ignore, container |
| 881 | } |
| 882 | if (val->xpath() == nukes + "/description") { |
| 883 | continue; // unused |
| 884 | } |
| 885 | |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 886 | if (val->xpath() == nukes + "/payload/kilotons") { |
| 887 | kilotons = val->data()->get_uint64(); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 888 | } else if (std::string_view{val->xpath()}.find(nukes + "/cities") == 0) { |
| 889 | hasCities = true; |
| 890 | } else { |
| 891 | throw std::runtime_error("RPC launch-nukes: unexpected input "s + val->xpath()); |
| 892 | } |
| 893 | } |
| 894 | if (kilotons == 333'666) { |
| 895 | // magic, just do not generate any output. This is important because the NETCONF RPC returns just <ok/>. |
| 896 | return SR_ERR_OK; |
| 897 | } |
| 898 | auto buf = output->allocate(2); |
| 899 | size_t i = 0; |
| 900 | buf->val(i++)->set((nukes + "/blast-radius").c_str(), uint32_t{33'666}); |
| 901 | buf->val(i++)->set((nukes + "/actual-yield").c_str(), static_cast<uint64_t>(1.33 * kilotons)); |
| 902 | if (hasCities) { |
| 903 | buf = output->reallocate(output->val_cnt() + 2); |
| 904 | buf->val(i++)->set((nukes + "/damaged-places/targets[city='London']/city").c_str(), "London"); |
| 905 | buf->val(i++)->set((nukes + "/damaged-places/targets[city='Berlin']/city").c_str(), "Berlin"); |
| 906 | } |
| 907 | return SR_ERR_OK; |
| 908 | } |
| 909 | throw std::runtime_error("unrecognized RPC"); |
| 910 | } |
| 911 | }; |
| 912 | |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 913 | TEST_CASE("rpc/action") |
| 914 | { |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 915 | trompeloeil::sequence seq1; |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 916 | |
| 917 | #ifdef sysrepo_BACKEND |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 918 | auto datastore = std::make_shared<SysrepoAccess>(); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 919 | #elif defined(netconf_BACKEND) |
Václav Kubernát | d1beedc | 2020-09-07 12:09:05 +0200 | [diff] [blame] | 920 | const auto NETOPEER_SOCKET = getenv("NETOPEER_SOCKET"); |
| 921 | auto datastore = std::make_shared<NetconfAccess>(NETOPEER_SOCKET); |
Václav Kubernát | 74487df | 2020-06-04 01:29:28 +0200 | [diff] [blame] | 922 | #elif defined(yang_BACKEND) |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 923 | auto datastore = std::make_shared<YangAccess>(); |
| 924 | datastore->addSchemaDir(schemaDir); |
| 925 | datastore->addSchemaFile(exampleSchemaFile); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 926 | #else |
| 927 | #error "Unknown backend" |
| 928 | #endif |
| 929 | |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 930 | auto srConn = std::make_shared<sysrepo::Connection>(); |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 931 | auto srSession = std::make_shared<sysrepo::Session>(srConn); |
| 932 | auto srSubscription = std::make_shared<sysrepo::Subscribe>(srSession); |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 933 | auto rpcCb = std::make_shared<RpcCb>(); |
| 934 | auto actionCb = std::make_shared<ActionCb>(); |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 935 | sysrepo::Logs{}.set_stderr(SR_LL_INF); |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 936 | SysrepoSubscription subscription("example-schema", nullptr); |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 937 | // careful here, sysrepo insists on module_change CBs being registered before RPC CBs, otherwise there's a memleak |
Václav Kubernát | 654303f | 2020-07-31 13:16:54 +0200 | [diff] [blame] | 938 | srSubscription->rpc_subscribe("/example-schema:noop", RpcCb{}, 0, SR_SUBSCR_CTX_REUSE); |
| 939 | srSubscription->rpc_subscribe("/example-schema:launch-nukes", RpcCb{}, 0, SR_SUBSCR_CTX_REUSE); |
| 940 | srSubscription->rpc_subscribe("/example-schema:fire", RpcCb{}, 0, SR_SUBSCR_CTX_REUSE); |
| 941 | srSubscription->rpc_subscribe("/example-schema:ports/shutdown", ActionCb{}, 0, SR_SUBSCR_CTX_REUSE); |
Václav Kubernát | e7248b2 | 2020-06-26 15:38:59 +0200 | [diff] [blame] | 942 | |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 943 | SECTION("rpc") |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 944 | { |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 945 | auto createTemporaryDatastore = [](const std::shared_ptr<DatastoreAccess>& datastore) { |
| 946 | return std::make_shared<YangAccess>(std::static_pointer_cast<YangSchema>(datastore->schema())); |
| 947 | }; |
| 948 | ProxyDatastore proxyDatastore(datastore, createTemporaryDatastore); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 949 | |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 950 | // ProxyDatastore cannot easily read DatastoreAccess::Tree, so we need to set the input via create/setLeaf/etc. |
| 951 | SECTION("valid") |
| 952 | { |
| 953 | std::string rpc; |
| 954 | DatastoreAccess::Tree input, output; |
| 955 | |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 956 | SECTION("noop") |
| 957 | { |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 958 | rpc = "/example-schema:noop"; |
Václav Kubernát | b3960f8 | 2020-12-01 03:21:48 +0100 | [diff] [blame] | 959 | proxyDatastore.initiate(rpc); |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 960 | } |
| 961 | |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 962 | SECTION("small nuke") |
| 963 | { |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 964 | rpc = "/example-schema:launch-nukes"; |
| 965 | input = { |
Václav Kubernát | 94bb7cf | 2021-02-03 09:59:39 +0100 | [diff] [blame] | 966 | {joinPaths(rpc, "description"), "dummy"s}, |
| 967 | {joinPaths(rpc, "payload/kilotons"), uint64_t{333'666}}, |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 968 | }; |
Václav Kubernát | b3960f8 | 2020-12-01 03:21:48 +0100 | [diff] [blame] | 969 | proxyDatastore.initiate(rpc); |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 970 | proxyDatastore.setLeaf("/example-schema:launch-nukes/example-schema:payload/example-schema:kilotons", uint64_t{333'666}); |
| 971 | // no data are returned |
| 972 | } |
| 973 | |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 974 | SECTION("small nuke") |
| 975 | { |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 976 | rpc = "/example-schema:launch-nukes"; |
| 977 | input = { |
Václav Kubernát | 94bb7cf | 2021-02-03 09:59:39 +0100 | [diff] [blame] | 978 | {joinPaths(rpc, "description"), "dummy"s}, |
| 979 | {joinPaths(rpc, "payload/kilotons"), uint64_t{4}}, |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 980 | }; |
Václav Kubernát | b3960f8 | 2020-12-01 03:21:48 +0100 | [diff] [blame] | 981 | proxyDatastore.initiate(rpc); |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 982 | proxyDatastore.setLeaf("/example-schema:launch-nukes/example-schema:payload/example-schema:kilotons", uint64_t{4}); |
| 983 | |
| 984 | output = { |
| 985 | {"blast-radius", uint32_t{33'666}}, |
| 986 | {"actual-yield", uint64_t{5}}, |
| 987 | }; |
| 988 | } |
| 989 | |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 990 | SECTION("with lists") |
| 991 | { |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 992 | rpc = "/example-schema:launch-nukes"; |
| 993 | input = { |
Václav Kubernát | 94bb7cf | 2021-02-03 09:59:39 +0100 | [diff] [blame] | 994 | {joinPaths(rpc, "payload/kilotons"), uint64_t{6}}, |
| 995 | {joinPaths(rpc, "cities/targets[city='Prague']/city"), "Prague"s}, |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 996 | }; |
Václav Kubernát | b3960f8 | 2020-12-01 03:21:48 +0100 | [diff] [blame] | 997 | proxyDatastore.initiate(rpc); |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 998 | proxyDatastore.setLeaf("/example-schema:launch-nukes/example-schema:payload/example-schema:kilotons", uint64_t{6}); |
| 999 | proxyDatastore.createItem("/example-schema:launch-nukes/example-schema:cities/example-schema:targets[city='Prague']"); |
| 1000 | output = { |
| 1001 | {"blast-radius", uint32_t{33'666}}, |
| 1002 | {"actual-yield", uint64_t{7}}, |
| 1003 | {"damaged-places", special_{SpecialValue::PresenceContainer}}, |
| 1004 | {"damaged-places/targets[city='London']", special_{SpecialValue::List}}, |
| 1005 | {"damaged-places/targets[city='London']/city", "London"s}, |
| 1006 | {"damaged-places/targets[city='Berlin']", special_{SpecialValue::List}}, |
| 1007 | {"damaged-places/targets[city='Berlin']/city", "Berlin"s}, |
| 1008 | }; |
| 1009 | } |
| 1010 | |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 1011 | SECTION("with leafref") |
| 1012 | { |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 1013 | datastore->createItem("/example-schema:person[name='Colton']"); |
| 1014 | datastore->commitChanges(); |
| 1015 | |
| 1016 | rpc = "/example-schema:fire"; |
| 1017 | input = { |
Václav Kubernát | 94bb7cf | 2021-02-03 09:59:39 +0100 | [diff] [blame] | 1018 | {joinPaths(rpc, "whom"), "Colton"s} |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 1019 | }; |
Václav Kubernát | b3960f8 | 2020-12-01 03:21:48 +0100 | [diff] [blame] | 1020 | proxyDatastore.initiate(rpc); |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 1021 | proxyDatastore.setLeaf("/example-schema:fire/example-schema:whom", "Colton"s); |
| 1022 | } |
| 1023 | |
Václav Kubernát | b3960f8 | 2020-12-01 03:21:48 +0100 | [diff] [blame] | 1024 | catching<OnExec>([&] { REQUIRE(datastore->execute(rpc, input) == output); }); |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 1025 | catching<OnExec>([&] { REQUIRE(proxyDatastore.execute() == output); }); |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 1026 | } |
| 1027 | |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 1028 | SECTION("non-existing RPC") |
| 1029 | { |
Václav Kubernát | b3960f8 | 2020-12-01 03:21:48 +0100 | [diff] [blame] | 1030 | catching<OnInvalidRpcPath>([&] { datastore->execute("/example-schema:non-existing", DatastoreAccess::Tree{}); }); |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 1031 | } |
Václav Kubernát | 2edfe54 | 2021-02-03 08:08:29 +0100 | [diff] [blame] | 1032 | |
| 1033 | SECTION("invalid RPC exec resets temporary datastore") |
| 1034 | { |
| 1035 | proxyDatastore.initiate("/example-schema:setIp"); |
| 1036 | catching<OnInvalidRpcInput>([&] { auto output = proxyDatastore.execute(); }); |
| 1037 | REQUIRE(proxyDatastore.inputDatastorePath() == std::nullopt); |
| 1038 | } |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 1039 | } |
| 1040 | |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 1041 | SECTION("action") |
Jan Kundrát | 7ec214d | 2020-06-19 17:05:07 +0200 | [diff] [blame] | 1042 | { |
Václav Kubernát | 94bb7cf | 2021-02-03 09:59:39 +0100 | [diff] [blame] | 1043 | auto createTemporaryDatastore = [](const std::shared_ptr<DatastoreAccess>& datastore) { |
| 1044 | return std::make_shared<YangAccess>(std::static_pointer_cast<YangSchema>(datastore->schema())); |
| 1045 | }; |
| 1046 | ProxyDatastore proxyDatastore(datastore, createTemporaryDatastore); |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 1047 | std::string path; |
| 1048 | DatastoreAccess::Tree input, output; |
| 1049 | |
| 1050 | output = { |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 1051 | {"success", true} |
| 1052 | }; |
| 1053 | datastore->createItem("/example-schema:ports[name='A']"); |
| 1054 | datastore->commitChanges(); |
Václav Kubernát | b4e5b18 | 2020-11-16 19:55:09 +0100 | [diff] [blame] | 1055 | SECTION("shutdown") |
| 1056 | { |
Václav Kubernát | 94bb7cf | 2021-02-03 09:59:39 +0100 | [diff] [blame] | 1057 | path = "/example-schema:ports[name='A']/example-schema:shutdown"; |
| 1058 | input = { |
| 1059 | {"/example-schema:ports[name='A']/shutdown/force", true} |
| 1060 | }; |
| 1061 | proxyDatastore.initiate(path); |
| 1062 | proxyDatastore.setLeaf("/example-schema:ports[name='A']/example-schema:shutdown/example-schema:force", true); |
| 1063 | |
Václav Kubernát | a878960 | 2020-07-20 15:18:19 +0200 | [diff] [blame] | 1064 | } |
| 1065 | |
Václav Kubernát | b3960f8 | 2020-12-01 03:21:48 +0100 | [diff] [blame] | 1066 | catching<OnExec>([&] { REQUIRE(datastore->execute(path, input) == output); }); |
Václav Kubernát | 94bb7cf | 2021-02-03 09:59:39 +0100 | [diff] [blame] | 1067 | catching<OnExec>([&] { REQUIRE(proxyDatastore.execute() == output); }); |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 1068 | } |
| 1069 | |
Jan Kundrát | 6ee8479 | 2020-01-24 01:43:36 +0100 | [diff] [blame] | 1070 | waitForCompletionAndBitMore(seq1); |
| 1071 | } |
Václav Kubernát | f5d7515 | 2020-12-03 03:52:34 +0100 | [diff] [blame] | 1072 | |
| 1073 | #if not defined(yang_BACKEND) |
| 1074 | TEST_CASE("datastore targets") |
| 1075 | { |
| 1076 | const auto testNode = "/example-schema:leafInt32"; |
| 1077 | { |
| 1078 | auto conn = std::make_shared<sysrepo::Connection>(); |
| 1079 | auto sess = std::make_shared<sysrepo::Session>(conn); |
| 1080 | sess->delete_item(testNode); |
| 1081 | sess->apply_changes(1000, 1); |
| 1082 | sess->session_switch_ds(SR_DS_STARTUP); |
| 1083 | sess->delete_item(testNode); |
| 1084 | sess->apply_changes(1000, 1); |
| 1085 | } |
| 1086 | MockRecorder mockRunning; |
| 1087 | MockRecorder mockStartup; |
| 1088 | |
| 1089 | #ifdef sysrepo_BACKEND |
| 1090 | SysrepoAccess datastore; |
| 1091 | #elif defined(netconf_BACKEND) |
| 1092 | const auto NETOPEER_SOCKET = getenv("NETOPEER_SOCKET"); |
| 1093 | NetconfAccess datastore(NETOPEER_SOCKET); |
| 1094 | #else |
| 1095 | #error "Unknown backend" |
| 1096 | #endif |
| 1097 | |
| 1098 | auto testGetItems = [&datastore] (const auto& path, const DatastoreAccess::Tree& expected) { |
| 1099 | REQUIRE(datastore.getItems(path) == expected); |
| 1100 | }; |
| 1101 | |
| 1102 | SECTION("subscriptions change operational target") |
| 1103 | { |
| 1104 | // Default target is operational, so setting a value and reading it should return no values as there are no |
| 1105 | // subscriptions yet. |
| 1106 | datastore.setLeaf(testNode, 10); |
| 1107 | datastore.commitChanges(); |
| 1108 | testGetItems(testNode, {}); |
| 1109 | |
| 1110 | // Now we create a subscription and try again. |
| 1111 | SysrepoSubscription subRunning("example-schema", &mockRunning); |
| 1112 | testGetItems(testNode, {{testNode, 10}}); |
| 1113 | } |
| 1114 | |
| 1115 | SECTION("running shows stuff even without subscriptions") |
| 1116 | { |
| 1117 | datastore.setTarget(DatastoreTarget::Running); |
| 1118 | datastore.setLeaf(testNode, 10); |
| 1119 | datastore.commitChanges(); |
| 1120 | testGetItems(testNode, {{testNode, 10}}); |
| 1121 | |
| 1122 | // Actually creating a subscription shouldn't make a difference. |
| 1123 | SysrepoSubscription subRunning("example-schema", &mockRunning); |
| 1124 | testGetItems(testNode, {{testNode, 10}}); |
| 1125 | } |
| 1126 | |
| 1127 | SECTION("startup changes only affect startup") |
| 1128 | { |
| 1129 | datastore.setTarget(DatastoreTarget::Startup); |
| 1130 | datastore.setLeaf(testNode, 10); |
| 1131 | datastore.commitChanges(); |
| 1132 | testGetItems(testNode, {{testNode, 10}}); |
| 1133 | datastore.setTarget(DatastoreTarget::Running); |
| 1134 | testGetItems(testNode, {}); |
| 1135 | datastore.setTarget(DatastoreTarget::Operational); |
| 1136 | testGetItems(testNode, {}); |
| 1137 | } |
| 1138 | |
| 1139 | } |
| 1140 | #endif |