Apply to Resource Hosting to RE resource server added feature.
[platform/upstream/iotivity.git] / service / resource-hosting / unittest / RequestObjectUnitTest.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 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 "UnitTestHelper.h"
22
23 #include "ResourceEncapsulationTestSimulator.h"
24 #include "OCResourceRequest.h"
25 #include "RequestObject.h"
26 #include "RCSRequest.h"
27
28 using namespace testing;
29 using namespace OIC::Service;
30
31 namespace
32 {
33     void setRequestCB(
34             const RCSResourceAttributes &, int,
35             const RCSRequest &, RequestObject::Ptr) { }
36 }
37
38 class RequestObjectTest : public TestWithMock
39 {
40 public:
41     ResourceEncapsulationTestSimulator::Ptr testObject;
42     RCSResourceObject::Ptr server;
43     RCSRemoteResourceObject::Ptr remoteObject;
44
45     RCSResourceAttributes attr;
46
47     std::mutex mutexForCondition;
48     std::condition_variable responseCon;
49
50 protected:
51
52     void SetUp()
53     {
54         TestWithMock::SetUp();
55
56         testObject = std::make_shared<ResourceEncapsulationTestSimulator>();
57         testObject->defaultRunSimulator();
58         remoteObject = testObject->getRemoteResource();
59     }
60
61     void TearDown()
62     {
63         TestWithMock::TearDown();
64         if(remoteObject)
65         {
66             if(remoteObject->isCaching())
67             {
68                 remoteObject->stopCaching();
69             }
70             if(remoteObject->isMonitoring())
71             {
72                 remoteObject->stopMonitoring();
73             }
74         }
75         testObject->destroy();
76     }
77
78 public:
79     void waitForCondition(int waitingTime = 1000)
80     {
81         std::unique_lock< std::mutex > lock{ mutexForCondition };
82         responseCon.wait_for(lock, std::chrono::milliseconds{ waitingTime });
83     }
84
85     void notifyCondition()
86     {
87         responseCon.notify_all();
88     }
89 };
90
91 TEST_F(RequestObjectTest, invokeRequestExpectCallwithSetter)
92 {
93    bool isCalled = false;
94
95    mocks.ExpectCallFunc(setRequestCB).Do(
96            [this, &isCalled](const RCSResourceAttributes &, int,
97                    const RCSRequest &, RequestObject::Ptr)
98            {
99                isCalled = true;
100                notifyCondition();
101            });
102
103    RCSResourceAttributes att;
104    std::shared_ptr<OC::OCResourceRequest> request;
105    RequestObject::invokeRequest(remoteObject, RCSRequest(testObject->getResourceServer(), request),
106            RequestObject::RequestMethod::Set, att, setRequestCB);
107
108    waitForCondition();
109
110    ASSERT_TRUE(isCalled);
111 }