Skip to content

Commit aeb9aae

Browse files
authored
Add integration tests for StreamApi::Subscribe method (#609)
Resolves: OLPEDGE-1471 Signed-off-by: Mykola Malik <[email protected]>
1 parent c72b7e6 commit aeb9aae

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed

olp-cpp-sdk-dataservice-read/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ set(OLP_SDK_DATASERVICE_READ_TEST_SOURCES
2323
ParserTest.cpp
2424
PartitionsRepositoryTest.cpp
2525
SerializerTest.cpp
26+
StreamApiTest.cpp
2627
StreamLayerClientImplTest.cpp
2728
VersionedLayerClientTest.cpp
2829
VolatileLayerClientImplTest.cpp
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*
2+
* Copyright (C) 2020 HERE Europe B.V.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
#include <gmock/gmock.h>
21+
#include <matchers/NetworkUrlMatchers.h>
22+
#include <mocks/NetworkMock.h>
23+
#include <olp/core/client/OlpClient.h>
24+
#include <olp/core/client/OlpClientSettingsFactory.h>
25+
#include "generated/api/StreamApi.h"
26+
27+
namespace {
28+
using namespace ::testing;
29+
using namespace olp;
30+
using namespace olp::client;
31+
using namespace olp::dataservice::read;
32+
using namespace olp::tests::common;
33+
34+
std::string ApiErrorToString(const ApiError& error) {
35+
std::ostringstream result_stream;
36+
result_stream << "ERROR: code: " << static_cast<int>(error.GetErrorCode())
37+
<< ", status: " << error.GetHttpStatusCode()
38+
<< ", message: " << error.GetMessage();
39+
return result_stream.str();
40+
}
41+
42+
MATCHER_P(BodyEq, expected_body, "") {
43+
std::string expected_body_str(expected_body);
44+
45+
return arg.GetBody()
46+
? std::equal(expected_body_str.begin(), expected_body_str.end(),
47+
arg.GetBody()->begin())
48+
: expected_body_str.empty();
49+
}
50+
51+
class StreamApiTest : public Test {
52+
protected:
53+
void SetUp() override {
54+
network_mock_ = std::make_shared<NetworkMock>();
55+
56+
OlpClientSettings settings;
57+
settings.network_request_handler = network_mock_;
58+
settings.task_scheduler =
59+
OlpClientSettingsFactory::CreateDefaultTaskScheduler(1);
60+
olp_client_.SetSettings(std::move(settings));
61+
}
62+
63+
void TearDown() override { network_mock_.reset(); }
64+
65+
protected:
66+
OlpClient olp_client_;
67+
std::shared_ptr<NetworkMock> network_mock_;
68+
};
69+
70+
const std::string kBaseUrl{
71+
"https://some.base.url/stream/v2/catalogs/"
72+
"hrn:here:data::olp-here-test:hereos-internal-test-v2"};
73+
const std::string kNodeBaseUrl{
74+
"https://some.node.base.url/stream/v2/catalogs/"
75+
"hrn:here:data::olp-here-test:hereos-internal-test-v2"};
76+
const std::string kSubscriptionId{"test-subscription-id-123"};
77+
const std::string kConsumerId{"test-consumer-id-987"};
78+
const std::string kLayerId{"test-layer"};
79+
const std::string kSerialMode{"serial"};
80+
81+
constexpr auto kUrlStreamSubscribeNoQueryParams =
82+
R"(https://some.base.url/stream/v2/catalogs/hrn:here:data::olp-here-test:hereos-internal-test-v2/layers/test-layer/subscribe)";
83+
84+
constexpr auto kUrlStreamSubscribeWithQueryParams =
85+
R"(https://some.base.url/stream/v2/catalogs/hrn:here:data::olp-here-test:hereos-internal-test-v2/layers/test-layer/subscribe?consumerId=test-consumer-id-987&mode=serial&subscriptionId=test-subscription-id-123)";
86+
87+
constexpr auto kHttpResponseSubscribeSucceeds =
88+
R"jsonString({ "nodeBaseURL": "https://some.node.base.url/stream/v2/catalogs/hrn:here:data::olp-here-test:hereos-internal-test-v2", "subscriptionId": "test-subscription-id-123" })jsonString";
89+
90+
constexpr auto kHttpResponseSubscribeFails =
91+
R"jsonString({ "title": "Subscription mode not supported", "status": 400, "code": "E213002", "cause": "Subscription mode 'singleton' not supported", "action": "Retry with valid subscription mode 'serial' or 'parallel'", "correlationId": "4199533b-6290-41db-8d79-edf4f4019a74" })jsonString";
92+
93+
constexpr auto kHttpRequestBodyWithConsumerProperties =
94+
R"jsonString({"kafkaConsumerProperties":{"field_string":"abc","field_int":"456","field_bool":"1"}})jsonString";
95+
96+
TEST_F(StreamApiTest, Subscribe) {
97+
{
98+
SCOPED_TRACE("Subscribe without optional input fields succeeds");
99+
100+
EXPECT_CALL(
101+
*network_mock_,
102+
Send(IsPostRequest(kUrlStreamSubscribeNoQueryParams), _, _, _, _))
103+
.WillOnce(ReturnHttpResponse(
104+
http::NetworkResponse().WithStatus(http::HttpStatusCode::CREATED),
105+
kHttpResponseSubscribeSucceeds));
106+
107+
olp_client_.SetBaseUrl(kBaseUrl);
108+
std::string x_correlation_id;
109+
CancellationContext context;
110+
const auto subscribe_response = StreamApi::Subscribe(
111+
olp_client_, kLayerId, boost::none, boost::none, boost::none,
112+
boost::none, context, x_correlation_id);
113+
114+
EXPECT_TRUE(subscribe_response.IsSuccessful())
115+
<< ApiErrorToString(subscribe_response.GetError());
116+
EXPECT_EQ(subscribe_response.GetResult().GetNodeBaseURL(), kNodeBaseUrl);
117+
EXPECT_EQ(subscribe_response.GetResult().GetSubscriptionId(),
118+
kSubscriptionId);
119+
120+
Mock::VerifyAndClearExpectations(network_mock_.get());
121+
}
122+
{
123+
SCOPED_TRACE("Subscribe with all optional input fields succeeds");
124+
125+
EXPECT_CALL(*network_mock_,
126+
Send(AllOf(IsPostRequest(kUrlStreamSubscribeWithQueryParams),
127+
BodyEq(kHttpRequestBodyWithConsumerProperties)),
128+
_, _, _, _))
129+
.WillOnce(ReturnHttpResponse(
130+
http::NetworkResponse().WithStatus(http::HttpStatusCode::CREATED),
131+
kHttpResponseSubscribeSucceeds));
132+
133+
ConsumerProperties subscription_properties{
134+
ConsumerOption("field_string", "abc"),
135+
ConsumerOption("field_int", 456),
136+
ConsumerOption("field_bool", true),
137+
};
138+
139+
olp_client_.SetBaseUrl(kBaseUrl);
140+
std::string x_correlation_id;
141+
CancellationContext context;
142+
const auto subscribe_response = StreamApi::Subscribe(
143+
olp_client_, kLayerId, kSubscriptionId, kSerialMode, kConsumerId,
144+
subscription_properties, context, x_correlation_id);
145+
146+
EXPECT_TRUE(subscribe_response.IsSuccessful())
147+
<< ApiErrorToString(subscribe_response.GetError());
148+
EXPECT_EQ(subscribe_response.GetResult().GetNodeBaseURL(), kNodeBaseUrl);
149+
EXPECT_EQ(subscribe_response.GetResult().GetSubscriptionId(),
150+
kSubscriptionId);
151+
152+
Mock::VerifyAndClearExpectations(network_mock_.get());
153+
}
154+
{
155+
SCOPED_TRACE("Subscribe fails");
156+
157+
EXPECT_CALL(
158+
*network_mock_,
159+
Send(IsPostRequest(kUrlStreamSubscribeNoQueryParams), _, _, _, _))
160+
.WillOnce(ReturnHttpResponse(
161+
http::NetworkResponse().WithStatus(http::HttpStatusCode::FORBIDDEN),
162+
kHttpResponseSubscribeFails));
163+
164+
olp_client_.SetBaseUrl(kBaseUrl);
165+
std::string x_correlation_id;
166+
CancellationContext context;
167+
const auto subscribe_response = StreamApi::Subscribe(
168+
olp_client_, kLayerId, boost::none, boost::none, boost::none,
169+
boost::none, context, x_correlation_id);
170+
171+
EXPECT_FALSE(subscribe_response.IsSuccessful());
172+
EXPECT_EQ(subscribe_response.GetError().GetHttpStatusCode(),
173+
http::HttpStatusCode::FORBIDDEN);
174+
EXPECT_EQ(subscribe_response.GetError().GetMessage(),
175+
kHttpResponseSubscribeFails);
176+
177+
Mock::VerifyAndClearExpectations(network_mock_.get());
178+
}
179+
}
180+
181+
} // namespace

0 commit comments

Comments
 (0)