diff --git a/fbpcf/io/FileManagerUtil.cpp b/fbpcf/io/FileManagerUtil.cpp deleted file mode 100644 index 230ba2ad..00000000 --- a/fbpcf/io/FileManagerUtil.cpp +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#include "FileManagerUtil.h" - -#include "LocalFileManager.h" -#include "S3FileManager.h" -#include "fbpcf/aws/S3Util.h" - -namespace fbpcf::io { -std::unique_ptr getInputStream(const std::string& fileName) { - auto manager = getFileManager(fileName); - return manager->getInputStream(fileName); -} - -std::string read(const std::string& fileName) { - auto manager = getFileManager(fileName); - return manager->read(fileName); -} - -void write(const std::string& fileName, const std::string& data) { - auto manager = getFileManager(fileName); - return manager->write(fileName, data); -} - -FileType getFileType(const std::string& fileName) { - // S3 file format: https://bucket-name.s3.Region.amazonaws.com/key-name - return fileName.find("https://", 0) == 0 ? FileType::S3 : FileType::Local; -} - -std::unique_ptr getFileManager( - const std::string& fileName) { - auto type = getFileType(fileName); - if (type == FileType ::S3) { - const auto& ref = fbpcf::aws::uriToObjectReference(fileName); - // Other options have to be set via environment variables - return std::make_unique(fbpcf::aws::createS3Client( - fbpcf::aws::S3ClientOption{.region = ref.region})); - } else { - return std::make_unique(); - } -} -} // namespace fbpcf::io diff --git a/fbpcf/io/FileManagerUtil.h b/fbpcf/io/FileManagerUtil.h deleted file mode 100644 index fa6cd3e8..00000000 --- a/fbpcf/io/FileManagerUtil.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -#include -#include - -#include "IFileManager.h" -#include "IInputStream.h" - -namespace fbpcf::io { -enum class FileType { Local, S3 }; - -std::unique_ptr getInputStream(const std::string& fileName); - -std::string read(const std::string& fileName); - -void write(const std::string& fileName, const std::string& data); - -FileType getFileType(const std::string& fileName); - -std::unique_ptr getFileManager( - const std::string& fileName); -} // namespace fbpcf::io diff --git a/fbpcf/io/GCSFileManager.h b/fbpcf/io/GCSFileManager.h deleted file mode 100644 index 44b85082..00000000 --- a/fbpcf/io/GCSFileManager.h +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -#include - -#include -#include -#include "IInputStream.h" -#include "fbpcf/io/IFileManager.h" - -#include -#include -#include - -#include "fbpcf/exception/GcpException.h" -#include "fbpcf/gcp/GCSUtil.h" -#include "fbpcf/io/GCSInputStream.h" - -namespace gcs = ::google::cloud::storage; - -namespace fbpcf { -template -class GCSFileManager : public IFileManager { - public: - explicit GCSFileManager(std::shared_ptr client) - : GCSClient_{std::move(client)} {} - - std::unique_ptr getInputStream( - const std::string& fileName) override; - - std::string readBytes( - const std::string& fileName, - std::size_t start, - std::size_t end) override; - - std::string read(const std::string& fileName) override; - - void write(const std::string& fileName, const std::string& data) override; - - void copy(const std::string& sourceFile, const std::string& destination) - override; - - private: - std::shared_ptr GCSClient_; -}; - -template -std::unique_ptr GCSFileManager::getInputStream( - const std::string& fileName) { - const auto& ref = fbpcf::gcp::uriToObjectReference(fileName); - - auto outcome = GCSClient_->ReadObject(ref.bucket, ref.key, gcs::ReadRange()); - if (!outcome.status().ok()) { - throw GcpException{outcome.status().message()}; - } - - return std::make_unique(std::move(outcome)); -} - -template -std::string GCSFileManager::readBytes( - const std::string& fileName, - std::size_t start, - std::size_t end) { - const auto& ref = fbpcf::gcp::uriToObjectReference(fileName); - - auto outcome = - GCSClient_->ReadObject(ref.bucket, ref.key, gcs::ReadRange(start, end)); - if (!outcome.status().ok()) { - throw GcpException{outcome.status().message()}; - } - std::stringstream ss; - ss << outcome.rdbuf(); - return ss.str(); -} - -template -std::string GCSFileManager::read(const std::string& fileName) { - auto stream = getInputStream(fileName); - std::stringstream ss; - ss << stream->get().rdbuf(); - return ss.str(); -} - -template -void GCSFileManager::write( - const std::string& fileName, - const std::string& data) { - const auto& ref = fbpcf::gcp::uriToObjectReference(fileName); - - auto writer = GCSClient_->WriteObject(ref.bucket, ref.key); - - writer << data; - writer.Close(); - if (!writer.metadata()) { - throw GcpException{writer.metadata().status().message()}; - } -} - -template -void GCSFileManager::copy( - const std::string& sourceFile, - const std::string& destination) { - const auto& ref = fbpcf::gcp::uriToObjectReference(destination); - auto uploader = GCSClient_->UploadFile(sourceFile, ref.bucket, ref.key); - if (!uploader) { - throw GcpException{uploader.status().message()}; - } -} -} // namespace fbpcf diff --git a/fbpcf/io/GCSInputStream.cpp b/fbpcf/io/GCSInputStream.cpp deleted file mode 100644 index f55e7f87..00000000 --- a/fbpcf/io/GCSInputStream.cpp +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#include "fbpcf/io/GCSInputStream.h" - -#include - -namespace fbpcf { -std::istream& GCSInputStream::get() { - return s_; -} -} // namespace fbpcf diff --git a/fbpcf/io/GCSInputStream.h b/fbpcf/io/GCSInputStream.h deleted file mode 100644 index a4e4ec46..00000000 --- a/fbpcf/io/GCSInputStream.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -#include -#include - -#include "fbpcf/io/IInputStream.h" -namespace gcs = ::google::cloud::storage; -namespace fbpcf { -class GCSInputStream : public IInputStream { - public: - explicit GCSInputStream(gcs::ObjectReadStream&& s) : s_{std::move(s)} {} - - std::istream& get() override; - - private: - gcs::ObjectReadStream s_; -}; -} // namespace fbpcf diff --git a/fbpcf/io/IFileManager.h b/fbpcf/io/IFileManager.h deleted file mode 100644 index e36a05be..00000000 --- a/fbpcf/io/IFileManager.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -#include -#include -#include - -#include "IInputStream.h" - -namespace fbpcf { -class IFileManager { - public: - virtual ~IFileManager() {} - - virtual std::unique_ptr getInputStream( - const std::string& fileName) = 0; - - virtual std::string read(const std::string& fileName) = 0; - - virtual void write(const std::string& fileName, const std::string& data) = 0; - - virtual void copy( - const std::string& sourceFile, - const std::string& destination) = 0; - - virtual std::string readBytes( - const std::string& fileName, - std::size_t start, - std::size_t end) = 0; -}; -} // namespace fbpcf diff --git a/fbpcf/io/IInputStream.h b/fbpcf/io/IInputStream.h deleted file mode 100644 index 735a6b21..00000000 --- a/fbpcf/io/IInputStream.h +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -#include - -namespace fbpcf { -class IInputStream { - public: - virtual ~IInputStream() {} - - virtual std::istream& get() = 0; -}; -} // namespace fbpcf diff --git a/fbpcf/io/LocalFileManager.cpp b/fbpcf/io/LocalFileManager.cpp deleted file mode 100644 index f37e5c9a..00000000 --- a/fbpcf/io/LocalFileManager.cpp +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#include "LocalFileManager.h" - -#include -#include -#include -#include -#include - -#include "folly/Format.h" - -#include "LocalInputStream.h" -#include "fbpcf/exception/PcfException.h" - -namespace fbpcf { -std::unique_ptr LocalFileManager::getInputStream( - const std::string& fileName) { - std::ifstream is{fileName, std::ios_base::binary}; - - if (is.fail()) { - throw PcfException{folly::sformat("Failed to open file {}", fileName)}; - } - - return std::make_unique(std::move(is)); -} - -std::string LocalFileManager::read(const std::string& fileName) { - auto stream = getInputStream(fileName); - std::stringstream ss; - ss << stream->get().rdbuf(); - - return ss.str(); -} - -void LocalFileManager::write( - const std::string& fileName, - const std::string& data) { - std::filesystem::path filePath{fileName}; - std::filesystem::create_directories(filePath.parent_path()); - std::ofstream os{fileName}; - if (!os.is_open()) { - throw PcfException{folly::sformat("Failed to open file {}", fileName)}; - } - - os << data; -} - -void LocalFileManager::copy( - const std::string& sourceFile, - const std::string& destination) { - auto fileData = read(sourceFile); - write(destination, fileData); -} - -std::string LocalFileManager::readBytes( - const std::string& fileName, - std::size_t start, - std::size_t end) { - if (start > end) { - throw PcfException{folly::sformat( - "Start byte: <{}> is larger than the end: <{}>", start, end)}; - } - auto stream = getInputStream(fileName); - std::istream& is = stream->get(); - - // get length of file - is.seekg(0, std::ios::end); - size_t length = is.tellg(); - if (start > length) { - throw PcfException{folly::sformat( - "Start byte: <{}> is larger than the length: <{}>", start, length)}; - } - is.seekg(start, std::ios::beg); - - auto validatedEnd = std::min(length, end); - size_t bufferLength = validatedEnd - start; - - char buffer[bufferLength + 1]; - - // read data as a block: - if (is.readsome(buffer, bufferLength) != bufferLength) { - throw PcfException{ - folly::sformat( - "Hit exception when trying to read bytes from {}", fileName), - }; - } - - buffer[bufferLength] = '\0'; - - std::string s{buffer}; - return s; -} -} // namespace fbpcf diff --git a/fbpcf/io/LocalFileManager.h b/fbpcf/io/LocalFileManager.h deleted file mode 100644 index 2f315728..00000000 --- a/fbpcf/io/LocalFileManager.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -#include - -#include "IFileManager.h" - -namespace fbpcf { -class LocalFileManager : public IFileManager { - public: - std::unique_ptr getInputStream( - const std::string& fileName) override; - - std::string read(const std::string& fileName) override; - - void write(const std::string& fileName, const std::string& data) override; - - void copy(const std::string& sourceFile, const std::string& destination) - override; - - std::string - readBytes(const std::string& fileName, std::size_t start, std::size_t end); -}; -} // namespace fbpcf diff --git a/fbpcf/io/LocalInputStream.cpp b/fbpcf/io/LocalInputStream.cpp deleted file mode 100644 index 6c494a00..00000000 --- a/fbpcf/io/LocalInputStream.cpp +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#include "LocalInputStream.h" - -#include - -namespace fbpcf { -std::istream& LocalInputStream::get() { - return is_; -} -} // namespace fbpcf diff --git a/fbpcf/io/LocalInputStream.h b/fbpcf/io/LocalInputStream.h deleted file mode 100644 index a8fcd9dc..00000000 --- a/fbpcf/io/LocalInputStream.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -#include -#include - -#include "IInputStream.h" - -namespace fbpcf { -class LocalInputStream : public IInputStream { - public: - explicit LocalInputStream(std::ifstream is) : is_{std::move(is)} {} - - std::istream& get() override; - - private: - std::ifstream is_; -}; -} // namespace fbpcf diff --git a/fbpcf/io/MockFileManager.h b/fbpcf/io/MockFileManager.h deleted file mode 100644 index 993929e7..00000000 --- a/fbpcf/io/MockFileManager.h +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -#include - -#include "IFileManager.h" - -namespace fbpcf { -class MockFileManager : public IFileManager { - public: - MOCK_METHOD1(read, std::string(const std::string& fileName)); - - MOCK_METHOD2( - write, - void(const std::string& fileName, const std::string& data)); -}; -} // namespace fbpcf diff --git a/fbpcf/io/S3FileManager.cpp b/fbpcf/io/S3FileManager.cpp deleted file mode 100644 index ddfe5462..00000000 --- a/fbpcf/io/S3FileManager.cpp +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#include "S3FileManager.h" - -#include -#include -#include -#include - -#include -#include -#include - -#include "LocalFileManager.h" -#include "S3InputStream.h" -#include "fbpcf/aws/S3Util.h" -#include "fbpcf/exception/AwsException.h" - -namespace fbpcf { - -std::unique_ptr S3FileManager::getInputStream( - const std::string& fileName) { - const auto& ref = fbpcf::aws::uriToObjectReference(fileName); - Aws::S3::Model::GetObjectRequest request; - request.SetBucket(ref.bucket); - request.SetKey(ref.key); - - auto outcome = s3Client_->GetObject(request); - if (!outcome.IsSuccess()) { - throw AwsException{outcome.GetError().GetMessage()}; - } - - return std::make_unique(outcome.GetResultWithOwnership()); -} - -std::string S3FileManager::readBytes( - const std::string& fileName, - std::size_t start, - std::size_t end) { - const auto& ref = fbpcf::aws::uriToObjectReference(fileName); - Aws::S3::Model::GetObjectRequest request; - std::stringstream ss; - // NOTE: The AWS API uses a closed interval [a, b] to request a range while - // C++ string (and most "normal" programming APIs) use a half-open interval - // [a, b). Therefore, we subtract one here to make this usage consistent with - // other APIs. If the user passes in readBytes(path, 0, 4), we would expect - // to generate the range "bytes=0-3" - ss << "bytes=" << start << '-' << (end - 1); - request.SetBucket(ref.bucket); - request.SetKey(ref.key); - request.SetRange(ss.str()); - - auto outcome = s3Client_->GetObject(request); - if (!outcome.IsSuccess()) { - throw AwsException{outcome.GetError().GetMessage()}; - } - - auto stream = - std::make_unique(outcome.GetResultWithOwnership()); - std::stringstream ss2; - ss2 << stream->get().rdbuf(); - return ss2.str(); -} - -std::string S3FileManager::read(const std::string& fileName) { - auto stream = getInputStream(fileName); - std::stringstream ss; - ss << stream->get().rdbuf(); - return ss.str(); -} - -void S3FileManager::write( - const std::string& fileName, - const std::string& data) { - auto ss = std::make_shared(data); - writeDataStream(fileName, ss); -} - -void S3FileManager::copy( - const std::string& sourceFile, - const std::string& destination) { - std::shared_ptr sourceData = std::make_shared( - sourceFile.c_str(), std::ios_base::in | std::ios_base::binary); - writeDataStream(destination, sourceData); -} - -void S3FileManager::writeDataStream( - const std::string& fileName, - std::shared_ptr>> - dataStream) { - const auto& ref = fbpcf::aws::uriToObjectReference(fileName); - Aws::S3::Model::PutObjectRequest request; - request.SetBucket(ref.bucket); - request.SetKey(ref.key); - request.SetBody(dataStream); - auto outcome = s3Client_->PutObject(request); - - if (!outcome.IsSuccess()) { - throw AwsException{outcome.GetError().GetMessage()}; - } -} -} // namespace fbpcf diff --git a/fbpcf/io/S3FileManager.h b/fbpcf/io/S3FileManager.h deleted file mode 100644 index a66b5d1d..00000000 --- a/fbpcf/io/S3FileManager.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -#include - -#include -#include - -#include "IFileManager.h" -#include "IInputStream.h" - -namespace fbpcf { -class S3FileManager : public IFileManager { - public: - explicit S3FileManager(std::unique_ptr client) - : s3Client_{std::move(client)} {} - - std::unique_ptr getInputStream( - const std::string& fileName) override; - - std::string - readBytes(const std::string& fileName, std::size_t start, std::size_t end); - std::string read(const std::string& fileName) override; - - void write(const std::string& fileName, const std::string& data) override; - - void copy(const std::string& sourceFile, const std::string& destination) - override; - - void writeDataStream( - const std::string& fileName, - std::shared_ptr>> - dataStream); - - private: - std::unique_ptr s3Client_; -}; -} // namespace fbpcf diff --git a/fbpcf/io/S3InputStream.cpp b/fbpcf/io/S3InputStream.cpp deleted file mode 100644 index 243c4d9e..00000000 --- a/fbpcf/io/S3InputStream.cpp +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#include "S3InputStream.h" - -#include - -namespace fbpcf { -std::istream& S3InputStream::get() { - return r_.GetBody(); -} -} // namespace fbpcf diff --git a/fbpcf/io/S3InputStream.h b/fbpcf/io/S3InputStream.h deleted file mode 100644 index 655d127a..00000000 --- a/fbpcf/io/S3InputStream.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#pragma once - -#include - -#include - -#include "IInputStream.h" - -namespace fbpcf { -class S3InputStream : public IInputStream { - public: - explicit S3InputStream(Aws::S3::Model::GetObjectResult r) - : r_{std::move(r)} {} - - std::istream& get() override; - - private: - Aws::S3::Model::GetObjectResult r_; -}; -} // namespace fbpcf diff --git a/fbpcf/io/test/FileManagerUtilTest.cpp b/fbpcf/io/test/FileManagerUtilTest.cpp deleted file mode 100644 index b142ecf1..00000000 --- a/fbpcf/io/test/FileManagerUtilTest.cpp +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#include - -#include "fbpcf/io/FileManagerUtil.h" - -namespace fbpcf::io { -TEST(FileManagerUtilTest, TestGetS3FileType) { - auto type = - getFileType("https://bucket-name.s3.Region.amazonaws.com/key-name"); - EXPECT_EQ(FileType::S3, type); -} - -TEST(FileManagerUtilTest, TestGetLocalFileType) { - auto type = getFileType("/root/local"); - EXPECT_EQ(FileType::Local, type); -} -} // namespace fbpcf::io diff --git a/fbpcf/io/test/GCSFileManagerTest.cpp b/fbpcf/io/test/GCSFileManagerTest.cpp deleted file mode 100644 index 2b8ea1e9..00000000 --- a/fbpcf/io/test/GCSFileManagerTest.cpp +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#include -#include - -#include -#include - -#include "fbpcf/io/LocalFileManager.h" -#include "folly/Format.h" -#include "folly/Random.h" - -#include "fbpcf/exception/GcpException.h" -#include "fbpcf/gcp/MockGCSClient.h" -#include "fbpcf/io/GCSFileManager.h" - -using ::testing::_; - -namespace fbpcf { -class GCSFileManagerTest : public ::testing::Test { - protected: - void TearDown() override { - std::remove(filePath_.c_str()); - } - - void SetUp() override { - GCSClient = std::make_shared(); - } - - std::shared_ptr GCSClient; - const std::string testData_ = "this is test data"; - const std::string filePath_ = - folly::sformat("./testfile_{}", folly::Random::rand32()); - const std::string GCSUrl = "https://storage.cloud.google.com/bucket/key"; -}; -TEST_F(GCSFileManagerTest, testReadWithException) { - EXPECT_CALL(*GCSClient, ReadObject(_, _, _)).Times(1); - GCSFileManager fileManager{std::move(GCSClient)}; - - // this call is failing because the mocked response for default - // ObjectReadStream will have status.ok() = false - EXPECT_THROW(fileManager.read(GCSUrl), GcpException); -} - -TEST_F(GCSFileManagerTest, testWriteWithException) { - EXPECT_CALL(*GCSClient, WriteObject(_, _)).Times(1); - GCSFileManager fileManager{std::move(GCSClient)}; - - // this call is failing because the mocked response for default - // ObjectWriteStream will have status.ok() = false - EXPECT_THROW(fileManager.write(GCSUrl, testData_), GcpException); -} - -TEST_F(GCSFileManagerTest, testCopyWithException) { - auto localFileManager = LocalFileManager{}; - localFileManager.write(filePath_, testData_); - EXPECT_CALL(*GCSClient, UploadFile(_, _, _)).Times(1); - GCSFileManager fileManager{std::move(GCSClient)}; - - // this call is failing because the mocked response for default - // ObjectWriteStream will have status.ok() = false - EXPECT_THROW(fileManager.copy(filePath_, GCSUrl), GcpException); -} -} // namespace fbpcf diff --git a/fbpcf/io/test/LocalFileManagerTest.cpp b/fbpcf/io/test/LocalFileManagerTest.cpp deleted file mode 100644 index bd753f0c..00000000 --- a/fbpcf/io/test/LocalFileManagerTest.cpp +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#include -#include -#include - -#include - -#include "folly/Format.h" -#include "folly/Random.h" - -#include "fbpcf/exception/PcfException.h" -#include "fbpcf/io/LocalFileManager.h" - -namespace fbpcf { -class LocalFileManagerTest : public ::testing::Test { - protected: - void TearDown() override { - std::remove(filePath_.c_str()); - std::remove(copiedFilePath_.c_str()); - std::remove(nonExistentFolderPath_.c_str()); - } - - const std::string testData_ = "this is test data"; - const std::string filePath_ = - folly::sformat("./testfile_{}", folly::Random::rand32()); - const std::string copiedFilePath_ = - folly::sformat("./copy_testfile_{}", folly::Random::rand32()); - const std::string nonExistentFolderPath_ = - folly::sformat("fakedfolder/testfile_{}", folly::Random::rand32()); -}; - -TEST_F(LocalFileManagerTest, testReadWrite) { - LocalFileManager fileManager; - fileManager.write(filePath_, testData_); - auto resp = fileManager.read(filePath_); - EXPECT_EQ(testData_, resp); -} - -TEST_F(LocalFileManagerTest, testReadException) { - LocalFileManager fileManager; - EXPECT_THROW(fileManager.read("./fakedfile"), PcfException); -} - -TEST_F(LocalFileManagerTest, testWriteNonExistentFolder) { - LocalFileManager fileManager; - fileManager.write(nonExistentFolderPath_, testData_); - auto resp = fileManager.read(nonExistentFolderPath_); - EXPECT_EQ(testData_, resp); -} - -TEST_F(LocalFileManagerTest, testWriteReadBytes) { - LocalFileManager fileManager; - fileManager.write(filePath_, testData_); - auto resp1 = fileManager.readBytes(filePath_, 0, 5); - EXPECT_EQ(resp1, "this "); - - auto resp2 = fileManager.readBytes(filePath_, 10, 15); - EXPECT_EQ(resp2, "st da"); - - auto resp3 = fileManager.readBytes(filePath_, 1, 1); - EXPECT_EQ(resp3, ""); - - auto resp4 = fileManager.readBytes(filePath_, 15, 20); - EXPECT_EQ(resp4, "ta"); -} - -TEST_F(LocalFileManagerTest, testByteReadException) { - LocalFileManager fileManager; - fileManager.write(filePath_, testData_); - EXPECT_THROW(fileManager.readBytes(filePath_, 100, 101), PcfException); - EXPECT_THROW(fileManager.readBytes(filePath_, 5, 1), PcfException); -} - -TEST_F(LocalFileManagerTest, testCopy) { - LocalFileManager fileManager; - fileManager.write(filePath_, testData_); - fileManager.copy(filePath_, copiedFilePath_); - auto copiedData = fileManager.read(copiedFilePath_); - EXPECT_EQ(testData_, copiedData); -} -} // namespace fbpcf diff --git a/fbpcf/io/test/S3FileManagerTest.cpp b/fbpcf/io/test/S3FileManagerTest.cpp deleted file mode 100644 index faf2b9fb..00000000 --- a/fbpcf/io/test/S3FileManagerTest.cpp +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#include -#include -#include -#include - -#include -#include - -#include "fbpcf/io/LocalFileManager.h" -#include "folly/Format.h" -#include "folly/Random.h" - -#include "fbpcf/aws/AwsSdk.h" -#include "fbpcf/aws/MockS3Client.h" -#include "fbpcf/exception/AwsException.h" -#include "fbpcf/io/S3FileManager.h" - -using ::testing::_; - -namespace fbpcf { -class S3FileManagerTest : public ::testing::Test { - protected: - void TearDown() override { - std::remove(filePath_.c_str()); - s3Client = nullptr; - } - - void SetUp() override { - AwsSdk::aquire(); - s3Client = std::make_unique(); - } - - std::unique_ptr s3Client; - - const std::string testData_ = "this is test data"; - const std::string filePath_ = - folly::sformat("./testfile_{}", folly::Random::rand32()); - const std::string kS3URL = "https://bucket.s3.region.amazonaws.com/key"; -}; - -TEST_F(S3FileManagerTest, testReadWithException) { - EXPECT_CALL(*s3Client, GetObject(_)).Times(1); - S3FileManager fileManager{std::move(s3Client)}; - // by default, the call will fail, because the response indicates failure - EXPECT_THROW(fileManager.read(kS3URL), AwsException); -} - -TEST_F(S3FileManagerTest, testWriteWithException) { - EXPECT_CALL(*s3Client, PutObject(_)).Times(1); - S3FileManager fileManager{std::move(s3Client)}; - // by default, the call will fail, because the response indicates failure - EXPECT_THROW(fileManager.write(kS3URL, testData_), AwsException); -} - -TEST_F(S3FileManagerTest, testCopyWritesToS3) { - EXPECT_CALL(*s3Client, PutObject(_)).Times(1); - S3FileManager fileManager{std::move(s3Client)}; - auto localFileManager = LocalFileManager{}; - localFileManager.write(filePath_, testData_); - // by default, the call will fail, because the response indicates failure - EXPECT_THROW(fileManager.copy(filePath_, kS3URL), AwsException); -} -} // namespace fbpcf