New listKeys() API to support glob-style key search patterns
[ric-plt/sdl.git] / include / sdl / tst / mockableasyncstorage.hpp
1 /*
2    Copyright (c) 2018-2020 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 #ifndef SHAREDDATALAYER_TST_MOCKABLEASYNCSTORAGE_HPP_
23 #define SHAREDDATALAYER_TST_MOCKABLEASYNCSTORAGE_HPP_
24
25 #include <iostream>
26 #include <cstdlib>
27 #include <sdl/asyncstorage.hpp>
28
29 namespace shareddatalayer
30 {
31     namespace tst
32     {
33         /**
34          * @brief Helper class for mocking AsyncStorage
35          *
36          * MockableAsyncStorage can be used in application's unit tests to mock AsyncStorage.
37          * MockableAsyncStorage guarantees that even if new functions are added into
38          * AsyncStorage, existing unit test cases do not have to be modified unless
39          * those new functions are used.
40          *
41          * If a function is not mocked but called in unit tests, then the call
42          * will be logged and the process will abort.
43          */
44         class MockableAsyncStorage: public AsyncStorage
45         {
46         public:
47             MockableAsyncStorage() { }
48
49             virtual int fd() const override { logAndAbort(__PRETTY_FUNCTION__); }
50
51             virtual void handleEvents() override { logAndAbort(__PRETTY_FUNCTION__); }
52
53             virtual void waitReadyAsync(const Namespace&, const ReadyAck&) override { logAndAbort(__PRETTY_FUNCTION__); }
54
55             virtual void setAsync(const Namespace&, const DataMap&, const ModifyAck&) override { logAndAbort(__PRETTY_FUNCTION__); }
56
57             virtual void setIfAsync(const Namespace&, const Key&, const Data&, const Data&, const ModifyIfAck&) override { logAndAbort(__PRETTY_FUNCTION__); }
58
59             virtual void setIfNotExistsAsync(const Namespace&, const Key&, const Data&, const ModifyIfAck&) override { logAndAbort(__PRETTY_FUNCTION__); }
60
61             virtual void getAsync(const Namespace&, const Keys&, const GetAck&) override { logAndAbort(__PRETTY_FUNCTION__); }
62
63             virtual void removeAsync(const Namespace&, const Keys&, const ModifyAck&) override { logAndAbort(__PRETTY_FUNCTION__); }
64
65             virtual void removeIfAsync(const Namespace&, const Key&, const Data&, const ModifyIfAck&) override { logAndAbort(__PRETTY_FUNCTION__); }
66
67             virtual void findKeysAsync(const Namespace&, const std::string&, const FindKeysAck&) override { logAndAbort(__PRETTY_FUNCTION__); }
68
69             virtual void listKeys(const Namespace&, const std::string&, const FindKeysAck&) override { logAndAbort(__PRETTY_FUNCTION__); }
70
71             virtual void removeAllAsync(const Namespace&, const ModifyAck&) override { logAndAbort(__PRETTY_FUNCTION__); }
72
73         private:
74             static void logAndAbort(const char* function) noexcept __attribute__ ((__noreturn__))
75             {
76                 std::cerr << "MockableAsyncStorage: calling not-mocked function "
77                           << function << ", aborting" << std::endl;
78                 abort();
79             }
80         };
81     }
82 }
83
84 #endif