Update unittest for remote opertion in scene manager
[platform/upstream/iotivity.git] / service / scene-manager / unittests / RemoteSceneListTest.cpp
1 //******************************************************************
2 //
3 // Copyright 2016 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 <mutex>
22 #include <condition_variable>
23
24 #include "RemoteSceneList.h"
25 #include "UnitTestHelper.h"
26 #include "SceneCommons.h"
27 #include "SceneList.h"
28 #include "RCSRemoteResourceObject.h"
29 #include "OCPlatform.h"
30 #include "RCSDiscoveryManager.h"
31 #include "RCSAddress.h"
32
33 using namespace std;
34 using namespace OIC::Service;
35 using namespace OC;
36
37 constexpr int DEFAULT_WAITTIME = 2000;
38
39 SceneList* g_sceneList = SceneList::getInstance();
40 RCSRemoteResourceObject::Ptr pListResource = nullptr;
41 RemoteSceneList::Ptr pSceneList = nullptr;
42
43 void discoverSceneListServer()
44 {
45     if(pListResource == nullptr)
46     {
47         std::vector< std::string > vecRT{ SCENE_LIST_RT };
48         std::vector< std::string > vecIF{ OC_RSRVD_INTERFACE_DEFAULT, OC::BATCH_INTERFACE };
49
50         pListResource = SceneUtils::createRCSResourceObject(
51                             "coap://" + SceneUtils::getNetAddress() + SCENE_LIST_URI,
52                             SCENE_CONNECTIVITY, vecRT, vecIF);
53     }
54 }
55
56 class RemoteSceneListTest : public TestWithMock
57 {
58 protected:
59     void SetUp()
60     {
61         TestWithMock::SetUp();
62         g_sceneList->getName();
63     }
64
65     void waitForCallback(int waitingTime = DEFAULT_WAITTIME)
66     {
67         std::unique_lock< std::mutex > lock{ mutex };
68         cond.wait_for(lock, std::chrono::milliseconds{ waitingTime });
69     }
70
71 public:
72     std::condition_variable cond;
73     std::mutex mutex;
74
75     void onRemoteSceneListCreated(RemoteSceneList::Ptr remoteSceneList, int)
76     {
77         pSceneList = std::move(remoteSceneList);
78         cond.notify_all();
79     }
80
81     void onSetName(int)
82     {
83         cond.notify_all();
84     }
85 };
86
87 TEST_F(RemoteSceneListTest, createRemoteSceneListInstance)
88 {
89     discoverSceneListServer();
90     if(pSceneList == nullptr)
91     {
92         RemoteSceneList::createInstance(pListResource, std::bind(
93             &RemoteSceneListTest::onRemoteSceneListCreated, this,
94             placeholders::_1, placeholders::_2));
95
96         waitForCallback();
97     }
98
99     EXPECT_NE(nullptr, pSceneList);
100 }
101
102 TEST_F(RemoteSceneListTest, setAndGetRemoteSceneListName)
103 {
104     discoverSceneListServer();
105     if(pSceneList == nullptr)
106     {
107         RemoteSceneList::createInstance(pListResource, std::bind(
108             &RemoteSceneListTest::onRemoteSceneListCreated, this,
109             placeholders::_1, placeholders::_2));
110
111         waitForCallback();
112     }
113
114     pSceneList->setName("Test Scene List", std::bind(
115         &RemoteSceneListTest::onSetName, this, placeholders::_1));
116
117     waitForCallback();
118
119     EXPECT_EQ("Test Scene List", pSceneList->getName());
120 }