Skip to content

Commit a3a9336

Browse files
Use Boost::json olp-cpp-sdk-authentication functional tests
Migrating from RapidJSON Relates-To: OCMAM-443 Signed-off-by: Rustam Gamidov <ext-rustam.gamidov@here.com>
1 parent cc46e90 commit a3a9336

File tree

2 files changed

+69
-75
lines changed

2 files changed

+69
-75
lines changed

tests/functional/olp-cpp-sdk-authentication/AuthenticationTestUtils.cpp

Lines changed: 59 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019 HERE Europe B.V.
2+
* Copyright (C) 2019-2026 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,26 +19,34 @@
1919

2020
#include "AuthenticationTestUtils.h"
2121

22-
#include <thread>
2322
#include <future>
23+
#include <thread>
2424

2525
#ifndef WIN32
2626
#include <unistd.h>
2727
#endif
2828

29-
#include <rapidjson/document.h>
30-
#include <rapidjson/istreamwrapper.h>
31-
#include <rapidjson/stringbuffer.h>
32-
#include <rapidjson/writer.h>
29+
#include <boost/json/parse.hpp>
30+
#include <boost/json/serialize.hpp>
31+
#include <boost/json/value.hpp>
32+
3333
#include <olp/core/http/HttpStatusCode.h>
3434
#include <olp/core/http/Network.h>
3535
#include <olp/core/logging/Log.h>
3636
#include <testutils/CustomParameters.hpp>
3737
#include "TestConstants.h"
3838

