merchant

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

testing_api_cmd_get_statisticscounter.c (6246B)


      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 src/testing/testing_api_cmd_get_statisticscounter.c
     21  * @brief command to test GET /statistics-counter/$SLUG
     22  * @author Martin Schanzenbach
     23  */
     24 #include "platform.h"
     25 struct GetStatisticsCounterState;
     26 #define TALER_MERCHANT_GET_PRIVATE_STATISTICS_COUNTER_RESULT_CLOSURE struct \
     27         GetStatisticsCounterState
     28 #include <taler/taler_exchange_service.h>
     29 #include <taler/taler_testing_lib.h>
     30 #include "taler/taler_merchant_service.h"
     31 #include "taler/taler_merchant_testing_lib.h"
     32 #include <taler/merchant/get-private-statistics-counter-SLUG.h>
     33 
     34 
     35 /**
     36  * State of a "GET statistics-counter" CMD.
     37  */
     38 struct GetStatisticsCounterState
     39 {
     40 
     41   /**
     42    * Handle for a "GET statistics-counter" request.
     43    */
     44   struct TALER_MERCHANT_GetPrivateStatisticsCounterHandle *scgh;
     45 
     46   /**
     47    * The interpreter state.
     48    */
     49   struct TALER_TESTING_Interpreter *is;
     50 
     51   /**
     52    * Base URL of the merchant serving the request.
     53    */
     54   const char *merchant_url;
     55 
     56   /**
     57    * Slug of the statistic to get.
     58    */
     59   const char *slug;
     60 
     61   /**
     62    * Expected HTTP response code.
     63    */
     64   unsigned int http_status;
     65 
     66   /**
     67    * Expected bucket size.
     68    */
     69   uint64_t buckets_length;
     70 
     71   /**
     72    * Expected intervals size.
     73    */
     74   uint64_t intervals_length;
     75 
     76 };
     77 
     78 
     79 /**
     80  * Callback for a GET /statistics-counter operation.
     81  *
     82  * @param cls closure for this function
     83  * @param scgr response details
     84  */
     85 static void
     86 get_statisticscounter_cb (
     87   struct GetStatisticsCounterState *scs,
     88   const struct TALER_MERCHANT_GetPrivateStatisticsCounterResponse *scgr)
     89 {
     90   const struct TALER_MERCHANT_HttpResponse *hr = &scgr->hr;
     91 
     92   scs->scgh = NULL;
     93   if (scs->http_status != hr->http_status)
     94   {
     95     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     96                 "Unexpected response code %u (%d) to command %s\n",
     97                 hr->http_status,
     98                 (int) hr->ec,
     99                 TALER_TESTING_interpreter_get_current_label (scs->is));
    100     TALER_TESTING_interpreter_fail (scs->is);
    101     return;
    102   }
    103   switch (hr->http_status)
    104   {
    105   case MHD_HTTP_OK:
    106     {
    107       if (scgr->details.ok.buckets_length < scs->buckets_length)
    108       {
    109         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    110                     "Length of buckets found does not match (Got %llu, expected at least %llu)\n",
    111                     (unsigned long long) scgr->details.ok.buckets_length,
    112                     (unsigned long long) scs->buckets_length);
    113         TALER_TESTING_interpreter_fail (scs->is);
    114         return;
    115       }
    116       if (scgr->details.ok.intervals_length < scs->intervals_length)
    117       {
    118         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    119                     "Length of intervals found does not match (Got %llu, expected at least %llu)\n",
    120                     (unsigned long long) scgr->details.ok.intervals_length,
    121                     (unsigned long long) scs->intervals_length);
    122         TALER_TESTING_interpreter_fail (scs->is);
    123         return;
    124       }
    125     }
    126     break;
    127   case MHD_HTTP_UNAUTHORIZED:
    128     break;
    129   case MHD_HTTP_NOT_FOUND:
    130     /* instance does not exist */
    131     break;
    132   default:
    133     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    134                 "Unhandled HTTP status %u (%d).\n",
    135                 hr->http_status,
    136                 hr->ec);
    137   }
    138   TALER_TESTING_interpreter_next (scs->is);
    139 }
    140 
    141 
    142 /**
    143  * Run the "GET /products" CMD.
    144  *
    145  *
    146  * @param cls closure.
    147  * @param cmd command being run now.
    148  * @param is interpreter state.
    149  */
    150 static void
    151 get_statisticscounter_run (void *cls,
    152                            const struct TALER_TESTING_Command *cmd,
    153                            struct TALER_TESTING_Interpreter *is)
    154 {
    155   struct GetStatisticsCounterState *scs = cls;
    156 
    157   scs->is = is;
    158   scs->scgh = TALER_MERCHANT_get_private_statistics_counter_create (
    159     TALER_TESTING_interpreter_get_context (is),
    160     scs->merchant_url,
    161     scs->slug);
    162   TALER_MERCHANT_get_private_statistics_counter_set_options (
    163     scs->scgh,
    164     TALER_MERCHANT_get_private_statistics_counter_option_type (
    165       TALER_MERCHANT_STATISTICS_ALL));
    166   {
    167     enum TALER_ErrorCode ec;
    168 
    169     ec = TALER_MERCHANT_get_private_statistics_counter_start (
    170       scs->scgh,
    171       &get_statisticscounter_cb,
    172       scs);
    173     GNUNET_assert (TALER_EC_NONE == ec);
    174   }
    175 }
    176 
    177 
    178 /**
    179  * Free the state of a "GET statistics-counter" CMD, and possibly
    180  * cancel a pending operation thereof.
    181  *
    182  * @param cls closure.
    183  * @param cmd command being run.
    184  */
    185 static void
    186 get_statisticscounter_cleanup (void *cls,
    187                                const struct TALER_TESTING_Command *cmd)
    188 {
    189   struct GetStatisticsCounterState *scs = cls;
    190 
    191   if (NULL != scs->scgh)
    192   {
    193     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    194                 "GET /statistics-counter operation did not complete\n");
    195     TALER_MERCHANT_get_private_statistics_counter_cancel (scs->scgh);
    196   }
    197   GNUNET_free (scs);
    198 }
    199 
    200 
    201 struct TALER_TESTING_Command
    202 TALER_TESTING_cmd_merchant_get_statisticscounter (
    203   const char *label,
    204   const char *merchant_url,
    205   const char *slug,
    206   uint64_t expected_buckets_length,
    207   uint64_t expected_intervals_length,
    208   unsigned int http_status)
    209 {
    210   struct GetStatisticsCounterState *scs;
    211 
    212   scs = GNUNET_new (struct GetStatisticsCounterState);
    213   scs->merchant_url = merchant_url;
    214   scs->slug = slug;
    215   scs->buckets_length = expected_buckets_length;
    216   scs->intervals_length = expected_intervals_length;
    217   scs->http_status = http_status;
    218   {
    219     struct TALER_TESTING_Command cmd = {
    220       .cls = scs,
    221       .label = label,
    222       .run = &get_statisticscounter_run,
    223       .cleanup = &get_statisticscounter_cleanup
    224     };
    225 
    226     return cmd;
    227   }
    228 }
    229 
    230 
    231 /* end of testing_api_cmd_get_statisticscounter.c */