3 #include "stg/user_property.h"
6 #include "testsettings.h"
7 #include "testtariffs.h"
11 #include "testusers.h"
13 class AFTER_CONNECTED_NOTIFIER : public PROPERTY_NOTIFIER_BASE<bool>,
16 AFTER_CONNECTED_NOTIFIER()
20 void Notify(const bool & oldValue, const bool & newValue);
22 size_t GetConnects() const { return connects; }
23 size_t GetDisconnects() const { return disconnects; }
30 class TEST_SETTINGS_LOCAL : public TEST_SETTINGS {
32 TEST_SETTINGS_LOCAL(bool _reconnectOnTariffChange)
34 reconnectOnTariffChange(_reconnectOnTariffChange)
37 bool GetReconnectOnTariffChange() const { return reconnectOnTariffChange; }
40 bool reconnectOnTariffChange;
45 struct reconnect_on_tariff_change_data {
48 typedef test_group<reconnect_on_tariff_change_data> tg;
49 tg reconnect_on_tariff_change_test_group("Reconnect on tariff change tests group");
51 typedef tg::object testobject;
55 void testobject::test<1>()
57 set_test_name("Check normal behaviour");
59 TEST_SETTINGS_LOCAL settings(false);
65 USER_IMPL user(&settings, &store, &tariffs, &admin, &users);
67 AFTER_CONNECTED_NOTIFIER connectionNotifier;
69 user.AddConnectedAfterNotifier(&connectionNotifier);
71 USER_PROPERTY<double> & cash(user.GetProperty().cash);
72 USER_PROPERTY<std::string> & tariffName(user.GetProperty().tariffName);
73 USER_PROPERTY<USER_IPS> & ips(user.GetProperty().ips);
77 ensure_equals("user.connected = false", user.GetConnected(), false);
78 ensure_equals("connects = 0", connectionNotifier.GetConnects(), 0);
79 ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), 0);
81 ensure_equals("user.tariffName == NO_TARIFF_NAME", user.GetProperty().tariffName.ConstData(), NO_TARIFF_NAME);
83 user.Authorize(inet_strington("127.0.0.1"), 0, &auth);
86 ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
88 ensure_equals("user.connected = true", user.GetConnected(), true);
89 ensure_equals("connects = 1", connectionNotifier.GetConnects(), 1);
90 ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), 0);
93 ensure_equals("user.tariffName == 'test'", user.GetProperty().tariffName.ConstData(), "test");
95 ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
97 ensure_equals("user.connected = true", user.GetConnected(), true);
98 ensure_equals("connects = 1", connectionNotifier.GetConnects(), 1);
99 ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), 0);
105 void testobject::test<2>()
107 set_test_name("Check reconnect on tariff change");
109 TEST_SETTINGS_LOCAL settings(true);
111 TEST_SETTINGS * s1 = &settings;
112 SETTINGS * s2 = &settings;
114 ensure("settings.GetReconnectOnTariffChange() == true", settings.GetReconnectOnTariffChange());
115 ensure("s1->GetReconnectOnTariffChange() == true", s1->GetReconnectOnTariffChange());
116 ensure("s2->GetReconnectOnTariffChange() == true", s2->GetReconnectOnTariffChange());
118 TEST_TARIFFS tariffs;
123 USER_IMPL user(&settings, &store, &tariffs, &admin, &users);
125 AFTER_CONNECTED_NOTIFIER connectionNotifier;
127 user.AddConnectedAfterNotifier(&connectionNotifier);
129 USER_PROPERTY<double> & cash(user.GetProperty().cash);
130 USER_PROPERTY<std::string> & tariffName(user.GetProperty().tariffName);
131 USER_PROPERTY<USER_IPS> & ips(user.GetProperty().ips);
135 ensure_equals("user.connected = false", user.GetConnected(), false);
136 ensure_equals("connects = 0", connectionNotifier.GetConnects(), 0);
137 ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), 0);
139 ensure_equals("user.tariffName == NO_TARIFF_NAME", user.GetProperty().tariffName.ConstData(), NO_TARIFF_NAME);
141 user.Authorize(inet_strington("127.0.0.1"), 0, &auth);
144 ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
146 ensure_equals("user.connected = true", user.GetConnected(), true);
147 ensure_equals("connects = 1", connectionNotifier.GetConnects(), 1);
148 ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), 0);
151 ensure_equals("user.tariffName == 'test'", user.GetProperty().tariffName.ConstData(), "test");
153 ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
155 ensure_equals("user.connected = true", user.GetConnected(), true);
156 ensure_equals("connects = 2", connectionNotifier.GetConnects(), 2);
157 ensure_equals("disconnects = 1", connectionNotifier.GetDisconnects(), 1);
161 void AFTER_CONNECTED_NOTIFIER::Notify(const bool & oldValue, const bool & newValue)
163 if (!oldValue && newValue)
165 if (oldValue && !newValue)