New listKeys() API to support glob-style key search patterns
[ric-plt/sdl.git] / include / sdl / tst / mockablesyncstorage.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_MOCKABLESYNCSTORAGE_HPP_
23 #define SHAREDDATALAYER_TST_MOCKABLESYNCSTORAGE_HPP_
24
25 #include <iostream>
26 #include <cstdlib>
27 #include <sdl/syncstorage.hpp>
28
29 namespace shareddatalayer
30 {
31     namespace tst
32     {
33         /**
34          * @brief Helper class for mocking SyncStorage
35          *
36          * MockableSyncStorage can be used in application's unit tests to mock SyncStorage.
37          * MockableSyncStorage guarantees that even if new functions are added into
38          * SyncStorage, 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 MockableSyncStorage: public SyncStorage
45         {
46         public:
47             MockableSyncStorage() { }
48
49             virtual void waitReady(const Namespace&, const std::chrono::steady_clock::duration&) override { logAndAbort(__PRETTY_FUNCTION__); }
50
51             virtual void set(const Namespace&, const DataMap&) override { logAndAbort(__PRETTY_FUNCTION__); }
52
53             virtual bool setIf(const Namespace&, const Key&, const Data&, const Data&) override { logAndAbort(__PRETTY_FUNCTION__); }
54
55             virtual bool setIfNotExists(const Namespace&, const Key&, const Data&) override { logAndAbort(__PRETTY_FUNCTION__); }
56
57             virtual DataMap get(const Namespace&, const Keys&) override { logAndAbort(__PRETTY_FUNCTION__); }
58
59             virtual void remove(const Namespace&, const Keys&) override { logAndAbort(__PRETTY_FUNCTION__); }
60
61             virtual bool removeIf(const Namespace&, const Key&, const Data&) override { logAndAbort(__PRETTY_FUNCTION__); }
62
63             virtual Keys findKeys(const Namespace&, const std::string&) override { logAndAbort(__PRETTY_FUNCTION__); }
64
65             virtual Keys listKeys(const Namespace&, const std::string&) override { logAndAbort(__PRETTY_FUNCTION__); }
66
67             virtual void removeAll(const Namespace&) override { logAndAbort(__PRETTY_FUNCTION__); }
68
69             virtual void setOperationTimeout(const std::chrono::steady_clock::duration&) override { logAndAbort(__PRETTY_FUNCTION__); }
70
71         private:
72             static void logAndAbort(const char* function) noexcept __attribute__ ((__noreturn__))
73             {
74                 std::cerr << "MockableSyncStorage: calling not-mocked function "
75                           << function << ", aborting" << std::endl;
76                 abort();
77             }
78         };
79     }
80 }
81
82 #endif