2 * SPDX-License-Identifier: LGPL-2.1-only
6 * @brief Mocking ML Agent APIs
7 * @see https://github.com/nnstreamer/nnstreamer
8 * @author Wook Song <wook.song16@samsung.com>
11 #ifndef __NNS_MLAGENT_MOCK_H__
12 #define __NNS_MLAGENT_MOCK_H__
16 #include <unordered_map>
23 #endif /* __cplusplus */
26 * @brief Initialize the static unique_ptr of MockMLAgent
28 void ml_agent_mock_init ();
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.
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);
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.
51 MockModel *ml_agent_mock_get_model (const gchar *name, const guint version);
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.
63 gint ml_agent_model_get (
64 const gchar *name, const guint version, gchar **description, GError **err);
67 #endif /* __cplusplus */
70 * @brief Class for mock Model object
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 } {
82 MockModel (const MockModel &other)
83 : MockModel (other.name_, other.path_, other.app_info_,
84 other.is_activated_, other.desc_, other.version_){
88 MockModel (MockModel &&other) noexcept
90 *this = std::move (other);
92 MockModel &operator= (const MockModel &other) = delete;
93 MockModel &operator= (MockModel &&other) noexcept
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);
108 bool operator== (const MockModel &model) const
110 if (name_ == model.name_ && version_ == model.version_)
116 std::string name () const
121 std::string path () const
125 std::string app_info () const
129 bool is_activated () const
131 return is_activated_;
134 std::string desc () const
139 guint version () const
145 void path (const std::string &path)
150 void is_activated (bool flag)
152 is_activated_ = flag;
155 void desc (const std::string &description)
160 void app_info (const std::string &info)
165 void version (guint ver)
171 * @brief Generate C-stringified JSON
172 * @return C-stringified JSON
174 gchar *to_cstr_json ();
179 std::string app_info_{};
180 bool is_activated_{ false };
182 guint version_{ 0U };
190 bool add_model (MockModel model)
192 std::string key = model.name ();
195 auto range = models_dict_.equal_range (key);
197 for (auto it = range.first; it != range.second; ++it) {
198 if (model == it->second)
203 models_dict_.insert (std::make_pair (key, model));
208 MockModel *get_model (const gchar *name, guint version)
210 std::string key{ name };
211 auto range = models_dict_.equal_range (key);
213 for (auto it = range.first; it != range.second; ++it) {
214 if (version == it->second.version ())
222 std::unordered_multimap<std::string, MockModel> models_dict_;
225 #endif /* __NNS_MLAGENT_MOCK_H__ */