Line data Source code
1 : /*
2 : ___________________________________________
3 : | _ ___ _ |
4 : | | | |__ \ | | |
5 : | | |__ ) |__ _ __ _ ___ _ __ | |_ |
6 : | | '_ \ / // _` |/ _` |/ _ \ '_ \| __| | HTTP/2 AGENT FOR MOCK TESTING
7 : | | | | |/ /| (_| | (_| | __/ | | | |_ | Version 0.0.z
8 : | |_| |_|____\__,_|\__, |\___|_| |_|\__| | https://github.com/testillano/h2agent
9 : | __/ | |
10 : | |___/ |
11 : |___________________________________________|
12 :
13 : Licensed under the MIT License <http://opensource.org/licenses/MIT>.
14 : SPDX-License-Identifier: MIT
15 : Copyright (c) 2021 Eduardo Ramos
16 :
17 : Permission is hereby granted, free of charge, to any person obtaining a copy
18 : of this software and associated documentation files (the "Software"), to deal
19 : in the Software without restriction, including without limitation the rights
20 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21 : copies of the Software, and to permit persons to whom the Software is
22 : furnished to do so, subject to the following conditions:
23 :
24 : The above copyright notice and this permission notice shall be included in all
25 : copies or substantial portions of the Software.
26 :
27 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 : SOFTWARE.
34 : */
35 :
36 : #include <string>
37 :
38 : #include <ert/tracing/Logger.hpp>
39 :
40 : #include <MockData.hpp>
41 :
42 :
43 : namespace h2agent
44 : {
45 : namespace model
46 : {
47 :
48 236 : std::shared_ptr<MockEventsHistory> MockData::getEvents(const DataKey &dataKey, bool &maiden) {
49 :
50 : bool exists;
51 236 : auto result = get(dataKey.getKey(), exists);
52 236 : if (exists) {
53 41 : return result;
54 : }
55 195 : maiden = true;
56 195 : return std::make_shared<MockEventsHistory>(dataKey);
57 236 : }
58 :
59 22 : bool MockData::clear(bool &somethingDeleted, const EventKey &ekey)
60 : {
61 22 : bool result = true;
62 22 : somethingDeleted = false;
63 :
64 22 : if (ekey.empty()) {
65 2 : somethingDeleted = (size() > 0);
66 2 : Map::clear();
67 2 : return result;
68 : }
69 :
70 20 : if (!ekey.checkSelection())
71 7 : return false;
72 :
73 : bool exists;
74 13 : auto key = ekey.getKey();
75 13 : auto value = get(key, exists);
76 13 : if (!exists)
77 5 : return true; // nothing found to be removed
78 :
79 : // Check event number:
80 8 : bool aux{};
81 8 : if (ekey.hasNumber()) {
82 6 : if (!ekey.validNumber())
83 3 : return false;
84 3 : somethingDeleted = value->removeEvent(ekey.getUNumber(), ekey.reverse());
85 3 : if (value->size() == 0) remove(key, aux); // remove key when history is dropped (https://github.com/testillano/h2agent/issues/53).
86 : }
87 : else {
88 2 : somethingDeleted = true;
89 2 : remove(key, aux); // remove key
90 : }
91 :
92 5 : return result;
93 13 : }
94 :
95 16 : std::string MockData::asJsonString(const EventLocationKey &elkey, bool &validQuery) const {
96 :
97 : //validQuery = false;
98 :
99 16 : if (elkey.empty()) {
100 2 : validQuery = true;
101 2 : return ((size() != 0) ? getJson().dump() : "[]"); // server data is shown as an array
102 : }
103 :
104 14 : if (!elkey.checkSelection()) {
105 2 : validQuery = false;
106 4 : return "[]";
107 : }
108 :
109 12 : validQuery = true;
110 :
111 : bool exists;
112 12 : auto result = get(elkey.getKey(), exists);
113 12 : if (!exists)
114 4 : return "[]"; // nothing found to be built
115 :
116 : // Check event number:
117 10 : if (elkey.hasNumber()) {
118 8 : if (!elkey.validNumber()) {
119 2 : validQuery = false;
120 4 : return "[]";
121 : }
122 :
123 6 : auto ptr = result->getEvent(elkey.getUNumber(), elkey.reverse());
124 6 : if (ptr) {
125 4 : return ptr->getJson(elkey.getPath()).dump();
126 : }
127 4 : else return "[]";
128 6 : }
129 : else {
130 2 : return result->getJson().dump();
131 : }
132 :
133 : return "[]";
134 12 : }
135 :
136 2 : std::string MockData::summary(const std::string &maxKeys) const {
137 2 : nlohmann::json result;
138 :
139 2 : result["totalKeys"] = (std::uint64_t)size();
140 :
141 2 : bool negative = false;
142 2 : std::uint64_t u_maxKeys = 0;
143 2 : if (!h2agent::model::string2uint64andSign(maxKeys, u_maxKeys, negative)) {
144 2 : u_maxKeys = std::numeric_limits<uint64_t>::max();
145 : }
146 :
147 2 : size_t totalEvents = 0;
148 2 : size_t historySize = 0;
149 2 : size_t displayedKeys = 0;
150 2 : nlohmann::json key;
151 :
152 2 : this->forEach([&](const KeyType& k, const ValueType& value) {
153 4 : size_t historySize = value->size();
154 4 : totalEvents += historySize;
155 4 : if (displayedKeys < u_maxKeys) {
156 4 : value->getKey().keyToJson(key);
157 4 : key["amount"] = (std::uint64_t)historySize;
158 4 : result["displayedKeys"]["list"].push_back(key);
159 4 : displayedKeys += 1;
160 : }
161 4 : });
162 :
163 2 : if (displayedKeys > 0) result["displayedKeys"]["amount"] = (std::uint64_t)displayedKeys;
164 2 : result["totalEvents"] = (std::uint64_t)totalEvents;
165 :
166 4 : return result.dump();
167 2 : }
168 :
169 9 : std::shared_ptr<MockEvent> MockData::getEvent(const EventKey &ekey) const {
170 :
171 9 : if (!ekey.complete()) {
172 2 : LOGDEBUG(ert::tracing::Logger::debug("Must provide everything to address the event (data key and event number)", ERT_FILE_LOCATION));
173 2 : return nullptr;
174 : }
175 :
176 7 : LOGDEBUG(ert::tracing::Logger::debug(ekey.asString(), ERT_FILE_LOCATION));
177 :
178 7 : bool exists{};
179 7 : auto result = get(ekey.getKey(), exists);
180 7 : if (!exists)
181 1 : return nullptr; // nothing found
182 :
183 6 : if (!ekey.validNumber())
184 2 : return nullptr;
185 :
186 4 : return (result->getEvent(ekey.getUNumber(), ekey.reverse()));
187 7 : }
188 :
189 4 : nlohmann::json MockData::getJson() const {
190 :
191 4 : nlohmann::json result;
192 :
193 4 : this->forEach([&](const KeyType& k, const ValueType& value) {
194 8 : result.push_back(value->getJson());
195 8 : });
196 :
197 4 : return result;
198 0 : }
199 :
200 19 : bool MockData::findLastRegisteredRequestState(const DataKey &key, std::string &state) const {
201 :
202 19 : bool exists{};
203 19 : auto result = get(key.getKey(), exists);
204 :
205 19 : if (exists) {
206 11 : state = result->getLastRegisteredRequestState(); // by design, a key always contains at least one history event (https://github.com/testillano/h2agent/issues/53).
207 11 : if (state.empty()) { // unprovisioned event must be understood as missing (ignore register)
208 5 : state = DEFAULT_ADMIN_PROVISION_STATE;
209 : }
210 :
211 11 : return true;
212 : }
213 :
214 8 : state = DEFAULT_ADMIN_PROVISION_STATE;
215 8 : return false;
216 19 : }
217 :
218 : }
219 : }
220 :
|