Imported Upstream version 1.2.0
[platform/upstream/iotivity.git] / service / scene-manager / unittests / RemoteSceneCollectionTest.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
26 #include "UnitTestHelper.h"
27 #include "SceneCommons.h"
28 #include "SceneList.h"
29 #include "RCSResourceObject.h"
30 #include "RCSRemoteResourceObject.h"
31 #include "OCPlatform.h"
32 #include "RCSDiscoveryManager.h"
33 #include "RCSAddress.h"
34
35 using namespace std;
36 using namespace OIC::Service;
37 using namespace OC;
38
39 constexpr int DEFAULT_WAITTIME = 2000;
40
41 SceneList* g_sceneList = SceneList::getInstance();
42 RCSRemoteResourceObject::Ptr pListResource = nullptr;
43 RemoteSceneList::Ptr pSceneList = nullptr;
44 RemoteSceneCollection::Ptr pSceneCollection = nullptr;
45 RemoteScene::Ptr pScene = nullptr;
46
47 void discoverSceneListServer()
48 {
49     if(pListResource == nullptr)
50     {
51         std::vector< std::string > vecRT{ SCENE_LIST_RT };
52         std::vector< std::string > vecIF{ OC_RSRVD_INTERFACE_DEFAULT, OC::BATCH_INTERFACE };
53
54         pListResource = SceneUtils::createRCSResourceObject(
55                             "coap://" + SceneUtils::getNetAddress() + SCENE_LIST_URI,
56                             SCENE_CONNECTIVITY, vecRT, vecIF);
57     }
58 }
59
60 class RemoteSceneCollectionTest : public TestWithMock
61 {
62 protected:
63     void SetUp()
64     {
65         TestWithMock::SetUp();
66
67         g_sceneList->getName();
68         discoverSceneListServer();
69
70         if(pSceneList == nullptr){
71             RemoteSceneList::createInstance(pListResource, std::bind(
72                 &RemoteSceneCollectionTest::onRemoteSceneListCreated, this,
73                 placeholders::_1, placeholders::_2));
74             waitForCallback();
75         }
76     }
77
78     void waitForCallback(int waitingTime = DEFAULT_WAITTIME)
79     {
80         std::unique_lock< std::mutex > lock{ mutex };
81         cond.wait_for(lock, std::chrono::milliseconds{ waitingTime });
82     }
83
84 public:
85     std::condition_variable cond;
86     std::mutex mutex;
87
88     void onRemoteSceneListCreated(RemoteSceneList::Ptr remoteSceneList, int)
89     {
90         pSceneList = std::move(remoteSceneList);
91         cond.notify_all();
92     }
93
94     void onRemoteSceneCollectionCreated(RemoteSceneCollection::Ptr remoteSceneCol, int)
95     {
96         pSceneCollection = remoteSceneCol;
97         cond.notify_all();
98     }
99
100     void onRemoteSceneCreated(RemoteScene::Ptr remoteScene, int)
101     {
102         pScene = remoteScene;
103         cond.notify_all();
104     }
105
106     void onSetName(int)
107     {
108         cond.notify_all();
109     }
110 };
111
112 TEST_F(RemoteSceneCollectionTest, addNewRemoteSceneCollection)
113 {
114     if(pSceneCollection == nullptr)
115     {
116         pSceneList->addNewSceneCollection(std::bind(
117             &RemoteSceneCollectionTest::onRemoteSceneCollectionCreated, this,
118             placeholders::_1, placeholders::_2));
119         waitForCallback();
120     }
121
122     if(pScene == nullptr)
123     {
124         pSceneCollection->addNewScene("Default", std::bind(
125             &RemoteSceneCollectionTest::onRemoteSceneCreated, this,
126             placeholders::_1, placeholders::_2));
127         waitForCallback();
128     }
129
130     EXPECT_NE(nullptr, pSceneCollection);
131 }
132
133 TEST_F(RemoteSceneCollectionTest, getRemoteSceneCollectionList)
134 {
135     std::vector< RemoteSceneCollection::Ptr > sceneCollections
136         = pSceneList->getRemoteSceneCollections();
137
138     bool getCollectionsOK = true;
139
140     if (!sceneCollections.empty())
141     {
142         for (const auto &it : sceneCollections)
143         {
144             if (it->getId() == "")
145             {
146                 getCollectionsOK = false;
147                 break;
148             }
149         }
150     }
151     else
152     {
153         getCollectionsOK = false;
154     }
155
156     ASSERT_TRUE(getCollectionsOK);
157 }
158
159 TEST_F(RemoteSceneCollectionTest, setAndGetSceneCollectionName)
160 {
161     if(pSceneCollection == nullptr)
162     {
163         pSceneList->addNewSceneCollection(std::bind(
164             &RemoteSceneCollectionTest::onRemoteSceneCollectionCreated, this,
165             placeholders::_1, placeholders::_2));
166         waitForCallback();
167     }
168
169     if(pScene == nullptr)
170     {
171         pSceneCollection->addNewScene("Default", std::bind(
172             &RemoteSceneCollectionTest::onRemoteSceneCreated, this,
173             placeholders::_1, placeholders::_2));
174         waitForCallback();
175     }
176
177     pSceneCollection->setName("Kitchen", std::bind(
178         &RemoteSceneCollectionTest::onSetName, this, placeholders::_1));
179
180     waitForCallback();
181
182     EXPECT_EQ("Kitchen", pSceneCollection->getName());
183 }