|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include "iceberg/file_io_registry.h" |
| 19 | + |
| 20 | +#include <gmock/gmock.h> |
| 21 | +#include <gtest/gtest.h> |
| 22 | + |
| 23 | +#include "iceberg/test/matchers.h" |
| 24 | + |
| 25 | +namespace iceberg { |
| 26 | + |
| 27 | +namespace { |
| 28 | + |
| 29 | +/// A minimal FileIO implementation for testing. |
| 30 | +class MockFileIO : public FileIO { |
| 31 | + public: |
| 32 | + Result<std::string> ReadFile(const std::string& /*file_location*/, |
| 33 | + std::optional<size_t> /*length*/) override { |
| 34 | + return std::string("mock"); |
| 35 | + } |
| 36 | + |
| 37 | + Status WriteFile(const std::string& /*file_location*/, |
| 38 | + std::string_view /*content*/) override { |
| 39 | + return {}; |
| 40 | + } |
| 41 | + |
| 42 | + Status DeleteFile(const std::string& /*file_location*/) override { return {}; } |
| 43 | +}; |
| 44 | + |
| 45 | +} // namespace |
| 46 | + |
| 47 | +TEST(FileIoRegistryTest, RegisterAndLoad) { |
| 48 | + const std::string impl_name = "com.test.MockFileIO"; |
| 49 | + FileIORegistry::Register( |
| 50 | + impl_name, |
| 51 | + [](const std::string& /*warehouse*/, |
| 52 | + const std::unordered_map<std::string, std::string>& /*properties*/) |
| 53 | + -> Result<std::shared_ptr<FileIO>> { |
| 54 | + return std::make_shared<MockFileIO>(); |
| 55 | + }); |
| 56 | + |
| 57 | + auto result = FileIORegistry::Load(impl_name, "/test/warehouse", {}); |
| 58 | + ASSERT_THAT(result, IsOk()); |
| 59 | + EXPECT_NE(result.value(), nullptr); |
| 60 | + |
| 61 | + // Verify the loaded FileIO works |
| 62 | + auto read_result = result.value()->ReadFile("any_file", std::nullopt); |
| 63 | + ASSERT_THAT(read_result, IsOk()); |
| 64 | + EXPECT_EQ(read_result.value(), "mock"); |
| 65 | +} |
| 66 | + |
| 67 | +TEST(FileIoRegistryTest, LoadNonExistentReturnsError) { |
| 68 | + auto result = FileIORegistry::Load("com.nonexistent.FileIO", "/test/warehouse", {}); |
| 69 | + EXPECT_THAT(result, IsError(ErrorKind::kNotFound)); |
| 70 | + EXPECT_THAT(result, HasErrorMessage("FileIO implementation not found")); |
| 71 | +} |
| 72 | + |
| 73 | +TEST(FileIoRegistryTest, OverrideExistingRegistration) { |
| 74 | + const std::string impl_name = "com.test.OverrideFileIO"; |
| 75 | + |
| 76 | + // Register first implementation |
| 77 | + FileIORegistry::Register( |
| 78 | + impl_name, |
| 79 | + [](const std::string& /*warehouse*/, |
| 80 | + const std::unordered_map<std::string, std::string>& /*properties*/) |
| 81 | + -> Result<std::shared_ptr<FileIO>> { |
| 82 | + return std::make_shared<MockFileIO>(); |
| 83 | + }); |
| 84 | + |
| 85 | + // Override with a different factory |
| 86 | + FileIORegistry::Register( |
| 87 | + impl_name, |
| 88 | + [](const std::string& /*warehouse*/, |
| 89 | + const std::unordered_map<std::string, std::string>& /*properties*/) |
| 90 | + -> Result<std::shared_ptr<FileIO>> { |
| 91 | + return std::make_shared<MockFileIO>(); |
| 92 | + }); |
| 93 | + |
| 94 | + // Should still work (the override replaces the original) |
| 95 | + auto result = FileIORegistry::Load(impl_name, "/test/warehouse", {}); |
| 96 | + ASSERT_THAT(result, IsOk()); |
| 97 | + EXPECT_NE(result.value(), nullptr); |
| 98 | +} |
| 99 | + |
| 100 | +TEST(FileIoRegistryTest, FactoryReceivesWarehouseAndProperties) { |
| 101 | + const std::string impl_name = "com.test.PropCheckFileIO"; |
| 102 | + std::string captured_warehouse; |
| 103 | + std::unordered_map<std::string, std::string> captured_properties; |
| 104 | + |
| 105 | + FileIORegistry::Register( |
| 106 | + impl_name, |
| 107 | + [&captured_warehouse, &captured_properties]( |
| 108 | + const std::string& warehouse, |
| 109 | + const std::unordered_map<std::string, std::string>& properties) |
| 110 | + -> Result<std::shared_ptr<FileIO>> { |
| 111 | + captured_warehouse = warehouse; |
| 112 | + captured_properties = properties; |
| 113 | + return std::make_shared<MockFileIO>(); |
| 114 | + }); |
| 115 | + |
| 116 | + std::unordered_map<std::string, std::string> props = {{"key1", "val1"}, |
| 117 | + {"key2", "val2"}}; |
| 118 | + auto result = FileIORegistry::Load(impl_name, "s3://my-bucket/warehouse", props); |
| 119 | + ASSERT_THAT(result, IsOk()); |
| 120 | + EXPECT_EQ(captured_warehouse, "s3://my-bucket/warehouse"); |
| 121 | + EXPECT_EQ(captured_properties.at("key1"), "val1"); |
| 122 | + EXPECT_EQ(captured_properties.at("key2"), "val2"); |
| 123 | +} |
| 124 | + |
| 125 | +} // namespace iceberg |
0 commit comments