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_statisticscounter.c
21 : * @brief command to test GET /statistics-counter/$SLUG
22 : * @author Martin Schanzenbach
23 : */
24 : #include "platform.h"
25 : struct GetStatisticsCounterState;
26 : #define TALER_MERCHANT_GET_PRIVATE_STATISTICS_COUNTER_RESULT_CLOSURE struct \
27 : GetStatisticsCounterState
28 : #include <taler/taler_exchange_service.h>
29 : #include <taler/taler_testing_lib.h>
30 : #include "taler/taler_merchant_service.h"
31 : #include "taler/taler_merchant_testing_lib.h"
32 : #include <taler/merchant/get-private-statistics-counter-SLUG.h>
33 :
34 :
35 : /**
36 : * State of a "GET statistics-counter" CMD.
37 : */
38 : struct GetStatisticsCounterState
39 : {
40 :
41 : /**
42 : * Handle for a "GET statistics-counter" request.
43 : */
44 : struct TALER_MERCHANT_GetPrivateStatisticsCounterHandle *scgh;
45 :
46 : /**
47 : * The interpreter state.
48 : */
49 : struct TALER_TESTING_Interpreter *is;
50 :
51 : /**
52 : * Base URL of the merchant serving the request.
53 : */
54 : const char *merchant_url;
55 :
56 : /**
57 : * Slug of the statistic to get.
58 : */
59 : const char *slug;
60 :
61 : /**
62 : * Expected HTTP response code.
63 : */
64 : unsigned int http_status;
65 :
66 : /**
67 : * Expected bucket size.
68 : */
69 : uint64_t buckets_length;
70 :
71 : /**
72 : * Expected intervals size.
73 : */
74 : uint64_t intervals_length;
75 :
76 : };
77 :
78 :
79 : /**
80 : * Callback for a GET /statistics-counter operation.
81 : *
82 : * @param cls closure for this function
83 : * @param scgr response details
84 : */
85 : static void
86 4 : get_statisticscounter_cb (
87 : struct GetStatisticsCounterState *scs,
88 : const struct TALER_MERCHANT_GetPrivateStatisticsCounterResponse *scgr)
89 : {
90 4 : const struct TALER_MERCHANT_HttpResponse *hr = &scgr->hr;
91 :
92 4 : scs->scgh = NULL;
93 4 : 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 4 : switch (hr->http_status)
104 : {
105 4 : case MHD_HTTP_OK:
106 : {
107 4 : 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 at least %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 4 : 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 at least %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 4 : 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 4 : 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 4 : get_statisticscounter_run (void *cls,
152 : const struct TALER_TESTING_Command *cmd,
153 : struct TALER_TESTING_Interpreter *is)
154 : {
155 4 : struct GetStatisticsCounterState *scs = cls;
156 :
157 4 : scs->is = is;
158 4 : scs->scgh = TALER_MERCHANT_get_private_statistics_counter_create (
159 : TALER_TESTING_interpreter_get_context (is),
160 : scs->merchant_url,
161 : scs->slug);
162 4 : TALER_MERCHANT_get_private_statistics_counter_set_options (
163 : scs->scgh,
164 : TALER_MERCHANT_get_private_statistics_counter_option_type (
165 : TALER_MERCHANT_STATISTICS_ALL));
166 : {
167 : enum TALER_ErrorCode ec;
168 :
169 4 : ec = TALER_MERCHANT_get_private_statistics_counter_start (
170 : scs->scgh,
171 : &get_statisticscounter_cb,
172 : scs);
173 4 : GNUNET_assert (TALER_EC_NONE == ec);
174 : }
175 4 : }
176 :
177 :
178 : /**
179 : * Free the state of a "GET statistics-counter" 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 4 : get_statisticscounter_cleanup (void *cls,
187 : const struct TALER_TESTING_Command *cmd)
188 : {
189 4 : struct GetStatisticsCounterState *scs = cls;
190 :
191 4 : if (NULL != scs->scgh)
192 : {
193 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
194 : "GET /statistics-counter operation did not complete\n");
195 0 : TALER_MERCHANT_get_private_statistics_counter_cancel (scs->scgh);
196 : }
197 4 : GNUNET_free (scs);
198 4 : }
199 :
200 :
201 : struct TALER_TESTING_Command
202 4 : TALER_TESTING_cmd_merchant_get_statisticscounter (
203 : const char *label,
204 : const char *merchant_url,
205 : const char *slug,
206 : uint64_t expected_buckets_length,
207 : uint64_t expected_intervals_length,
208 : unsigned int http_status)
209 : {
210 : struct GetStatisticsCounterState *scs;
211 :
212 4 : scs = GNUNET_new (struct GetStatisticsCounterState);
213 4 : scs->merchant_url = merchant_url;
214 4 : scs->slug = slug;
215 4 : scs->buckets_length = expected_buckets_length;
216 4 : scs->intervals_length = expected_intervals_length;
217 4 : scs->http_status = http_status;
218 : {
219 4 : struct TALER_TESTING_Command cmd = {
220 : .cls = scs,
221 : .label = label,
222 : .run = &get_statisticscounter_run,
223 : .cleanup = &get_statisticscounter_cleanup
224 : };
225 :
226 4 : return cmd;
227 : }
228 : }
229 :
230 :
231 : /* end of testing_api_cmd_get_statisticscounter.c */
|