forked from enwi/hueplusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ResourceList.cpp
300 lines (258 loc) · 12 KB
/
test_ResourceList.cpp
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
\file test_ResourceList.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 <gtest/gtest.h>
#include "testhelper.h"
#include "hueplusplus/ResourceList.h"
#include "mocks/mock_HttpHandler.h"
using namespace hueplusplus;
using namespace testing;
class TestResource
{
public:
TestResource(int id, std::shared_ptr<APICache> baseCache) {}
TestResource(int id, HueCommandAPI api, std::chrono::steady_clock::duration refreshDuration, const nlohmann::json& currentState) : id(id) { }
void refresh(bool force = false) { }
public:
int id;
};
class TestResourceFactory
{
public:
void refresh(bool force = false) { }
};
class TestStringResource
{
public:
TestStringResource(const std::string& id, std::shared_ptr<APICache> baseCache) {}
TestStringResource(const std::string& id, HueCommandAPI api, std::chrono::steady_clock::duration refreshDuration, const nlohmann::json& currentState)
: id(id)
{ }
void refresh(bool force = false) { }
public:
std::string id;
};
class TestCreateType
{
public:
MOCK_CONST_METHOD0(getRequest, nlohmann::json());
};
TEST(ResourceList, refresh)
{
auto handler = std::make_shared<MockHttpHandler>();
HueCommandAPI commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);
const std::string path = "/resources";
{
ResourceList<TestResource, int> list(commands, path, std::chrono::steady_clock::duration::max());
EXPECT_CALL(*handler,
GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
.Times(2)
.WillRepeatedly(Return(nlohmann::json::object()));
list.refresh();
list.refresh();
Mock::VerifyAndClearExpectations(handler.get());
}
{
auto baseCache = std::make_shared<APICache>("", commands, std::chrono::steady_clock::duration::max(), nullptr);
ResourceList<TestResource, int> list(baseCache, "resources", std::chrono::steady_clock::duration::max());
InSequence s;
EXPECT_CALL(
*handler, GETJson("/api/" + getBridgeUsername(), nlohmann::json::object(), getBridgeIp(), getBridgePort()))
.WillOnce(Return(nlohmann::json {{"resources", nlohmann::json::object()}}));
EXPECT_CALL(*handler,
GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
.Times(2)
.WillRepeatedly(Return(nlohmann::json::object()));
list.refresh();
list.refresh();
list.refresh();
Mock::VerifyAndClearExpectations(handler.get());
}
}
TEST(ResourceList, get)
{
auto handler = std::make_shared<MockHttpHandler>();
HueCommandAPI commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);
const std::string path = "/resources";
// No factory
{
const int id = 2;
const nlohmann::json response = {{std::to_string(id), {{"resource", "state"}}}};
ResourceList<TestResource, int> list(commands, path, std::chrono::steady_clock::duration::max());
EXPECT_CALL(*handler,
GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
.WillOnce(Return(response));
TestResource r = list.get(id);
EXPECT_EQ(id, r.id);
TestResource r2 = list.get(id);
EXPECT_EQ(id, r2.id);
}
// With factory
{
const int id = 2;
const nlohmann::json state = {{"resource", "state"}};
const nlohmann::json response = {{std::to_string(id), state}};
MockFunction<TestResource(int, const nlohmann::json&, const std::shared_ptr<APICache>&)> factory;
EXPECT_CALL(factory, Call(id, state, std::shared_ptr<APICache>()))
.WillOnce(Return(TestResource(id, commands, std::chrono::steady_clock::duration::max(), nullptr)));
ResourceList<TestResource, int> list(
commands, path, std::chrono::steady_clock::duration::max(), factory.AsStdFunction());
EXPECT_CALL(*handler,
GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
.WillOnce(Return(response));
TestResource r = list.get(id);
EXPECT_EQ(id, r.id);
}
// String id without factory
{
const std::string id = "id-2";
const nlohmann::json response = {{id, {{"resource", "state"}}}};
ResourceList<TestStringResource, std::string> list(commands, path, std::chrono::steady_clock::duration::max());
EXPECT_CALL(*handler,
GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
.WillOnce(Return(response));
TestStringResource r = list.get(id);
EXPECT_EQ(id, r.id);
TestStringResource r2 = list.get(id);
EXPECT_EQ(id, r2.id);
}
{
ResourceList<TestResourceFactory, int> list(commands, path, std::chrono::steady_clock::duration::max());
const int id = 2;
const nlohmann::json response = {{std::to_string(id), {{"resource", "state"}}}};
EXPECT_CALL(*handler,
GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
.WillOnce(Return(response));
EXPECT_THROW(list.get(id), HueException);
}
{
ResourceList<TestResourceFactory, int> list(commands, path, std::chrono::steady_clock::duration::max(),
[](int, const nlohmann::json&, const std::shared_ptr<APICache>&) { return TestResourceFactory(); });
const int id = 2;
const nlohmann::json response = {{std::to_string(id), {{"resource", "state"}}}};
EXPECT_CALL(*handler,
GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
.WillOnce(Return(response));
EXPECT_NO_THROW(list.get(id));
}
}
TEST(ResourceList, exists)
{
auto handler = std::make_shared<MockHttpHandler>();
HueCommandAPI commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);
const std::string path = "/resources";
const int id = 2;
const nlohmann::json response = {{std::to_string(id), {{"resource", "state"}}}};
ResourceList<TestResource, int> list(commands, path, std::chrono::steady_clock::duration::max());
EXPECT_CALL(*handler,
GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
.WillOnce(Return(response));
list.refresh();
EXPECT_TRUE(list.exists(id));
EXPECT_FALSE(list.exists(4));
}
TEST(ResourceList, getAll)
{
auto handler = std::make_shared<MockHttpHandler>();
HueCommandAPI commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);
const std::string path = "/resources";
const int id = 2;
const nlohmann::json response = {{std::to_string(id), {{"resource", "state"}}}};
ResourceList<TestResource, int> list(commands, path, std::chrono::steady_clock::duration::max());
EXPECT_CALL(*handler,
GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
.WillOnce(Return(response));
auto resources = list.getAll();
EXPECT_THAT(resources, ElementsAre(testing::Field("id", &TestResource::id, Eq(id))));
const int id2 = 3;
const nlohmann::json response2 = {{std::to_string(id), {{"r", "s"}}}, {std::to_string(id2), {{"b", "c"}}}};
EXPECT_CALL(*handler,
GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
.WillOnce(Return(response2));
list.refresh();
auto resources2 = list.getAll();
EXPECT_THAT(resources2,
ElementsAre(testing::Field("id", &TestResource::id, Eq(id)), testing::Field("id", &TestResource::id, Eq(id2))));
}
TEST(ResourceList, remove)
{
auto handler = std::make_shared<MockHttpHandler>();
HueCommandAPI commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);
const std::string path = "/resources";
const int id = 2;
const std::string requestPath = path + "/" + std::to_string(id);
const nlohmann::json response = {{{"success", requestPath + " deleted"}}};
ResourceList<TestResource, int> list(commands, path, std::chrono::steady_clock::duration::max());
EXPECT_CALL(*handler,
DELETEJson(
"/api/" + getBridgeUsername() + requestPath, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
.WillOnce(Return(response))
.WillOnce(Return(nlohmann::json()));
EXPECT_TRUE(list.remove(id));
EXPECT_FALSE(list.remove(id));
}
TEST(SearchableResourceList, search)
{
auto handler = std::make_shared<MockHttpHandler>();
HueCommandAPI commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);
const std::string path = "/resources";
SearchableResourceList<TestResource> list(commands, path, std::chrono::steady_clock::duration::max());
const nlohmann::json response = {{{"success", {{path, "Searching for new devices"}}}}};
EXPECT_CALL(*handler,
POSTJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort()))
.WillOnce(Return(response));
list.search();
EXPECT_CALL(*handler,
POSTJson("/api/" + getBridgeUsername() + path, nlohmann::json({{"deviceid", {"abcd", "def", "fgh"}}}),
getBridgeIp(), getBridgePort()))
.WillOnce(Return(response));
list.search({"abcd", "def", "fgh"});
}
TEST(SearchableResourceList, getNewDevices)
{
auto handler = std::make_shared<MockHttpHandler>();
HueCommandAPI commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);
const std::string path = "/resources";
SearchableResourceList<TestResource> list(commands, path, std::chrono::steady_clock::duration::max());
const nlohmann::json response = {{"lastscan", "active"}, {"1", {{"name", "A"}}}};
EXPECT_CALL(*handler,
GETJson(
"/api/" + getBridgeUsername() + path + "/new", nlohmann::json::object(), getBridgeIp(), getBridgePort()))
.WillOnce(Return(response));
NewDeviceList newDevices = list.getNewDevices();
EXPECT_TRUE(newDevices.isScanActive());
EXPECT_THAT(newDevices.getNewDevices(), ElementsAre(std::make_pair(1, "A")));
}
TEST(CreateableResourceList, create)
{
auto handler = std::make_shared<MockHttpHandler>();
HueCommandAPI commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);
const std::string path = "/resources";
const nlohmann::json response = {{{"success", {{"id", path + "/2"}}}}};
const nlohmann::json request = {{"name", "bla"}};
CreateableResourceList<ResourceList<TestResource, int>, TestCreateType> list(
commands, path, std::chrono::steady_clock::duration::max());
EXPECT_CALL(*handler, POSTJson("/api/" + getBridgeUsername() + path, request, getBridgeIp(), getBridgePort()))
.WillOnce(Return(response))
.WillOnce(Return(nlohmann::json()));
EXPECT_CALL(*handler, GETJson("/api/" + getBridgeUsername() + path, _, getBridgeIp(), getBridgePort()))
.Times(AnyNumber())
.WillRepeatedly(Return(nlohmann::json::object()));
TestCreateType params;
EXPECT_CALL(params, getRequest()).Times(2).WillRepeatedly(Return(request));
EXPECT_EQ(2, list.create(params));
EXPECT_EQ(0, list.create(params));
}