Skip to content

Commit badf1af

Browse files
author
Liubov Didkivska
authored
Add responses generator to test-common lib. (#1082)
Integration tests should have dependency only from public dataservice api. Move generating urls and responses to test-common lib. Relates-To: OLPEDGE-1901 Signed-off-by: Liubov Didkivska <[email protected]>
1 parent 0adef45 commit badf1af

File tree

8 files changed

+267
-152
lines changed

8 files changed

+267
-152
lines changed

olp-cpp-sdk-dataservice-read/tests/VersionedLayerClientImplTest.cpp

Lines changed: 34 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
#include <olp/core/utils/Dir.h>
3030
#include <olp/dataservice/read/VersionedLayerClient.h>
3131
#include "ApiDefaultResponses.h"
32+
#include "PlatformUrlsGenerator.h"
3233
#include "ReadDefaultResponses.h"
33-
#include "UrlGenerators.h"
34+
#include "ResponseGenerator.h"
3435
#include "VersionedLayerClientImpl.h"
3536
#include "repositories/QuadTreeIndex.h"
3637
// clang-format off
@@ -60,17 +61,6 @@ constexpr auto kOtherHereTile2 = "5904591";
6061
constexpr auto kUrlLookup =
6162
R"(https://api-lookup.data.api.platform.here.com/lookup/v1/resources/hrn:here:data::olp-here-test:hereos-internal-test-v2/apis)";
6263

63-
template <class T>
64-
std::string serialize(std::vector<T> data) {
65-
std::string str = "[";
66-
for (const auto& el : data) {
67-
str.append(olp::serializer::serialize(el));
68-
str.append(",");
69-
}
70-
str[str.length() - 1] = ']';
71-
return str;
72-
}
73-
7464
TEST(VersionedLayerClientTest, CanBeMoved) {
7565
read::VersionedLayerClient client_a(olp::client::HRN(), "", boost::none, {});
7666
read::VersionedLayerClient client_b(std::move(client_a));
@@ -363,33 +353,22 @@ TEST(VersionedLayerClientTest, ProtectThanRelease) {
363353
settings.default_cache_expiration = std::chrono::seconds(2);
364354
settings.network_request_handler = network_mock;
365355
auto version = 4u;
366-
auto api_response =
367-
mockserver::ApiDefaultResponses::GenerateResourceApisResponse(kCatalog);
368-
auto quad_path = mock::GeneratePath(
369-
api_response, "query",
370-
mock::GenerateGetQuadKeyPath("92259", kLayerId, version, 4));
356+
auto api_response = ResponseGenerator::ResourceApis(kCatalog);
357+
PlatformUrlsGenerator generator(api_response, kLayerId);
358+
auto quad_path = generator.VersionedQuadTree("92259", version, 4);
371359
ASSERT_FALSE(quad_path.empty());
372360
auto tile_key = olp::geo::TileKey::FromHereTile(kHereTile);
373361
auto responce_quad =
374362
mockserver::ReadDefaultResponses::GenerateQuadTreeResponse(
375363
tile_key.ChangedLevelBy(-4), 4, {9, 10, 11, 12});
376-
auto tile_path = mock::GeneratePath(
377-
api_response, "blob",
378-
mock::GenerateGetDataPath(
379-
kLayerId,
380-
mockserver::ReadDefaultResponses::GenerateDataHandle(kHereTile)));
364+
auto tile_path = generator.DataBlob(
365+
mockserver::ReadDefaultResponses::GenerateDataHandle(kHereTile));
381366
ASSERT_FALSE(tile_path.empty());
382-
auto tile2_path = mock::GeneratePath(
383-
api_response, "blob",
384-
mock::GenerateGetDataPath(
385-
kLayerId, mockserver::ReadDefaultResponses::GenerateDataHandle(
386-
kOtherHereTile2)));
367+
auto tile2_path = generator.DataBlob(
368+
mockserver::ReadDefaultResponses::GenerateDataHandle(kOtherHereTile2));
387369
ASSERT_FALSE(tile2_path.empty());
388-
auto other_tile_path = mock::GeneratePath(
389-
api_response, "blob",
390-
mock::GenerateGetDataPath(
391-
kLayerId, mockserver::ReadDefaultResponses::GenerateDataHandle(
392-
kOtherHereTile)));
370+
auto other_tile_path = generator.DataBlob(
371+
mockserver::ReadDefaultResponses::GenerateDataHandle(kOtherHereTile));
393372
ASSERT_FALSE(other_tile_path.empty());
394373

395374
read::VersionedLayerClientImpl client(kHrn, kLayerId, boost::none, settings);
@@ -399,9 +378,8 @@ TEST(VersionedLayerClientTest, ProtectThanRelease) {
399378
EXPECT_CALL(*network_mock, Send(IsGetRequest(kUrlLookup), _, _, _, _))
400379
.WillOnce(ReturnHttpResponse(olp::http::NetworkResponse().WithStatus(
401380
olp::http::HttpStatusCode::OK),
402-
serialize(api_response)));
403-
auto version_path = mock::GeneratePath(
404-
api_response, "metadata", mock::GenerateGetLatestVersionPath());
381+
api_response));
382+
auto version_path = generator.LatestVersion();
405383
ASSERT_FALSE(version_path.empty());
406384
EXPECT_CALL(*network_mock, Send(IsGetRequest(version_path), _, _, _, _))
407385
.WillOnce(ReturnHttpResponse(
@@ -574,8 +552,8 @@ TEST(VersionedLayerClientTest, PrefetchPartitionsSplitted) {
574552
{
575553
SCOPED_TRACE("Prefetch multiple partitions");
576554

577-
auto api_response =
578-
mockserver::ApiDefaultResponses::GenerateResourceApisResponse(kCatalog);
555+
auto api_response = ResponseGenerator::ResourceApis(kCatalog);
556+
PlatformUrlsGenerator generator(api_response, kLayerId);
579557
auto partitions_response1 =
580558
mockserver::ReadDefaultResponses::GeneratePartitionsResponse(
581559
partitions_count / 2);
@@ -586,10 +564,9 @@ TEST(VersionedLayerClientTest, PrefetchPartitionsSplitted) {
586564
EXPECT_CALL(*network_mock, Send(IsGetRequest(kUrlLookup), _, _, _, _))
587565
.WillOnce(ReturnHttpResponse(olp::http::NetworkResponse().WithStatus(
588566
olp::http::HttpStatusCode::OK),
589-
serialize(api_response)));
567+
api_response));
590568

591-
auto version_path = mock::GeneratePath(
592-
api_response, "metadata", mock::GenerateGetLatestVersionPath());
569+
auto version_path = generator.LatestVersion();
593570
ASSERT_FALSE(version_path.empty());
594571

595572
EXPECT_CALL(*network_mock, Send(IsGetRequest(version_path), _, _, _, _))
@@ -600,13 +577,9 @@ TEST(VersionedLayerClientTest, PrefetchPartitionsSplitted) {
600577
mockserver::ReadDefaultResponses::GenerateVersionResponse(
601578
version))));
602579

603-
auto partitions_path1 = mock::GeneratePath(
604-
api_response, "query",
605-
mock::GenerateGetPartitionsPath(kLayerId, partitions1, version));
580+
auto partitions_path1 = generator.PartitionsQuery(partitions1, version);
606581
ASSERT_FALSE(partitions_path1.empty());
607-
auto partitions_path2 = mock::GeneratePath(
608-
api_response, "query",
609-
mock::GenerateGetPartitionsPath(kLayerId, partitions2, version));
582+
auto partitions_path2 = generator.PartitionsQuery(partitions2, version);
610583
ASSERT_FALSE(partitions_path2.empty());
611584

612585
EXPECT_CALL(*network_mock, Send(IsGetRequest(partitions_path1), _, _, _, _))
@@ -621,9 +594,7 @@ TEST(VersionedLayerClientTest, PrefetchPartitionsSplitted) {
621594
olp::serializer::serialize(partitions_response2)));
622595

623596
for (const auto& partition : partitions_response1.GetPartitions()) {
624-
auto partition_path = mock::GeneratePath(
625-
api_response, "blob",
626-
mock::GenerateGetDataPath(kLayerId, partition.GetDataHandle()));
597+
auto partition_path = generator.DataBlob(partition.GetDataHandle());
627598
ASSERT_FALSE(partition_path.empty());
628599

629600
EXPECT_CALL(*network_mock, Send(IsGetRequest(partition_path), _, _, _, _))
@@ -633,9 +604,7 @@ TEST(VersionedLayerClientTest, PrefetchPartitionsSplitted) {
633604
}
634605

635606
for (const auto& partition : partitions_response2.GetPartitions()) {
636-
auto partition_path = mock::GeneratePath(
637-
api_response, "blob",
638-
mock::GenerateGetDataPath(kLayerId, partition.GetDataHandle()));
607+
auto partition_path = generator.DataBlob(partition.GetDataHandle());
639608
ASSERT_FALSE(partition_path.empty());
640609

641610
EXPECT_CALL(*network_mock, Send(IsGetRequest(partition_path), _, _, _, _))
@@ -704,29 +673,26 @@ TEST(VersionedLayerClientTest, PrefetchPartitionsSomeFail) {
704673
for (auto i = 0u; i < partitions_count; i++) {
705674
partitions.emplace_back(std::to_string(i));
706675
}
707-
auto api_response =
708-
mockserver::ApiDefaultResponses::GenerateResourceApisResponse(kCatalog);
676+
auto api_response = ResponseGenerator::ResourceApis(kCatalog);
677+
PlatformUrlsGenerator generator(api_response, kLayerId);
709678
auto partitions_response =
710679
mockserver::ReadDefaultResponses::GeneratePartitionsResponse(
711680
partitions_count);
712681
const auto request =
713682
olp::dataservice::read::PrefetchPartitionsRequest().WithPartitionIds(
714683
partitions);
715684
read::VersionedLayerClientImpl client(kHrn, kLayerId, boost::none, settings);
716-
auto partitions_path = mock::GeneratePath(
717-
api_response, "query",
718-
mock::GenerateGetPartitionsPath(kLayerId, partitions, version));
685+
auto partitions_path = generator.PartitionsQuery(partitions, version);
719686
ASSERT_FALSE(partitions_path.empty());
720687
{
721688
SCOPED_TRACE("Prefetch partitions, some fails");
722689

723690
EXPECT_CALL(*network_mock, Send(IsGetRequest(kUrlLookup), _, _, _, _))
724691
.WillOnce(ReturnHttpResponse(olp::http::NetworkResponse().WithStatus(
725692
olp::http::HttpStatusCode::OK),
726-
serialize(api_response)));
693+
api_response));
727694

728-
auto version_path = mock::GeneratePath(
729-
api_response, "metadata", mock::GenerateGetLatestVersionPath());
695+
auto version_path = generator.LatestVersion();
730696
ASSERT_FALSE(version_path.empty());
731697

732698
EXPECT_CALL(*network_mock, Send(IsGetRequest(version_path), _, _, _, _))
@@ -746,9 +712,7 @@ TEST(VersionedLayerClientTest, PrefetchPartitionsSomeFail) {
746712
olp::serializer::serialize(partitions_response)));
747713
for (auto i = 0u; i < partitions_response.GetPartitions().size(); i++) {
748714
const auto& partition = partitions_response.GetPartitions().at(i);
749-
auto partition_path = mock::GeneratePath(
750-
api_response, "blob",
751-
mock::GenerateGetDataPath(kLayerId, partition.GetDataHandle()));
715+
auto partition_path = generator.DataBlob(partition.GetDataHandle());
752716
ASSERT_FALSE(partition_path.empty());
753717

754718
EXPECT_CALL(*network_mock, Send(IsGetRequest(partition_path), _, _, _, _))
@@ -800,9 +764,7 @@ TEST(VersionedLayerClientTest, PrefetchPartitionsSomeFail) {
800764

801765
for (auto i = 0u; i < partitions_response.GetPartitions().size(); i++) {
802766
const auto& partition = partitions_response.GetPartitions().at(i);
803-
auto partition_path = mock::GeneratePath(
804-
api_response, "blob",
805-
mock::GenerateGetDataPath(kLayerId, partition.GetDataHandle()));
767+
auto partition_path = generator.DataBlob(partition.GetDataHandle());
806768
ASSERT_FALSE(partition_path.empty());
807769

808770
EXPECT_CALL(*network_mock, Send(IsGetRequest(partition_path), _, _, _, _))
@@ -846,15 +808,13 @@ TEST(VersionedLayerClientTest, PrefetchPartitionsFail) {
846808
for (auto i = 0u; i < partitions_count; i++) {
847809
partitions.emplace_back(std::to_string(i));
848810
}
849-
auto api_response =
850-
mockserver::ApiDefaultResponses::GenerateResourceApisResponse(kCatalog);
811+
auto api_response = ResponseGenerator::ResourceApis(kCatalog);
812+
PlatformUrlsGenerator generator(api_response, kLayerId);
851813
const auto request =
852814
olp::dataservice::read::PrefetchPartitionsRequest().WithPartitionIds(
853815
partitions);
854816
read::VersionedLayerClientImpl client(kHrn, kLayerId, boost::none, settings);
855-
auto partitions_path = mock::GeneratePath(
856-
api_response, "query",
857-
mock::GenerateGetPartitionsPath(kLayerId, partitions, version));
817+
auto partitions_path = generator.PartitionsQuery(partitions, version);
858818
ASSERT_FALSE(partitions_path.empty());
859819
{
860820
SCOPED_TRACE("Prefetch partitions, empty request");
@@ -881,10 +841,9 @@ TEST(VersionedLayerClientTest, PrefetchPartitionsFail) {
881841
EXPECT_CALL(*network_mock, Send(IsGetRequest(kUrlLookup), _, _, _, _))
882842
.WillOnce(ReturnHttpResponse(olp::http::NetworkResponse().WithStatus(
883843
olp::http::HttpStatusCode::OK),
884-
serialize(api_response)));
844+
api_response));
885845

886-
auto version_path = mock::GeneratePath(
887-
api_response, "metadata", mock::GenerateGetLatestVersionPath());
846+
auto version_path = generator.LatestVersion();
888847
ASSERT_FALSE(version_path.empty());
889848

890849
EXPECT_CALL(*network_mock, Send(IsGetRequest(version_path), _, _, _, _))
@@ -915,8 +874,7 @@ TEST(VersionedLayerClientTest, PrefetchPartitionsFail) {
915874
{
916875
SCOPED_TRACE("Get data handles fails");
917876

918-
auto version_path = mock::GeneratePath(
919-
api_response, "metadata", mock::GenerateGetLatestVersionPath());
877+
auto version_path = generator.LatestVersion();
920878
ASSERT_FALSE(version_path.empty());
921879
EXPECT_CALL(*network_mock, Send(IsGetRequest(version_path), _, _, _, _))
922880
.WillOnce(ReturnHttpResponse(

tests/common/ApiDefaultResponses.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@
2323
#include <utility>
2424
#include <vector>
2525

26-
#include <olp/core/generated/serializer/SerializerWrapper.h>
27-
#include <rapidjson/document.h>
28-
#include <rapidjson/stringbuffer.h>
29-
#include <rapidjson/writer.h>
30-
3126
#include "generated/model/Api.h"
3227

3328
namespace mockserver {

tests/common/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ set(OLP_SDK_TESTS_COMMON_HEADERS
2222
${CMAKE_CURRENT_SOURCE_DIR}/ApiDefaultResponses.h
2323
${CMAKE_CURRENT_SOURCE_DIR}/ReadDefaultResponses.h
2424
${CMAKE_CURRENT_SOURCE_DIR}/WriteDefaultResponses.h
25+
${CMAKE_CURRENT_SOURCE_DIR}/PlatformUrlsGenerator.h
26+
${CMAKE_CURRENT_SOURCE_DIR}/ResponseGenerator.h
2527
)
2628

2729
set(OLP_SDK_TESTS_COMMON_SOURCES
2830
${CMAKE_CURRENT_SOURCE_DIR}/mocks/CacheMock.cpp
2931
${CMAKE_CURRENT_SOURCE_DIR}/mocks/NetworkMock.cpp
3032
${CMAKE_CURRENT_SOURCE_DIR}/ReadDefaultResponses.cpp
33+
${CMAKE_CURRENT_SOURCE_DIR}/PlatformUrlsGenerator.cpp
34+
${CMAKE_CURRENT_SOURCE_DIR}/ResponseGenerator.cpp
3135
)
3236

3337
add_library(olp-cpp-sdk-tests-common
@@ -38,6 +42,11 @@ target_include_directories(olp-cpp-sdk-tests-common PUBLIC
3842
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
3943
)
4044

45+
target_include_directories(olp-cpp-sdk-tests-common
46+
PRIVATE
47+
${CMAKE_CURRENT_SOURCE_DIR}/../../olp-cpp-sdk-dataservice-read/src/
48+
)
49+
4150
target_link_libraries(olp-cpp-sdk-tests-common
4251
PUBLIC
4352
gmock
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 "PlatformUrlsGenerator.h"
21+
22+
#include <string>
23+
#include <vector>
24+
25+
// clang-format off
26+
#include "generated/parser/ApiParser.h"
27+
#include <olp/core/generated/parser/JsonParser.h>
28+
// clang-format on
29+
#include <olp/dataservice/read/model/Partitions.h>
30+
#include <olp/dataservice/read/model/VersionResponse.h>
31+
#include "generated/model/Api.h"
32+
33+
PlatformUrlsGenerator::PlatformUrlsGenerator(const std::string& apis,
34+
const std::string& layer)
35+
: apis_(std::make_shared<olp::dataservice::read::model::Apis>(
36+
olp::parser::parse<olp::dataservice::read::model::Apis>(apis))),
37+
layer_(layer) {}
38+
39+
std::string PlatformUrlsGenerator::PartitionsQuery(
40+
const olp::dataservice::read::PartitionsRequest::PartitionIds& partitions,
41+
uint64_t version) {
42+
std::string path = "/layers/" + layer_ + "/partitions?";
43+
for (const auto& partition : partitions) {
44+
path.append("partition=" + partition + "&");
45+
}
46+
path.append("version=" + std::to_string(version));
47+
48+
return FullPath("query", path);
49+
}
50+
51+
std::string PlatformUrlsGenerator::DataBlob(const std::string& data_handle) {
52+
return FullPath("blob", "/layers/" + layer_ + "/data/" + data_handle);
53+
}
54+
55+
std::string PlatformUrlsGenerator::LatestVersion() {
56+
return FullPath("metadata", "/versions/latest?startVersion=-1");
57+
}
58+
59+
std::string PlatformUrlsGenerator::VersionedQuadTree(const std::string& quadkey,
60+
uint64_t version,
61+
uint64_t depth) {
62+
auto path = "/layers/" + layer_ + "/versions/" + std::to_string(version) +
63+
"/quadkeys/" + quadkey + "/depths/" + std::to_string(depth);
64+
return FullPath("query", path);
65+
}
66+
67+
std::string PlatformUrlsGenerator::FullPath(const std::string& api_type,
68+
const std::string& path) {
69+
auto it = find_if(apis_->begin(), apis_->end(),
70+
[&](olp::dataservice::read::model::Api api) {
71+
return api.GetApi() == api_type;
72+
});
73+
if (it == apis_->end()) {
74+
return {};
75+
}
76+
auto url = it->GetBaseUrl();
77+
return url.append(path);
78+
}

0 commit comments

Comments
 (0)