3 #include "stg/settings.h"
4 #include "stg/user_property.h"
6 #include "tariff_impl.h"
8 const volatile time_t stgTime = 0;
12 struct fee_charge_rules_data {
15 typedef test_group<fee_charge_rules_data> tg;
16 tg fee_charge_rules_test_group("Fee charge rules tests group");
18 typedef tg::object testobject;
20 class TEST_SETTINGS : public SETTINGS {
22 TEST_SETTINGS(unsigned _feeChargeType)
23 : feeChargeType(_feeChargeType) {}
25 const std::string & GetDirName(size_t) const { return dirName; }
26 const std::string & GetScriptsDir() const { return scriptsDir; }
27 unsigned GetDetailStatWritePeriod() const { return 10; }
28 unsigned GetStatWritePeriod() const { return 10; }
29 unsigned GetDayFee() const { return 0; }
30 bool GetFullFee() const { return false; }
31 unsigned GetDayResetTraff() const { return 0; }
32 bool GetSpreadFee() const { return false; }
33 bool GetFreeMbAllowInet() const { return false; }
34 bool GetDayFeeIsLastDay() const { return false; }
35 bool GetWriteFreeMbTraffCost() const { return false; }
36 bool GetShowFeeInCash() const { return false; }
37 unsigned GetMessageTimeout() const { return 0; }
38 unsigned GetFeeChargeType() const { return feeChargeType; }
39 const std::string & GetMonitorDir() const { return monitorDir; }
40 bool GetMonitoring() const { return false; }
44 std::string scriptsDir;
45 std::string monitorDir;
46 unsigned feeChargeType;
49 class TEST_TARIFFS : public TARIFFS {
53 int ReadTariffs () { return 0; }
54 const TARIFF * FindByName(const std::string & name) const { return &testTariff; }
55 const TARIFF * GetNoTariff() const { return NULL; }
56 int GetTariffsNum() const { return 0; }
57 int Del(const std::string & name, const ADMIN * admin) { return 0; }
58 int Add(const std::string & name, const ADMIN * admin) { return 0; }
59 int Chg(const TARIFF_DATA & td, const ADMIN * admin) { return 0; }
61 void GetTariffsData(std::list<TARIFF_DATA> * tdl) {}
63 const std::string & GetStrError() const { return strError; }
65 void SetFee(double fee);
69 TARIFF_IMPL testTariff;
72 class TEST_ADMIN : public ADMIN {
74 TEST_ADMIN() : priv(0xffFFffFF) {}
76 ADMIN & operator=(const ADMIN &) { return *this; }
77 ADMIN & operator=(const ADMIN_CONF &) { return *this; }
78 bool operator==(const ADMIN & rhs) const { return true; }
79 bool operator!=(const ADMIN & rhs) const { return false; }
80 bool operator<(const ADMIN & rhs) const { return true; }
81 bool operator<=(const ADMIN & rhs) const { return true; }
83 const std::string & GetPassword() const { return password; }
84 const std::string & GetLogin() const { return login; }
85 PRIV const * GetPriv() const { return &priv; }
86 uint16_t GetPrivAsInt() const { return priv.ToInt(); }
87 const ADMIN_CONF & GetConf() const { return conf; }
88 uint32_t GetIP() const { return ip; }
89 std::string GetIPStr() const { return inet_ntostring(ip); }
90 void SetIP(uint32_t ip) { TEST_ADMIN::ip = ip; }
91 const std::string GetLogStr() const { return ""; }
101 class TEST_STORE : public STORE {
105 int GetUsersList(std::vector<std::string> * usersList) const { return 0; }
106 int AddUser(const std::string & login) const { return 0; }
107 int DelUser(const std::string & login) const { return 0; }
108 int SaveUserStat(const USER_STAT & stat, const std::string & login) const { return 0; }
109 int SaveUserConf(const USER_CONF & conf, const std::string & login) const { return 0; }
110 int RestoreUserStat(USER_STAT * stat, const std::string & login) const { return 0; }
111 int RestoreUserConf(USER_CONF * conf, const std::string & login) const { return 0; }
113 int WriteUserChgLog(const std::string & login,
114 const std::string & admLogin,
116 const std::string & paramName,
117 const std::string & oldValue,
118 const std::string & newValue,
119 const std::string & message = "") const { return 0; }
121 int WriteUserConnect(const std::string & login, uint32_t ip) const { return 0; }
123 int WriteUserDisconnect(const std::string & login,
124 const DIR_TRAFF & up,
125 const DIR_TRAFF & down,
126 const DIR_TRAFF & sessionUp,
127 const DIR_TRAFF & sessionDown,
130 const std::string & reason) const { return 0; }
132 int WriteDetailedStat(const TRAFF_STAT & statTree,
134 const std::string & login) const { return 0; }
136 int AddMessage(STG_MSG * msg, const std::string & login) const { return 0; }
137 int EditMessage(const STG_MSG & msg, const std::string & login) const { return 0; }
138 int GetMessage(uint64_t id, STG_MSG * msg, const std::string & login) const { return 0; }
139 int DelMessage(uint64_t id, const std::string & login) const { return 0; }
140 int GetMessageHdrs(vector<STG_MSG_HDR> * hdrsList, const std::string & login) const { return 0; }
142 int SaveMonthStat(const USER_STAT & stat, int month, int year, const std::string & login) const { return 0; }
144 int GetAdminsList(std::vector<std::string> * adminsList) const { return 0; }
145 int SaveAdmin(const ADMIN_CONF & ac) const { return 0; }
146 int RestoreAdmin(ADMIN_CONF * ac, const std::string & login) const { return 0; }
147 int AddAdmin(const std::string & login) const { return 0; }
148 int DelAdmin(const std::string & login) const { return 0; }
150 int GetTariffsList(std::vector<std::string> * tariffsList) const { return 0; }
151 int AddTariff(const std::string & name) const { return 0; }
152 int DelTariff(const std::string & name) const { return 0; }
153 int SaveTariff(const TARIFF_DATA & td, const std::string & tariffName) const { return 0; }
154 int RestoreTariff(TARIFF_DATA * td, const std::string & tariffName) const { return 0; }
156 int GetCorpsList(std::vector<std::string> * corpsList) const { return 0; }
157 int SaveCorp(const CORP_CONF & cc) const { return 0; }
158 int RestoreCorp(CORP_CONF * cc, const std::string & name) const { return 0; }
159 int AddCorp(const std::string & name) const { return 0; }
160 int DelCorp(const std::string & name) const { return 0; }
162 int GetServicesList(std::vector<std::string> * corpsList) const { return 0; }
163 int SaveService(const SERVICE_CONF & sc) const { return 0; }
164 int RestoreService(SERVICE_CONF * sc, const std::string & name) const { return 0; }
165 int AddService(const std::string & name) const { return 0; }
166 int DelService(const std::string & name) const { return 0; }
168 void SetSettings(const MODULE_SETTINGS & s) {}
169 int ParseSettings() { return 0; }
170 const std::string & GetStrError() const { return strError; }
171 const std::string & GetVersion() const { return version; }
174 std::string strError;
180 void testobject::test<1>()
182 set_test_name("Check classic rules");
184 TEST_SETTINGS settings(0);
185 TEST_TARIFFS tariffs;
188 USER_IMPL user(&settings, &store, &tariffs, &admin, NULL);
190 USER_PROPERTY<double> & cash(user.GetProperty().cash);
191 USER_PROPERTY<std::string> & tariffName(user.GetProperty().tariffName);
193 ensure_equals("user.cash == 0", user.GetProperty().cash, 0);
195 ensure_equals("user.cash == 100", user.GetProperty().cash, 100);
199 ensure_equals("user.tariffName == 'test'", user.GetProperty().tariffName.ConstData(), "test");
200 user.ProcessDayFee();
201 ensure_equals("user.cash == 50", user.GetProperty().cash, 50);
202 user.ProcessDayFee();
203 ensure_equals("user.cash == 0", user.GetProperty().cash, 0);
204 user.ProcessDayFee();
205 ensure_equals("user.cash == -50", user.GetProperty().cash, -50);
210 void testobject::test<2>()
212 set_test_name("Check second rules (allow fee if cash value is positive)");
214 TEST_SETTINGS settings(1);
215 TEST_TARIFFS tariffs;
218 USER_IMPL user(&settings, &store, &tariffs, &admin, NULL);
220 USER_PROPERTY<double> & cash(user.GetProperty().cash);
221 USER_PROPERTY<std::string> & tariffName(user.GetProperty().tariffName);
223 ensure_equals("user.cash == 0", user.GetProperty().cash, 0);
225 ensure_equals("user.cash == 100", user.GetProperty().cash, 100);
229 ensure_equals("user.tariffName == 'test'", user.GetProperty().tariffName.ConstData(), "test");
230 user.ProcessDayFee();
231 ensure_equals("user.cash == 50", user.GetProperty().cash, 50);
232 user.ProcessDayFee();
233 ensure_equals("user.cash == 0", user.GetProperty().cash, 0);
234 user.ProcessDayFee();
235 ensure_equals("user.cash == 0", user.GetProperty().cash, 0);
237 ensure_equals("user.cash == 49", user.GetProperty().cash, 49);
238 user.ProcessDayFee();
239 ensure_equals("user.cash == -1", user.GetProperty().cash, -1);
244 void testobject::test<3>()
246 set_test_name("Check third rules (allow fee if cash value is greater than fee)");
248 TEST_SETTINGS settings(2);
249 TEST_TARIFFS tariffs;
252 USER_IMPL user(&settings, &store, &tariffs, &admin, NULL);
254 USER_PROPERTY<double> & cash(user.GetProperty().cash);
255 USER_PROPERTY<std::string> & tariffName(user.GetProperty().tariffName);
257 ensure_equals("user.cash == 0", user.GetProperty().cash, 0);
259 ensure_equals("user.cash == 100", user.GetProperty().cash, 100);
263 ensure_equals("user.tariffName == 'test'", user.GetProperty().tariffName.ConstData(), "test");
264 user.ProcessDayFee();
265 ensure_equals("user.cash == 50", user.GetProperty().cash, 50);
266 user.ProcessDayFee();
267 ensure_equals("user.cash == 50", user.GetProperty().cash, 50);
269 user.ProcessDayFee();
270 ensure_equals("user.cash == 1", user.GetProperty().cash, 1);
272 ensure_equals("user.cash == 0", user.GetProperty().cash, 0);
273 user.ProcessDayFee();
274 ensure_equals("user.cash == 0", user.GetProperty().cash, 0);
277 void TEST_TARIFFS::SetFee(double fee)
279 TARIFF_DATA td(testTariff.GetTariffData());
280 td.tariffConf.fee = fee;