[resource-manipulation] Add Test Helper class
[platform/upstream/iotivity.git] / service / resource-manipulation / src / serverBuilder / unittests / RequestHandlerTest.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 <RequestHandler.h>
24
25 #include <OCPlatform.h>
26
27 using namespace std;
28
29 using namespace OIC::Service;
30
31 constexpr char EXISTING[]{ "ext" };
32 constexpr int ORIGIN_VALUE{ 100 };
33
34 constexpr int NEW_VALUE{ 1 };
35
36 typedef OCStackResult (*RegisterResource)(OCResourceHandle&, std::string&,
37         const std::string&, const std::string&, OC::EntityHandler, uint8_t);
38
39 class RequestHandlerTest: public TestWithMock
40 {
41 public:
42     ResourceObject::Ptr server;
43
44 protected:
45     void SetUp()
46     {
47         TestWithMock::SetUp();
48
49         mocks.OnCallFuncOverload(static_cast<RegisterResource>(OC::OCPlatform::registerResource))
50                 .Return(OC_STACK_OK);
51
52         mocks.OnCallFunc(OC::OCPlatform::unregisterResource).Return(OC_STACK_OK);
53
54         server = ResourceObject::Builder("a/test", "resourceType", "").build();
55
56         server->setAutoNotifyPolicy(ResourceObject::AutoNotifyPolicy::NEVER);
57         server->setAttribute(EXISTING, ORIGIN_VALUE);
58     }
59 };
60
61 TEST_F(RequestHandlerTest, ResponseHasSameValuesPassedToHandlerConstructor)
62 {
63     RequestHandler handler{ OC_EH_ERROR, -1000 };
64
65     auto response = handler.buildResponse(*server);
66
67     ASSERT_EQ(OC_EH_ERROR, response->getResponseResult());
68     ASSERT_EQ(-1000, response->getErrorCode());
69 }
70
71 TEST_F(RequestHandlerTest, ResponseHasSameAttrsWithServerAttrs)
72 {
73     RequestHandler handler{};
74
75     auto response = handler.buildResponse(*server);
76
77     ASSERT_EQ(ORIGIN_VALUE, response->getResourceRepresentation()[EXISTING].getValue<int>());
78 }
79
80 TEST_F(RequestHandlerTest, ResponseHasAttrsSetByCustomAttrRequestHandler)
81 {
82     constexpr char key[] { "key" };
83     constexpr int newValue{ 100 };
84
85     ResourceAttributes attrs;
86     attrs[key] = newValue;
87     RequestHandler handler{ attrs };
88
89     auto response = handler.buildResponse(*server);
90
91     ASSERT_EQ(ORIGIN_VALUE, response->getResourceRepresentation()[key].getValue<int>());
92 }
93
94
95
96 class SetRequestHandlerAcceptanceTest: public RequestHandlerTest
97 {
98 public:
99     SetRequestHandler::Ptr setRequestHandler;
100
101     ResourceAttributes requestAttrs;
102
103 protected:
104     void SetUp()
105     {
106         RequestHandlerTest::SetUp();
107
108         setRequestHandler = make_shared< SetRequestHandler >();
109
110         requestAttrs[EXISTING] = NEW_VALUE;
111     }
112 };
113
114 TEST_F(SetRequestHandlerAcceptanceTest, NothingReplacedWithIgnoreMethod)
115 {
116     auto replaced = setRequestHandler->applyAcceptanceMethod(
117             RCSSetResponse::AcceptanceMethod::IGNORE, *server, requestAttrs);
118
119     ASSERT_TRUE(replaced.empty());
120 }
121
122
123 TEST_F(SetRequestHandlerAcceptanceTest, NewValueApplyedWithAcceptMethod)
124 {
125     setRequestHandler->applyAcceptanceMethod(
126             RCSSetResponse::AcceptanceMethod::ACCEPT, *server, requestAttrs);
127
128     ASSERT_EQ(NEW_VALUE, server->getAttribute<int>(EXISTING));
129 }
130
131 TEST_F(SetRequestHandlerAcceptanceTest, ReturnedAttrPairsHaveOldValue)
132 {
133     auto replaced = setRequestHandler->applyAcceptanceMethod(
134             RCSSetResponse::AcceptanceMethod::ACCEPT, *server, requestAttrs);
135
136     ASSERT_EQ(ORIGIN_VALUE, replaced[0].second);
137 }
138
139 TEST_F(SetRequestHandlerAcceptanceTest, NothingHappenedWithEmptyAttrs)
140 {
141     setRequestHandler->applyAcceptanceMethod(
142             RCSSetResponse::AcceptanceMethod::ACCEPT, *server, ResourceAttributes{ });
143
144     ASSERT_EQ(ORIGIN_VALUE, server->getAttribute<int>(EXISTING));
145 }
146
147 TEST_F(SetRequestHandlerAcceptanceTest, EverythingAppliedIfMethodIsAccept)
148 {
149     requestAttrs[EXISTING] = "";
150
151     auto replaced = setRequestHandler->applyAcceptanceMethod(
152              RCSSetResponse::AcceptanceMethod::ACCEPT, *server, requestAttrs);
153
154      ASSERT_EQ(ORIGIN_VALUE, replaced[0].second);
155 }
156
157
158 TEST_F(SetRequestHandlerAcceptanceTest, NoReplaceIfMethodIsDefaultAndTypeMismatch)
159 {
160     requestAttrs[EXISTING] = "";
161
162     auto replaced = setRequestHandler->applyAcceptanceMethod(
163              RCSSetResponse::AcceptanceMethod::DEFAULT, *server, requestAttrs);
164
165      ASSERT_TRUE(replaced.empty());
166 }
167
168 TEST_F(SetRequestHandlerAcceptanceTest, NoReplacefMethodIsDefaultAndRequestAttrsHasUnknownKey)
169 {
170     constexpr char unknownKey[]{ "???" };
171
172     requestAttrs[EXISTING] = ORIGIN_VALUE;
173     requestAttrs[unknownKey] = ORIGIN_VALUE;
174
175
176     auto replaced = setRequestHandler->applyAcceptanceMethod(
177              RCSSetResponse::AcceptanceMethod::DEFAULT, *server, requestAttrs);
178
179      ASSERT_TRUE(replaced.empty());
180 }