Skip to content

Commit 2d3e76a

Browse files
Use Boost::json in mock-server-client
Migrating from RapidJSON Relates-To: OCMAM-446 Signed-off-by: Rustam Gamidov <ext-rustam.gamidov@here.com>
1 parent 1d922e9 commit 2d3e76a

File tree

4 files changed

+121
-160
lines changed

4 files changed

+121
-160
lines changed

tests/utils/mock-server-client/Client.h

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020-2024 HERE Europe B.V.
2+
* Copyright (C) 2020-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.
@@ -21,6 +21,7 @@
2121

2222
#include <memory>
2323
#include <string>
24+
#include <utility>
2425
#include <vector>
2526

2627
#include <olp/core/client/OlpClient.h>
@@ -150,17 +151,11 @@ inline void Client::Reset() {
150151

151152
inline bool Client::RemoveMockResponse(const std::string& method_matcher,
152153
const std::string& path_matcher) {
153-
rapidjson::StringBuffer buffer;
154-
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
154+
boost::json::object object;
155+
object.emplace("method", method_matcher);
156+
object.emplace("path", path_matcher);
155157

156-
writer.StartObject();
157-
writer.Key("method");
158-
writer.String(method_matcher.c_str());
159-
writer.Key("path");
160-
writer.String(path_matcher.c_str());
161-
writer.EndObject();
162-
163-
const auto data = std::string{buffer.GetString()};
158+
const auto data = boost::json::serialize(object);
164159

165160
const auto request_body =
166161
std::make_shared<std::vector<unsigned char>>(data.begin(), data.end());
@@ -187,21 +182,16 @@ inline void Client::CreateExpectation(const Expectation& expectation) {
187182

188183
inline bool Client::VerifySequence(
189184
const std::vector<std::string>& pathes) const {
190-
rapidjson::StringBuffer buffer;
191-
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
192-
193-
writer.StartObject();
194-
writer.Key("httpRequests");
195-
writer.StartArray();
185+
boost::json::array array;
196186
for (const auto& str : pathes) {
197-
writer.StartObject();
198-
writer.Key("path");
199-
writer.String(str.c_str());
200-
writer.EndObject();
187+
boost::json::object array_value;
188+
array_value.emplace("path", str);
189+
array.emplace_back(std::move(array_value));
201190
}
202-
writer.EndArray();
203-
writer.EndObject();
204-
const auto data = std::string{buffer.GetString()};
191+
boost::json::object object;
192+
object.emplace("httpRequests", std::move(array));
193+
194+
const auto data = boost::json::serialize(object);
205195

206196
const auto request_body =
207197
std::make_shared<std::vector<unsigned char>>(data.begin(), data.end());
Lines changed: 48 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020-2024 HERE Europe B.V.
2+
* Copyright (C) 2020-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.
@@ -20,9 +20,13 @@
2020
#pragma once
2121

2222
#include <string>
23+
#include <vector>
2324

2425
#include <olp/core/porting/any.h>
2526
#include <olp/core/porting/optional.h>
27+
#include <boost/json/serialize.hpp>
28+
#include <boost/json/value.hpp>
29+
2630
#include "JsonHelpers.h"
2731

2832
namespace mockserver {
@@ -67,110 +71,89 @@ struct Expectation {
6771
olp::porting::optional<ResponseTimes> times = olp::porting::none;
6872
};
6973

70-
void to_json(const Expectation& x, rapidjson::Value& value,
71-
rapidjson::Document::AllocatorType& allocator);
74+
void to_json(const Expectation& x, boost::json::value& value);
7275
void to_json(const Expectation::QueryStringParameter& x,
73-
rapidjson::Value& value,
74-
rapidjson::Document::AllocatorType& allocator);
75-
void to_json(const Expectation::RequestMatcher& x, rapidjson::Value& value,
76-
rapidjson::Document::AllocatorType& allocator);
77-
void to_json(const Expectation::BinaryResponse& x, rapidjson::Value& value,
78-
rapidjson::Document::AllocatorType& allocator);
79-
void to_json(const Expectation::ResponseDelay& x, rapidjson::Value& value,
80-
rapidjson::Document::AllocatorType& allocator);
81-
void to_json(const Expectation::ResponseAction& x, rapidjson::Value& value,
82-
rapidjson::Document::AllocatorType& allocator);
83-
void to_json(const Expectation::ResponseTimes& x, rapidjson::Value& value,
84-
rapidjson::Document::AllocatorType& allocator);
76+
boost::json::value& value);
77+
void to_json(const Expectation::RequestMatcher& x, boost::json::value& value);
78+
void to_json(const Expectation::BinaryResponse& x, boost::json::value& value);
79+
void to_json(const Expectation::ResponseDelay& x, boost::json::value& value);
80+
void to_json(const Expectation::ResponseAction& x, boost::json::value& value);
81+
void to_json(const Expectation::ResponseTimes& x, boost::json::value& value);
8582

8683
std::string serialize(const Expectation& object);
8784

88-
inline void to_json(const Expectation& x, rapidjson::Value& value,
89-
rapidjson::Document::AllocatorType& allocator) {
90-
value.SetObject();
91-
serialize("httpRequest", x.request, value, allocator);
85+
inline void to_json(const Expectation& x, boost::json::value& value) {
86+
value.emplace_object();
87+
serialize("httpRequest", x.request, value);
9288

9389
if (x.action != olp::porting::none) {
94-
serialize("httpResponse", x.action, value, allocator);
90+
serialize("httpResponse", x.action, value);
9591
}
9692
if (x.times != olp::porting::none) {
97-
serialize("times", x.times, value, allocator);
93+
serialize("times", x.times, value);
9894
}
9995
}
10096

10197
inline void to_json(const Expectation::QueryStringParameter& x,
102-
rapidjson::Value& value,
103-
rapidjson::Document::AllocatorType& allocator) {
104-
value.SetObject();
105-
serialize("name", x.name, value, allocator);
106-
serialize("values", x.values, value, allocator);
98+
boost::json::value& value) {
99+
value.emplace_object();
100+
serialize("name", x.name, value);
101+
serialize("values", x.values, value);
107102
}
108103

109104
inline void to_json(const Expectation::RequestMatcher& x,
110-
rapidjson::Value& value,
111-
rapidjson::Document::AllocatorType& allocator) {
112-
value.SetObject();
113-
serialize("path", x.path, value, allocator);
114-
serialize("method", x.method, value, allocator);
115-
serialize("queryStringParameters", x.query_string_parameters, value,
116-
allocator);
105+
boost::json::value& value) {
106+
value.emplace_object();
107+
serialize("path", x.path, value);
108+
serialize("method", x.method, value);
109+
serialize("queryStringParameters", x.query_string_parameters, value);
117110
}
118111

119112
inline void to_json(const Expectation::BinaryResponse& x,
120-
rapidjson::Value& value,
121-
rapidjson::Document::AllocatorType& allocator) {
122-
value.SetObject();
123-
serialize("type", x.type, value, allocator);
124-
serialize("base64Bytes", x.base64_string, value, allocator);
113+
boost::json::value& value) {
114+
value.emplace_object();
115+
serialize("type", x.type, value);
116+
serialize("base64Bytes", x.base64_string, value);
125117
}
126118

127119
inline void to_json(const Expectation::ResponseDelay& x,
128-
rapidjson::Value& value,
129-
rapidjson::Document::AllocatorType& allocator) {
130-
value.SetObject();
131-
serialize("timeUnit", x.time_unit, value, allocator);
132-
serialize("value", x.value, value, allocator);
120+
boost::json::value& value) {
121+
value.emplace_object();
122+
serialize("timeUnit", x.time_unit, value);
123+
serialize("value", x.value, value);
133124
}
134125

135126
inline void to_json(const Expectation::ResponseAction& x,
136-
rapidjson::Value& value,
137-
rapidjson::Document::AllocatorType& allocator) {
138-
value.SetObject();
139-
serialize("statusCode", x.status_code, value, allocator);
127+
boost::json::value& value) {
128+
value.emplace_object();
129+
serialize("statusCode", x.status_code, value);
140130

141131
if (x.body.type() == typeid(std::string)) {
142-
serialize("body", olp::porting::any_cast<std::string>(x.body), value,
143-
allocator);
132+
serialize("body", olp::porting::any_cast<std::string>(x.body), value);
144133
} else if (x.body.type() == typeid(Expectation::BinaryResponse)) {
145134
serialize("body",
146135
olp::porting::any_cast<Expectation::BinaryResponse>(x.body),
147-
value, allocator);
136+
value);
148137
}
149138

150139
if (x.delay) {
151-
serialize("delay", *x.delay, value, allocator);
140+
serialize("delay", *x.delay, value);
152141
}
153142
}
154143

155144
inline void to_json(const Expectation::ResponseTimes& x,
156-
rapidjson::Value& value,
157-
rapidjson::Document::AllocatorType& allocator) {
158-
value.SetObject();
159-
serialize("remainingTimes", x.remaining_times, value, allocator);
160-
serialize("unlimited", x.unlimited, value, allocator);
145+
boost::json::value& value) {
146+
value.emplace_object();
147+
serialize("remainingTimes", x.remaining_times, value);
148+
serialize("unlimited", x.unlimited, value);
161149
}
162150

163151
inline std::string serialize(const Expectation& object) {
164-
rapidjson::Document doc;
165-
auto& allocator = doc.GetAllocator();
166-
167-
doc.SetObject();
168-
to_json(object, doc, allocator);
152+
boost::json::value doc;
153+
doc.emplace_object();
154+
to_json(object, doc);
169155

170-
rapidjson::StringBuffer buffer;
171-
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
172-
doc.Accept(writer);
173-
return buffer.GetString();
156+
return boost::json::serialize(doc);
174157
};
175158

176159
} // namespace mockserver

0 commit comments

Comments
 (0)