LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_get_statisticsamount.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 65.3 % 49 32
Test Date: 2026-09-11 17:45:24 Functions: 100.0 % 4 4

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

Generated by: LCOV version 2.0-1