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 :
39 : #include <vector>
40 : #include <map>
41 : #include <memory>
42 :
43 : #include <MockEvent.hpp>
44 : #include <common.hpp>
45 : #include <keys.hpp>
46 :
47 :
48 : namespace h2agent
49 : {
50 : namespace model
51 : {
52 :
53 : class MockEventsHistory
54 : {
55 : std::vector<std::shared_ptr<MockEvent>> events_{};
56 : std::map<std::string, std::string> chain_variables_{}; // scoped variables propagated across outState chain
57 : mutable mutex_t rw_mutex_{}; // specific mutex to protect events_ and chain_variables_
58 :
59 : protected:
60 : DataKey data_key_;
61 :
62 : public:
63 :
64 : /**
65 : * Constructor
66 : *
67 : * @param dataKey Events key ([client enpoint id,] method & uri).
68 : */
69 319 : MockEventsHistory(const DataKey &dataKey) : data_key_(dataKey) {;}
70 :
71 : // setters:
72 :
73 : /**
74 : * Loads event into history
75 : *
76 : * @param event Event to load
77 : * @param historyEnabled Events complete history storage
78 : *
79 : * Memory must be reserved by the user
80 : */
81 : void loadEvent(std::shared_ptr<MockEvent> event, bool historyEnabled);
82 :
83 : /**
84 : * Removes vector item for a given position
85 : * We could also access from the tail (reverse chronological order)
86 :
87 : * @param eventNumber Event history number (1..N) to filter selection.
88 : * @param reverse Reverse the order to get the event from the tail instead the head.
89 : *
90 : * @return Boolean about if something was deleted
91 : */
92 : bool removeEvent(std::uint64_t eventNumber, bool reverse);
93 :
94 : // getters:
95 :
96 : /**
97 : * Gets the mock key event in specific position (last by default)
98 : *
99 : * @param eventNumber Event history number (1..N) to filter selection.
100 : * Value '0' is not accepted, and null will be returned in this case.
101 : * @param reverse Reverse the order to get the event from the tail instead the head.
102 : *
103 : * @return mock request pointer
104 : * @see size()
105 : */
106 : std::shared_ptr<MockEvent> getEvent(std::uint64_t eventNumber, bool reverse) const;
107 :
108 : /**
109 : * Builds json document for class information
110 : *
111 : * @return Json object
112 : */
113 : nlohmann::json getJson() const;
114 :
115 : /**
116 : * Gets the mock events data key
117 : *
118 : * @return Mock event data key
119 : */
120 11 : const DataKey &getKey() const {
121 11 : return data_key_;
122 : }
123 :
124 : /** Number of events
125 : *
126 : * @return Events list size
127 : */
128 16 : size_t size() const {
129 16 : return events_.size();
130 : }
131 :
132 : /** Last registered request state
133 : *
134 : * @return Last registered request state
135 : */
136 : const std::string &getLastRegisteredRequestState() const;
137 :
138 : /** Sets chain variables (scoped variables propagated across outState chain)
139 : *
140 : * @param vars Variables map to store
141 : */
142 0 : void setChainVariables(const std::map<std::string, std::string> &vars) {
143 0 : write_guard_t guard(rw_mutex_);
144 0 : chain_variables_ = vars;
145 0 : }
146 :
147 : /** Gets chain variables
148 : *
149 : * @return Chain variables map
150 : */
151 0 : std::map<std::string, std::string> getChainVariables() const {
152 0 : read_guard_t guard(rw_mutex_);
153 0 : return chain_variables_;
154 0 : }
155 : };
156 :
157 : }
158 : }
159 :
|