|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/catalog/rest/rest_catalog.h" |
| 21 | + |
| 22 | +#include <httplib.h> |
| 23 | + |
| 24 | +#include <memory> |
| 25 | +#include <thread> |
| 26 | + |
| 27 | +#include <gmock/gmock.h> |
| 28 | +#include <gtest/gtest.h> |
| 29 | +#include <nlohmann/json.hpp> |
| 30 | + |
| 31 | +namespace iceberg::catalog::rest { |
| 32 | + |
| 33 | +class RestCatalogIntegrationTest : public ::testing::Test { |
| 34 | + protected: |
| 35 | + void SetUp() override { |
| 36 | + server_ = std::make_unique<httplib::Server>(); |
| 37 | + port_ = server_->bind_to_any_port("127.0.0.1"); |
| 38 | + |
| 39 | + server_thread_ = std::thread([this]() { server_->listen_after_bind(); }); |
| 40 | + } |
| 41 | + |
| 42 | + void TearDown() override { |
| 43 | + server_->stop(); |
| 44 | + if (server_thread_.joinable()) { |
| 45 | + server_thread_.join(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + std::unique_ptr<httplib::Server> server_; |
| 50 | + int port_ = -1; |
| 51 | + std::thread server_thread_; |
| 52 | +}; |
| 53 | + |
| 54 | +TEST_F(RestCatalogIntegrationTest, GetConfigSuccessfully) { |
| 55 | + server_->Get("/v1/config", [](const httplib::Request&, httplib::Response& res) { |
| 56 | + res.status = 200; |
| 57 | + res.set_content(R"({"warehouse": "s3://test-bucket"})", "application/json"); |
| 58 | + }); |
| 59 | + |
| 60 | + std::string base_uri = "http://127.0.0.1:" + std::to_string(port_); |
| 61 | + RestCatalog catalog(base_uri); |
| 62 | + cpr::Response response = catalog.GetConfig(); |
| 63 | + |
| 64 | + ASSERT_EQ(response.error.code, cpr::ErrorCode::OK); |
| 65 | + ASSERT_EQ(response.status_code, 200); |
| 66 | + |
| 67 | + auto json_body = nlohmann::json::parse(response.text); |
| 68 | + EXPECT_EQ(json_body["warehouse"], "s3://test-bucket"); |
| 69 | +} |
| 70 | + |
| 71 | +TEST_F(RestCatalogIntegrationTest, ListNamespacesReturnsMultipleResults) { |
| 72 | + server_->Get("/v1/namespaces", [](const httplib::Request&, httplib::Response& res) { |
| 73 | + res.status = 200; |
| 74 | + res.set_content(R"({ |
| 75 | + "namespaces": [ |
| 76 | + ["accounting", "db"], |
| 77 | + ["production", "db"] |
| 78 | + ] |
| 79 | + })", |
| 80 | + "application/json"); |
| 81 | + }); |
| 82 | + |
| 83 | + std::string base_uri = "http://127.0.0.1:" + std::to_string(port_); |
| 84 | + RestCatalog catalog(base_uri); |
| 85 | + cpr::Response response = catalog.ListNamespaces(); |
| 86 | + |
| 87 | + ASSERT_EQ(response.error.code, cpr::ErrorCode::OK); |
| 88 | + ASSERT_EQ(response.status_code, 200); |
| 89 | + |
| 90 | + auto json_body = nlohmann::json::parse(response.text); |
| 91 | + ASSERT_TRUE(json_body.contains("namespaces")); |
| 92 | + EXPECT_EQ(json_body["namespaces"].size(), 2); |
| 93 | + EXPECT_THAT(json_body["namespaces"][0][0], "accounting"); |
| 94 | +} |
| 95 | + |
| 96 | +TEST_F(RestCatalogIntegrationTest, HandlesServerError) { |
| 97 | + server_->Get("/v1/config", [](const httplib::Request&, httplib::Response& res) { |
| 98 | + res.status = 500; |
| 99 | + res.set_content("Internal Server Error", "text/plain"); |
| 100 | + }); |
| 101 | + |
| 102 | + std::string base_uri = "http://127.0.0.1:" + std::to_string(port_); |
| 103 | + RestCatalog catalog(base_uri); |
| 104 | + cpr::Response response = catalog.GetConfig(); |
| 105 | + |
| 106 | + ASSERT_EQ(response.error.code, cpr::ErrorCode::OK); |
| 107 | + ASSERT_EQ(response.status_code, 500); |
| 108 | + ASSERT_EQ(response.text, "Internal Server Error"); |
| 109 | +} |
| 110 | + |
| 111 | +} // namespace iceberg::catalog::rest |
0 commit comments