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