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