LCOV - code coverage report
Current view: top level - model - keys.hpp (source / functions) Coverage Total Hit
Test: final-coverage.info Lines: 100.0 % 92 92
Test Date: 2025-02-14 17:40:40 Functions: 100.0 % 28 28

            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 <ert/tracing/Logger.hpp>
      40              : 
      41              : #include <functions.hpp>
      42              : 
      43              : #include <nlohmann/json.hpp>
      44              : 
      45              : 
      46              : namespace h2agent
      47              : {
      48              : namespace model
      49              : {
      50              : // Mock events key:
      51              : typedef std::string mock_events_key_t;
      52              : 
      53              : /**
      54              :  * Events history key.
      55              :  */
      56              : class DataKey {
      57              : 
      58              :     std::string client_endpoint_id_{};
      59              :     std::string method_{};
      60              :     std::string uri_{};
      61              :     std::string provision_uri_{}; // for requests
      62              : 
      63              :     mock_events_key_t key_{};
      64              :     int nkeys_;
      65              : 
      66              : public:
      67              : 
      68              :     /**
      69              :      * Constructor for method & uri
      70              :      *
      71              :      * @param method Http method
      72              :      * @param uri Http Uri
      73              :      */
      74          206 :     DataKey(const std::string &method, const std::string &uri): method_(method), uri_(uri), nkeys_(2) {
      75          206 :         h2agent::model::calculateStringKey(key_, method_, uri_);
      76          206 :     }
      77              : 
      78              :     /**
      79              :      * Constructor for client endpoint id & method & uri
      80              :      *
      81              :      * @param clientEndpointId Client endpoint identifier
      82              :      * @param method Http method
      83              :      * @param uri Http Uri
      84              :      */
      85           90 :     DataKey(const std::string &clientEndpointId, const std::string &method, const std::string &uri): client_endpoint_id_(clientEndpointId), method_(method), uri_(uri), nkeys_(3) {
      86           90 :         h2agent::model::calculateStringKey(key_, client_endpoint_id_, method_, uri_);
      87           90 :     }
      88              : 
      89              :     /**
      90              :      * Gets number of used keys
      91              :      */
      92          198 :     int getNKeys() const {
      93          198 :         return nkeys_;
      94              :     }
      95              : 
      96              :     /**
      97              :      * Gets client endpoint id
      98              :      */
      99            9 :     const std::string &getClientEndpointId() const {
     100            9 :         return client_endpoint_id_;
     101              :     }
     102              : 
     103              :     /**
     104              :      * Gets method
     105              :      */
     106           37 :     const std::string &getMethod() const {
     107           37 :         return method_;
     108              :     }
     109              : 
     110              :     /**
     111              :      * Gets uri
     112              :      */
     113           11 :     const std::string &getUri() const {
     114           11 :         return uri_;
     115              :     }
     116              : 
     117              :     /**
     118              :      * Gets aggregated key
     119              :      */
     120          491 :     const mock_events_key_t &getKey() const {
     121          491 :         return key_;
     122              :     }
     123              : 
     124              :     /**
     125              :     * Boolean about if event key is empty (all components are empty)
     126              :     */
     127           82 :     bool empty() const {
     128           82 :         bool none = (method_.empty() && uri_.empty());
     129           82 :         if (getNKeys() == 3) {
     130           48 :             none &= (client_endpoint_id_.empty());
     131              :         }
     132              : 
     133           82 :         return none;
     134              :     }
     135              : 
     136              :     /**
     137              :     * Boolean about if event key is complete (all components are provided)
     138              :     */
     139           87 :     bool complete() const {
     140           87 :         bool all = (!method_.empty() && !uri_.empty());
     141           87 :         if (getNKeys() == 3) {
     142           52 :             all &= (!client_endpoint_id_.empty());
     143              :         }
     144              : 
     145           87 :         return all;
     146              :     }
     147              : 
     148              :     /**
     149              :     * Build data key into json document
     150              :     *
     151              :     * param doc Json document to update
     152              :     */
     153           20 :     void keyToJson(nlohmann::json &doc) const {
     154           20 :         doc["method"] =  method_;
     155           20 :         doc["uri"] = uri_;
     156           20 :         if (!provision_uri_.empty()) doc["provisionUri"] = provision_uri_;
     157           20 :         if (getNKeys() == 3) {
     158            9 :             doc["clientEndpointId"] = client_endpoint_id_;
     159              :         }
     160           20 :     }
     161              : 
     162              :     /**
     163              :      * Check key selection: all or nothing must be provided
     164              :      */
     165           58 :     bool checkSelection() const {
     166           58 :         LOGDEBUG(
     167              :         if(!complete() && !empty()) {
     168              :         if (getNKeys() == 2) {
     169              :                 ert::tracing::Logger::debug("Key parts 'requestMethod' and 'requestUri' must be all provided or all missing", ERT_FILE_LOCATION);
     170              :             }
     171              :             else {
     172              :                 ert::tracing::Logger::debug("Key parts 'clientEndpointId', 'requestMethod' and 'requestUri' must be all provided or all missing", ERT_FILE_LOCATION);
     173              :             }
     174              :         }
     175              :         );
     176              : 
     177           58 :         return (complete() || empty());
     178              :     }
     179              : 
     180              :     /**
     181              :      * Set provision URI (additional context data for requests in mock server mode)
     182              :      */
     183            7 :     void setProvisionUri(const std::string &provisionUri) {
     184            7 :         provision_uri_ = provisionUri;
     185            7 :     }
     186              : };
     187              : 
     188              : /**
     189              :  * Event key
     190              :  *
     191              :  * Extends events history key with the event location within the history (1..N).
     192              :  */
     193              : class EventKey : public DataKey {
     194              : 
     195              :     std::string number_;
     196              :     bool valid_;
     197              :     std::uint64_t u_number_;
     198              :     bool reverse_;
     199              : 
     200              : public:
     201              : 
     202              :     /**
     203              :      * Constructor
     204              :      *
     205              :      * @param key Data key
     206              :      * @param number Position in events history (1..N)
     207              :      */
     208            2 :     EventKey(const DataKey &key, const std::string &number): DataKey(key), number_(number), valid_(false), u_number_(0), reverse_(false) {
     209            2 :         if (hasNumber()) valid_ = h2agent::model::string2uint64andSign(number_, u_number_, reverse_);
     210            2 :     }
     211              : 
     212              :     /**
     213              :      * Constructor
     214              :      * Used for method & uri
     215              :      *
     216              :      * @param method Http method
     217              :      * @param uri Http Uri
     218              :      * @param number Position in events history (1..N)
     219              :      */
     220           33 :     EventKey(const std::string &method, const std::string &uri, const std::string &number):
     221           33 :         DataKey(method, uri), number_(number), valid_(false), u_number_(0), reverse_(false) {
     222           33 :         if (hasNumber()) valid_ = h2agent::model::string2uint64andSign(number_, u_number_, reverse_);
     223           33 :     }
     224              : 
     225              :     /**
     226              :      * Constructor
     227              :      * Used for client endpoint & method & uri
     228              :      *
     229              :      * @param clientEndpointId Client endpoint identifier
     230              :      * @param method Http method
     231              :      * @param uri Http Uri
     232              :      * @param number Position in events history (1..N)
     233              :      */
     234           34 :     EventKey(const std::string &clientEndpointId, const std::string &method, const std::string &uri, const std::string &number):
     235           34 :         DataKey(clientEndpointId, method, uri), number_(number), valid_(false), u_number_(0), reverse_(false) {
     236           34 :         if (hasNumber()) valid_ = h2agent::model::string2uint64andSign(number_, u_number_, reverse_);
     237           34 :     }
     238              : 
     239              :     /**
     240              :      * Gets position in events history (1..N)
     241              :      */
     242           10 :     const std::string &getNumber() const {
     243           10 :         return number_;
     244              :     }
     245              : 
     246              :     /**
     247              :      * Returns boolean about if number is set or not
     248              :      */
     249          191 :     bool hasNumber() const {
     250          191 :         return !number_.empty();
     251              :     }
     252              : 
     253              :     /**
     254              :      * Returns boolean about if number is validated
     255              :      */
     256           26 :     bool validNumber() const {
     257           26 :         return valid_;
     258              :     }
     259              : 
     260              :     /**
     261              :      * Gets absolute part of the number
     262              :      */
     263           19 :     const std::uint64_t &getUNumber() const {
     264           19 :         return u_number_;
     265              :     }
     266              : 
     267              :     /**
     268              :      * Gets sign of the number which indicated reserve order of access
     269              :      */
     270           19 :     bool reverse() const {
     271           19 :         return reverse_;
     272              :     }
     273              : 
     274              :     /**
     275              :     * Boolean about if event key is empty (all components are empty) and also number is missing
     276              :     */
     277           43 :     bool empty() const {
     278           43 :         bool none = DataKey::empty();
     279           43 :         none &= (!hasNumber());
     280              : 
     281           43 :         return none;
     282              :     }
     283              : 
     284              :     /**
     285              :     * Boolean about if event key is complete (all components are provided) and also number exists
     286              :     */
     287           14 :     bool complete() const {
     288           14 :         bool all = DataKey::complete();
     289           14 :         all &= (hasNumber());
     290              : 
     291           14 :         return all;
     292              :     }
     293              : 
     294              :     /**
     295              :      * Check key selection: all or nothing must be provided.
     296              :      * Also, event number needs for key.
     297              :      */
     298           43 :     bool checkSelection() const {
     299              : 
     300           43 :         if (!DataKey::checkSelection()) return false;
     301              : 
     302           37 :         if (hasNumber() && getMethod().empty()) {
     303            4 :             LOGDEBUG(
     304              :             if (getNKeys() == 2) {
     305              :             ert::tracing::Logger::debug("Query parameter 'eventNumber' cannot be provided alone: requestMethod and requestUri are also needed", ERT_FILE_LOCATION);
     306              :             }
     307              :             else {
     308              :                 ert::tracing::Logger::debug("Query parameter 'eventNumber' cannot be provided alone: clientEndpointId, requestMethod and requestUri are also needed", ERT_FILE_LOCATION);
     309              :             }
     310              :             );
     311            4 :             return false;
     312              :         }
     313              : 
     314           33 :         return true;
     315              :     }
     316              : 
     317              :     /**
     318              :      * Class string representation
     319              :      */
     320            2 :     std::string asString() const {
     321            2 :         std::string result{};
     322            2 :         if (getNKeys() == 3) {
     323            1 :             result = ert::tracing::Logger::asString("clientEndpointId: %s | ", getClientEndpointId().c_str());
     324              :         }
     325            2 :         result += ert::tracing::Logger::asString("requestMethod: %s | requestUri: %s | eventNumber: %s", getMethod().c_str(), getUri().c_str(), getNumber().c_str());
     326            2 :         return result;
     327              :     } // LCOV_EXCL_LINE
     328              : };
     329              : 
     330              : /**
     331              :  * Event location key
     332              :  *
     333              :  * Extends events key with the event path to access event subset.
     334              :  */
     335              : class EventLocationKey : public EventKey {
     336              : 
     337              :     std::string path_;
     338              : 
     339              : public:
     340              : 
     341              :     /**
     342              :      * Constructor
     343              :      * Used for method & uri
     344              :      *
     345              :      * @param method Http method
     346              :      * @param uri Http Uri
     347              :      * @param number Position in events history (1..N)
     348              :      * @param path Json path within event
     349              :      */
     350            9 :     EventLocationKey(const std::string &method, const std::string &uri, const std::string &number, const std::string &path):
     351            9 :         EventKey(method, uri, number), path_(path) {;}
     352              : 
     353              :     /**
     354              :      * Constructor
     355              :      * Used for client endpoint & method & uri
     356              :      *
     357              :      * @param clientEndpointId Client endpoint identifier
     358              :      * @param method Http method
     359              :      * @param uri Http Uri
     360              :      * @param number Position in events history (1..N)
     361              :      * @param path Json path within event
     362              :      */
     363           13 :     EventLocationKey(const std::string &clientEndpointId, const std::string &method, const std::string &uri, const std::string &number, const std::string &path):
     364           13 :         EventKey(clientEndpointId, method, uri, number), path_(path) {;}
     365              : 
     366              :     /**
     367              :      * Gets the path within the event
     368              :      */
     369           22 :     const std::string &getPath() const {
     370           22 :         return path_;
     371              :     }
     372              : 
     373              : //    /**
     374              : //     * Gets the content of the event path
     375              : //     */
     376              : //    const nlohmann::json &getLocation(const nlohmann::json &document) const {
     377              : //        nlohmann::json::json_pointer jptr(path_);
     378              : //        return document[jptr];
     379              : //    }
     380              : //
     381              : //    /**
     382              : //     * Boolean about if event key is empty (all components are empty) and also event number and path are missing
     383              : //     */
     384              : //    bool empty() const {
     385              : //       bool none = EventKey::empty();
     386              : //       none &= (path_.empty());
     387              : //
     388              : //       return none;
     389              : //    }
     390              : //
     391              : //    /**
     392              : //    * Boolean about if event key is complete (all components are provided) and also event number and path exist
     393              : //    */
     394              : //    bool complete() const {
     395              : //        bool all = EventKey::complete();
     396              : //        all &= (!path_.empty());
     397              : //
     398              : //        return all;
     399              : //    }
     400              : 
     401              :     /**
     402              :      * Check key selection: all or nothing must be provided.
     403              :      * Also, event number needs for key, and path needs for event number.
     404              :      */
     405           18 :     bool checkSelection() const {
     406              : 
     407           18 :         if (!EventKey::checkSelection()) return false;
     408              : 
     409           16 :         if (!getPath().empty() && !hasNumber()) {
     410            1 :             LOGDEBUG(
     411              :             if (getNKeys() == 2) {
     412              :             ert::tracing::Logger::error("Query parameter 'eventPath' cannot be provided alone: (requestMethod, requestUri and) eventNumber are also needed", ERT_FILE_LOCATION);
     413              :             }
     414              :             else {
     415              :                 ert::tracing::Logger::error("Query parameter 'eventPath' cannot be provided alone: (clientEndpointId, requestMethod, requestUri and) eventNumber are also needed", ERT_FILE_LOCATION);
     416              :             }
     417              :             );
     418            1 :             return false;
     419              :         }
     420              : 
     421           15 :         return true;
     422              :     }
     423              : };
     424              : 
     425              : }
     426              : }
     427              : 
        

Generated by: LCOV version 2.0-1