39-
using namespace ::olp::authentication;
40-
4139
namespace {
40+
41+
using olp::authentication::kAccessToken;
42+
using olp::authentication::kAndParam;
43+
using olp::authentication::kEqualsParam;
44+
using olp::authentication::kHereAccountStagingURL;
45+
using olp::authentication::kMaxRetryCount;
46+
using olp::authentication::kQuestionParam;
47+
using olp::authentication::kRetryDelayInSecs;
48+
using olp::authentication::kTestUserName;
49+
4250
constexpr auto kFacebookUrl = "https://graph.facebook.com/v2.12";
4351
constexpr auto kId = "id";
4452

@@ -79,8 +87,8 @@ bool AuthenticationTestUtils::CreateFacebookTestUser(
7987
unsigned int retry = 0u;
8088
do {
8189
if (retry > 0u) {
82-
OLP_SDK_LOG_WARNING(__func__, "Request retry attempted (" << retry
83-
<< ")");
90+
OLP_SDK_LOG_WARNING(__func__,
91+
"Request retry attempted (" << retry << ")");
8492
std::this_thread::sleep_for(
8593
std::chrono::seconds(retry * kRetryDelayInSecs));
8694
}
@@ -89,27 +97,26 @@ bool AuthenticationTestUtils::CreateFacebookTestUser(
8997

9098
std::promise<void> promise;
9199
auto future = promise.get_future();
92-
network.Send(request, payload,
93-
[payload, &promise,
94-
&user](const olp::http::NetworkResponse &network_response) {
95-
user.token.status = network_response.GetStatus();
96-
if (user.token.status == olp::http::HttpStatusCode::OK) {
97-
auto document = std::make_shared<rapidjson::Document>();
98-
rapidjson::IStreamWrapper stream(*payload);
99-
document->ParseStream(stream);
100-
const bool is_valid =
101-
!document->HasParseError() &&
102-
document->HasMember(kAccessToken.c_str()) &&
103-
document->HasMember(kId);
104-
105-
if (is_valid) {
106-
user.token.access_token =
107-
(*document)[kAccessToken.c_str()].GetString();
108-
user.id = (*document)[kId].GetString();
109-
}
110-
}
111-
promise.set_value();
112-
});
100+
network.Send(
101+
request, payload,
102+
[payload, &promise,
103+
&user](const olp::http::NetworkResponse &network_response) {
104+
user.token.status = network_response.GetStatus();
105+
if (user.token.status == olp::http::HttpStatusCode::OK) {
106+
boost::json::error_code ec;
107+
auto document = boost::json::parse(*payload, ec);
108+
const bool is_valid = !ec.failed() && document.is_object() &&
109+
document.as_object().contains(kAccessToken) &&
110+
document.as_object().contains(kId);
111+
112+
if (is_valid) {
113+
user.token.access_token =
114+
document.as_object()[kAccessToken].get_string().c_str();
115+
user.id = document.as_object()[kId].get_string().c_str();
116+
}
117+
}
118+
promise.set_value();
119+
});
113120
future.wait();
114121
} while ((user.token.status < 0) && (++retry < kMaxRetryCount));
115122

@@ -132,8 +139,8 @@ bool AuthenticationTestUtils::DeleteFacebookTestUser(
132139
unsigned int retry = 0u;
133140
do {
134141
if (retry > 0u) {
135-
OLP_SDK_LOG_WARNING(__func__, "Request retry attempted (" << retry
136-
<< ")");
142+
OLP_SDK_LOG_WARNING(__func__,
143+
"Request retry attempted (" << retry << ")");
137144
std::this_thread::sleep_for(
138145
std::chrono::seconds(retry * kRetryDelayInSecs));
139146
}
@@ -220,8 +227,8 @@ bool AuthenticationTestUtils::GetAccessTokenImpl(
220227
unsigned int retry = 0u;
221228
do {
222229
if (retry > 0u) {
223-
OLP_SDK_LOG_WARNING(__func__, "Request retry attempted (" << retry
224-
<< ")");
230+
OLP_SDK_LOG_WARNING(__func__,
231+
"Request retry attempted (" << retry << ")");
225232
std::this_thread::sleep_for(
226233
std::chrono::seconds(retry * kRetryDelayInSecs));
227234
}
@@ -230,23 +237,23 @@ bool AuthenticationTestUtils::GetAccessTokenImpl(
230237

231238
std::promise<void> promise;
232239
auto future = promise.get_future();
233-
network.Send(request, payload,
234-
[payload, &promise,
235-
&token](const olp::http::NetworkResponse &network_response) {
236-
token.status = network_response.GetStatus();
237-
if (token.status == olp::http::HttpStatusCode::OK) {
238-
auto document = std::make_shared<rapidjson::Document>();
239-
rapidjson::IStreamWrapper stream(*payload);
240-
document->ParseStream(stream);
241-
bool is_valid = !document->HasParseError() &&
242-
document->HasMember(kAccessToken.c_str());
243-
if (is_valid) {
244-
token.access_token =
245-
(*document)[kAccessToken.c_str()].GetString();
246-
}
247-
}
248-
promise.set_value();
249-
});
240+
network.Send(
241+
request, payload,
242+
[payload, &promise,
243+
&token](const olp::http::NetworkResponse &network_response) {
244+
token.status = network_response.GetStatus();
245+
if (token.status == olp::http::HttpStatusCode::OK) {
246+
boost::json::error_code ec;
247+
auto document = boost::json::parse(*payload, ec);
248+
const bool is_valid = !ec.failed() && document.is_object() &&
249+
document.as_object().contains(kAccessToken);
250+
if (is_valid) {
251+
token.access_token =
252+
document.as_object()[kAccessToken].get_string().c_str();
253+
}
254+
}
255+
promise.set_value();
256+
});
250257
future.wait();
251258
} while ((token.status < 0) && (++retry < kMaxRetryCount));
252259

tests/functional/olp-cpp-sdk-authentication/FederatedAuthenticationTest.cpp

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020 HERE Europe B.V.
2+
* Copyright (C) 2020-2025 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,18 +17,15 @@
1717
* License-Filename: LICENSE
1818
*/
1919

20-
#include <rapidjson/rapidjson.h>
21-
#include <rapidjson/stringbuffer.h>
22-
#include <rapidjson/writer.h>
20+
#include <boost/json/parse.hpp>
21+
#include <boost/json/serialize.hpp>
2322

2423
#include <olp/core/http/HttpStatusCode.h>
2524
#include <olp/core/porting/make_unique.h>
2625
#include "AuthenticationCommonTestFixture.h"
2726
#include "AuthenticationTestUtils.h"
2827
#include "TestConstants.h"
2928

30-
using namespace ::olp::authentication;
31-
3229
namespace {
3330

3431
class FederatedAuthenticationTest : public AuthenticationCommonTestFixture {
@@ -65,23 +62,13 @@ class FederatedAuthenticationTest : public AuthenticationCommonTestFixture {
6562

6663
std::string GoogleAuthenticationBody(const std::string& email,
6764
const std::string& access_token) {
68-
rapidjson::StringBuffer data;
69-
70-
rapidjson::Writer<rapidjson::StringBuffer> writer(data);
71-
writer.StartObject();
72-
writer.Key("grantType");
73-
writer.String("google");
74-
writer.Key("accessToken");
75-
writer.String(access_token.c_str());
76-
writer.Key("countryCode");
77-
writer.String("USA");
78-
writer.Key("language");
79-
writer.String("en");
80-
writer.Key("email");
81-
writer.String(email.c_str());
82-
writer.EndObject();
83-
84-
return data.GetString();
65+
boost::json::object object;
66+
object["grantType"] = "google";
67+
object["accessToken"] = access_token;
68+
object["countryCode"] = "USA";
69+
object["language"] = "en";
70+
object["email"] = email;
71+
return boost::json::serialize(object);
8572
}
8673

8774
protected:

0 commit comments

Comments
 (0)