LCOV - code coverage report
Current view: top level - testing - testing_api_cmd_get_statisticscounter.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 31 48 64.6 %
Date: 2025-06-23 16:22:09 Functions: 4 4 100.0 %

          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 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             : #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-counter" CMD.
      33             :  */
      34             : struct GetStatisticsCounterState
      35             : {
      36             : 
      37             :   /**
      38             :    * Handle for a "GET statistics-counter" request.
      39             :    */
      40             :   struct TALER_MERCHANT_StatisticsCounterGetHandle *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-counter operation.
      77             :  *
      78             :  * @param cls closure for this function
      79             :  * @param gpr response details
      80             :  */
      81             : static void
      82           4 : get_statisticscounter_cb (void *cls,
      83             :                           const struct
      84             :                           TALER_MERCHANT_StatisticsCounterGetResponse *scgr)
      85             : {
      86           4 :   struct GetStatisticsCounterState *scs = cls;
      87           4 :   const struct TALER_MERCHANT_HttpResponse *hr = &scgr->hr;
      88             : 
      89           4 :   scs->scgh = NULL;
      90           4 :   if (scs->http_status != hr->http_status)
      91             :   {
      92           0 :     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           0 :     TALER_TESTING_interpreter_fail (scs->is);
      98           0 :     return;
      99             :   }
     100           4 :   switch (hr->http_status)
     101             :   {
     102           4 :   case MHD_HTTP_OK:
     103             :     {
     104           4 :       if (scgr->details.ok.buckets_length != scs->buckets_length)
     105             :       {
     106           0 :         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           0 :         TALER_TESTING_interpreter_fail (scs->is);
     111           0 :         return;
     112             :       }
     113           4 :       if (scgr->details.ok.intervals_length != scs->intervals_length)
     114             :       {
     115           0 :         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           0 :         TALER_TESTING_interpreter_fail (scs->is);
     120           0 :         return;
     121             :       }
     122             :     }
     123           4 :     break;
     124           0 :   case MHD_HTTP_UNAUTHORIZED:
     125           0 :     break;
     126           0 :   case MHD_HTTP_NOT_FOUND:
     127             :     /* instance does not exist */
     128           0 :     break;
     129           0 :   default:
     130           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     131             :                 "Unhandled HTTP status %u (%d).\n",
     132             :                 hr->http_status,
     133             :                 hr->ec);
     134             :   }
     135           4 :   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           4 : get_statisticscounter_run (void *cls,
     149             :                            const struct TALER_TESTING_Command *cmd,
     150             :                            struct TALER_TESTING_Interpreter *is)
     151             : {
     152           4 :   struct GetStatisticsCounterState *scs = cls;
     153             : 
     154           4 :   scs->is = is;
     155           4 :   scs->scgh = TALER_MERCHANT_statistic_counter_get (
     156             :     TALER_TESTING_interpreter_get_context (is),
     157             :     scs->merchant_url,
     158             :     scs->slug,
     159             :     TALER_MERCHANT_STATISTICS_ALL,
     160             :     &get_statisticscounter_cb,
     161             :     scs);
     162           4 :   GNUNET_assert (NULL != scs->scgh);
     163           4 : }
     164             : 
     165             : 
     166             : /**
     167             :  * Free the state of a "GET statistics-counter" 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           4 : get_statisticscounter_cleanup (void *cls,
     175             :                                const struct TALER_TESTING_Command *cmd)
     176             : {
     177           4 :   struct GetStatisticsCounterState *scs = cls;
     178             : 
     179           4 :   if (NULL != scs->scgh)
     180             :   {
     181           0 :     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
     182             :                 "GET /statistics-counter operation did not complete\n");
     183           0 :     TALER_MERCHANT_statistic_counter_get_cancel (scs->scgh);
     184             :   }
     185           4 :   GNUNET_free (scs);
     186           4 : }
     187             : 
     188             : 
     189             : struct TALER_TESTING_Command
     190           4 : TALER_TESTING_cmd_merchant_get_statisticscounter (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 GetStatisticsCounterState *scs;
     200             : 
     201           4 :   scs = GNUNET_new (struct GetStatisticsCounterState);
     202           4 :   scs->merchant_url = merchant_url;
     203           4 :   scs->slug = slug;
     204           4 :   scs->buckets_length = expected_buckets_length;
     205           4 :   scs->intervals_length = expected_intervals_length;
     206           4 :   scs->http_status = http_status;
     207             :   {
     208           4 :     struct TALER_TESTING_Command cmd = {
     209             :       .cls = scs,
     210             :       .label = label,
     211             :       .run = &get_statisticscounter_run,
     212             :       .cleanup = &get_statisticscounter_cleanup
     213             :     };
     214             : 
     215           4 :     return cmd;
     216             :   }
     217             : }
     218             : 
     219             : 
     220             : /* end of testing_api_cmd_get_statisticscounter.c */

Generated by: LCOV version 1.16