Skip to content

Commit faec881

Browse files
committed
Implemented unit-tests for wsjcppjsonrpc20usersession
1 parent 2d80a39 commit faec881

File tree

6 files changed

+610
-61
lines changed

6 files changed

+610
-61
lines changed

src/wsjcpp_jsonrpc20.cpp

Lines changed: 334 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,334 @@
1+
#include "wsjcpp_jsonrpc20.h"
2+
#include <wsjcpp_core.h>
3+
4+
// ---------------------------------------------------------------------
5+
6+
/*!
7+
* WsjcppJsonRpc20UserSession - all data by current user session
8+
* */
9+
10+
WsjcppJsonRpc20UserSession::WsjcppJsonRpc20UserSession() {
11+
TAG = "WsjcppJsonRpc20UserSession";
12+
m_nUserID = 0;
13+
m_sUserRole = "";
14+
m_sUserEmail = "";
15+
m_sUserUuid = "";
16+
m_sUserName = "";
17+
m_sSessionUuid = "";
18+
m_nSessionCreated = 0;
19+
m_nSessionUpdated = 0;
20+
m_nSessionExpireAt = 0;
21+
m_jsonSessionCustom = nlohmann::json{};
22+
}
23+
24+
// ---------------------------------------------------------------------
25+
26+
WsjcppJsonRpc20UserSession::WsjcppJsonRpc20UserSession(nlohmann::json const& obj) : WsjcppJsonRpc20UserSession() {
27+
this->fillFrom(obj);
28+
}
29+
30+
// ---------------------------------------------------------------------
31+
32+
void WsjcppJsonRpc20UserSession::fillFrom(const nlohmann::json &obj) {
33+
if (obj.find("uuid") != obj.end()) {
34+
try {
35+
m_sSessionUuid = obj.at("uuid").get<std::string>();
36+
} catch (const std::exception &e) {
37+
WsjcppLog::err(TAG, "Something wrong param session.uuid in struct: "
38+
+ obj.dump() + "; error: " + std::string(e.what()));
39+
m_sSessionUuid = "";
40+
}
41+
} else {
42+
WsjcppLog::warn(TAG, "Not found param 'uuid' in struct");
43+
m_sSessionUuid = "";
44+
}
45+
46+
if (obj.find("created") != obj.end()) {
47+
try {
48+
m_nSessionCreated = obj.at("created").get<long>();
49+
} catch (const std::exception &e) {
50+
WsjcppLog::err(TAG, "Something wrong param session.created in struct: "
51+
+ obj.dump() + "; error: " + std::string(e.what()));
52+
m_nSessionCreated = 0;
53+
}
54+
} else {
55+
WsjcppLog::warn(TAG, "Not found param 'created' in struct");
56+
m_nSessionCreated = 0;
57+
}
58+
59+
if (obj.find("updated") != obj.end()) {
60+
try {
61+
m_nSessionUpdated = obj.at("updated").get<long>();
62+
} catch (const std::exception &e) {
63+
WsjcppLog::err(TAG, "Something wrong param session.updated in struct: "
64+
+ obj.dump() + "; error: " + std::string(e.what()));
65+
m_nSessionUpdated = 0;
66+
}
67+
} else {
68+
WsjcppLog::warn(TAG, "Not found param 'updated' in struct");
69+
m_nSessionUpdated = 0;
70+
}
71+
72+
if (obj.find("expire_at") != obj.end()) {
73+
try {
74+
m_nSessionExpireAt = obj.at("expire_at").get<long>();
75+
} catch (const std::exception &e) {
76+
WsjcppLog::err(TAG, "Something wrong param session.expire_at in struct: "
77+
+ obj.dump() + "; error: " + std::string(e.what()));
78+
m_nSessionExpireAt = 0;
79+
}
80+
} else {
81+
WsjcppLog::warn(TAG, "Not found param 'expire_at' in struct");
82+
m_nSessionExpireAt = 0;
83+
}
84+
85+
if (obj.find("custom") != obj.end()) {
86+
m_jsonSessionCustom = obj["custom"];
87+
} else {
88+
WsjcppLog::warn(TAG, "Not found param 'custom' in struct");
89+
m_jsonSessionCustom = nlohmann::json{};
90+
}
91+
92+
if (obj.find("user") != obj.end()) {
93+
nlohmann::json user = obj.at("user");
94+
95+
if (user.find("role") != user.end()) {
96+
try {
97+
m_sUserRole = user.at("role").get<std::string>();
98+
} catch (const std::exception &e) {
99+
WsjcppLog::err(TAG, "Something wrong param session.user.role in struct: "
100+
+ obj.dump() + "; error: " + std::string(e.what()));
101+
m_sUserRole = "";
102+
}
103+
} else {
104+
m_sUserRole = "";
105+
}
106+
107+
if (user.find("id") != user.end()) {
108+
try {
109+
m_nUserID = user.at("id").get<int>();
110+
} catch (const std::exception &e) {
111+
WsjcppLog::err(TAG, "Something wrong param session.user.id in struct: "
112+
+ obj.dump() + "; error: " + std::string(e.what()));
113+
m_nUserID = 0;
114+
}
115+
} else {
116+
m_nUserID = 0;
117+
}
118+
119+
// user.email
120+
if (user.find("email") != user.end()) {
121+
try {
122+
m_sUserEmail = user.at("email").get<std::string>();
123+
} catch (const std::exception &e) {
124+
WsjcppLog::err(TAG, "Something wrong param session.user.email in struct: "
125+
+ obj.dump() + "; error: " + std::string(e.what()));
126+
m_sUserEmail = "";
127+
}
128+
} else {
129+
m_sUserEmail = "";
130+
}
131+
132+
// user.nick
133+
if (user.find("name") != user.end()) {
134+
try {
135+
m_sUserName = user.at("name").get<std::string>();
136+
} catch (const std::exception &e) {
137+
WsjcppLog::err(TAG, "Something wrong param session.user.name in struct: "
138+
+ obj.dump() + "; error: " + std::string(e.what()));
139+
m_sUserName = "";
140+
}
141+
} else {
142+
m_sUserName = "";
143+
}
144+
145+
// user.uuid
146+
if (user.find("uuid") != user.end()) {
147+
try {
148+
m_sUserUuid = user.at("uuid").get<std::string>();
149+
} catch (const std::exception &e) {
150+
WsjcppLog::err(TAG, "Something wrong param session.user.uuid in struct: "
151+
+ obj.dump() + "; error: " + std::string(e.what()));
152+
m_sUserUuid = "";
153+
}
154+
} else {
155+
m_sUserUuid = "";
156+
}
157+
} else {
158+
WsjcppLog::warn(TAG, "Not found param 'user' in struct");
159+
m_nUserID = 0;
160+
m_sUserRole = "";
161+
m_sUserName = "";
162+
m_sUserUuid = "";
163+
m_sUserEmail = "";
164+
}
165+
}
166+
167+
// ---------------------------------------------------------------------
168+
169+
nlohmann::json WsjcppJsonRpc20UserSession::toJson() {
170+
nlohmann::json jsonRet;
171+
jsonRet["uuid"] = m_sSessionUuid;
172+
jsonRet["created"] = m_nSessionCreated;
173+
jsonRet["updated"] = m_nSessionUpdated;
174+
jsonRet["expire_at"] = m_nSessionExpireAt;
175+
176+
nlohmann::json jsonUser;
177+
jsonUser["id"] = m_nUserID;
178+
jsonUser["uuid"] = m_sUserUuid;
179+
jsonUser["name"] = m_sUserName;
180+
jsonUser["email"] = m_sUserEmail;
181+
jsonUser["role"] = m_sUserRole;
182+
jsonRet["user"] = jsonUser;
183+
184+
jsonRet["custom"] = m_jsonSessionCustom;
185+
186+
return jsonRet;
187+
}
188+
189+
// ---------------------------------------------------------------------
190+
191+
std::string WsjcppJsonRpc20UserSession::getSessionUuid() {
192+
return m_sSessionUuid;
193+
}
194+
195+
// ---------------------------------------------------------------------
196+
197+
void WsjcppJsonRpc20UserSession::setSessionUuid(const std::string& sSessionUuid) {
198+
m_sSessionUuid = sSessionUuid;
199+
}
200+
201+
202+
// ---------------------------------------------------------------------
203+
204+
long WsjcppJsonRpc20UserSession::getSessionCreated() {
205+
return m_nSessionCreated;
206+
}
207+
208+
// ---------------------------------------------------------------------
209+
210+
void WsjcppJsonRpc20UserSession::setSessionCreated(long nSessionCreated) {
211+
m_nSessionCreated = nSessionCreated;
212+
}
213+
214+
// ---------------------------------------------------------------------
215+
216+
long WsjcppJsonRpc20UserSession::getSessionUpdated() {
217+
return m_nSessionUpdated;
218+
}
219+
220+
// ---------------------------------------------------------------------
221+
222+
void WsjcppJsonRpc20UserSession::setSessionUpdated(long nSessionUpdated) {
223+
m_nSessionUpdated = nSessionUpdated;
224+
}
225+
226+
// ---------------------------------------------------------------------
227+
228+
long WsjcppJsonRpc20UserSession::getSessionExpireAt() {
229+
return m_nSessionExpireAt;
230+
}
231+
232+
// ---------------------------------------------------------------------
233+
234+
void WsjcppJsonRpc20UserSession::setSessionExpireAt(long nSessionExpireAt) {
235+
m_nSessionExpireAt = nSessionExpireAt;
236+
}
237+
238+
// ---------------------------------------------------------------------
239+
240+
int WsjcppJsonRpc20UserSession::getUserId() {
241+
return m_nUserID;
242+
}
243+
244+
// ---------------------------------------------------------------------
245+
246+
void WsjcppJsonRpc20UserSession::setUserId(int nUserId) {
247+
m_nUserID = nUserId;
248+
}
249+
250+
// ---------------------------------------------------------------------
251+
252+
std::string WsjcppJsonRpc20UserSession::getUserUuid() {
253+
return m_sUserUuid;
254+
}
255+
256+
// ---------------------------------------------------------------------
257+
258+
void WsjcppJsonRpc20UserSession::setUserUuid(const std::string& sUserUuid) {
259+
m_sUserUuid = sUserUuid;
260+
}
261+
262+
// ---------------------------------------------------------------------
263+
264+
std::string WsjcppJsonRpc20UserSession::getUserName() {
265+
return m_sUserName;
266+
}
267+
268+
// ---------------------------------------------------------------------
269+
270+
void WsjcppJsonRpc20UserSession::setUserName(const std::string &sUserName) {
271+
m_sUserName = sUserName;
272+
}
273+
274+
// ---------------------------------------------------------------------
275+
276+
std::string WsjcppJsonRpc20UserSession::getUserEmail() {
277+
return m_sUserEmail;
278+
}
279+
280+
// ---------------------------------------------------------------------
281+
282+
void WsjcppJsonRpc20UserSession::setUserEmail(const std::string &sUserEmail) {
283+
m_sUserEmail = sUserEmail;
284+
}
285+
286+
// ---------------------------------------------------------------------
287+
288+
std::string WsjcppJsonRpc20UserSession::getUserRole() {
289+
return m_sUserRole;
290+
}
291+
292+
// ---------------------------------------------------------------------
293+
294+
void WsjcppJsonRpc20UserSession::setUserRole(const std::string &sUserRole) {
295+
m_sUserRole = sUserRole;
296+
}
297+
298+
// ---------------------------------------------------------------------
299+
300+
bool WsjcppJsonRpc20UserSession::isAdmin() {
301+
return m_sUserRole == "admin";
302+
}
303+
304+
// ---------------------------------------------------------------------
305+
306+
bool WsjcppJsonRpc20UserSession::isUser() {
307+
return m_sUserRole == "user";
308+
}
309+
310+
// ---------------------------------------------------------------------
311+
312+
bool WsjcppJsonRpc20UserSession::isTester() {
313+
return m_sUserRole == "tester";
314+
}
315+
316+
// ---------------------------------------------------------------------
317+
318+
bool WsjcppJsonRpc20UserSession::hasRole() {
319+
return m_sUserRole != "";
320+
}
321+
322+
// ---------------------------------------------------------------------
323+
324+
nlohmann::json WsjcppJsonRpc20UserSession::getSessionCustom() {
325+
return m_jsonSessionCustom;
326+
}
327+
328+
// ---------------------------------------------------------------------
329+
330+
void WsjcppJsonRpc20UserSession::setSessionCustom(const nlohmann::json &jsonSessionCustom) {
331+
m_jsonSessionCustom = jsonSessionCustom;
332+
}
333+
334+
// ---------------------------------------------------------------------

0 commit comments

Comments
 (0)