RIC:1060: Change in PTL
[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 /*
18  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19  * platform project (RICP).
20 */
21
22 #include <gtest/gtest.h>
23 #include <type_traits>
24 #include <memory>
25 #include <sys/eventfd.h>
26 #include "private/asyncdummystorage.hpp"
27 #include "private/tst/enginemock.hpp"
28
29 using namespace shareddatalayer;
30 using namespace shareddatalayer::tst;
31 using namespace testing;
32
33 namespace
34 {
35     class AsyncDummyStorageTest: public testing::Test
36     {
37     public:
38         std::shared_ptr<StrictMock<EngineMock>> engineMock;
39         int fd;
40         AsyncStorage::Namespace ns;
41         Engine::Callback storedCallback;
42         std::unique_ptr<AsyncDummyStorage> dummyStorage;
43
44         AsyncDummyStorageTest():
45             engineMock(std::make_shared<StrictMock<EngineMock>>()),
46             fd(10),
47             ns("someKnownNamespace")
48         {
49             dummyStorage.reset(new AsyncDummyStorage(engineMock));
50         }
51
52         MOCK_METHOD1(ack1, void(const std::error_code&));
53
54         MOCK_METHOD2(ack2, void(const std::error_code&, bool));
55
56         MOCK_METHOD2(ack3, void(const std::error_code&, const AsyncStorage::DataMap&));
57
58         MOCK_METHOD2(ack4, void(const std::error_code&, const AsyncStorage::Keys&));
59
60         void expectAck1()
61         {
62             EXPECT_CALL(*this, ack1(std::error_code()))
63                 .Times(1);
64         }
65
66         void expectAck2()
67         {
68             EXPECT_CALL(*this, ack2(std::error_code(), true))
69                 .Times(1);
70         }
71
72         void expectAck3()
73         {
74             EXPECT_CALL(*this, ack3(std::error_code(), IsEmpty()))
75                 .Times(1);
76         }
77
78         void expectAck4()
79         {
80             EXPECT_CALL(*this, ack4(std::error_code(), IsEmpty()))
81                 .Times(1);
82         }
83
84         void expectPostCallback()
85         {
86             EXPECT_CALL(*engineMock, postCallback(_))
87                 .Times(1)
88                 .WillOnce(SaveArg<0>(&storedCallback));
89         }
90     };
91 }
92
93 TEST_F(AsyncDummyStorageTest, IsNotCopyableAndIsNotMovable)
94 {
95     EXPECT_FALSE(std::is_copy_assignable<AsyncDummyStorage>::value);
96     EXPECT_FALSE(std::is_move_assignable<AsyncDummyStorage>::value);
97     EXPECT_FALSE(std::is_copy_constructible<AsyncDummyStorage>::value);
98     EXPECT_FALSE(std::is_move_constructible<AsyncDummyStorage>::value);
99 }
100
101 TEST_F(AsyncDummyStorageTest, ImplementsAsyncStorage)
102 {
103     EXPECT_TRUE((std::is_base_of<AsyncStorage, AsyncDummyStorage>::value));
104 }
105
106 TEST_F(AsyncDummyStorageTest, CanGetFd)
107 {
108     EXPECT_CALL(*engineMock, fd())
109         .Times(1)
110         .WillOnce(Return(fd));
111     EXPECT_EQ(fd, dummyStorage->fd());
112 }
113
114 TEST_F(AsyncDummyStorageTest, CanHandleEvents)
115 {
116     EXPECT_CALL(*engineMock, handleEvents())
117         .Times(1);
118     dummyStorage->handleEvents();
119 }
120
121 TEST_F(AsyncDummyStorageTest, AcksAreImmediatelyScheduled)
122 {
123     InSequence dummy;
124
125     expectPostCallback();
126     dummyStorage->waitReadyAsync(ns, std::bind(&AsyncDummyStorageTest::ack1,
127                                                this,
128                                                std::placeholders::_1));
129     expectAck1();
130     storedCallback();
131
132     expectPostCallback();
133     dummyStorage->setAsync(ns, { }, std::bind(&AsyncDummyStorageTest::ack1,
134                                               this,
135                                               std::placeholders::_1));
136     expectAck1();
137     storedCallback();
138
139     expectPostCallback();
140     dummyStorage->setIfAsync(ns, { }, { }, { }, std::bind(&AsyncDummyStorageTest::ack2,
141                                                           this,
142                                                           std::placeholders::_1,
143                                                           std::placeholders::_2));
144     expectAck2();
145     storedCallback();
146
147     expectPostCallback();
148     dummyStorage->setIfNotExistsAsync(ns, { }, { }, std::bind(&AsyncDummyStorageTest::ack2,
149                                                               this,
150                                                               std::placeholders::_1,
151                                                               std::placeholders::_2));
152     expectAck2();
153     storedCallback();
154
155     expectPostCallback();
156     dummyStorage->getAsync(ns, { }, std::bind(&AsyncDummyStorageTest::ack3,
157                                               this,
158                                               std::placeholders::_1,
159                                               std::placeholders::_2));
160     expectAck3();
161     storedCallback();
162
163     expectPostCallback();
164     dummyStorage->removeAsync(ns, { }, std::bind(&AsyncDummyStorageTest::ack1,
165                                                  this,
166                                                  std::placeholders::_1));
167     expectAck1();
168     storedCallback();
169
170     expectPostCallback();
171     dummyStorage->removeIfAsync(ns, { }, { }, std::bind(&AsyncDummyStorageTest::ack2,
172                                                         this,
173                                                         std::placeholders::_1,
174                                                         std::placeholders::_2));
175     expectAck2();
176     storedCallback();
177
178     expectPostCallback();
179     dummyStorage->findKeysAsync(ns,
180                                 "*",
181                                 std::bind(&AsyncDummyStorageTest::ack4,
182                                           this,
183                                           std::placeholders::_1,
184                                           std::placeholders::_2));
185     expectAck4();
186     storedCallback();
187
188     expectPostCallback();
189     dummyStorage->removeAllAsync(ns, std::bind(&AsyncDummyStorageTest::ack1,
190                                                this,
191                                                std::placeholders::_1));
192     expectAck1();
193     storedCallback();
194 }