LCOV - code coverage report
Current view: top level - model - MockData.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 81.9 % 127 104
Test Date: 2026-03-07 03:59:50 Functions: 81.8 % 11 9

            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          350 : std::shared_ptr<MockEventsHistory> MockData::getEvents(const DataKey &dataKey, bool &maiden) {
      49              : 
      50              :     bool exists;
      51          350 :     auto result = get(dataKey.getKey(), exists);
      52          350 :     if (exists) {
      53           38 :         return result;
      54              :     }
      55          312 :     maiden = true;
      56          312 :     return std::make_shared<MockEventsHistory>(dataKey);
      57          350 : }
      58              : 
      59           21 : bool MockData::clear(bool &somethingDeleted, const EventKey &ekey)
      60              : {
      61           21 :     bool result = true;
      62           21 :     somethingDeleted = false;
      63              : 
      64           21 :     if (ekey.empty()) {
      65            2 :         somethingDeleted = (size() > 0);
      66            2 :         Map::clear();
      67            2 :         return result;
      68              :     }
      69              : 
      70           19 :     if (!ekey.checkSelection())
      71            7 :         return false;
      72              : 
      73              :     bool exists;
      74           12 :     auto key = ekey.getKey();
      75           12 :     auto value = get(key, exists);
      76           12 :     if (!exists)
      77            4 :         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           12 : }
      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           12 : std::shared_ptr<MockEvent> MockData::getEvent(const EventKey &ekey) const {
     170              : 
     171           12 :     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           10 :     LOGDEBUG(ert::tracing::Logger::debug(ekey.asString(), ERT_FILE_LOCATION));
     177              : 
     178           10 :     bool exists{};
     179           10 :     auto result = get(ekey.getKey(), exists);
     180           10 :     if (!exists)
     181            2 :         return nullptr; // nothing found
     182              : 
     183            8 :     if (!ekey.validNumber())
     184            2 :         return nullptr;
     185              : 
     186            6 :     return (result->getEvent(ekey.getUNumber(), ekey.reverse()));
     187           10 : }
     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            8 : bool MockData::findLastRegisteredRequestState(const DataKey &key, std::string &state) const {
     201              : 
     202            8 :     bool exists{};
     203            8 :     auto result = get(key.getKey(), exists);
     204              : 
     205            8 :     if (exists) {
     206            8 :         state = result->getLastRegisteredRequestState(); // by design, a key always contains at least one history event (https://github.com/testillano/h2agent/issues/53).
     207            8 :         if (state.empty()) { // unprovisioned event must be understood as missing (ignore register)
     208            2 :             state = DEFAULT_ADMIN_PROVISION_STATE;
     209              :         }
     210              : 
     211            8 :         return true;
     212              :     }
     213              : 
     214            0 :     state = DEFAULT_ADMIN_PROVISION_STATE;
     215            0 :     return false;
     216            8 : }
     217              : 
     218            0 : bool MockData::findLastRegisteredRequestState(const DataKey &key, std::string &state, std::map<std::string, std::string> &chainVariables) const {
     219              : 
     220            0 :     bool exists{};
     221            0 :     auto result = get(key.getKey(), exists);
     222              : 
     223            0 :     if (exists) {
     224            0 :         state = result->getLastRegisteredRequestState();
     225            0 :         chainVariables = result->getChainVariables();
     226            0 :         if (state.empty()) {
     227            0 :             state = DEFAULT_ADMIN_PROVISION_STATE;
     228            0 :             chainVariables.clear();
     229              :         }
     230              : 
     231            0 :         return true;
     232              :     }
     233              : 
     234            0 :     state = DEFAULT_ADMIN_PROVISION_STATE;
     235            0 :     chainVariables.clear();
     236            0 :     return false;
     237            0 : }
     238              : 
     239            0 : void MockData::storeChainVariables(const DataKey &key, const std::map<std::string, std::string> &chainVariables) {
     240              : 
     241            0 :     bool exists{};
     242            0 :     auto result = get(key.getKey(), exists);
     243              : 
     244            0 :     if (exists) {
     245            0 :         result->setChainVariables(chainVariables);
     246              :     }
     247            0 : }
     248              : 
     249              : }
     250              : }
     251              : 
        

Generated by: LCOV version 2.0-1