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]);
347 TEST(ResourceAttributesConverterTest, NestedOCRepresentationCanBeConvertedIntoResourceAttributes)
349 std::string nested_value { "nested" };
350 OC::OCRepresentation ocRep;
351 OC::OCRepresentation nested;
352 nested[KEY] = nested_value;
355 RCSResourceAttributes resourceAttributes{
356 ResourceAttributesConverter::fromOCRepresentation(ocRep) };
358 ASSERT_EQ(nested_value, resourceAttributes[KEY].get<RCSResourceAttributes>()[KEY]);
362 TEST(ResourceAttributesConverterTest, ResourceAttributesCanBeConvertedIntoOCRepresentation)
364 double value { 3453453 };
365 RCSResourceAttributes resourceAttributes;
366 resourceAttributes[KEY] = value;
368 OC::OCRepresentation ocRep{
369 ResourceAttributesConverter::toOCRepresentation(resourceAttributes) };
371 ASSERT_EQ(value, ocRep[KEY].getValue<double>());
374 TEST(ResourceAttributesConverterTest, NestedResourceAttributesCanBeConvertedIntoOCRepresentation)
376 std::string nested_value { "nested" };
377 RCSResourceAttributes resourceAttributes;
378 RCSResourceAttributes nested;
379 nested[KEY] = nested_value;
380 resourceAttributes[KEY] = nested;
382 OC::OCRepresentation ocRep{
383 ResourceAttributesConverter::toOCRepresentation(resourceAttributes) };
385 ASSERT_EQ(nested_value,
386 ocRep[KEY].getValue<OC::OCRepresentation>()[KEY].getValue<std::string>());
389 TEST(ResourceAttributesConverterTest, OCRepresentationNullTypeIsNullptrInResourceAttributes)
391 OC::OCRepresentation ocRep;
394 RCSResourceAttributes resourceAttributes{
395 ResourceAttributesConverter::fromOCRepresentation(ocRep) };
397 //ASSERT_EQ(nullptr, resourceAttributes[KEY]);
400 TEST(ResourceAttributesConverterTest, OCRepresentationHasNullWhenResourceAttributeIsNullptr)
402 RCSResourceAttributes resourceAttributes;
403 resourceAttributes[KEY] = nullptr;
405 OC::OCRepresentation ocRep{
406 ResourceAttributesConverter::toOCRepresentation(resourceAttributes) };
408 ASSERT_TRUE(ocRep.isNULL(KEY));
411 TEST(ResourceAttributesConverterTest, ResourceAttributesWithSequenceTypeCanBeConverted)
413 typedef std::vector< std::vector< std::vector< int > > > NestedVector;
414 constexpr int value { 3453453 };
416 RCSResourceAttributes resourceAttributes;
417 NestedVector seq(10);
418 seq[1].resize(10, std::vector< int >(10));
419 seq[1][2][3] = value;
420 resourceAttributes[KEY] = seq;
422 NestedVector ocSeq = ResourceAttributesConverter::toOCRepresentation(resourceAttributes)[KEY];
424 ASSERT_EQ(ocSeq[1][2][3], value);
427 TEST(ResourceAttributesConverterTest, OCRepresentationWithSequenceTypeCanBeConverted)
429 typedef std::vector< std::vector< std::vector< std::string > > > NestedVector;
430 constexpr char value[]{ "some_string" };
432 OC::OCRepresentation ocRep;
433 NestedVector seq(10);
434 seq[1].resize(10, std::vector< std::string >(10));
435 seq[1][2][3] = value;
438 RCSResourceAttributes resourceAttributes{
439 ResourceAttributesConverter::fromOCRepresentation(ocRep) };
441 ASSERT_EQ(seq, resourceAttributes[KEY]);
445 class ResourceAttributesUtilTest: public Test
448 RCSResourceAttributes resourceAttributes;
453 resourceAttributes[KEY] = 1;
457 TEST_F(ResourceAttributesUtilTest, EmptyAttributesIsAcceptable)
459 ASSERT_TRUE(acceptableAttributes(resourceAttributes, RCSResourceAttributes()));
462 TEST_F(ResourceAttributesUtilTest, AttributesItselfIsAcceptable)
464 ASSERT_TRUE(acceptableAttributes(resourceAttributes, resourceAttributes));
467 TEST_F(ResourceAttributesUtilTest, UnknownKeyIsNotAcceptable)
469 RCSResourceAttributes newAttrs;
470 newAttrs["unknown"] = 1;
472 ASSERT_FALSE(acceptableAttributes(resourceAttributes, newAttrs));
475 TEST_F(ResourceAttributesUtilTest, DifferentTypeWithOriginalIsNotAcceptable)
477 RCSResourceAttributes newAttrs;
480 ASSERT_FALSE(acceptableAttributes(resourceAttributes, newAttrs));
484 TEST_F(ResourceAttributesUtilTest, DifferentTypeOfNestedAttributeIsNotAcceptable)
486 constexpr char KEY_NESTED_ATTR[]{ "nested" };
487 constexpr char KEY_NESTED_VALUE[]{ "nested_value" };
489 RCSResourceAttributes nested;
490 nested[KEY_NESTED_VALUE] = -99;
491 resourceAttributes[KEY_NESTED_ATTR] = nested;
494 RCSResourceAttributes newAttrs;
495 nested[KEY_NESTED_VALUE] = "abc";
496 newAttrs[KEY_NESTED_ATTR] = nested;
498 ASSERT_FALSE(acceptableAttributes(resourceAttributes, newAttrs));
501 TEST_F(ResourceAttributesUtilTest, ReplaceWillOverwriteOriginal)
503 constexpr char NEW_VALUE[]{ "newValue" };
505 RCSResourceAttributes newAttrs;
506 newAttrs[KEY] = NEW_VALUE;
508 replaceAttributes(resourceAttributes, newAttrs);
510 ASSERT_EQ(NEW_VALUE, resourceAttributes[KEY]);