test_APICache.cpp
4.55 KB
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
/**
\file test_Hue.cpp
Copyright Notice\n
Copyright (C) 2017 Jan Rogall - developer\n
Copyright (C) 2017 Moritz Wirger - 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 <gtest/gtest.h>
#include "testhelper.h"
#include "hueplusplus/APICache.h"
#include "mocks/mock_HttpHandler.h"
using namespace hueplusplus;
TEST(APICache, GetRefreshDuration)
{
auto handler = std::make_shared<MockHttpHandler>();
HueCommandAPI commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);
{
std::chrono::steady_clock::duration refresh = std::chrono::seconds(20);
APICache cache("", commands, refresh);
EXPECT_EQ(refresh, cache.GetRefreshDuration());
}
{
std::chrono::steady_clock::duration refresh = std::chrono::seconds(0);
APICache cache("", commands, refresh);
EXPECT_EQ(refresh, cache.GetRefreshDuration());
}
{
std::chrono::steady_clock::duration refresh = std::chrono::steady_clock::duration::max();
APICache cache("", commands, refresh);
EXPECT_EQ(refresh, cache.GetRefreshDuration());
}
}
TEST(APICache, Refresh)
{
using namespace ::testing;
auto handler = std::make_shared<MockHttpHandler>();
HueCommandAPI commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);
{
std::string path = "/test/abc";
APICache cache(path, commands, std::chrono::seconds(10));
EXPECT_CALL(*handler,
GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
.WillOnce(Return(nlohmann::json::object()));
cache.Refresh();
Mock::VerifyAndClearExpectations(handler.get());
}
{
std::string path = "";
APICache cache(path, commands, std::chrono::seconds(10));
EXPECT_CALL(*handler,
GETJson(
"/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
.Times(2)
.WillRepeatedly(Return(nlohmann::json::object()));
cache.Refresh();
cache.Refresh();
Mock::VerifyAndClearExpectations(handler.get());
}
}
TEST(APICache, GetValue)
{
using namespace ::testing;
auto handler = std::make_shared<MockHttpHandler>();
HueCommandAPI commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);
// Always refresh
{
std::string path = "/test/abc";
APICache cache(path, commands, std::chrono::seconds(0));
nlohmann::json value = { {"a", "b"} };
EXPECT_CALL(*handler,
GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
.Times(2)
.WillRepeatedly(Return(value));
EXPECT_EQ(value, cache.GetValue());
EXPECT_EQ(value, cache.GetValue());
Mock::VerifyAndClearExpectations(handler.get());
}
// Only refresh once
{
std::string path = "/test/abc";
APICache cache(path, commands, std::chrono::steady_clock::duration::max());
nlohmann::json value = { {"a", "b"} };
EXPECT_CALL(*handler,
GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
.WillOnce(Return(value));
EXPECT_EQ(value, cache.GetValue());
EXPECT_EQ(value, cache.GetValue());
Mock::VerifyAndClearExpectations(handler.get());
}
// No refresh with const
{
std::string path = "/test/abc";
const APICache cache(path, commands, std::chrono::steady_clock::duration::max());
nlohmann::json value = { {"a", "b"} };
EXPECT_CALL(*handler,
GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
.Times(0);
EXPECT_EQ(nullptr, cache.GetValue());
Mock::VerifyAndClearExpectations(handler.get());
}
}