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) {
176 TEST_F(ResourceAttributesIteratorTest, IteratesWithRef)
178 const char arbitraryStr[] { "ftryb457" };
179 resourceAttributes[KEY] = 1;
181 for (auto& i : resourceAttributes) {
182 i.value() = arbitraryStr;
185 ASSERT_EQ(resourceAttributes[KEY], arbitraryStr);
188 TEST_F(ResourceAttributesIteratorTest, IteratorIsCopyable)
190 RCSResourceAttributes::iterator it;
192 it = resourceAttributes.begin();
194 ASSERT_TRUE(it == resourceAttributes.begin());
197 TEST_F(ResourceAttributesIteratorTest, IteratorIndicateNextItemAfterIncreased)
199 resourceAttributes[KEY] = 1;
201 RCSResourceAttributes::iterator it = resourceAttributes.begin();
205 ASSERT_TRUE(it == resourceAttributes.end());
208 TEST_F(ResourceAttributesIteratorTest, IteratorCanBeConvertedIntoConstIterator)
210 resourceAttributes[KEY] = 1;
211 RCSResourceAttributes::const_iterator it { resourceAttributes.begin() };
212 it = resourceAttributes.cbegin();
216 ASSERT_TRUE(it == resourceAttributes.cend());
219 TEST_F(ResourceAttributesIteratorTest, ConstIteratorIsUsedForConst)
221 resourceAttributes[KEY] = 1;
222 const RCSResourceAttributes& constAttrs = resourceAttributes;
224 auto iter = constAttrs.begin();
226 ASSERT_TRUE((std::is_same<decltype(iter), RCSResourceAttributes::const_iterator>::value));
230 TEST(ResourceAttributesValueTest, MovedValueHasNull)
232 RCSResourceAttributes::Value one { 1 };
233 RCSResourceAttributes::Value another { std::move(one) };
235 ASSERT_EQ(nullptr, one);
238 TEST(ResourceAttributesValueTest, MovedValueWithAssignmentHasNull)
240 RCSResourceAttributes::Value one { 1 };
241 RCSResourceAttributes::Value another;
243 another = std::move(one);
245 ASSERT_EQ(nullptr, one);
248 TEST(ResourceAttributesValueTest, SameValuesAreEqual)
250 RCSResourceAttributes::Value one { 1 };
251 RCSResourceAttributes::Value another { 1 };
253 ASSERT_EQ(one, another);
256 TEST(ResourceAttributesValueTest, DifferentValuesAreNotEqual)
258 RCSResourceAttributes::Value one { 1 };
259 RCSResourceAttributes::Value another { 2 };
261 ASSERT_NE(one, another);
264 TEST(ResourceAttributesValueTest, ValuesCanBeSwapped)
266 constexpr int i { 0 };
267 constexpr char str[]{ "abc" };
269 RCSResourceAttributes::Value intValue { i };
270 RCSResourceAttributes::Value strValue { str };
272 intValue.swap(strValue);
274 ASSERT_EQ(str, intValue);
275 ASSERT_EQ(i, strValue);
278 TEST(ResourceAttributesTypeTest, TypeIdMatchesTypeOfValue)
280 RCSResourceAttributes::Value intValue { 0 };
282 ASSERT_EQ(intValue.getType().getId(), RCSResourceAttributes::TypeId::INT);
285 TEST(ResourceAttributesTypeTest, TypeCanBeConstructedFromValue)
287 RCSResourceAttributes::Value intValue { 1 };
289 RCSResourceAttributes::Type t = RCSResourceAttributes::Type::typeOf(0);
291 ASSERT_EQ(intValue.getType(), t);
294 TEST(ResourceAttributesTypeTest, DepthOfNonSequceTypeIsZero)
296 RCSResourceAttributes::Value intValue { 1 };
298 RCSResourceAttributes::Type t = intValue.getType();
300 ASSERT_EQ(0U, RCSResourceAttributes::Type::getDepth(t));
303 TEST(ResourceAttributesTypeTest, DepthOfSequceTypeIsNumberOfNested)
305 typedef std::vector< std::vector< std::vector< int > > > NestedVector;
307 RCSResourceAttributes::Type t = RCSResourceAttributes::Type::typeOf(NestedVector{ });
309 ASSERT_EQ(3U, RCSResourceAttributes::Type::getDepth(t));
312 TEST(ResourceAttributesTypeTest, BaseTypeOfNonSequceTypeIsItself)
314 RCSResourceAttributes::Value intValue { 1 };
316 RCSResourceAttributes::Type t = intValue.getType();
318 ASSERT_EQ(RCSResourceAttributes::TypeId::INT, RCSResourceAttributes::Type::getBaseTypeId(t));
321 TEST(ResourceAttributesTypeTest, BaseTypeOfSequceTypeIsMostNestedType)
323 typedef std::vector< std::vector< std::vector< RCSResourceAttributes > > > NestedVector;
325 RCSResourceAttributes::Type t = RCSResourceAttributes::Type::typeOf(NestedVector{ });
327 ASSERT_EQ(RCSResourceAttributes::TypeId::ATTRIBUTES,
328 RCSResourceAttributes::Type::getBaseTypeId(t));
332 TEST(ResourceAttributesConverterTest, OCRepresentationCanBeConvertedIntoResourceAttributes)
334 constexpr double value = 9876;
335 OC::OCRepresentation ocRep;
338 RCSResourceAttributes resourceAttributes{
339 ResourceAttributesConverter::fromOCRepresentation(ocRep) };
341 ASSERT_TRUE(value == resourceAttributes[KEY]);
345 TEST(ResourceAttributesConverterTest, NestedOCRepresentationCanBeConvertedIntoResourceAttributes)
347 std::string nested_value { "nested" };
348 OC::OCRepresentation ocRep;
349 OC::OCRepresentation nested;
350 nested[KEY] = nested_value;
353 RCSResourceAttributes resourceAttributes{
354 ResourceAttributesConverter::fromOCRepresentation(ocRep) };
356 ASSERT_EQ(nested_value, resourceAttributes[KEY].get<RCSResourceAttributes>()[KEY]);
360 TEST(ResourceAttributesConverterTest, ResourceAttributesCanBeConvertedIntoOCRepresentation)
362 double value { 3453453 };
363 RCSResourceAttributes resourceAttributes;
364 resourceAttributes[KEY] = value;
366 OC::OCRepresentation ocRep{
367 ResourceAttributesConverter::toOCRepresentation(resourceAttributes) };
369 ASSERT_EQ(value, ocRep[KEY].getValue<double>());
372 TEST(ResourceAttributesConverterTest, NestedResourceAttributesCanBeConvertedIntoOCRepresentation)
374 std::string nested_value { "nested" };
375 RCSResourceAttributes resourceAttributes;
376 RCSResourceAttributes nested;
377 nested[KEY] = nested_value;
378 resourceAttributes[KEY] = nested;
380 OC::OCRepresentation ocRep{
381 ResourceAttributesConverter::toOCRepresentation(resourceAttributes) };
383 ASSERT_EQ(nested_value,
384 ocRep[KEY].getValue<OC::OCRepresentation>()[KEY].getValue<std::string>());
387 TEST(ResourceAttributesConverterTest, OCRepresentationNullTypeIsNullptrInResourceAttributes)
389 OC::OCRepresentation ocRep;
392 RCSResourceAttributes resourceAttributes{
393 ResourceAttributesConverter::fromOCRepresentation(ocRep) };
395 ASSERT_EQ(nullptr, resourceAttributes[KEY]);
398 TEST(ResourceAttributesConverterTest, OCRepresentationHasNullWhenResourceAttributeIsNullptr)
400 RCSResourceAttributes resourceAttributes;
401 resourceAttributes[KEY] = nullptr;
403 OC::OCRepresentation ocRep{
404 ResourceAttributesConverter::toOCRepresentation(resourceAttributes) };
406 ASSERT_TRUE(ocRep.isNULL(KEY));
409 TEST(ResourceAttributesConverterTest, ResourceAttributesWithSequenceTypeCanBeConverted)
411 typedef std::vector< std::vector< std::vector< int > > > NestedVector;
412 constexpr int value { 3453453 };
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;
420 NestedVector ocSeq = ResourceAttributesConverter::toOCRepresentation(resourceAttributes)[KEY];
422 ASSERT_EQ(ocSeq[1][2][3], value);
425 TEST(ResourceAttributesConverterTest, OCRepresentationWithSequenceTypeCanBeConverted)
427 typedef std::vector< std::vector< std::vector< std::string > > > NestedVector;
428 constexpr char value[]{ "some_string" };
430 OC::OCRepresentation ocRep;
431 NestedVector seq(10);
432 seq[1].resize(10, std::vector< std::string >(10));
433 seq[1][2][3] = value;
436 RCSResourceAttributes resourceAttributes{
437 ResourceAttributesConverter::fromOCRepresentation(ocRep) };
439 ASSERT_EQ(seq, resourceAttributes[KEY]);
443 class ResourceAttributesUtilTest: public Test
446 RCSResourceAttributes resourceAttributes;
451 resourceAttributes[KEY] = 1;
455 TEST_F(ResourceAttributesUtilTest, EmptyAttributesIsAcceptable)
457 ASSERT_TRUE(acceptableAttributes(resourceAttributes, RCSResourceAttributes()));
460 TEST_F(ResourceAttributesUtilTest, AttributesItselfIsAcceptable)
462 ASSERT_TRUE(acceptableAttributes(resourceAttributes, resourceAttributes));
465 TEST_F(ResourceAttributesUtilTest, UnknownKeyIsNotAcceptable)
467 RCSResourceAttributes newAttrs;
468 newAttrs["unknown"] = 1;
470 ASSERT_FALSE(acceptableAttributes(resourceAttributes, newAttrs));
473 TEST_F(ResourceAttributesUtilTest, DifferentTypeWithOriginalIsNotAcceptable)
475 RCSResourceAttributes newAttrs;
478 ASSERT_FALSE(acceptableAttributes(resourceAttributes, newAttrs));
482 TEST_F(ResourceAttributesUtilTest, DifferentTypeOfNestedAttributeIsNotAcceptable)
484 constexpr char KEY_NESTED_ATTR[]{ "nested" };
485 constexpr char KEY_NESTED_VALUE[]{ "nested_value" };
487 RCSResourceAttributes nested;
488 nested[KEY_NESTED_VALUE] = -99;
489 resourceAttributes[KEY_NESTED_ATTR] = nested;
492 RCSResourceAttributes newAttrs;
493 nested[KEY_NESTED_VALUE] = "abc";
494 newAttrs[KEY_NESTED_ATTR] = nested;
496 ASSERT_FALSE(acceptableAttributes(resourceAttributes, newAttrs));
499 TEST_F(ResourceAttributesUtilTest, ReplaceWillOverwriteOriginal)
501 constexpr char NEW_VALUE[]{ "newValue" };
503 RCSResourceAttributes newAttrs;
504 newAttrs[KEY] = NEW_VALUE;
506 replaceAttributes(resourceAttributes, newAttrs);
508 ASSERT_EQ(NEW_VALUE, resourceAttributes[KEY]);