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 <gtest/gtest.h>
22 #include <HippoMocks/hippomocks.h>
24 #include <RequestHandler.h>
26 #include <OCPlatform.h>
30 using namespace testing;
31 using namespace OIC::Service;
33 constexpr char EXISTING[]{ "ext" };
34 constexpr int ORIGIN_VALUE{ 100 };
36 constexpr int NEW_VALUE{ 1 };
38 typedef OCStackResult (*RegisterResource)(OCResourceHandle&, std::string&,
39 const std::string&, const std::string&, OC::EntityHandler, uint8_t);
41 class RequestHandlerTest: public Test
44 ResourceObject::Ptr server;
51 mocks.OnCallFuncOverload(static_cast<RegisterResource>(OC::OCPlatform::registerResource))
54 mocks.OnCallFunc(OC::OCPlatform::unregisterResource).Return(OC_STACK_OK);
56 server = ResourceObject::Builder("a/test", "resourceType", "").build();
58 server->setAutoNotifyPolicy(ResourceObject::AutoNotifyPolicy::NEVER);
59 server->setAttribute(EXISTING, ORIGIN_VALUE);
63 TEST_F(RequestHandlerTest, ResponseHasSameValuesPassedToHandlerConstructor)
65 RequestHandler handler{ OC_EH_ERROR, -1000 };
67 auto response = handler.buildResponse(*server);
69 ASSERT_EQ(OC_EH_ERROR, response->getResponseResult());
70 ASSERT_EQ(-1000, response->getErrorCode());
73 TEST_F(RequestHandlerTest, ResponseHasSameAttrsWithServerAttrs)
75 RequestHandler handler{};
77 auto response = handler.buildResponse(*server);
79 ASSERT_EQ(ORIGIN_VALUE, response->getResourceRepresentation()[EXISTING].getValue<int>());
82 TEST_F(RequestHandlerTest, ResponseHasAttrsSetByCustomAttrRequestHandler)
84 constexpr char key[] { "key" };
85 constexpr int newValue{ 100 };
87 ResourceAttributes attrs;
88 attrs[key] = newValue;
89 RequestHandler handler{ attrs };
91 auto response = handler.buildResponse(*server);
93 ASSERT_EQ(ORIGIN_VALUE, response->getResourceRepresentation()[key].getValue<int>());
98 class SetRequestHandlerAcceptanceTest: public RequestHandlerTest
101 SetRequestHandler::Ptr setRequestHandler;
103 ResourceAttributes requestAttrs;
108 RequestHandlerTest::SetUp();
110 setRequestHandler = make_shared< SetRequestHandler >();
112 requestAttrs[EXISTING] = NEW_VALUE;
116 TEST_F(SetRequestHandlerAcceptanceTest, NothingReplacedWithIgnoreMethod)
118 auto replaced = setRequestHandler->applyAcceptanceMethod(
119 RCSSetResponse::AcceptanceMethod::IGNORE, *server, requestAttrs);
121 ASSERT_TRUE(replaced.empty());
125 TEST_F(SetRequestHandlerAcceptanceTest, NewValueApplyedWithAcceptMethod)
127 setRequestHandler->applyAcceptanceMethod(
128 RCSSetResponse::AcceptanceMethod::ACCEPT, *server, requestAttrs);
130 ASSERT_EQ(NEW_VALUE, server->getAttribute<int>(EXISTING));
133 TEST_F(SetRequestHandlerAcceptanceTest, ReturnedAttrPairsHaveOldValue)
135 auto replaced = setRequestHandler->applyAcceptanceMethod(
136 RCSSetResponse::AcceptanceMethod::ACCEPT, *server, requestAttrs);
138 ASSERT_EQ(ORIGIN_VALUE, replaced[0].second);
141 TEST_F(SetRequestHandlerAcceptanceTest, NothingHappenedWithEmptyAttrs)
143 setRequestHandler->applyAcceptanceMethod(
144 RCSSetResponse::AcceptanceMethod::ACCEPT, *server, ResourceAttributes{ });
146 ASSERT_EQ(ORIGIN_VALUE, server->getAttribute<int>(EXISTING));
149 TEST_F(SetRequestHandlerAcceptanceTest, EverythingAppliedIfMethodIsAccept)
151 requestAttrs[EXISTING] = "";
153 auto replaced = setRequestHandler->applyAcceptanceMethod(
154 RCSSetResponse::AcceptanceMethod::ACCEPT, *server, requestAttrs);
156 ASSERT_EQ(ORIGIN_VALUE, replaced[0].second);
160 TEST_F(SetRequestHandlerAcceptanceTest, NoReplaceIfMethodIsDefaultAndTypeMismatch)
162 requestAttrs[EXISTING] = "";
164 auto replaced = setRequestHandler->applyAcceptanceMethod(
165 RCSSetResponse::AcceptanceMethod::DEFAULT, *server, requestAttrs);
167 ASSERT_TRUE(replaced.empty());
170 TEST_F(SetRequestHandlerAcceptanceTest, NoReplacefMethodIsDefaultAndRequestAttrsHasUnknownKey)
172 constexpr char unknownKey[]{ "???" };
174 requestAttrs[EXISTING] = ORIGIN_VALUE;
175 requestAttrs[unknownKey] = ORIGIN_VALUE;
178 auto replaced = setRequestHandler->applyAcceptanceMethod(
179 RCSSetResponse::AcceptanceMethod::DEFAULT, *server, requestAttrs);
181 ASSERT_TRUE(replaced.empty());