Repo Merge: Moving resource API down a directory
[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                     std::shared_ptr<OCResourceResponse> response)
40 {
41     return OC_EH_OK;
42 }
43
44 // Condition variables used for register and find
45 std::mutex mutex_rf;
46 std::condition_variable cv_rf;
47 std::shared_ptr <OCResource> res_rf;
48
49 void foundResource_rf(std::shared_ptr<OCResource> resource)
50 {
51     if(resource)
52     {
53         res_rf = resource;
54         cv_rf.notify_all();
55     }
56 }
57
58 // Resource : Register and find test
59 TEST(Resource, rf) {
60     // Create PlatformConfig object
61     PlatformConfig cfg {
62         OC::ServiceType::InProc,
63         OC::ModeType::Both,
64         "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
65         0,         // Uses randomly available port
66         OC::QualityOfService::LowQos
67     };
68     OCPlatform::Configure(cfg);
69
70     std::string resourceURI = "/a/res";
71     std::string resourceTypeName = "core.res";
72     std::string resourceInterface = DEFAULT_INTERFACE;
73
74     uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
75
76     OCResourceHandle resourceHandle;
77
78     // This will internally create and register the resource.
79     if(OC_STACK_OK == OCPlatform::registerResource(
80                                     resourceHandle, resourceURI, resourceTypeName,
81                                     resourceInterface, entityHandler_rf, resourceProperty))
82     {
83         OCPlatform::findResource("","coap://224.0.1.187/oc/core?rt=core.res", foundResource_rf);
84
85         {
86             std::unique_lock<std::mutex> lk(mutex_rf);
87             cv_rf.wait(lk);
88         }
89
90         if(res_rf)
91         {
92             EXPECT_EQ(res_rf->uri(), "/a/res");
93             vector<std::string> rts = res_rf->getResourceTypes();
94             EXPECT_EQ(rts.size(), (unsigned) 1);
95             EXPECT_EQ(rts[0], "core.res");
96             vector<std::string> ifs = res_rf->getResourceInterfaces();
97             EXPECT_EQ(ifs.size(), (unsigned) 1);
98             EXPECT_EQ(ifs[0], DEFAULT_INTERFACE);
99         }
100     }
101 }
102