Update container and build script for rearrangement of headers
[platform/upstream/iotivity.git] / service / resource-manipulation / src / resourceContainer / unittests / ResourceContainerTest.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <string>
22 #include <map>
23 #include <vector>
24
25 #include <gtest/gtest.h>
26 #include <HippoMocks/hippomocks.h>
27
28 #include "Configuration.h"
29 #include "BundleResource.h"
30 #include "ResourceContainer.h"
31 #include "ResourceContainerBundleAPI.h"
32 #include "ResourceContainerImpl.h"
33
34 #include "ResourceObject.h"
35
36 using namespace std;
37 using namespace testing;
38 using namespace OIC::Service;
39
40 string CONFIG_FILE = "ResourceContainerTestConfig.xml";
41
42 /*Fake bundle resource class for testing*/
43 class TestBundleResource: public BundleResource
44 {
45     public:
46         string getAttribute(string attributeName)
47         {
48             return "test";
49         }
50         ;
51         void setAttribute(string attributeName, string value)
52         {
53         }
54         ;
55         void initAttributes()
56         {
57             BundleResource::setAttribute("attri", "test");
58         }
59         ;
60 };
61
62 class ResourceContainerTest: public Test
63 {
64     public:
65         ResourceContainer *m_pResourceContainer;
66
67     protected:
68         void SetUp() override
69         {
70             m_pResourceContainer = ResourceContainer::getInstance();
71         }
72 };
73
74 TEST_F(ResourceContainerTest, BundleRegisteredWhenContainerStartedWithValidConfigFile)
75 {
76     m_pResourceContainer->startContainer(CONFIG_FILE);
77
78     EXPECT_GT(m_pResourceContainer->listBundles().size(), (unsigned int) 0);
79     EXPECT_STREQ("oic.bundle.test",
80                  (*m_pResourceContainer->listBundles().begin())->getID().c_str());
81     EXPECT_STREQ("libTestBundle.so",
82                  (*m_pResourceContainer->listBundles().begin())->getPath().c_str());
83     EXPECT_STREQ("1.0.0", (*m_pResourceContainer->listBundles().begin())->getVersion().c_str());
84
85     m_pResourceContainer->stopContainer();
86 }
87
88 TEST_F(ResourceContainerTest, BundleLoadedWhenContainerStartedWithValidConfigFile)
89 {
90     m_pResourceContainer->startContainer(CONFIG_FILE);
91
92     EXPECT_GT(m_pResourceContainer->listBundles().size(), (unsigned int) 0);
93     EXPECT_TRUE(((BundleInfoInternal *)(*m_pResourceContainer->listBundles().begin()))->isLoaded());
94     EXPECT_NE(nullptr,
95               ((BundleInfoInternal *)( *m_pResourceContainer->listBundles().begin()))->getBundleHandle());
96
97     m_pResourceContainer->stopContainer();
98 }
99
100 TEST_F(ResourceContainerTest, BundleActivatedWhenContainerStartedWithValidConfigFile)
101 {
102     m_pResourceContainer->startContainer(CONFIG_FILE);
103
104     EXPECT_GT(m_pResourceContainer->listBundles().size(), (unsigned int) 0);
105     EXPECT_TRUE(
106         ((BundleInfoInternal *)(*m_pResourceContainer->listBundles().begin()))->isActivated());
107     EXPECT_NE(nullptr,
108               ((BundleInfoInternal *)( *m_pResourceContainer->listBundles().begin()))->getBundleActivator());
109
110     m_pResourceContainer->stopContainer();
111 }
112
113 TEST_F(ResourceContainerTest, BundleNotRegisteredWhenContainerStartedWithInvalidConfigFile)
114 {
115     m_pResourceContainer->startContainer("invalideConfig");
116
117     EXPECT_EQ((unsigned int) 0, m_pResourceContainer->listBundles().size());
118 }
119
120 TEST_F(ResourceContainerTest, BundleUnregisteredWhenContainerStopped)
121 {
122     m_pResourceContainer->startContainer(CONFIG_FILE);
123     m_pResourceContainer->stopContainer();
124
125     EXPECT_EQ((unsigned int) 0, m_pResourceContainer->listBundles().size());
126 }
127
128 TEST_F(ResourceContainerTest, BundleStoppedWithStartBundleAPI)
129 {
130     m_pResourceContainer->startContainer(CONFIG_FILE);
131     m_pResourceContainer->stopBundle("oic.bundle.test");
132
133     EXPECT_FALSE(
134         ((BundleInfoInternal *)(*m_pResourceContainer->listBundles().begin()))->isActivated());
135 }
136
137 TEST_F(ResourceContainerTest, BundleStartedWithStartBundleAPI)
138 {
139     m_pResourceContainer->startContainer(CONFIG_FILE);
140     m_pResourceContainer->stopBundle("oic.bundle.test");
141     m_pResourceContainer->startBundle("oic.bundle.test");
142
143     EXPECT_TRUE(
144         ((BundleInfoInternal *)(*m_pResourceContainer->listBundles().begin()))->isActivated());
145 }
146
147 class ResourceContainerBundleAPITest: public Test
148 {
149     public:
150         MockRepository mocks;
151         ResourceObject *m_pResourceObject;
152         ResourceContainerBundleAPI *m_pResourceContainer;
153         TestBundleResource *m_pBundleResource;
154
155     protected:
156         void SetUp() override
157         {
158             m_pResourceObject = mocks.Mock<ResourceObject>();
159             m_pResourceContainer = ResourceContainerBundleAPI::getInstance();
160
161             m_pBundleResource = new TestBundleResource();
162             m_pBundleResource->m_bundleId = "oic.bundle.test";
163             m_pBundleResource->m_uri = "/test_resource";
164             m_pBundleResource->m_resourceType = "oic.test";
165         }
166 };
167
168 TEST_F(ResourceContainerBundleAPITest, ResourceServerCreatedWhenRegisterResourceCalled)
169 {
170     mocks.ExpectCallFunc(ResourceContainerImpl::buildResourceObject).With(m_pBundleResource->m_uri,
171             m_pBundleResource->m_resourceType).Return(nullptr);
172
173     m_pResourceContainer->registerResource(m_pBundleResource);
174 }
175
176 TEST_F(ResourceContainerBundleAPITest, RequestHandlerForResourceServerSetWhenRegisterResourceCalled)
177 {
178     mocks.OnCallFunc(ResourceContainerImpl::buildResourceObject).Return(
179         ResourceObject::Ptr(m_pResourceObject, [](ResourceObject *)
180     {}));
181
182     mocks.ExpectCall(m_pResourceObject, ResourceObject::setGetRequestHandler);
183     mocks.ExpectCall(m_pResourceObject, ResourceObject::setSetRequestHandler);
184
185     m_pResourceContainer->registerResource(m_pBundleResource);
186 }
187
188 TEST_F(ResourceContainerBundleAPITest, BundleResourceUnregisteredWhenUnregisterResourceCalled)
189 {
190     mocks.OnCallFunc(ResourceContainerImpl::buildResourceObject).Return(
191         ResourceObject::Ptr(m_pResourceObject, [](ResourceObject *)
192     {}));
193
194     mocks.ExpectCall(m_pResourceObject, ResourceObject::setGetRequestHandler);
195     mocks.ExpectCall(m_pResourceObject, ResourceObject::setSetRequestHandler);
196
197     m_pResourceContainer->registerResource(m_pBundleResource);
198     m_pResourceContainer->unregisterResource(m_pBundleResource);
199
200     EXPECT_EQ((unsigned int) 0,
201               ((ResourceContainerImpl *)m_pResourceContainer)->listBundleResources(
202                   m_pBundleResource->m_bundleId).size());
203 }
204
205 TEST_F(ResourceContainerBundleAPITest,
206        ServerNotifiesToObserversWhenNotificationReceivedFromResource)
207 {
208     mocks.OnCallFunc(ResourceContainerImpl::buildResourceObject).Return(
209         ResourceObject::Ptr(m_pResourceObject, [](ResourceObject *)
210     {}));
211
212     mocks.ExpectCall(m_pResourceObject, ResourceObject::setGetRequestHandler);
213     mocks.ExpectCall(m_pResourceObject, ResourceObject::setSetRequestHandler);
214
215     m_pResourceContainer->registerResource(m_pBundleResource);
216
217     mocks.ExpectCall(m_pResourceObject, ResourceObject::notify);
218
219     m_pResourceContainer->onNotificationReceived(m_pBundleResource->m_uri);
220 }
221
222 TEST_F(ResourceContainerBundleAPITest, BundleConfigurationParsedWithValidBundleId)
223 {
224     configInfo bundle;
225     map< string, string > results;
226
227     m_pResourceContainer->getBundleConfiguration("oic.bundle.test", &bundle);
228
229     results = *bundle.begin();
230
231     EXPECT_STREQ("oic.bundle.test", results["id"].c_str());
232     EXPECT_STREQ("libTestBundle.so", results["path"].c_str());
233     EXPECT_STREQ("1.0.0", results["version"].c_str());
234 }
235
236 TEST_F(ResourceContainerBundleAPITest, BundleResourceConfigurationListParsed)
237 {
238     vector< resourceInfo > resourceConfig;
239     resourceInfo result;
240
241     m_pResourceContainer->getResourceConfiguration("oic.bundle.test", &resourceConfig);
242
243     result = *resourceConfig.begin();
244
245     EXPECT_STREQ("test_resource", result.name.c_str());
246     EXPECT_STREQ("oic.test", result.resourceType.c_str());
247 }
248
249 class ResourceContainerImplTest: public Test
250 {
251     public:
252         MockRepository mocks;
253         ResourceContainerImpl *m_pResourceContainer;
254         BundleInfo *m_pBundleInfo;
255
256     protected:
257         void SetUp() override
258         {
259             m_pResourceContainer = ResourceContainerImpl::getImplInstance();
260             m_pBundleInfo = BundleInfo::build();
261         }
262 };
263
264 TEST_F(ResourceContainerImplTest, SoBundleLoadedWhenRegisteredWithRegisterBundleAPI)
265 {
266     m_pBundleInfo->setPath("libTestBundle.so");
267     m_pBundleInfo->setVersion("1.0");
268     m_pBundleInfo->setLibraryPath(".");
269     m_pBundleInfo->setID("oic.bundle.test");
270
271     m_pResourceContainer->registerBundle(m_pBundleInfo);
272
273     EXPECT_NE(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleHandle());
274 }
275
276 TEST_F(ResourceContainerImplTest, JavaBundleLoadedWhenRegisteredWithRegisterBundleAPIWrongPath)
277 {
278     m_pBundleInfo->setPath("wrong_path.jar");
279     m_pBundleInfo->setActivatorName("org/iotivity/bundle/hue/HueBundleActivator");
280     m_pBundleInfo->setLibraryPath("../.");
281     m_pBundleInfo->setVersion("1.0");
282     m_pBundleInfo->setID("oic.bundle.java.test");
283
284     m_pResourceContainer->registerBundle(m_pBundleInfo);
285     EXPECT_FALSE(((BundleInfoInternal *)m_pBundleInfo)->isLoaded());
286 }
287
288 TEST_F(ResourceContainerImplTest, JavaBundleTest)
289 {
290     m_pBundleInfo->setPath("TestBundleJava/hue-0.1-jar-with-dependencies.jar");
291     m_pBundleInfo->setActivatorName("org/iotivity/bundle/hue/HueBundleActivator");
292     m_pBundleInfo->setLibraryPath("../.");
293     m_pBundleInfo->setVersion("1.0");
294     m_pBundleInfo->setID("oic.bundle.java.test");
295
296     m_pResourceContainer->registerBundle(m_pBundleInfo);
297     EXPECT_TRUE(((BundleInfoInternal *)m_pBundleInfo)->isLoaded());
298
299     m_pResourceContainer->activateBundle(m_pBundleInfo);
300     EXPECT_TRUE(((BundleInfoInternal *) m_pBundleInfo)->isActivated());
301
302     m_pResourceContainer->deactivateBundle(m_pBundleInfo);
303     EXPECT_FALSE(((BundleInfoInternal *) m_pBundleInfo)->isActivated());
304 }
305
306 TEST_F(ResourceContainerImplTest, BundleNotRegisteredIfBundlePathIsInvalid)
307 {
308     m_pBundleInfo->setPath("");
309     m_pBundleInfo->setVersion("1.0");
310     m_pBundleInfo->setLibraryPath("../.");
311     m_pBundleInfo->setID("oic.bundle.test");
312
313     m_pResourceContainer->registerBundle(m_pBundleInfo);
314
315     EXPECT_EQ(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleHandle());
316
317 }
318
319 TEST_F(ResourceContainerImplTest, SoBundleActivatedWithValidBundleInfo)
320 {
321     m_pBundleInfo->setPath("libTestBundle.so");
322     m_pBundleInfo->setVersion("1.0");
323     m_pBundleInfo->setLibraryPath("../.");
324     m_pBundleInfo->setID("oic.bundle.test");
325
326     m_pResourceContainer->registerBundle(m_pBundleInfo);
327     m_pResourceContainer->activateBundle(m_pBundleInfo);
328
329     EXPECT_NE(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleActivator());
330 }
331
332 /*TEST_F(ResourceContainerImplTest, JavaBundleActivatedWithValidBundleInfo)
333 {
334     m_pBundleInfo->setPath("TestBundleJava/hue-0.1-jar-with-dependencies.jar");
335     m_pBundleInfo->setActivatorName("org/iotivity/bundle/hue/HueBundleActivator");
336     m_pBundleInfo->setLibraryPath("../.");
337     m_pBundleInfo->setVersion("1.0");
338     m_pBundleInfo->setID("oic.bundle.java.test2");
339
340     m_pResourceContainer->registerBundle(m_pBundleInfo);
341     m_pResourceContainer->activateBundle(m_pBundleInfo);
342     EXPECT_TRUE(((BundleInfoInternal *) m_pBundleInfo)->isActivated());
343
344 }*/
345
346 TEST_F(ResourceContainerImplTest, BundleNotActivatedWhenNotRegistered)
347 {
348     m_pBundleInfo->setPath("libTestBundle.so");
349     m_pBundleInfo->setVersion("1.0");
350     m_pBundleInfo->setLibraryPath("../.");
351     m_pBundleInfo->setID("oic.bundle.test");
352
353     m_pResourceContainer->activateBundle(m_pBundleInfo);
354
355     EXPECT_EQ(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleActivator());
356 }
357
358 TEST_F(ResourceContainerImplTest, SoBundleActivatedWithBundleID)
359 {
360     m_pBundleInfo->setPath("libTestBundle.so");
361     m_pBundleInfo->setVersion("1.0");
362     m_pBundleInfo->setLibraryPath("../.");
363     m_pBundleInfo->setID("oic.bundle.test");
364
365     m_pResourceContainer->registerBundle(m_pBundleInfo);
366     m_pResourceContainer->activateBundle(m_pBundleInfo->getID());
367
368     EXPECT_NE(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleActivator());
369     EXPECT_TRUE(((BundleInfoInternal *)m_pBundleInfo)->isActivated());
370 }
371
372 TEST_F(ResourceContainerImplTest, BundleDeactivatedWithBundleInfo)
373 {
374     m_pBundleInfo->setPath("libTestBundle.so");
375     m_pBundleInfo->setVersion("1.0");
376     m_pBundleInfo->setLibraryPath("../.");
377     m_pBundleInfo->setID("oic.bundle.test");
378
379     m_pResourceContainer->registerBundle(m_pBundleInfo);
380     m_pResourceContainer->activateBundle(m_pBundleInfo);
381     m_pResourceContainer->deactivateBundle(m_pBundleInfo);
382
383     EXPECT_NE(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleDeactivator());
384     EXPECT_FALSE(((BundleInfoInternal *)m_pBundleInfo)->isActivated());
385 }
386
387 TEST_F(ResourceContainerImplTest, BundleDeactivatedWithBundleInfoJava)
388 {
389     m_pBundleInfo->setPath("TestBundle/hue-0.1-jar-with-dependencies.jar");
390     m_pBundleInfo->setActivatorName("org/iotivity/bundle/hue/HueBundleActivator");
391     m_pBundleInfo->setLibraryPath("../.");
392     m_pBundleInfo->setVersion("1.0");
393     m_pBundleInfo->setID("oic.bundle.java.test");
394
395     m_pResourceContainer->registerBundle(m_pBundleInfo);
396     m_pResourceContainer->activateBundle(m_pBundleInfo);
397     m_pResourceContainer->deactivateBundle(m_pBundleInfo);
398     EXPECT_FALSE(((BundleInfoInternal *) m_pBundleInfo)->isActivated());
399 }
400
401 TEST_F(ResourceContainerImplTest, SoBundleDeactivatedWithBundleID)
402 {
403     m_pBundleInfo->setPath("libTestBundle.so");
404     m_pBundleInfo->setVersion("1.0");
405     m_pBundleInfo->setLibraryPath("../.");
406     m_pBundleInfo->setID("oic.bundle.test");
407
408     m_pResourceContainer->registerBundle(m_pBundleInfo);
409     m_pResourceContainer->activateBundle(m_pBundleInfo);
410
411     m_pResourceContainer->deactivateBundle(m_pBundleInfo->getID());
412
413     EXPECT_FALSE(((BundleInfoInternal *)m_pBundleInfo)->isActivated());
414 }
415
416 //TEST_F(ResourceContainerImplTest, JavaBundleDeactivatedWithBundleID)
417 //{
418 //}
419
420 /* Test for Configuration */
421 TEST(ConfigurationTest, ConfigFileLoadedWithValidPath)
422 {
423     Configuration *config = new Configuration(CONFIG_FILE);
424
425     EXPECT_TRUE(config->isLoaded());
426 }
427
428 TEST(ConfigurationTest, ConfigFileNotLoadedWithInvalidPath)
429 {
430     Configuration *config = new Configuration("InvalidPath");
431
432     EXPECT_FALSE(config->isLoaded());
433 }
434
435 TEST(ConfigurationTest, BundleConfigurationListParsed)
436 {
437     Configuration *config = new Configuration(CONFIG_FILE);
438
439     configInfo bundles;
440     map< string, string > results;
441
442     config->getConfiguredBundles(&bundles);
443
444     results = *bundles.begin();
445
446     EXPECT_STREQ("oic.bundle.test", results["id"].c_str());
447     EXPECT_STREQ("libTestBundle.so", results["path"].c_str());
448     EXPECT_STREQ("1.0.0", results["version"].c_str());
449 }
450
451 TEST(ConfigurationTest, BundleConfigurationParsedWithValidBundleId)
452 {
453     Configuration *config = new Configuration(CONFIG_FILE);
454
455     configInfo bundle;
456     map< string, string > results;
457
458     config->getBundleConfiguration("oic.bundle.test", &bundle);
459
460     results = *bundle.begin();
461
462     EXPECT_STREQ("oic.bundle.test", results["id"].c_str());
463     EXPECT_STREQ("libTestBundle.so", results["path"].c_str());
464     EXPECT_STREQ("1.0.0", results["version"].c_str());
465 }
466
467 TEST(ConfigurationTest, BundleConfigurationNotParsedWithInvalidBundleId)
468 {
469     Configuration *config = new Configuration(CONFIG_FILE);
470
471     configInfo bundles;
472     config->getBundleConfiguration("test", &bundles);
473
474     EXPECT_TRUE(bundles.empty());
475 }
476
477 TEST(ConfigurationTest, BundleResourceConfigurationListParsed)
478 {
479     Configuration *config = new Configuration(CONFIG_FILE);
480
481     vector< resourceInfo > resourceConfig;
482     resourceInfo result;
483
484     config->getResourceConfiguration("oic.bundle.test", &resourceConfig);
485
486     result = *resourceConfig.begin();
487
488     EXPECT_STREQ("test_resource", result.name.c_str());
489     EXPECT_STREQ("oic.test", result.resourceType.c_str());
490 }
491
492 TEST(ConfigurationTest, BundleResourceConfigurationNotParsedWithInvalidBundleId)
493 {
494     Configuration *config = new Configuration(CONFIG_FILE);
495
496     configInfo bundles;
497     vector< resourceInfo > resourceConfig;
498     config->getResourceConfiguration("test", &resourceConfig);
499
500     EXPECT_TRUE(bundles.empty());
501 }