Skip to content

Commit 232bc9d

Browse files
author
Kirill Zhuchkov
authored
Split declarations and definitions. (#711)
This fixes the "already defined" compilation error. Also improves the code readability a bit, as the code delcaring classes and structs becames slimer. Relates-To: OAM-188 Signed-off-by: Kirill Zhuchkov <[email protected]>
1 parent 58af2d1 commit 232bc9d

File tree

3 files changed

+128
-67
lines changed

3 files changed

+128
-67
lines changed

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

Lines changed: 84 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -41,80 +41,106 @@ const auto kTimeout = std::chrono::seconds{10};
4141

4242
class Client {
4343
public:
44-
explicit Client(olp::client::OlpClientSettings settings) {
45-
http_client_ = olp::client::OlpClientFactory::Create(settings);
46-
http_client_->SetBaseUrl(kBaseUrl);
47-
}
44+
explicit Client(olp::client::OlpClientSettings settings);
4845

4946
void MockResponse(const std::string& method_matcher,
5047
const std::string& path_matcher,
51-
const std::string& response_body) {
52-
auto expectation = Expectation{};
53-
expectation.request.path = path_matcher;
54-
expectation.request.method = method_matcher;
55-
56-
boost::optional<Expectation::ResponseAction> action =
57-
Expectation::ResponseAction{};
58-
action->body = response_body;
59-
expectation.action = action;
60-
61-
CreateExpectation(expectation);
62-
}
48+
const std::string& response_body);
6349

6450
void MockBinaryResponse(const std::string& method_matcher,
6551
const std::string& path_matcher,
66-
const std::string& response_body) {
67-
auto expectation = Expectation{};
68-
expectation.request.path = path_matcher;
69-
expectation.request.method = method_matcher;
52+
const std::string& response_body);
7053

71-
auto binary_response = Expectation::BinaryResponse{};
72-
binary_response.base64_string = response_body;
54+
Status::Ports Ports() const;
7355

74-
boost::optional<Expectation::ResponseAction> action =
75-
Expectation::ResponseAction{};
76-
action->body = binary_response;
77-
expectation.action = action;
56+
void Reset();
7857

79-
CreateExpectation(expectation);
80-
}
81-
82-
std::vector<int32_t> Ports() const {
83-
auto response =
84-
http_client_->CallApi(kStatusPath, "PUT", {}, {}, {}, nullptr, "",
85-
olp::client::CancellationContext{});
86-
87-
if (response.status != olp::http::HttpStatusCode::OK) {
88-
return {};
89-
}
58+
private:
59+
void CreateExpectation(const Expectation& expectation);
9060

91-
const auto status = olp::parser::parse<Status>(response.response);
61+
private:
62+
std::shared_ptr<olp::client::OlpClient> http_client_;
63+
};
9264

93-
return status.ports;
65+
inline Client::Client(olp::client::OlpClientSettings settings) {
66+
http_client_ = olp::client::OlpClientFactory::Create(settings);
67+
http_client_->SetBaseUrl(kBaseUrl);
68+
}
69+
70+
inline void Client::MockResponse(const std::string& method_matcher,
71+
const std::string& path_matcher,
72+
const std::string& response_body) {
73+
auto expectation = Expectation{};
74+
expectation.request.path = path_matcher;
75+
expectation.request.method = method_matcher;
76+
77+
boost::optional<Expectation::ResponseAction> action =
78+
Expectation::ResponseAction{};
79+
action->body = response_body;
80+
expectation.action = action;
81+
82+
boost::optional<Expectation::ResponseTimes> times =
83+
Expectation::ResponseTimes{};
84+
times->remaining_times = 1;
85+
times->unlimited = false;
86+
expectation.times = times;
87+
88+
CreateExpectation(expectation);
89+
}
90+
91+
inline void Client::MockBinaryResponse(const std::string& method_matcher,
92+
const std::string& path_matcher,
93+
const std::string& response_body) {
94+
auto expectation = Expectation{};
95+
expectation.request.path = path_matcher;
96+
expectation.request.method = method_matcher;
97+
98+
auto binary_response = Expectation::BinaryResponse{};
99+
binary_response.base64_string = response_body;
100+
101+
boost::optional<Expectation::ResponseAction> action =
102+
Expectation::ResponseAction{};
103+
action->body = binary_response;
104+
expectation.action = action;
105+
106+
boost::optional<Expectation::ResponseTimes> times =
107+
Expectation::ResponseTimes{};
108+
times->remaining_times = 1;
109+
times->unlimited = false;
110+
expectation.times = times;
111+
112+
CreateExpectation(expectation);
113+
}
114+
115+
inline Status::Ports Client::Ports() const {
116+
auto response = http_client_->CallApi(kStatusPath, "PUT", {}, {}, {}, nullptr,
117+
"", olp::client::CancellationContext{});
118+
119+
if (response.status != olp::http::HttpStatusCode::OK) {
120+
return {};
94121
}
95122

96-
void Reset() {
97-
auto response =
98-
http_client_->CallApi(kResetPath, "PUT", {}, {}, {}, nullptr, "",
99-
olp::client::CancellationContext{});
123+
const auto status = olp::parser::parse<Status>(response.response);
124+
return status.ports;
125+
}
100126

101-
return;
102-
}
127+
inline void Client::Reset() {
128+
auto response = http_client_->CallApi(kResetPath, "PUT", {}, {}, {}, nullptr,
129+
"", olp::client::CancellationContext{});
103130

104-
private:
105-
void CreateExpectation(const Expectation& expectation) {
106-
const auto data = serialize(expectation);
107-
const std::shared_ptr<std::vector<unsigned char>> request_body =
108-
std::make_shared<std::vector<unsigned char>>(data.begin(), data.end());
131+
return;
132+
}
109133

110-
auto response =
111-
http_client_->CallApi(kExpectationPath, "PUT", {}, {}, {}, request_body,
112-
"", olp::client::CancellationContext{});
134+
inline void Client::CreateExpectation(const Expectation& expectation) {
135+
const auto data = serialize(expectation);
136+
const std::shared_ptr<std::vector<unsigned char>> request_body =
137+
std::make_shared<std::vector<unsigned char>>(data.begin(), data.end());
113138

114-
return;
115-
}
139+
auto response =
140+
http_client_->CallApi(kExpectationPath, "PUT", {}, {}, {}, request_body,
141+
"", olp::client::CancellationContext{});
142+
143+
return;
144+
}
116145

