Merge test folder of test branch
[iotivity.git] / test / src / tc / ic / gtest / src / stc / ICOCResourceTest.cpp
1 /******************************************************************
2  *
3  * Copyright 2018 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 #include "ICHelper.h"
21 #include "ICResourceHelper.h"
22
23 static OCAccountManager::Ptr g_accountMgrControlee = nullptr;
24
25 class ICOCResourceTest_stc: public ::testing::Test
26 {
27
28 public:
29     OC::OCResource::Ptr m_mqBrokerResource = nullptr;
30     OC::OCResource::Ptr m_mqSelectedTopicResource = nullptr;
31     OCRepresentation m_rep;
32     int index = 0;
33
34 protected:
35     std::string m_errorMsg;
36     ICHelper m_ICHelper;
37     string m_hostAddress = COAP_HOST_ADDRESS;
38     QueryParamsMap m_queryParams;
39     ICResourceHelper m_ICResourceHelper;
40     string m_Topic_uri1 = "/oic/ps/light55";
41     string m_UTopic_uri1 = "/oic/ps/light7";
42     string m_UTopic_uri2 = "/oic/ps/light13";
43
44     static FILE *client_open(const char * /*path*/, const char *mode)
45     {
46         return fopen(MQPUBLISHER, mode);
47     }
48
49     virtual void SetUp()
50     {
51         ICResourceHelper::s_isCreateTopicSuccess = false;
52         ICResourceHelper::s_isDiscoverTopicSuccess = false;
53         ICResourceHelper::s_isPublishTopicSuccess = false;
54         ICResourceHelper::s_isrequestTopicPublishSuccess = false;
55         ICResourceHelper::s_isSubscribeSuccess = false;
56         CommonTestUtil::runCommonTCSetUpPart();
57         m_hostAddress= CloudCommonUtil::getDefaultHostAddess();
58         m_ICHelper.initCloudClient();
59         OCPersistentStorage ps
60         { client_open, fread, fwrite, fclose, unlink };
61         PlatformConfig cfg
62         { ServiceType::InProc, ModeType::Both, CT_DEFAULT, // By setting to "0.0.0.0", it binds to all available interfaces
63             CT_DEFAULT, // Uses randomly available port
64                 QualityOfService::LowQos, &ps };
65         OCPlatform::Configure(cfg);
66
67         g_accountMgrControlee = OCPlatform::constructAccountManagerObject(m_hostAddress,
68                 CT_ADAPTER_TCP);
69         // MQ broker resource
70         cout<<"m_hostAddress"<<m_hostAddress<<endl;
71         string host = "coap+tcp://";
72         host += IC_CLOUD_INTERFACE_HOST_ADDRESS;
73         m_mqBrokerResource = OCPlatform::constructResourceObject(m_hostAddress, DEFAULT_MQ_BROKER_URI,
74                 static_cast< OCConnectivityType>(CT_ADAPTER_TCP | CT_IP_USE_V4), false,
75                 { string("oic.wk.ps") },
76                 { string(DEFAULT_INTERFACE) });
77     }
78
79     virtual void TearDown()
80     {
81     }
82
83 };
84
85 /**
86  * @since 2017-01-26
87  * @see OCStackResult signUp(const std::string& authProvider, const std::string& authCode, PostCallback cloudConnectHandler)() API
88  * @see OCStackResult signIn(const std::string& userUuid, const std::string& accessToken, PostCallback cloudConnectHandler) API
89  * @see OCStackResult signOut(const std::string& accessToken, PostCallback cloudConnectHandler) API
90  * @objective Test 'createMQTopic' API With valid scenario
91  * @target createMQTopic(const OCRepresentation& rep,
92                                     const std::string& topicUri,
93                                     const QueryParamsMap& queryParametersMap,
94                                     MQTopicCallback attributeHandler,
95                                     QualityOfService qos)
96  * @test_data 1. OCRepresentation 2. topicUri 3. queryParametersMap 4. MQTopicCallback 5. QualityOfService
97  * @pre_condition 1. constructAccountManagerObject(host, connectivity_type) API
98  *                2. SignUp
99  *                3. SignIn
100  * @procedure Call createMQTopic API with valid parameter
101  * @post_condition SignOut
102  * @expected createMQTopic API will return OC_STACK_OK.
103  **/
104 #if defined(__LINUX__) || defined(__TIZEN__)
105 TEST_F(ICOCResourceTest_stc, CreateMQTopic_SRC_P)
106 {
107     EXPECT_EQ(true, CloudCommonUtil::signIn(g_accountMgrControlee));
108     ASSERT_EQ(OC_STACK_OK, m_mqBrokerResource->createMQTopic(m_rep, m_UTopic_uri1,
109                     m_queryParams, ICResourceHelper::createTopicCB, QualityOfService::LowQos))<<"Create MQ does not work.";
110     ICHelper::waitForServerResponse();
111     ASSERT_EQ(true, ICResourceHelper::s_isCreateTopicSuccess)<<"Topic is not created.";
112     EXPECT_EQ(true, CloudCommonUtil::signOut(g_accountMgrControlee));
113     SUCCEED();
114 }
115 #endif
116
117 /**
118  * @since 2017-01-26
119  * @see OCStackResult signUp(const std::string& authProvider, const std::string& authCode, PostCallback cloudConnectHandler)() API
120  * @see OCStackResult signIn(const std::string& userUuid, const std::string& accessToken, PostCallback cloudConnectHandler) API
121  * @see OCStackResult signOut(const std::string& accessToken, PostCallback cloudConnectHandler) API
122  * @objective Test 'createMQTopic' API With valid scenario
123  * @target createMQTopic(const OCRepresentation& rep,
124                                     const std::string& topicUri,
125                                     const QueryParamsMap& queryParametersMap,
126                                     MQTopicCallback attributeHandler,
127                                     QualityOfService qos)
128  * @test_data 1. OCRepresentation 2. topicUri 3. queryParametersMap 4. MQTopicCallback 5. QualityOfService
129  * @pre_condition 1. constructAccountManagerObject(host, connectivity_type) API
130  *                2. SignUp
131  *                3. SignIn
132  * @procedure Call createMQTopic API with valid parameter
133  * @post_condition SignOut
134  * @expected createMQTopic API should return OC_STACK_OK.
135  **/
136 #if defined(__LINUX__) || defined(__TIZEN__)
137 TEST_F(ICOCResourceTest_stc, CreateMQTopicByType_SRC_P)
138 {
139     QueryParamsMap queryParams;
140     queryParams["rt"] = LIGHT;
141
142     EXPECT_EQ(true, CloudCommonUtil::signIn(g_accountMgrControlee));
143     ASSERT_EQ(OC_STACK_OK, m_mqBrokerResource->createMQTopic(m_rep, m_UTopic_uri2,
144                     queryParams, ICResourceHelper::createTopicCB, QualityOfService::LowQos))<<"Create MQ does not work.";
145     ICHelper::waitForServerResponse();
146     ASSERT_EQ(true, ICResourceHelper::s_isCreateTopicSuccess)<<"Topic is not created.";
147     EXPECT_EQ(true, CloudCommonUtil::signOut(g_accountMgrControlee));
148     SUCCEED();
149 }
150 #endif
151
152 /**
153  * @since 2017-01-26
154  * @see OCStackResult signUp(const std::string& authProvider, const std::string& authCode, PostCallback cloudConnectHandler)() API
155  * @see OCStackResult signIn(const std::string& userUuid, const std::string& accessToken, PostCallback cloudConnectHandler) API
156  * @see OCStackResult signOut(const std::string& accessToken, PostCallback cloudConnectHandler) API
157  * @objective Test 'discoveryMQTopics' API With valid scenario
158  * @target OCStackResult discoveryMQTopics(const QueryParamsMap& queryParametersMap,
159                                         MQTopicCallback attributeHandler,
160                                         QualityOfService qos)
161  * @test_data 1. OCRepresentation 2. topicUri 3. queryParametersMap 4. MQTopicCallback 5. QualityOfService
162  * @pre_condition 1. constructAccountManagerObject(host, connectivity_type) API
163  *                2. SignUp
164  *                3. SignIn
165  *                4. Create Topic
166  * @procedure Call discoveryMQTopics API with valid parameter
167  * @post_condition SignOut
168  * @expected discoveryMQTopics API should return OC_STACK_OK.
169  **/
170 #if defined(__LINUX__) || defined(__TIZEN__)
171 TEST_F(ICOCResourceTest_stc, DiscoveryMQTopics_SRC_P)
172 {
173     EXPECT_EQ(true, CloudCommonUtil::signIn(g_accountMgrControlee));
174     ASSERT_EQ(OC_STACK_OK, m_mqBrokerResource->createMQTopic(m_rep, TOPIC_URI,
175                     m_queryParams, ICResourceHelper::createTopicCB, QualityOfService::LowQos))<<"Create MQ does not work.";
176     ICHelper::waitForServerResponse();
177     ICResourceHelper::s_TopicList.clear();
178     EXPECT_EQ(OC_STACK_OK, m_mqBrokerResource->discoveryMQTopics(m_queryParams,
179                     ICResourceHelper::discoverTopicCB, QualityOfService::LowQos))<<"discovery MQ does not work.";
180     ICHelper::waitForServerResponse();
181     ASSERT_EQ(true, ICResourceHelper::s_isDiscoverTopicSuccess)<<"Discovery MQ got fail.";
182     EXPECT_EQ(true, CloudCommonUtil::signOut(g_accountMgrControlee));
183     SUCCEED();
184
185 }
186 #endif
187
188 /**
189  * @since 2017-01-26
190  * @see OCStackResult signUp(const std::string& authProvider, const std::string& authCode, PostCallback cloudConnectHandler)() API
191  * @see OCStackResult signIn(const std::string& userUuid, const std::string& accessToken, PostCallback cloudConnectHandler) API
192  * @see OCStackResult signOut(const std::string& accessToken, PostCallback cloudConnectHandler) API
193  * @objective Test 'discoveryMQTopics' API With valid scenario
194  * @target OCStackResult discoveryMQTopics(const QueryParamsMap& queryParametersMap,
195                                         MQTopicCallback attributeHandler,
196                                         QualityOfService qos)
197  * @test_data 1. OCRepresentation 2. topicUri 3. queryParametersMap 4. MQTopicCallback 5. QualityOfService
198  * @pre_condition 1. constructAccountManagerObject(host, connectivity_type) API
199  *                2. SignUp
200  *                3. SignIn
201  * @procedure Call discoveryMQTopics API with valid parameter
202  * @post_condition SignOut
203  * @expected discoveryMQTopics API should return OC_STACK_OK.
204  **/
205 #if defined(__LINUX__) || defined(__TIZEN__)
206 TEST_F(ICOCResourceTest_stc, DiscoveryMQTopicsByType_SRC_P)
207 {
208     m_queryParams["rt"] = LIGHT;
209
210     EXPECT_EQ(true, CloudCommonUtil::signIn(g_accountMgrControlee));
211     ASSERT_EQ(OC_STACK_OK, m_mqBrokerResource->createMQTopic(m_rep, m_Topic_uri1,
212                     m_queryParams, ICResourceHelper::createTopicCB, QualityOfService::LowQos))<<"Create MQ does not work.";
213     ICHelper::waitForServerResponse();
214     ICResourceHelper::s_TopicList.clear();
215     EXPECT_EQ(OC_STACK_OK, m_mqBrokerResource->discoveryMQTopics(m_queryParams,
216                     ICResourceHelper::discoverTopicCB, QualityOfService::LowQos))<<"discovery MQ does not work.";
217     ICHelper::waitForServerResponse();
218     ASSERT_EQ(true, ICResourceHelper::s_isDiscoverTopicSuccess)<<"Discovery MQ got fail.";
219     EXPECT_EQ(true, CloudCommonUtil::signOut(g_accountMgrControlee));
220     SUCCEED();
221
222 }
223 #endif
224
225 /**
226  * @since 2017-01-26
227  * @see OCStackResult signUp(const std::string& authProvider, const std::string& authCode, PostCallback cloudConnectHandler)() API
228  * @see OCStackResult signIn(const std::string& userUuid, const std::string& accessToken, PostCallback cloudConnectHandler) API
229  * @see OCStackResult signOut(const std::string& accessToken, PostCallback cloudConnectHandler) API
230  * @objective Test 'publishMQTopic' API With valid scenario
231  * @target OCStackResult publishMQTopic(const QueryParamsMap& queryParametersMap,
232                                         MQTopicCallback attributeHandler,
233                                         QualityOfService qos)
234  * @test_data 1. OCRepresentation 2. topicUri 3. queryParametersMap 4. MQTopicCallback 5. QualityOfService
235  * @pre_condition 1. constructAccountManagerObject(host, connectivity_type) API
236  *                2. Call SignUp
237  *                3. Call SignIn
238  *                4. Call createMQTopic
239  *                5. Call discoveryMQTopics
240  * @procedure Call publishMQTopic API with valid parameter
241  * @post_condition SignOut
242  * @expected publishMQTopic API should return OC_STACK_OK.
243  **/
244 #if defined(__LINUX__) || defined(__TIZEN__)
245 TEST_F(ICOCResourceTest_stc, PublishMQTopic_SRC_P)
246 {
247     m_rep["message"] = MESSAGE;
248     EXPECT_EQ(true, CloudCommonUtil::signIn(g_accountMgrControlee));
249     ASSERT_EQ(OC_STACK_OK, m_mqBrokerResource->createMQTopic(m_rep, TOPIC_URI,
250                     m_queryParams, ICResourceHelper::createTopicCB, QualityOfService::LowQos))<<"Create MQ does not work.";
251     ICHelper::waitForServerResponse();
252     ICResourceHelper::s_TopicList.clear();
253     EXPECT_EQ(OC_STACK_OK, m_mqBrokerResource->discoveryMQTopics(m_queryParams,
254                     ICResourceHelper::discoverTopicCB, QualityOfService::LowQos))<<"discovery MQ does not work.";
255     ICHelper::waitForServerResponse();
256
257     m_mqSelectedTopicResource = ICResourceHelper::s_TopicList[index];
258     EXPECT_EQ(OC_STACK_OK, m_mqSelectedTopicResource->publishMQTopic(m_rep, m_queryParams,
259                     ICResourceHelper::publishMessageCB, QualityOfService::LowQos))<<"publish MQ does not work.";
260     ICHelper::waitForServerResponse();
261     ASSERT_EQ(true, ICResourceHelper::s_isPublishTopicSuccess)<<"publishMQTopic got fail.";
262     EXPECT_EQ(true, CloudCommonUtil::signOut(g_accountMgrControlee));
263     SUCCEED();
264
265 }
266 #endif
267
268 /**
269  * @since 2017-01-26
270  * @see OCStackResult signUp(const std::string& authProvider, const std::string& authCode, PostCallback cloudConnectHandler)() API
271  * @see OCStackResult signIn(const std::string& userUuid, const std::string& accessToken, PostCallback cloudConnectHandler) API
272  * @see OCStackResult signOut(const std::string& accessToken, PostCallback cloudConnectHandler) API
273  * @objective Test 'requestMQPublish' API With valid scenario
274  * @target OCStackResult publishMQTopic(const QueryParamsMap& queryParametersMap,
275                                         MQTopicCallback attributeHandler,
276                                         QualityOfService qos)
277  * @test_data 1. OCRepresentation 2. topicUri 3. queryParametersMap 4. MQTopicCallback 5. QualityOfService
278  * @pre_condition 1. constructAccountManagerObject(host, connectivity_type) API
279  *                2. SignUp
280  *                3. SignIn
281  *                4. Call createMQTopic
282  *                5. Call discoveryMQTopics
283  * @procedure Call requestMQPublish API with valid parameter
284  * @post_condition SignOut
285  * @expected requestMQPublish API should return OC_STACK_OK.
286  **/
287 #if defined(__LINUX__) || defined(__TIZEN__)
288 TEST_F(ICOCResourceTest_stc, RequestpublishMQTopic_SRC_P)
289 {
290     EXPECT_EQ(true, CloudCommonUtil::signIn(g_accountMgrControlee));
291     ASSERT_EQ(OC_STACK_OK, m_mqBrokerResource->createMQTopic(m_rep, TOPIC_URI,
292                     m_queryParams, ICResourceHelper::createTopicCB, QualityOfService::LowQos))<<"Create MQ does not work.";
293     ICHelper::waitForServerResponse();
294     ICResourceHelper::s_TopicList.clear();
295     EXPECT_EQ(OC_STACK_OK, m_mqBrokerResource->discoveryMQTopics(m_queryParams,
296                     ICResourceHelper::discoverTopicCB, QualityOfService::LowQos))<<"discovery MQ does not work.";
297     ICHelper::waitForServerResponse();
298
299     m_mqSelectedTopicResource = ICResourceHelper::s_TopicList[index];
300     EXPECT_EQ(OC_STACK_OK, m_mqBrokerResource->requestMQPublish(m_queryParams,
301                     ICResourceHelper::requestMQPublishCB, QualityOfService::LowQos))<<"publish MQ does not work.";
302     ICHelper::waitForServerResponse();
303     ASSERT_EQ(true, ICResourceHelper::s_isrequestTopicPublishSuccess)<<"publishMQTopic got fail.";
304     EXPECT_EQ(true, CloudCommonUtil::signOut(g_accountMgrControlee));
305     SUCCEED();
306
307 }
308 #endif
309
310 /**
311  * @since 2017-02-03
312  * @see OCStackResult signUp(const std::string& authProvider, const std::string& authCode, PostCallback cloudConnectHandler)() API
313  * @see OCStackResult signIn(const std::string& userUuid, const std::string& accessToken, PostCallback cloudConnectHandler) API
314  * @see OCStackResult signOut(const std::string& accessToken, PostCallback cloudConnectHandler) API
315  * @objective Test 'subscribeMQTopic' API With valid scenario
316  * @target OCStackResult subscribeMQTopic(const QueryParamsMap& queryParametersMap,
317                                         MQTopicCallback attributeHandler,
318                                         QualityOfService qos)
319  * @test_data 1. OCRepresentation 2. topicUri 3. queryParametersMap 4. MQTopicCallback 5. QualityOfService
320  * @pre_condition 1. constructAccountManagerObject(host, connectivity_type) API
321  *                2. SignUp
322  *                3. SignIn and CreateGroup
323  *                4. discover Topic
324  * @procedure Call subscribeMQTopic API with valid parameter
325  * @post_condition SignOut
326  * @expected subscribeMQTopic API will return OC_STACK_OK.
327  **/
328 #if defined(__LINUX__) || defined(__TIZEN__)
329 TEST_F(ICOCResourceTest_stc, SubscribeMQTopic_SRC_P)
330 {
331     EXPECT_EQ(true, CloudCommonUtil::signIn(g_accountMgrControlee));
332     ASSERT_EQ(OC_STACK_OK, m_mqBrokerResource->createMQTopic(m_rep, TOPIC_URI,
333                     m_queryParams, ICResourceHelper::createTopicCB, QualityOfService::LowQos))<<"Create MQ does not work.";
334     ICHelper::waitForServerResponse();
335     ICResourceHelper::s_TopicList.clear();
336
337     EXPECT_EQ(OC_STACK_OK, m_mqBrokerResource->discoveryMQTopics(m_queryParams,
338                     ICResourceHelper::discoverTopicCB, QualityOfService::LowQos))<<"discovery MQ does not work.";
339
340     ICHelper::waitForServerResponse();
341
342     m_mqSelectedTopicResource = ICResourceHelper::s_TopicList[index];
343     EXPECT_EQ(OC_STACK_OK, m_mqSelectedTopicResource->subscribeMQTopic(ObserveType::Observe, m_queryParams,
344                     ICResourceHelper::subscribeCB, QualityOfService::LowQos))<<"subscribeMQTopic does not work.";
345     ICHelper::waitForServerResponse();
346     ASSERT_EQ(true, ICResourceHelper::s_isSubscribeSuccess)<<"subscribeMQTopic got fail.";
347     EXPECT_EQ(true, CloudCommonUtil::signOut(g_accountMgrControlee));
348     SUCCEED();
349
350 }
351 #endif
352
353 /**
354  * @since 2017-02-03
355  * @see OCStackResult signUp(const std::string& authProvider, const std::string& authCode, PostCallback cloudConnectHandler)() API
356  * @see OCStackResult signIn(const std::string& userUuid, const std::string& accessToken, PostCallback cloudConnectHandler) API
357  * @see OCStackResult signOut(const std::string& accessToken, PostCallback cloudConnectHandler) API
358  * @objective Test 'unsubscribeMQTopic' API With valid scenario
359  * @target OCStackResult unsubscribeMQTopic(QualityOfService qos)
360  * @test_data 1. OCRepresentation 2. topicUri 3. queryParametersMap 4. MQTopicCallback 5. QualityOfService
361  * @pre_condition 1. constructAccountManagerObject(host, connectivity_type) API
362  *                2. SignUp
363  *                3. SignIn
364  *                4. discover Topic
365  * @procedure Call unsubscribeMQTopic API with valid parameter
366  * @post_condition SignOut
367  * @expected unsubscribeMQTopic API should return OC_STACK_OK.
368  **/
369 #if defined(__LINUX__) || defined(__TIZEN__)
370 TEST_F(ICOCResourceTest_stc, UnsubscribeMQTopic_SRC_P)
371 {
372     try
373     {
374         m_rep["message"] = MESSAGE;
375         EXPECT_EQ(true, CloudCommonUtil::signIn(g_accountMgrControlee));
376         ASSERT_EQ(OC_STACK_OK, m_mqBrokerResource->createMQTopic(m_rep, TOPIC_URI,
377                         m_queryParams, ICResourceHelper::createTopicCB, QualityOfService::LowQos))<<"Create MQ does not work.";
378         ICHelper::waitForServerResponse();
379         ICResourceHelper::s_TopicList.clear();
380         EXPECT_EQ(OC_STACK_OK, m_mqBrokerResource->discoveryMQTopics(m_queryParams,
381                         ICResourceHelper::discoverTopicCB, QualityOfService::LowQos))<<"discovery MQ does not work.";
382         ICHelper::waitForServerResponse();
383         m_mqSelectedTopicResource = ICResourceHelper::s_TopicList[index];
384         EXPECT_EQ(OC_STACK_OK, m_mqSelectedTopicResource->unsubscribeMQTopic(QualityOfService::LowQos))<<"publish MQ does not work.";
385         ICHelper::waitForServerResponse();
386
387         EXPECT_EQ(true, CloudCommonUtil::signOut(g_accountMgrControlee));
388         SUCCEED();
389     }
390     catch (OCException &ex)
391     {
392         cout << "OCException result string : " << CommonUtil::s_OCStackResultString.at(ex.code());
393         return false;
394     }
395
396 }
397 #endif