cdb333493c20a06a64753f2848d07298d042e7d7
[platform/upstream/iotivity.git] / service / things-manager / unittests / ThingsManagerTest.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 "UnitTestHelper.h"
22 #include "MaintenanceCollection.h"
23 #include "ConfigurationCollection.h"
24 #include "FactorySetCollection.h"
25
26 #include "timer.h"
27 #include "ActionSet.h"
28 #include "GroupManager.h"
29 #include "ThingsConfiguration.h"
30 #include "ThingsMaintenance.h"
31 #include "ocstack.h"
32 #include "OCPlatform.h"
33 #include "OCApi.h"
34
35 #include <iostream>
36 #include <functional>
37 #include <pthread.h>
38 #include <condition_variable>
39
40 #define SUCCESS_RESPONSE 0
41 constexpr int DEFAULT_WAITING_TIME_IN_MILLIS = 3000;
42
43 using namespace OIC;
44 using namespace OC;
45 using namespace std;
46 namespace PH = std::placeholders;
47
48 int result = 0;
49 bool isSlowResponse = false;
50
51 std::string defaultDeviceName = "Legacy Device";
52 std::string defaultLocation = "37.256616, 127.052806";
53 std::string defaultLocationName = "Living Room";
54 std::string defaultRegion = "Won";
55 std::string defaultCurrency = "Seoul, Korea";
56
57 OCResourceHandle resourceHandle;
58 OCResourceHandle foundResourceHandle;
59
60 std::shared_ptr< OCResource > g_resource;
61 std::shared_ptr< OCResource > g_room_resource;
62 std::shared_ptr< OCResource > g_light;
63 std::shared_ptr< OCResource > configurationResource;
64 std::vector< string > lights;
65 std::vector< OCResourceHandle > resourceHandleVector;
66
67 GroupManager *groupMgr = new GroupManager();
68 ConfigurationResource *myConfigurationResource;
69 MaintenanceResource *myMaintenanceResource;
70 FactorySetResource *myFactorySetResource;
71
72 std::condition_variable cv1;
73 std::condition_variable cv2;
74 std::condition_variable cv3;
75 std::condition_variable cv4;
76 std::condition_variable cv5;
77
78 bool prepareResponseForResource(std::shared_ptr< OCResourceRequest > request);
79 OCStackResult sendResponseForResource(std::shared_ptr< OCResourceRequest > pRequest);
80 OCEntityHandlerResult entityHandlerForResource(std::shared_ptr< OCResourceRequest > request);
81 OCEntityHandlerResult entityHandlerBootstrap(std::shared_ptr< OCResourceRequest > request);
82
83 typedef std::function< void(OCRepresentation &) > putFunc;
84 typedef std::function< OCRepresentation(void) > getFunc;
85
86 void *ChangeLightRepresentation(void *param);
87 void *handleSlowResponse(void *param, std::shared_ptr< OCResourceRequest > pRequest);
88
89 /****** Light Resource [Required to gtestcases of GroupManager APIs]  ******/
90
91 class LightResource
92 {
93 public:
94     std::string m_power;
95     std::string testing;
96     std::string m_lightUri;
97     OCResourceHandle m_resourceHandle;
98     OCRepresentation m_lightRep;
99
100 public:
101     LightResource() :
102             m_power("on"), m_lightUri("/a/light"), m_resourceHandle(0)
103     {
104         m_lightRep.setUri(m_lightUri);
105         m_lightRep.setValue("power", m_power);
106     }
107
108     void createResource()
109     {
110         std::string resourceURI = m_lightUri;
111         std::string resourceTypeName = "core.light";
112         std::string resourceInterface = DEFAULT_INTERFACE;
113         EntityHandler cb = std::bind(&LightResource::entityHandler, this, PH::_1);
114
115         OCStackResult result = OCPlatform::registerResource(m_resourceHandle, resourceURI,
116                 resourceTypeName, resourceInterface, cb, OC_DISCOVERABLE | OC_OBSERVABLE);
117
118         if (OC_STACK_OK != result)
119         {
120             printf("\nLightResource : OC_STACK_OK != result...");
121         }
122         else
123         {
124             cv2.notify_all();
125             std::mutex blocker;
126             std::condition_variable cv;
127             std::unique_lock < std::mutex > lock(blocker);
128             cv.wait(lock);
129         }
130     }
131
132     OCResourceHandle getHandle()
133     {
134         return m_resourceHandle;
135     }
136
137     void put(OCRepresentation &rep)
138     {
139         try
140         {
141             std::string test;
142             if (rep.getValue < std::string > ("power", test))
143             {
144                 cout << "\t\t\t\t" << "power: " << test << endl;
145             }
146             else
147             {
148                 cout << "\t\t\t\t" << "power not found in the representation" << endl;
149             }
150         }
151         catch (exception &e)
152         {
153             cout << e.what() << endl;
154         }
155     }
156
157     OCRepresentation post(OCRepresentation &rep)
158     {
159         put(rep);
160         return get();
161     }
162
163     OCRepresentation get()
164     {
165         m_lightRep.setValue("power", m_power);
166
167         return m_lightRep;
168     }
169
170     void addType(const std::string &type) const
171     {
172         OCStackResult result = OCPlatform::bindTypeToResource(m_resourceHandle, type);
173         if (OC_STACK_OK != result)
174         {
175             cout << "Binding TypeName to Resource was unsuccessful\n";
176         }
177     }
178
179     void addInterface(const std::string &interface) const
180     {
181         OCStackResult result = OCPlatform::bindInterfaceToResource(m_resourceHandle, interface);
182         if (OC_STACK_OK != result)
183         {
184             cout << "Binding TypeName to Resource was unsuccessful\n";
185         }
186     }
187
188 private:
189     OCEntityHandlerResult entityHandler(std::shared_ptr< OCResourceRequest > request)
190     {
191         cout << "\tIn Server CPP entity handler:\n";
192         OCEntityHandlerResult ehResult = OC_EH_ERROR;
193         if (request)
194         {
195             std::string requestType = request->getRequestType();
196             int requestFlag = request->getRequestHandlerFlag();
197
198             if (requestFlag & RequestHandlerFlag::RequestFlag)
199             {
200                 cout << "\t\trequestFlag : Request\n";
201                 auto pResponse = std::make_shared< OC::OCResourceResponse >();
202                 pResponse->setRequestHandle(request->getRequestHandle());
203                 pResponse->setResourceHandle(request->getResourceHandle());
204
205                 if (requestType == "GET")
206                 {
207                     cout << "\t\t\trequestType : GET\n";
208                     if (isSlowResponse)
209                     {
210                         static int startedThread = 0;
211                         if (!startedThread)
212                         {
213                             std::thread t(handleSlowResponse, (void *) this, request);
214                             startedThread = 1;
215                             t.detach();
216                         }
217                         ehResult = OC_EH_SLOW;
218                     }
219                     else
220                     {
221                         pResponse->setErrorCode(200);
222                         pResponse->setResponseResult(OC_EH_OK);
223                         pResponse->setResourceRepresentation(get());
224                         if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
225                         {
226                             ehResult = OC_EH_OK;
227                         }
228                     }
229                 }
230                 else if (requestType == "PUT")
231                 {
232                     cout << "\t\t\trequestType : PUT\n";
233                     OCRepresentation rep = request->getResourceRepresentation();
234                     put(rep);
235                     pResponse->setErrorCode(200);
236                     pResponse->setResponseResult(OC_EH_OK);
237                     pResponse->setResourceRepresentation(rep);
238                     if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
239                     {
240                         ehResult = OC_EH_OK;
241                     }
242                 }
243                 else if (requestType == "POST")
244                 {
245                     cout << "\t\t\trequestType : POST\n";
246
247                     OCRepresentation rep = request->getResourceRepresentation();
248                     OCRepresentation rep_post = post(rep);
249
250                     pResponse->setResourceRepresentation(rep_post);
251                     pResponse->setErrorCode(200);
252                     if (rep_post.hasAttribute("createduri"))
253                     {
254                         pResponse->setResponseResult(OC_EH_RESOURCE_CREATED);
255                         pResponse->setNewResourceUri(
256                                 rep_post.getValue < std::string > ("createduri"));
257                     }
258
259                     if (OC_STACK_OK == OCPlatform::sendResponse(pResponse))
260                     {
261                         ehResult = OC_EH_OK;
262                     }
263                 }
264                 else if (requestType == "DELETE")
265                 {
266                 }
267             }
268         }
269         else
270         {
271             std::cout << "Request invalid" << std::endl;
272         }
273         return ehResult;
274     }
275 };
276
277 void *handleSlowResponse(void *param, std::shared_ptr< OCResourceRequest > pRequest)
278 {
279     LightResource *lightPtr = (LightResource *) param;
280     sleep(10);
281
282     auto pResponse = std::make_shared< OC::OCResourceResponse >();
283     pResponse->setRequestHandle(pRequest->getRequestHandle());
284     pResponse->setResourceHandle(pRequest->getResourceHandle());
285     pResponse->setResourceRepresentation(lightPtr->get());
286     pResponse->setErrorCode(200);
287     pResponse->setResponseResult(OC_EH_OK);
288
289     isSlowResponse = false;
290     OCPlatform::sendResponse(pResponse);
291     return NULL;
292 }
293
294 /****** Configuration Resource  ******/
295
296 void ConfigurationResource::createResources(ResourceEntityHandler callback)
297 {
298     using namespace OC::OCPlatform;
299
300     if (callback == NULL)
301     {
302         std::cout << "callback should be binded\t";
303         return;
304     }
305
306     OCStackResult result = registerResource(m_configurationHandle, m_configurationUri,
307             m_configurationTypes[0], m_configurationInterfaces[0], callback,
308             OC_DISCOVERABLE | OC_OBSERVABLE);
309
310     if (OC_STACK_OK != result)
311     {
312         std::cout << "Resource creation (configuration) was unsuccessful\n";
313     }
314     else
315     {
316         cv2.notify_all();
317         std::mutex blocker;
318         std::condition_variable cv;
319         std::unique_lock < std::mutex > lock(blocker);
320         cv.wait(lock);
321     }
322 }
323
324 void ConfigurationResource::setConfigurationRepresentation(OCRepresentation &rep)
325 {
326     string value;
327     if (rep.getValue("n", value))
328     {
329         m_deviceName = value;
330         std::cout << "\t\t\t\t" << "m_deviceName: " << m_deviceName << std::endl;
331     }
332
333     if (rep.getValue("loc", value))
334     {
335         m_location = value;
336         std::cout << "\t\t\t\t" << "m_location: " << m_location << std::endl;
337     }
338
339     if (rep.getValue("locn", value))
340     {
341         m_locationName = value;
342         std::cout << "\t\t\t\t" << "m_locationName: " << m_locationName << std::endl;
343     }
344
345     if (rep.getValue("c", value))
346     {
347         m_currency = value;
348         std::cout << "\t\t\t\t" << "m_currency: " << m_currency << std::endl;
349     }
350
351     if (rep.getValue("r", value))
352     {
353         m_region = value;
354         std::cout << "\t\t\t\t" << "m_region: " << m_region << std::endl;
355     }
356 }
357
358 OCRepresentation ConfigurationResource::getConfigurationRepresentation()
359 {
360     m_configurationRep.setValue("n", m_deviceName);
361     m_configurationRep.setValue("loc", m_location);
362     m_configurationRep.setValue("locn", m_locationName);
363     m_configurationRep.setValue("c", m_currency);
364     m_configurationRep.setValue("r", m_region);
365
366     return m_configurationRep;
367 }
368
369 std::string ConfigurationResource::getUri()
370 {
371     return m_configurationUri;
372 }
373
374 void ConfigurationResource::factoryReset()
375 {
376     m_deviceName = defaultDeviceName;
377     m_location = defaultLocation;
378     m_locationName = defaultLocationName;
379     m_currency = defaultCurrency;
380     m_region = defaultRegion;
381 }
382
383 /****** FactorySet  Resource  ******/
384
385 FactorySetResource::FactorySetResource()
386 {
387     m_configurationUri = "/factoryset"; // URI of the resource
388     m_configurationTypes.clear();
389     m_configurationTypes.push_back("factoryset"); // resource type name.
390     m_configurationRep.setUri(m_configurationUri);
391     m_configurationRep.setResourceTypes(m_configurationTypes);
392 }
393
394 FactorySetResource::~FactorySetResource()
395 {
396 }
397
398 void FactorySetResource::createResources(ResourceEntityHandler callback)
399 {
400     using namespace OC::OCPlatform;
401
402     if (callback == NULL)
403     {
404         std::cout << "callback should be binded\t";
405         return;
406     }
407
408     OCStackResult result = registerResource(m_configurationHandle, m_configurationUri,
409             m_configurationTypes[0], m_configurationInterfaces[0], callback,
410             OC_DISCOVERABLE | OC_OBSERVABLE);
411
412     if (OC_STACK_OK != result)
413     {
414         std::cout << "Resource creation (configuration) was unsuccessful\n";
415     }
416
417     else
418     {
419         cv4.notify_all();
420         std::mutex blocker;
421         std::condition_variable cv;
422         std::unique_lock < std::mutex > lock(blocker);
423         cv.wait(lock);
424     }
425 }
426
427 void FactorySetResource::setFactorySetRepresentation(OCRepresentation &rep)
428 {
429     string value;
430
431     if (rep.getValue("n", value))
432     {
433         m_deviceName = value;
434         std::cout << "\t\t\t\t" << "m_deviceName: " << m_deviceName << std::endl;
435     }
436
437     if (rep.getValue("loc", value))
438     {
439         m_location = value;
440         std::cout << "\t\t\t\t" << "m_location: " << m_location << std::endl;
441     }
442
443     if (rep.getValue("locn", value))
444     {
445         m_locationName = value;
446         std::cout << "\t\t\t\t" << "m_locationName: " << m_locationName << std::endl;
447     }
448
449     if (rep.getValue("c", value))
450     {
451         m_currency = value;
452         std::cout << "\t\t\t\t" << "m_currency: " << m_currency << std::endl;
453     }
454
455     if (rep.getValue("r", value))
456     {
457         m_region = value;
458         std::cout << "\t\t\t\t" << "m_region: " << m_region << std::endl;
459     }
460 }
461
462 OCRepresentation FactorySetResource::getFactorySetRepresentation()
463 {
464     m_configurationRep.setValue("n", m_deviceName);
465     m_configurationRep.setValue("loc", m_location);
466     m_configurationRep.setValue("locn", m_locationName);
467     m_configurationRep.setValue("c", m_currency);
468     m_configurationRep.setValue("r", m_region);
469
470     return m_configurationRep;
471 }
472
473 std::string FactorySetResource::getUri()
474 {
475     return m_configurationUri;
476 }
477
478 /****** Maintenance Resource ********/
479
480 void MaintenanceResource::createResources(ResourceEntityHandler callback)
481 {
482     using namespace OC::OCPlatform;
483
484     if (callback == NULL)
485     {
486         std::cout << "callback should be binded\t";
487         return;
488     }
489
490     OCStackResult result = registerResource(m_maintenanceHandle, m_maintenanceUri,
491             m_maintenanceTypes[0], m_maintenanceInterfaces[0], callback,
492             OC_DISCOVERABLE | OC_OBSERVABLE);
493
494     if (OC_STACK_OK != result)
495     {
496         std::cout << "Resource creation (maintenance) was unsuccessful\n";
497     }
498
499     thread exec(
500             std::function< void(int second) >(
501                     std::bind(&MaintenanceResource::maintenanceMonitor, this,
502                             std::placeholders::_1)), 10);
503     exec.detach();
504     cv3.notify_all();
505     std::mutex blocker;
506     std::condition_variable cv;
507     std::unique_lock < std::mutex > lock(blocker);
508     cv.wait(lock);
509
510     std::cout << "maintenance Resource is Created!\n";
511 }
512
513 void MaintenanceResource::setMaintenanceRepresentation(OCRepresentation &rep)
514 {
515     string value;
516
517     if (rep.getValue("fr", value))
518     {
519         m_factoryReset = value;
520         std::cout << "\t\t\t\t" << "m_factoryReset: " << m_factoryReset << std::endl;
521     }
522
523     if (rep.getValue("rb", value))
524     {
525         m_reboot = value;
526         std::cout << "\t\t\t\t" << "m_reboot: " << m_reboot << std::endl;
527     }
528
529     if (rep.getValue("ssc", value))
530     {
531         m_startStatCollection = value;
532         std::cout << "\t\t\t\t" << "m_startStatCollection: " << m_startStatCollection << std::endl;
533     }
534 }
535
536 OCRepresentation MaintenanceResource::getMaintenanceRepresentation()
537 {
538     m_maintenanceRep.setValue("fr", m_factoryReset);
539     m_maintenanceRep.setValue("rb", m_reboot);
540     m_maintenanceRep.setValue("ssc", m_startStatCollection);
541
542     return m_maintenanceRep;
543 }
544
545 std::string MaintenanceResource::getUri()
546 {
547     return m_maintenanceUri;
548 }
549
550 void MaintenanceResource::maintenanceMonitor(int second)
551 {
552     while (1)
553     {
554         sleep(second);
555
556         if (m_reboot == "true")
557         {
558             int res;
559             std::cout << "Reboot will be soon..." << std::endl;
560             m_reboot = defaultReboot;
561             res = system("/usr/bin/sudo /etc/init.d/reboot");
562
563             std::cout << "return: " << res << std::endl;
564
565         }
566         else if (m_factoryReset == "true")
567         {
568             std::cout << "Factory Reset will be soon..." << std::endl;
569             m_factoryReset = defaultFactoryReset;
570             factoryReset();
571         }
572     }
573 }
574
575 getFunc getGetFunction(std::string uri)
576 {
577     getFunc res = NULL;
578
579     if (uri == myConfigurationResource->getUri())
580     {
581         res = std::bind(&ConfigurationResource::getConfigurationRepresentation,
582                 myConfigurationResource);
583     }
584     else if (uri == myMaintenanceResource->getUri())
585     {
586         res = std::bind(&MaintenanceResource::getMaintenanceRepresentation, myMaintenanceResource);
587     }
588     return res;
589 }
590
591 putFunc getPutFunction(std::string uri)
592 {
593     putFunc res = NULL;
594
595     if (uri == myConfigurationResource->getUri())
596     {
597         res = std::bind(&ConfigurationResource::setConfigurationRepresentation,
598                 myConfigurationResource, std::placeholders::_1);
599     }
600     else if (uri == myMaintenanceResource->getUri())
601     {
602         res = std::bind(&MaintenanceResource::setMaintenanceRepresentation, myMaintenanceResource,
603                 std::placeholders::_1);
604     }
605     return res;
606 }
607
608 bool prepareResponseForResource(std::shared_ptr< OCResourceRequest > request)
609 {
610     std::cout << "\tIn Server CPP prepareResponseForResource:\n";
611     bool result = false;
612     if (request)
613     {
614         std::string requestType = request->getRequestType();
615         int requestFlag = request->getRequestHandlerFlag();
616
617         if (requestFlag == RequestHandlerFlag::RequestFlag)
618         {
619             std::cout << "\t\trequestFlag : Request\n";
620             if (requestType == "GET")
621             {
622                 std::cout << "\t\t\trequestType : GET\n";
623                 result = true;
624             }
625             else if (requestType == "PUT")
626             {
627                 std::cout << "\t\t\trequestType : PUT\n";
628                 putFunc putFunction;
629                 OCRepresentation rep = request->getResourceRepresentation();
630
631                 putFunction = getPutFunction(request->getResourceUri());
632                 putFunction(rep);
633                 result = true;
634             }
635             else if (requestType == "POST")
636             {
637             }
638             else if (requestType == "DELETE")
639             {
640             }
641         }
642         else if (requestFlag == RequestHandlerFlag::ObserverFlag)
643         {
644             std::cout << "\t\trequestFlag : Observer\n";
645         }
646     }
647     else
648     {
649         std::cout << "Request invalid" << std::endl;
650     }
651
652     return result;
653 }
654
655 OCStackResult sendResponseForResource(std::shared_ptr< OCResourceRequest > pRequest)
656 {
657     auto pResponse = std::make_shared< OC::OCResourceResponse >();
658     QueryParamsMap queryParamsMap = pRequest->getQueryParameters();
659
660     pResponse->setRequestHandle(pRequest->getRequestHandle());
661     pResponse->setResourceHandle(pRequest->getResourceHandle());
662
663     getFunc getFunction;
664     getFunction = getGetFunction(pRequest->getResourceUri());
665
666     OCRepresentation rep;
667     rep = getFunction();
668
669     auto findRes = queryParamsMap.find("if");
670
671     if (findRes != queryParamsMap.end())
672     {
673         pResponse->setResourceRepresentation(rep, findRes->second);
674     }
675     else
676     {
677         pResponse->setResourceRepresentation(rep, DEFAULT_INTERFACE);
678     }
679
680     pResponse->setErrorCode(200);
681     pResponse->setResponseResult(OC_EH_OK);
682
683     return OCPlatform::sendResponse(pResponse);
684 }
685
686 OCEntityHandlerResult entityHandlerForResource(std::shared_ptr< OCResourceRequest > request)
687 {
688     std::cout << "\tIn Server CPP (entityHandlerForResource) entity handler:\n";
689     OCEntityHandlerResult ehResult = OC_EH_ERROR;
690
691     QueryParamsMap test = request->getQueryParameters();
692
693     if (prepareResponseForResource(request))
694     {
695         if (OC_STACK_OK == sendResponseForResource(request))
696         {
697             ehResult = OC_EH_OK;
698         }
699         else
700         {
701             std::cout << "sendResponse failed." << std::endl;
702         }
703     }
704     else
705     {
706         std::cout << "PrepareResponse failed." << std::endl;
707     }
708     return ehResult;
709 }
710
711 /****** BootStrap Resource [Required for doBootstrap API of ThingsConfiguration class]  ******/
712
713 class BootstrapResource
714 {
715 public:
716     std::string m_bootstrapUri;
717     std::vector< std::string > m_bootstrapTypes;
718     std::vector< std::string > m_bootstrapInterfaces;
719     OCResourceHandle m_bootstrapHandle;
720     OCRepresentation m_bootstrapRep;
721
722 public:
723     BootstrapResource()
724     {
725         m_bootstrapUri = "/bootstrap";
726         m_bootstrapTypes.push_back("bootstrap");
727         m_bootstrapInterfaces.push_back(DEFAULT_INTERFACE);
728         m_bootstrapRep.setUri(m_bootstrapUri);
729         m_bootstrapRep.setResourceTypes(m_bootstrapTypes);
730         m_bootstrapRep.setResourceInterfaces(m_bootstrapInterfaces);
731         m_bootstrapHandle = NULL;
732     }
733     void createResources()
734     {
735         using namespace OC::OCPlatform;
736         OCStackResult result = registerResource(m_bootstrapHandle, m_bootstrapUri,
737                 m_bootstrapTypes[0], m_bootstrapInterfaces[0], entityHandlerBootstrap,
738                 OC_DISCOVERABLE | OC_OBSERVABLE);
739
740         if (OC_STACK_OK != result)
741         {
742             cout << "Resource creation (room) was unsuccessful\n";
743         }
744
745         cv5.notify_all();
746         std::mutex blocker;
747         std::condition_variable cv;
748         std::unique_lock < std::mutex > lock(blocker);
749         cv.wait(lock);
750     }
751
752     void setBootstrapRepresentation(OCRepresentation& /*rep*/)
753     {
754     }
755
756     OCRepresentation getBootstrapRepresentation()
757     {
758         m_bootstrapRep.setValue < std::string > ("n", defaultDeviceName);
759         m_bootstrapRep.setValue < std::string > ("loc", defaultLocation);
760         m_bootstrapRep.setValue < std::string > ("locn", defaultLocationName);
761         m_bootstrapRep.setValue < std::string > ("c", defaultCurrency);
762         m_bootstrapRep.setValue < std::string > ("r", defaultRegion);
763
764         return m_bootstrapRep;
765     }
766 };
767
768 BootstrapResource myBootstrapResource;
769
770 bool prepareResponse(std::shared_ptr< OCResourceRequest > request)
771 {
772     cout << "\tIn Server CPP prepareResponse:\n";
773     bool result = false;
774     if (request)
775     {
776         std::string requestType = request->getRequestType();
777         int requestFlag = request->getRequestHandlerFlag();
778
779         if (requestFlag == RequestHandlerFlag::RequestFlag)
780         {
781             cout << "\t\trequestFlag : Request\n";
782             if (requestType == "GET")
783             {
784                 cout << "\t\t\trequestType : GET\n";
785                 result = true;
786             }
787             else if (requestType == "PUT")
788             {
789                 cout << "\t\t\trequestType : PUT\n";
790
791                 OCRepresentation rep = request->getResourceRepresentation();
792                 myBootstrapResource.setBootstrapRepresentation(rep);
793                 result = true;
794             }
795             else if (requestType == "POST")
796             {
797             }
798             else if (requestType == "DELETE")
799             {
800             }
801         }
802         else if (requestFlag == RequestHandlerFlag::ObserverFlag)
803         {
804             cout << "\t\trequestFlag : Observer\n";
805         }
806     }
807     else
808     {
809         std::cout << "Request invalid" << std::endl;
810     }
811
812     return result;
813 }
814
815 OCStackResult sendResponse(std::shared_ptr< OCResourceRequest > pRequest)
816 {
817     auto pResponse = std::make_shared< OC::OCResourceResponse >();
818     pResponse->setRequestHandle(pRequest->getRequestHandle());
819     pResponse->setResourceHandle(pRequest->getResourceHandle());
820     pResponse->setResourceRepresentation(myBootstrapResource.getBootstrapRepresentation());
821     pResponse->setErrorCode(200);
822     pResponse->setResponseResult(OC_EH_OK);
823
824     return OCPlatform::sendResponse(pResponse);
825 }
826
827 OCEntityHandlerResult entityHandlerBootstrap(std::shared_ptr< OCResourceRequest > request)
828 {
829     cout << "\tIn Server CPP (entityHandlerBootstrap) entity handler:\n";
830     OCEntityHandlerResult ehResult = OC_EH_ERROR;
831
832     if (prepareResponse(request))
833     {
834         if (OC_STACK_OK == sendResponse(request))
835         {
836             ehResult = OC_EH_OK;
837         }
838         else
839         {
840             std::cout << "sendResponse failed." << std::endl;
841         }
842     }
843     else
844     {
845         std::cout << "PrepareResponse failed." << std::endl;
846     }
847     return ehResult;
848 }
849
850 /****** gtest class ******/
851
852 class ThingsManagerTest: public TestWithMock
853 {
854 public:
855     void Proceed()
856     {
857         cond.notify_all();
858     }
859
860     void Wait(int waitingTime = DEFAULT_WAITING_TIME_IN_MILLIS)
861     {
862         std::unique_lock < std::mutex > lock
863         { mutex };
864         cond.wait_for(lock, std::chrono::milliseconds
865         { waitingTime });
866     }
867
868 protected:
869     void SetUp()
870     {
871         TestWithMock::SetUp();
872     }
873
874     void TearDown()
875     {
876         TestWithMock::TearDown();
877     }
878
879 private:
880     std::condition_variable cond;
881     std::mutex mutex;
882 };
883
884 //Callbacks
885 void onUpdate(const HeaderOptions& /*headerOptions*/, const OCRepresentation& /*rep*/,
886         const int /*eCode*/)
887 {
888 }
889
890 void onGetBootstrapInformation(const HeaderOptions& /*headerOptions*/,
891         const OCRepresentation& /*rep*/, const int /*eCode*/)
892 {
893 }
894
895 void onReboot(const HeaderOptions& /*headerOptions*/, const OCRepresentation& /*rep*/,
896         const int /*eCode*/)
897 {
898 }
899
900 void onFactoryReset(const HeaderOptions& /*headerOptions*/, const OCRepresentation& /*rep*/,
901         const int /*eCode*/)
902 {
903 }
904
905 void onGet(const HeaderOptions& /*headerOptions*/, const OCRepresentation& /*rep*/,
906         const int /*eCode*/)
907 {
908 }
909
910 void onPut(const HeaderOptions& /*headerOptions*/, const OCRepresentation& /*rep*/,
911         const int /*eCode*/)
912 {
913 }
914
915 void onPost(const HeaderOptions& /*headerOptions*/, const OCRepresentation& /*rep*/,
916         const int /*eCode*/)
917 {
918 }
919
920 void foundResources(std::vector< std::shared_ptr< OC::OCResource > > listOfResource)
921 {
922     for (auto rsrc = listOfResource.begin(); rsrc != listOfResource.end(); ++rsrc)
923     {
924         if(((*rsrc)->connectivityType() & CT_ADAPTER_TCP) == CT_ADAPTER_TCP)
925         {
926             continue;
927         }
928
929         std::string resourceURI = (*rsrc)->uri();
930         std::string hostAddress = (*rsrc)->host();
931
932         if (resourceURI == "/a/light")
933         {
934             result = OCPlatform::registerResource(foundResourceHandle, (*rsrc));
935             if (result == OC_STACK_OK)
936             {
937                 OCPlatform::bindResource(resourceHandle, foundResourceHandle);
938                 resourceHandleVector.push_back(foundResourceHandle);
939             }
940             else
941             {
942                 cout << "\tresource Error!" << endl;
943             }
944             lights.push_back((hostAddress + resourceURI));
945
946             g_light = (*rsrc);
947         }
948         else
949         {
950             configurationResource = (*rsrc);
951         }
952     }
953     cv2.notify_all();
954 }
955
956 void foundGroupResource(std::shared_ptr< OCResource > resource)
957 {
958     if((resource->connectivityType() & CT_ADAPTER_TCP) == CT_ADAPTER_TCP)
959     {
960         return ;
961     }
962
963     std::string resourceURI;
964     resourceURI = resource->uri();
965     if (resourceURI == "/core/a/collection")
966     {
967         g_resource = resource;
968     }
969     else
970     {
971         g_room_resource = resource;
972     }
973     cv1.notify_all();
974 }
975
976 //This test case is to create the lightserver , BootstrapServer & configuration sever
977 TEST_F(ThingsManagerTest, testCreateResources)
978 {
979     PlatformConfig cfg
980     {   OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos};
981     OCPlatform::Configure(cfg);
982
983     LightResource myLight;
984     std::thread t1(&LightResource::createResource, &myLight);
985     t1.detach();
986     std::mutex blocker1;
987     std::unique_lock < std::mutex > lock1(blocker1);
988     cv2.wait(lock1);
989
990     std::thread t2(&BootstrapResource::createResources, &myBootstrapResource);
991     t2.detach();
992     std::mutex blocker2;
993     std::unique_lock < std::mutex > lock2(blocker2);
994     cv5.wait(lock2);
995
996     myConfigurationResource = new ConfigurationResource();
997     std::thread t3(&ConfigurationResource::createResources, myConfigurationResource,
998             &entityHandlerForResource);
999
1000     t3.detach();
1001     std::mutex blocker3;
1002     std::unique_lock < std::mutex > lock3(blocker3);
1003     cv2.wait(lock3);
1004
1005     myMaintenanceResource = new MaintenanceResource();
1006     std::thread t4(&MaintenanceResource::createResources, myMaintenanceResource,
1007             &entityHandlerForResource);
1008     t4.detach();
1009
1010     std::mutex blocker4;
1011     std::unique_lock < std::mutex > lock4(blocker4);
1012     cv3.wait(lock4);
1013
1014     myFactorySetResource = new FactorySetResource();
1015     std::thread t5(&FactorySetResource::createResources, myFactorySetResource,
1016             &entityHandlerForResource);
1017     t5.detach();
1018
1019     std::mutex blocker5;
1020     std::unique_lock < std::mutex > lock5(blocker5);
1021     cv4.wait(lock5);
1022
1023     myMaintenanceResource->factoryReset = std::function < void()
1024     > (std::bind(&ConfigurationResource::factoryReset,
1025                     myConfigurationResource));
1026 }
1027
1028 //Check findCandidateResources
1029 TEST_F(ThingsManagerTest, testFindCandidateResources)
1030 {
1031
1032     string resourceURI = "/core/a/collection";
1033     string resourceTypeName = "a.collection";
1034     string resourceInterface = BATCH_INTERFACE;
1035
1036     OCStackResult res = OCPlatform::registerResource(resourceHandle, resourceURI,
1037             resourceTypeName, resourceInterface, NULL, OC_DISCOVERABLE);
1038
1039     if ( res != OC_STACK_OK )
1040     {
1041         cout << "Resource registeration failed." << endl;
1042     }
1043
1044     OCPlatform::bindInterfaceToResource(resourceHandle, GROUP_INTERFACE);
1045     OCPlatform::bindInterfaceToResource(resourceHandle, DEFAULT_INTERFACE);
1046
1047     std::string query = OC_RSRVD_WELL_KNOWN_URI;
1048     query.append("?rt=");
1049     query.append(resourceTypeName);
1050
1051     OCPlatform::findResource("", query, CT_DEFAULT, &foundGroupResource);
1052
1053     std::mutex blocker1;
1054     std::unique_lock < std::mutex > lock1(blocker1);
1055     cv1.wait(lock1);
1056
1057     GroupManager *instance = new GroupManager();
1058     vector<string> types;
1059     types.push_back("core.light");
1060
1061     result = instance->findCandidateResources(types, &foundResources);
1062
1063     std::mutex blocker2;
1064     std::unique_lock < std::mutex > lock2(blocker2);
1065     cv2.wait(lock2);
1066 }
1067
1068 //Find Candidate Resource when no resources are specified
1069 TEST_F(ThingsManagerTest, testFindCandidateResourcesEmptyResourceType)
1070 {
1071     GroupManager *instance = new GroupManager();
1072     vector<string> types;
1073     result = instance->findCandidateResources(types, &foundResources);
1074     EXPECT_TRUE(result == OC_STACK_ERROR);
1075     delete instance;
1076 }
1077
1078 //Find Candidate Resource when Callback is null
1079 TEST_F(ThingsManagerTest, testFindCandidateResourcesNullCallback)
1080 {
1081     GroupManager *instance = new GroupManager();
1082     vector<string> types;
1083     types.push_back("core.light");
1084     result = instance->findCandidateResources(types, NULL);
1085     EXPECT_TRUE(result == OC_STACK_ERROR);
1086     delete instance;
1087 }
1088
1089 //test bind resource to group
1090 TEST_F(ThingsManagerTest, testBindResourceToGroup)
1091 {
1092     GroupManager *instance = new GroupManager();
1093     OCResourceHandle rHandle = NULL;
1094
1095     string resourceURI = "/core/room-large";
1096     string resourceTypeName = "core.room-large";
1097     string resourceInterface = BATCH_INTERFACE;
1098
1099     OCStackResult res = OCPlatform::registerResource(rHandle, resourceURI,
1100             resourceTypeName, resourceInterface, NULL, OC_DISCOVERABLE);
1101
1102     if ( res != OC_STACK_OK )
1103     {
1104         cout << "Resource registeration failed." << endl;
1105     }
1106
1107     OCPlatform::bindInterfaceToResource(rHandle, GROUP_INTERFACE);
1108     OCPlatform::bindInterfaceToResource(rHandle, DEFAULT_INTERFACE);
1109
1110     std::string query = OC_RSRVD_WELL_KNOWN_URI;
1111     query.append("?rt=");
1112     query.append(resourceTypeName);
1113
1114     OCPlatform::findResource("", query, CT_DEFAULT, &foundGroupResource);
1115
1116     std::mutex blocker1;
1117     std::unique_lock < std::mutex > lock1(blocker1);
1118     cv1.wait(lock1);
1119
1120     result = instance->bindResourceToGroup (resourceHandle, g_room_resource, rHandle);
1121
1122     EXPECT_TRUE(result == OC_STACK_OK);
1123     delete instance;
1124 }
1125
1126 //Add actionset
1127 TEST_F(ThingsManagerTest, testAddActionSetAllBulbOff)
1128 {
1129     string actionsetDesc;
1130     ActionSet *allBulbOff = new ActionSet();
1131     allBulbOff->actionsetName = "AllBulbOff";
1132
1133     mocks.ExpectCallFunc(onPut).
1134     Do([this](const HeaderOptions& /*headerOptions*/, const OCRepresentation& /*rep*/,
1135             const int /*eCode*/) {   Proceed();});
1136
1137     for (auto iter = lights.begin(); iter != lights.end(); ++iter)
1138     {
1139         Action *action = new Action();
1140         action->target = (*iter);
1141
1142         Capability *capa = new Capability();
1143         capa->capability = "power";
1144         capa->status = "off";
1145
1146         action->listOfCapability.push_back(capa);
1147         allBulbOff->listOfAction.push_back(action);
1148     }
1149     if (g_resource)
1150     {
1151         result = groupMgr->addActionSet(g_resource, allBulbOff, &onPut);
1152         Wait();
1153         EXPECT_TRUE(result == OC_STACK_OK);
1154         result = 0;
1155     }
1156     delete allBulbOff;
1157 }
1158
1159 //Add actionset with NULL resource
1160 TEST_F(ThingsManagerTest, testAddActionSetAllBulbOffResourceNull)
1161 {
1162     string actionsetDesc;
1163     ActionSet *allBulbOff = new ActionSet();
1164     allBulbOff->actionsetName = "AllBulbOff";
1165
1166     for (auto iter = lights.begin(); iter != lights.end(); ++iter)
1167     {
1168         Action *action = new Action();
1169         action->target = (*iter);
1170
1171         Capability *capa = new Capability();
1172         capa->capability = "power";
1173         capa->status = "off";
1174
1175         action->listOfCapability.push_back(capa);
1176         allBulbOff->listOfAction.push_back(action);
1177     }
1178
1179     result = groupMgr->addActionSet(NULL, allBulbOff, &onPut);
1180     Wait();
1181     EXPECT_TRUE(result == OC_STACK_ERROR);
1182     result = 0;
1183
1184     delete allBulbOff;
1185 }
1186
1187 //Add actionset with NULL ActionSet
1188 TEST_F(ThingsManagerTest, testAddActionSetAllBulbOffActionsetNull)
1189 {
1190     if (g_resource)
1191     {
1192         result = groupMgr->addActionSet(g_resource, NULL, &onPut);
1193         Wait();
1194         EXPECT_TRUE(result == OC_STACK_ERROR);
1195         result = 0;
1196     }
1197 }
1198
1199 //Add actionset
1200 TEST_F(ThingsManagerTest, testAddActionSetAllBulbOn)
1201 {
1202     string actionsetDesc;
1203     ActionSet *allBulbON = new ActionSet();
1204     allBulbON->actionsetName = "AllBulbOn";
1205
1206     mocks.ExpectCallFunc(onPut).
1207     Do([this](const HeaderOptions& /*headerOptions*/, const OCRepresentation& /*rep*/,
1208             const int /*eCode*/) {   Proceed();});
1209
1210     for (auto iter = lights.begin(); iter != lights.end(); ++iter)
1211     {
1212         Action *action = new Action();
1213         action->target = (*iter);
1214
1215         Capability *capa = new Capability();
1216         capa->capability = "power";
1217         capa->status = "on";
1218
1219         action->listOfCapability.push_back(capa);
1220         allBulbON->listOfAction.push_back(action);
1221     }
1222     if (g_resource)
1223     {
1224         result = groupMgr->addActionSet(g_resource, allBulbON, onPut);
1225         Wait();
1226         EXPECT_TRUE(result == OC_STACK_OK);
1227         result = 0;
1228     }
1229     delete allBulbON;
1230 }
1231
1232 //Add actionset with NULL Resource
1233 TEST_F(ThingsManagerTest, testAddActionSetAllBulbOnResourceNull)
1234 {
1235     string actionsetDesc;
1236     ActionSet *allBulbON = new ActionSet();
1237     allBulbON->actionsetName = "AllBulbOn";
1238
1239     for (auto iter = lights.begin(); iter != lights.end(); ++iter)
1240     {
1241         Action *action = new Action();
1242         action->target = (*iter);
1243
1244         Capability *capa = new Capability();
1245         capa->capability = "power";
1246         capa->status = "on";
1247
1248         action->listOfCapability.push_back(capa);
1249         allBulbON->listOfAction.push_back(action);
1250     }
1251
1252     result = groupMgr->addActionSet(NULL, allBulbON, onPut);
1253     Wait();
1254     EXPECT_TRUE(result == OC_STACK_ERROR);
1255     result = 0;
1256
1257     delete allBulbON;
1258 }
1259
1260 //Add actionset with NULL ActionSet
1261 TEST_F(ThingsManagerTest, testAddActionSetAllBulbOnActionSetNull)
1262 {
1263     if (g_resource)
1264     {
1265         result = groupMgr->addActionSet(g_resource, NULL, onPut);
1266         Wait();
1267         EXPECT_TRUE(result == OC_STACK_ERROR);
1268         result = 0;
1269     }
1270 }
1271
1272 //Execute actionset
1273 TEST_F(ThingsManagerTest, testExecuteActionSetAllBulbOn)
1274 {
1275     string actionsetDesc;
1276     ActionSet *allBulbON = new ActionSet();
1277     allBulbON->actionsetName = "AllBulbOn1";
1278
1279     mocks.ExpectCallFunc(onPut).
1280     Do([this](const HeaderOptions& /*headerOptions*/, const OCRepresentation& /*rep*/,
1281             const int /*eCode*/) {   Proceed();});
1282
1283     for (auto iter = lights.begin(); iter != lights.end(); ++iter)
1284     {
1285         Action *action = new Action();
1286         action->target = (*iter);
1287
1288         Capability *capa = new Capability();
1289         capa->capability = "power";
1290         capa->status = "on";
1291
1292         action->listOfCapability.push_back(capa);
1293         allBulbON->listOfAction.push_back(action);
1294     }
1295     if (g_resource)
1296     {
1297         result = groupMgr->addActionSet(g_resource, allBulbON, onPut);
1298         Wait();
1299         EXPECT_TRUE(result == OC_STACK_OK);
1300         result = 0;
1301     }
1302
1303     mocks.ExpectCallFunc(onPost).
1304     Do([this](const HeaderOptions& /*headerOptions*/, const OCRepresentation& /*rep*/,
1305             const int /*eCode*/) {   Proceed();});
1306
1307     if (g_resource)
1308     {
1309         result = groupMgr->executeActionSet(g_resource, "AllBulbOn", &onPost);
1310         Wait();
1311         EXPECT_TRUE(result == OC_STACK_OK);
1312         result = 0;
1313     }
1314     delete allBulbON;
1315 }
1316
1317 //Execute actionset with NULL Resource
1318 TEST_F(ThingsManagerTest, testExecuteActionSetAllBulbOnResourceNull)
1319 {
1320     result = groupMgr->executeActionSet(NULL, "AllBulbOn", &onPost);
1321     Wait();
1322     EXPECT_TRUE(result == OC_STACK_ERROR);
1323     result = 0;
1324 }
1325
1326 //Execute actionset
1327 TEST_F(ThingsManagerTest, testExecuteActionSetAllBulbOff)
1328 {
1329     mocks.ExpectCallFunc(onPost).
1330     Do([this](const HeaderOptions& /*headerOptions*/, const OCRepresentation& /*rep*/,
1331             const int /*eCode*/) {   Proceed();});
1332
1333     if (g_resource)
1334     {
1335         result = groupMgr->executeActionSet(g_resource, "AllBulbOff", &onPost);
1336         Wait();
1337         EXPECT_TRUE(result == OC_STACK_OK);
1338         result = 0;
1339     }
1340 }
1341
1342 //Execute actionset with NULL resource
1343 TEST_F(ThingsManagerTest, testExecuteActionSetAllBulbOffResourceNull)
1344 {
1345     result = groupMgr->executeActionSet(NULL, "AllBulbOff", &onPost);
1346     Wait();
1347     EXPECT_TRUE(result == OC_STACK_ERROR);
1348     result = 0;
1349 }
1350
1351 //Execute actionset with Delay
1352 TEST_F(ThingsManagerTest, testExcecuteActionSetWithDelay)
1353 {
1354     string actionsetDesc;
1355     ActionSet *allBulbON = new ActionSet();
1356     allBulbON->actionsetName = "AllBulbOnDelay";
1357     allBulbON->setDelay(1);
1358
1359     mocks.ExpectCallFunc(onPut).
1360     Do([this](const HeaderOptions& /*headerOptions*/, const OCRepresentation& /*rep*/,
1361             const int /*eCode*/) {   Proceed();});
1362
1363     for (auto iter = lights.begin(); iter != lights.end(); ++iter)
1364     {
1365         Action *action = new Action();
1366         action->target = (*iter);
1367
1368         Capability *capa = new Capability();
1369         capa->capability = "power";
1370         capa->status = "off";
1371
1372         action->listOfCapability.push_back(capa);
1373         allBulbON->listOfAction.push_back(action);
1374     }
1375     if (g_resource)
1376     {
1377         result = groupMgr->addActionSet(g_resource, allBulbON, onPut);
1378         Wait();
1379     }
1380
1381     mocks.ExpectCallFunc(onPost).
1382     Do([this](const HeaderOptions& /*headerOptions*/, const OCRepresentation& /*rep*/,
1383             const int /*eCode*/) {   Proceed();});
1384
1385     if (g_resource)
1386     {
1387         result = groupMgr->executeActionSet(g_resource, "AllBulbOnDelay", &onPost);
1388         Wait();
1389         EXPECT_TRUE(result == OC_STACK_OK);
1390         result = 0;
1391     }
1392
1393     delete allBulbON;
1394 }
1395
1396 //Execute actionset with Delay = 0
1397 TEST_F(ThingsManagerTest, testExcecuteActionSetWithDelayEqulasZero)
1398 {
1399     string actionsetDesc;
1400     ActionSet *allBulbON = new ActionSet();
1401     allBulbON->actionsetName = "AllBulbOnDelay";
1402
1403     mocks.ExpectCallFunc(onPut).
1404     Do([this](const HeaderOptions& /*headerOptions*/, const OCRepresentation& /*rep*/,
1405             const int /*eCode*/) {   Proceed();});
1406
1407     for (auto iter = lights.begin(); iter != lights.end(); ++iter)
1408     {
1409         Action *action = new Action();
1410         action->target = (*iter);
1411
1412         Capability *capa = new Capability();
1413         capa->capability = "power";
1414         capa->status = "off";
1415
1416         action->listOfCapability.push_back(capa);
1417         allBulbON->listOfAction.push_back(action);
1418     }
1419     if (g_resource)
1420     {
1421         result = groupMgr->addActionSet(g_resource, allBulbON, onPut);
1422         Wait();
1423     }
1424
1425     if (g_resource)
1426     {
1427         result = groupMgr->executeActionSet(g_resource, "AllBulbOnDelay", 0, &onPost);
1428         Wait();
1429         EXPECT_TRUE(result == OC_STACK_INVALID_PARAM);
1430         result = 0;
1431     }
1432
1433     delete allBulbON;
1434 }
1435
1436 //Execute actionset with invalid Delay
1437 TEST_F(ThingsManagerTest, testExcecuteActionSetWithInvalidDelay)
1438 {
1439     string actionsetDesc;
1440     ActionSet *allBulbON = new ActionSet();
1441     allBulbON->actionsetName = "AllBulbOnDelay";
1442
1443     mocks.ExpectCallFunc(onPut).
1444     Do([this](const HeaderOptions& /*headerOptions*/, const OCRepresentation& /*rep*/,
1445             const int /*eCode*/) {   Proceed();});
1446
1447     for (auto iter = lights.begin(); iter != lights.end(); ++iter)
1448     {
1449         Action *action = new Action();
1450         action->target = (*iter);
1451
1452         Capability *capa = new Capability();
1453         capa->capability = "power";
1454         capa->status = "off";
1455
1456         action->listOfCapability.push_back(capa);
1457         allBulbON->listOfAction.push_back(action);
1458     }
1459     if (g_resource)
1460     {
1461         result = groupMgr->addActionSet(g_resource, allBulbON, onPut);
1462         Wait();
1463     }
1464
1465     if (g_resource)
1466     {
1467         result = groupMgr->executeActionSet(g_resource, "AllBulbOnDelay", -10, &onPost);
1468         Wait();
1469         EXPECT_TRUE(result == OC_STACK_INVALID_PARAM);
1470         result = 0;
1471     }
1472
1473     delete allBulbON;
1474 }
1475
1476 //Execute actionset with delay on NULL Resource
1477 TEST_F(ThingsManagerTest, testExcecuteActionSetWithDelayWithResourceNull)
1478 {
1479     string actionsetDesc;
1480     ActionSet *allBulbON = new ActionSet();
1481     allBulbON->actionsetName = "AllBulbOnDelay";
1482     allBulbON->setDelay(5);
1483
1484     mocks.ExpectCallFunc(onPut).
1485     Do([this](const HeaderOptions& /*headerOptions*/, const OCRepresentation& /*rep*/,
1486             const int /*eCode*/) {   Proceed();});
1487
1488     for (auto iter = lights.begin(); iter != lights.end(); ++iter)
1489     {
1490         Action *action = new Action();
1491         action->target = (*iter);
1492
1493         Capability *capa = new Capability();
1494         capa->capability = "power";
1495         capa->status = "off";
1496
1497         action->listOfCapability.push_back(capa);
1498         allBulbON->listOfAction.push_back(action);
1499     }
1500     if (g_resource)
1501     {
1502         result = groupMgr->addActionSet(g_resource, allBulbON, onPut);
1503         Wait();
1504     }
1505     result = groupMgr->executeActionSet(NULL, "AllBulbOnDelay", &onPost);
1506     EXPECT_TRUE(result == OC_STACK_ERROR);
1507     result = 0;
1508
1509     delete allBulbON;
1510 }
1511
1512 //Cancel ActionSet
1513 TEST_F(ThingsManagerTest, testCancelActionSet)
1514 {
1515     mocks.ExpectCallFunc(onPost).
1516     Do([this](const HeaderOptions& /*headerOptions*/, const OCRepresentation& /*rep*/,
1517             const int /*eCode*/) {   Proceed();});
1518
1519     if (g_resource)
1520     {
1521         result = groupMgr->cancelActionSet(g_resource, "AllBulbOff", &onPost);
1522         Wait();
1523         EXPECT_TRUE(result == OC_STACK_OK);
1524         result = 0;
1525     }
1526 }
1527
1528 //Cancel ActionSet on NULL Resource
1529 TEST_F(ThingsManagerTest, testCancelActionSetResourceNull)
1530 {
1531     result = groupMgr->cancelActionSet(NULL, "AllBulbOff", &onPost);
1532     Wait();
1533     EXPECT_TRUE(result == OC_STACK_ERROR);
1534     result = 0;
1535 }
1536
1537 //Delete ActionSet
1538 TEST_F(ThingsManagerTest, testDeleteActionSet)
1539 {
1540     mocks.ExpectCallFunc(onPut).
1541     Do([this](const HeaderOptions& /*headerOptions*/, const OCRepresentation& /*rep*/,
1542             const int /*eCode*/) {   Proceed();});
1543
1544     if (g_resource)
1545     {
1546         result = groupMgr->deleteActionSet(g_resource, "AllBulbOff", &onPut);
1547         Wait();
1548         EXPECT_TRUE(result == OC_STACK_OK);
1549         result = 0;
1550     }
1551 }
1552
1553 //Delete ActionSet on NULL Resource
1554 TEST_F(ThingsManagerTest, testDeleteActionSetResourceNull)
1555 {
1556     result = groupMgr->deleteActionSet(NULL, "AllBulbOff", &onPut);
1557     Wait();
1558     EXPECT_TRUE(result == OC_STACK_ERROR);
1559     result = 0;
1560 }
1561
1562 //Get ActionSet
1563 TEST_F(ThingsManagerTest, testGetActionSet)
1564 {
1565     mocks.ExpectCallFunc(onPost).
1566     Do([this](const HeaderOptions& /*headerOptions*/, const OCRepresentation& /*rep*/,
1567             const int /*eCode*/) {   Proceed();});
1568
1569     if (g_resource)
1570     {
1571         result = groupMgr->getActionSet(g_resource, "AllBulbOn", &onPost);
1572         Wait();
1573         EXPECT_TRUE(result == OC_STACK_OK);
1574         result = 0;
1575     }
1576 }
1577
1578 //Get ActionSet on NULL Resource
1579 TEST_F(ThingsManagerTest, testGetActionSetResourceNull)
1580 {
1581     result = groupMgr->getActionSet(NULL, "AllBulbOn", &onPost);
1582     Wait();
1583     EXPECT_TRUE(result == OC_STACK_ERROR);
1584     result = 0;
1585 }
1586
1587 //Get Configurations
1588 TEST_F(ThingsManagerTest, testGetConfigurations)
1589 {
1590     ConfigurationName name = "all";
1591
1592     ThingsConfiguration *g_thingsConf = ThingsConfiguration::getInstance();
1593     std::vector< ConfigurationName > configurations;
1594
1595     configurations.push_back(name);
1596
1597     mocks.ExpectCallFunc(onGet).
1598     Do([this](const HeaderOptions& /*headerOptions*/, const OCRepresentation& /*rep*/,
1599             const int /*eCode*/) {   Proceed();});
1600
1601     vector<string> types;
1602     types.push_back("oic.wk.con");
1603
1604     result = groupMgr->findCandidateResources(types, &foundResources);
1605
1606     std::mutex blocker;
1607     std::unique_lock < std::mutex > lock(blocker);
1608     cv2.wait(lock);
1609
1610     if (result == OC_STACK_OK)
1611     {
1612         result = g_thingsConf->getConfigurations(configurationResource, configurations, &onGet);
1613         Wait();
1614         EXPECT_TRUE(result == OC_STACK_OK);
1615         result = 0;
1616     }
1617 }
1618
1619 //Get Configurations with empty Configuration
1620 TEST_F(ThingsManagerTest, testGetConfigurationsEmptyConfiguration)
1621 {
1622     ThingsConfiguration *g_thingsConf = ThingsConfiguration::getInstance();
1623     std::vector< ConfigurationName > configurations;
1624
1625     vector<string> types;
1626     types.push_back("oic.wk.con");
1627
1628     result = groupMgr->findCandidateResources(types, &foundResources);
1629
1630     std::mutex blocker;
1631     std::unique_lock < std::mutex > lock(blocker);
1632     cv2.wait(lock);
1633
1634     if (result == OC_STACK_OK)
1635     {
1636         result = g_thingsConf->getConfigurations(configurationResource, configurations, &onGet);
1637         Wait();
1638         EXPECT_TRUE(result == OC_STACK_ERROR);
1639         result = 0;
1640     }
1641 }
1642
1643 //Get Configurations on NULL Resource
1644 TEST_F(ThingsManagerTest, testGetConfigurationsResourceNull)
1645 {
1646     ConfigurationName name = "all";
1647     ThingsConfiguration *g_thingsConf = ThingsConfiguration::getInstance();
1648     std::vector< ConfigurationName > configurations;
1649
1650     configurations.push_back(name);
1651
1652     result = g_thingsConf->getConfigurations(NULL, configurations, &onGet);
1653     Wait();
1654     EXPECT_TRUE(result == OC_STACK_ERROR);
1655     result = 0;
1656 }
1657
1658 //Get all supported Configurations
1659 TEST_F(ThingsManagerTest, testGetallSupportedCOnfigurations)
1660 {
1661     ThingsConfiguration *g_thingsConf = ThingsConfiguration::getInstance();
1662     string retVal = g_thingsConf->getListOfSupportedConfigurationUnits();
1663     EXPECT_FALSE(retVal.size() == 0);
1664 }
1665
1666 //DoBootstrap
1667 TEST_F(ThingsManagerTest, testDoBootstrap)
1668 {
1669     ThingsConfiguration *g_thingsConf = ThingsConfiguration::getInstance();
1670
1671     mocks.ExpectCallFunc(onGetBootstrapInformation).
1672     Do([this](const HeaderOptions& /*headerOptions*/, const OCRepresentation& /*rep*/,
1673             const int /*eCode*/) {   Proceed();});
1674     result = g_thingsConf->doBootstrap(&onGetBootstrapInformation);
1675     Wait();
1676     EXPECT_TRUE(result == OC_STACK_OK);
1677     result = 0;
1678 }
1679
1680 //DoBootstrap with NULL callback
1681 TEST_F(ThingsManagerTest, testDoBootstrapCallBackNull)
1682 {
1683     ThingsConfiguration *g_thingsConf = ThingsConfiguration::getInstance();
1684
1685     result = g_thingsConf->doBootstrap(NULL);
1686     Wait();
1687     EXPECT_TRUE(result == OC_STACK_ERROR);
1688     result = 0;
1689 }
1690
1691 //Update Configuration
1692 TEST_F(ThingsManagerTest, testUpdateConfiguration)
1693 {
1694     ConfigurationName name = "r";
1695     ConfigurationValue value = "INDIA";
1696
1697     std::map< ConfigurationName, ConfigurationValue > configurations;
1698     ThingsConfiguration *g_thingsConf = ThingsConfiguration::getInstance();
1699     configurations.insert(std::make_pair(name, value));
1700
1701     mocks.ExpectCallFunc(onUpdate).
1702     Do([this](const HeaderOptions& /*headerOptions*/, const OCRepresentation& /*rep*/,
1703             const int /*eCode*/) {   Proceed();});
1704
1705     vector<string> types;
1706     types.push_back("oic.wk.con");
1707     result = groupMgr->findCandidateResources(types, &foundResources);
1708
1709     std::mutex blocker2;
1710     std::unique_lock < std::mutex > lock2(blocker2);
1711     cv2.wait(lock2);
1712
1713     if (result == OC_STACK_OK)
1714     {
1715         result = g_thingsConf->updateConfigurations(configurationResource, configurations,
1716                                                     &onUpdate);
1717         Wait();
1718         EXPECT_TRUE(result == OC_STACK_OK);
1719         result = 0;
1720     }
1721 }
1722
1723 //Update Configuration with Empty Configuration
1724 TEST_F(ThingsManagerTest, testUpdateConfigurationEmptyConfiguration)
1725 {
1726     std::map< ConfigurationName, ConfigurationValue > configurations;
1727     ThingsConfiguration *g_thingsConf = ThingsConfiguration::getInstance();
1728
1729     vector<string> types;
1730     types.push_back("oic.wk.con");
1731     result = groupMgr->findCandidateResources(types, &foundResources);
1732
1733     std::mutex blocker2;
1734     std::unique_lock < std::mutex > lock2(blocker2);
1735     cv2.wait(lock2);
1736
1737     if (result == OC_STACK_OK)
1738     {
1739         result = g_thingsConf->updateConfigurations(configurationResource, configurations,
1740                                                     &onUpdate);
1741         Wait();
1742         EXPECT_TRUE(result == OC_STACK_ERROR);
1743         result = 0;
1744     }
1745 }
1746
1747 //Update Configuration on NULL Resource
1748 TEST_F(ThingsManagerTest, testUpdateConfigurationResourceNull)
1749 {
1750     ConfigurationName name = "r";
1751     ConfigurationValue value = "INDIA";
1752
1753     std::map< ConfigurationName, ConfigurationValue > configurations;
1754     ThingsConfiguration *g_thingsConf = ThingsConfiguration::getInstance();
1755
1756     configurations.insert(std::make_pair(name, value));
1757
1758     result = g_thingsConf->updateConfigurations(NULL, configurations, &onUpdate);
1759     Wait();
1760     EXPECT_TRUE(result == OC_STACK_ERROR);
1761     result = 0;
1762 }
1763
1764 //Reboot
1765 TEST_F(ThingsManagerTest, testReboot)
1766 {
1767     ThingsMaintenance *g_thingsMnt = ThingsMaintenance::getInstance();
1768
1769     mocks.ExpectCallFunc(onReboot).
1770     Do([this](const HeaderOptions& /*headerOptions*/, const OCRepresentation& /*rep*/,
1771             const int /*eCode*/) {   Proceed();});
1772
1773     vector<string> types;
1774     types.push_back("oic.wk.mnt");
1775     result = groupMgr->findCandidateResources(types, &foundResources);
1776
1777     std::mutex blocker;
1778     std::unique_lock < std::mutex > lock(blocker);
1779     cv2.wait(lock);
1780
1781     if (result == OC_STACK_OK)
1782     {
1783         result = g_thingsMnt->reboot(configurationResource, &onReboot);
1784         Wait();
1785         EXPECT_TRUE(result == OC_STACK_OK);
1786         result = 0;
1787     }
1788 }
1789
1790 //Reboot on NULL Resource
1791 TEST_F(ThingsManagerTest, testRebootResourceNull)
1792 {
1793     ThingsMaintenance *g_thingsMnt = ThingsMaintenance::getInstance();
1794
1795     result = g_thingsMnt->reboot(NULL, &onReboot);
1796     Wait();
1797     EXPECT_TRUE(result == OC_STACK_ERROR);
1798     result = 0;
1799 }
1800
1801 //Factory Reset
1802 TEST_F(ThingsManagerTest, testFactoryReset)
1803 {
1804     ThingsMaintenance *g_thingsMnt = ThingsMaintenance::getInstance();
1805
1806     mocks.ExpectCallFunc(onFactoryReset).
1807     Do([this](const HeaderOptions& /*headerOptions*/, const OCRepresentation& /*rep*/,
1808             const int /*eCode*/) {   Proceed();});
1809
1810     vector<string> types;
1811     types.push_back("oic.wk.mnt");
1812     result = groupMgr->findCandidateResources(types, &foundResources);
1813
1814     std::mutex blocker;
1815     std::unique_lock < std::mutex > lock(blocker);
1816     cv2.wait(lock);
1817
1818     if (result == OC_STACK_OK)
1819     {
1820         result = g_thingsMnt->factoryReset(configurationResource, &onFactoryReset);
1821         Wait();
1822         EXPECT_TRUE(result == OC_STACK_OK);
1823         result = 0;
1824     }
1825 }
1826
1827 //Factory Reset on NULL Resource
1828 TEST_F(ThingsManagerTest, testFactoryResetResourceNull)
1829 {
1830     ThingsMaintenance *g_thingsMnt = ThingsMaintenance::getInstance();
1831
1832     result = g_thingsMnt->factoryReset(NULL, &onFactoryReset);
1833     Wait();
1834     EXPECT_TRUE(result == OC_STACK_ERROR);
1835     result = 0;
1836 }
1837