|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "google/cloud/storage/list_buckets_partial_reader.h" |
| 16 | +#include "google/cloud/storage/internal/bucket_metadata_parser.h" |
| 17 | +#include "google/cloud/storage/testing/canonical_errors.h" |
| 18 | +#include "google/cloud/storage/testing/mock_client.h" |
| 19 | +#include "google/cloud/testing_util/status_matchers.h" |
| 20 | +#include <gmock/gmock.h> |
| 21 | +#include <nlohmann/json.hpp> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +namespace google { |
| 25 | +namespace cloud { |
| 26 | +namespace storage { |
| 27 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 28 | +namespace { |
| 29 | + |
| 30 | +using ::google::cloud::storage::internal::ListBucketsRequest; |
| 31 | +using ::google::cloud::storage::internal::ListBucketsResponse; |
| 32 | +using ::google::cloud::storage::testing::MockClient; |
| 33 | +using ::google::cloud::storage::testing::canonical_errors::PermanentError; |
| 34 | +using ::testing::ContainerEq; |
| 35 | +using ::testing::Return; |
| 36 | + |
| 37 | +BucketMetadata CreateElement(int index) { |
| 38 | + std::string id = "bucket-" + std::to_string(index); |
| 39 | + std::string name = id; |
| 40 | + std::string link = "https://storage.googleapis.com/storage/v1/b/" + id; |
| 41 | + nlohmann::json metadata{ |
| 42 | + {"id", id}, |
| 43 | + {"name", name}, |
| 44 | + {"selfLink", link}, |
| 45 | + {"kind", "storage#bucket"}, |
| 46 | + }; |
| 47 | + return internal::BucketMetadataParser::FromJson(metadata).value(); |
| 48 | +} |
| 49 | + |
| 50 | +TEST(ListBucketsPartialReaderTest, Basic) { |
| 51 | + // We will have 3 pages. |
| 52 | + // Page 0: 2 buckets, 1 unreachable |
| 53 | + // Page 1: 1 bucket, 0 unreachable |
| 54 | + // Page 2: 0 buckets, 1 unreachable |
| 55 | + |
| 56 | + std::vector<BucketsPartial> expected; |
| 57 | + |
| 58 | + // Page 0 |
| 59 | + expected.push_back({{CreateElement(0), CreateElement(1)}, {"region-a"}}); |
| 60 | + |
| 61 | + // Page 1 |
| 62 | + expected.push_back({{CreateElement(2)}, {}}); |
| 63 | + |
| 64 | + // Page 2 |
| 65 | + expected.push_back({{}, {"region-b"}}); |
| 66 | + |
| 67 | + auto create_mock = [&](int i) { |
| 68 | + ListBucketsResponse response; |
| 69 | + if (i < 2) { |
| 70 | + response.next_page_token = "page-" + std::to_string(i + 1); |
| 71 | + } |
| 72 | + response.items = expected[i].buckets; |
| 73 | + response.unreachable = expected[i].unreachable; |
| 74 | + |
| 75 | + return [response](ListBucketsRequest const&) { |
| 76 | + return make_status_or(response); |
| 77 | + }; |
| 78 | + }; |
| 79 | + |
| 80 | + auto mock = std::make_shared<MockClient>(); |
| 81 | + EXPECT_CALL(*mock, ListBuckets) |
| 82 | + .WillOnce(create_mock(0)) |
| 83 | + .WillOnce(create_mock(1)) |
| 84 | + .WillOnce(create_mock(2)); |
| 85 | + |
| 86 | + auto reader = |
| 87 | + google::cloud::internal::MakePaginationRange<ListBucketsPartialReader>( |
| 88 | + ListBucketsRequest("test-project"), |
| 89 | + [mock](ListBucketsRequest const& r) { return mock->ListBuckets(r); }, |
| 90 | + [](internal::ListBucketsResponse r) { |
| 91 | + std::vector<BucketsPartial> result; |
| 92 | + if (r.items.empty() && r.unreachable.empty()) return result; |
| 93 | + result.push_back( |
| 94 | + BucketsPartial{std::move(r.items), std::move(r.unreachable)}); |
| 95 | + return result; |
| 96 | + }); |
| 97 | + |
| 98 | + std::vector<BucketsPartial> actual; |
| 99 | + for (auto&& page : reader) { |
| 100 | + ASSERT_STATUS_OK(page); |
| 101 | + actual.push_back(*std::move(page)); |
| 102 | + } |
| 103 | + |
| 104 | + ASSERT_EQ(actual.size(), expected.size()); |
| 105 | + for (size_t i = 0; i < actual.size(); ++i) { |
| 106 | + EXPECT_THAT(actual[i].buckets, ContainerEq(expected[i].buckets)); |
| 107 | + EXPECT_THAT(actual[i].unreachable, ContainerEq(expected[i].unreachable)); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +TEST(ListBucketsPartialReaderTest, Empty) { |
| 112 | + auto mock = std::make_shared<MockClient>(); |
| 113 | + EXPECT_CALL(*mock, ListBuckets) |
| 114 | + .WillOnce(Return(make_status_or(ListBucketsResponse()))); |
| 115 | + |
| 116 | + auto reader = |
| 117 | + google::cloud::internal::MakePaginationRange<ListBucketsPartialReader>( |
| 118 | + ListBucketsRequest("test-project"), |
| 119 | + [mock](ListBucketsRequest const& r) { return mock->ListBuckets(r); }, |
| 120 | + [](internal::ListBucketsResponse r) { |
| 121 | + std::vector<BucketsPartial> result; |
| 122 | + if (r.items.empty() && r.unreachable.empty()) return result; |
| 123 | + result.push_back( |
| 124 | + BucketsPartial{std::move(r.items), std::move(r.unreachable)}); |
| 125 | + return result; |
| 126 | + }); |
| 127 | + |
| 128 | + auto count = std::distance(reader.begin(), reader.end()); |
| 129 | + EXPECT_EQ(0, count); |
| 130 | +} |
| 131 | + |
| 132 | +TEST(ListBucketsPartialReaderTest, PermanentFailure) { |
| 133 | + // Create a synthetic list of BucketsPartial elements. |
| 134 | + std::vector<BucketsPartial> expected; |
| 135 | + |
| 136 | + // Page 0 |
| 137 | + expected.push_back({{CreateElement(0), CreateElement(1)}, {"region-a"}}); |
| 138 | + // Page 1 |
| 139 | + expected.push_back({{CreateElement(2)}, {}}); |
| 140 | + |
| 141 | + auto create_mock = [&](int i) { |
| 142 | + ListBucketsResponse response; |
| 143 | + response.next_page_token = "page-" + std::to_string(i + 1); |
| 144 | + response.items = expected[i].buckets; |
| 145 | + response.unreachable = expected[i].unreachable; |
| 146 | + return [response](ListBucketsRequest const&) { |
| 147 | + return StatusOr<ListBucketsResponse>(response); |
| 148 | + }; |
| 149 | + }; |
| 150 | + |
| 151 | + auto mock = std::make_shared<MockClient>(); |
| 152 | + EXPECT_CALL(*mock, ListBuckets) |
| 153 | + .WillOnce(create_mock(0)) |
| 154 | + .WillOnce(create_mock(1)) |
| 155 | + .WillOnce([](ListBucketsRequest const&) { |
| 156 | + return StatusOr<ListBucketsResponse>(PermanentError()); |
| 157 | + }); |
| 158 | + |
| 159 | + auto reader = |
| 160 | + google::cloud::internal::MakePaginationRange<ListBucketsPartialReader>( |
| 161 | + ListBucketsRequest("test-project"), |
| 162 | + [mock](ListBucketsRequest const& r) { return mock->ListBuckets(r); }, |
| 163 | + [](internal::ListBucketsResponse r) { |
| 164 | + std::vector<BucketsPartial> result; |
| 165 | + if (r.items.empty() && r.unreachable.empty()) return result; |
| 166 | + result.push_back( |
| 167 | + BucketsPartial{std::move(r.items), std::move(r.unreachable)}); |
| 168 | + return result; |
| 169 | + }); |
| 170 | + std::vector<BucketsPartial> actual; |
| 171 | + bool has_status_or_error = false; |
| 172 | + for (auto&& page : reader) { |
| 173 | + if (page.ok()) { |
| 174 | + actual.emplace_back(*std::move(page)); |
| 175 | + continue; |
| 176 | + } |
| 177 | + // The iteration should fail only once, an error should reset the iterator |
| 178 | + // to `end()`. |
| 179 | + EXPECT_FALSE(has_status_or_error); |
| 180 | + has_status_or_error = true; |
| 181 | + // Verify the error is what we expect. |
| 182 | + Status status = std::move(page).status(); |
| 183 | + EXPECT_EQ(PermanentError().code(), status.code()); |
| 184 | + EXPECT_EQ(PermanentError().message(), status.message()); |
| 185 | + } |
| 186 | + // The iteration should have returned an error at least once. |
| 187 | + EXPECT_TRUE(has_status_or_error); |
| 188 | + |
| 189 | + // The iteration should have returned all the elements prior to the error. |
| 190 | + ASSERT_EQ(actual.size(), expected.size()); |
| 191 | + for (size_t i = 0; i < actual.size(); ++i) { |
| 192 | + EXPECT_THAT(actual[i].buckets, ContainerEq(expected[i].buckets)); |
| 193 | + EXPECT_THAT(actual[i].unreachable, ContainerEq(expected[i].unreachable)); |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +} // namespace |
| 198 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 199 | +} // namespace storage |
| 200 | +} // namespace cloud |
| 201 | +} // namespace google |
0 commit comments