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