1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include <UnitTestHelper.h>
23 #include <RequestHandler.h>
24 #include <RCSResourceObject.h>
26 #include <OCPlatform.h>
30 using namespace OIC::Service;
32 constexpr char EXISTING[]{ "ext" };
33 constexpr int ORIGIN_VALUE{ 100 };
35 constexpr int NEW_VALUE{ 1 };
37 typedef OCStackResult (*RegisterResource)(OCResourceHandle&, std::string&,
38 const std::string&, const std::string&, OC::EntityHandler, uint8_t);
40 class RequestHandlerTest: public TestWithMock
43 RCSResourceObject::Ptr server;
48 TestWithMock::SetUp();
50 mocks.OnCallFuncOverload(static_cast<RegisterResource>(OC::OCPlatform::registerResource))
53 mocks.OnCallFunc(OC::OCPlatform::unregisterResource).Return(OC_STACK_OK);
55 server = RCSResourceObject::Builder("a/test", "resourceType", "").build();
57 server->setAutoNotifyPolicy(RCSResourceObject::AutoNotifyPolicy::NEVER);
58 server->setAttribute(EXISTING, ORIGIN_VALUE);
62 TEST_F(RequestHandlerTest, ResponseHasSameValuesPassedToHandlerConstructor)
64 RequestHandler handler{ OC_EH_ERROR, -1000 };
66 auto response = handler.buildResponse(*server);
68 ASSERT_EQ(OC_EH_ERROR, response->getResponseResult());
69 ASSERT_EQ(-1000, response->getErrorCode());
72 TEST_F(RequestHandlerTest, ResponseHasSameAttrsWithServerAttrs)
74 RequestHandler handler{};
76 auto response = handler.buildResponse(*server);
78 ASSERT_EQ(ORIGIN_VALUE, response->getResourceRepresentation()[EXISTING].getValue<int>());
81 TEST_F(RequestHandlerTest, ResponseHasAttrsSetByCustomAttrRequestHandler)
83 constexpr char key[] { "key" };
84 constexpr int newValue{ 100 };
86 RCSResourceAttributes attrs;
87 attrs[key] = newValue;
88 RequestHandler handler{ attrs };
90 auto response = handler.buildResponse(*server);
92 ASSERT_EQ(ORIGIN_VALUE, response->getResourceRepresentation()[key].getValue<int>());
97 class SetRequestHandlerAcceptanceTest: public RequestHandlerTest
100 SetRequestHandler::Ptr setRequestHandler;
102 RCSResourceAttributes requestAttrs;
107 RequestHandlerTest::SetUp();
109 setRequestHandler = make_shared< SetRequestHandler >();
111 requestAttrs[EXISTING] = NEW_VALUE;
115 TEST_F(SetRequestHandlerAcceptanceTest, NothingReplacedWithIgnoreMethod)
117 auto replaced = setRequestHandler->applyAcceptanceMethod(
118 RCSSetResponse::AcceptanceMethod::IGNORE, *server, requestAttrs);
120 ASSERT_TRUE(replaced.empty());
124 TEST_F(SetRequestHandlerAcceptanceTest, NewValueApplyedWithAcceptMethod)
126 setRequestHandler->applyAcceptanceMethod(
127 RCSSetResponse::AcceptanceMethod::ACCEPT, *server, requestAttrs);
129 ASSERT_EQ(NEW_VALUE, server->getAttribute<int>(EXISTING));
132 TEST_F(SetRequestHandlerAcceptanceTest, ReturnedAttrPairsHaveOldValue)
134 auto replaced = setRequestHandler->applyAcceptanceMethod(
135 RCSSetResponse::AcceptanceMethod::ACCEPT, *server, requestAttrs);
137 ASSERT_EQ(ORIGIN_VALUE, replaced[0].second);
140 TEST_F(SetRequestHandlerAcceptanceTest, NothingHappenedWithEmptyAttrs)
142 setRequestHandler->applyAcceptanceMethod(
143 RCSSetResponse::AcceptanceMethod::ACCEPT, *server, RCSResourceAttributes{ });
145 ASSERT_EQ(ORIGIN_VALUE, server->getAttribute<int>(EXISTING));
148 TEST_F(SetRequestHandlerAcceptanceTest, EverythingAppliedIfMethodIsAccept)
150 requestAttrs[EXISTING] = "";
152 auto replaced = setRequestHandler->applyAcceptanceMethod(
153 RCSSetResponse::AcceptanceMethod::ACCEPT, *server, requestAttrs);
155 ASSERT_EQ(ORIGIN_VALUE, replaced[0].second);
159 TEST_F(SetRequestHandlerAcceptanceTest, NoReplaceIfMethodIsDefaultAndTypeMismatch)
161 requestAttrs[EXISTING] = "";
163 auto replaced = setRequestHandler->applyAcceptanceMethod(
164 RCSSetResponse::AcceptanceMethod::DEFAULT, *server, requestAttrs);
166 ASSERT_TRUE(replaced.empty());
169 TEST_F(SetRequestHandlerAcceptanceTest, NoReplacefMethodIsDefaultAndRequestAttrsHasUnknownKey)
171 constexpr char unknownKey[]{ "???" };
173 requestAttrs[EXISTING] = ORIGIN_VALUE;
174 requestAttrs[unknownKey] = ORIGIN_VALUE;
177 auto replaced = setRequestHandler->applyAcceptanceMethod(
178 RCSSetResponse::AcceptanceMethod::DEFAULT, *server, requestAttrs);
180 ASSERT_TRUE(replaced.empty());