Add Sentinel change notification handling logic
[ric-plt/sdl.git] / tst / asyncdummystorage_test.cpp
1 /*
2    Copyright (c) 2018-2019 Nokia.
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 */
16
17 #include <gtest/gtest.h>
18 #include <type_traits>
19 #include <memory>
20 #include <sys/eventfd.h>
21 #include "private/asyncdummystorage.hpp"
22 #include "private/tst/enginemock.hpp"
23
24 using namespace shareddatalayer;
25 using namespace shareddatalayer::tst;
26 using namespace testing;
27
28 namespace
29 {
30     class AsyncDummyStorageTest: public testing::Test
31     {
32     public:
33         std::shared_ptr<StrictMock<EngineMock>> engineMock;
34         int fd;
35         AsyncStorage::Namespace ns;
36         Engine::Callback storedCallback;
37         std::unique_ptr<AsyncDummyStorage> dummyStorage;
38
39         AsyncDummyStorageTest():
40             engineMock(std::make_shared<StrictMock<EngineMock>>()),
41             fd(10),
42             ns("someKnownNamespace")
43         {
44             dummyStorage.reset(new AsyncDummyStorage(engineMock));
45         }
46
47         MOCK_METHOD1(ack1, void(const std::error_code&));
48
49         MOCK_METHOD2(ack2, void(const std::error_code&, bool));
50
51         MOCK_METHOD2(ack3, void(const std::error_code&, const AsyncStorage::DataMap&));
52
53         MOCK_METHOD2(ack4, void(const std::error_code&, const AsyncStorage::Keys&));
54
55         void expectAck1()
56         {
57             EXPECT_CALL(*this, ack1(std::error_code()))
58                 .Times(1);
59         }
60
61         void expectAck2()
62         {
63             EXPECT_CALL(*this, ack2(std::error_code(), true))
64                 .Times(1);
65         }
66
67         void expectAck3()
68         {
69             EXPECT_CALL(*this, ack3(std::error_code(), IsEmpty()))
70                 .Times(1);
71         }
72
73         void expectAck4()
74         {
75             EXPECT_CALL(*this, ack4(std::error_code(), IsEmpty()))
76                 .Times(1);
77         }
78
79         void expectPostCallback()
80         {
81             EXPECT_CALL(*engineMock, postCallback(_))
82                 .Times(1)
83                 .WillOnce(SaveArg<0>(&storedCallback));
84         }
85     };
86 }
87
88 TEST_F(AsyncDummyStorageTest, IsNotCopyableAndIsNotMovable)
89 {
90     EXPECT_FALSE(std::is_copy_assignable<AsyncDummyStorage>::value);
91     EXPECT_FALSE(std::is_move_assignable<AsyncDummyStorage>::value);
92     EXPECT_FALSE(std::is_copy_constructible<AsyncDummyStorage>::value);
93     EXPECT_FALSE(std::is_move_constructible<AsyncDummyStorage>::value);
94 }
95
96 TEST_F(AsyncDummyStorageTest, ImplementsAsyncStorage)
97 {
98     EXPECT_TRUE((std::is_base_of<AsyncStorage, AsyncDummyStorage>::value));
99 }
100
101 TEST_F(AsyncDummyStorageTest, CanGetFd)
102 {
103     EXPECT_CALL(*engineMock, fd())
104         .Times(1)
105         .WillOnce(Return(fd));
106     EXPECT_EQ(fd, dummyStorage->fd());
107 }
108
109 TEST_F(AsyncDummyStorageTest, CanHandleEvents)
110 {
111     EXPECT_CALL(*engineMock, handleEvents())
112         .Times(1);
113     dummyStorage->handleEvents();
114 }
115
116 TEST_F(AsyncDummyStorageTest, AcksAreImmediatelyScheduled)
117 {
118     InSequence dummy;
119
120     expectPostCallback();
121     dummyStorage->waitReadyAsync(ns, std::bind(&AsyncDummyStorageTest::ack1,
122                                                this,
123                                                std::placeholders::_1));
124     expectAck1();
125     storedCallback();
126
127     expectPostCallback();
128     dummyStorage->setAsync(ns, { }, std::bind(&AsyncDummyStorageTest::ack1,
129                                               this,
130                                               std::placeholders::_1));
131     expectAck1();
132     storedCallback();
133
134     expectPostCallback();
135     dummyStorage->setIfAsync(ns, { }, { }, { }, std::bind(&AsyncDummyStorageTest::ack2,
136                                                           this,
137                                                           std::placeholders::_1,
138                                                           std::placeholders::_2));
139     expectAck2();
140     storedCallback();
141
142     expectPostCallback();
143     dummyStorage->setIfNotExistsAsync(ns, { }, { }, std::bind(&AsyncDummyStorageTest::ack2,
144                                                               this,
145                                                               std::placeholders::_1,
146                                                               std::placeholders::_2));
147     expectAck2();
148     storedCallback();
149
150     expectPostCallback();
151     dummyStorage->getAsync(ns, { }, std::bind(&AsyncDummyStorageTest::ack3,
152                                               this,
153                                               std::placeholders::_1,
154                                               std::placeholders::_2));
155     expectAck3();
156     storedCallback();
157
158     expectPostCallback();
159     dummyStorage->removeAsync(ns, { }, std::bind(&AsyncDummyStorageTest::ack1,
160                                                  this,
161                                                  std::placeholders::_1));
162     expectAck1();
163     storedCallback();
164
165     expectPostCallback();
166     dummyStorage->removeIfAsync(ns, { }, { }, std::bind(&AsyncDummyStorageTest::ack2,
167                                                         this,
168                                                         std::placeholders::_1,
169                                                         std::placeholders::_2));
170     expectAck2();
171     storedCallback();
172
173     expectPostCallback();
174     dummyStorage->findKeysAsync(ns,
175                                 "*",
176                                 std::bind(&AsyncDummyStorageTest::ack4,
177                                           this,
178                                           std::placeholders::_1,
179                                           std::placeholders::_2));
180     expectAck4();
181     storedCallback();
182
183     expectPostCallback();
184     dummyStorage->removeAllAsync(ns, std::bind(&AsyncDummyStorageTest::ack1,
185                                                this,
186                                                std::placeholders::_1));
187     expectAck1();
188     storedCallback();
189 }