Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 1 | #include "handler_unittest.hpp" |
| 2 | |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 3 | using ::testing::_; |
| 4 | using ::testing::Return; |
| 5 | using ::testing::StrEq; |
| 6 | using ::testing::StrNe; |
| 7 | using namespace std::string_literals; |
| 8 | using namespace binstore; |
| 9 | |
Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 10 | namespace blobs |
| 11 | { |
| 12 | |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 13 | class BinaryStoreBlobHandlerBasicTest : public BinaryStoreBlobHandlerTest |
| 14 | { |
| 15 | protected: |
| 16 | static inline std::string basicTestBaseId = "/test/"s; |
| 17 | static inline std::string basicTestBlobId = "/test/blob0"s; |
| 18 | static inline std::string basicTestInvalidBlobId = "/invalid/blob0"s; |
| 19 | }; |
| 20 | |
| 21 | TEST_F(BinaryStoreBlobHandlerBasicTest, CanHandleBlobZeroStoreFail) |
| 22 | { |
| 23 | // Cannot handle since there is no store. Shouldn't crash. |
| 24 | EXPECT_FALSE(handler.canHandleBlob(basicTestInvalidBlobId)); |
| 25 | } |
| 26 | |
Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 27 | TEST_F(BinaryStoreBlobHandlerBasicTest, CanHandleBlobChecksNameInvalid) |
| 28 | { |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 29 | auto bstore = std::make_unique<MockBinaryStore>(); |
| 30 | |
| 31 | handler.addNewBinaryStore(std::move(bstore)); |
| 32 | |
Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 33 | // Verify canHandleBlob checks and returns false on an invalid name. |
Kun Yi | 68c8114 | 2018-12-18 11:17:14 -0800 | [diff] [blame] | 34 | EXPECT_FALSE(handler.canHandleBlob(basicTestInvalidBlobId)); |
| 35 | } |
| 36 | |
| 37 | TEST_F(BinaryStoreBlobHandlerBasicTest, CanHandleBlobCanOpenValidBlob) |
| 38 | { |
| 39 | auto bstore = std::make_unique<MockBinaryStore>(); |
| 40 | |
| 41 | EXPECT_CALL(*bstore, getBaseBlobId()) |
| 42 | .WillRepeatedly(Return(basicTestBaseId)); |
| 43 | EXPECT_CALL(*bstore, canHandleBlob(StrNe(basicTestBlobId))) |
| 44 | .WillRepeatedly(Return(false)); |
| 45 | EXPECT_CALL(*bstore, canHandleBlob(StrEq(basicTestBlobId))) |
| 46 | .WillRepeatedly(Return(true)); |
| 47 | handler.addNewBinaryStore(std::move(bstore)); |
| 48 | |
| 49 | // Verify canHandleBlob return true for a blob id that it can handle |
| 50 | EXPECT_FALSE(handler.canHandleBlob(basicTestInvalidBlobId)); |
| 51 | EXPECT_TRUE(handler.canHandleBlob(basicTestBlobId)); |
| 52 | } |
| 53 | |
| 54 | TEST_F(BinaryStoreBlobHandlerBasicTest, CanHandleBlobCanOpenValidBlobMultiple) |
| 55 | { |
| 56 | auto bstore = std::make_unique<MockBinaryStore>(); |
| 57 | auto bstore1 = std::make_unique<MockBinaryStore>(); |
| 58 | const std::string anotherBaseId = "/another/"s; |
| 59 | const std::string anotherBlobId = "/another/blob/id"s; |
| 60 | |
| 61 | EXPECT_CALL(*bstore, getBaseBlobId()) |
| 62 | .WillRepeatedly(Return(basicTestBaseId)); |
| 63 | EXPECT_CALL(*bstore, canHandleBlob(StrNe(basicTestBlobId))) |
| 64 | .WillRepeatedly(Return(false)); |
| 65 | EXPECT_CALL(*bstore, canHandleBlob(StrEq(basicTestBlobId))) |
| 66 | .WillRepeatedly(Return(true)); |
| 67 | handler.addNewBinaryStore(std::move(bstore)); |
| 68 | |
| 69 | EXPECT_CALL(*bstore1, getBaseBlobId()) |
| 70 | .WillRepeatedly(Return(anotherBaseId)); |
| 71 | EXPECT_CALL(*bstore1, canHandleBlob(StrNe(anotherBlobId))) |
| 72 | .WillRepeatedly(Return(false)); |
| 73 | EXPECT_CALL(*bstore1, canHandleBlob(StrEq(anotherBlobId))) |
| 74 | .WillRepeatedly(Return(true)); |
| 75 | handler.addNewBinaryStore(std::move(bstore1)); |
| 76 | |
| 77 | // Verify canHandleBlob return true for a blob id that it can handle |
| 78 | EXPECT_FALSE(handler.canHandleBlob(basicTestInvalidBlobId)); |
| 79 | EXPECT_TRUE(handler.canHandleBlob(basicTestBlobId)); |
| 80 | EXPECT_TRUE(handler.canHandleBlob(anotherBlobId)); |
| 81 | } |
| 82 | |
| 83 | TEST_F(BinaryStoreBlobHandlerBasicTest, GetBlobIdEqualsConcatenationsOfIds) |
| 84 | { |
| 85 | std::string baseId0 = "/test/"s; |
| 86 | std::string baseId1 = "/test1/"s; |
| 87 | std::vector<std::string> idList0 = {"/test/"s, "/test/0"s}; |
| 88 | std::vector<std::string> idList1 = {"/test1/"s, "/test1/2"s}; |
| 89 | auto expectedIdList = idList0; |
| 90 | expectedIdList.insert(expectedIdList.end(), idList1.begin(), idList1.end()); |
| 91 | |
| 92 | auto bstore0 = std::make_unique<MockBinaryStore>(); |
| 93 | EXPECT_CALL(*bstore0, getBaseBlobId()).WillOnce(Return(baseId0)); |
| 94 | EXPECT_CALL(*bstore0, getBlobIds()).WillOnce(Return(idList0)); |
| 95 | handler.addNewBinaryStore(std::move(bstore0)); |
| 96 | |
| 97 | auto bstore1 = std::make_unique<MockBinaryStore>(); |
| 98 | EXPECT_CALL(*bstore1, getBaseBlobId()).WillOnce(Return(baseId1)); |
| 99 | EXPECT_CALL(*bstore1, getBlobIds()).WillOnce(Return(idList1)); |
| 100 | handler.addNewBinaryStore(std::move(bstore1)); |
| 101 | |
| 102 | // Verify canHandleBlob return true for a blob id that it can handle |
| 103 | EXPECT_EQ(expectedIdList, handler.getBlobIds()); |
Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 106 | TEST_F(BinaryStoreBlobHandlerBasicTest, DeleteReturnsWhatStoreReturns) |
| 107 | { |
| 108 | auto bstore = std::make_unique<MockBinaryStore>(); |
| 109 | |
| 110 | EXPECT_CALL(*bstore, getBaseBlobId()) |
| 111 | .WillRepeatedly(Return(basicTestBaseId)); |
| 112 | EXPECT_CALL(*bstore, canHandleBlob(StrNe(basicTestBlobId))) |
| 113 | .WillRepeatedly(Return(false)); |
| 114 | EXPECT_CALL(*bstore, canHandleBlob(StrEq(basicTestBlobId))) |
| 115 | .WillRepeatedly(Return(true)); |
| 116 | EXPECT_CALL(*bstore, deleteBlob(StrEq(basicTestBlobId))) |
| 117 | .WillOnce(Return(false)) |
| 118 | .WillOnce(Return(true)); |
| 119 | handler.addNewBinaryStore(std::move(bstore)); |
| 120 | |
| 121 | // Verify canHandleBlob return true for a blob id that it can handle |
| 122 | EXPECT_FALSE(handler.canHandleBlob(basicTestInvalidBlobId)); |
| 123 | EXPECT_TRUE(handler.canHandleBlob(basicTestBlobId)); |
| 124 | EXPECT_FALSE(handler.deleteBlob(basicTestInvalidBlobId)); |
| 125 | EXPECT_FALSE(handler.deleteBlob(basicTestBlobId)); |
| 126 | EXPECT_TRUE(handler.deleteBlob(basicTestBlobId)); |
| 127 | } |
| 128 | |
Kun Yi | 91beea6 | 2018-11-26 15:23:14 -0800 | [diff] [blame] | 129 | } // namespace blobs |