blob: 10524ea12d75e8e6d4e0a760494a626a5e8e1234 [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
8#include <experimental/iterator>
Václav Kubernát26b56082020-02-03 18:28:56 +01009#include "trompeloeil_doctest.hpp"
Václav Kubernátab612e92019-11-26 19:51:31 +010010
11#ifdef sysrepo_BACKEND
12#include "sysrepo_access.hpp"
13#elif defined(netconf_BACKEND)
14#include "netconf_access.hpp"
15#include "netopeer_vars.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;
31 SysrepoSubscription subscriptionExample("example-schema");
32 SysrepoSubscription subscriptionOther("other-module");
33
34#ifdef sysrepo_BACKEND
Václav Kubernát715c85c2020-04-14 01:46:08 +020035 SysrepoAccess datastore("netconf-cli-test", Datastore::Running);
Václav Kubernátab612e92019-11-26 19:51:31 +010036#elif defined(netconf_BACKEND)
37 NetconfAccess datastore(NETOPEER_SOCKET_PATH);
Václav Kubernát74487df2020-06-04 01:29:28 +020038#elif defined(yang_BACKEND)
39 YangAccess datastore;
40 datastore.addSchemaDir(schemaDir);
41 datastore.addSchemaFile(exampleSchemaFile);
Václav Kubernátab612e92019-11-26 19:51:31 +010042#else
43#error "Unknown backend"
44#endif
45
46 DataQuery dataquery(datastore);
47
48 SECTION("listKeys")
49 {
Václav Kubernát2db124c2020-05-28 21:58:36 +020050 dataPath_ listPath;
51 listPath.m_scope = Scope::Absolute;
Václav Kubernátc15fe822020-06-04 11:28:39 +020052 std::vector<ListInstance> expected;
Václav Kubernátab612e92019-11-26 19:51:31 +010053
54 SECTION("example-schema:person")
55 {
Jan Kundrátcbf288b2020-06-18 20:44:39 +020056 datastore.createItem("/example-schema:person[name='Vaclav']");
57 datastore.createItem("/example-schema:person[name='Tomas']");
58 datastore.createItem("/example-schema:person[name='Jan Novak']");
Václav Kubernátfaacd022020-07-08 16:44:38 +020059 listPath.m_nodes.emplace_back(module_{"example-schema"}, list_{"person"});
Václav Kubernátab612e92019-11-26 19:51:31 +010060 expected = {
61 {{"name", std::string{"Jan Novak"}}},
62 {{"name", std::string{"Tomas"}}},
63 {{"name", std::string{"Vaclav"}}}
64 };
65 }
66
Václav Kubernát45e55462020-02-04 11:19:32 +010067 SECTION("example-schema:person - no instances")
68 {
Václav Kubernátfaacd022020-07-08 16:44:38 +020069 listPath.m_nodes.emplace_back(module_{"example-schema"}, list_{"person"});
Václav Kubernát45e55462020-02-04 11:19:32 +010070 expected = {
71 };
72 }
73
Václav Kubernátab612e92019-11-26 19:51:31 +010074 SECTION("example-schema:selectedNumbers")
75 {
Jan Kundrátcbf288b2020-06-18 20:44:39 +020076 datastore.createItem("/example-schema:selectedNumbers[value='45']");
77 datastore.createItem("/example-schema:selectedNumbers[value='99']");
78 datastore.createItem("/example-schema:selectedNumbers[value='127']");
Václav Kubernátfaacd022020-07-08 16:44:38 +020079 listPath.m_nodes.emplace_back(module_{"example-schema"}, list_{"selectedNumbers"});
Václav Kubernátab612e92019-11-26 19:51:31 +010080 expected = {
81 {{"value", int8_t{127}}},
82 {{"value", int8_t{45}}},
83 {{"value", int8_t{99}}}
84 };
85 }
86
87 SECTION("example-schema:animalWithColor")
88 {
Jan Kundrátcbf288b2020-06-18 20:44:39 +020089 datastore.createItem("/example-schema:animalWithColor[name='Dog'][color='brown']");
90 datastore.createItem("/example-schema:animalWithColor[name='Dog'][color='white']");
91 datastore.createItem("/example-schema:animalWithColor[name='Cat'][color='grey']");
Václav Kubernátfaacd022020-07-08 16:44:38 +020092 listPath.m_nodes.emplace_back(module_{"example-schema"}, list_{"animalWithColor"});
Václav Kubernátab612e92019-11-26 19:51:31 +010093 expected = {
94 {{"name", std::string{"Cat"}}, {"color", std::string{"grey"}}},
95 {{"name", std::string{"Dog"}}, {"color", std::string{"brown"}}},
96 {{"name", std::string{"Dog"}}, {"color", std::string{"white"}}}
97 };
98 }
99
100 SECTION("example-schema:animalWithColor - quotes in values")
101 {
Václav Kubernát1db04ca2020-07-08 17:23:26 +0200102 datastore.createItem(R"(/example-schema:animalWithColor[name='D"o"g'][color="b'r'own"])");
Václav Kubernátfaacd022020-07-08 16:44:38 +0200103 listPath.m_nodes.emplace_back(module_{"example-schema"}, list_{"animalWithColor"});
Václav Kubernátab612e92019-11-26 19:51:31 +0100104 expected = {
105 {{"name", std::string{"D\"o\"g"}}, {"color", std::string{"b'r'own"}}}
106 };
107 }
108
109 SECTION("example-schema:ports")
110 {
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200111 datastore.createItem("/example-schema:ports[name='A']");
112 datastore.createItem("/example-schema:ports[name='B']");
113 datastore.createItem("/example-schema:ports[name='E']");
Václav Kubernátfaacd022020-07-08 16:44:38 +0200114 listPath.m_nodes.emplace_back(module_{"example-schema"}, list_{"ports"});
Václav Kubernátab612e92019-11-26 19:51:31 +0100115 expected = {
116 {{"name", enum_{"A"}}},
117 {{"name", enum_{"B"}}},
118 {{"name", enum_{"E"}}},
119 };
120 }
121
122 SECTION("example-schema:org/example:people - nested list")
123 {
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200124 datastore.createItem("/example-schema:org[department='accounting']");
125 datastore.createItem("/example-schema:org[department='sales']");
126 datastore.createItem("/example-schema:org[department='programmers']");
127 datastore.createItem("/example-schema:org[department='accounting']/people[name='Alice']");
128 datastore.createItem("/example-schema:org[department='accounting']/people[name='Bob']");
129 datastore.createItem("/example-schema:org[department='sales']/people[name='Alice']");
130 datastore.createItem("/example-schema:org[department='sales']/people[name='Cyril']");
131 datastore.createItem("/example-schema:org[department='sales']/people[name='Alice']/computers[type='laptop']");
132 datastore.createItem("/example-schema:org[department='sales']/people[name='Alice']/computers[type='server']");
133 datastore.createItem("/example-schema:org[department='sales']/people[name='Cyril']/computers[type='PC']");
134 datastore.createItem("/example-schema:org[department='sales']/people[name='Cyril']/computers[type='server']");
Václav Kubernátab612e92019-11-26 19:51:31 +0100135
136 SECTION("outer list")
137 {
Václav Kubernátfaacd022020-07-08 16:44:38 +0200138 listPath.m_nodes.emplace_back(module_{"example-schema"}, list_{"org"});
Václav Kubernátab612e92019-11-26 19:51:31 +0100139 expected = {
140 {{"department", std::string{"accounting"}}},
141 {{"department", std::string{"sales"}}},
142 {{"department", std::string{"programmers"}}},
143 };
144 }
145
146 SECTION("nested list")
147 {
148 listElement_ list;
149 list.m_name = "org";
Václav Kubernátab612e92019-11-26 19:51:31 +0100150 SECTION("accounting department")
151 {
152 list.m_keys = {
153 {"department", std::string{"accounting"}}
154 };
155 expected = {
156 {{"name", std::string{"Alice"}}},
157 {{"name", std::string{"Bob"}}},
158 };
159 }
160 SECTION("sales department")
161 {
162 list.m_keys = {
163 {"department", std::string{"sales"}}
164 };
165 expected = {
166 {{"name", std::string{"Alice"}}},
167 {{"name", std::string{"Cyril"}}},
168 };
169 }
170 SECTION("programmers department")
171 {
172 list.m_keys = {
173 {"department", std::string{"programmers"}}
174 };
175 expected = {
176 };
177 }
Václav Kubernátfaacd022020-07-08 16:44:38 +0200178 listPath.m_nodes.emplace_back(module_{"example-schema"}, list);
179 listPath.m_nodes.emplace_back(list_{"people"});
Václav Kubernátab612e92019-11-26 19:51:31 +0100180 }
181
182 SECTION("THREE MF NESTED LISTS")
183 {
184 listElement_ listOrg;
185 listOrg.m_name = "org";
186 listOrg.m_keys = {
187 {"department", std::string{"sales"}}
188 };
189
190 listElement_ listPeople;
Václav Kubernátab612e92019-11-26 19:51:31 +0100191
192 SECTION("alice computers")
193 {
194 listPeople.m_name = "people";
195 listPeople.m_keys = {
196 {"name", std::string{"Alice"}}
197 };
198 expected = {
199 {{"type", enum_{"laptop"}}},
200 {{"type", enum_{"server"}}},
201 };
202
203 }
204
205 SECTION("cyril computers")
206 {
207 listPeople.m_name = "people";
208 listPeople.m_keys = {
209 {"name", std::string{"Cyril"}}
210 };
211 expected = {
212 {{"type", enum_{"PC"}}},
213 {{"type", enum_{"server"}}},
214 };
215 }
216
Václav Kubernátfaacd022020-07-08 16:44:38 +0200217 listPath.m_nodes.emplace_back(module_{"example-schema"}, listOrg);
218 listPath.m_nodes.emplace_back(listPeople);
219 listPath.m_nodes.emplace_back(list_{"computers"});
Václav Kubernátab612e92019-11-26 19:51:31 +0100220 }
221 }
222
223 SECTION("/other-module:parking-lot/example-schema:cars - list coming from an augment")
224 {
Jan Kundrátcbf288b2020-06-18 20:44:39 +0200225 datastore.createItem("/other-module:parking-lot/example-schema:cars[id='1']");
226 datastore.createItem("/other-module:parking-lot/example-schema:cars[id='2']");
Václav Kubernátab612e92019-11-26 19:51:31 +0100227
Václav Kubernátfaacd022020-07-08 16:44:38 +0200228 listPath.m_nodes.emplace_back(module_{"other-module"}, container_{"parking-lot"});
229 listPath.m_nodes.emplace_back(module_{"example-schema"}, list_{"cars"});
Václav Kubernátab612e92019-11-26 19:51:31 +0100230 expected = {
231 {{"id", int32_t{1}}},
232 {{"id", int32_t{2}}},
233 };
234
235 }
236
237 datastore.commitChanges();
238 std::sort(expected.begin(), expected.end());
Václav Kubernát2db124c2020-05-28 21:58:36 +0200239 auto keys = dataquery.listKeys(listPath);
Václav Kubernátab612e92019-11-26 19:51:31 +0100240 std::sort(keys.begin(), keys.end());
241 REQUIRE(keys == expected);
242 }
243
Václav Kubernát0f362fb2020-06-24 15:16:56 +0200244 SECTION("empty datastore")
245 {
246 dataPath_ listPath;
247 listPath.m_scope = Scope::Absolute;
Václav Kubernátfaacd022020-07-08 16:44:38 +0200248 listPath.m_nodes.emplace_back(module_{"example-schema"}, list_{"person"});
Václav Kubernát0f362fb2020-06-24 15:16:56 +0200249 auto keys = dataquery.listKeys(listPath);
250 REQUIRE(keys == std::vector<ListInstance>{});
251 }
252
Václav Kubernátab612e92019-11-26 19:51:31 +0100253 waitForCompletionAndBitMore(seq1);
254}