da043bd75f76b35c481eb774af0a1d12d2c3be73
[platform/upstream/nnstreamer.git] / tests / unittest_mlagent / mock_mlagent.h
1 /**
2  * SPDX-License-Identifier: LGPL-2.1-only
3  *
4  * @file        mlagent_mock.h
5  * @date        29 Nov 2023
6  * @brief       Mocking ML Agent APIs
7  * @see         https://github.com/nnstreamer/nnstreamer
8  * @author      Wook Song <wook.song16@samsung.com>
9  * @bug         No known bugs
10  */
11 #ifndef __NNS_MLAGENT_MOCK_H__
12 #define __NNS_MLAGENT_MOCK_H__
13
14 #include <glib.h>
15
16 #include <unordered_map>
17 #include <vector>
18
19 struct MockModel;
20
21 #ifdef __cplusplus
22 extern "C" {
23 #endif /* __cplusplus */
24
25 /**
26  * @brief Initialize the static unique_ptr of MockMLAgent
27  */
28 void ml_agent_mock_init ();
29
30 /**
31  * @brief C-wrapper for the MockModel's method add_model.
32  * @param[in] name The model's name to add
33  * @param[in] path The model's name to add
34  * @param[in] app_info The model's name to add
35  * @param[in] is_activated The model's name to add
36  * @param[in] desc The model's version to add
37  * @param[in] version The JSON c-string containing the information of the model matching to the given name and version
38  * @return true if there is a matching model to the given name and version, otherwise a negative integer
39  * @note ML Agent provides the original implementation of this function and
40  * the following implementation is only for testing purposes.
41  */
42 bool ml_agent_mock_add_model (const gchar *name, const gchar *path, const gchar *info,
43     const bool is_activated, const char *desc, const guint version);
44
45 /**
46  * @brief C-wrapper for the MockModel's method get_model.
47  * @param[in] name The model's name to retreive
48  * @param[in] version The model's version to retreive
49  * @return A pointer to the model matching the given information. If there is no model possible, NULL is returned.
50  */
51 MockModel *ml_agent_mock_get_model (const gchar *name, const guint version);
52
53 /**
54  * @brief Pass the JSON c-string generated by the ML Agent mock class to the caller.
55  * @param[in] name The model's name to retreive
56  * @param[in] version The model's version to retreive
57  * @param[out] description The JSON c-string, containing the information of the model matching the given name and version
58  * @param[out] err The return location for a recoverable error. This can be NULL.
59  * @return 0 if there is a matching model to the given name and version otherwise a negative integer
60  * @note ML Agent provides the original implementation of this function and
61  * the following implementation is only for testing purposes.
62  */
63 gint ml_agent_model_get (
64     const gchar *name, const guint version, gchar **description, GError **err);
65 #ifdef __cplusplus
66 }
67 #endif /* __cplusplus */
68
69 /**
70  * @brief Class for mock Model object
71  */
72 struct MockModel {
73   // Constructors
74   MockModel () = delete;
75   MockModel (std::string name, std::string path = {}, std::string app_info = {},
76       bool is_activated = false, std::string desc = {}, guint version = 0U)
77       : name_{ name }, path_{ path }, app_info_{ app_info },
78         is_activated_{ is_activated }, desc_{ desc }, version_{ version } {
79
80         };
81   // Copy constructor
82   MockModel (const MockModel &other)
83       : MockModel (other.name_, other.path_, other.app_info_,
84           other.is_activated_, other.desc_, other.version_){
85
86         };
87   // Move constructor
88   MockModel (MockModel &&other) noexcept
89   {
90     *this = std::move (other);
91   }
92   MockModel &operator= (const MockModel &other) = delete;
93   MockModel &operator= (MockModel &&other) noexcept
94   {
95     if (this == &other)
96       return *this;
97
98     this->name_ = std::move (other.name_);
99     this->path_ = std::move (other.path_);
100     this->app_info_ = std::move (other.app_info_);
101     this->is_activated_ = std::exchange (other.is_activated_, false);
102     this->desc_ = std::move (other.desc_);
103     this->version_ = std::exchange (other.version_, 0U);
104
105     return *this;
106   }
107
108   bool operator== (const MockModel &model) const
109   {
110     if (name_ == model.name_ && version_ == model.version_)
111       return true;
112     return false;
113   }
114
115   // Getters
116   std::string name () const
117   {
118     return name_;
119   }
120
121   std::string path () const
122   {
123     return path_;
124   }
125   std::string app_info () const
126   {
127     return app_info_;
128   }
129   bool is_activated () const
130   {
131     return is_activated_;
132   }
133
134   std::string desc () const
135   {
136     return desc_;
137   }
138
139   guint version () const
140   {
141     return version_;
142   }
143
144   // Setters
145   void path (const std::string &path)
146   {
147     path_ = path;
148   }
149
150   void is_activated (bool flag)
151   {
152     is_activated_ = flag;
153   }
154
155   void desc (const std::string &description)
156   {
157     desc_ = description;
158   }
159
160   void app_info (const std::string &info)
161   {
162     app_info_ = info;
163   }
164
165   void version (guint ver)
166   {
167     version_ = ver;
168   }
169
170   /**
171    * @brief Generate C-stringified JSON
172    * @return C-stringified JSON
173    */
174   gchar *to_cstr_json ();
175
176   private:
177   std::string name_;
178   std::string path_{};
179   std::string app_info_{};
180   bool is_activated_{ false };
181   std::string desc_{};
182   guint version_{ 0U };
183 };
184
185 struct MockMLAgent {
186   MockMLAgent ()
187   {
188   }
189
190   bool add_model (MockModel model)
191   {
192     std::string key = model.name ();
193
194     {
195       auto range = models_dict_.equal_range (key);
196
197       for (auto it = range.first; it != range.second; ++it) {
198         if (model == it->second)
199           return false;
200       }
201     }
202
203     models_dict_.insert (std::make_pair (key, model));
204
205     return true;
206   }
207
208   MockModel *get_model (const gchar *name, guint version)
209   {
210     std::string key{ name };
211     auto range = models_dict_.equal_range (key);
212
213     for (auto it = range.first; it != range.second; ++it) {
214       if (version == it->second.version ())
215         return &it->second;
216     }
217
218     return nullptr;
219   }
220
221   private:
222   std::unordered_multimap<std::string, MockModel> models_dict_;
223 };
224
225 #endif /* __NNS_MLAGENT_MOCK_H__ */