change location of resource container directory
[platform/upstream/iotivity.git] / service / resource-container / 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 #if defined(__linux__)
22 #include <unistd.h>
23 #endif
24
25 #include <string>
26 #include <map>
27 #include <vector>
28
29 #include <UnitTestHelper.h>
30
31 #include <gtest/gtest.h>
32 #include <HippoMocks/hippomocks.h>
33
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"
41
42 #include "RCSResourceObject.h"
43 #include "RCSRemoteResourceObject.h"
44
45 #include "ResourceContainerTestSimulator.h"
46
47 using namespace std;
48 using namespace testing;
49 using namespace OIC::Service;
50
51 #define MAX_PATH 2048
52
53 string CONFIG_FILE = "ResourceContainerTestConfig.xml";
54
55 void getCurrentPath(std::string *pPath)
56 {
57     char buffer[MAX_PATH];
58
59 #if defined(__linux__)
60     char *strPath = NULL;
61     int length = readlink("/proc/self/exe", buffer, MAX_PATH - 1);
62
63     if (length != -1)
64     {
65         buffer[length] = '\0';
66         strPath = strrchr(buffer, '/');
67
68         if (strPath != NULL)
69             *strPath = '\0';
70     }
71 #endif
72     pPath->append(buffer);
73 }
74
75 /*Fake bundle resource class for testing*/
76 class TestBundleResource: public BundleResource
77 {
78     public:
79         virtual void initAttributes() { }
80
81         virtual void handleSetAttributesRequest(RCSResourceAttributes &attr)
82         {
83             BundleResource::setAttributes(attr);
84         }
85
86         virtual RCSResourceAttributes &handleGetAttributesRequest()
87         {
88             return BundleResource::getAttributes();
89         }
90 };
91
92 class ResourceContainerTest: public TestWithMock
93 {
94
95     public:
96         RCSResourceContainer *m_pResourceContainer;
97         std::string m_strConfigPath;
98
99     protected:
100         void SetUp()
101         {
102             TestWithMock::SetUp();
103             m_pResourceContainer = RCSResourceContainer::getInstance();
104             getCurrentPath(&m_strConfigPath);
105             m_strConfigPath.append("/");
106             m_strConfigPath.append(CONFIG_FILE);
107         }
108 };
109
110 TEST_F(ResourceContainerTest, BundleRegisteredWhenContainerStartedWithValidConfigFile)
111 {
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());
120
121     cout << "Now stopping container." << endl;
122     m_pResourceContainer->stopContainer();
123 }
124
125 TEST_F(ResourceContainerTest, BundleLoadedWhenContainerStartedWithValidConfigFile)
126 {
127     m_pResourceContainer->startContainer(m_strConfigPath);
128
129     EXPECT_GT(m_pResourceContainer->listBundles().size(), (unsigned int) 0);
130     EXPECT_TRUE(((BundleInfoInternal *)(*m_pResourceContainer->listBundles().begin()))->isLoaded());
131     EXPECT_NE(nullptr,
132               ((BundleInfoInternal *)( *m_pResourceContainer->listBundles().begin()))->getBundleHandle());
133
134     m_pResourceContainer->stopContainer();
135 }
136
137 TEST_F(ResourceContainerTest, BundleActivatedWhenContainerStartedWithValidConfigFile)
138 {
139     m_pResourceContainer->startContainer(m_strConfigPath);
140
141     EXPECT_GT(m_pResourceContainer->listBundles().size(), (unsigned int) 0);
142     EXPECT_TRUE(
143         ((BundleInfoInternal *)(*m_pResourceContainer->listBundles().begin()))->isActivated());
144     EXPECT_NE(nullptr,
145               ((BundleInfoInternal *)( *m_pResourceContainer->listBundles().begin()))->getBundleActivator());
146
147     m_pResourceContainer->stopContainer();
148 }
149
150 TEST_F(ResourceContainerTest, BundleNotRegisteredWhenContainerStartedWithInvalidConfigFile)
151 {
152     m_pResourceContainer->startContainer("invalidConfig");
153
154     EXPECT_EQ((unsigned int) 0, m_pResourceContainer->listBundles().size());
155 }
156
157 TEST_F(ResourceContainerTest, BundleNotRegisteredWhenContainerStartedWithEmptyConfigFile)
158 {
159     m_pResourceContainer->startContainer("");
160
161     EXPECT_EQ((unsigned int) 0, m_pResourceContainer->listBundles().size());
162 }
163
164 TEST_F(ResourceContainerTest, BundleUnregisteredWhenContainerStopped)
165 {
166     m_pResourceContainer->startContainer(m_strConfigPath);
167     m_pResourceContainer->stopContainer();
168
169     EXPECT_EQ((unsigned int) 0, m_pResourceContainer->listBundles().size());
170 }
171
172 TEST_F(ResourceContainerTest, BundleStoppedWithStartBundleAPI)
173 {
174     m_pResourceContainer->startContainer(m_strConfigPath);
175     m_pResourceContainer->stopBundle("oic.bundle.test");
176
177     EXPECT_FALSE(
178         ((BundleInfoInternal *)(*m_pResourceContainer->listBundles().begin()))->isActivated());
179
180     m_pResourceContainer->stopContainer();
181 }
182
183 TEST_F(ResourceContainerTest, BundleStartedWithStartBundleAPI)
184 {
185     m_pResourceContainer->startContainer(m_strConfigPath);
186     m_pResourceContainer->stopBundle("oic.bundle.test");
187     m_pResourceContainer->startBundle("oic.bundle.test");
188
189     EXPECT_TRUE(
190         ((BundleInfoInternal *)(*m_pResourceContainer->listBundles().begin()))->isActivated());
191
192     m_pResourceContainer->stopContainer();
193 }
194
195 TEST_F(ResourceContainerTest, AddNewSoBundleToContainer)
196 {
197     std::map<string, string> bundleParams;
198     std::list<RCSBundleInfo *> bundles;
199
200     bundles = m_pResourceContainer->listBundles();
201     m_pResourceContainer->addBundle("oic.bundle.test", "", "libTestBundle.so", "test", bundleParams);
202
203     EXPECT_EQ(bundles.size() + 1, m_pResourceContainer->listBundles().size());
204     EXPECT_TRUE(((BundleInfoInternal *)(*m_pResourceContainer->listBundles().begin()))->isLoaded());
205 }
206
207 TEST_F(ResourceContainerTest, RemoveSoBundleFromContainer)
208 {
209     std::map<string, string> bundleParams;
210     std::list<RCSBundleInfo *> bundles;
211
212     bundles = m_pResourceContainer->listBundles();
213     m_pResourceContainer->removeBundle("oic.bundle.test");
214
215     EXPECT_EQ(bundles.size() - 1, m_pResourceContainer->listBundles().size());
216 }
217
218 TEST_F(ResourceContainerTest, AddBundleAlreadyRegistered)
219 {
220     std::map<string, string> bundleParams;
221     std::list<RCSBundleInfo *> bundles;
222
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);
226
227     EXPECT_EQ(bundles.size(), m_pResourceContainer->listBundles().size());
228 }
229
230 TEST_F(ResourceContainerTest, AddAndRemoveSoBundleResource)
231 {
232     std::list<string> resources;
233     std::map<string, string> resourceParams;
234     resourceParams["resourceType"] = "container.test";
235
236     m_pResourceContainer->startContainer(m_strConfigPath);
237     resources = m_pResourceContainer->listBundleResources("oic.bundle.test");
238
239     m_pResourceContainer->addResourceConfig("oic.bundle.test", "/test_resource", resourceParams);
240
241     EXPECT_EQ(resources.size() + 1,
242               m_pResourceContainer->listBundleResources("oic.bundle.test").size());
243
244     m_pResourceContainer->removeResourceConfig("oic.bundle.test", "/test_resource");
245
246     EXPECT_EQ(resources.size(), m_pResourceContainer->listBundleResources("oic.bundle.test").size());
247
248     m_pResourceContainer->stopContainer();
249 }
250
251 TEST_F(ResourceContainerTest, TryAddingSoBundleResourceToNotRegisteredBundle)
252 {
253     std::map<string, string> resourceParams;
254
255     mocks.NeverCallFunc(ResourceContainerImpl::buildResourceObject);
256
257     m_pResourceContainer->addResourceConfig("unvalidBundleId", "", resourceParams);
258 }
259
260 class ResourceContainerBundleAPITest: public TestWithMock
261 {
262
263     public:
264         RCSResourceObject *m_pResourceObject;
265         ResourceContainerBundleAPI *m_pResourceContainer;
266         BundleResource::Ptr m_pBundleResource;
267         std::string m_strConfigPath;
268
269     protected:
270         void SetUp()
271         {
272             TestWithMock::SetUp();
273             m_pResourceObject = mocks.Mock<RCSResourceObject>();
274             m_pResourceContainer = ResourceContainerBundleAPI::getInstance();
275
276             getCurrentPath(&m_strConfigPath);
277             m_strConfigPath.append("/");
278             m_strConfigPath.append(CONFIG_FILE);
279
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";
284         }
285 };
286
287 TEST_F(ResourceContainerBundleAPITest, ResourceServerCreatedWhenRegisterResourceCalled)
288 {
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";
293
294     mocks.ExpectCallFunc(ResourceContainerImpl::buildResourceObject).With(m_pBundleResource->m_uri,
295             m_pBundleResource->m_resourceType).Return(nullptr);
296
297     m_pResourceContainer->registerResource(m_pBundleResource);
298 }
299
300 TEST_F(ResourceContainerBundleAPITest, RequestHandlerForResourceServerSetWhenRegisterResourceCalled)
301 {
302     mocks.OnCallFunc(ResourceContainerImpl::buildResourceObject).Return(
303         RCSResourceObject::Ptr(m_pResourceObject, [](RCSResourceObject *)
304     {}));
305
306     mocks.ExpectCall(m_pResourceObject, RCSResourceObject::setGetRequestHandler);
307     mocks.ExpectCall(m_pResourceObject, RCSResourceObject::setSetRequestHandler);
308
309     m_pResourceContainer->registerResource(m_pBundleResource);
310
311     m_pResourceContainer->unregisterResource(m_pBundleResource);
312 }
313
314 TEST_F(ResourceContainerBundleAPITest, BundleResourceUnregisteredWhenUnregisterResourceCalled)
315 {
316     mocks.OnCallFunc(ResourceContainerImpl::buildResourceObject).Return(
317         RCSResourceObject::Ptr(m_pResourceObject, [](RCSResourceObject *)
318     {}));
319
320     mocks.ExpectCall(m_pResourceObject, RCSResourceObject::setGetRequestHandler);
321     mocks.ExpectCall(m_pResourceObject, RCSResourceObject::setSetRequestHandler);
322
323     m_pResourceContainer->registerResource(m_pBundleResource);
324     m_pResourceContainer->unregisterResource(m_pBundleResource);
325
326     EXPECT_EQ((unsigned int) 0,
327               ((ResourceContainerImpl *)m_pResourceContainer)->listBundleResources(
328                   m_pBundleResource->m_bundleId).size());
329 }
330
331 TEST_F(ResourceContainerBundleAPITest,
332        ServerNotifiesToObserversWhenNotificationReceivedFromResource)
333 {
334     mocks.OnCallFunc(ResourceContainerImpl::buildResourceObject).Return(
335         RCSResourceObject::Ptr(m_pResourceObject, [](RCSResourceObject *)
336     {}));
337
338     mocks.ExpectCall(m_pResourceObject, RCSResourceObject::setGetRequestHandler);
339     mocks.ExpectCall(m_pResourceObject, RCSResourceObject::setSetRequestHandler);
340
341     m_pResourceContainer->registerResource(m_pBundleResource);
342
343     mocks.ExpectCall(m_pResourceObject, RCSResourceObject::notify);
344
345     m_pResourceContainer->onNotificationReceived(m_pBundleResource->m_uri);
346
347     m_pResourceContainer->unregisterResource(m_pBundleResource);
348 }
349
350 TEST_F(ResourceContainerBundleAPITest, BundleConfigurationParsedWithValidBundleId)
351 {
352     configInfo bundle;
353     map< string, string > results;
354
355     ((ResourceContainerImpl *)m_pResourceContainer)->startContainer(m_strConfigPath);
356     m_pResourceContainer->getBundleConfiguration("oic.bundle.test", &bundle);
357
358     results = *bundle.begin();
359
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());
363
364     ((ResourceContainerImpl *)m_pResourceContainer)->stopContainer();
365 }
366
367 TEST_F(ResourceContainerBundleAPITest, BundleResourceConfigurationListParsed)
368 {
369     vector< resourceInfo > resourceConfig;
370     resourceInfo result;
371
372     ((ResourceContainerImpl *)m_pResourceContainer)->startContainer(m_strConfigPath);
373     m_pResourceContainer->getResourceConfiguration("oic.bundle.test", &resourceConfig);
374
375     result = *resourceConfig.begin();
376
377     EXPECT_STREQ("test_resource", result.name.c_str());
378     EXPECT_STREQ("container.test", result.resourceType.c_str());
379
380     ((ResourceContainerImpl *)m_pResourceContainer)->stopContainer();
381 }
382
383 class ResourceContainerImplTest: public TestWithMock
384 {
385
386     public:
387         ResourceContainerImpl *m_pResourceContainer;
388         BundleInfoInternal *m_pBundleInfo;
389
390     protected:
391         void SetUp()
392         {
393             TestWithMock::SetUp();
394             m_pResourceContainer = ResourceContainerImpl::getImplInstance();
395             m_pBundleInfo = new BundleInfoInternal();
396         }
397
398         void TearDown()
399         {
400             delete m_pBundleInfo;
401         }
402 };
403
404 TEST_F(ResourceContainerImplTest, SoBundleLoadedWhenRegisteredWithRegisterBundleAPI)
405 {
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");
411
412     m_pResourceContainer->registerBundle(m_pBundleInfo);
413
414     EXPECT_NE(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleHandle());
415 }
416
417 #if (JAVA_SUPPORT_TEST)
418 TEST_F(ResourceContainerImplTest, JavaBundleLoadedWhenRegisteredWithRegisterBundleAPIWrongPath)
419 {
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");
425
426     m_pResourceContainer->registerBundle(m_pBundleInfo);
427     EXPECT_FALSE(((BundleInfoInternal *)m_pBundleInfo)->isLoaded());
428 }
429
430 TEST_F(ResourceContainerImplTest, JavaBundleTest)
431 {
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");
437
438     m_pResourceContainer->registerBundle(m_pBundleInfo);
439     EXPECT_TRUE(((BundleInfoInternal *)m_pBundleInfo)->isLoaded());
440
441     m_pResourceContainer->activateBundle(m_pBundleInfo);
442     EXPECT_TRUE(((BundleInfoInternal *) m_pBundleInfo)->isActivated());
443
444     m_pResourceContainer->deactivateBundle(m_pBundleInfo);
445     EXPECT_FALSE(((BundleInfoInternal *) m_pBundleInfo)->isActivated());
446 }
447 #endif
448
449 TEST_F(ResourceContainerImplTest, BundleNotRegisteredIfBundlePathIsInvalid)
450 {
451     m_pBundleInfo->setPath("");
452     m_pBundleInfo->setVersion("1.0");
453     m_pBundleInfo->setLibraryPath("../.");
454     m_pBundleInfo->setID("oic.bundle.test");
455
456     m_pResourceContainer->registerBundle(m_pBundleInfo);
457
458     EXPECT_EQ(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleHandle());
459
460 }
461
462 TEST_F(ResourceContainerImplTest, SoBundleActivatedWithValidBundleInfo)
463 {
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");
469
470     m_pResourceContainer->registerBundle(m_pBundleInfo);
471     m_pResourceContainer->activateBundle(m_pBundleInfo);
472
473     EXPECT_NE(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleActivator());
474 }
475
476 TEST_F(ResourceContainerImplTest, BundleNotActivatedWhenNotRegistered)
477 {
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");
483
484     m_pResourceContainer->activateBundle(m_pBundleInfo);
485
486     EXPECT_EQ(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleActivator());
487 }
488
489 TEST_F(ResourceContainerImplTest, SoBundleActivatedWithBundleID)
490 {
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");
496
497     m_pResourceContainer->registerBundle(m_pBundleInfo);
498     m_pResourceContainer->activateBundle(m_pBundleInfo->getID());
499
500     EXPECT_NE(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleActivator());
501     EXPECT_TRUE(((BundleInfoInternal *)m_pBundleInfo)->isActivated());
502 }
503
504 TEST_F(ResourceContainerImplTest, BundleDeactivatedWithBundleInfo)
505 {
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");
511
512     m_pResourceContainer->registerBundle(m_pBundleInfo);
513     m_pResourceContainer->activateBundle(m_pBundleInfo);
514     m_pResourceContainer->deactivateBundle(m_pBundleInfo);
515
516     EXPECT_NE(nullptr, ((BundleInfoInternal *)m_pBundleInfo)->getBundleDeactivator());
517     EXPECT_FALSE(((BundleInfoInternal *)m_pBundleInfo)->isActivated());
518 }
519
520 TEST_F(ResourceContainerImplTest, BundleDeactivatedWithBundleInfoJava)
521 {
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");
527
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());
532 }
533
534 TEST_F(ResourceContainerImplTest, SoBundleDeactivatedWithBundleID)
535 {
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");
541
542     m_pResourceContainer->registerBundle(m_pBundleInfo);
543     m_pResourceContainer->activateBundle(m_pBundleInfo);
544
545     m_pResourceContainer->deactivateBundle(m_pBundleInfo->getID());
546
547     EXPECT_FALSE(((BundleInfoInternal *)m_pBundleInfo)->isActivated());
548 }
549
550
551 /* Test for Configuration */
552 TEST(ConfigurationTest, ConfigFileLoadedWithValidPath)
553 {
554     std::string strConfigPath;
555     getCurrentPath(&strConfigPath);
556     strConfigPath.append("/");
557     strConfigPath.append(CONFIG_FILE);
558
559     Configuration *config = new Configuration(strConfigPath);
560
561     EXPECT_TRUE(config->isLoaded());
562
563     delete config;
564 }
565
566 TEST(ConfigurationTest, ConfigFileNotLoadedWithInvalidPath)
567 {
568     Configuration *config = new Configuration("InvalidPath");
569
570     EXPECT_FALSE(config->isLoaded());
571
572     delete config;
573 }
574
575 TEST(ConfigurationTest, BundleConfigurationListParsed)
576 {
577     std::string strConfigPath;
578     getCurrentPath(&strConfigPath);
579     strConfigPath.append("/");
580     strConfigPath.append(CONFIG_FILE);
581
582     Configuration *config = new Configuration(strConfigPath);
583
584     configInfo bundles;
585     map< string, string > results;
586
587     config->getConfiguredBundles(&bundles);
588
589     results = *bundles.begin();
590
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());
594
595     delete config;
596 }
597
598 TEST(ConfigurationTest, BundleConfigurationParsedWithValidBundleId)
599 {
600     std::string strConfigPath;
601     getCurrentPath(&strConfigPath);
602     strConfigPath.append("/");
603     strConfigPath.append(CONFIG_FILE);
604
605     Configuration *config = new Configuration(strConfigPath);
606
607     configInfo bundle;
608     map< string, string > results;
609
610     config->getBundleConfiguration("oic.bundle.test", &bundle);
611
612     results = *bundle.begin();
613
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());
617
618     delete config;
619 }
620
621 TEST(ConfigurationTest, BundleConfigurationNotParsedWithInvalidBundleId)
622 {
623     std::string strConfigPath;
624     getCurrentPath(&strConfigPath);
625     strConfigPath.append("/");
626     strConfigPath.append(CONFIG_FILE);
627
628     Configuration *config = new Configuration(strConfigPath);
629
630     configInfo bundles;
631     config->getBundleConfiguration("test", &bundles);
632
633     EXPECT_TRUE(bundles.empty());
634
635     delete config;
636 }
637
638 TEST(ConfigurationTest, BundleResourceConfigurationListParsed)
639 {
640     std::string strConfigPath;
641     getCurrentPath(&strConfigPath);
642     strConfigPath.append("/");
643     strConfigPath.append(CONFIG_FILE);
644
645     Configuration *config = new Configuration(strConfigPath);
646
647     vector< resourceInfo > resourceConfig;
648     resourceInfo result;
649
650     config->getResourceConfiguration("oic.bundle.test", &resourceConfig);
651
652     result = *resourceConfig.begin();
653
654     EXPECT_STREQ("test_resource", result.name.c_str());
655     EXPECT_STREQ("container.test", result.resourceType.c_str());
656
657     delete config;
658 }
659
660 TEST(ConfigurationTest, BundleResourceConfigurationNotParsedWithInvalidBundleId)
661 {
662     std::string strConfigPath;
663     getCurrentPath(&strConfigPath);
664     strConfigPath.append("/");
665     strConfigPath.append(CONFIG_FILE);
666
667     Configuration *config = new Configuration(strConfigPath);
668
669     configInfo bundles;
670     vector< resourceInfo > resourceConfig;
671     config->getResourceConfiguration("test", &resourceConfig);
672
673     EXPECT_TRUE(bundles.empty());
674
675     delete config;
676 }
677
678 namespace
679 {
680     void discoverdCB(RCSRemoteResourceObject::Ptr);
681     void onUpdate(RemoteResourceUnit::UPDATE_MSG, RCSRemoteResourceObject::Ptr);
682 }
683
684 class DiscoverResourceUnitTest: public TestWithMock
685 {
686     private:
687         typedef std::function<void(const std::string attributeName,
688                                    std::vector<RCSResourceAttributes::Value> values)>
689         UpdatedCB;
690     public:
691         ResourceContainerTestSimulator::Ptr testObject;
692         DiscoverResourceUnit::Ptr m_pDiscoverResourceUnit;
693         std::string m_bundleId;
694         UpdatedCB m_updatedCB;
695
696     protected:
697         void SetUp()
698         {
699             TestWithMock::SetUp();
700
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 >) { });
706         }
707
708         void TearDown()
709         {
710             m_pDiscoverResourceUnit.reset();
711             testObject.reset();
712             TestWithMock::TearDown();
713         }
714 };
715
716 TEST_F(DiscoverResourceUnitTest, startDiscover)
717 {
718     std::string type = "Resource.Container";
719     std::string attributeName = "TestResourceContainer";
720
721     m_pDiscoverResourceUnit->startDiscover(
722         DiscoverResourceUnit::DiscoverResourceInfo("", type, attributeName), m_updatedCB);
723
724     std::chrono::milliseconds interval(400);
725     std::this_thread::sleep_for(interval);
726 }
727
728 TEST_F(DiscoverResourceUnitTest, onUpdateCalled)
729 {
730     std::string type = "Resource.Container";
731     std::string attributeName = "TestResourceContainer";
732
733     m_pDiscoverResourceUnit->startDiscover(
734         DiscoverResourceUnit::DiscoverResourceInfo("", type, attributeName), m_updatedCB);
735
736     std::chrono::milliseconds interval(400);
737     std::this_thread::sleep_for(interval);
738
739     testObject->ChangeAttributeValue();
740
741 }
742
743 namespace
744 {
745     void onStateCB(ResourceState) { }
746     void onCacheCB(const RCSResourceAttributes &) { }
747 }
748
749 class RemoteResourceUnitTest: public TestWithMock
750 {
751     private:
752         typedef std::function<void(RemoteResourceUnit::UPDATE_MSG, RCSRemoteResourceObject::Ptr)>
753         UpdatedCBFromServer;
754
755     public:
756         ResourceContainerTestSimulator::Ptr testObject;
757         RemoteResourceUnit::Ptr m_pRemoteResourceUnit;
758         RCSRemoteResourceObject::Ptr m_pRCSRemoteResourceObject;
759         UpdatedCBFromServer m_updatedCBFromServer;
760
761     protected:
762         void SetUp()
763         {
764             TestWithMock::SetUp();
765
766             testObject = std::make_shared<ResourceContainerTestSimulator>();
767             testObject->defaultRunSimulator();
768             m_pRCSRemoteResourceObject = testObject->getRemoteResource();
769             m_updatedCBFromServer = ([](RemoteResourceUnit::UPDATE_MSG, RCSRemoteResourceObject::Ptr) {});
770         }
771
772         void TearDown()
773         {
774             m_pRCSRemoteResourceObject.reset();
775             testObject.reset();
776             TestWithMock::TearDown();
777         }
778 };
779
780 TEST_F(RemoteResourceUnitTest, createRemoteResourceInfo)
781 {
782     EXPECT_NE(nullptr, m_pRemoteResourceUnit->createRemoteResourceInfo(m_pRCSRemoteResourceObject,
783               m_updatedCBFromServer));
784 }
785
786 TEST_F(RemoteResourceUnitTest, getRemoteResourceObject)
787 {
788     RemoteResourceUnit::Ptr ptr = m_pRemoteResourceUnit->createRemoteResourceInfo(
789                                       m_pRCSRemoteResourceObject, m_updatedCBFromServer);
790     EXPECT_EQ(m_pRCSRemoteResourceObject, ptr->getRemoteResourceObject());
791 }
792
793 TEST_F(RemoteResourceUnitTest, getRemoteResourceUri)
794 {
795     RemoteResourceUnit::Ptr ptr = m_pRemoteResourceUnit->createRemoteResourceInfo(
796                                       m_pRCSRemoteResourceObject, m_updatedCBFromServer);
797     EXPECT_NE("", ptr->getRemoteResourceUri());
798 }
799
800 TEST_F(RemoteResourceUnitTest, startCaching)
801 {
802     RemoteResourceUnit::Ptr ptr = m_pRemoteResourceUnit->createRemoteResourceInfo(
803                                       m_pRCSRemoteResourceObject, m_updatedCBFromServer);
804     ptr->startCaching();
805 }
806
807 TEST_F(RemoteResourceUnitTest, startMonitoring)
808 {
809     RemoteResourceUnit::Ptr ptr = m_pRemoteResourceUnit->createRemoteResourceInfo(
810                                       m_pRCSRemoteResourceObject, m_updatedCBFromServer);
811     ptr->startMonitoring();
812 }
813
814 TEST_F(RemoteResourceUnitTest, onCacheCBCalled)
815 {
816     bool isCalled = false;
817     mocks.ExpectCallFunc(onCacheCB).Do(
818         [this, &isCalled](const RCSResourceAttributes &)
819     {
820         isCalled = true;
821     });
822     RemoteResourceUnit::Ptr ptr = m_pRemoteResourceUnit->createRemoteResourceInfoWithCacheCB(
823                                       m_pRCSRemoteResourceObject, m_updatedCBFromServer, onCacheCB);
824     ptr->startCaching();
825     testObject->ChangeAttributeValue();
826     EXPECT_TRUE(isCalled);
827 }