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_TRUE(resourceAttributes[KEY] == 1);
51 TEST_F(ResourceAttributesTest, ValueThrowsIfTypeDoesNotMatch)
53 resourceAttributes[KEY] = 1;
54 auto& valueRef = resourceAttributes[KEY];
56 ASSERT_THROW(valueRef.get< std::string >(), BadGetException);
59 TEST_F(ResourceAttributesTest, GettingWithAtThrowsIfThereIsNoMatchedValue)
61 ASSERT_THROW(resourceAttributes.at(KEY), InvalidKeyException);
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_TRUE(resourceAttributes[KEY] == 1);
73 ASSERT_TRUE(copied == arbitraryStr);
76 TEST_F(ResourceAttributesTest, IsNullWhenAssignmentNullptr)
78 resourceAttributes[KEY] = nullptr;
80 ASSERT_TRUE(resourceAttributes[KEY] == nullptr);
83 TEST_F(ResourceAttributesTest, ValueChangedIfPutWithSameKey)
85 resourceAttributes[KEY] = "string";
86 resourceAttributes[KEY] = true;
88 ASSERT_TRUE(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), InvalidKeyException);
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_TRUE(resourceAttributes[KEY] == "after");
123 TEST_F(ResourceAttributesTest, CanHaveNestedResourceAttributes)
125 RCSResourceAttributes nested;
126 nested["nested"] = "nested_value";
127 resourceAttributes[KEY] = nested;
129 ASSERT_TRUE("nested_value" == resourceAttributes[KEY].get<RCSResourceAttributes>()["nested"]);
132 TEST_F(ResourceAttributesTest, ToStringReturnsStringForValue)
134 resourceAttributes[KEY] = true;
136 ASSERT_EQ("true", resourceAttributes[KEY].toString());
139 TEST_F(ResourceAttributesTest, ToStringReturnsEmptyStringForNullValue)
141 resourceAttributes[KEY] = nullptr;
143 ASSERT_EQ("", resourceAttributes[KEY].toString());
147 class ResourceAttributesIteratorTest: public Test
150 RCSResourceAttributes resourceAttributes;
153 TEST_F(ResourceAttributesIteratorTest, BeginEqualsEndWhenEmpty)
155 ASSERT_TRUE(resourceAttributes.begin() == resourceAttributes.end());
158 TEST_F(ResourceAttributesIteratorTest, CanIteratesWithForeach)
160 resourceAttributes["first"] = 1;
161 resourceAttributes["second"] = 2;
165 for (auto& i : resourceAttributes) {
173 TEST_F(ResourceAttributesIteratorTest, IteratesWithRef)
175 const char arbitraryStr[] { "ftryb457" };
176 resourceAttributes[KEY] = 1;
178 for (auto& i : resourceAttributes) {
179 i.value() = arbitraryStr;
182 ASSERT_TRUE(resourceAttributes[KEY] == arbitraryStr);
185 TEST_F(ResourceAttributesIteratorTest, IteratorIsCopyable)
187 RCSResourceAttributes::iterator it;
189 it = resourceAttributes.begin();
191 ASSERT_EQ(it, resourceAttributes.begin());
194 TEST_F(ResourceAttributesIteratorTest, IteratorIndicateNextItemAfterIncreased)
196 resourceAttributes[KEY] = 1;
198 RCSResourceAttributes::iterator it = resourceAttributes.begin();
202 ASSERT_TRUE(it == resourceAttributes.end());
205 TEST_F(ResourceAttributesIteratorTest, IteratorCanBeConvertedIntoConstIterator)
207 resourceAttributes[KEY] = 1;
208 RCSResourceAttributes::const_iterator it { resourceAttributes.begin() };
209 it = resourceAttributes.cbegin();
213 ASSERT_TRUE(it == resourceAttributes.cend());
216 TEST_F(ResourceAttributesIteratorTest, ConstIteratorIsUsedForConst)
218 resourceAttributes[KEY] = 1;
219 const RCSResourceAttributes& constAttrs = resourceAttributes;
221 auto iter = constAttrs.begin();
223 ASSERT_TRUE((std::is_same<decltype(iter), RCSResourceAttributes::const_iterator>::value));
227 TEST(ResourceAttributesValueTest, MovedValueHasNull)
229 RCSResourceAttributes::Value one { 1 };
230 RCSResourceAttributes::Value another { std::move(one) };
232 ASSERT_EQ(nullptr, one);
235 TEST(ResourceAttributesValueTest, MovedValueWithAssignmentHasNull)
237 RCSResourceAttributes::Value one { 1 };
238 RCSResourceAttributes::Value another;
240 another = std::move(one);
242 ASSERT_EQ(nullptr, one);
245 TEST(ResourceAttributesValueTest, SameValuesAreEqual)
247 RCSResourceAttributes::Value one { 1 };
248 RCSResourceAttributes::Value another { 1 };
250 ASSERT_EQ(one, another);
253 TEST(ResourceAttributesValueTest, DifferentValuesAreNotEqual)
255 RCSResourceAttributes::Value one { 1 };
256 RCSResourceAttributes::Value another { 2 };
258 ASSERT_NE(one, another);
261 TEST(ResourceAttributesValueTest, ValuesCanBeSwapped)
263 constexpr int i { 1 };
264 constexpr char str[]{ "abc" };
266 RCSResourceAttributes::Value intValue { i };
267 RCSResourceAttributes::Value strValue { str };
269 intValue.swap(strValue);
271 ASSERT_EQ(str, intValue);
272 ASSERT_EQ(i, strValue);
275 TEST(ResourceAttributesTypeTest, TypeIdMatchesTypeOfValue)
277 RCSResourceAttributes::Value intValue { 1 };
279 ASSERT_EQ(intValue.getType().getId(), RCSResourceAttributes::TypeId::INT);
282 TEST(ResourceAttributesTypeTest, TypeCanBeConstructedFromValue)
284 RCSResourceAttributes::Value intValue { 1 };
286 RCSResourceAttributes::Type t = RCSResourceAttributes::Type::typeOf(0);
288 ASSERT_EQ(intValue.getType(), t);
291 TEST(ResourceAttributesConverterTest, OCRepresentationCanBeConvertedIntoResourceAttributes)
293 constexpr double value = 9876;
294 OC::OCRepresentation ocRep;
297 RCSResourceAttributes resourceAttributes{
298 ResourceAttributesConverter::fromOCRepresentation(ocRep) };
300 ASSERT_TRUE(value == resourceAttributes[KEY]);
304 TEST(ResourceAttributesConverterTest, NestedOCRepresentationCanBeConvertedIntoResourceAttributes)
306 std::string nested_value { "nested" };
307 OC::OCRepresentation ocRep;
308 OC::OCRepresentation nested;
309 nested[KEY] = nested_value;
312 RCSResourceAttributes resourceAttributes{
313 ResourceAttributesConverter::fromOCRepresentation(ocRep) };
315 ASSERT_TRUE(nested_value == resourceAttributes[KEY].get<RCSResourceAttributes>()[KEY]);
319 TEST(ResourceAttributesConverterTest, ResourceAttributesCanBeConvertedIntoOCRepresentation)
321 double value { 3453453 };
322 RCSResourceAttributes resourceAttributes;
323 resourceAttributes[KEY] = value;
325 OC::OCRepresentation ocRep{
326 ResourceAttributesConverter::toOCRepresentation(resourceAttributes) };
328 ASSERT_TRUE(value == ocRep[KEY].getValue<double>());
331 TEST(ResourceAttributesConverterTest, NestedResourceAttributesCanBeConvertedIntoOCRepresentation)
333 std::string nested_value { "nested" };
334 RCSResourceAttributes resourceAttributes;
335 RCSResourceAttributes nested;
336 nested[KEY] = nested_value;
337 resourceAttributes[KEY] = nested;
339 OC::OCRepresentation ocRep{
340 ResourceAttributesConverter::toOCRepresentation(resourceAttributes) };
342 ASSERT_EQ(nested_value,
343 ocRep[KEY].getValue<OC::OCRepresentation>()[KEY].getValue<std::string>());
346 TEST(ResourceAttributesConverterTest, OCRepresentationNullTypeIsNullptrInResourceAttributes)
348 OC::OCRepresentation ocRep;
351 RCSResourceAttributes resourceAttributes{
352 ResourceAttributesConverter::fromOCRepresentation(ocRep) };
354 ASSERT_EQ(nullptr, resourceAttributes[KEY]);
357 TEST(ResourceAttributesConverterTest, OCRepresentationHasNullWhenResourceAttributeIsNullptr)
359 RCSResourceAttributes resourceAttributes;
360 resourceAttributes[KEY] = nullptr;
362 OC::OCRepresentation ocRep{
363 ResourceAttributesConverter::toOCRepresentation(resourceAttributes) };
365 ASSERT_TRUE(ocRep.isNULL(KEY));
370 class ResourceAttributesUtilTest: public Test
373 RCSResourceAttributes resourceAttributes;
378 resourceAttributes[KEY] = 1;
382 TEST_F(ResourceAttributesUtilTest, EmptyAttributesIsAcceptable)
384 ASSERT_TRUE(acceptableAttributes(resourceAttributes, RCSResourceAttributes()));
387 TEST_F(ResourceAttributesUtilTest, AttributesItselfIsAcceptable)
389 ASSERT_TRUE(acceptableAttributes(resourceAttributes, resourceAttributes));
392 TEST_F(ResourceAttributesUtilTest, UnknownKeyIsNotAcceptable)
394 RCSResourceAttributes newAttrs;
395 newAttrs["unknown"] = 1;
397 ASSERT_FALSE(acceptableAttributes(resourceAttributes, newAttrs));
400 TEST_F(ResourceAttributesUtilTest, DifferentTypeWithOriginalIsNotAcceptable)
402 RCSResourceAttributes newAttrs;
405 ASSERT_FALSE(acceptableAttributes(resourceAttributes, newAttrs));
409 TEST_F(ResourceAttributesUtilTest, DifferentTypeOfNestedAttributeIsNotAcceptable)
411 constexpr char KEY_NESTED_ATTR[]{ "nested" };
412 constexpr char KEY_NESTED_VALUE[]{ "nested_value" };
414 RCSResourceAttributes nested;
415 nested[KEY_NESTED_VALUE] = -99;
416 resourceAttributes[KEY_NESTED_ATTR] = nested;
419 RCSResourceAttributes newAttrs;
420 nested[KEY_NESTED_VALUE] = "abc";
421 newAttrs[KEY_NESTED_ATTR] = nested;
423 ASSERT_FALSE(acceptableAttributes(resourceAttributes, newAttrs));
426 TEST_F(ResourceAttributesUtilTest, ReplaceWillOverwriteOriginal)
428 constexpr char NEW_VALUE[]{ "newValue" };
430 RCSResourceAttributes newAttrs;
431 newAttrs[KEY] = NEW_VALUE;
433 replaceAttributes(resourceAttributes, newAttrs);
435 ASSERT_EQ(NEW_VALUE, resourceAttributes[KEY]);