merchant

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

testing_api_cmd_get_statisticsamount.c (6472B)


      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_statisticsamount.c
     21  * @brief command to test GET /statistics-amount/$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-amount-SLUG.h>
     30 
     31 
     32 /**
     33  * State of a "GET statistics-amount" CMD.
     34  */
     35 struct GetStatisticsAmountState
     36 {
     37 
     38   /**
     39    * Handle for a "GET statistics-amount" request.
     40    */
     41   struct TALER_MERCHANT_GetPrivateStatisticsAmountHandle *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-amount operation.
     78  *
     79  * @param cls closure for this function
     80  * @param scgr response details
     81  */
     82 static void
     83 get_statisticsamount_cb (void *cls,
     84                          const struct
     85                          TALER_MERCHANT_GetPrivateStatisticsAmountResponse *scgr
     86                          )
     87 {
     88   struct GetStatisticsAmountState *scs = cls;
     89   const struct TALER_MERCHANT_HttpResponse *hr = &scgr->hr;
     90 
     91   scs->scgh = NULL;
     92   if (scs->http_status != hr->http_status)
     93   {
     94     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     95                 "Unexpected response code %u (%d) to command %s\n",
     96                 hr->http_status,
     97                 (int) hr->ec,
     98                 TALER_TESTING_interpreter_get_current_label (scs->is));
     99     TALER_TESTING_interpreter_fail (scs->is);
    100     return;
    101   }
    102   switch (hr->http_status)
    103   {
    104   case MHD_HTTP_OK:
    105     {
    106       if (scgr->details.ok.buckets_length != scs->buckets_length)
    107       {
    108         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    109                     "Length of buckets found does not match (Got %llu, expected %llu)\n",
    110                     (unsigned long long) scgr->details.ok.buckets_length,
    111                     (unsigned long long) scs->buckets_length);
    112         TALER_TESTING_interpreter_fail (scs->is);
    113         return;
    114       }
    115       if (scgr->details.ok.intervals_length != scs->intervals_length)
    116       {
    117         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    118                     "Length of intervals found does not match (Got %llu, expected %llu)\n",
    119                     (unsigned long long) scgr->details.ok.intervals_length,
    120                     (unsigned long long) scs->intervals_length);
    121         TALER_TESTING_interpreter_fail (scs->is);
    122         return;
    123       }
    124     }
    125     break;
    126   case MHD_HTTP_UNAUTHORIZED:
    127     break;
    128   case MHD_HTTP_NOT_FOUND:
    129     /* instance does not exist */
    130     break;
    131   default:
    132     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    133                 "Unhandled HTTP status %u (%d).\n",
    134                 hr->http_status,
    135                 hr->ec);
    136   }
    137   TALER_TESTING_interpreter_next (scs->is);
    138 }
    139 
    140 
    141 /**
    142  * Run the "GET /products" CMD.
    143  *
    144  *
    145  * @param cls closure.
    146  * @param cmd command being run now.
    147  * @param is interpreter state.
    148  */
    149 static void
    150 get_statisticsamount_run (void *cls,
    151                           const struct TALER_TESTING_Command *cmd,
    152                           struct TALER_TESTING_Interpreter *is)
    153 {
    154   struct GetStatisticsAmountState *scs = cls;
    155 
    156   scs->is = is;
    157   scs->scgh = TALER_MERCHANT_get_private_statistics_amount_create (
    158     TALER_TESTING_interpreter_get_context (is),
    159     scs->merchant_url,
    160     scs->slug);
    161   TALER_MERCHANT_get_private_statistics_amount_set_options (
    162     scs->scgh,
    163     TALER_MERCHANT_get_private_statistics_amount_option_type (
    164       TALER_MERCHANT_STATISTICS_ALL));
    165   {
    166     enum TALER_ErrorCode ec;
    167 
    168     ec = TALER_MERCHANT_get_private_statistics_amount_start (
    169       scs->scgh,
    170       &get_statisticsamount_cb,
    171       scs);
    172     GNUNET_assert (TALER_EC_NONE == ec);
    173   }
    174 }
    175 
    176 
    177 /**
    178  * Free the state of a "GET statistics-amount" CMD, and possibly
    179  * cancel a pending operation thereof.
    180  *
    181  * @param cls closure.
    182  * @param cmd command being run.
    183  */
    184 static void
    185 get_statisticsamount_cleanup (void *cls,
    186                               const struct TALER_TESTING_Command *cmd)
    187 {
    188   struct GetStatisticsAmountState *scs = cls;
    189 
    190   if (NULL != scs->scgh)
    191   {
    192     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
    193                 "GET /statistics-amount operation did not complete\n");
    194     TALER_MERCHANT_get_private_statistics_amount_cancel (scs->scgh);
    195   }
    196   GNUNET_free (scs);
    197 }
    198 
    199 
    200 struct TALER_TESTING_Command
    201 TALER_TESTING_cmd_merchant_get_statisticsamount (const char *label,
    202                                                  const char *merchant_url,
    203                                                  const char *slug,
    204                                                  uint64_t
    205                                                  expected_buckets_length,
    206                                                  uint64_t
    207                                                  expected_intervals_length,
    208                                                  unsigned int http_status)
    209 {
    210   struct GetStatisticsAmountState *scs;
    211 
    212   scs = GNUNET_new (struct GetStatisticsAmountState);
    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_statisticsamount_run,
    223       .cleanup = &get_statisticsamount_cleanup
    224     };
    225 
    226     return cmd;
    227   }
    228 }
    229 
    230 
    231 /* end of testing_api_cmd_get_statisticsamount.c */