acb87328ea7ea9ee8007e29022f60d1af53b4640
[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_EQ(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 >(), RCSBadGetException);
57 }
58
59 TEST_F(ResourceAttributesTest, GettingWithAtThrowsIfThereIsNoMatchedValue)
60 {
61     ASSERT_THROW(resourceAttributes.at(KEY), RCSInvalidKeyException);
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_EQ(resourceAttributes[KEY], 1);
73     ASSERT_EQ(copied, arbitraryStr);
74 }
75
76 TEST_F(ResourceAttributesTest, IsNullWhenAssignmentNullptr)
77 {
78     resourceAttributes[KEY] = nullptr;
79
80     ASSERT_EQ(resourceAttributes[KEY], nullptr);
81 }
82
83 TEST_F(ResourceAttributesTest, ValueChangedIfPutWithSameKey)
84 {
85     resourceAttributes[KEY] = "string";
86     resourceAttributes[KEY] = true;
87
88     ASSERT_EQ(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), RCSInvalidKeyException);
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_EQ(resourceAttributes[KEY], "after");
121 }
122
123 TEST_F(ResourceAttributesTest, CanHaveNestedResourceAttributes)
124 {
125     constexpr char nestedKey[]{ "nested" };
126     constexpr char value[]{ "nested_value" };
127
128     RCSResourceAttributes nested;
129     nested[nestedKey] = value;
130     resourceAttributes[KEY] = nested;
131
132     ASSERT_EQ(value, resourceAttributes[KEY].get<RCSResourceAttributes>()[nestedKey]);
133 }
134
135 TEST_F(ResourceAttributesTest, ToStringReturnsStringForValue)
136 {
137     resourceAttributes[KEY] = true;
138
139     ASSERT_EQ("true", resourceAttributes[KEY].toString());
140 }
141
142 TEST_F(ResourceAttributesTest, ToStringReturnsEmptyStringForNullValue)
143 {
144     resourceAttributes[KEY] = nullptr;
145
146     ASSERT_EQ("", resourceAttributes[KEY].toString());
147 }
148
149
150 class ResourceAttributesIteratorTest: public Test
151 {
152 public:
153     RCSResourceAttributes resourceAttributes;
154 };
155
156 TEST_F(ResourceAttributesIteratorTest, BeginEqualsEndWhenEmpty)
157 {
158     ASSERT_TRUE(resourceAttributes.begin() == resourceAttributes.end());
159 }
160
161 TEST_F(ResourceAttributesIteratorTest, CanIteratesWithForeach)
162 {
163     resourceAttributes["first"] = 1;
164     resourceAttributes["second"] = 2;
165
166     int count = 0;
167
168     for (auto& i : resourceAttributes) {
169         i.key();
170         ++count;
171     }
172
173     ASSERT_EQ(2, count);
174 }
175
176 TEST_F(ResourceAttributesIteratorTest, IteratesWithRef)
177 {
178     const char arbitraryStr[] { "ftryb457" };
179     resourceAttributes[KEY] = 1;
180
181     for (auto& i : resourceAttributes) {
182         i.value() = arbitraryStr;
183     }
184
185     ASSERT_EQ(resourceAttributes[KEY], arbitraryStr);
186 }
187
188 TEST_F(ResourceAttributesIteratorTest, IteratorIsCopyable)
189 {
190     RCSResourceAttributes::iterator it;
191
192     it = resourceAttributes.begin();
193
194     ASSERT_TRUE(it == resourceAttributes.begin());
195 }
196
197 TEST_F(ResourceAttributesIteratorTest, IteratorIndicateNextItemAfterIncreased)
198 {
199     resourceAttributes[KEY] = 1;
200
201     RCSResourceAttributes::iterator it = resourceAttributes.begin();
202
203     it++;
204
205     ASSERT_TRUE(it == resourceAttributes.end());
206 }
207
208 TEST_F(ResourceAttributesIteratorTest, IteratorCanBeConvertedIntoConstIterator)
209 {
210     resourceAttributes[KEY] = 1;
211     RCSResourceAttributes::const_iterator it { resourceAttributes.begin() };
212     it = resourceAttributes.cbegin();
213
214     it++;
215
216     ASSERT_TRUE(it == resourceAttributes.cend());
217 }
218
219 TEST_F(ResourceAttributesIteratorTest, ConstIteratorIsUsedForConst)
220 {
221     resourceAttributes[KEY] = 1;
222     const RCSResourceAttributes& constAttrs = resourceAttributes;
223
224     auto iter = constAttrs.begin();
225
226     ASSERT_TRUE((std::is_same<decltype(iter), RCSResourceAttributes::const_iterator>::value));
227 }
228
229
230 TEST(ResourceAttributesValueTest, MovedValueHasNull)
231 {
232     RCSResourceAttributes::Value one { 1 };
233     RCSResourceAttributes::Value another { std::move(one) };
234
235     ASSERT_EQ(nullptr, one);
236 }
237
238 TEST(ResourceAttributesValueTest, MovedValueWithAssignmentHasNull)
239 {
240     RCSResourceAttributes::Value one { 1 };
241     RCSResourceAttributes::Value another;
242
243     another = std::move(one);
244
245     ASSERT_EQ(nullptr, one);
246 }
247
248 TEST(ResourceAttributesValueTest, SameValuesAreEqual)
249 {
250     RCSResourceAttributes::Value one { 1 };
251     RCSResourceAttributes::Value another { 1 };
252
253     ASSERT_EQ(one, another);
254 }
255
256 TEST(ResourceAttributesValueTest, DifferentValuesAreNotEqual)
257 {
258     RCSResourceAttributes::Value one { 1 };
259     RCSResourceAttributes::Value another { 2 };
260
261     ASSERT_NE(one, another);
262 }
263
264 TEST(ResourceAttributesValueTest, ValuesCanBeSwapped)
265 {
266     constexpr int i { 0 };
267     constexpr char str[]{ "abc" };
268
269     RCSResourceAttributes::Value intValue { i };
270     RCSResourceAttributes::Value strValue { str };
271
272     intValue.swap(strValue);
273
274     ASSERT_EQ(str, intValue);
275     ASSERT_EQ(i, strValue);
276 }
277
278 TEST(ResourceAttributesTypeTest, TypeIdMatchesTypeOfValue)
279 {
280     RCSResourceAttributes::Value intValue { 0 };
281
282     ASSERT_EQ(intValue.getType().getId(), RCSResourceAttributes::TypeId::INT);
283 }
284
285 TEST(ResourceAttributesTypeTest, TypeCanBeConstructedFromValue)
286 {
287     RCSResourceAttributes::Value intValue { 1 };
288
289     RCSResourceAttributes::Type t = RCSResourceAttributes::Type::typeOf(0);
290
291     ASSERT_EQ(intValue.getType(), t);
292 }
293
294 TEST(ResourceAttributesTypeTest, DepthOfNonSequceTypeIsZero)
295 {
296     RCSResourceAttributes::Value intValue { 1 };
297
298     RCSResourceAttributes::Type t = intValue.getType();
299
300     ASSERT_EQ(0U, RCSResourceAttributes::Type::getDepth(t));
301 }
302
303 TEST(ResourceAttributesTypeTest, DepthOfSequceTypeIsNumberOfNested)
304 {
305     typedef std::vector< std::vector< std::vector< int > > > NestedVector;
306
307     RCSResourceAttributes::Type t = RCSResourceAttributes::Type::typeOf(NestedVector{ });
308
309     ASSERT_EQ(3U, RCSResourceAttributes::Type::getDepth(t));
310 }
311
312 TEST(ResourceAttributesTypeTest, BaseTypeOfNonSequceTypeIsItself)
313 {
314     RCSResourceAttributes::Value intValue { 1 };
315
316     RCSResourceAttributes::Type t = intValue.getType();
317
318     ASSERT_EQ(RCSResourceAttributes::TypeId::INT, RCSResourceAttributes::Type::getBaseTypeId(t));
319 }
320
321 TEST(ResourceAttributesTypeTest, BaseTypeOfSequceTypeIsMostNestedType)
322 {
323     typedef std::vector< std::vector< std::vector< RCSResourceAttributes > > > NestedVector;
324
325     RCSResourceAttributes::Type t = RCSResourceAttributes::Type::typeOf(NestedVector{ });
326
327     ASSERT_EQ(RCSResourceAttributes::TypeId::ATTRIBUTES,
328             RCSResourceAttributes::Type::getBaseTypeId(t));
329 }
330
331
332 TEST(ResourceAttributesConverterTest, OCRepresentationCanBeConvertedIntoResourceAttributes)
333 {
334     constexpr double value = 9876;
335     OC::OCRepresentation ocRep;
336     ocRep[KEY] = value;
337
338     RCSResourceAttributes resourceAttributes{
339         ResourceAttributesConverter::fromOCRepresentation(ocRep) };
340
341     ASSERT_TRUE(value == resourceAttributes[KEY]);
342 }
343
344
345 TEST(ResourceAttributesConverterTest, NestedOCRepresentationCanBeConvertedIntoResourceAttributes)
346 {
347     std::string nested_value { "nested" };
348     OC::OCRepresentation ocRep;
349     OC::OCRepresentation nested;
350     nested[KEY] = nested_value;
351     ocRep[KEY] = nested;
352
353     RCSResourceAttributes resourceAttributes{
354         ResourceAttributesConverter::fromOCRepresentation(ocRep) };
355
356     ASSERT_EQ(nested_value, resourceAttributes[KEY].get<RCSResourceAttributes>()[KEY]);
357 }
358
359
360 TEST(ResourceAttributesConverterTest, ResourceAttributesCanBeConvertedIntoOCRepresentation)
361 {
362     double value { 3453453 };
363     RCSResourceAttributes resourceAttributes;
364     resourceAttributes[KEY] = value;
365
366     OC::OCRepresentation ocRep{
367         ResourceAttributesConverter::toOCRepresentation(resourceAttributes) };
368
369     ASSERT_EQ(value, ocRep[KEY].getValue<double>());
370 }
371
372 TEST(ResourceAttributesConverterTest, NestedResourceAttributesCanBeConvertedIntoOCRepresentation)
373 {
374     std::string nested_value { "nested" };
375     RCSResourceAttributes resourceAttributes;
376     RCSResourceAttributes nested;
377     nested[KEY] = nested_value;
378     resourceAttributes[KEY] = nested;
379
380     OC::OCRepresentation ocRep{
381         ResourceAttributesConverter::toOCRepresentation(resourceAttributes) };
382
383     ASSERT_EQ(nested_value,
384             ocRep[KEY].getValue<OC::OCRepresentation>()[KEY].getValue<std::string>());
385 }
386
387 TEST(ResourceAttributesConverterTest, OCRepresentationNullTypeIsNullptrInResourceAttributes)
388 {
389     OC::OCRepresentation ocRep;
390     ocRep.setNULL(KEY);
391
392     RCSResourceAttributes resourceAttributes{
393         ResourceAttributesConverter::fromOCRepresentation(ocRep) };
394
395     ASSERT_EQ(nullptr, resourceAttributes[KEY]);
396 }
397
398 TEST(ResourceAttributesConverterTest, OCRepresentationHasNullWhenResourceAttributeIsNullptr)
399 {
400     RCSResourceAttributes resourceAttributes;
401     resourceAttributes[KEY] = nullptr;
402
403     OC::OCRepresentation ocRep{
404         ResourceAttributesConverter::toOCRepresentation(resourceAttributes) };
405
406     ASSERT_TRUE(ocRep.isNULL(KEY));
407 }
408
409 TEST(ResourceAttributesConverterTest, ResourceAttributesWithSequenceTypeCanBeConverted)
410 {
411     typedef std::vector< std::vector< std::vector< int > > > NestedVector;
412     constexpr int value { 3453453 };
413
414     RCSResourceAttributes resourceAttributes;
415     NestedVector seq(10);
416     seq[1].resize(10, std::vector< int >(10));
417     seq[1][2][3] = value;
418     resourceAttributes[KEY] = seq;
419
420     NestedVector ocSeq = ResourceAttributesConverter::toOCRepresentation(resourceAttributes)[KEY];
421
422     ASSERT_EQ(ocSeq[1][2][3], value);
423 }
424
425 TEST(ResourceAttributesConverterTest, OCRepresentationWithSequenceTypeCanBeConverted)
426 {
427     typedef std::vector< std::vector< std::vector< std::string > > > NestedVector;
428     constexpr char value[]{ "some_string" };
429
430     OC::OCRepresentation ocRep;
431     NestedVector seq(10);
432     seq[1].resize(10, std::vector< std::string >(10));
433     seq[1][2][3] = value;
434     ocRep[KEY] = seq;
435
436     RCSResourceAttributes resourceAttributes{
437         ResourceAttributesConverter::fromOCRepresentation(ocRep) };
438
439     ASSERT_EQ(seq, resourceAttributes[KEY]);
440 }
441
442
443 class ResourceAttributesUtilTest: public Test
444 {
445 public:
446     RCSResourceAttributes resourceAttributes;
447
448 protected:
449     void SetUp()
450     {
451         resourceAttributes[KEY] = 1;
452     }
453 };
454
455 TEST_F(ResourceAttributesUtilTest, EmptyAttributesIsAcceptable)
456 {
457     ASSERT_TRUE(acceptableAttributes(resourceAttributes, RCSResourceAttributes()));
458 }
459
460 TEST_F(ResourceAttributesUtilTest, AttributesItselfIsAcceptable)
461 {
462     ASSERT_TRUE(acceptableAttributes(resourceAttributes, resourceAttributes));
463 }
464
465 TEST_F(ResourceAttributesUtilTest, UnknownKeyIsNotAcceptable)
466 {
467     RCSResourceAttributes newAttrs;
468     newAttrs["unknown"] = 1;
469
470     ASSERT_FALSE(acceptableAttributes(resourceAttributes, newAttrs));
471 }
472
473 TEST_F(ResourceAttributesUtilTest, DifferentTypeWithOriginalIsNotAcceptable)
474 {
475     RCSResourceAttributes newAttrs;
476     newAttrs[KEY] = "";
477
478     ASSERT_FALSE(acceptableAttributes(resourceAttributes, newAttrs));
479 }
480
481
482 TEST_F(ResourceAttributesUtilTest, DifferentTypeOfNestedAttributeIsNotAcceptable)
483 {
484     constexpr char KEY_NESTED_ATTR[]{ "nested" };
485     constexpr char KEY_NESTED_VALUE[]{ "nested_value" };
486
487     RCSResourceAttributes nested;
488     nested[KEY_NESTED_VALUE] = -99;
489     resourceAttributes[KEY_NESTED_ATTR] = nested;
490
491
492     RCSResourceAttributes newAttrs;
493     nested[KEY_NESTED_VALUE] = "abc";
494     newAttrs[KEY_NESTED_ATTR] = nested;
495
496     ASSERT_FALSE(acceptableAttributes(resourceAttributes, newAttrs));
497 }
498
499 TEST_F(ResourceAttributesUtilTest, ReplaceWillOverwriteOriginal)
500 {
501     constexpr char NEW_VALUE[]{ "newValue" };
502
503     RCSResourceAttributes newAttrs;
504     newAttrs[KEY] = NEW_VALUE;
505
506     replaceAttributes(resourceAttributes, newAttrs);
507
508     ASSERT_EQ(NEW_VALUE, resourceAttributes[KEY]);
509 }