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>
25 #include <OCPlatform.h>
29 using namespace OIC::Service;
31 constexpr char EXISTING[]{ "ext" };
32 constexpr int ORIGIN_VALUE{ 100 };
34 constexpr int NEW_VALUE{ 1 };
36 typedef OCStackResult (*RegisterResource)(OCResourceHandle&, std::string&,
37 const std::string&, const std::string&, OC::EntityHandler, uint8_t);
39 class RequestHandlerTest: public TestWithMock
42 ResourceObject::Ptr server;
47 TestWithMock::SetUp();
49 mocks.OnCallFuncOverload(static_cast<RegisterResource>(OC::OCPlatform::registerResource))
52 mocks.OnCallFunc(OC::OCPlatform::unregisterResource).Return(OC_STACK_OK);
54 server = ResourceObject::Builder("a/test", "resourceType", "").build();
56 server->setAutoNotifyPolicy(ResourceObject::AutoNotifyPolicy::NEVER);
57 server->setAttribute(EXISTING, ORIGIN_VALUE);
61 TEST_F(RequestHandlerTest, ResponseHasSameValuesPassedToHandlerConstructor)
63 RequestHandler handler{ OC_EH_ERROR, -1000 };
65 auto response = handler.buildResponse(*server);
67 ASSERT_EQ(OC_EH_ERROR, response->getResponseResult());
68 ASSERT_EQ(-1000, response->getErrorCode());
71 TEST_F(RequestHandlerTest, ResponseHasSameAttrsWithServerAttrs)
73 RequestHandler handler{};
75 auto response = handler.buildResponse(*server);
77 ASSERT_EQ(ORIGIN_VALUE, response->getResourceRepresentation()[EXISTING].getValue<int>());
80 TEST_F(RequestHandlerTest, ResponseHasAttrsSetByCustomAttrRequestHandler)
82 constexpr char key[] { "key" };
83 constexpr int newValue{ 100 };
85 ResourceAttributes attrs;
86 attrs[key] = newValue;
87 RequestHandler handler{ attrs };
89 auto response = handler.buildResponse(*server);
91 ASSERT_EQ(ORIGIN_VALUE, response->getResourceRepresentation()[key].getValue<int>());
96 class SetRequestHandlerAcceptanceTest: public RequestHandlerTest
99 SetRequestHandler::Ptr setRequestHandler;
101 ResourceAttributes requestAttrs;
106 RequestHandlerTest::SetUp();
108 setRequestHandler = make_shared< SetRequestHandler >();
110 requestAttrs[EXISTING] = NEW_VALUE;
114 TEST_F(SetRequestHandlerAcceptanceTest, NothingReplacedWithIgnoreMethod)
116 auto replaced = setRequestHandler->applyAcceptanceMethod(
117 RCSSetResponse::AcceptanceMethod::IGNORE, *server, requestAttrs);
119 ASSERT_TRUE(replaced.empty());
123 TEST_F(SetRequestHandlerAcceptanceTest, NewValueApplyedWithAcceptMethod)
125 setRequestHandler->applyAcceptanceMethod(
126 RCSSetResponse::AcceptanceMethod::ACCEPT, *server, requestAttrs);
128 ASSERT_EQ(NEW_VALUE, server->getAttribute<int>(EXISTING));
131 TEST_F(SetRequestHandlerAcceptanceTest, ReturnedAttrPairsHaveOldValue)
133 auto replaced = setRequestHandler->applyAcceptanceMethod(
134 RCSSetResponse::AcceptanceMethod::ACCEPT, *server, requestAttrs);
136 ASSERT_EQ(ORIGIN_VALUE, replaced[0].second);
139 TEST_F(SetRequestHandlerAcceptanceTest, NothingHappenedWithEmptyAttrs)
141 setRequestHandler->applyAcceptanceMethod(
142 RCSSetResponse::AcceptanceMethod::ACCEPT, *server, ResourceAttributes{ });
144 ASSERT_EQ(ORIGIN_VALUE, server->getAttribute<int>(EXISTING));
147 TEST_F(SetRequestHandlerAcceptanceTest, EverythingAppliedIfMethodIsAccept)
149 requestAttrs[EXISTING] = "";
151 auto replaced = setRequestHandler->applyAcceptanceMethod(
152 RCSSetResponse::AcceptanceMethod::ACCEPT, *server, requestAttrs);
154 ASSERT_EQ(ORIGIN_VALUE, replaced[0].second);
158 TEST_F(SetRequestHandlerAcceptanceTest, NoReplaceIfMethodIsDefaultAndTypeMismatch)
160 requestAttrs[EXISTING] = "";
162 auto replaced = setRequestHandler->applyAcceptanceMethod(
163 RCSSetResponse::AcceptanceMethod::DEFAULT, *server, requestAttrs);
165 ASSERT_TRUE(replaced.empty());
168 TEST_F(SetRequestHandlerAcceptanceTest, NoReplacefMethodIsDefaultAndRequestAttrsHasUnknownKey)
170 constexpr char unknownKey[]{ "???" };
172 requestAttrs[EXISTING] = ORIGIN_VALUE;
173 requestAttrs[unknownKey] = ORIGIN_VALUE;
176 auto replaced = setRequestHandler->applyAcceptanceMethod(
177 RCSSetResponse::AcceptanceMethod::DEFAULT, *server, requestAttrs);
179 ASSERT_TRUE(replaced.empty());