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