Skip to content

Commit 132a778

Browse files
author
Liubov Didkivska
authored
Add metadata api to get versions list (#895)
Implement MetadataApi function. Relates-To: OLPEDGE-1606 Signed-off-by: Liubov Didkivska <ext-liubov.didkivska@here.com>
1 parent 4aadeb3 commit 132a778

File tree

4 files changed

+176
-10
lines changed

4 files changed

+176
-10
lines changed

olp-cpp-sdk-dataservice-read/src/generated/api/MetadataApi.cpp

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "generated/parser/LayerVersionsParser.h"
3131
#include "generated/parser/PartitionsParser.h"
3232
#include "generated/parser/VersionResponseParser.h"
33+
#include "generated/parser/VersionInfosParser.h"
3334
#include <olp/core/generated/parser/JsonParser.h>
3435
// clang-format on
3536

@@ -53,10 +54,9 @@ std::string concatStringArray(const std::vector<std::string>& strings,
5354
namespace olp {
5455
namespace dataservice {
5556
namespace read {
56-
using namespace olp::client;
5757

5858
MetadataApi::LayerVersionsResponse MetadataApi::GetLayerVersions(
59-
const OlpClient& client, int64_t version,
59+
const client::OlpClient& client, std::int64_t version,
6060
boost::optional<std::string> billing_tag,
6161
const client::CancellationContext& context) {
6262
std::multimap<std::string, std::string> header_params;
@@ -74,16 +74,16 @@ MetadataApi::LayerVersionsResponse MetadataApi::GetLayerVersions(
7474
header_params, {}, nullptr, "", context);
7575

7676
if (api_response.status != http::HttpStatusCode::OK) {
77-
return ApiError(api_response.status, api_response.response.str());
77+
return client::ApiError(api_response.status, api_response.response.str());
7878
}
7979

8080
return LayerVersionsResponse(
8181
olp::parser::parse<model::LayerVersions>(api_response.response));
8282
}
8383

8484
MetadataApi::PartitionsResponse MetadataApi::GetPartitions(
85-
const OlpClient& client, const std::string& layer_id,
86-
boost::optional<int64_t> version,
85+
const client::OlpClient& client, const std::string& layer_id,
86+
boost::optional<std::int64_t> version,
8787
const std::vector<std::string>& additional_fields,
8888
boost::optional<std::string> range,
8989
boost::optional<std::string> billing_tag,
@@ -112,15 +112,15 @@ MetadataApi::PartitionsResponse MetadataApi::GetPartitions(
112112
header_params, {}, nullptr, "", context);
113113

114114
if (api_response.status != http::HttpStatusCode::OK) {
115-
return ApiError(api_response.status, api_response.response.str());
115+
return {{api_response.status, api_response.response.str()}};
116116
}
117117

118118
return PartitionsResponse(
119119
olp::parser::parse<model::Partitions>(api_response.response));
120120
}
121121

122122
MetadataApi::CatalogVersionResponse MetadataApi::GetLatestCatalogVersion(
123-
const OlpClient& client, int64_t startVersion,
123+
const client::OlpClient& client, std::int64_t startVersion,
124124
boost::optional<std::string> billing_tag,
125125
const client::CancellationContext& context) {
126126
std::multimap<std::string, std::string> header_params;
@@ -138,12 +138,38 @@ MetadataApi::CatalogVersionResponse MetadataApi::GetLatestCatalogVersion(
138138
header_params, {}, nullptr, "", context);
139139

140140
if (api_response.status != http::HttpStatusCode::OK) {
141-
return ApiError(api_response.status, api_response.response.str());
141+
return {{api_response.status, api_response.response.str()}};
142142
}
143143
return CatalogVersionResponse(
144144
olp::parser::parse<model::VersionResponse>(api_response.response));
145145
}
146146

147+
MetadataApi::VersionsResponse MetadataApi::ListVersions(
148+
const client::OlpClient& client, std::int64_t start_version,
149+
std::int64_t end_version, boost::optional<std::string> billing_tag,
150+
const client::CancellationContext& context) {
151+
std::multimap<std::string, std::string> header_params;
152+
header_params.emplace("Accept", "application/json");
153+
154+
std::multimap<std::string, std::string> query_params;
155+
query_params.emplace("startVersion", std::to_string(start_version));
156+
query_params.emplace("endVersion", std::to_string(end_version));
157+
if (billing_tag) {
158+
query_params.emplace("billingTag", *billing_tag);
159+
}
160+
161+
std::string metadata_uri = "/versions";
162+
163+
auto api_response = client.CallApi(metadata_uri, "GET", query_params,
164+
header_params, {}, nullptr, "", context);
165+
166+
if (api_response.status != http::HttpStatusCode::OK) {
167+
return {{api_response.status, api_response.response.str()}};
168+
}
169+
return VersionsResponse(
170+
olp::parser::parse<model::VersionInfos>(api_response.response));
171+
}
172+
147173
} // namespace read
148174

149175
} // namespace dataservice

olp-cpp-sdk-dataservice-read/src/generated/api/MetadataApi.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@
2626
#include <olp/core/client/ApiResponse.h>
2727
#include <boost/optional.hpp>
2828
#include "generated/model/LayerVersions.h"
29-
#include "olp/dataservice/read/model/VersionResponse.h"
3029
#include "olp/dataservice/read/model/Partitions.h"
30+
#include "olp/dataservice/read/model/VersionInfos.h"
31+
#include "olp/dataservice/read/model/VersionResponse.h"
3132

3233
namespace olp {
3334
namespace client {
3435
class OlpClient;
3536
class CancellationContext;
36-
}
37+
} // namespace client
3738

3839
namespace dataservice {
3940
namespace read {
@@ -42,6 +43,8 @@ namespace read {
4243
*/
4344
class MetadataApi {
4445
public:
46+
using VersionsResponse =
47+
client::ApiResponse<model::VersionInfos, client::ApiError>;
4548
using PartitionsResponse =
4649
client::ApiResponse<model::Partitions, client::ApiError>;
4750

@@ -116,6 +119,11 @@ class MetadataApi {
116119
const client::OlpClient& client, int64_t start_version,
117120
boost::optional<std::string> billing_tag,
118121
const client::CancellationContext& context);
122+
123+
static VersionsResponse ListVersions(
124+
const client::OlpClient& client, int64_t start_version,
125+
int64_t end_version, boost::optional<std::string> billing_tag,
126+
const client::CancellationContext& context);
119127
};
120128

121129
} // namespace read

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ set(OLP_SDK_DATASERVICE_READ_TEST_SOURCES
2222
CatalogRepositoryTest.cpp
2323
DataCacheRepositoryTest.cpp
2424
DataRepositoryTest.cpp
25+
MetadataApiTest.cpp
2526
ParserTest.cpp
2627
PartitionsCacheRepositoryTest.cpp
2728
PartitionsRepositoryTest.cpp
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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-matchers.h>
21+
#include <gmock/gmock.h>
22+
#include <matchers/NetworkUrlMatchers.h>
23+
#include <mocks/NetworkMock.h>
24+
#include <olp/core/client/OlpClient.h>
25+
#include <olp/core/client/OlpClientFactory.h>
26+
#include <olp/core/client/OlpClientSettingsFactory.h>
27+
#include "generated/api/MetadataApi.h"
28+
29+
namespace {
30+
31+
constexpr auto kStartVersion = 299;
32+
constexpr auto kEndVersion = 300;
33+
constexpr auto kNodeBaseUrl =
34+
"https://some.node.base.url/metadata/v1/catalogs/"
35+
"hrn:here:data::olp-here-test:hereos-internal-test-v2";
36+
37+
constexpr auto kUrlVersionsList =
38+
R"(https://some.node.base.url/metadata/v1/catalogs/hrn:here:data::olp-here-test:hereos-internal-test-v2/versions?endVersion=300&startVersion=299)";
39+
40+
constexpr auto kUrlVersionsListBillingTag =
41+
R"(https://some.node.base.url/metadata/v1/catalogs/hrn:here:data::olp-here-test:hereos-internal-test-v2/versions?billingTag=OlpCppSdkTest&endVersion=300&startVersion=299)";
42+
43+
constexpr auto kHttpResponse =
44+
R"jsonString({"versions":[{"version":4,"timestamp":1547159598712,"partitionCounts":{"testlayer":5,"testlayer_res":1,"multilevel_testlayer":33, "hype-test-prefetch-2":7,"testlayer_gzip":1,"hype-test-prefetch":7},"dependencies":[ { "hrn":"hrn:here:data::olp-here-test:hereos-internal-test-v2","version":0,"direct":false},{"hrn":"hrn:here:data:::hereos-internal-test-v2","version":0,"direct":false }]}]})jsonString";
45+
46+
using ::testing::_;
47+
namespace http = olp::http;
48+
namespace client = olp::client;
49+
namespace common = olp::tests::common;
50+
51+
class MetadataApiTest : public testing::Test {
52+
protected:
53+
void SetUp() override {
54+
network_mock_ = std::make_shared<common::NetworkMock>();
55+
56+
settings_ = std::make_shared<client::OlpClientSettings>();
57+
settings_->network_request_handler = network_mock_;
58+
59+
client_ = client::OlpClientFactory::Create(*settings_);
60+
client_->SetBaseUrl(kNodeBaseUrl);
61+
}
62+
void TearDown() override { network_mock_.reset(); }
63+
64+
protected:
65+
std::shared_ptr<client::OlpClientSettings> settings_;
66+
std::shared_ptr<client::OlpClient> client_;
67+
std::shared_ptr<common::NetworkMock> network_mock_;
68+
};
69+
70+
TEST_F(MetadataApiTest, GetListVersions) {
71+
{
72+
SCOPED_TRACE("Request metadata versions.");
73+
EXPECT_CALL(*network_mock_,
74+
Send(testing::AllOf(common::IsGetRequest(kUrlVersionsList)), _,
75+
_, _, _))
76+
.WillOnce(common::ReturnHttpResponse(
77+
http::NetworkResponse().WithStatus(http::HttpStatusCode::OK),
78+
kHttpResponse));
79+
80+
auto index_response = olp::dataservice::read::MetadataApi::ListVersions(
81+
*client_, kStartVersion, kEndVersion, boost::none,
82+
olp::client::CancellationContext{});
83+
84+
ASSERT_TRUE(index_response.IsSuccessful());
85+
auto result = index_response.GetResult();
86+
87+
ASSERT_EQ(1u, result.GetVersions().size());
88+
ASSERT_EQ(4, result.GetVersions().front().GetVersion());
89+
ASSERT_EQ(2u, result.GetVersions().front().GetDependencies().size());
90+
ASSERT_EQ(6u, result.GetVersions().front().GetPartitionCounts().size());
91+
}
92+
93+
{
94+
SCOPED_TRACE("Request with billing tag.");
95+
std::string billing_tag = "OlpCppSdkTest";
96+
EXPECT_CALL(
97+
*network_mock_,
98+
Send(testing::AllOf(common::IsGetRequest(kUrlVersionsListBillingTag)),
99+
_, _, _, _))
100+
.WillOnce(common::ReturnHttpResponse(
101+
http::NetworkResponse().WithStatus(http::HttpStatusCode::OK),
102+
kHttpResponse));
103+
104+
auto index_response = olp::dataservice::read::MetadataApi::ListVersions(
105+
*client_, kStartVersion, kEndVersion, billing_tag,
106+
olp::client::CancellationContext{});
107+
108+
ASSERT_TRUE(index_response.IsSuccessful());
109+
auto result = index_response.GetResult();
110+
111+
ASSERT_EQ(1u, result.GetVersions().size());
112+
ASSERT_EQ(4, result.GetVersions().front().GetVersion());
113+
ASSERT_EQ(2u, result.GetVersions().front().GetDependencies().size());
114+
ASSERT_EQ(6u, result.GetVersions().front().GetPartitionCounts().size());
115+
}
116+
{
117+
SCOPED_TRACE("Cancelled CancellationContext");
118+
119+
auto context = olp::client::CancellationContext();
120+
context.CancelOperation();
121+
122+
auto index_response = olp::dataservice::read::MetadataApi::ListVersions(
123+
*client_, kStartVersion, kEndVersion, boost::none, context);
124+
125+
ASSERT_FALSE(index_response.IsSuccessful());
126+
auto error = index_response.GetError();
127+
ASSERT_EQ(olp::client::ErrorCode::Cancelled, error.GetErrorCode());
128+
}
129+
}
130+
131+
} // namespace

0 commit comments

Comments
 (0)