merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

testing_api_cmd_get_statisticscounter.c (6119B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 2025 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify
      6   it under the terms of the GNU General Public License as
      7   published by the Free Software Foundation; either version 3, or
      8   (at your option) any later version.
      9 
     10   TALER is distributed in the hope that it will be useful, but
     11   WITHOUT ANY WARRANTY; without even the implied warranty of
     12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13   GNU General Public License for more details.
     14 
     15   You should have received a copy of the GNU General Public
     16   License along with TALER; see the file COPYING.  If not, see
     17   <http://www.gnu.org/licenses/>
     18 */
     19 /**
     20  * @file testing_api_cmd_get_statisticscounter.c
     21  * @brief command to test GET /statistics-counter/$SLUG
     22  * @author Martin Schanzenbach
     23  */
     24 #include "taler/platform.h"
     25 #include <taler/taler_exchange_service.h>
     26 #include <taler/taler_testing_lib.h>
     27 #include "taler/taler_merchant_service.h"
     28 #include "taler/taler_merchant_testing_lib.h"
     29 #include <taler/taler-merchant/get-private-statistics-counter-SLUG.h>
     30 
     31 
     32 /**
     33  * State of a "GET statistics-counter" CMD.
     34  */
     35 struct GetStatisticsCounterState
     36 {
     37 
     38   /**
     39    * Handle for a "GET statistics-counter" request.
     40    */
     41   struct TALER_MERCHANT_GetPrivateStatisticsCounterHandle *scgh;
     42 
     43   /**
     44    * The interpreter state.
     45    */
     46   struct TALER_TESTING_Interpreter *is;
     47 
     48   /**
     49    * Base URL of the merchant serving the request.
     50    */
     51   const char *merchant_url;
     52 
     53   /**
     54    * Slug of the statistic to get.
     55    */
     56   const char *slug;
     57 
     58   /**
     59    * Expected HTTP response code.
     60    */
     61   unsigned int http_status;
     62 
     63   /**
     64    * Expected bucket size.
     65    */
     66   uint64_t buckets_length;
     67 
     68   /**
     69    * Expected intervals size.
     70    */
     71   uint64_t intervals_length;
     72 
     73 };
     74 
     75 
     76 /**
     77  * Callback for a GET /statistics-counter operation.
     78  *
     79  * @param cls closure for this function
     80  * @param scgr response details
     81  */
     82 static void
     83 get_statisticscounter_cb (
     84   void *cls,
     85   const struct TALER_MERCHANT_GetPrivateStatisticsCounterResponse *scgr)
     86 {
     87   struct GetStatisticsCounterState *scs = cls;
     88   const struct TALER_MERCHANT_HttpResponse *hr = &scgr->hr;
     89 
     90   scs->scgh = NULL;
     91   if (scs->http_status != hr->http_status)
     92   {
     93     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     94                 "Unexpected response code %u (%d) to command %s\n",
     95                 hr->http_status,
     96                 (int) hr->ec,
     97                 TALER_TESTING_interpreter_get_current_label (scs->is));
     98     TALER_TESTING_interpreter_fail (scs->is);
     99     return;
    100   }
    101   switch (hr->http_status)
    102   {
    103   case MHD_HTTP_OK:
    104     {
    105       if (scgr->details.ok.buckets_length < scs->buckets_length)
    106       {
    107         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    108                     "Length of buckets found does not match (Got %llu, expected at least %llu)\n",
    109                     (unsigned long long) scgr->details.ok.buckets_length,
    110                     (unsigned long long) scs->buckets_length);
    111         TALER_TESTING_interpreter_fail (scs->is);
    112         return;
    113       }
    114       if (scgr->details.ok.intervals_length < scs->intervals_length)
    115       {
    116         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    117                     "Length of intervals found does not match (Got %llu, expected at least %llu)\n",
    118                     (unsigned long long) scgr->details.ok.intervals_length,
    119                     (unsigned long long) scs->intervals_length);
    120         TALER_TESTING_interpreter_fail (scs->is);
    121         return;
    122       }
    123     }
    124     break;
    125   case MHD_HTTP_UNAUTHORIZED:
    126     break;
    127   case MHD_HTTP_NOT_FOUND:
    128     /* instance does not exist */
    129     break;
    130   default:
    131     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    132                 "Unhandled HTTP status %u (%d).\n",
    133                 hr->http_status,
    134                 hr->ec);
    135   }
    136   TALER_TESTING_interpreter_next (scs->is);
    137 }
    138 
    139 
    140 /**
    141  * Run the "GET /products" CMD.
    142  *
    143  *
    144  * @param cls closure.
    145  * @param cmd command being run now.
    146  * @param is interpreter state.
    147  */
    148 static void
    149 get_statisticscounter_run (void *cls,
    150                            const struct TALER_TESTING_Command *cmd,
    151                            struct TALER_TESTING_Interpreter *is)
    152 {
    153   struct GetStatisticsCounterState *scs = cls;
    154 
    155   scs->is = is;
    156   scs->scgh = TALER_MERCHANT_get_private_statistics_counter_create (
    157     TALER_TESTING_interpreter_get_context (is),
    158     scs->merchant_url,
    159     scs->slug);
    160   TALER_MERCHANT_get_private_statistics_counter_set_options (
    161     scs->scgh,
    162     TALER_MERCHANT_get_private_statistics_counter_option_type (
    163       TALER_MERCHANT_STATISTICS_ALL));
    164   {
    165     enum TALER_ErrorCode ec;
    166 
    167     ec = TALER_MERCHANT_get_private_statistics_counter_start (
    168       scs->scgh,
    169       &get_statisticscounter_cb,
    170       scs);
    171     GNUNET_assert (TALER_EC_NONE == ec);
    172   }
    173 }
    174 
    175 
    176 /**
    177  * Free the state of a "GET statistics-counter" CMD, and possibly
    178  * cancel a pending operation thereof.
    179  *
    180  * @param cls closure.
    181  * @param cmd command being run.
    182  */
    183 static void
    184 get_statisticscounter_cleanup (void *cls,
    185                                const struct TALER_TESTING_Command *cmd)
    186 {
    187   struct GetStatisticsCounterState *scs = cls;
    188 
    189   if (NULL != scs->scgh)
    190   {
    191     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    192                 "GET /statistics-counter operation did not complete\n");
    193     TALER_MERCHANT_get_private_statistics_counter_cancel (scs->scgh);
    194   }
    195   GNUNET_free (scs);
    196 }
    197 
    198 
    199 struct TALER_TESTING_Command
    200 TALER_TESTING_cmd_merchant_get_statisticscounter (
    201   const char *label,
    202   const char *merchant_url,
    203   const char *slug,
    204   uint64_t expected_buckets_length,
    205   uint64_t expected_intervals_length,
    206   unsigned int http_status)
    207 {
    208   struct GetStatisticsCounterState *scs;
    209 
    210   scs = GNUNET_new (struct GetStatisticsCounterState);
    211   scs->merchant_url = merchant_url;
    212   scs->slug = slug;
    213   scs->buckets_length = expected_buckets_length;
    214   scs->intervals_length = expected_intervals_length;
    215   scs->http_status = http_status;
    216   {
    217     struct TALER_TESTING_Command cmd = {
    218       .cls = scs,
    219       .label = label,
    220       .run = &get_statisticscounter_run,
    221       .cleanup = &get_statisticscounter_cleanup
    222     };
    223 
    224     return cmd;
    225   }
    226 }
    227 
    228 
    229 /* end of testing_api_cmd_get_statisticscounter.c */