117-
private:
118-
std::shared_ptr<olp::client::OlpClient> http_client_;
119-
};
120146
} // namespace mockserver

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

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,36 +48,61 @@ struct Expectation {
4848
std::string base64_string;
4949
};
5050

51+
struct ResponseTimes {
52+
int64_t remaining_times = 1;
53+
bool unlimited = false;
54+
};
55+
5156
RequestMatcher request;
5257
boost::optional<ResponseAction> action = boost::none;
58+
boost::optional<ResponseTimes> times = boost::none;
5359
};
5460

5561
void to_json(const Expectation& x, rapidjson::Value& value,
56-
rapidjson::Document::AllocatorType& allocator) {
62+
rapidjson::Document::AllocatorType& allocator);
63+
void to_json(const Expectation::RequestMatcher& x, rapidjson::Value& value,
64+
rapidjson::Document::AllocatorType& allocator);
65+
void to_json(const Expectation::BinaryResponse& x, rapidjson::Value& value,
66+
rapidjson::Document::AllocatorType& allocator);
67+
void to_json(const Expectation::ResponseAction& x, rapidjson::Value& value,
68+
rapidjson::Document::AllocatorType& allocator);
69+
void to_json(const Expectation::ResponseTimes& x, rapidjson::Value& value,
70+
rapidjson::Document::AllocatorType& allocator);
71+
72+
std::string serialize(const Expectation& object);
73+
74+
inline void to_json(const Expectation& x, rapidjson::Value& value,
75+
rapidjson::Document::AllocatorType& allocator) {
5776
value.SetObject();
5877
olp::serializer::serialize("httpRequest", x.request, value, allocator);
5978

6079
if (x.action != boost::none) {
6180
olp::serializer::serialize("httpResponse", x.action, value, allocator);
6281
}
82+
if (x.times != boost::none) {
83+
olp::serializer::serialize("times", x.times, value, allocator);
84+
}
6385
}
6486

65-
void to_json(const Expectation::RequestMatcher& x, rapidjson::Value& value,
66-
rapidjson::Document::AllocatorType& allocator) {
87+
inline void to_json(const Expectation::RequestMatcher& x,
88+
rapidjson::Value& value,
89+
rapidjson::Document::AllocatorType& allocator) {
6790
value.SetObject();
6891
olp::serializer::serialize("path", x.path, value, allocator);
6992
olp::serializer::serialize("method", x.method, value, allocator);
7093
}
7194

72-
void to_json(const Expectation::BinaryResponse& x, rapidjson::Value& value,
73-
rapidjson::Document::AllocatorType& allocator) {
95+
inline void to_json(const Expectation::BinaryResponse& x,
96+
rapidjson::Value& value,
97+
rapidjson::Document::AllocatorType& allocator) {
7498
value.SetObject();
7599
olp::serializer::serialize("type", x.type, value, allocator);
76100
olp::serializer::serialize("base64Bytes", x.base64_string, value, allocator);
77101
}
78102

79-
void to_json(const Expectation::ResponseAction& x, rapidjson::Value& value,
80-
rapidjson::Document::AllocatorType& allocator) {
103+
inline void to_json(const Expectation::ResponseAction& x,
104+
rapidjson::Value& value,
105+
rapidjson::Document::AllocatorType& allocator) {
81106
value.SetObject();
82107
olp::serializer::serialize("statusCode", x.status_code, value, allocator);
83108

@@ -91,6 +116,15 @@ void to_json(const Expectation::ResponseAction& x, rapidjson::Value& value,
91116
}
92117
}
93118

119+
inline void to_json(const Expectation::ResponseTimes& x,
120+
rapidjson::Value& value,
121+
rapidjson::Document::AllocatorType& allocator) {
122+
value.SetObject();
123+
olp::serializer::serialize("remainingTimes", x.remaining_times, value,
124+
allocator);
125+
olp::serializer::serialize("unlimited", x.unlimited, value, allocator);
126+
}
127+
94128
inline std::string serialize(const Expectation& object) {
95129
rapidjson::Document doc;
96130
auto& allocator = doc.GetAllocator();

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ struct Status {
3232
Ports ports;
3333
};
3434

35-
void from_json(const rapidjson::Value& value, Status& x){
35+
void from_json(const rapidjson::Value& value, Status& x);
36+
37+
inline void from_json(const rapidjson::Value& value, Status& x) {
3638
x.ports = olp::parser::parse<Status::Ports>(value, "ports");
3739
}
3840

39-
4041
} // namespace mockserver

0 commit comments

Comments
 (0)