|
| 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/QueryApi.h" |
| 28 | + |
| 29 | +namespace { |
| 30 | +constexpr auto kLayerid = "test-layer"; |
| 31 | +constexpr auto kQuadKey = "23618401"; |
| 32 | +constexpr auto kNodeBaseUrl = |
| 33 | + "https://some.node.base.url/stream/v2/catalogs/" |
| 34 | + "hrn:here:data::olp-here-test:hereos-internal-test-v2"; |
| 35 | +constexpr auto kUrlQuadTreeIndexVolatile = |
| 36 | + R"(https://some.node.base.url/stream/v2/catalogs/hrn:here:data::olp-here-test:hereos-internal-test-v2/layers/test-layer/quadkeys/23618401/depths/2)"; |
| 37 | + |
| 38 | +constexpr auto kUrlQuadTreeIndexVolatileAllInputs = |
| 39 | + R"(https://some.node.base.url/stream/v2/catalogs/hrn:here:data::olp-here-test:hereos-internal-test-v2/layers/test-layer/quadkeys/23618401/depths/2?additionalFields=checksum%2CdataSize&billingTag=OlpCppSdkTest)"; |
| 40 | + |
| 41 | +constexpr auto kHttpResponseEmpty = R"jsonString()jsonString"; |
| 42 | +constexpr auto kHttpResponseQuadTreeIndexVolatile = |
| 43 | + R"jsonString({ "parentQuads": [ { "additionalMetadata": "string", "checksum": "string", "compressedDataSize": 0, "dataHandle": "675911FF6236B7C7604BF8B105F1BB58", "dataSize": 0, "crc": "c3f276d7", "partition": "73982", "version": 0 } ], "subQuads": [ { "additionalMetadata": "string", "checksum": "291f66029c232400e3403cd6e9cfd36e", "compressedDataSize": 200, "dataHandle": "1b2ca68f-d4a0-4379-8120-cd025640510c", "dataSize": 1024, "crc": "c3f276d7", "subQuadKey": "string", "version": 1 } ] })jsonString"; |
| 44 | + |
| 45 | +using namespace ::testing; |
| 46 | +using namespace olp; |
| 47 | +using namespace olp::client; |
| 48 | +using namespace olp::dataservice::read; |
| 49 | +using namespace olp::tests::common; |
| 50 | + |
| 51 | +class QueryApiTest : public Test { |
| 52 | + protected: |
| 53 | + void SetUp() override { |
| 54 | + network_mock_ = std::make_shared<NetworkMock>(); |
| 55 | + |
| 56 | + settings_ = std::make_shared<OlpClientSettings>(); |
| 57 | + settings_->network_request_handler = network_mock_; |
| 58 | + |
| 59 | + client_ = OlpClientFactory::Create(*settings_); |
| 60 | + client_->SetBaseUrl(kNodeBaseUrl); |
| 61 | + } |
| 62 | + void TearDown() override { network_mock_.reset(); } |
| 63 | + |
| 64 | + protected: |
| 65 | + std::shared_ptr<OlpClientSettings> settings_; |
| 66 | + std::shared_ptr<OlpClient> client_; |
| 67 | + std::shared_ptr<NetworkMock> network_mock_; |
| 68 | +}; |
| 69 | + |
| 70 | +TEST_F(QueryApiTest, QuadTreeIndexVolatile) { |
| 71 | + { |
| 72 | + SCOPED_TRACE("Request metadata for tile."); |
| 73 | + EXPECT_CALL( |
| 74 | + *network_mock_, |
| 75 | + Send(AllOf(IsGetRequest(kUrlQuadTreeIndexVolatile)), _, _, _, _)) |
| 76 | + .WillOnce(ReturnHttpResponse( |
| 77 | + http::NetworkResponse().WithStatus(http::HttpStatusCode::OK), |
| 78 | + kHttpResponseQuadTreeIndexVolatile)); |
| 79 | + |
| 80 | + int32_t depth = 2; |
| 81 | + auto index_response = |
| 82 | + olp::dataservice::read::QueryApi::QuadTreeIndexVolatile( |
| 83 | + *client_, kLayerid, kQuadKey, depth, boost::none, boost::none, |
| 84 | + olp::client::CancellationContext{}); |
| 85 | + |
| 86 | + ASSERT_TRUE(index_response.IsSuccessful()); |
| 87 | + auto result = index_response.GetResult(); |
| 88 | + ASSERT_EQ(1u, result.GetSubQuads().size()); |
| 89 | + ASSERT_EQ(1u, result.GetParentQuads().size()); |
| 90 | + } |
| 91 | + { |
| 92 | + SCOPED_TRACE("Request with billing tag + additional fields."); |
| 93 | + EXPECT_CALL(*network_mock_, |
| 94 | + Send(AllOf(IsGetRequest(kUrlQuadTreeIndexVolatileAllInputs)), _, |
| 95 | + _, _, _)) |
| 96 | + .WillOnce(ReturnHttpResponse( |
| 97 | + http::NetworkResponse().WithStatus(http::HttpStatusCode::OK), |
| 98 | + kHttpResponseQuadTreeIndexVolatile)); |
| 99 | + |
| 100 | + int32_t depth = 2; |
| 101 | + std::string billing_tag = "OlpCppSdkTest"; |
| 102 | + std::vector<std::string> additional_fields = {"checksum", "dataSize"}; |
| 103 | + auto index_response = |
| 104 | + olp::dataservice::read::QueryApi::QuadTreeIndexVolatile( |
| 105 | + *client_, kLayerid, kQuadKey, depth, additional_fields, billing_tag, |
| 106 | + olp::client::CancellationContext{}); |
| 107 | + |
| 108 | + ASSERT_TRUE(index_response.IsSuccessful()); |
| 109 | + auto result = index_response.GetResult(); |
| 110 | + ASSERT_EQ(1u, result.GetSubQuads().size()); |
| 111 | + ASSERT_EQ(1u, result.GetParentQuads().size()); |
| 112 | + } |
| 113 | + // negative tests |
| 114 | + { |
| 115 | + SCOPED_TRACE("Requested quead not found."); |
| 116 | + EXPECT_CALL( |
| 117 | + *network_mock_, |
| 118 | + Send(AllOf(IsGetRequest(kUrlQuadTreeIndexVolatile)), _, _, _, _)) |
| 119 | + .WillOnce(ReturnHttpResponse( |
| 120 | + http::NetworkResponse().WithStatus(http::HttpStatusCode::NOT_FOUND), |
| 121 | + kHttpResponseEmpty)); |
| 122 | + |
| 123 | + int32_t depth = 2; |
| 124 | + auto index_response = |
| 125 | + olp::dataservice::read::QueryApi::QuadTreeIndexVolatile( |
| 126 | + *client_, kLayerid, kQuadKey, depth, boost::none, boost::none, |
| 127 | + olp::client::CancellationContext{}); |
| 128 | + |
| 129 | + ASSERT_FALSE(index_response.IsSuccessful()); |
| 130 | + auto error = index_response.GetError(); |
| 131 | + ASSERT_EQ(http::HttpStatusCode::NOT_FOUND, error.GetHttpStatusCode()); |
| 132 | + ASSERT_EQ(kHttpResponseEmpty, error.GetMessage()); |
| 133 | + } |
| 134 | + { |
| 135 | + SCOPED_TRACE("Not configured olp client"); |
| 136 | + auto client = OlpClient(); |
| 137 | + int32_t depth = 2; |
| 138 | + auto index_response = |
| 139 | + olp::dataservice::read::QueryApi::QuadTreeIndexVolatile( |
| 140 | + client, kLayerid, kQuadKey, depth, boost::none, boost::none, |
| 141 | + olp::client::CancellationContext{}); |
| 142 | + |
| 143 | + ASSERT_FALSE(index_response.IsSuccessful()); |
| 144 | + auto error = index_response.GetError(); |
| 145 | + ASSERT_EQ(static_cast<int>(olp::http::ErrorCode::OFFLINE_ERROR), error.GetHttpStatusCode()); |
| 146 | + } |
| 147 | + { |
| 148 | + SCOPED_TRACE("Invalid layer id"); |
| 149 | + EXPECT_CALL(*network_mock_, Send(_, _, _, _, _)) |
| 150 | + .WillOnce(ReturnHttpResponse(http::NetworkResponse().WithStatus( |
| 151 | + http::HttpStatusCode::BAD_REQUEST), |
| 152 | + kHttpResponseEmpty)); |
| 153 | + int32_t depth = 2; |
| 154 | + auto index_response = |
| 155 | + olp::dataservice::read::QueryApi::QuadTreeIndexVolatile( |
| 156 | + *client_, {}, kQuadKey, depth, boost::none, boost::none, |
| 157 | + olp::client::CancellationContext{}); |
| 158 | + |
| 159 | + ASSERT_FALSE(index_response.IsSuccessful()); |
| 160 | + auto error = index_response.GetError(); |
| 161 | + ASSERT_EQ(http::HttpStatusCode::BAD_REQUEST, error.GetHttpStatusCode()); |
| 162 | + ASSERT_EQ(kHttpResponseEmpty, error.GetMessage()); |
| 163 | + } |
| 164 | + { |
| 165 | + SCOPED_TRACE("Invalid quad key."); |
| 166 | + EXPECT_CALL(*network_mock_, Send(_, _, _, _, _)) |
| 167 | + .WillOnce(ReturnHttpResponse(http::NetworkResponse().WithStatus( |
| 168 | + http::HttpStatusCode::BAD_REQUEST), |
| 169 | + kHttpResponseEmpty)); |
| 170 | + int32_t depth = 2; |
| 171 | + auto index_response = |
| 172 | + olp::dataservice::read::QueryApi::QuadTreeIndexVolatile( |
| 173 | + *client_, kLayerid, {}, depth, boost::none, boost::none, |
| 174 | + olp::client::CancellationContext{}); |
| 175 | + |
| 176 | + ASSERT_FALSE(index_response.IsSuccessful()); |
| 177 | + auto error = index_response.GetError(); |
| 178 | + ASSERT_EQ(http::HttpStatusCode::BAD_REQUEST, error.GetHttpStatusCode()); |
| 179 | + ASSERT_EQ(kHttpResponseEmpty, error.GetMessage()); |
| 180 | + } |
| 181 | + { |
| 182 | + SCOPED_TRACE("Invalid depth."); |
| 183 | + EXPECT_CALL(*network_mock_, Send(_, _, _, _, _)) |
| 184 | + .WillOnce(ReturnHttpResponse(http::NetworkResponse().WithStatus( |
| 185 | + http::HttpStatusCode::BAD_REQUEST), |
| 186 | + kHttpResponseEmpty)); |
| 187 | + int32_t depth = -1; |
| 188 | + auto index_response = |
| 189 | + olp::dataservice::read::QueryApi::QuadTreeIndexVolatile( |
| 190 | + *client_, kLayerid, kQuadKey, depth, boost::none, boost::none, |
| 191 | + olp::client::CancellationContext{}); |
| 192 | + |
| 193 | + ASSERT_FALSE(index_response.IsSuccessful()); |
| 194 | + auto error = index_response.GetError(); |
| 195 | + ASSERT_EQ(http::HttpStatusCode::BAD_REQUEST, error.GetHttpStatusCode()); |
| 196 | + ASSERT_EQ(kHttpResponseEmpty, error.GetMessage()); |
| 197 | + } |
| 198 | + { |
| 199 | + SCOPED_TRACE("Empty additional fields."); |
| 200 | + EXPECT_CALL(*network_mock_, Send(_, _, _, _, _)) |
| 201 | + .WillOnce(ReturnHttpResponse(http::NetworkResponse().WithStatus( |
| 202 | + http::HttpStatusCode::BAD_REQUEST), |
| 203 | + kHttpResponseEmpty)); |
| 204 | + |
| 205 | + int32_t depth = 2; |
| 206 | + std::vector<std::string> empty_fields; |
| 207 | + auto index_response = |
| 208 | + olp::dataservice::read::QueryApi::QuadTreeIndexVolatile( |
| 209 | + *client_, kLayerid, kQuadKey, depth, empty_fields, boost::none, |
| 210 | + olp::client::CancellationContext{}); |
| 211 | + |
| 212 | + ASSERT_FALSE(index_response.IsSuccessful()); |
| 213 | + auto error = index_response.GetError(); |
| 214 | + ASSERT_EQ(http::HttpStatusCode::BAD_REQUEST, error.GetHttpStatusCode()); |
| 215 | + ASSERT_EQ(kHttpResponseEmpty, error.GetMessage()); |
| 216 | + } |
| 217 | + { |
| 218 | + SCOPED_TRACE("Empty billing tag."); |
| 219 | + EXPECT_CALL(*network_mock_, Send(_, _, _, _, _)) |
| 220 | + .WillOnce(ReturnHttpResponse(http::NetworkResponse().WithStatus( |
| 221 | + http::HttpStatusCode::BAD_REQUEST), |
| 222 | + kHttpResponseEmpty)); |
| 223 | + |
| 224 | + int32_t depth = 2; |
| 225 | + std::string empty_tag; |
| 226 | + auto index_response = |
| 227 | + olp::dataservice::read::QueryApi::QuadTreeIndexVolatile( |
| 228 | + *client_, kLayerid, kQuadKey, depth, boost::none, empty_tag, |
| 229 | + olp::client::CancellationContext{}); |
| 230 | + |
| 231 | + ASSERT_FALSE(index_response.IsSuccessful()); |
| 232 | + auto error = index_response.GetError(); |
| 233 | + ASSERT_EQ(http::HttpStatusCode::BAD_REQUEST, error.GetHttpStatusCode()); |
| 234 | + ASSERT_EQ(kHttpResponseEmpty, error.GetMessage()); |
| 235 | + } |
| 236 | + { |
| 237 | + SCOPED_TRACE("Cancelled CancellationContext"); |
| 238 | + |
| 239 | + auto context = olp::client::CancellationContext(); |
| 240 | + context.CancelOperation(); |
| 241 | + int32_t depth = 2; |
| 242 | + auto index_response = |
| 243 | + olp::dataservice::read::QueryApi::QuadTreeIndexVolatile( |
| 244 | + *client_, kLayerid, kQuadKey, depth, boost::none, boost::none, |
| 245 | + context); |
| 246 | + |
| 247 | + ASSERT_FALSE(index_response.IsSuccessful()); |
| 248 | + auto error = index_response.GetError(); |
| 249 | + ASSERT_EQ(olp::client::ErrorCode::Cancelled, error.GetErrorCode()); |
| 250 | + } |
| 251 | +} |
| 252 | + |
| 253 | +} // namespace |
0 commit comments