Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / common / primitiveResource / unittests / ResourceAttributesTest.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 <RCSResourceAttributes.h>
22 #include <ResourceAttributesConverter.h>
23 #include <ResourceAttributesUtils.h>
24
25 #include <gtest/gtest.h>
26
27 using namespace testing;
28 using namespace OIC::Service;
29
30 constexpr char KEY[]{ "key" };
31
32 class ResourceAttributesTest: public Test
33 {
34 public:
35     RCSResourceAttributes resourceAttributes;
36 };
37
38 TEST_F(ResourceAttributesTest, InitialSizeIsZero)
39 {
40     ASSERT_EQ(0U, resourceAttributes.size());
41     ASSERT_TRUE(resourceAttributes.empty());
42 }
43
44 TEST_F(ResourceAttributesTest, InsertWithSquareBracket)
45 {
46     resourceAttributes[KEY] = 1;
47
48     ASSERT_TRUE(resourceAttributes[KEY] == 1);
49 }
50
51 TEST_F(ResourceAttributesTest, ValueThrowsIfTypeDoesNotMatch)
52 {
53      resourceAttributes[KEY] = 1;
54     auto& valueRef = resourceAttributes[KEY];
55
56     ASSERT_THROW(valueRef.get< std::string >(), BadGetException);
57 }
58
59 TEST_F(ResourceAttributesTest, GettingWithAtThrowsIfThereIsNoMatchedValue)
60 {
61     ASSERT_THROW(resourceAttributes.at(KEY), InvalidKeyException);
62 }
63
64 TEST_F(ResourceAttributesTest, CopyingValueDoesNotShareState)
65 {
66     const char arbitraryStr[] { "ftryb457" };
67     resourceAttributes[KEY] = 1;
68
69     RCSResourceAttributes::Value copied { resourceAttributes[KEY] };
70     copied = arbitraryStr;
71
72     ASSERT_TRUE(resourceAttributes[KEY] == 1);
73     ASSERT_TRUE(copied == arbitraryStr);
74 }
75
76 TEST_F(ResourceAttributesTest, IsNullWhenAssignmentNullptr)
77 {
78     resourceAttributes[KEY] = nullptr;
79
80     ASSERT_TRUE(resourceAttributes[KEY] == nullptr);
81 }
82
83 TEST_F(ResourceAttributesTest, ValueChangedIfPutWithSameKey)
84 {
85     resourceAttributes[KEY] = "string";
86     resourceAttributes[KEY] = true;
87
88     ASSERT_TRUE(resourceAttributes[KEY] == true);
89 }
90
91 TEST_F(ResourceAttributesTest, ObjectIsEmptyAfterMoved)
92 {
93     resourceAttributes[KEY] = 1;
94
95     RCSResourceAttributes moved{ std::move(resourceAttributes) };
96
97     ASSERT_TRUE(resourceAttributes.empty());
98 }
99
100 TEST_F(ResourceAttributesTest, GettingWithAtThrowsAfterRemoved)
101 {
102     resourceAttributes[KEY] = 1;
103
104     resourceAttributes.erase(KEY);
105
106     ASSERT_THROW(resourceAttributes.at(KEY), InvalidKeyException);
107 }
108
109 TEST_F(ResourceAttributesTest, NoDataErasedIfKeyDoesNotMatch)
110 {
111     ASSERT_FALSE(resourceAttributes.erase(KEY));
112 }
113
114 TEST_F(ResourceAttributesTest, ChangeValueWithAtGetter)
115 {
116     resourceAttributes[KEY] = 1;
117
118     resourceAttributes.at(KEY) = "after";
119
120     ASSERT_TRUE(resourceAttributes[KEY] == "after");
121 }
122
123 TEST_F(ResourceAttributesTest, CanHaveNestedResourceAttributes)
124 {
125     RCSResourceAttributes nested;
126     nested["nested"] = "nested_value";
127     resourceAttributes[KEY] = nested;
128
129     ASSERT_TRUE("nested_value" == resourceAttributes[KEY].get<RCSResourceAttributes>()["nested"]);
130 }
131
132 TEST_F(ResourceAttributesTest, ToStringReturnsStringForValue)
133 {
134     resourceAttributes[KEY] = true;
135
136     ASSERT_EQ("true", resourceAttributes[KEY].toString());
137 }
138
139 TEST_F(ResourceAttributesTest, ToStringReturnsEmptyStringForNullValue)
140 {
141     resourceAttributes[KEY] = nullptr;
142
143     ASSERT_EQ("", resourceAttributes[KEY].toString());
144 }
145
146
147 class ResourceAttributesIteratorTest: public Test
148 {
149 public:
150     RCSResourceAttributes resourceAttributes;
151 };
152
153 TEST_F(ResourceAttributesIteratorTest, BeginEqualsEndWhenEmpty)
154 {
155     ASSERT_TRUE(resourceAttributes.begin() == resourceAttributes.end());
156 }
157
158 TEST_F(ResourceAttributesIteratorTest, CanIteratesWithForeach)
159 {
160     resourceAttributes["first"] = 1;
161     resourceAttributes["second"] = 2;
162
163     int count = 0;
164
165     for (auto& i : resourceAttributes) {
166         i.key();
167         ++count;
168     }
169
170     ASSERT_EQ(2, count);
171 }
172
173 TEST_F(ResourceAttributesIteratorTest, IteratesWithRef)
174 {
175     const char arbitraryStr[] { "ftryb457" };
176     resourceAttributes[KEY] = 1;
177
178     for (auto& i : resourceAttributes) {
179         i.value() = arbitraryStr;
180     }
181
182     ASSERT_TRUE(resourceAttributes[KEY] == arbitraryStr);
183 }
184
185 TEST_F(ResourceAttributesIteratorTest, IteratorIsCopyable)
186 {
187     RCSResourceAttributes::iterator it;
188
189     it = resourceAttributes.begin();
190
191     ASSERT_EQ(it, resourceAttributes.begin());
192 }
193
194 TEST_F(ResourceAttributesIteratorTest, IteratorIndicateNextItemAfterIncreased)
195 {
196     resourceAttributes[KEY] = 1;
197
198     RCSResourceAttributes::iterator it = resourceAttributes.begin();
199
200     it++;
201
202     ASSERT_TRUE(it == resourceAttributes.end());
203 }
204
205 TEST_F(ResourceAttributesIteratorTest, IteratorCanBeConvertedIntoConstIterator)
206 {
207     resourceAttributes[KEY] = 1;
208     RCSResourceAttributes::const_iterator it { resourceAttributes.begin() };
209     it = resourceAttributes.cbegin();
210
211     it++;
212
213     ASSERT_TRUE(it == resourceAttributes.cend());
214 }
215
216 TEST_F(ResourceAttributesIteratorTest, ConstIteratorIsUsedForConst)
217 {
218     resourceAttributes[KEY] = 1;
219     const RCSResourceAttributes& constAttrs = resourceAttributes;
220
221     auto iter = constAttrs.begin();
222
223     ASSERT_TRUE((std::is_same<decltype(iter), RCSResourceAttributes::const_iterator>::value));
224 }
225
226
227 TEST(ResourceAttributesValueTest, MovedValueHasNull)
228 {
229     RCSResourceAttributes::Value one { 1 };
230     RCSResourceAttributes::Value another { std::move(one) };
231
232     ASSERT_EQ(nullptr, one);
233 }
234
235 TEST(ResourceAttributesValueTest, MovedValueWithAssignmentHasNull)
236 {
237     RCSResourceAttributes::Value one { 1 };
238     RCSResourceAttributes::Value another;
239
240     another = std::move(one);
241
242     ASSERT_EQ(nullptr, one);
243 }
244
245 TEST(ResourceAttributesValueTest, SameValuesAreEqual)
246 {
247     RCSResourceAttributes::Value one { 1 };
248     RCSResourceAttributes::Value another { 1 };
249
250     ASSERT_EQ(one, another);
251 }
252
253 TEST(ResourceAttributesValueTest, DifferentValuesAreNotEqual)
254 {
255     RCSResourceAttributes::Value one { 1 };
256     RCSResourceAttributes::Value another { 2 };
257
258     ASSERT_NE(one, another);
259 }
260
261 TEST(ResourceAttributesValueTest, ValuesCanBeSwapped)
262 {
263     constexpr int i { 0 };
264     constexpr char str[]{ "abc" };
265
266     RCSResourceAttributes::Value intValue { i };
267     RCSResourceAttributes::Value strValue { str };
268
269     intValue.swap(strValue);
270
271     ASSERT_EQ(str, intValue);
272     ASSERT_EQ(i, strValue);
273 }
274
275 TEST(ResourceAttributesTypeTest, TypeIdMatchesTypeOfValue)
276 {
277     RCSResourceAttributes::Value intValue { 0 };
278
279     ASSERT_EQ(intValue.getType().getId(), RCSResourceAttributes::TypeId::INT);
280 }
281
282 TEST(ResourceAttributesTypeTest, TypeCanBeConstructedFromValue)
283 {
284     RCSResourceAttributes::Value intValue { 1 };
285
286     RCSResourceAttributes::Type t = RCSResourceAttributes::Type::typeOf(0);
287
288     ASSERT_EQ(intValue.getType(), t);
289 }
290
291 TEST(ResourceAttributesConverterTest, OCRepresentationCanBeConvertedIntoResourceAttributes)
292 {
293     constexpr double value = 9876;
294     OC::OCRepresentation ocRep;
295     ocRep[KEY] = value;
296
297     RCSResourceAttributes resourceAttributes{
298         ResourceAttributesConverter::fromOCRepresentation(ocRep) };
299
300     ASSERT_TRUE(value == resourceAttributes[KEY]);
301 }
302
303
304 TEST(ResourceAttributesConverterTest, NestedOCRepresentationCanBeConvertedIntoResourceAttributes)
305 {
306     std::string nested_value { "nested" };
307     OC::OCRepresentation ocRep;
308     OC::OCRepresentation nested;
309     nested[KEY] = nested_value;
310     ocRep[KEY] = nested;
311
312     RCSResourceAttributes resourceAttributes{
313         ResourceAttributesConverter::fromOCRepresentation(ocRep) };
314
315     ASSERT_TRUE(nested_value == resourceAttributes[KEY].get<RCSResourceAttributes>()[KEY]);
316 }
317
318
319 TEST(ResourceAttributesConverterTest, ResourceAttributesCanBeConvertedIntoOCRepresentation)
320 {
321     double value { 3453453 };
322     RCSResourceAttributes resourceAttributes;
323     resourceAttributes[KEY] = value;
324
325     OC::OCRepresentation ocRep{
326         ResourceAttributesConverter::toOCRepresentation(resourceAttributes) };
327
328     ASSERT_TRUE(value == ocRep[KEY].getValue<double>());
329 }
330
331 TEST(ResourceAttributesConverterTest, NestedResourceAttributesCanBeConvertedIntoOCRepresentation)
332 {
333     std::string nested_value { "nested" };
334     RCSResourceAttributes resourceAttributes;
335     RCSResourceAttributes nested;
336     nested[KEY] = nested_value;
337     resourceAttributes[KEY] = nested;
338
339     OC::OCRepresentation ocRep{
340         ResourceAttributesConverter::toOCRepresentation(resourceAttributes) };
341
342     ASSERT_EQ(nested_value,
343             ocRep[KEY].getValue<OC::OCRepresentation>()[KEY].getValue<std::string>());
344 }
345
346 TEST(ResourceAttributesConverterTest, OCRepresentationNullTypeIsNullptrInResourceAttributes)
347 {
348     OC::OCRepresentation ocRep;
349     ocRep.setNULL(KEY);
350
351     RCSResourceAttributes resourceAttributes{
352         ResourceAttributesConverter::fromOCRepresentation(ocRep) };
353
354     ASSERT_EQ(nullptr, resourceAttributes[KEY]);
355 }
356
357 TEST(ResourceAttributesConverterTest, OCRepresentationHasNullWhenResourceAttributeIsNullptr)
358 {
359     RCSResourceAttributes resourceAttributes;
360     resourceAttributes[KEY] = nullptr;
361
362     OC::OCRepresentation ocRep{
363         ResourceAttributesConverter::toOCRepresentation(resourceAttributes) };
364
365     ASSERT_TRUE(ocRep.isNULL(KEY));
366 }
367
368
369
370 class ResourceAttributesUtilTest: public Test
371 {
372 public:
373     RCSResourceAttributes resourceAttributes;
374
375 protected:
376     void SetUp()
377     {
378         resourceAttributes[KEY] = 1;
379     }
380 };
381
382 TEST_F(ResourceAttributesUtilTest, EmptyAttributesIsAcceptable)
383 {
384     ASSERT_TRUE(acceptableAttributes(resourceAttributes, RCSResourceAttributes()));
385 }
386
387 TEST_F(ResourceAttributesUtilTest, AttributesItselfIsAcceptable)
388 {
389     ASSERT_TRUE(acceptableAttributes(resourceAttributes, resourceAttributes));
390 }
391
392 TEST_F(ResourceAttributesUtilTest, UnknownKeyIsNotAcceptable)
393 {
394     RCSResourceAttributes newAttrs;
395     newAttrs["unknown"] = 1;
396
397     ASSERT_FALSE(acceptableAttributes(resourceAttributes, newAttrs));
398 }
399
400 TEST_F(ResourceAttributesUtilTest, DifferentTypeWithOriginalIsNotAcceptable)
401 {
402     RCSResourceAttributes newAttrs;
403     newAttrs[KEY] = "";
404
405     ASSERT_FALSE(acceptableAttributes(resourceAttributes, newAttrs));
406 }
407
408
409 TEST_F(ResourceAttributesUtilTest, DifferentTypeOfNestedAttributeIsNotAcceptable)
410 {
411     constexpr char KEY_NESTED_ATTR[]{ "nested" };
412     constexpr char KEY_NESTED_VALUE[]{ "nested_value" };
413
414     RCSResourceAttributes nested;
415     nested[KEY_NESTED_VALUE] = -99;
416     resourceAttributes[KEY_NESTED_ATTR] = nested;
417
418
419     RCSResourceAttributes newAttrs;
420     nested[KEY_NESTED_VALUE] = "abc";
421     newAttrs[KEY_NESTED_ATTR] = nested;
422
423     ASSERT_FALSE(acceptableAttributes(resourceAttributes, newAttrs));
424 }
425
426 TEST_F(ResourceAttributesUtilTest, ReplaceWillOverwriteOriginal)
427 {
428     constexpr char NEW_VALUE[]{ "newValue" };
429
430     RCSResourceAttributes newAttrs;
431     newAttrs[KEY] = NEW_VALUE;
432
433     replaceAttributes(resourceAttributes, newAttrs);
434
435     ASSERT_EQ(NEW_VALUE, resourceAttributes[KEY]);
436 }