DeviceAssociationUnitTest.cpp 2.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
//******************************************************************
//
// Copyright 2015 Samsung Electronics All Rights Reserved.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

doil.kwon's avatar
doil.kwon committed
21 22
#include <iostream>

23
#include <gtest/gtest.h>
doil.kwon's avatar
doil.kwon committed
24 25 26 27 28 29 30 31 32
#include "HippoMocks/hippomocks.h"

#include "OCPlatform.h"

#include "DevicePresence.h"
#include "DeviceAssociation.h"
#include "ResourcePresence.h"
#include "PrimitiveResource.h"
#include "ResponseStatement.h"
33
#include "UnitTestHelper.h"
doil.kwon's avatar
doil.kwon committed
34 35 36 37 38 39 40 41 42 43

using namespace testing;
using namespace OIC::Service;
using namespace OC;

#define STRING_VALUE "10.242.34.235"

typedef OCStackResult (*subscribePresenceSig1)(OC::OCPlatform::OCPresenceHandle&,
        const std::string&, OCConnectivityType, SubscribeCallback);

44
class DeviceAssociationTest : public TestWithMock
doil.kwon's avatar
doil.kwon committed
45 46 47 48 49 50 51
{
public:

    DeviceAssociation * instance;
    DevicePresencePtr device;
    PrimitiveResource::Ptr pResource;
protected:
52

doil.kwon's avatar
doil.kwon committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66
    void setMockingFunc()
    {
        mocks.OnCall(pResource.get(), PrimitiveResource::requestGet);
        mocks.OnCall(pResource.get(), PrimitiveResource::getHost).Return(STRING_VALUE);
        mocks.OnCallFuncOverload(static_cast< subscribePresenceSig1 >(OC::OCPlatform::subscribePresence)).Return(OC_STACK_OK);
    }

    void SetAssociationDevice()
    {
        setMockingFunc();
        device->initializeDevicePresence(pResource);
        instance->addDevice(device);
    }

67
    void SetUp()
68
    {
69
        TestWithMock::SetUp();
doil.kwon's avatar
doil.kwon committed
70 71
        instance = DeviceAssociation::getInstance();
        device = (DevicePresencePtr)new DevicePresence();
72 73 74 75
        pResource = PrimitiveResource::Ptr(mocks.Mock< PrimitiveResource >(),
                [](PrimitiveResource*)
                {
                });
doil.kwon's avatar
doil.kwon committed
76 77
    }

78
    void TearDown()
doil.kwon's avatar
doil.kwon committed
79
    {
80
        TestWithMock::TearDown();
doil.kwon's avatar
doil.kwon committed
81 82 83 84 85 86 87 88
        device.reset();
        pResource.reset();

    }
};

TEST_F(DeviceAssociationTest,findDevice_ReturnNormalValueIfNormalParam)
{
89

doil.kwon's avatar
doil.kwon committed
90 91
    SetAssociationDevice();
    ASSERT_NE(nullptr,instance->findDevice(pResource->getHost()));
92

doil.kwon's avatar
doil.kwon committed
93 94 95 96 97

}

TEST_F(DeviceAssociationTest,addDevice_NormalHandlingIfNormalParam)
{
98

doil.kwon's avatar
doil.kwon committed
99 100 101 102 103 104
    SetAssociationDevice();
    ASSERT_FALSE(instance->isEmptyDeviceList());
}

TEST_F(DeviceAssociationTest,removeDevice_NormalHandlingIfNormalParam)
{
105

doil.kwon's avatar
doil.kwon committed
106 107 108 109 110
    SetAssociationDevice();
    instance->removeDevice(device);
    ASSERT_TRUE(instance->isEmptyDeviceList());
}