testing_api_cmd_get_statisticsamount.c (6586B)
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 get_statisticsamount_cb (struct GetStatisticsAmountState *scs, 86 const struct 87 TALER_MERCHANT_GetPrivateStatisticsAmountResponse *scgr 88 ) 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 %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 %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_statisticsamount_run (void *cls, 152 const struct TALER_TESTING_Command *cmd, 153 struct TALER_TESTING_Interpreter *is) 154 { 155 struct GetStatisticsAmountState *scs = cls; 156 157 scs->is = is; 158 scs->scgh = TALER_MERCHANT_get_private_statistics_amount_create ( 159 TALER_TESTING_interpreter_get_context (is), 160 scs->merchant_url, 161 scs->slug); 162 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 ec = TALER_MERCHANT_get_private_statistics_amount_start ( 170 scs->scgh, 171 &get_statisticsamount_cb, 172 scs); 173 GNUNET_assert (TALER_EC_NONE == ec); 174 } 175 } 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 get_statisticsamount_cleanup (void *cls, 187 const struct TALER_TESTING_Command *cmd) 188 { 189 struct GetStatisticsAmountState *scs = cls; 190 191 if (NULL != scs->scgh) 192 { 193 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 194 "GET /statistics-amount operation did not complete\n"); 195 TALER_MERCHANT_get_private_statistics_amount_cancel (scs->scgh); 196 } 197 GNUNET_free (scs); 198 } 199 200 201 struct TALER_TESTING_Command 202 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 scs = GNUNET_new (struct GetStatisticsAmountState); 214 scs->merchant_url = merchant_url; 215 scs->slug = slug; 216 scs->buckets_length = expected_buckets_length; 217 scs->intervals_length = expected_intervals_length; 218 scs->http_status = http_status; 219 { 220 struct TALER_TESTING_Command cmd = { 221 .cls = scs, 222 .label = label, 223 .run = &get_statisticsamount_run, 224 .cleanup = &get_statisticsamount_cleanup 225 }; 226 227 return cmd; 228 } 229 } 230 231 232 /* end of testing_api_cmd_get_statisticsamount.c */