Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / resourceBroker / unittest / DeviceAssociationUnitTest.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 <iostream>
22
23 #include "gtest/gtest.h"
24 #include "HippoMocks/hippomocks.h"
25
26 #include "OCPlatform.h"
27
28 #include "DevicePresence.h"
29 #include "DeviceAssociation.h"
30 #include "ResourcePresence.h"
31 #include "PrimitiveResource.h"
32 #include "ResponseStatement.h"
33 #include "UnitTestHelper.h"
34
35 using namespace testing;
36 using namespace OIC::Service;
37 using namespace OC;
38
39 #define STRING_VALUE "10.242.34.235"
40
41 typedef OCStackResult (*subscribePresenceSig1)(OC::OCPlatform::OCPresenceHandle&,
42         const std::string&, OCConnectivityType, SubscribeCallback);
43
44 class DeviceAssociationTest : public TestWithMock
45 {
46 public:
47
48     DeviceAssociation * instance;
49     DevicePresencePtr device;
50     PrimitiveResource::Ptr pResource;
51 protected:
52
53     void setMockingFunc()
54     {
55         mocks.OnCall(pResource.get(), PrimitiveResource::requestGet);
56         mocks.OnCall(pResource.get(), PrimitiveResource::getHost).Return(STRING_VALUE);
57         mocks.OnCallFuncOverload(static_cast< subscribePresenceSig1 >(OC::OCPlatform::subscribePresence)).Return(OC_STACK_OK);
58     }
59
60     void SetAssociationDevice()
61     {
62         setMockingFunc();
63         device->initializeDevicePresence(pResource);
64         instance->addDevice(device);
65     }
66
67     void SetUp()
68     {
69         TestWithMock::SetUp();
70         instance = DeviceAssociation::getInstance();
71         device = (DevicePresencePtr)new DevicePresence();
72         pResource = PrimitiveResource::Ptr(mocks.Mock< PrimitiveResource >(), [](PrimitiveResource*){});
73     }
74
75     void TearDown()
76     {
77         TestWithMock::TearDown();
78         device.reset();
79         pResource.reset();
80
81     }
82 };
83
84 TEST_F(DeviceAssociationTest,findDevice_ReturnNormalValueIfNormalParam)
85 {
86
87     SetAssociationDevice();
88     ASSERT_NE(nullptr,instance->findDevice(pResource->getHost()));
89
90
91 }
92
93 TEST_F(DeviceAssociationTest,addDevice_NormalHandlingIfNormalParam)
94 {
95
96     SetAssociationDevice();
97     ASSERT_FALSE(instance->isEmptyDeviceList());
98 }
99
100 TEST_F(DeviceAssociationTest,removeDevice_NormalHandlingIfNormalParam)
101 {
102
103     SetAssociationDevice();
104     instance->removeDevice(device);
105     ASSERT_TRUE(instance->isEmptyDeviceList());
106 }
107