File: test_BaseDevice.cpp

package info (click to toggle)
hueplusplus 1.2.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,372 kB
  • sloc: cpp: 14,028; ansic: 70; makefile: 12
file content (125 lines) | stat: -rw-r--r-- 3,758 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
    \file test_BaseDevice.cpp
    Copyright Notice\n
    Copyright (C) 2020  Jan Rogall	- developer\n

    This file is part of hueplusplus.

    hueplusplus is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    hueplusplus is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with hueplusplus.  If not, see <http://www.gnu.org/licenses/>.
**/

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include "testhelper.h"

#include "hueplusplus/BaseDevice.h"
#include "mocks/mock_HttpHandler.h"

using namespace hueplusplus;
using namespace testing;

class TestDevice : public BaseDevice
{
public:
    TestDevice(int id, const HueCommandAPI& commands, const std::string& path,
        std::chrono::steady_clock::duration refreshDuration, const nlohmann::json& currentState)
        : BaseDevice(id, commands, path, refreshDuration, currentState)
    { }
};

class BaseDeviceTest : public Test
{
protected:
    std::shared_ptr<MockHttpHandler> handler;
    HueCommandAPI commands;
    nlohmann::json state;
    std::string path = "/test/";

protected:
    BaseDeviceTest()
        : handler(std::make_shared<MockHttpHandler>()),
          commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler),
          state({{"type", "testType"}, {"name", "Test name"}, {"swversion", "1.2.3.4"}, {"modelid", "TEST"},
              {"manufacturername", "testManuf"}, {"uniqueid", "00:00:00:00:00:00:00:00-00"},
              {"productname", "Test type"}})
    { }

    TestDevice getDevice(int id)
    {
        EXPECT_CALL(*handler,
            GETJson("/api/" + getBridgeUsername() + path + std::to_string(id), _, getBridgeIp(), getBridgePort()))
            .WillOnce(Return(state));
        return TestDevice(id, commands, path, std::chrono::steady_clock::duration::max(), nullptr);
    }
};

TEST_F(BaseDeviceTest, getId)
{
    const int id = 1;
    EXPECT_EQ(id, getDevice(id).getId());
}

TEST_F(BaseDeviceTest, getName)
{
    EXPECT_EQ("Test name", getDevice(1).getName());
}

TEST_F(BaseDeviceTest, getType)
{
    EXPECT_EQ("testType", getDevice(1).getType());
}

TEST_F(BaseDeviceTest, getModelId)
{
    EXPECT_EQ("TEST", getDevice(1).getModelId());
}

TEST_F(BaseDeviceTest, getUId)
{
    EXPECT_EQ("00:00:00:00:00:00:00:00-00", getDevice(1).getUId());
    state.erase("uniqueid");
    EXPECT_EQ("", getDevice(1).getUId());
}

TEST_F(BaseDeviceTest, getManufacturername)
{
    EXPECT_EQ("testManuf", getDevice(1).getManufacturername());
    state.erase("manufacturername");
    EXPECT_EQ("", getDevice(1).getManufacturername());
}

TEST_F(BaseDeviceTest, getProductname)
{
    EXPECT_EQ("Test type", getDevice(1).getProductname());
    state.erase("productname");
    EXPECT_EQ("", getDevice(1).getProductname());
}

TEST_F(BaseDeviceTest, getSwVersion)
{
    EXPECT_EQ("1.2.3.4", getDevice(1).getSwVersion());
}

TEST_F(BaseDeviceTest, setName)
{
    const std::string name = "asdbsdakfl";
    const nlohmann::json request = {{"name", name}};
    const nlohmann::json response = { {{"success", {{"/lights/1/name", name}}}} };
    
    TestDevice device = getDevice(1);
    EXPECT_CALL(*handler, PUTJson("/api/" + getBridgeUsername() + path + "1/name", request, getBridgeIp(), getBridgePort()))
        .WillOnce(Return(response));
    EXPECT_TRUE(device.setName(name));
}