blob: 64250da9ed5973b565f32f134d123b08d46679fc [file] [log] [blame]
Václav Kubernátab612e92019-11-26 19:51:31 +01001/*
2 * Copyright (C) 2019 CESNET, https://photonics.cesnet.cz/
3 *
4 * Written by Václav Kubernát <kubervac@fit.cvut.cz>
5 *
6*/
7
Václav Kubernátb4e5b182020-11-16 19:55:09 +01008#include "trompeloeil_doctest.hpp"
Václav Kubernátab612e92019-11-26 19:51:31 +01009#include <experimental/iterator>
Václav Kubernát654303f2020-07-31 13:16:54 +020010#include <sysrepo-cpp/Session.hpp>
Václav Kubernátab612e92019-11-26 19:51:31 +010011
12#ifdef sysrepo_BACKEND
13#include "sysrepo_access.hpp"
14#elif defined(netconf_BACKEND)
15#include "netconf_access.hpp"
Václav Kubernát74487df2020-06-04 01:29:28 +020016#elif defined(yang_BACKEND)
17#include "yang_access.hpp"
18#include "yang_access_test_vars.hpp"
Václav Kubernátab612e92019-11-26 19:51:31 +010019#else
20#error "Unknown backend"
21#endif
22#include "data_query.hpp"
Václav Kubernáted824d02020-06-09 15:48:30 +020023#include "pretty_printers.hpp"
Václav Kubernátab612e92019-11-26 19:51:31 +010024#include "sysrepo_subscription.hpp"
25#include "utils.hpp"
26
Václav Kubernátab612e92019-11-26 19:51:31 +010027
28TEST_CASE("data query")
29{
30 trompeloeil::sequence seq1;
Václav Kubernát654303f2020-07-31 13:16:54 +020031 {
32 auto conn = std::make_shared<sysrepo::Connection>();
33 auto sess = std::make_shared<sysrepo::Session>(conn);
34 sess->copy_config(SR_DS_STARTUP, "example-schema", 1000, true);
35 }
Václav Kubernátab612e92019-11-26 19:51:31 +010036 SysrepoSubscription subscriptionExample("example-schema");
37 SysrepoSubscription subscriptionOther("other-module");
38
39#ifdef sysrepo_BACKEND
Václav Kubernátf5d75152020-12-03 03:52:34 +010040 SysrepoAccess datastore;
Václav Kubernátab612e92019-11-26 19:51:31 +010041#elif defined(netconf_BACKEND)
Václav Kubernátd1beedc2020-09-07 12:09:05 +020042 const auto NETOPEER_SOCKET = getenv("NETOPEER_SOCKET");
43 NetconfAccess datastore(NETOPEER_SOCKET);
Václav Kubernát74487df2020-06-04 01:29:28 +020044#elif defined(yang_BACKEND)
45 YangAccess datastore;
46 datastore.addSchemaDir(schemaDir);
47 datastore.addSchemaFile(exampleSchemaFile);
Václav Kubernátab612e92019-11-26 19:51:31 +010048#else
49#error "Unknown backend"
50#endif
51
52 DataQuery dataquery(datastore);
53
54 SECTION("listKeys")
55 {
Václav Kubernát2db124c2020-05-28 21:58:36 +020056 dataPath_ listPath;
57 listPath.m_scope = Scope::Absolute;
Václav Kubernátc15fe822020-06-04 11:28:39 +020058 std::vector<ListInstance> expected;
Václav Kubernátab612e92019-11-26 19:51:31 +010059
60 SECTION("example-schema:person")
61 {
Jan Kundrátcbf288b2020-06-18 20:44:39 +020062 datastore.createItem("/example-schema:person[name='Vaclav']");
63 datastore.createItem("/example-schema:person[name='Tomas']");
64 datastore.createItem("/example-schema:person[name='Jan Novak']");
Václav Kubernátfaacd022020-07-08 16:44:38 +020065 listPath.m_nodes.emplace_back(module_{"example-schema"}, list_{"person"});
Václav Kubernátab612e92019-11-26 19:51:31 +010066 expected = {
67 {{"name", std::string{"Jan Novak"}}},
68 {{"name", std::string{"Tomas"}}},
69 {{"name", std::string{"Vaclav"}}}
70 };
71 }
72
Václav Kubernát45e55462020-02-04 11:19:32 +010073 SECTION("example-schema:person - no instances")
74 {
Václav Kubernátfaacd022020-07-08 16:44:38 +020075 listPath.m_nodes.emplace_back(module_{"example-schema"}, list_{"person"});
Václav Kubernát45e55462020-02-04 11:19:32 +010076 expected = {
77 };
78 }
79
Václav Kubernátab612e92019-11-26 19:51:31 +010080 SECTION("example-schema:selectedNumbers")
81 {
Jan Kundrátcbf288b2020-06-18 20:44:39 +020082 datastore.createItem("/example-schema:selectedNumbers[value='45']");
83 datastore.createItem("/example-schema:selectedNumbers[value='99']");
84 datastore.createItem("/example-schema:selectedNumbers[value='127']");
Václav Kubernátfaacd022020-07-08 16:44:38 +020085 listPath.m_nodes.emplace_back(module_{"example-schema"}, list_{"selectedNumbers"});
Václav Kubernátab612e92019-11-26 19:51:31 +010086 expected = {
87 {{"value", int8_t{127}}},
88 {{"value", int8_t{45}}},
89 {{"value", int8_t{99}}}
90 };
91 }
92
93 SECTION("example-schema:animalWithColor")
94 {
Jan Kundrátcbf288b2020-06-18 20:44:39 +020095 datastore.createItem("/example-schema:animalWithColor[name='Dog'][color='brown']");
96 datastore.createItem("/example-schema:animalWithColor[name='Dog'][color='white']");
97 datastore.createItem("/example-schema:animalWithColor[name='Cat'][color='grey']");
Václav Kubernátfaacd022020-07-08 16:44:38 +020098 listPath.m_nodes.emplace_back(module_{"example-schema"}, list_{"animalWithColor"});
Václav Kubernátab612e92019-11-26 19:51:31 +010099 expected = {
100 {{"name", std::string{"Cat"}}, {"color", std::string{"grey"}}},
101 {{"name", std::string{"Dog"}}, {"color", std::string{"brown"}}},
102 {{"name", std::string{"Dog"}}, {"color", std::string{"white"}}}
103 };
104 }
105
106 SECTION("example-schema:animalWithColor - quotes in values")
107 {
Václav Kubernát1db04ca2020-07-08 17:23:26 +0200108 datastore.createItem(R"(/example-schema:animalWithColor[name='D"o"g'][color="b'r'own"])");
Václav Kubernátfaacd022020-07-08 16:44:38 +0200109 listPath.m_nodes.emplace_back(module_{"example-schema"}, list_{"animalWithColor"});
Václav Kubernátab612e92019-11-26 19:51:31 +0100110 expected = {
111 {{"name", std::string{"D\"o\"g"}}, {"color", std::string{"b'r'own"}}}
112 };
113 }
114
115 SECTION("example-schema:ports")
116 {
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200117 datastore.createItem("/example-schema:ports[name='A']");
118 datastore.createItem("/example-schema:ports[name='B']");
119 datastore.createItem("/example-schema:ports[name='E']");
Václav Kubernátfaacd022020-07-08 16:44:38 +0200120 listPath.m_nodes.emplace_back(module_{"example-schema"}, list_{"ports"});
Václav Kubernátab612e92019-11-26 19:51:31 +0100121 expected = {
122 {{"name", enum_{"A"}}},
123 {{"name", enum_{"B"}}},
124 {{"name", enum_{"E"}}},
125 };
126 }
127
128 SECTION("example-schema:org/example:people - nested list")
129 {
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200130 datastore.createItem("/example-schema:org[department='accounting']");
131 datastore.createItem("/example-schema:org[department='sales']");
132 datastore.createItem("/example-schema:org[department='programmers']");
133 datastore.createItem("/example-schema:org[department='accounting']/people[name='Alice']");
134 datastore.createItem("/example-schema:org[department='accounting']/people[name='Bob']");
135 datastore.createItem("/example-schema:org[department='sales']/people[name='Alice']");
136 datastore.createItem("/example-schema:org[department='sales']/people[name='Cyril']");
137 datastore.createItem("/example-schema:org[department='sales']/people[name='Alice']/computers[type='laptop']");
138 datastore.createItem("/example-schema:org[department='sales']/people[name='Alice']/computers[type='server']");
139 datastore.createItem("/example-schema:org[department='sales']/people[name='Cyril']/computers[type='PC']");
140 datastore.createItem("/example-schema:org[department='sales']/people[name='Cyril']/computers[type='server']");
Václav Kubernátab612e92019-11-26 19:51:31 +0100141
142 SECTION("outer list")
143 {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200144 listPath.m_nodes.emplace_back(module_{"example-schema"}, list_{"org"});
Václav Kubernátab612e92019-11-26 19:51:31 +0100145 expected = {
146 {{"department", std::string{"accounting"}}},
147 {{"department", std::string{"sales"}}},
148 {{"department", std::string{"programmers"}}},
149 };
150 }
151
152 SECTION("nested list")
153 {
154 listElement_ list;
155 list.m_name = "org";
Václav Kubernátab612e92019-11-26 19:51:31 +0100156 SECTION("accounting department")
157 {
158 list.m_keys = {
159 {"department", std::string{"accounting"}}
160 };
161 expected = {
162 {{"name", std::string{"Alice"}}},
163 {{"name", std::string{"Bob"}}},
164 };
165 }
166 SECTION("sales department")
167 {
168 list.m_keys = {
169 {"department", std::string{"sales"}}
170 };
171 expected = {
172 {{"name", std::string{"Alice"}}},
173 {{"name", std::string{"Cyril"}}},
174 };
175 }
176 SECTION("programmers department")
177 {
178 list.m_keys = {
179 {"department", std::string{"programmers"}}
180 };
181 expected = {
182 };
183 }
Václav Kubernátfaacd022020-07-08 16:44:38 +0200184 listPath.m_nodes.emplace_back(module_{"example-schema"}, list);
185 listPath.m_nodes.emplace_back(list_{"people"});
Václav Kubernátab612e92019-11-26 19:51:31 +0100186 }
187
188 SECTION("THREE MF NESTED LISTS")
189 {
190 listElement_ listOrg;
191 listOrg.m_name = "org";
192 listOrg.m_keys = {
193 {"department", std::string{"sales"}}
194 };
195
196 listElement_ listPeople;
Václav Kubernátab612e92019-11-26 19:51:31 +0100197
198 SECTION("alice computers")
199 {
200 listPeople.m_name = "people";
201 listPeople.m_keys = {
202 {"name", std::string{"Alice"}}
203 };
204 expected = {
205 {{"type", enum_{"laptop"}}},
206 {{"type", enum_{"server"}}},
207 };
Václav Kubernátab612e92019-11-26 19:51:31 +0100208 }
209
210 SECTION("cyril computers")
211 {
212 listPeople.m_name = "people";
213 listPeople.m_keys = {
214 {"name", std::string{"Cyril"}}
215 };
216 expected = {
217 {{"type", enum_{"PC"}}},
218 {{"type", enum_{"server"}}},
219 };
220 }
221
Václav Kubernátfaacd022020-07-08 16:44:38 +0200222 listPath.m_nodes.emplace_back(module_{"example-schema"}, listOrg);
223 listPath.m_nodes.emplace_back(listPeople);
224 listPath.m_nodes.emplace_back(list_{"computers"});
Václav Kubernátab612e92019-11-26 19:51:31 +0100225 }
226 }
227
228 SECTION("/other-module:parking-lot/example-schema:cars - list coming from an augment")
229 {
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200230 datastore.createItem("/other-module:parking-lot/example-schema:cars[id='1']");
231 datastore.createItem("/other-module:parking-lot/example-schema:cars[id='2']");
Václav Kubernátab612e92019-11-26 19:51:31 +0100232
Václav Kubernátfaacd022020-07-08 16:44:38 +0200233 listPath.m_nodes.emplace_back(module_{"other-module"}, container_{"parking-lot"});
234 listPath.m_nodes.emplace_back(module_{"example-schema"}, list_{"cars"});
Václav Kubernátab612e92019-11-26 19:51:31 +0100235 expected = {
236 {{"id", int32_t{1}}},
237 {{"id", int32_t{2}}},
238 };
Václav Kubernátab612e92019-11-26 19:51:31 +0100239 }
240
241 datastore.commitChanges();
242 std::sort(expected.begin(), expected.end());
Václav Kubernát2db124c2020-05-28 21:58:36 +0200243 auto keys = dataquery.listKeys(listPath);
Václav Kubernátab612e92019-11-26 19:51:31 +0100244 std::sort(keys.begin(), keys.end());
245 REQUIRE(keys == expected);
246 }
247
Václav Kubernát0f362fb2020-06-24 15:16:56 +0200248 SECTION("empty datastore")
249 {
250 dataPath_ listPath;
251 listPath.m_scope = Scope::Absolute;
Václav Kubernátfaacd022020-07-08 16:44:38 +0200252 listPath.m_nodes.emplace_back(module_{"example-schema"}, list_{"person"});
Václav Kubernát0f362fb2020-06-24 15:16:56 +0200253 auto keys = dataquery.listKeys(listPath);
254 REQUIRE(keys == std::vector<ListInstance>{});
255 }
256
Václav Kubernátab612e92019-11-26 19:51:31 +0100257 waitForCompletionAndBitMore(seq1);
258}