1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #if defined(__linux__)
29 #include <UnitTestHelper.h>
31 #include <gtest/gtest.h>
32 #include <HippoMocks/hippomocks.h>
34 #include "Configuration.h"
35 #include "BundleActivator.h"
36 #include "BundleResource.h"
37 #include "RCSResourceContainer.h"
38 #include "ResourceContainerBundleAPI.h"
39 #include "ResourceContainerImpl.h"
41 #include "RCSResourceObject.h"
44 using namespace testing;
45 using namespace OIC::Service;
49 string CONFIG_FILE = "ResourceContainerTestConfig.xml";
51 void getCurrentPath(std::string *pPath)
53 char buffer[MAX_PATH];
55 #if defined(__linux__)
57 int length = readlink("/proc/self/exe", buffer, MAX_PATH - 1);
61 buffer[length] = '\0';
62 strPath = strrchr(buffer, '/');
68 pPath->append(buffer);
71 /*Fake bundle resource class for testing*/
72 class TestBundleResource: public BundleResource
75 string getAttribute(string attributeName)
81 void setAttribute(string attributeName, string value)
89 BundleResource::setAttribute("attri", "test");
94 class ResourceContainerTest: public TestWithMock
98 RCSResourceContainer *m_pResourceContainer;
99 std::string m_strConfigPath;
104 TestWithMock::SetUp();
105 m_pResourceContainer = RCSResourceContainer::getInstance();
106 getCurrentPath(&m_strConfigPath);
107 m_strConfigPath.append("/");
108 m_strConfigPath.append(CONFIG_FILE);
112 TEST_F(ResourceContainerTest, BundleRegisteredWhenContainerStartedWithValidConfigFile)
114 m_pResourceContainer->startContainer(m_strConfigPath);
115 EXPECT_GT(m_pResourceContainer->listBundles().size(), (unsigned int) 0);
116 EXPECT_STREQ("oic.bundle.test",
117 (*m_pResourceContainer->listBundles().begin())->getID().c_str());
118 EXPECT_STREQ("libTestBundle.so",
119 (*m_pResourceContainer->listBundles().begin())->getPath().c_str());
120 EXPECT_STREQ("1.0.0", (*m_pResourceContainer->listBundles().begin())->getVersion().c_str());
122 m_pResourceContainer->stopContainer();
125 TEST_F(ResourceContainerTest, BundleLoadedWhenContainerStartedWithValidConfigFile)
127 m_pResourceContainer->startContainer(m_strConfigPath);
129 EXPECT_GT(m_pResourceContainer->listBundles().size(), (unsigned int) 0);
130 EXPECT_TRUE(((BundleInfoInternal *)(*m_pResourceContainer->listBundles().begin()))->isLoaded());
132 ((BundleInfoInternal *)( *m_pResourceContainer->listBundles().begin()))->getBundleHandle());
134 m_pResourceContainer->stopContainer();
137 TEST_F(ResourceContainerTest, BundleActivatedWhenContainerStartedWithValidConfigFile)
139 m_pResourceContainer->startContainer(m_strConfigPath);
141 EXPECT_GT(m_pResourceContainer->listBundles().size(), (unsigned int) 0);
143 ((BundleInfoInternal *)(*m_pResourceContainer->listBundles().begin()))->isActivated());
145 ((BundleInfoInternal *)( *m_pResourceContainer->listBundles().begin()))->getBundleActivator());
147 m_pResourceContainer->stopContainer();
150 TEST_F(ResourceContainerTest, BundleNotRegisteredWhenContainerStartedWithInvalidConfigFile)
152 m_pResourceContainer->startContainer("invalidConfig");
154 EXPECT_EQ((unsigned int) 0, m_pResourceContainer->listBundles().size());
157 TEST_F(ResourceContainerTest, BundleNotRegisteredWhenContainerStartedWithEmptyConfigFile)
159 m_pResourceContainer->startContainer("");
161 EXPECT_EQ((unsigned int) 0, m_pResourceContainer->listBundles().size());
164 TEST_F(ResourceContainerTest, BundleUnregisteredWhenContainerStopped)
166 m_pResourceContainer->startContainer(m_strConfigPath);
167 m_pResourceContainer->stopContainer();
169 EXPECT_EQ((unsigned int) 0, m_pResourceContainer->listBundles().size());
172 TEST_F(ResourceContainerTest, BundleStoppedWithStartBundleAPI)
174 m_pResourceContainer->startContainer(m_strConfigPath);
175 m_pResourceContainer->stopBundle("oic.bundle.test");
178 ((BundleInfoInternal *)(*m_pResourceContainer->listBundles().begin()))->isActivated());
180 m_pResourceContainer->stopContainer();
183 TEST_F(ResourceContainerTest, BundleStartedWithStartBundleAPI)
185 m_pResourceContainer->startContainer(m_strConfigPath);
186 m_pResourceContainer->stopBundle("oic.bundle.test");
187 m_pResourceContainer->startBundle("oic.bundle.test");
190 ((BundleInfoInternal *)(*m_pResourceContainer->listBundles().begin()))->isActivated());
192 m_pResourceContainer->stopContainer();
195 TEST_F(ResourceContainerTest, AddNewSoBundleToContainer)
197 std::map<string, string> bundleParams;
198 std::list<RCSBundleInfo *> bundles;
200 bundles = m_pResourceContainer->listBundles();
201 m_pResourceContainer->addBundle("oic.bundle.test", "", "libTestBundle.so", bundleParams);
203 EXPECT_EQ(bundles.size() + 1, m_pResourceContainer->listBundles().size());
204 EXPECT_TRUE(((BundleInfoInternal *)(*m_pResourceContainer->listBundles().begin()))->isLoaded());
207 TEST_F(ResourceContainerTest, RemoveSoBundleFromContainer)
209 std::map<string, string> bundleParams;
210 std::list<RCSBundleInfo *> bundles;
212 bundles = m_pResourceContainer->listBundles();
213 m_pResourceContainer->removeBundle("oic.bundle.test");
215 EXPECT_EQ(bundles.size() - 1, m_pResourceContainer->listBundles().size());
218 TEST_F(ResourceContainerTest, AddBundleAlreadyRegistered)
220 std::map<string, string> bundleParams;
221 std::list<RCSBundleInfo *> bundles;
223 m_pResourceContainer->addBundle("oic.bundle.test", "", "libTestBundle.so", bundleParams);
224 bundles = m_pResourceContainer->listBundles();
225 m_pResourceContainer->addBundle("oic.bundle.test", "", "libTestBundle.so", bundleParams);
227 EXPECT_EQ(bundles.size(), m_pResourceContainer->listBundles().size());
230 TEST_F(ResourceContainerTest, AddAndRemoveSoBundleResource)
232 std::list<string> resources;
233 std::map<string, string> resourceParams;
234 resourceParams["resourceType"] = "oic.test";
236 m_pResourceContainer->startContainer(m_strConfigPath);
237 resources = m_pResourceContainer->listBundleResources("oic.bundle.test");
239 m_pResourceContainer->addResourceConfig("oic.bundle.test", "/test_resource", resourceParams);
241 EXPECT_EQ(resources.size() + 1,
242 m_pResourceContainer->listBundleResources("oic.bundle.test").size());
244 m_pResourceContainer->removeResourceConfig("oic.bundle.test", "/test_resource");
246 EXPECT_EQ(resources.size(), m_pResourceContainer->listBundleResources("oic.bundle.test").size());
248 m_pResourceContainer->stopContainer();
251 TEST_F(ResourceContainerTest, TryAddingSoBundleResourceToNotRegisteredBundle)
253 std::map<string, string> resourceParams;
255 mocks.NeverCallFunc(ResourceContainerImpl::buildResourceObject);
257 m_pResourceContainer->addResourceConfig("unvalidBundleId", "", resourceParams);
260 class ResourceContainerBundleAPITest: public TestWithMock
264 RCSResourceObject *m_pResourceObject;
265 ResourceContainerBundleAPI *m_pResourceContainer;
266 TestBundleResource *m_pBundleResource;
267 std::string m_strConfigPath;
272 TestWithMock::SetUp();
273 m_pResourceObject = mocks.Mock<RCSResourceObject>();
274 m_pResourceContainer = ResourceContainerBundleAPI::getInstance();
276 getCurrentPath(&m_strConfigPath);
277 m_strConfigPath.append("/");
278 m_strConfigPath.append(CONFIG_FILE);
280 m_pBundleResource = new TestBundleResource();
281 m_pBundleResource->m_bundleId = "oic.bundle.test";
282 m_pBundleResource->m_uri = "/test_resource";
283 m_pBundleResource->m_resourceType = "oic.test";
287 TEST_F(ResourceContainerBundleAPITest, ResourceServerCreatedWhenRegisterResourceCalled)
289 m_pBundleResource = new TestBundleResource();
290 m_pBundleResource->m_bundleId = "oic.bundle.test";
291 m_pBundleResource->m_uri = "/test_resource/test";
292 m_pBundleResource->m_resourceType = "oic.test";
294 mocks.ExpectCallFunc(ResourceContainerImpl::buildResourceObject).With(m_pBundleResource->m_uri,
295 m_pBundleResource->m_resourceType).Return(nullptr);
297 m_pResourceContainer->registerResource(m_pBundleResource);
300 TEST_F(ResourceContainerBundleAPITest, RequestHandlerForResourceServerSetWhenRegisterResourceCalled)
302 mocks.OnCallFunc(ResourceContainerImpl::buildResourceObject).Return(
303 RCSResourceObject::Ptr(m_pResourceObject, [](RCSResourceObject *)
306 mocks.ExpectCall(m_pResourceObject, RCSResourceObject::setGetRequestHandler);
307 mocks.ExpectCall(m_pResourceObject, RCSResourceObject::setSetRequestHandler);
309 m_pResourceContainer->registerResource(m_pBundleResource);
311 m_pResourceContainer->unregisterResource(m_pBundleResource);
314 TEST_F(ResourceContainerBundleAPITest, BundleResourceUnregisteredWhenUnregisterResourceCalled)
316 mocks.OnCallFunc(ResourceContainerImpl::buildResourceObject).Return(
317 RCSResourceObject::Ptr(m_pResourceObject, [](RCSResourceObject *)
320 mocks.ExpectCall(m_pResourceObject, RCSResourceObject::setGetRequestHandler);
321 mocks.ExpectCall(m_pResourceObject, RCSResourceObject::setSetRequestHandler);
323 m_pResourceContainer->registerResource(m_pBundleResource);
324 m_pResourceContainer->unregisterResource(m_pBundleResource);
326 EXPECT_EQ((unsigned int) 0,
327 ((ResourceContainerImpl *)m_pResourceContainer)->listBundleResources(
328 m_pBundleResource->m_bundleId).size());
331 TEST_F(ResourceContainerBundleAPITest,
332 ServerNotifiesToObserversWhenNotificationReceivedFromResource)
334 mocks.OnCallFunc(ResourceContainerImpl::buildResourceObject).Return(
335 RCSResourceObject::Ptr(m_pResourceObject, [](RCSResourceObject *)
338 mocks.ExpectCall(m_pResourceObject, RCSResourceObject::setGetRequestHandler);
339 mocks.ExpectCall(m_pResourceObject, RCSResourceObject::setSetRequestHandler);
341 m_pResourceContainer->registerResource(m_pBundleResource);
343 mocks.ExpectCall(m_pResourceObject, RCSResourceObject::notify);
345 m_pResourceContainer->onNotificationReceived(m_pBundleResource->m_uri);
347 m_pResourceContainer->unregisterResource(m_pBundleResource);
350 TEST_F(ResourceContainerBundleAPITest, BundleConfigurationParsedWithValidBundleId)
353 map< string, string > results;
355 ((ResourceContainerImpl *)m_pResourceContainer)->startContainer(m_strConfigPath);
356 m_pResourceContainer->getBundleConfiguration("oic.bundle.test", &bundle);
358 results = *bundle.begin();
360 EXPECT_STREQ("oic.bundle.test", results["id"].c_str());
361 EXPECT_STREQ("libTestBundle.so", results["path"].c_str());
362 EXPECT_STREQ("1.0.0", results["version"].c_str());
364 ((ResourceContainerImpl *)m_pResourceContainer)->stopContainer();
367 TEST_F(ResourceContainerBundleAPITest, BundleResourceConfigurationListParsed)
369 vector< resourceInfo > resourceConfig;
372 ((ResourceContainerImpl *)m_pResourceContainer)->startContainer(m_strConfigPath);
373 m_pResourceContainer->getResourceConfiguration("oic.bundle.test", &resourceConfig);
375 result = *resourceConfig.begin();
377 EXPECT_STREQ("test_resource", result.name.c_str());
378 EXPECT_STREQ("oic.test", result.resourceType.c_str());
380 ((ResourceContainerImpl *)m_pResourceContainer)->stopContainer();
383 class ResourceContainerImplTest: public TestWithMock
387 ResourceContainerImpl *m_pResourceContainer;
388 RCSBundleInfo *m_pBundleInfo;
393 TestWithMock::SetUp();
394 m_pResourceContainer = ResourceContainerImpl::getImplInstance();
395 m_pBundleInfo = RCSBundleInfo::build();
399 TEST_F(ResourceContainerImplTest, SoBundleLoadedWhenRegisteredWithRegisterBundleAPI)
401 m_pBundleInfo->setPath("libTestBundle.so");
402 m_pBundleInfo->setVersion("1.0");
403 m_pBundleInfo->setLibraryPath(".");
404 m_pBundleInfo->setID("oic.bundle.test");
406 m_pResourceContainer->registerBundle(m_pBundleInfo);
408 EXPECT_NE(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleHandle());
411 #if (JAVA_SUPPORT_TEST)
412 TEST_F(ResourceContainerImplTest, JavaBundleLoadedWhenRegisteredWithRegisterBundleAPIWrongPath)
414 m_pBundleInfo->setPath("wrong_path.jar");
415 m_pBundleInfo->setActivatorName("org/iotivity/bundle/hue/HueBundleActivator");
416 m_pBundleInfo->setLibraryPath("../.");
417 m_pBundleInfo->setVersion("1.0");
418 m_pBundleInfo->setID("oic.bundle.java.test");
420 m_pResourceContainer->registerBundle(m_pBundleInfo);
421 EXPECT_FALSE(((BundleInfoInternal *)m_pBundleInfo)->isLoaded());
424 TEST_F(ResourceContainerImplTest, JavaBundleTest)
426 m_pBundleInfo->setPath("TestBundleJava/hue-0.1-jar-with-dependencies.jar");
427 m_pBundleInfo->setActivatorName("org/iotivity/bundle/hue/HueBundleActivator");
428 m_pBundleInfo->setLibraryPath("../.");
429 m_pBundleInfo->setVersion("1.0");
430 m_pBundleInfo->setID("oic.bundle.java.test");
432 m_pResourceContainer->registerBundle(m_pBundleInfo);
433 EXPECT_TRUE(((BundleInfoInternal *)m_pBundleInfo)->isLoaded());
435 m_pResourceContainer->activateBundle(m_pBundleInfo);
436 EXPECT_TRUE(((BundleInfoInternal *) m_pBundleInfo)->isActivated());
438 m_pResourceContainer->deactivateBundle(m_pBundleInfo);
439 EXPECT_FALSE(((BundleInfoInternal *) m_pBundleInfo)->isActivated());
443 TEST_F(ResourceContainerImplTest, BundleNotRegisteredIfBundlePathIsInvalid)
445 m_pBundleInfo->setPath("");
446 m_pBundleInfo->setVersion("1.0");
447 m_pBundleInfo->setLibraryPath("../.");
448 m_pBundleInfo->setID("oic.bundle.test");
450 m_pResourceContainer->registerBundle(m_pBundleInfo);
452 EXPECT_EQ(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleHandle());
456 TEST_F(ResourceContainerImplTest, SoBundleActivatedWithValidBundleInfo)
458 m_pBundleInfo->setPath("libTestBundle.so");
459 m_pBundleInfo->setVersion("1.0");
460 m_pBundleInfo->setLibraryPath("../.");
461 m_pBundleInfo->setID("oic.bundle.test");
463 m_pResourceContainer->registerBundle(m_pBundleInfo);
464 m_pResourceContainer->activateBundle(m_pBundleInfo);
466 EXPECT_NE(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleActivator());
469 TEST_F(ResourceContainerImplTest, BundleNotActivatedWhenNotRegistered)
471 m_pBundleInfo->setPath("libTestBundle.so");
472 m_pBundleInfo->setVersion("1.0");
473 m_pBundleInfo->setLibraryPath("../.");
474 m_pBundleInfo->setID("oic.bundle.test");
476 m_pResourceContainer->activateBundle(m_pBundleInfo);
478 EXPECT_EQ(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleActivator());
481 TEST_F(ResourceContainerImplTest, SoBundleActivatedWithBundleID)
483 m_pBundleInfo->setPath("libTestBundle.so");
484 m_pBundleInfo->setVersion("1.0");
485 m_pBundleInfo->setLibraryPath("../.");
486 m_pBundleInfo->setID("oic.bundle.test");
488 m_pResourceContainer->registerBundle(m_pBundleInfo);
489 m_pResourceContainer->activateBundle(m_pBundleInfo->getID());
491 EXPECT_NE(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleActivator());
492 EXPECT_TRUE(((BundleInfoInternal *)m_pBundleInfo)->isActivated());
495 TEST_F(ResourceContainerImplTest, BundleDeactivatedWithBundleInfo)
497 m_pBundleInfo->setPath("libTestBundle.so");
498 m_pBundleInfo->setVersion("1.0");
499 m_pBundleInfo->setLibraryPath("../.");
500 m_pBundleInfo->setID("oic.bundle.test");
502 m_pResourceContainer->registerBundle(m_pBundleInfo);
503 m_pResourceContainer->activateBundle(m_pBundleInfo);
504 m_pResourceContainer->deactivateBundle(m_pBundleInfo);
506 EXPECT_NE(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleDeactivator());
507 EXPECT_FALSE(((BundleInfoInternal *)m_pBundleInfo)->isActivated());
510 TEST_F(ResourceContainerImplTest, BundleDeactivatedWithBundleInfoJava)
512 m_pBundleInfo->setPath("TestBundle/hue-0.1-jar-with-dependencies.jar");
513 m_pBundleInfo->setActivatorName("org/iotivity/bundle/hue/HueBundleActivator");
514 m_pBundleInfo->setLibraryPath("../.");
515 m_pBundleInfo->setVersion("1.0");
516 m_pBundleInfo->setID("oic.bundle.java.test");
518 m_pResourceContainer->registerBundle(m_pBundleInfo);
519 m_pResourceContainer->activateBundle(m_pBundleInfo);
520 m_pResourceContainer->deactivateBundle(m_pBundleInfo);
521 EXPECT_FALSE(((BundleInfoInternal *) m_pBundleInfo)->isActivated());
524 TEST_F(ResourceContainerImplTest, SoBundleDeactivatedWithBundleID)
526 m_pBundleInfo->setPath("libTestBundle.so");
527 m_pBundleInfo->setVersion("1.0");
528 m_pBundleInfo->setLibraryPath("../.");
529 m_pBundleInfo->setID("oic.bundle.test");
531 m_pResourceContainer->registerBundle(m_pBundleInfo);
532 m_pResourceContainer->activateBundle(m_pBundleInfo);
534 m_pResourceContainer->deactivateBundle(m_pBundleInfo->getID());
536 EXPECT_FALSE(((BundleInfoInternal *)m_pBundleInfo)->isActivated());
539 /* Test for Configuration */
540 TEST(ConfigurationTest, ConfigFileLoadedWithValidPath)
542 std::string strConfigPath;
543 getCurrentPath(&strConfigPath);
544 strConfigPath.append("/");
545 strConfigPath.append(CONFIG_FILE);
547 Configuration *config = new Configuration(strConfigPath);
549 EXPECT_TRUE(config->isLoaded());
552 TEST(ConfigurationTest, ConfigFileNotLoadedWithInvalidPath)
554 Configuration *config = new Configuration("InvalidPath");
556 EXPECT_FALSE(config->isLoaded());
559 TEST(ConfigurationTest, BundleConfigurationListParsed)
561 std::string strConfigPath;
562 getCurrentPath(&strConfigPath);
563 strConfigPath.append("/");
564 strConfigPath.append(CONFIG_FILE);
566 Configuration *config = new Configuration(strConfigPath);
569 map< string, string > results;
571 config->getConfiguredBundles(&bundles);
573 results = *bundles.begin();
575 EXPECT_STREQ("oic.bundle.test", results["id"].c_str());
576 EXPECT_STREQ("libTestBundle.so", results["path"].c_str());
577 EXPECT_STREQ("1.0.0", results["version"].c_str());
580 TEST(ConfigurationTest, BundleConfigurationParsedWithValidBundleId)
582 std::string strConfigPath;
583 getCurrentPath(&strConfigPath);
584 strConfigPath.append("/");
585 strConfigPath.append(CONFIG_FILE);
587 Configuration *config = new Configuration(strConfigPath);
590 map< string, string > results;
592 config->getBundleConfiguration("oic.bundle.test", &bundle);
594 results = *bundle.begin();
596 EXPECT_STREQ("oic.bundle.test", results["id"].c_str());
597 EXPECT_STREQ("libTestBundle.so", results["path"].c_str());
598 EXPECT_STREQ("1.0.0", results["version"].c_str());
601 TEST(ConfigurationTest, BundleConfigurationNotParsedWithInvalidBundleId)
603 std::string strConfigPath;
604 getCurrentPath(&strConfigPath);
605 strConfigPath.append("/");
606 strConfigPath.append(CONFIG_FILE);
608 Configuration *config = new Configuration(strConfigPath);
611 config->getBundleConfiguration("test", &bundles);
613 EXPECT_TRUE(bundles.empty());
616 TEST(ConfigurationTest, BundleResourceConfigurationListParsed)
618 std::string strConfigPath;
619 getCurrentPath(&strConfigPath);
620 strConfigPath.append("/");
621 strConfigPath.append(CONFIG_FILE);
623 Configuration *config = new Configuration(strConfigPath);
625 vector< resourceInfo > resourceConfig;
628 config->getResourceConfiguration("oic.bundle.test", &resourceConfig);
630 result = *resourceConfig.begin();
632 EXPECT_STREQ("test_resource", result.name.c_str());
633 EXPECT_STREQ("oic.test", result.resourceType.c_str());
636 TEST(ConfigurationTest, BundleResourceConfigurationNotParsedWithInvalidBundleId)
638 std::string strConfigPath;
639 getCurrentPath(&strConfigPath);
640 strConfigPath.append("/");
641 strConfigPath.append(CONFIG_FILE);
643 Configuration *config = new Configuration(strConfigPath);
646 vector< resourceInfo > resourceConfig;
647 config->getResourceConfiguration("test", &resourceConfig);
649 EXPECT_TRUE(bundles.empty());