blob: 83053ad3bae348fbed785fe75154aa41f0627991 [file] [log] [blame]
Kun Yi38146a02018-12-18 21:54:26 -08001#include "handler_unittest.hpp"
2
3using ::testing::_;
4using ::testing::Return;
5using ::testing::StartsWith;
6
7using namespace std::string_literals;
8using namespace binstore;
9
10namespace blobs
11{
12
13TEST_F(BinaryStoreBlobHandlerOpenTest, FailWhenCannotHandleId)
14{
15 uint16_t flags = OpenFlags::read, sessionId = 0;
16 EXPECT_FALSE(handler.open(sessionId, flags, "/invalid/blob"s));
17}
18
19TEST_F(BinaryStoreBlobHandlerOpenTest, FailWhenStoreOpenReturnsFailure)
20{
21 auto testBaseId = "/test/"s;
22 auto testBlobId = "/test/blob0"s;
23 uint16_t flags = OpenFlags::read, sessionId = 0;
24 auto bstore = std::make_unique<MockBinaryStore>();
25
26 EXPECT_CALL(*bstore, getBaseBlobId()).WillRepeatedly(Return(testBaseId));
27 EXPECT_CALL(*bstore, canHandleBlob(StartsWith(testBaseId)))
28 .WillRepeatedly(Return(true));
29 EXPECT_CALL(*bstore, openOrCreateBlob(_, flags)).WillOnce(Return(false));
30
31 handler.addNewBinaryStore(std::move(bstore));
32
33 EXPECT_FALSE(handler.open(sessionId, flags, testBlobId));
34}
35
36TEST_F(BinaryStoreBlobHandlerOpenTest, SucceedWhenStoreOpenReturnsTrue)
37{
38 auto testBaseId = "/test/"s;
39 auto testBlobId = "/test/blob0"s;
40 uint16_t flags = OpenFlags::read, sessionId = 0;
41 auto bstore = std::make_unique<MockBinaryStore>();
42
43 EXPECT_CALL(*bstore, getBaseBlobId()).WillRepeatedly(Return(testBaseId));
44 EXPECT_CALL(*bstore, canHandleBlob(StartsWith(testBaseId)))
45 .WillRepeatedly(Return(true));
46 EXPECT_CALL(*bstore, openOrCreateBlob(_, flags)).WillOnce(Return(true));
47
48 handler.addNewBinaryStore(std::move(bstore));
49
50 EXPECT_TRUE(handler.open(sessionId, flags, testBlobId));
51}
52
Kun Yic0adbc32018-12-18 22:35:29 -080053TEST_F(BinaryStoreBlobHandlerOpenTest, CloseFailForInvalidSession)
54{
55 uint16_t invalidSessionId = 1;
56 EXPECT_FALSE(handler.close(invalidSessionId));
57}
58
59TEST_F(BinaryStoreBlobHandlerOpenTest, CloseFailWhenStoreCloseFails)
60{
61 auto testBaseId = "/test/"s;
62 auto testBlobId = "/test/blob0"s;
63 uint16_t flags = OpenFlags::read, sessionId = 0;
64 auto bstore = std::make_unique<MockBinaryStore>();
65
66 EXPECT_CALL(*bstore, getBaseBlobId()).WillRepeatedly(Return(testBaseId));
67 EXPECT_CALL(*bstore, canHandleBlob(StartsWith(testBaseId)))
68 .WillRepeatedly(Return(true));
69 EXPECT_CALL(*bstore, openOrCreateBlob(_, flags)).WillOnce(Return(true));
70 EXPECT_CALL(*bstore, close()).WillOnce(Return(false));
71
72 handler.addNewBinaryStore(std::move(bstore));
73
74 EXPECT_TRUE(handler.open(sessionId, flags, testBlobId));
75 EXPECT_FALSE(handler.close(sessionId));
76}
77
78TEST_F(BinaryStoreBlobHandlerOpenTest, CloseSucceedWhenStoreCloseSucceeds)
79{
80 auto testBaseId = "/test/"s;
81 auto testBlobId = "/test/blob0"s;
82 uint16_t flags = OpenFlags::read, sessionId = 0;
83 auto bstore = std::make_unique<MockBinaryStore>();
84
85 EXPECT_CALL(*bstore, getBaseBlobId()).WillRepeatedly(Return(testBaseId));
86 EXPECT_CALL(*bstore, canHandleBlob(StartsWith(testBaseId)))
87 .WillRepeatedly(Return(true));
88 EXPECT_CALL(*bstore, openOrCreateBlob(_, flags)).WillOnce(Return(true));
89 EXPECT_CALL(*bstore, close()).WillOnce(Return(true));
90
91 handler.addNewBinaryStore(std::move(bstore));
92
93 EXPECT_TRUE(handler.open(sessionId, flags, testBlobId));
94 EXPECT_TRUE(handler.close(sessionId));
95}
96
97TEST_F(BinaryStoreBlobHandlerOpenTest, ClosedSessionCannotBeReclosed)
98{
99 auto testBaseId = "/test/"s;
100 auto testBlobId = "/test/blob0"s;
101 uint16_t flags = OpenFlags::read, sessionId = 0;
102 auto bstore = std::make_unique<MockBinaryStore>();
103
104 EXPECT_CALL(*bstore, getBaseBlobId()).WillRepeatedly(Return(testBaseId));
105 EXPECT_CALL(*bstore, canHandleBlob(StartsWith(testBaseId)))
106 .WillRepeatedly(Return(true));
107 EXPECT_CALL(*bstore, openOrCreateBlob(_, flags)).WillOnce(Return(true));
108 EXPECT_CALL(*bstore, close()).WillRepeatedly(Return(true));
109
110 handler.addNewBinaryStore(std::move(bstore));
111
112 EXPECT_TRUE(handler.open(sessionId, flags, testBlobId));
113 EXPECT_TRUE(handler.close(sessionId));
114 EXPECT_FALSE(handler.close(sessionId));
115}
116
Kun Yi38146a02018-12-18 21:54:26 -0800117} // namespace blobs