Modify database query appropriately
[platform/core/connectivity/stc-manager.git] / unittest / statistics.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <iostream>
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22
23 #include "statistics.h"
24 #include "common.h"
25
26 static void AsyncReadyCallback(GObject *source_object,
27         GAsyncResult *res, gpointer user_data)
28 {
29         MainLoop *M = (MainLoop *)user_data;
30         GLOGD("Succeeded to response async callback");
31         M->quit();
32 }
33
34 SRule::SRule()
35 {
36         this->m_IfaceType = IFACE_UNKNOWN;
37         this->m_TimePeriod = TIME_PERIOD_UNKNOWN;
38         this->m_Interval.from = 0;
39         this->m_Interval.to = 0;
40 }
41
42 SRule::~SRule()
43 {
44 }
45
46 Statistics::Statistics()
47 {
48         Create();
49 }
50
51 Statistics::~Statistics()
52 {
53         Destroy();
54 }
55
56 error_e Statistics::SetRule(const char *app_id, const char *subscriber_id,
57         iface_type_e iface_type, time_t from, time_t to, time_period_e time_period)
58 {
59         if (app_id == NULL || strlen(app_id) == 0)
60                 this->m_Rule.m_AppID[0] = '\0';
61         else
62                 g_strlcpy(this->m_Rule.m_AppID, app_id, APP_ID_LEN);
63
64         if (subscriber_id == NULL || strlen(subscriber_id) == 0)
65                 this->m_Rule.m_SubscriberID[0] = '\0';
66         else
67                 g_strlcpy(this->m_Rule.m_SubscriberID, subscriber_id, SUBSCRIBER_ID_LEN);
68
69         switch(iface_type) {
70         case IFACE_UNKNOWN:
71         case IFACE_DATACALL:
72         case IFACE_WIFI:
73         case IFACE_WIRED:
74         case IFACE_BLUETOOTH:
75         case IFACE_ALL:
76                 this->m_Rule.m_IfaceType = iface_type;
77                 break;
78         default:
79                 return ERROR_INVALID_PARAMETER;
80         }
81
82         if (from < 0 || to < 0)
83                 return ERROR_INVALID_PARAMETER;
84
85         this->m_Rule.m_Interval.from = from;
86         this->m_Rule.m_Interval.to = to;
87
88         switch(time_period) {
89         case TIME_PERIOD_UNKNOWN:
90         case TIME_PERIOD_HOUR:
91         case TIME_PERIOD_DAY:
92         case TIME_PERIOD_WEEK:
93         case TIME_PERIOD_MONTH:
94                 this->m_Rule.m_TimePeriod = time_period;
95                 break;
96         default:
97                 return ERROR_INVALID_PARAMETER;
98         }
99
100         return ERROR_NONE;
101 }
102
103 time_t Statistics::MakeTime(int year, int mon, int day, int hour, int min)
104 {
105         struct tm curr = { 0, };
106         curr.tm_year = year - 1900;
107         curr.tm_mon = mon - 1;
108         curr.tm_mday = day;
109         curr.tm_hour = hour;
110         curr.tm_min = min;
111         return mktime(&curr);
112 }
113
114 void Statistics::MakeRuleParams(GVariant **params, int mode)
115 {
116         GVariantBuilder *builder;
117
118         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
119
120         g_variant_builder_add(builder, "{sv}",
121                 STATISTICS_RULE_INTERVAL_FROM,
122                 g_variant_new_uint64(this->m_Rule.m_Interval.from));
123
124         g_variant_builder_add(builder, "{sv}",
125                 STATISTICS_RULE_INTERVAL_TO,
126                 g_variant_new_uint64(this->m_Rule.m_Interval.to));
127
128         g_variant_builder_add(builder, "{sv}",
129                 STATISTICS_RULE_IFTYPE,
130                 g_variant_new_uint16(this->m_Rule.m_IfaceType));
131
132         g_variant_builder_add(builder, "{sv}",
133                 STATISTICS_RULE_TIME_PERIOD,
134                 g_variant_new_int32(this->m_Rule.m_TimePeriod));
135
136         switch (mode) {
137         case 0: /* reset */
138                 g_variant_builder_add(builder, "{sv}",
139                         STATISTICS_RULE_APP_ID,
140                         g_variant_new_string(this->m_Rule.m_AppID));
141
142                 g_variant_builder_add(builder, "{sv}",
143                         RESET_RULE_SUBSCRIBER_ID,
144                         g_variant_new_string(this->m_Rule.m_SubscriberID));
145
146                 *params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
147                 break;
148         case 1: /* get app */
149                 *params = g_variant_new("(s@a{sv})", this->m_Rule.m_AppID, g_variant_builder_end(builder));
150                 break;
151         case 2: /* get total */
152                 *params = g_variant_new("(s@a{sv})", "", g_variant_builder_end(builder));
153                 break;
154         case 3: /* get all */
155         default:
156                 *params = g_variant_new("(@a{sv})", g_variant_builder_end(builder));            
157                 break;
158         }
159
160         g_variant_builder_unref(builder);
161 }
162
163 error_e Statistics::InitStatistics(void)
164 {
165         GVariant *message = NULL;
166         error_e error = ERROR_NONE;
167
168         message = InvokeMethod(STC_MGR_SERVICE,
169                 STC_MGR_STATISTICS_PATH,
170                 STC_MGR_STATISTICS_INTERFACE,
171                 STC_MGR_METHOD_STATISTICS_INIT,
172                 NULL,
173                 &error);
174
175         if (message == NULL) {
176                 GLOGD("Failed to invoke dbus method");
177                 return error;
178         }
179
180         GLOGD("Succeeded to init statistics");
181         g_variant_unref(message);
182
183         return ERROR_NONE;
184 }
185
186 error_e Statistics::GetStatistics(void)
187 {
188         MainLoop M;
189         GVariant *params = NULL;
190         error_e error = ERROR_NONE;
191
192         MakeRuleParams(&params, 1);
193
194         error = InvokeMethodNonblock(STC_MGR_SERVICE,
195                 STC_MGR_STATISTICS_PATH,
196                 STC_MGR_STATISTICS_INTERFACE,
197                 STC_MGR_METHOD_STATISTICS_GET_PER_APP_ID,
198                 params,
199                 DBUS_REPLY_TIMEOUT,
200                 AsyncReadyCallback,
201                 &M);
202
203         if (error != ERROR_NONE) {
204                 GLOGD("Failed to invoke dbus method nonblock");
205                 return error;
206         }
207
208         M.run(GMAINTIMEOUT);
209         GLOGD("Succeeded to get statistics per app ID");
210
211         return ERROR_NONE;
212 }
213
214 error_e Statistics::GetTotalStatistics(void)
215 {
216         MainLoop M;
217         GVariant *params = NULL;
218         error_e error = ERROR_NONE;
219
220         MakeRuleParams(&params, 2);
221
222         error = InvokeMethodNonblock(STC_MGR_SERVICE,
223                 STC_MGR_STATISTICS_PATH,
224                 STC_MGR_STATISTICS_INTERFACE,
225                 STC_MGR_METHOD_STATISTICS_GET_TOTAL,
226                 params,
227                 DBUS_REPLY_TIMEOUT,
228                 AsyncReadyCallback,
229                 &M);
230
231         if (error != ERROR_NONE) {
232                 GLOGD("Failed to invoke dbus method nonblock");
233                 return error;
234         }
235
236         M.run(GMAINTIMEOUT);
237         GLOGD("Succeeded to get total statistics");
238
239         return ERROR_NONE;
240 }
241
242 error_e Statistics::GetAllStatistics(void)
243 {
244         MainLoop M;
245         GVariant *params = NULL;
246         error_e error = ERROR_NONE;
247
248         MakeRuleParams(&params, 3);
249
250         error = InvokeMethodNonblock(STC_MGR_SERVICE,
251                 STC_MGR_STATISTICS_PATH,
252                 STC_MGR_STATISTICS_INTERFACE,
253                 STC_MGR_METHOD_STATISTICS_GET_ALL,
254                 params,
255                 DBUS_REPLY_TIMEOUT,
256                 AsyncReadyCallback,
257                 &M);
258
259         if (error != ERROR_NONE) {
260                 GLOGD("Failed to invoke dbus method nonblock");
261                 return error;
262         }
263
264         M.run(GMAINTIMEOUT);
265         GLOGD("Succeeded to get all statistics");
266
267         return ERROR_NONE;
268 }
269
270 error_e Statistics::ResetStatistics(void)
271 {
272         GVariant *message = NULL;
273         GVariant *params = NULL;
274         error_e error = ERROR_NONE;
275         int result = 0;
276
277         MakeRuleParams(&params, 0);
278
279         message = InvokeMethod(STC_MGR_SERVICE,
280                 STC_MGR_STATISTICS_PATH,
281                 STC_MGR_STATISTICS_INTERFACE,
282                 STC_MGR_METHOD_STATISTICS_RESET,
283                 params,
284                 &error);
285
286         if (message == NULL) {
287                 GLOGD("Failed to invoke dbus method");
288                 return error;
289         }
290
291         g_variant_get(message, "(i)", &result);
292         GLOGD("Succeeded to reset statistics", result);
293         g_variant_unref(message);
294
295         return ERROR_NONE;
296 }