tizen 2.3 release
[framework/web/wearable/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         unsigned int size = PluginDAO::getPluginHandleList().size();
67         PluginDAO::unregisterPlugin(handle);
68         RUNNER_ASSERT(PluginDAO::getPluginHandleList().size() ==  size - 1);
69     }
70
71     {
72         std::string libraryName("np2");
73
74         PluginMetafileData pluginData;
75         pluginData.m_libraryName = libraryName;
76
77         PluginHandle handle = PluginDAO::registerPlugin(pluginData, "");
78         PluginDAO::setPluginInstallationStatus(
79             handle,
80             PluginDAO::
81                 INSTALLATION_COMPLETED);
82         RUNNER_ASSERT(PluginDAO::isPluginInstalled(libraryName) == true);
83
84         PluginDAO dao(handle);
85         RUNNER_ASSERT(dao.getLibraryPath() == "");
86
87         unsigned int size = PluginDAO::getPluginHandleList().size();
88         PluginDAO::unregisterPlugin(handle);
89         RUNNER_ASSERT(PluginDAO::getPluginHandleList().size() ==  size - 1);
90     }
91 }
92
93 /*
94  * Name: plugin_dao_test_register_plugin_implemented_object
95  * Description: registers new PluginImplementedObject
96  * and check if it was correctly registered
97  * Expected: plugin dao shoudld be upodated with PluginImplementedObject
98  */
99 RUNNER_TEST(plugin_dao_test_register_plugin_implemented_object)
100 {
101     {
102         std::string libraryPath("np3 lib_path");
103         std::string libraryName("np3");
104
105         PluginMetafileData pluginData;
106         pluginData.m_libraryName = libraryName;
107
108         PluginHandle handle =
109             PluginDAO::registerPlugin(pluginData, libraryPath);
110         RUNNER_ASSERT(PluginDAO::isPluginInstalled(libraryName) == true);
111
112         std::string object1("object1");
113         std::string object2("object2");
114         PluginDAO::registerPluginImplementedObject(object1, handle);
115         PluginDAO::registerPluginImplementedObject(object2, handle);
116
117         PluginHandle retHandle1 =
118             PluginDAO::getPluginHandleForImplementedObject(object1);
119         PluginHandle retHandle2 =
120             PluginDAO::getPluginHandleForImplementedObject(object1);
121         RUNNER_ASSERT(retHandle1 == handle);
122         RUNNER_ASSERT(retHandle2 == handle);
123     }
124 }
125
126 /*
127  * Name: plugin_dao_test_get_root_plugin_handle_list
128  * Description: test of returning root plugin handle list
129  * Expected: -
130  */
131 RUNNER_TEST(plugin_dao_test_get_root_plugin_handle_list)
132 {
133     PluginHandleList handles = PluginDAOReadOnly::getRootPluginHandleList();
134     RUNNER_ASSERT(handles.size() == 3);
135
136     int count = 0;
137     FOREACH(n, handles){
138         if(*n == 2 || *n == 3 || *n == 5)
139             count++;
140         else RUNNER_ASSERT_MSG(false, "Wrong plugin handle");
141     }
142
143     RUNNER_ASSERT(count == 3);
144 }
145
146 /*
147  * Name: plugin_dao_test_get_implemented_objects
148  * Description: test of returning plugin handle list
149  * Expected: -
150  */
151 RUNNER_TEST(plugin_dao_test_get_implemented_objects)
152 {
153     ImplementedObjectsList handles = PluginDAOReadOnly::getImplementedObjects();
154     RUNNER_ASSERT(handles.size() == 6);
155
156     int count = 0;
157     FOREACH(n, handles){
158         if(*n == "" || *n == "Plugin_3_Object_A" || *n == "Plugin_4_Object_A" ||
159                 *n == "Plugin_4_Object_B" || *n == "Plugin_4_Object_C" || *n == "Plugin_5_Object_A")
160             count++;
161         else RUNNER_ASSERT_MSG(false, "Wrong implemented object");
162     }
163
164     RUNNER_ASSERT(count == 6);
165 }
166
167 /*
168  * Name: plugin_dao_test_register_plugin_implemented_object
169  * Description: registers dependecies for plugins and checks if they were saved
170  * Expected: registered dependecies should be returned from database
171  */
172 RUNNER_TEST(plugin_dao_test_register_library_dependencies)
173 {
174     {
175         std::string libraryPath("np4 lib_path");
176         std::string libraryName("np4");
177
178         PluginMetafileData pluginData;
179         pluginData.m_libraryName = libraryName;
180
181         PluginHandle handle =
182             PluginDAO::registerPlugin(pluginData, libraryPath);
183         PluginDAO::setPluginInstallationStatus(
184             handle,
185             PluginDAO::
186                 INSTALLATION_COMPLETED);
187         RUNNER_ASSERT(PluginDAO::isPluginInstalled(libraryName) == true);
188
189         PluginHandle depHandles[] = { 117, 119 };
190
191         PluginHandleSetPtr dependencies(new PluginHandleSet);
192         dependencies->insert(depHandles[0]);
193         dependencies->insert(depHandles[1]);
194
195         PluginDAO::registerPluginLibrariesDependencies(handle, dependencies);
196
197         PluginDAO dao(handle);
198         PluginHandleSetPtr retDependencies;
199         retDependencies = dao.getLibraryDependencies();
200
201         RUNNER_ASSERT(
202             retDependencies->size() == sizeof(depHandles) /
203             sizeof(depHandles[0]));
204         RUNNER_ASSERT(
205             retDependencies->find(depHandles[0]) != retDependencies->end());
206         RUNNER_ASSERT(
207             retDependencies->find(depHandles[1]) != retDependencies->end());
208     }
209 }
210
211 /*
212  * Name: plugin_dao_test_register_required_object
213  * Description: registers required plugin objects for plugins and checks if they
214  * were saved
215  * Expected: registered required plugin objects should be returned from database
216  */
217 RUNNER_TEST(plugin_dao_test_register_required_object)
218 {
219     {
220         std::string libraryPath("np5 lib_path");
221         std::string libraryName("np5");
222
223         PluginMetafileData pluginData;
224         pluginData.m_libraryName = libraryName;
225
226         PluginHandle handle =
227             PluginDAO::registerPlugin(pluginData, libraryPath);
228         PluginDAO::setPluginInstallationStatus(
229             handle,
230             PluginDAO::
231                 INSTALLATION_COMPLETED);
232         RUNNER_ASSERT(PluginDAO::isPluginInstalled(libraryName) == true);
233
234         const size_t numObjects = 2;
235         std::string objectReq[numObjects];
236         objectReq[0] = std::string("object1.req");
237         objectReq[1] = std::string("object2.req");
238         PluginDAO::registerPluginRequiredObject(objectReq[0], handle);
239         PluginDAO::registerPluginRequiredObject(objectReq[1], handle);
240
241         WrtDB::PluginObjectsDAO::ObjectsPtr objects =
242             PluginDAO::getRequiredObjectsForPluginHandle(handle);
243
244         RUNNER_ASSERT(objects->size() == numObjects
245                       && objects->find(objectReq[0]) != objects->end()
246                       && objects->find(objectReq[1]) != objects->end());
247     }
248 }
249
250 /*
251  * Name: plugin_dao_test_is_library_installed
252  * Description: tests if plugin isntallation/registrartion works
253  * Expected: only registered plugins should be reported as installed
254  */
255 RUNNER_TEST(plugin_dao_test_is_library_installed)
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         RUNNER_ASSERT_MSG(PluginDAO::isPluginInstalled(*it),
268                           std::string("Not found: ") + *it);
269     }
270
271     {
272         //does not exist
273         RUNNER_ASSERT_MSG(
274             PluginDAO::isPluginInstalled("not_installed1") == false,
275             "Found not_installed1");
276         RUNNER_ASSERT_MSG(PluginDAO::isPluginInstalled("p 4") == false,
277                           "Found p 4");
278         RUNNER_ASSERT_MSG(PluginDAO::isPluginInstalled("") == false,
279                           "Found <empty>");
280         RUNNER_ASSERT_MSG(PluginDAO::isPluginInstalled("p33") == false,
281                           "Found p33");
282         RUNNER_ASSERT_MSG(PluginDAO::isPluginInstalled("feature1") == false,
283                           "Found feature1");
284     }
285 }
286
287 /*
288  * Name: plugin_dao_test_get_plugin_handle_list
289  * Description: test of returning plugin handle list
290  * Expected: returned list should be no less than number of registered plugins
291  */
292 RUNNER_TEST(plugin_dao_test_get_plugin_handle_list)
293 {
294     PluginHandleList handles = PluginDAO::getPluginHandleList();
295     RUNNER_ASSERT(handles.size() >= 5);
296 }
297
298 /*
299  * Name: plugin_dao_test_constructor_name
300  * Description: tests construction of plugin dao based on plugin name
301  * Expected: Instance of dao should be constructed only
302  * if there is given plugin in database
303  */
304 RUNNER_TEST(plugin_dao_test_constructor_name)
305 {
306     {
307         //exist
308         std::list<const char *> preinstalled;
309         preinstalled.push_back("plugin1");
310         preinstalled.push_back("plugin2");
311         preinstalled.push_back("plugin3");
312         preinstalled.push_back("p4");
313         preinstalled.push_back("p5");
314
315         FOREACH(it, preinstalled)
316         {
317             PluginDAO dao(*it);
318             RUNNER_ASSERT_WHAT_EQUALS(dao.getLibraryName(), *it);
319         }
320     }
321
322     {
323         //does not exist
324         std::list<const char *> not_installed;
325         not_installed.push_back("plugin 1");
326         not_installed.push_back("");
327         not_installed.push_back("p 3");
328
329         FOREACH(it, not_installed)
330         {
331             Try {
332                 //Plugin not exist
333                 PluginDAO dao(*it);
334                 RUNNER_ASSERT_MSG(false, "should not be found");
335             }
336             Catch(PluginDAO::Exception::PluginNotExist) {
337                 continue;
338             }
339         }
340     }
341 }
342
343 /*
344  * Name: plugin_dao_test_get_plugin_properties
345  * Description: tests reading plugin properties from database
346  * Expected: Data, inserted into database, should be accessible via dao
347  */
348 RUNNER_TEST(plugin_dao_test_get_plugin_properties)
349 {
350     {
351         PluginDAO dao("p4");
352         RUNNER_ASSERT(dao.getPluginHandle() == 4);
353         RUNNER_ASSERT_WHAT_EQUALS(dao.getLibraryName(), "p4");
354         RUNNER_ASSERT_WHAT_EQUALS(dao.getLibraryPath(), "path_to_p4");
355     }
356
357     {
358         PluginDAO dao(5);
359         RUNNER_ASSERT(dao.getPluginHandle() == 5);
360         RUNNER_ASSERT_WHAT_EQUALS(dao.getLibraryName(), "p5");
361         RUNNER_ASSERT_WHAT_EQUALS(dao.getLibraryPath(), "path_to_p5");
362     }
363
364     {
365         PluginDAO dao(2);
366         RUNNER_ASSERT(dao.getPluginHandle() == 2);
367         RUNNER_ASSERT_WHAT_EQUALS(dao.getLibraryName(), "plugin2");
368         RUNNER_ASSERT_WHAT_EQUALS(dao.getLibraryPath(), "path_to_plugin2");
369     }
370
371     {
372         PluginDAO dao(1);
373         RUNNER_ASSERT(dao.getPluginHandle() == 1);
374         RUNNER_ASSERT_WHAT_EQUALS(dao.getLibraryName(), "plugin1");
375         RUNNER_ASSERT_WHAT_EQUALS(dao.getLibraryPath(), "");
376     }
377 }
378
379 /*
380  * Name: plugin_dao_test_get_implemented_objects_for_plugin_handle_1
381  * Description: tests receiving from dao Implemented Objects
382  * Expected: returned object is size 0
383  */
384 RUNNER_TEST(plugin_dao_test_get_implemented_objects_for_plugin_handle_1)
385 {
386     {
387         const int handle = 1;
388         PluginDAOReadOnly dao(handle);
389         auto dbHandle = dao.getPluginHandle();
390         RUNNER_ASSERT(dbHandle == handle);
391         auto objects = dao.getImplementedObjectsForPluginHandle(dbHandle);
392
393         RUNNER_ASSERT(objects.empty());
394     }
395 }
396
397 /*
398  * Name: plugin_dao_test_get_implemented_objects_for_plugin_handle_2
399  * Description: tests receiving from dao Implemented Objects
400  * Expected: returned object is size as it was inserted
401  */
402 RUNNER_TEST(plugin_dao_test_get_implemented_objects_for_plugin_handle_2)
403 {
404     {
405         std::set< std::string > preinstalled = {
406             ""
407         };
408
409         PluginDAOReadOnly dao(2);
410         auto dbHandle = dao.getPluginHandle();
411         auto objects = dao.getImplementedObjectsForPluginHandle(dbHandle);
412
413         //LogError("\n" << objects.size() << " " << preinstalled.size() <<
414         // "\n");
415
416         RUNNER_ASSERT(objects.size() == preinstalled.size());
417
418         FOREACH(dbObject, objects)
419         {
420             RUNNER_ASSERT(preinstalled.find(*dbObject) != preinstalled.end());
421         }
422     }
423 }
424
425 /*
426  * Name: plugin_dao_test_get_implemented_objects_for_plugin_handle_3
427  * Description: tests receiving from dao Implemented Objects
428  * Expected: returned objects list has preinserted object
429  */
430 RUNNER_TEST(plugin_dao_test_get_implemented_objects_for_plugin_handle_3)
431 {
432     {
433         std::set< std::string > preinstalled = {
434             "Plugin_3_Object_A"
435         };
436
437         PluginDAOReadOnly dao(3);
438         auto dbHandle = dao.getPluginHandle();
439         auto objects = dao.getImplementedObjectsForPluginHandle(dbHandle);
440         RUNNER_ASSERT(objects.size() == preinstalled.size());
441
442         FOREACH(dbObject, objects)
443         {
444             RUNNER_ASSERT(preinstalled.find(*dbObject) != preinstalled.end());
445         }
446     }
447 }
448
449 /*
450  * Name: plugin_dao_test_get_implemented_objects_for_plugin_handle_4
451  * Description: tests receiving from dao Implemented Objects
452  * Expected: returned objects list has all preinserted objects
453  */
454 RUNNER_TEST(plugin_dao_test_get_implemented_objects_for_plugin_handle_4)
455 {
456     {
457         std::set< std::string > preinstalled = {
458             "Plugin_4_Object_A",
459             "Plugin_4_Object_B",
460             "Plugin_4_Object_C",
461         };
462
463         PluginDAOReadOnly dao(4);
464         auto dbHandle = dao.getPluginHandle();
465         auto objects = dao.getImplementedObjectsForPluginHandle(dbHandle);
466         RUNNER_ASSERT(objects.size() == preinstalled.size());
467
468         FOREACH(dbObject, objects)
469         {
470             RUNNER_ASSERT(preinstalled.find(*dbObject) != preinstalled.end());
471         }
472     }
473 }
474
475 /*
476  * Name: plugin_dao_test_get_implemented_objects_for_plugin_handle_5
477  * Description: tests receiving from dao Implemented Objects
478  * Expected: returned objects list do not have object that was not inserted
479  */
480 RUNNER_TEST(plugin_dao_test_get_implemented_objects_for_plugin_handle_5)
481 {
482     {
483         std::set< std::string > preinstalled = {
484             "Plugin_5_Object_B",
485         };
486
487         PluginDAOReadOnly dao(5);
488         auto dbHandle = dao.getPluginHandle();
489         auto objects = dao.getImplementedObjectsForPluginHandle(dbHandle);
490         RUNNER_ASSERT(objects.size() == preinstalled.size());
491
492         FOREACH(dbObject, objects)
493         {
494             RUNNER_ASSERT(preinstalled.find(*dbObject) == preinstalled.end());
495         }
496     }
497 }
498
499 #undef RUNNER_ASSERT_WHAT_EQUALS