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 : #pragma once
37 :
38 : #include <vector>
39 : #include <cstdint>
40 :
41 : #include <nlohmann/json.hpp>
42 :
43 : #include <Map.hpp>
44 : #include <MockEventsHistory.hpp>
45 : #include <MockEvent.hpp>
46 :
47 :
48 : namespace h2agent
49 : {
50 : namespace model
51 : {
52 :
53 :
54 : /**
55 : * This class stores the mock events.
56 : *
57 : * This is useful to post-verify the internal data (content and schema validation) on testing system.
58 : * Also, the last request for an specific key, is used to know the state which is used to get the
59 : * corresponding provision information.
60 : */
61 : class MockData : public Map<mock_events_key_t, std::shared_ptr<MockEventsHistory>>
62 : {
63 : protected:
64 : // Get the events list for data key provided, and pass by reference a boolean to know if the list must be inaugurated
65 : std::shared_ptr<MockEventsHistory> getEvents(const DataKey &dataKey, bool &maiden);
66 :
67 : public:
68 245 : MockData() {};
69 245 : ~MockData() = default;
70 :
71 : using KeyType = mock_events_key_t;
72 : using ValueType = std::shared_ptr<MockEventsHistory>;
73 :
74 :
75 : /** Clears internal data
76 : *
77 : * @param somethingDeleted boolean to know if something was deleted, by reference
78 : * @param ekey Event key given by data key and event history number (1..N) to filter selection
79 : * Empty event key will remove the whole history for data key provided.
80 : * Event number '-1' will select the latest event in events history.
81 : *
82 : * @return Boolean about success of operation (not related to the number of events removed)
83 : */
84 : bool clear(bool &somethingDeleted, const EventKey &ekey);
85 :
86 : /**
87 : * Json string representation for class information filtered (json array)
88 : *
89 : * @param elkey Event location key given by data key, event history number (1..N) and event path to filter selection
90 : * Event history number (1..N) is optional, but cannot be provided alone (needs data key).
91 : * If empty, whole history is selected for data key provided.
92 : * If provided '-1' (unsigned long long max), the latest event is selected.
93 : * Event Path within the event selected is optional, but cannot be provided alone (needs data key and event number).
94 : * @param validQuery Boolean result passed by reference.
95 : *
96 : * @return Json string representation ('[]' for empty array).
97 : * @warning This method may throw exception due to dump() when unexpected data is stored on json wrap: execute under try/catch block.
98 :
99 : */
100 : std::string asJsonString(const EventLocationKey &elkey, bool &validQuery) const;
101 :
102 : /**
103 : * Json string representation for class summary (total number of events and first/last/random keys).
104 : *
105 : * @param maxKeys Maximum number of keys to be displayed in the summary (protection for huge server data size). No limit by default.
106 : *
107 : * @return Json string representation.
108 : */
109 : std::string summary(const std::string &maxKeys = "") const;
110 :
111 : /**
112 : * Gets the mock server key event in specific position
113 : *
114 : * @param ekey Event key given by data key and event history number (1..N) to filter selection
115 : * Value '-1' (unsigned long long max) selects the latest event.
116 : * Value '0' is not accepted, and null will be returned in this case.
117 : *
118 : * All the parameters are mandatory to select the single request event.
119 : * If any is missing, nullptr is returned.
120 : *
121 : * @return mock request pointer
122 : * @see size()
123 : */
124 : std::shared_ptr<MockEvent> getEvent(const EventKey &ekey) const;
125 :
126 : /**
127 : * Builds json document for class information
128 : *
129 : * @return Json object
130 : */
131 : nlohmann::json getJson() const;
132 :
133 : /**
134 : * Finds most recent mock context entry state.
135 : *
136 : * @param key Data key triggered.
137 : * @param state Request current state filled by reference.
138 : * If nothing found or no state (unprovisioned events), 'initial' will be set.
139 : *
140 : * @return Boolean about if the request is found or not
141 : */
142 : bool findLastRegisteredRequestState(const DataKey &key, std::string &state) const;
143 : };
144 :
145 : }
146 : }
147 :
|