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