blob: d51818e91affc4ff8ef6a430cf692763d713baf6 [file] [log] [blame]
Kun Yi91beea62018-11-26 15:23:14 -08001#include "handler_unittest.hpp"
2
Kun Yi68c81142018-12-18 11:17:14 -08003using ::testing::_;
4using ::testing::Return;
5using ::testing::StrEq;
6using ::testing::StrNe;
7using namespace std::string_literals;
8using namespace binstore;
9
Kun Yi91beea62018-11-26 15:23:14 -080010namespace blobs
11{
12
Kun Yi68c81142018-12-18 11:17:14 -080013class 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
21TEST_F(BinaryStoreBlobHandlerBasicTest, CanHandleBlobZeroStoreFail)
22{
23 // Cannot handle since there is no store. Shouldn't crash.
24 EXPECT_FALSE(handler.canHandleBlob(basicTestInvalidBlobId));
25}
26
Kun Yi91beea62018-11-26 15:23:14 -080027TEST_F(BinaryStoreBlobHandlerBasicTest, CanHandleBlobChecksNameInvalid)
28{
Kun Yi68c81142018-12-18 11:17:14 -080029 auto bstore = std::make_unique<MockBinaryStore>();
30
31 handler.addNewBinaryStore(std::move(bstore));
32
Kun Yi91beea62018-11-26 15:23:14 -080033 // Verify canHandleBlob checks and returns false on an invalid name.
Kun Yi68c81142018-12-18 11:17:14 -080034 EXPECT_FALSE(handler.canHandleBlob(basicTestInvalidBlobId));
35}
36
37TEST_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
54TEST_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
83TEST_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 Yi91beea62018-11-26 15:23:14 -0800104}
105
Kun Yic0adbc32018-12-18 22:35:29 -0800106TEST_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 Yi91beea62018-11-26 15:23:14 -0800129} // namespace blobs