Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / serverBuilder / unittests / RCSResponseTest.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 <RCSResponse.h>
24 #include <RCSResourceObject.h>
25
26 #include <RequestHandler.h>
27 #include <ResourceAttributesConverter.h>
28
29 #include <OCPlatform.h>
30
31 using namespace std;
32
33 using namespace OIC::Service;
34 using namespace OC;
35
36 typedef OCStackResult (*registerResourceSig)(OCResourceHandle&,
37                        string&,
38                        const string&,
39                        const string&,
40                        EntityHandler,
41                        uint8_t );
42
43 static constexpr char KEY[] = "key";
44
45
46 void EXPECT_RESPONSE(shared_ptr< OCResourceResponse > ocResponse, int errorCode,
47         const RCSResourceAttributes& attrs)
48 {
49     EXPECT_EQ(ocResponse->getErrorCode(), errorCode);
50     EXPECT_EQ(ResourceAttributesConverter::fromOCRepresentation(
51                     ocResponse->getResourceRepresentation()), attrs);
52 }
53
54
55 class RCSResponseTest: public TestWithMock
56 {
57 public:
58     template< typename T >
59     shared_ptr< OCResourceResponse > buildResponse(const T& response)
60     {
61         RCSResourceObject::Ptr server =
62                 RCSResourceObject::Builder("a/test", "", "").build();
63
64         return response.getHandler()->buildResponse(*server);
65     }
66
67 protected:
68     void SetUp()
69     {
70         TestWithMock::SetUp();
71
72         mocks.OnCallFuncOverload(static_cast< registerResourceSig >(OCPlatform::registerResource))
73                 .Return(OC_STACK_OK);
74
75         mocks.OnCallFunc(OCPlatform::unregisterResource).Return(OC_STACK_OK);
76     }
77 };
78
79 TEST_F(RCSResponseTest, GetDefaultActionHasEmptyAttrs)
80 {
81     EXPECT_RESPONSE(buildResponse(RCSGetResponse::defaultAction()),
82             RequestHandler::DEFAULT_ERROR_CODE, RCSResourceAttributes());
83 }
84
85 TEST_F(RCSResponseTest, GetResponseHasResultsPassedCodes)
86 {
87     constexpr int errorCode{ -10 };
88
89     EXPECT_RESPONSE(buildResponse(RCSGetResponse::create(errorCode)),
90             errorCode, RCSResourceAttributes());
91 }
92
93 TEST_F(RCSResponseTest, GetResponseHasAttrsAndResultsPassedCodes)
94 {
95     constexpr int errorCode{ -10 };
96
97     RCSResourceAttributes attrs;
98     attrs[KEY] = 100;
99
100     EXPECT_RESPONSE(buildResponse(RCSGetResponse::create(attrs, errorCode)), errorCode, attrs);
101 }
102
103 TEST_F(RCSResponseTest, GetResponseCanMoveAttrs)
104 {
105     constexpr int errorCode{ -10 };
106
107     RCSResourceAttributes attrs;
108     attrs[KEY] = 100;
109
110     RCSResourceAttributes attrsClone;
111     attrsClone[KEY] = 100;
112
113     EXPECT_RESPONSE(
114             buildResponse(RCSGetResponse::create(std::move(attrs), errorCode)),
115             errorCode, attrsClone);
116
117     EXPECT_TRUE(attrs.empty());
118 }
119
120 TEST_F(RCSResponseTest, SetDefaultActionHasEmptyAttrs)
121 {
122     EXPECT_RESPONSE(buildResponse(RCSSetResponse::defaultAction()),
123             RequestHandler::DEFAULT_ERROR_CODE, RCSResourceAttributes());
124 }
125
126 TEST_F(RCSResponseTest, SetResponseHasResultsPassedCodes)
127 {
128     constexpr int errorCode{ -10 };
129
130     EXPECT_RESPONSE(buildResponse(RCSSetResponse::create(errorCode)),
131             errorCode, RCSResourceAttributes());
132 }
133
134 TEST_F(RCSResponseTest, SetResponseHasAttrsAndResultsPassedCodes)
135 {
136     constexpr int errorCode{ -10 };
137
138     RCSResourceAttributes attrs;
139     attrs[KEY] = 100;
140
141     EXPECT_RESPONSE(buildResponse(RCSSetResponse::create(attrs, errorCode)),
142             errorCode, attrs);
143 }
144
145 TEST_F(RCSResponseTest, SetResponseCanMoveAttrs)
146 {
147     constexpr int errorCode{ -10 };
148
149     RCSResourceAttributes attrs;
150     attrs[KEY] = 100;
151
152     RCSResourceAttributes attrsClone;
153     attrsClone[KEY] = 100;
154
155     EXPECT_RESPONSE(buildResponse(RCSSetResponse::create(std::move(attrs), errorCode)),
156             errorCode, attrsClone);
157
158     EXPECT_TRUE(attrs.empty());
159 }
160
161
162 TEST_F(RCSResponseTest, DefaultSetResponseHasDefaultMethod)
163 {
164     EXPECT_EQ(RCSSetResponse::AcceptanceMethod::DEFAULT,
165             RCSSetResponse::defaultAction().getAcceptanceMethod());
166 }
167
168 TEST_F(RCSResponseTest, AcceptSetResponseHasAcceptMethod)
169 {
170     EXPECT_EQ(RCSSetResponse::AcceptanceMethod::ACCEPT,
171             RCSSetResponse::accept().getAcceptanceMethod());
172 }
173
174 TEST_F(RCSResponseTest, IgnoreSetResponseHasIgnoreMethod)
175 {
176     EXPECT_EQ(RCSSetResponse::AcceptanceMethod::IGNORE,
177             RCSSetResponse::ignore().getAcceptanceMethod());
178 }
179
180 TEST_F(RCSResponseTest, SetResponseHasMethodSetBySetter)
181 {
182     RCSSetResponse::AcceptanceMethod method = RCSSetResponse::AcceptanceMethod::ACCEPT;
183     RCSSetResponse response =
184             RCSSetResponse::defaultAction().setAcceptanceMethod(method);
185
186     EXPECT_EQ(method, response.getAcceptanceMethod());
187 }