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"
40 #include "RemoteResourceUnit.h"
42 #include "RCSResourceObject.h"
43 #include "RCSRemoteResourceObject.h"
45 #include "ResourceContainerTestSimulator.h"
48 using namespace testing;
49 using namespace OIC::Service;
53 string CONFIG_FILE = "ResourceContainerTestConfig.xml";
55 void getCurrentPath(std::string *pPath)
57 char buffer[MAX_PATH];
59 #if defined(__linux__)
61 int length = readlink("/proc/self/exe", buffer, MAX_PATH - 1);
65 buffer[length] = '\0';
66 strPath = strrchr(buffer, '/');
72 pPath->append(buffer);
75 /*Fake bundle resource class for testing*/
76 class TestBundleResource: public BundleResource
79 virtual void initAttributes() { }
81 virtual void handleSetAttributesRequest(RCSResourceAttributes &attr)
83 BundleResource::setAttributes(attr);
86 virtual RCSResourceAttributes &handleGetAttributesRequest()
88 return BundleResource::getAttributes();
92 class ResourceContainerTest: public TestWithMock
96 RCSResourceContainer *m_pResourceContainer;
97 std::string m_strConfigPath;
102 TestWithMock::SetUp();
103 m_pResourceContainer = RCSResourceContainer::getInstance();
104 getCurrentPath(&m_strConfigPath);
105 m_strConfigPath.append("/");
106 m_strConfigPath.append(CONFIG_FILE);
110 TEST_F(ResourceContainerTest, BundleRegisteredWhenContainerStartedWithValidConfigFile)
112 m_pResourceContainer->startContainer(m_strConfigPath);
113 EXPECT_GT(m_pResourceContainer->listBundles().size(), (unsigned int) 0);
114 cout << "now checking for bunlde ids " << endl;
115 EXPECT_STREQ("oic.bundle.test",
116 (*m_pResourceContainer->listBundles().begin())->getID().c_str());
117 EXPECT_STREQ("libTestBundle.so",
118 (*m_pResourceContainer->listBundles().begin())->getPath().c_str());
119 EXPECT_STREQ("1.0.0", (*m_pResourceContainer->listBundles().begin())->getVersion().c_str());
121 cout << "Now stopping container." << endl;
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", "test", 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", "test", bundleParams);
224 bundles = m_pResourceContainer->listBundles();
225 m_pResourceContainer->addBundle("oic.bundle.test", "", "libTestBundle.so", "test", 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"] = "container.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 BundleResource::Ptr 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 = std::make_shared< TestBundleResource >();
281 m_pBundleResource->m_bundleId = "oic.bundle.test";
282 m_pBundleResource->m_uri = "/test_resource";
283 m_pBundleResource->m_resourceType = "container.test";
287 TEST_F(ResourceContainerBundleAPITest, ResourceServerCreatedWhenRegisterResourceCalled)
289 m_pBundleResource = std::make_shared< TestBundleResource >();
290 m_pBundleResource->m_bundleId = "oic.bundle.test";
291 m_pBundleResource->m_uri = "/test_resource/test";
292 m_pBundleResource->m_resourceType = "container.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("container.test", result.resourceType.c_str());
380 ((ResourceContainerImpl *)m_pResourceContainer)->stopContainer();
383 class ResourceContainerImplTest: public TestWithMock
387 ResourceContainerImpl *m_pResourceContainer;
388 BundleInfoInternal *m_pBundleInfo;
393 TestWithMock::SetUp();
394 m_pResourceContainer = ResourceContainerImpl::getImplInstance();
395 m_pBundleInfo = new BundleInfoInternal();
400 delete m_pBundleInfo;
404 TEST_F(ResourceContainerImplTest, SoBundleLoadedWhenRegisteredWithRegisterBundleAPI)
406 m_pBundleInfo->setPath("libTestBundle.so");
407 m_pBundleInfo->setActivatorName("test");
408 m_pBundleInfo->setVersion("1.0");
409 m_pBundleInfo->setLibraryPath(".");
410 m_pBundleInfo->setID("oic.bundle.test");
412 m_pResourceContainer->registerBundle(m_pBundleInfo);
414 EXPECT_NE(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleHandle());
417 #if (JAVA_SUPPORT_TEST)
418 TEST_F(ResourceContainerImplTest, JavaBundleLoadedWhenRegisteredWithRegisterBundleAPIWrongPath)
420 m_pBundleInfo->setPath("wrong_path.jar");
421 m_pBundleInfo->setActivatorName("org/iotivity/bundle/hue/HueBundleActivator");
422 m_pBundleInfo->setLibraryPath("../.");
423 m_pBundleInfo->setVersion("1.0");
424 m_pBundleInfo->setID("oic.bundle.java.test");
426 m_pResourceContainer->registerBundle(m_pBundleInfo);
427 EXPECT_FALSE(((BundleInfoInternal *)m_pBundleInfo)->isLoaded());
430 TEST_F(ResourceContainerImplTest, JavaBundleTest)
432 m_pBundleInfo->setPath("TestBundleJava/hue-0.1-jar-with-dependencies.jar");
433 m_pBundleInfo->setActivatorName("org/iotivity/bundle/hue/HueBundleActivator");
434 m_pBundleInfo->setLibraryPath("../.");
435 m_pBundleInfo->setVersion("1.0");
436 m_pBundleInfo->setID("oic.bundle.java.test");
438 m_pResourceContainer->registerBundle(m_pBundleInfo);
439 EXPECT_TRUE(((BundleInfoInternal *)m_pBundleInfo)->isLoaded());
441 m_pResourceContainer->activateBundle(m_pBundleInfo);
442 EXPECT_TRUE(((BundleInfoInternal *) m_pBundleInfo)->isActivated());
444 m_pResourceContainer->deactivateBundle(m_pBundleInfo);
445 EXPECT_FALSE(((BundleInfoInternal *) m_pBundleInfo)->isActivated());
449 TEST_F(ResourceContainerImplTest, BundleNotRegisteredIfBundlePathIsInvalid)
451 m_pBundleInfo->setPath("");
452 m_pBundleInfo->setVersion("1.0");
453 m_pBundleInfo->setLibraryPath("../.");
454 m_pBundleInfo->setID("oic.bundle.test");
456 m_pResourceContainer->registerBundle(m_pBundleInfo);
458 EXPECT_EQ(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleHandle());
462 TEST_F(ResourceContainerImplTest, SoBundleActivatedWithValidBundleInfo)
464 m_pBundleInfo->setPath("libTestBundle.so");
465 m_pBundleInfo->setVersion("1.0");
466 m_pBundleInfo->setActivatorName("test");
467 m_pBundleInfo->setLibraryPath("../.");
468 m_pBundleInfo->setID("oic.bundle.test");
470 m_pResourceContainer->registerBundle(m_pBundleInfo);
471 m_pResourceContainer->activateBundle(m_pBundleInfo);
473 EXPECT_NE(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleActivator());
476 TEST_F(ResourceContainerImplTest, BundleNotActivatedWhenNotRegistered)
478 m_pBundleInfo->setPath("libTestBundle.so");
479 m_pBundleInfo->setActivatorName("test");
480 m_pBundleInfo->setVersion("1.0");
481 m_pBundleInfo->setLibraryPath("../.");
482 m_pBundleInfo->setID("oic.bundle.test");
484 m_pResourceContainer->activateBundle(m_pBundleInfo);
486 EXPECT_EQ(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleActivator());
489 TEST_F(ResourceContainerImplTest, SoBundleActivatedWithBundleID)
491 m_pBundleInfo->setPath("libTestBundle.so");
492 m_pBundleInfo->setVersion("1.0");
493 m_pBundleInfo->setLibraryPath("../.");
494 m_pBundleInfo->setActivatorName("test");
495 m_pBundleInfo->setID("oic.bundle.test");
497 m_pResourceContainer->registerBundle(m_pBundleInfo);
498 m_pResourceContainer->activateBundle(m_pBundleInfo->getID());
500 EXPECT_NE(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleActivator());
501 EXPECT_TRUE(((BundleInfoInternal *)m_pBundleInfo)->isActivated());
504 TEST_F(ResourceContainerImplTest, BundleDeactivatedWithBundleInfo)
506 m_pBundleInfo->setPath("libTestBundle.so");
507 m_pBundleInfo->setVersion("1.0");
508 m_pBundleInfo->setLibraryPath("../.");
509 m_pBundleInfo->setActivatorName("test");
510 m_pBundleInfo->setID("oic.bundle.test");
512 m_pResourceContainer->registerBundle(m_pBundleInfo);
513 m_pResourceContainer->activateBundle(m_pBundleInfo);
514 m_pResourceContainer->deactivateBundle(m_pBundleInfo);
516 EXPECT_NE(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleDeactivator());
517 EXPECT_FALSE(((BundleInfoInternal *)m_pBundleInfo)->isActivated());
520 TEST_F(ResourceContainerImplTest, BundleDeactivatedWithBundleInfoJava)
522 m_pBundleInfo->setPath("TestBundle/hue-0.1-jar-with-dependencies.jar");
523 m_pBundleInfo->setActivatorName("org/iotivity/bundle/hue/HueBundleActivator");
524 m_pBundleInfo->setLibraryPath("../.");
525 m_pBundleInfo->setVersion("1.0");
526 m_pBundleInfo->setID("oic.bundle.java.test");
528 m_pResourceContainer->registerBundle(m_pBundleInfo);
529 m_pResourceContainer->activateBundle(m_pBundleInfo);
530 m_pResourceContainer->deactivateBundle(m_pBundleInfo);
531 EXPECT_FALSE(((BundleInfoInternal *) m_pBundleInfo)->isActivated());
534 TEST_F(ResourceContainerImplTest, SoBundleDeactivatedWithBundleID)
536 m_pBundleInfo->setPath("libTestBundle.so");
537 m_pBundleInfo->setVersion("1.0");
538 m_pBundleInfo->setLibraryPath("../.");
539 m_pBundleInfo->setActivatorName("test");
540 m_pBundleInfo->setID("oic.bundle.test");
542 m_pResourceContainer->registerBundle(m_pBundleInfo);
543 m_pResourceContainer->activateBundle(m_pBundleInfo);
545 m_pResourceContainer->deactivateBundle(m_pBundleInfo->getID());
547 EXPECT_FALSE(((BundleInfoInternal *)m_pBundleInfo)->isActivated());
551 /* Test for Configuration */
552 TEST(ConfigurationTest, ConfigFileLoadedWithValidPath)
554 std::string strConfigPath;
555 getCurrentPath(&strConfigPath);
556 strConfigPath.append("/");
557 strConfigPath.append(CONFIG_FILE);
559 Configuration *config = new Configuration(strConfigPath);
561 EXPECT_TRUE(config->isLoaded());
566 TEST(ConfigurationTest, ConfigFileNotLoadedWithInvalidPath)
568 Configuration *config = new Configuration("InvalidPath");
570 EXPECT_FALSE(config->isLoaded());
575 TEST(ConfigurationTest, BundleConfigurationListParsed)
577 std::string strConfigPath;
578 getCurrentPath(&strConfigPath);
579 strConfigPath.append("/");
580 strConfigPath.append(CONFIG_FILE);
582 Configuration *config = new Configuration(strConfigPath);
585 map< string, string > results;
587 config->getConfiguredBundles(&bundles);
589 results = *bundles.begin();
591 EXPECT_STREQ("oic.bundle.test", results["id"].c_str());
592 EXPECT_STREQ("libTestBundle.so", results["path"].c_str());
593 EXPECT_STREQ("1.0.0", results["version"].c_str());
598 TEST(ConfigurationTest, BundleConfigurationParsedWithValidBundleId)
600 std::string strConfigPath;
601 getCurrentPath(&strConfigPath);
602 strConfigPath.append("/");
603 strConfigPath.append(CONFIG_FILE);
605 Configuration *config = new Configuration(strConfigPath);
608 map< string, string > results;
610 config->getBundleConfiguration("oic.bundle.test", &bundle);
612 results = *bundle.begin();
614 EXPECT_STREQ("oic.bundle.test", results["id"].c_str());
615 EXPECT_STREQ("libTestBundle.so", results["path"].c_str());
616 EXPECT_STREQ("1.0.0", results["version"].c_str());
621 TEST(ConfigurationTest, BundleConfigurationNotParsedWithInvalidBundleId)
623 std::string strConfigPath;
624 getCurrentPath(&strConfigPath);
625 strConfigPath.append("/");
626 strConfigPath.append(CONFIG_FILE);
628 Configuration *config = new Configuration(strConfigPath);
631 config->getBundleConfiguration("test", &bundles);
633 EXPECT_TRUE(bundles.empty());
638 TEST(ConfigurationTest, BundleResourceConfigurationListParsed)
640 std::string strConfigPath;
641 getCurrentPath(&strConfigPath);
642 strConfigPath.append("/");
643 strConfigPath.append(CONFIG_FILE);
645 Configuration *config = new Configuration(strConfigPath);
647 vector< resourceInfo > resourceConfig;
650 config->getResourceConfiguration("oic.bundle.test", &resourceConfig);
652 result = *resourceConfig.begin();
654 EXPECT_STREQ("test_resource", result.name.c_str());
655 EXPECT_STREQ("container.test", result.resourceType.c_str());
660 TEST(ConfigurationTest, BundleResourceConfigurationNotParsedWithInvalidBundleId)
662 std::string strConfigPath;
663 getCurrentPath(&strConfigPath);
664 strConfigPath.append("/");
665 strConfigPath.append(CONFIG_FILE);
667 Configuration *config = new Configuration(strConfigPath);
670 vector< resourceInfo > resourceConfig;
671 config->getResourceConfiguration("test", &resourceConfig);
673 EXPECT_TRUE(bundles.empty());
680 void discoverdCB(RCSRemoteResourceObject::Ptr);
681 void onUpdate(RemoteResourceUnit::UPDATE_MSG, RCSRemoteResourceObject::Ptr);
684 class DiscoverResourceUnitTest: public TestWithMock
687 typedef std::function<void(const std::string attributeName,
688 std::vector<RCSResourceAttributes::Value> values)>
691 ResourceContainerTestSimulator::Ptr testObject;
692 DiscoverResourceUnit::Ptr m_pDiscoverResourceUnit;
693 std::string m_bundleId;
694 UpdatedCB m_updatedCB;
699 TestWithMock::SetUp();
701 testObject = std::make_shared<ResourceContainerTestSimulator>();
702 testObject->createResource();
703 m_bundleId = "/a/TempHumSensor/Container";
704 m_pDiscoverResourceUnit = std::make_shared< DiscoverResourceUnit >( m_bundleId );
705 m_updatedCB = ([](const std::string, std::vector< RCSResourceAttributes::Value >) { });
710 m_pDiscoverResourceUnit.reset();
712 TestWithMock::TearDown();
716 TEST_F(DiscoverResourceUnitTest, startDiscover)
718 std::string type = "Resource.Container";
719 std::string attributeName = "TestResourceContainer";
721 m_pDiscoverResourceUnit->startDiscover(
722 DiscoverResourceUnit::DiscoverResourceInfo("", type, attributeName), m_updatedCB);
724 std::chrono::milliseconds interval(400);
725 std::this_thread::sleep_for(interval);
728 TEST_F(DiscoverResourceUnitTest, onUpdateCalled)
730 std::string type = "Resource.Container";
731 std::string attributeName = "TestResourceContainer";
733 m_pDiscoverResourceUnit->startDiscover(
734 DiscoverResourceUnit::DiscoverResourceInfo("", type, attributeName), m_updatedCB);
736 std::chrono::milliseconds interval(400);
737 std::this_thread::sleep_for(interval);
739 testObject->ChangeAttributeValue();
745 void onStateCB(ResourceState) { }
746 void onCacheCB(const RCSResourceAttributes &) { }
749 class RemoteResourceUnitTest: public TestWithMock
752 typedef std::function<void(RemoteResourceUnit::UPDATE_MSG, RCSRemoteResourceObject::Ptr)>
756 ResourceContainerTestSimulator::Ptr testObject;
757 RemoteResourceUnit::Ptr m_pRemoteResourceUnit;
758 RCSRemoteResourceObject::Ptr m_pRCSRemoteResourceObject;
759 UpdatedCBFromServer m_updatedCBFromServer;
764 TestWithMock::SetUp();
766 testObject = std::make_shared<ResourceContainerTestSimulator>();
767 testObject->defaultRunSimulator();
768 m_pRCSRemoteResourceObject = testObject->getRemoteResource();
769 m_updatedCBFromServer = ([](RemoteResourceUnit::UPDATE_MSG, RCSRemoteResourceObject::Ptr) {});
774 m_pRCSRemoteResourceObject.reset();
776 TestWithMock::TearDown();
780 TEST_F(RemoteResourceUnitTest, createRemoteResourceInfo)
782 EXPECT_NE(nullptr, m_pRemoteResourceUnit->createRemoteResourceInfo(m_pRCSRemoteResourceObject,
783 m_updatedCBFromServer));
786 TEST_F(RemoteResourceUnitTest, getRemoteResourceObject)
788 RemoteResourceUnit::Ptr ptr = m_pRemoteResourceUnit->createRemoteResourceInfo(
789 m_pRCSRemoteResourceObject, m_updatedCBFromServer);
790 EXPECT_EQ(m_pRCSRemoteResourceObject, ptr->getRemoteResourceObject());
793 TEST_F(RemoteResourceUnitTest, getRemoteResourceUri)
795 RemoteResourceUnit::Ptr ptr = m_pRemoteResourceUnit->createRemoteResourceInfo(
796 m_pRCSRemoteResourceObject, m_updatedCBFromServer);
797 EXPECT_NE("", ptr->getRemoteResourceUri());
800 TEST_F(RemoteResourceUnitTest, startCaching)
802 RemoteResourceUnit::Ptr ptr = m_pRemoteResourceUnit->createRemoteResourceInfo(
803 m_pRCSRemoteResourceObject, m_updatedCBFromServer);
807 TEST_F(RemoteResourceUnitTest, startMonitoring)
809 RemoteResourceUnit::Ptr ptr = m_pRemoteResourceUnit->createRemoteResourceInfo(
810 m_pRCSRemoteResourceObject, m_updatedCBFromServer);
811 ptr->startMonitoring();
814 TEST_F(RemoteResourceUnitTest, onCacheCBCalled)
816 bool isCalled = false;
817 mocks.ExpectCallFunc(onCacheCB).Do(
818 [this, &isCalled](const RCSResourceAttributes &)
822 RemoteResourceUnit::Ptr ptr = m_pRemoteResourceUnit->createRemoteResourceInfoWithCacheCB(
823 m_pRCSRemoteResourceObject, m_updatedCBFromServer, onCacheCB);
825 testObject->ChangeAttributeValue();
826 EXPECT_TRUE(isCalled);