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