merge with master
[platform/framework/web/wrt-commons.git] / tests / dao / TestCases_PluginDAO.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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  * @file   TestCases_PluginDAO.cpp
18  * @author  Pawel Sikorski (p.sikorski@samsung.com)
19  * @version 1.0
20  * @brief   This file contains tests for feature dao class.
21  */
22
23 #include <list>
24 #include <dpl/test/test_runner.h>
25 #include <dpl/foreach.h>
26 #include <dpl/exception.h>
27 #include <dpl/wrt-dao-rw/plugin_dao.h>
28 //#include <plugin_install/plugin_objects.h>
29 #include <dpl/wrt-dao-ro/wrt_db_types.h>
30 #include <dpl/log/log.h>
31
32 using namespace WrtDB;
33
34 #define RUNNER_ASSERT_WHAT_EQUALS(in, test)                   \
35     { std::string tmp(in);                                     \
36       RUNNER_ASSERT_MSG(tmp == test, "Equals: [" + tmp + "]"); }
37
38 RUNNER_TEST_GROUP_INIT(DAO)
39
40 /*
41  * Name: plugin_dao_test_register_plugins
42  * Description: registers new plugin and check if it was correctly registered
43  * Expected: plugin should be correctly registered
44  */
45 RUNNER_TEST(plugin_dao_test_register_plugins)
46 {
47     {
48         std::string libraryPath("np1 lib_path");
49         std::string libraryName("np1");
50
51         PluginMetafileData pluginData;
52         pluginData.m_libraryName = libraryName;
53
54         PluginHandle handle = PluginDAO::registerPlugin(pluginData, libraryPath);
55         PluginDAO::setPluginInstallationStatus(
56             handle,
57             PluginDAO::
58                 INSTALLATION_COMPLETED);
59         RUNNER_ASSERT(PluginDAO::isPluginInstalled(libraryName) == true);
60
61         PluginDAO dao(handle);
62         std::string tmp;
63         tmp = dao.getLibraryPath(); //do for each
64         RUNNER_ASSERT_MSG(tmp == libraryPath, "Equals: " + tmp);
65     }
66
67     {
68         std::string libraryName("np2");
69
70         PluginMetafileData pluginData;
71         pluginData.m_libraryName = libraryName;
72
73         PluginHandle handle = PluginDAO::registerPlugin(pluginData, "");
74         PluginDAO::setPluginInstallationStatus(
75             handle,
76             PluginDAO::
77                 INSTALLATION_COMPLETED);
78         RUNNER_ASSERT(PluginDAO::isPluginInstalled(libraryName) == true);
79
80         PluginDAO dao(handle);
81         RUNNER_ASSERT(dao.getLibraryPath() == "");
82     }
83 }
84
85 /*
86  * Name: plugin_dao_test_register_plugin_implemented_object
87  * Description: registers new PluginImplementedObject
88  * and check if it was correctly registered
89  * Expected: plugin dao shoudld be upodated with PluginImplementedObject
90  */
91 RUNNER_TEST(plugin_dao_test_register_plugin_implemented_object)
92 {
93     {
94         std::string libraryPath("np3 lib_path");
95         std::string libraryName("np3");
96
97         PluginMetafileData pluginData;
98         pluginData.m_libraryName = libraryName;
99
100         PluginHandle handle =
101             PluginDAO::registerPlugin(pluginData, libraryPath);
102         RUNNER_ASSERT(PluginDAO::isPluginInstalled(libraryName) == true);
103
104         std::string object1("object1");
105         std::string object2("object2");
106         PluginDAO::registerPluginImplementedObject(object1, handle);
107         PluginDAO::registerPluginImplementedObject(object2, handle);
108
109         PluginHandle retHandle1 =
110             PluginDAO::getPluginHandleForImplementedObject(object1);
111         PluginHandle retHandle2 =
112             PluginDAO::getPluginHandleForImplementedObject(object1);
113         RUNNER_ASSERT(retHandle1 == handle);
114         RUNNER_ASSERT(retHandle2 == handle);
115     }
116 }
117
118 /*
119  * Name: plugin_dao_test_register_plugin_implemented_object
120  * Description: registers dependecies for plugins and checks if they were saved
121  * Expected: registered dependecies should be returned from database
122  */
123 RUNNER_TEST(plugin_dao_test_register_library_dependencies)
124 {
125     {
126         std::string libraryPath("np4 lib_path");
127         std::string libraryName("np4");
128
129         PluginMetafileData pluginData;
130         pluginData.m_libraryName = libraryName;
131
132         PluginHandle handle =
133             PluginDAO::registerPlugin(pluginData, libraryPath);
134         PluginDAO::setPluginInstallationStatus(
135             handle,
136             PluginDAO::
137                 INSTALLATION_COMPLETED);
138         RUNNER_ASSERT(PluginDAO::isPluginInstalled(libraryName) == true);
139
140         PluginHandle depHandles[] = { 117, 119 };
141
142         PluginHandleSetPtr dependencies(new PluginHandleSet);
143         dependencies->insert(depHandles[0]);
144         dependencies->insert(depHandles[1]);
145
146         PluginDAO::registerPluginLibrariesDependencies(handle, dependencies);
147
148         PluginDAO dao(handle);
149         PluginHandleSetPtr retDependencies;
150         retDependencies = dao.getLibraryDependencies();
151
152         RUNNER_ASSERT(
153             retDependencies->size() == sizeof(depHandles) /
154             sizeof(depHandles[0]));
155         RUNNER_ASSERT(
156             retDependencies->find(depHandles[0]) != retDependencies->end());
157         RUNNER_ASSERT(
158             retDependencies->find(depHandles[1]) != retDependencies->end());
159     }
160 }
161
162 /*
163  * Name: plugin_dao_test_register_required_object
164  * Description: registers required plugin objects for plugins and checks if they
165  * were saved
166  * Expected: registered required plugin objects should be returned from database
167  */
168 RUNNER_TEST(plugin_dao_test_register_required_object)
169 {
170     {
171         std::string libraryPath("np5 lib_path");
172         std::string libraryName("np5");
173
174         PluginMetafileData pluginData;
175         pluginData.m_libraryName = libraryName;
176
177         PluginHandle handle =
178             PluginDAO::registerPlugin(pluginData, libraryPath);
179         PluginDAO::setPluginInstallationStatus(
180             handle,
181             PluginDAO::
182                 INSTALLATION_COMPLETED);
183         RUNNER_ASSERT(PluginDAO::isPluginInstalled(libraryName) == true);
184
185         const size_t numObjects = 2;
186         std::string objectReq[numObjects];
187         objectReq[0] = std::string("object1.req");
188         objectReq[1] = std::string("object2.req");
189         PluginDAO::registerPluginRequiredObject(objectReq[0], handle);
190         PluginDAO::registerPluginRequiredObject(objectReq[1], handle);
191
192         WrtDB::PluginObjectsDAO::ObjectsPtr objects =
193             PluginDAO::getRequiredObjectsForPluginHandle(handle);
194
195         RUNNER_ASSERT(objects->size() == numObjects
196                       && objects->find(objectReq[0]) != objects->end()
197                       && objects->find(objectReq[1]) != objects->end());
198     }
199 }
200
201 /*
202  * Name: plugin_dao_test_is_library_installed
203  * Description: tests if plugin isntallation/registrartion works
204  * Expected: only registered plugins should be reported as installed
205  */
206 RUNNER_TEST(plugin_dao_test_is_library_installed)
207 {
208     {
209         //exist
210         std::list<const char *> preinstalled;
211         preinstalled.push_back("plugin1");
212         preinstalled.push_back("plugin2");
213         preinstalled.push_back("plugin3");
214         preinstalled.push_back("p4");
215         preinstalled.push_back("p5");
216
217         FOREACH(it, preinstalled)
218         RUNNER_ASSERT_MSG(PluginDAO::isPluginInstalled(*it),
219                           std::string("Not found: ") + *it);
220     }
221
222     {
223         //does not exist
224         RUNNER_ASSERT_MSG(
225             PluginDAO::isPluginInstalled("not_installed1") == false,
226             "Found not_installed1");
227         RUNNER_ASSERT_MSG(PluginDAO::isPluginInstalled("p 4") == false,
228                           "Found p 4");
229         RUNNER_ASSERT_MSG(PluginDAO::isPluginInstalled("") == false,
230                           "Found <empty>");
231         RUNNER_ASSERT_MSG(PluginDAO::isPluginInstalled("p33") == false,
232                           "Found p33");
233         RUNNER_ASSERT_MSG(PluginDAO::isPluginInstalled("feature1") == false,
234                           "Found feature1");
235     }
236 }
237
238 /*
239  * Name: plugin_dao_test_get_plugin_handle_list
240  * Description: test of returning plugin handle list
241  * Expected: returned list should be no less than number of registered plugins
242  */
243 RUNNER_TEST(plugin_dao_test_get_plugin_handle_list)
244 {
245     PluginHandleList handles = PluginDAO::getPluginHandleList();
246     RUNNER_ASSERT(handles.size() >= 5);
247 }
248
249 /*
250  * Name: plugin_dao_test_constructor_name
251  * Description: tests construction of plugin dao based on plugin name
252  * Expected: Instance of dao should be constructed only
253  * if there is given plugin in database
254  */
255 RUNNER_TEST(plugin_dao_test_constructor_name)
256 {
257     {
258         //exist
259         std::list<const char *> preinstalled;
260         preinstalled.push_back("plugin1");
261         preinstalled.push_back("plugin2");
262         preinstalled.push_back("plugin3");
263         preinstalled.push_back("p4");
264         preinstalled.push_back("p5");
265
266         FOREACH(it, preinstalled)
267         {
268             PluginDAO dao(*it);
269             RUNNER_ASSERT_WHAT_EQUALS(dao.getLibraryName(), *it);
270         }
271     }
272
273     {
274         //does not exist
275         std::list<const char *> not_installed;
276         not_installed.push_back("plugin 1");
277         not_installed.push_back("");
278         not_installed.push_back("p 3");
279
280         FOREACH(it, not_installed)
281         {
282             Try {
283                 //Plugin not exist
284                 PluginDAO dao(*it);
285                 RUNNER_ASSERT_MSG(false, "should not be found");
286             }
287             Catch(PluginDAO::Exception::PluginNotExist) {
288                 continue;
289             }
290         }
291     }
292 }
293
294 /*
295  * Name: plugin_dao_test_get_plugin_properties
296  * Description: tests reading plugin properties from database
297  * Expected: Data, inserted into database, should be accessible via dao
298  */
299 RUNNER_TEST(plugin_dao_test_get_plugin_properties)
300 {
301     {
302         PluginDAO dao("p4");
303         RUNNER_ASSERT(dao.getPluginHandle() == 4);
304         RUNNER_ASSERT_WHAT_EQUALS(dao.getLibraryName(), "p4");
305         RUNNER_ASSERT_WHAT_EQUALS(dao.getLibraryPath(), "path_to_p4");
306     }
307
308     {
309         PluginDAO dao(5);
310         RUNNER_ASSERT(dao.getPluginHandle() == 5);
311         RUNNER_ASSERT_WHAT_EQUALS(dao.getLibraryName(), "p5");
312         RUNNER_ASSERT_WHAT_EQUALS(dao.getLibraryPath(), "path_to_p5");
313     }
314
315     {
316         PluginDAO dao(2);
317         RUNNER_ASSERT(dao.getPluginHandle() == 2);
318         RUNNER_ASSERT_WHAT_EQUALS(dao.getLibraryName(), "plugin2");
319         RUNNER_ASSERT_WHAT_EQUALS(dao.getLibraryPath(), "path_to_plugin2");
320     }
321
322     {
323         PluginDAO dao(1);
324         RUNNER_ASSERT(dao.getPluginHandle() == 1);
325         RUNNER_ASSERT_WHAT_EQUALS(dao.getLibraryName(), "plugin1");
326         RUNNER_ASSERT_WHAT_EQUALS(dao.getLibraryPath(), "");
327     }
328 }
329
330 /*
331  * Name: plugin_dao_test_get_implemented_objects_for_plugin_handle_1
332  * Description: tests receiving from dao Implemented Objects
333  * Expected: returned object is size 0
334  */
335 RUNNER_TEST(plugin_dao_test_get_implemented_objects_for_plugin_handle_1)
336 {
337     {
338         const int handle = 1;
339         PluginDAOReadOnly dao(handle);
340         auto dbHandle = dao.getPluginHandle();
341         RUNNER_ASSERT(dbHandle == handle);
342         auto objects = dao.getImplementedObjectsForPluginHandle(dbHandle);
343
344         RUNNER_ASSERT(objects.empty());
345     }
346 }
347
348 /*
349  * Name: plugin_dao_test_get_implemented_objects_for_plugin_handle_2
350  * Description: tests receiving from dao Implemented Objects
351  * Expected: returned object is size as it was inserted
352  */
353 RUNNER_TEST(plugin_dao_test_get_implemented_objects_for_plugin_handle_2)
354 {
355     {
356         std::set< std::string > preinstalled = {
357             ""
358         };
359
360         PluginDAOReadOnly dao(2);
361         auto dbHandle = dao.getPluginHandle();
362         auto objects = dao.getImplementedObjectsForPluginHandle(dbHandle);
363
364         //LogError("\n" << objects.size() << " " << preinstalled.size() <<
365         // "\n");
366
367         RUNNER_ASSERT(objects.size() == preinstalled.size());
368
369         FOREACH(dbObject, objects)
370         {
371             RUNNER_ASSERT(preinstalled.find(*dbObject) != preinstalled.end());
372         }
373     }
374 }
375
376 /*
377  * Name: plugin_dao_test_get_implemented_objects_for_plugin_handle_3
378  * Description: tests receiving from dao Implemented Objects
379  * Expected: returned objects list has preinserted object
380  */
381 RUNNER_TEST(plugin_dao_test_get_implemented_objects_for_plugin_handle_3)
382 {
383     {
384         std::set< std::string > preinstalled = {
385             "Plugin_3_Object_A"
386         };
387
388         PluginDAOReadOnly dao(3);
389         auto dbHandle = dao.getPluginHandle();
390         auto objects = dao.getImplementedObjectsForPluginHandle(dbHandle);
391         RUNNER_ASSERT(objects.size() == preinstalled.size());
392
393         FOREACH(dbObject, objects)
394         {
395             RUNNER_ASSERT(preinstalled.find(*dbObject) != preinstalled.end());
396         }
397     }
398 }
399
400 /*
401  * Name: plugin_dao_test_get_implemented_objects_for_plugin_handle_4
402  * Description: tests receiving from dao Implemented Objects
403  * Expected: returned objects list has all preinserted objects
404  */
405 RUNNER_TEST(plugin_dao_test_get_implemented_objects_for_plugin_handle_4)
406 {
407     {
408         std::set< std::string > preinstalled = {
409             "Plugin_4_Object_A",
410             "Plugin_4_Object_B",
411             "Plugin_4_Object_C",
412         };
413
414         PluginDAOReadOnly dao(4);
415         auto dbHandle = dao.getPluginHandle();
416         auto objects = dao.getImplementedObjectsForPluginHandle(dbHandle);
417         RUNNER_ASSERT(objects.size() == preinstalled.size());
418
419         FOREACH(dbObject, objects)
420         {
421             RUNNER_ASSERT(preinstalled.find(*dbObject) != preinstalled.end());
422         }
423     }
424 }
425
426 /*
427  * Name: plugin_dao_test_get_implemented_objects_for_plugin_handle_5
428  * Description: tests receiving from dao Implemented Objects
429  * Expected: returned objects list do not have object that was not inserted
430  */
431 RUNNER_TEST(plugin_dao_test_get_implemented_objects_for_plugin_handle_5)
432 {
433     {
434         std::set< std::string > preinstalled = {
435             "Plugin_5_Object_B",
436         };
437
438         PluginDAOReadOnly dao(5);
439         auto dbHandle = dao.getPluginHandle();
440         auto objects = dao.getImplementedObjectsForPluginHandle(dbHandle);
441         RUNNER_ASSERT(objects.size() == preinstalled.size());
442
443         FOREACH(dbObject, objects)
444         {
445             RUNNER_ASSERT(preinstalled.find(*dbObject) == preinstalled.end());
446         }
447     }
448 }
449
450 #undef RUNNER_ASSERT_WHAT_EQUALS