iotivity 0.9.0
[platform/upstream/iotivity.git] / resource / unittests / tests.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 <iostream>
23 #include <thread>
24 #include <functional>
25 #include <condition_variable>
26
27 #include <OCPlatform.h>
28 #include <OCApi.h>
29
30 #include <gtest/gtest.h>
31
32 namespace PH = std::placeholders;
33
34 using namespace OC;
35 using namespace std;
36
37 // Entity handler used for register and find test
38 OCEntityHandlerResult entityHandler_rf(std::shared_ptr<OCResourceRequest> request)
39 {
40     return OC_EH_OK;
41 }
42
43 // Condition variables used for register and find
44 std::mutex mutex_rf;
45 std::condition_variable cv_rf;
46 std::shared_ptr <OCResource> res_rf;
47
48 void foundResource_rf(std::shared_ptr<OCResource> resource)
49 {
50     if(resource)
51     {
52         res_rf = resource;
53         cv_rf.notify_all();
54     }
55 }
56
57 // Resource : Register and find test
58 TEST(Resource, rf) {
59     // Create PlatformConfig object
60     PlatformConfig cfg {
61         OC::ServiceType::InProc,
62         OC::ModeType::Both,
63         "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
64         0,         // Uses randomly available port
65         OC::QualityOfService::LowQos
66     };
67     OCPlatform::Configure(cfg);
68
69     std::string resourceURI = "/a/res";
70     std::string resourceTypeName = "core.res";
71     std::string resourceInterface = DEFAULT_INTERFACE;
72
73     uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
74
75     OCResourceHandle resourceHandle;
76
77     // This will internally create and register the resource.
78     if(OC_STACK_OK == OCPlatform::registerResource(
79                                     resourceHandle, resourceURI, resourceTypeName,
80                                     resourceInterface, entityHandler_rf, resourceProperty))
81     {
82         OCPlatform::findResource("","coap://224.0.1.187/oc/core?rt=core.res", foundResource_rf);
83
84         {
85             std::unique_lock<std::mutex> lk(mutex_rf);
86             cv_rf.wait(lk);
87         }
88
89         if(res_rf)
90         {
91             EXPECT_EQ(res_rf->uri(), "/a/res");
92             vector<std::string> rts = res_rf->getResourceTypes();
93             EXPECT_EQ(rts.size(), (unsigned) 1);
94             EXPECT_EQ(rts[0], "core.res");
95             vector<std::string> ifs = res_rf->getResourceInterfaces();
96             EXPECT_EQ(ifs.size(), (unsigned) 1);
97             EXPECT_EQ(ifs[0], DEFAULT_INTERFACE);
98         }
99     }
100 }
101