|
| 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