Support HighQoS and Cancel Observe in CA and RI
[platform/upstream/iotivity.git] / resource / unittests / OCResourceTest.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH 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
22 #include <OCPlatform.h>
23 #include <OCApi.h>
24 #include <gtest/gtest.h>
25 #include <string>
26 #include <map>
27
28 namespace OCResourceTest
29 {
30     using namespace OC;
31     // Callbacks
32     void onObserve(const HeaderOptions, const OCRepresentation&, const int&, const int&)
33     {
34     }
35
36     void onGetPut(const HeaderOptions&, const OCRepresentation& , const int eCode)
37     {
38         EXPECT_EQ(eCode, OC_STACK_OK);
39     }
40
41     void foundResource(std::shared_ptr<OCResource> )
42     {
43     }
44
45     void deleteResponse(const HeaderOptions&, const int)
46     {
47     }
48
49     //Helper method
50     OCResource::Ptr ConstructResourceObject(std::string host, std::string uri)
51     {
52         OCConnectivityType connectivityType = OC_WIFI;
53         std::vector<std::string> types = {"intel.rpost"};
54         std::vector<std::string> ifaces = {DEFAULT_INTERFACE};
55
56         return OCPlatform::constructResourceObject(host, uri,
57                 connectivityType, false, types, ifaces);
58     }
59
60      //Get Test
61     TEST(ConstructResourceTest, ConstructResourceObject)
62     {
63         EXPECT_ANY_THROW(ConstructResourceObject(std::string(""), std::string("")));
64     }
65
66     TEST(ResourceGetTest, ResourceGetForValidUri)
67     {
68         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
69         EXPECT_TRUE(resource != NULL);
70         EXPECT_EQ(OC_STACK_OK, resource->get(OC::QueryParamsMap(), &onGetPut));
71
72     }
73
74     TEST(ResourceGetTest, ResourceGetForBadUri)
75     {
76         OCResource::Ptr resource = ConstructResourceObject("", "coap://192.168.1.2:5000");
77         EXPECT_TRUE(resource != NULL);
78         EXPECT_THROW(resource->get(OC::QueryParamsMap(), &onGetPut), OC::OCException);
79     }
80
81     TEST(ResourceGetTest, ResourceGetWithHighQos)
82     {
83         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
84         EXPECT_TRUE(resource != NULL);
85         EXPECT_EQ(OC_STACK_OK,
86                 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::HighQos));
87     }
88
89     TEST(ResourceGetTest, ResourceGetWithLowQos)
90     {
91         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
92         EXPECT_TRUE(resource != NULL);
93         EXPECT_EQ(OC_STACK_OK,
94                 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::LowQos));
95     }
96
97     TEST(ResourceGetTest, ResourceGetWithMidQos)
98     {
99         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
100         EXPECT_TRUE(resource != NULL);
101         EXPECT_EQ(OC_STACK_OK,
102                 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::MidQos));
103     }
104
105     TEST(ResourceGetTest, ResourceGetWithNaQos)
106     {
107         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
108         EXPECT_TRUE(resource != NULL);
109         EXPECT_EQ(OC_STACK_OK,
110                 resource->get(OC::QueryParamsMap(), &onGetPut, QualityOfService::NaQos));
111     }
112
113     TEST(ResourceGetTest, ResourceGetWithRTIFNaQos)
114     {
115         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
116         EXPECT_TRUE(resource != NULL);
117         EXPECT_EQ(OC_STACK_OK,
118                 resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut,
119                         QualityOfService::NaQos));
120     }
121
122     TEST(ResourceGetTest, ResourceGetWithNullResourceType)
123     {
124         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
125         EXPECT_TRUE(resource != NULL);
126         EXPECT_ANY_THROW(
127                 resource->get(nullptr, DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut,
128                         QualityOfService::NaQos));
129     }
130
131
132     TEST(ResourceGetTest, ResourceGetWithNullResourceInterface)
133     {
134         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
135         EXPECT_TRUE(resource != NULL);
136         EXPECT_ANY_THROW(
137                 resource->get("", nullptr, QueryParamsMap(), &onGetPut, QualityOfService::NaQos));
138     }
139
140     TEST(ResourceGetTest, ResourceGetWithTypeAndInterface)
141     {
142         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
143         EXPECT_TRUE(resource != NULL);
144         EXPECT_EQ(OC_STACK_OK, resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), &onGetPut));
145     }
146
147     //Post Test
148     TEST(ResourcePostTest, ResourcePostValidConfiguration)
149     {
150         OCRepresentation rep;
151         QueryParamsMap query = {};
152         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
153         EXPECT_TRUE(resource != NULL);
154         EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut));
155     }
156
157     TEST(ResourcePostTest, ResourcePostWithNaQos)
158     {
159         OCRepresentation rep;
160         QueryParamsMap query = {};
161         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
162         EXPECT_TRUE(resource != NULL);
163         EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::NaQos));
164     }
165
166     TEST(ResourcePostTest, ResourcePostWithMidQos)
167     {
168         OCRepresentation rep;
169         QueryParamsMap query = {};
170         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
171         EXPECT_TRUE(resource != NULL);
172         EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::MidQos));
173     }
174
175     TEST(ResourcePostTest, ResourcePostWithLowQos)
176     {
177         OCRepresentation rep;
178         QueryParamsMap query = {};
179         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
180         EXPECT_TRUE(resource != NULL);
181         EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::LowQos));
182     }
183
184     TEST(ResourcePostTest, ResourcePostWithHighQos)
185     {
186         OCRepresentation rep;
187         QueryParamsMap query = {};
188         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
189         EXPECT_TRUE(resource != NULL);
190         EXPECT_EQ(OC_STACK_OK, resource->post(rep, query, &onGetPut, QualityOfService::HighQos));
191     }
192
193     //Put Test
194     TEST(ResourcePutTest, ResourcePutForValid)
195     {
196         OCRepresentation rep;
197         QueryParamsMap query = {};
198         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
199         EXPECT_TRUE(resource != NULL);
200         EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut));
201     }
202
203     TEST(ResourcePutTest, ResourcePutWithNaQos)
204     {
205         OCRepresentation rep;
206         QueryParamsMap query = {};
207         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
208         EXPECT_TRUE(resource != NULL);
209         EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::NaQos));
210     }
211
212     TEST(ResourcePutTest, ResourcePutWithLowQos)
213     {
214         OCRepresentation rep;
215         QueryParamsMap query = {};
216         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
217         EXPECT_TRUE(resource != NULL);
218         EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::LowQos));
219     }
220
221     TEST(ResourcePutTest, ResourcePutWithMidQos)
222     {
223         OCRepresentation rep;
224         QueryParamsMap query = {};
225         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
226         EXPECT_TRUE(resource != NULL);
227         EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::MidQos));
228     }
229
230     TEST(ResourcePutTest, ResourcePutWithHighQos)
231     {
232         OCRepresentation rep;
233         QueryParamsMap query = {};
234         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
235         EXPECT_TRUE(resource != NULL);
236         EXPECT_EQ(OC_STACK_OK, resource->put(rep, query, &onGetPut, QualityOfService::HighQos));
237     }
238
239     TEST(ResourcePutTest, ResourcePutWithTypeAndInterface)
240     {
241         OCRepresentation rep;
242         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
243         EXPECT_TRUE(resource != NULL);
244         EXPECT_EQ(OC_STACK_OK,
245                 resource->put("", DEFAULT_INTERFACE, rep, QueryParamsMap(), &onGetPut));
246     }
247
248     TEST(ResourcePutTest, ResourcePutWithNullType)
249     {
250         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
251         EXPECT_TRUE(resource != NULL);
252         OCRepresentation rep;
253         EXPECT_ANY_THROW(
254                 resource->put(nullptr, DEFAULT_INTERFACE, rep, QueryParamsMap(), &onGetPut));
255         HeaderOptions headerOptions = {};
256         onGetPut(headerOptions, rep, OC_STACK_OK);
257     }
258
259     TEST(ResourcePutTest, ResourcePutWithNullInterface)
260     {
261         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
262         EXPECT_TRUE(resource != NULL);
263         OCRepresentation rep;
264         EXPECT_ANY_THROW(resource->put("", nullptr, rep, QueryParamsMap(), &onGetPut));
265     }
266
267     // Observer Test
268     TEST(ResourceObserveTest, ResourceObserveValidUri)
269     {
270         OCResource::Ptr resource =
271                 ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
272         EXPECT_TRUE(resource != NULL);
273         QueryParamsMap query = {};
274         EXPECT_EQ(OC_STACK_OK, resource->observe(ObserveType::ObserveAll, query, &onObserve));
275     }
276
277     TEST(ResourceObserveTest, ResourceObserveLoQos)
278     {
279         QueryParamsMap query = {};
280         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
281         EXPECT_TRUE(resource != NULL);
282         EXPECT_EQ(OC_STACK_OK,
283                 resource->observe(ObserveType::ObserveAll, query, &onObserve,
284                         QualityOfService::LowQos));
285     }
286
287     TEST(ResourceObserveTest, ResourceObserveNaQos)
288     {
289         QueryParamsMap query = {};
290         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
291         EXPECT_TRUE(resource != NULL);
292         EXPECT_EQ(OC_STACK_OK,
293                 resource->observe(ObserveType::ObserveAll, query, &onObserve,
294                         QualityOfService::NaQos));
295     }
296
297     TEST(ResourceObserveTest, ResourceObserveMidQos)
298     {
299         QueryParamsMap query = {};
300         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
301         EXPECT_TRUE(resource != NULL);
302         EXPECT_EQ(OC_STACK_OK,
303                 resource->observe(ObserveType::ObserveAll, query, &onObserve,
304                         QualityOfService::MidQos));
305     }
306
307     TEST(ResourceObserveTest, ResourceObserveHighQos)
308     {
309         QueryParamsMap query = {};
310         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/Observe");
311         EXPECT_TRUE(resource != NULL);
312         EXPECT_EQ(OC_STACK_OK,
313                 resource->observe(ObserveType::ObserveAll, query, &onObserve,
314                         QualityOfService::HighQos));
315     }
316
317     TEST(ResourceObserveTest, ResourceCancelObserveValidUri)
318     {
319         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
320         EXPECT_TRUE(resource != NULL);
321         QueryParamsMap query = {};
322         EXPECT_EQ(OC_STACK_OK, resource->observe(ObserveType::ObserveAll, query, &onObserve));
323         EXPECT_EQ(OC_STACK_OK, resource->cancelObserve());
324     }
325
326     TEST(ResourceObserveTest, ResourceCancelObserveWithNaQos)
327     {
328         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
329         EXPECT_TRUE(resource != NULL);
330         QueryParamsMap query = {};
331         EXPECT_EQ(OC_STACK_OK,
332                 resource->observe(ObserveType::ObserveAll, query, &onObserve,
333                         QualityOfService::NaQos));
334         EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::NaQos));
335     }
336
337     TEST(ResourceObserveTest, ResourceCancelObserveWithLowQos)
338     {
339         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
340         EXPECT_TRUE(resource != NULL);
341         QueryParamsMap query = {};
342         EXPECT_EQ(OC_STACK_OK,
343                 resource->observe(ObserveType::ObserveAll, query, &onObserve,
344                         QualityOfService::LowQos));
345         EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::LowQos));
346     }
347
348     TEST(ResourceObserveTest, ResourceCancelObserveWithMidQos)
349     {
350         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
351         EXPECT_TRUE(resource != NULL);
352             QueryParamsMap query = {};
353         EXPECT_EQ(OC_STACK_OK,
354                 resource->observe(ObserveType::ObserveAll, query, &onObserve,
355                         QualityOfService::MidQos));
356         EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::MidQos));
357     }
358
359     TEST(ResourceObserveTest, ResourceCancelObserveWithHighQos)
360     {
361         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
362         EXPECT_TRUE(resource != NULL);
363             QueryParamsMap query = {};
364         EXPECT_EQ(OC_STACK_OK,
365                 resource->observe(ObserveType::ObserveAll, query, &onObserve,
366                         QualityOfService::HighQos));
367         EXPECT_EQ(OC_STACK_OK, resource->cancelObserve(QualityOfService::HighQos));
368     }
369
370     //DeleteResource
371     TEST(DeleteResourceTest, DeleteResourceValidUri)
372     {
373         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
374         EXPECT_TRUE(resource != NULL);
375         EXPECT_EQ(OC_STACK_OK, resource->deleteResource(&deleteResponse));
376     }
377
378     TEST(DeleteResourceTest, DeleteResourceWithLowQos)
379     {
380         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
381         EXPECT_TRUE(resource != NULL);
382         EXPECT_EQ(OC_STACK_OK,
383                 resource->deleteResource(&deleteResponse, QualityOfService::LowQos));
384     }
385
386     TEST(DeleteResourceTest, DeleteResourceWithMidQos)
387     {
388         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
389         EXPECT_TRUE(resource != NULL);
390         EXPECT_EQ(OC_STACK_OK,
391                 resource->deleteResource(&deleteResponse, QualityOfService::MidQos));
392     }
393
394     TEST(DeleteResourceTest, DeleteResourceWithHighQos)
395     {
396         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
397         EXPECT_TRUE(resource != NULL);
398         EXPECT_EQ(OC_STACK_OK,
399                 resource->deleteResource(&deleteResponse, QualityOfService::HighQos));
400     }
401     TEST(DeleteResourceTest, DeleteResourceWithNaQos)
402     {
403         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
404         EXPECT_TRUE(resource != NULL);
405         EXPECT_EQ(OC_STACK_OK, resource->deleteResource(&deleteResponse, QualityOfService::NaQos));
406     }
407
408     //GetResourceInterfaces Test
409     TEST(GetResourceInterfacesTest, GetResourceInterfaces)
410     {
411         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
412         EXPECT_TRUE(resource != NULL);
413         EXPECT_TRUE(resource->getResourceInterfaces().front() == DEFAULT_INTERFACE);
414     }
415
416     //GetResourceTypes Test
417     TEST(GetResourceTypesTest, GetResourceTypes)
418     {
419         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
420         EXPECT_TRUE(resource != NULL);
421         EXPECT_TRUE(resource->getResourceTypes().front() == "intel.rpost");
422     }
423
424     // Host Test
425     TEST(HostTest, Host)
426     {
427         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
428         EXPECT_TRUE(resource != NULL);
429         EXPECT_TRUE(resource->host() == "coap://192.168.1.2:5000");
430     }
431
432     //Uri Test
433     TEST(UriTest, Uri)
434     {
435         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
436         EXPECT_TRUE(resource != NULL);
437         EXPECT_TRUE(resource->uri() == "/resource");
438     }
439
440     //ConnectivityType Test
441     TEST(ConnectivityTypeTest, ConnectivityType)
442     {
443         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
444         EXPECT_TRUE(resource != NULL);
445         EXPECT_TRUE(resource->connectivityType() == OC_WIFI);
446     }
447
448     //IsObservable Test
449     TEST(IsObservableTest, isObservable)
450     {
451         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
452         EXPECT_TRUE(resource != NULL);
453         EXPECT_FALSE(resource->isObservable());
454     }
455
456     //SID Test
457     TEST(SidTest, sid)
458     {
459         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
460         EXPECT_TRUE(resource != NULL);
461         EXPECT_TRUE(resource->sid() == "0");
462     }
463
464     //UniqueIdentifier Test
465     TEST(UniqueIdentifierTest, uniqueIdentifier)
466     {
467         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
468         EXPECT_TRUE(resource != NULL);
469         std::ostringstream ss;
470         ss << resource->uniqueIdentifier();
471         EXPECT_TRUE(ss.str() == "0/resource");
472     }
473
474     // HeaderOptions Test
475     TEST(HeaderOptionsTest, SetHeaderOptionsValidInput)
476     {
477         const uint16_t API_VERSION = 2048;
478         const uint16_t TOKEN = 3000;
479         HeaderOptions headerOptions;
480         HeaderOption::OCHeaderOption apiVersion(API_VERSION, "v.1.0");
481         HeaderOption::OCHeaderOption clientToken(TOKEN, "21ae43gf");
482         headerOptions.push_back(apiVersion);
483         headerOptions.push_back(clientToken);
484
485         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
486         EXPECT_TRUE(resource != NULL);
487         EXPECT_NO_THROW(resource->setHeaderOptions(headerOptions));
488         EXPECT_NO_THROW(resource->unsetHeaderOptions());
489     }
490
491     TEST(HeaderOptionsTest, SetHeaderOptionsEmptyInput)
492     {
493         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
494         EXPECT_TRUE(resource != NULL);
495         HeaderOptions headerOptions = {};
496         EXPECT_NO_THROW(resource->setHeaderOptions(headerOptions));
497         EXPECT_NO_THROW(resource->unsetHeaderOptions());
498     }
499 }
500