merchant

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

testing_api_cmd_get_statisticsamount.c (6051B)


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