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 <RCSResourceAttributes.h>
22 #include <ResourceAttributesConverter.h>
23 #include <ResourceAttributesUtils.h>
25 #include <gtest/gtest.h>
27 using namespace testing;
28 using namespace OIC::Service;
30 constexpr char KEY[]{ "key" };
32 class ResourceAttributesTest: public Test
35 RCSResourceAttributes resourceAttributes;
38 TEST_F(ResourceAttributesTest, InitialSizeIsZero)
40 ASSERT_EQ(0U, resourceAttributes.size());
41 ASSERT_TRUE(resourceAttributes.empty());
44 TEST_F(ResourceAttributesTest, InsertWithSquareBracket)
46 resourceAttributes[KEY] = 1;
48 ASSERT_EQ(resourceAttributes[KEY], 1);
51 TEST_F(ResourceAttributesTest, ValueThrowsIfTypeDoesNotMatch)
53 resourceAttributes[KEY] = 1;
54 auto& valueRef = resourceAttributes[KEY];
56 ASSERT_THROW(valueRef.get< std::string >(), RCSBadGetException);
59 TEST_F(ResourceAttributesTest, GettingWithAtThrowsIfThereIsNoMatchedValue)
61 ASSERT_THROW(resourceAttributes.at(KEY), RCSInvalidKeyException);
64 TEST_F(ResourceAttributesTest, CopyingValueDoesNotShareState)
66 const char arbitraryStr[] { "ftryb457" };
67 resourceAttributes[KEY] = 1;
69 RCSResourceAttributes::Value copied { resourceAttributes[KEY] };
70 copied = arbitraryStr;
72 ASSERT_EQ(resourceAttributes[KEY], 1);
73 ASSERT_EQ(copied, arbitraryStr);
76 TEST_F(ResourceAttributesTest, IsNullWhenAssignmentNullptr)
78 resourceAttributes[KEY] = nullptr;
80 //ASSERT_EQ(resourceAttributes[KEY], nullptr);
83 TEST_F(ResourceAttributesTest, ValueChangedIfPutWithSameKey)
85 resourceAttributes[KEY] = "string";
86 resourceAttributes[KEY] = true;
88 ASSERT_EQ(resourceAttributes[KEY], true);
91 TEST_F(ResourceAttributesTest, ObjectIsEmptyAfterMoved)
93 resourceAttributes[KEY] = 1;
95 RCSResourceAttributes moved{ std::move(resourceAttributes) };
97 ASSERT_TRUE(resourceAttributes.empty());
100 TEST_F(ResourceAttributesTest, GettingWithAtThrowsAfterRemoved)
102 resourceAttributes[KEY] = 1;
104 resourceAttributes.erase(KEY);
106 ASSERT_THROW(resourceAttributes.at(KEY), RCSInvalidKeyException);
109 TEST_F(ResourceAttributesTest, NoDataErasedIfKeyDoesNotMatch)
111 ASSERT_FALSE(resourceAttributes.erase(KEY));
114 TEST_F(ResourceAttributesTest, ChangeValueWithAtGetter)
116 resourceAttributes[KEY] = 1;
118 resourceAttributes.at(KEY) = "after";
120 ASSERT_EQ(resourceAttributes[KEY], "after");
123 TEST_F(ResourceAttributesTest, CanHaveNestedResourceAttributes)
125 constexpr char nestedKey[]{ "nested" };
126 constexpr char value[]{ "nested_value" };
128 RCSResourceAttributes nested;
129 nested[nestedKey] = value;
130 resourceAttributes[KEY] = nested;
132 ASSERT_EQ(value, resourceAttributes[KEY].get<RCSResourceAttributes>()[nestedKey]);
135 TEST_F(ResourceAttributesTest, ToStringReturnsStringForValue)
137 resourceAttributes[KEY] = true;
139 ASSERT_EQ("true", resourceAttributes[KEY].toString());
142 TEST_F(ResourceAttributesTest, ToStringReturnsEmptyStringForNullValue)
144 resourceAttributes[KEY] = nullptr;
146 ASSERT_EQ("", resourceAttributes[KEY].toString());
150 class ResourceAttributesIteratorTest: public Test
153 RCSResourceAttributes resourceAttributes;
156 TEST_F(ResourceAttributesIteratorTest, BeginEqualsEndWhenEmpty)
158 ASSERT_TRUE(resourceAttributes.begin() == resourceAttributes.end());
161 TEST_F(ResourceAttributesIteratorTest, CanIteratesWithForeach)
163 resourceAttributes["first"] = 1;
164 resourceAttributes["second"] = 2;
168 for (auto& i : resourceAttributes)
177 TEST_F(ResourceAttributesIteratorTest, IteratesWithRef)
179 const char arbitraryStr[] { "ftryb457" };
180 resourceAttributes[KEY] = 1;
182 for (auto& i : resourceAttributes)
184 i.value() = arbitraryStr;
187 ASSERT_EQ(resourceAttributes[KEY], arbitraryStr);
190 TEST_F(ResourceAttributesIteratorTest, IteratorIsCopyable)
192 RCSResourceAttributes::iterator it;
194 it = resourceAttributes.begin();
196 ASSERT_TRUE(it == resourceAttributes.begin());
199 TEST_F(ResourceAttributesIteratorTest, IteratorIndicateNextItemAfterIncreased)
201 resourceAttributes[KEY] = 1;
203 RCSResourceAttributes::iterator it = resourceAttributes.begin();
207 ASSERT_TRUE(it == resourceAttributes.end());
210 TEST_F(ResourceAttributesIteratorTest, IteratorCanBeConvertedIntoConstIterator)
212 resourceAttributes[KEY] = 1;
213 RCSResourceAttributes::const_iterator it { resourceAttributes.begin() };
214 it = resourceAttributes.cbegin();
218 ASSERT_TRUE(it == resourceAttributes.cend());
221 TEST_F(ResourceAttributesIteratorTest, ConstIteratorIsUsedForConst)
223 resourceAttributes[KEY] = 1;
224 const RCSResourceAttributes& constAttrs = resourceAttributes;
226 auto iter = constAttrs.begin();
228 ASSERT_TRUE((std::is_same<decltype(iter), RCSResourceAttributes::const_iterator>::value));
232 TEST(ResourceAttributesValueTest, MovedValueHasNull)
234 RCSResourceAttributes::Value one { 1 };
235 RCSResourceAttributes::Value another { std::move(one) };
237 //ASSERT_EQ(nullptr, one);
240 TEST(ResourceAttributesValueTest, MovedValueWithAssignmentHasNull)
242 RCSResourceAttributes::Value one { 1 };
243 RCSResourceAttributes::Value another;
245 another = std::move(one);
247 //ASSERT_EQ(nullptr, one);
250 TEST(ResourceAttributesValueTest, SameValuesAreEqual)
252 RCSResourceAttributes::Value one { 1 };
253 RCSResourceAttributes::Value another { 1 };
255 ASSERT_EQ(one, another);
258 TEST(ResourceAttributesValueTest, DifferentValuesAreNotEqual)
260 RCSResourceAttributes::Value one { 1 };
261 RCSResourceAttributes::Value another { 2 };
263 ASSERT_NE(one, another);
266 TEST(ResourceAttributesValueTest, ValuesCanBeSwapped)
268 constexpr int i { 0 };
269 constexpr char str[]{ "abc" };
271 RCSResourceAttributes::Value intValue { i };
272 RCSResourceAttributes::Value strValue { str };
274 intValue.swap(strValue);
276 ASSERT_EQ(str, intValue);
277 ASSERT_EQ(i, strValue);
280 TEST(ResourceAttributesTypeTest, TypeIdMatchesTypeOfValue)
282 RCSResourceAttributes::Value intValue { 0 };
284 ASSERT_EQ(intValue.getType().getId(), RCSResourceAttributes::TypeId::INT);
287 TEST(ResourceAttributesTypeTest, TypeCanBeConstructedFromValue)
289 RCSResourceAttributes::Value intValue { 1 };
291 RCSResourceAttributes::Type t = RCSResourceAttributes::Type::typeOf(0);
293 ASSERT_EQ(intValue.getType(), t);
296 TEST(ResourceAttributesTypeTest, DepthOfNonSequceTypeIsZero)
298 RCSResourceAttributes::Value intValue { 1 };
300 RCSResourceAttributes::Type t = intValue.getType();
302 ASSERT_EQ(0U, RCSResourceAttributes::Type::getDepth(t));
305 TEST(ResourceAttributesTypeTest, DepthOfSequceTypeIsNumberOfNested)
307 typedef std::vector< std::vector< std::vector< int > > > NestedVector;
309 RCSResourceAttributes::Type t = RCSResourceAttributes::Type::typeOf(NestedVector{ });
311 ASSERT_EQ(3U, RCSResourceAttributes::Type::getDepth(t));
314 TEST(ResourceAttributesTypeTest, BaseTypeOfNonSequceTypeIsItself)
316 RCSResourceAttributes::Value intValue { 1 };
318 RCSResourceAttributes::Type t = intValue.getType();
320 ASSERT_EQ(RCSResourceAttributes::TypeId::INT, RCSResourceAttributes::Type::getBaseTypeId(t));
323 TEST(ResourceAttributesTypeTest, BaseTypeOfSequceTypeIsMostNestedType)
325 typedef std::vector< std::vector< std::vector< RCSResourceAttributes > > > NestedVector;
327 RCSResourceAttributes::Type t = RCSResourceAttributes::Type::typeOf(NestedVector{ });
329 ASSERT_EQ(RCSResourceAttributes::TypeId::ATTRIBUTES,
330 RCSResourceAttributes::Type::getBaseTypeId(t));
334 TEST(ResourceAttributesConverterTest, OCRepresentationCanBeConvertedIntoResourceAttributes)
336 constexpr double value = 9876;
337 OC::OCRepresentation ocRep;
340 RCSResourceAttributes resourceAttributes{
341 ResourceAttributesConverter::fromOCRepresentation(ocRep) };
343 ASSERT_TRUE(value == resourceAttributes[KEY]);
346 TEST(ResourceAttributesConverterTest, OCRepresentationCanBeConvertedIntoResourceAttributesTypeBinary)
348 static uint8_t binval[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8,
349 0x9, 0x0, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF};
351 OCByteString value {binval, sizeof(binval)};
352 OC::OCRepresentation ocRep;
355 RCSResourceAttributes resourceAttributes{
356 ResourceAttributesConverter::fromOCRepresentation(ocRep) };
358 auto rcsValue = resourceAttributes[KEY].get<RCSByteString>();
359 for (size_t i = 0; i < rcsValue.size(); ++i)
361 ASSERT_EQ(binval[i], rcsValue[i]);
365 TEST(ResourceAttributesConverterTest, ResourceAttributesCanBeConvertedIntoOCRepresentationTypeBinary)
367 static RCSByteString::DataType binval {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8,
368 0x9, 0x0, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF};
369 RCSResourceAttributes resourceAttributes;
370 RCSByteString value {binval};
371 resourceAttributes[KEY] = value;
373 OC::OCRepresentation ocRep{
374 ResourceAttributesConverter::toOCRepresentation(resourceAttributes) };
376 auto rcsValue = resourceAttributes[KEY].get<RCSByteString>();
377 auto ocValue = ocRep[KEY].getValue<OCByteString>();
379 ASSERT_EQ(rcsValue.size(), ocValue.len);
380 ASSERT_EQ(rcsValue.size(), binval.size());
381 ASSERT_EQ(binval.size(), ocValue.len);
383 for (size_t i = 0; i < rcsValue.size(); ++i)
385 ASSERT_EQ(ocValue.bytes[i], rcsValue[i]);
389 TEST(ResourceAttributesConverterTest, NestedOCRepresentationCanBeConvertedIntoResourceAttributes)
391 std::string nested_value { "nested" };
392 OC::OCRepresentation ocRep;
393 OC::OCRepresentation nested;
394 nested[KEY] = nested_value;
397 RCSResourceAttributes resourceAttributes{
398 ResourceAttributesConverter::fromOCRepresentation(ocRep) };
400 ASSERT_EQ(nested_value, resourceAttributes[KEY].get<RCSResourceAttributes>()[KEY]);
404 TEST(ResourceAttributesConverterTest, ResourceAttributesCanBeConvertedIntoOCRepresentation)
406 double value { 3453453 };
407 RCSResourceAttributes resourceAttributes;
408 resourceAttributes[KEY] = value;
410 OC::OCRepresentation ocRep{
411 ResourceAttributesConverter::toOCRepresentation(resourceAttributes) };
413 ASSERT_EQ(value, ocRep[KEY].getValue<double>());
416 TEST(ResourceAttributesConverterTest, NestedResourceAttributesCanBeConvertedIntoOCRepresentation)
418 std::string nested_value { "nested" };
419 RCSResourceAttributes resourceAttributes;
420 RCSResourceAttributes nested;
421 nested[KEY] = nested_value;
422 resourceAttributes[KEY] = nested;
424 OC::OCRepresentation ocRep{
425 ResourceAttributesConverter::toOCRepresentation(resourceAttributes) };
427 ASSERT_EQ(nested_value,
428 ocRep[KEY].getValue<OC::OCRepresentation>()[KEY].getValue<std::string>());
431 TEST(ResourceAttributesConverterTest, OCRepresentationNullTypeIsNullptrInResourceAttributes)
433 OC::OCRepresentation ocRep;
436 RCSResourceAttributes resourceAttributes{
437 ResourceAttributesConverter::fromOCRepresentation(ocRep) };
439 //ASSERT_EQ(nullptr, resourceAttributes[KEY]);
442 TEST(ResourceAttributesConverterTest, OCRepresentationHasNullWhenResourceAttributeIsNullptr)
444 RCSResourceAttributes resourceAttributes;
445 resourceAttributes[KEY] = nullptr;
447 OC::OCRepresentation ocRep{
448 ResourceAttributesConverter::toOCRepresentation(resourceAttributes) };
450 ASSERT_TRUE(ocRep.isNULL(KEY));
453 TEST(ResourceAttributesConverterTest, ResourceAttributesWithSequenceTypeCanBeConverted)
455 typedef std::vector< std::vector< std::vector< int > > > NestedVector;
456 constexpr int value { 3453453 };
458 RCSResourceAttributes resourceAttributes;
459 NestedVector seq(10);
460 seq[1].resize(10, std::vector< int >(10));
461 seq[1][2][3] = value;
462 resourceAttributes[KEY] = seq;
464 NestedVector ocSeq = ResourceAttributesConverter::toOCRepresentation(resourceAttributes)[KEY];
466 ASSERT_EQ(ocSeq[1][2][3], value);
469 TEST(ResourceAttributesConverterTest, OCRepresentationWithSequenceTypeCanBeConverted)
471 typedef std::vector< std::vector< std::vector< std::string > > > NestedVector;
472 constexpr char value[]{ "some_string" };
474 OC::OCRepresentation ocRep;
475 NestedVector seq(10);
476 seq[1].resize(10, std::vector< std::string >(10));
477 seq[1][2][3] = value;
480 RCSResourceAttributes resourceAttributes{
481 ResourceAttributesConverter::fromOCRepresentation(ocRep) };
483 ASSERT_EQ(seq, resourceAttributes[KEY]);
487 class ResourceAttributesUtilTest: public Test
490 RCSResourceAttributes resourceAttributes;
495 resourceAttributes[KEY] = 1;
499 TEST_F(ResourceAttributesUtilTest, EmptyAttributesIsAcceptable)
501 ASSERT_TRUE(acceptableAttributes(resourceAttributes, RCSResourceAttributes()));
504 TEST_F(ResourceAttributesUtilTest, AttributesItselfIsAcceptable)
506 ASSERT_TRUE(acceptableAttributes(resourceAttributes, resourceAttributes));
509 TEST_F(ResourceAttributesUtilTest, UnknownKeyIsNotAcceptable)
511 RCSResourceAttributes newAttrs;
512 newAttrs["unknown"] = 1;
514 ASSERT_FALSE(acceptableAttributes(resourceAttributes, newAttrs));
517 TEST_F(ResourceAttributesUtilTest, DifferentTypeWithOriginalIsNotAcceptable)
519 RCSResourceAttributes newAttrs;
522 ASSERT_FALSE(acceptableAttributes(resourceAttributes, newAttrs));
526 TEST_F(ResourceAttributesUtilTest, DifferentTypeOfNestedAttributeIsNotAcceptable)
528 constexpr char KEY_NESTED_ATTR[]{ "nested" };
529 constexpr char KEY_NESTED_VALUE[]{ "nested_value" };
531 RCSResourceAttributes nested;
532 nested[KEY_NESTED_VALUE] = -99;
533 resourceAttributes[KEY_NESTED_ATTR] = nested;
536 RCSResourceAttributes newAttrs;
537 nested[KEY_NESTED_VALUE] = "abc";
538 newAttrs[KEY_NESTED_ATTR] = nested;
540 ASSERT_FALSE(acceptableAttributes(resourceAttributes, newAttrs));
543 TEST_F(ResourceAttributesUtilTest, ReplaceWillOverwriteOriginal)
545 constexpr char NEW_VALUE[]{ "newValue" };
547 RCSResourceAttributes newAttrs;
548 newAttrs[KEY] = NEW_VALUE;
550 replaceAttributes(resourceAttributes, newAttrs);
552 ASSERT_EQ(NEW_VALUE, resourceAttributes[KEY]);