Commit 8d45d3421dcead6542eeee3eab84fc3c83003283
Committed by
Moritz Wirger
1 parent
c04fb6a0
Add test for shared device state, fix request paths with shared state.
Showing
5 changed files
with
44 additions
and
7 deletions
include/hueplusplus/BaseDevice.h
| @@ -145,7 +145,6 @@ protected: | @@ -145,7 +145,6 @@ protected: | ||
| 145 | 145 | ||
| 146 | protected: | 146 | protected: |
| 147 | int id; //!< holds the id of the device | 147 | int id; //!< holds the id of the device |
| 148 | - std::string path; //!< holds the path of the device | ||
| 149 | APICache state; //!< holds the current state of the device | 148 | APICache state; //!< holds the current state of the device |
| 150 | }; | 149 | }; |
| 151 | } // namespace hueplusplus | 150 | } // namespace hueplusplus |
src/BaseDevice.cpp
| @@ -97,7 +97,7 @@ BaseDevice::BaseDevice(int id, const std::shared_ptr<APICache>& baseCache) | @@ -97,7 +97,7 @@ BaseDevice::BaseDevice(int id, const std::shared_ptr<APICache>& baseCache) | ||
| 97 | 97 | ||
| 98 | BaseDevice::BaseDevice( | 98 | BaseDevice::BaseDevice( |
| 99 | int id, const HueCommandAPI& commands, const std::string& path, std::chrono::steady_clock::duration refreshDuration, const nlohmann::json& currentState) | 99 | int id, const HueCommandAPI& commands, const std::string& path, std::chrono::steady_clock::duration refreshDuration, const nlohmann::json& currentState) |
| 100 | - : id(id), path(path), state(path + std::to_string(id), commands, refreshDuration, currentState) | 100 | + : id(id), state(path + std::to_string(id), commands, refreshDuration, currentState) |
| 101 | { | 101 | { |
| 102 | // Initialize value if not null | 102 | // Initialize value if not null |
| 103 | state.getValue(); | 103 | state.getValue(); |
| @@ -105,7 +105,7 @@ BaseDevice::BaseDevice( | @@ -105,7 +105,7 @@ BaseDevice::BaseDevice( | ||
| 105 | 105 | ||
| 106 | nlohmann::json BaseDevice::sendPutRequest(const std::string& subPath, const nlohmann::json& request, FileInfo fileInfo) | 106 | nlohmann::json BaseDevice::sendPutRequest(const std::string& subPath, const nlohmann::json& request, FileInfo fileInfo) |
| 107 | { | 107 | { |
| 108 | - return state.getCommandAPI().PUTRequest(path + std::to_string(id) + subPath, request, std::move(fileInfo)); | 108 | + return state.getCommandAPI().PUTRequest(state.getRequestPath() + subPath, request, std::move(fileInfo)); |
| 109 | } | 109 | } |
| 110 | 110 | ||
| 111 | void BaseDevice::refresh(bool force) | 111 | void BaseDevice::refresh(bool force) |
src/Sensor.cpp
| @@ -205,7 +205,7 @@ void Sensor::setStateAttribute(const std::string& key, const nlohmann::json& val | @@ -205,7 +205,7 @@ void Sensor::setStateAttribute(const std::string& key, const nlohmann::json& val | ||
| 205 | 205 | ||
| 206 | std::string Sensor::getStateAddress(const std::string& key) const | 206 | std::string Sensor::getStateAddress(const std::string& key) const |
| 207 | { | 207 | { |
| 208 | - return path + "/state/" + key; | 208 | + return state.getRequestPath() + "/state/" + key; |
| 209 | } | 209 | } |
| 210 | 210 | ||
| 211 | nlohmann::json Sensor::getConfig() const | 211 | nlohmann::json Sensor::getConfig() const |
test/test_BaseDevice.cpp
| @@ -122,4 +122,4 @@ TEST_F(BaseDeviceTest, setName) | @@ -122,4 +122,4 @@ TEST_F(BaseDeviceTest, setName) | ||
| 122 | EXPECT_CALL(*handler, PUTJson("/api/" + getBridgeUsername() + path + "1/name", request, getBridgeIp(), getBridgePort())) | 122 | EXPECT_CALL(*handler, PUTJson("/api/" + getBridgeUsername() + path + "1/name", request, getBridgeIp(), getBridgePort())) |
| 123 | .WillOnce(Return(response)); | 123 | .WillOnce(Return(response)); |
| 124 | EXPECT_TRUE(device.setName(name)); | 124 | EXPECT_TRUE(device.setName(name)); |
| 125 | -} | ||
| 126 | \ No newline at end of file | 125 | \ No newline at end of file |
| 126 | +} |
test/test_Bridge.cpp
| @@ -265,7 +265,7 @@ TEST(Bridge, getLight) | @@ -265,7 +265,7 @@ TEST(Bridge, getLight) | ||
| 265 | *handler, GETJson("/api/" + getBridgeUsername(), nlohmann::json::object(), getBridgeIp(), getBridgePort())) | 265 | *handler, GETJson("/api/" + getBridgeUsername(), nlohmann::json::object(), getBridgeIp(), getBridgePort())) |
| 266 | .Times(1) | 266 | .Times(1) |
| 267 | .WillOnce(Return(hue_bridge_state)); | 267 | .WillOnce(Return(hue_bridge_state)); |
| 268 | - | 268 | + |
| 269 | // Refresh cache | 269 | // Refresh cache |
| 270 | test_bridge = Bridge(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler); | 270 | test_bridge = Bridge(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler); |
| 271 | 271 | ||
| @@ -281,6 +281,45 @@ TEST(Bridge, getLight) | @@ -281,6 +281,45 @@ TEST(Bridge, getLight) | ||
| 281 | EXPECT_EQ(test_light_1.getColorType(), ColorType::TEMPERATURE); | 281 | EXPECT_EQ(test_light_1.getColorType(), ColorType::TEMPERATURE); |
| 282 | } | 282 | } |
| 283 | 283 | ||
| 284 | +TEST(Bridge, SharedState) | ||
| 285 | +{ | ||
| 286 | + using namespace ::testing; | ||
| 287 | + std::shared_ptr<MockHttpHandler> handler = std::make_shared<MockHttpHandler>(); | ||
| 288 | + | ||
| 289 | + nlohmann::json hue_bridge_state {{"lights", | ||
| 290 | + {{"1", | ||
| 291 | + {{"state", | ||
| 292 | + {{"on", true}, {"bri", 254}, {"ct", 366}, {"alert", "none"}, {"colormode", "ct"}, | ||
| 293 | + {"reachable", true}}}, | ||
| 294 | + {"swupdate", {{"state", "noupdates"}, {"lastinstall", nullptr}}}, {"type", "Color temperature light"}, | ||
| 295 | + {"name", "Hue ambiance lamp 1"}, {"modelid", "LTW001"}, {"manufacturername", "Philips"}, | ||
| 296 | + {"uniqueid", "00:00:00:00:00:00:00:00-00"}, {"swversion", "5.50.1.19085"}}}}}}; | ||
| 297 | + | ||
| 298 | + EXPECT_CALL( | ||
| 299 | + *handler, GETJson("/api/" + getBridgeUsername(), nlohmann::json::object(), getBridgeIp(), getBridgePort())) | ||
| 300 | + .Times(1) | ||
| 301 | + .WillOnce(Return(hue_bridge_state)); | ||
| 302 | + Bridge test_bridge(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler, std::chrono::seconds(10), true); | ||
| 303 | + | ||
| 304 | + // Test when correct data is sent | ||
| 305 | + Light test_light_1 = test_bridge.lights().get(1); | ||
| 306 | + | ||
| 307 | + Light test_light_copy = test_bridge.lights().get(1); | ||
| 308 | + const std::string newName = "New light name"; | ||
| 309 | + EXPECT_CALL(*handler, PUTJson("/api/" + getBridgeUsername() + "/lights/1/name", _, getBridgeIp(), getBridgePort())) | ||
| 310 | + .WillOnce(Return(nlohmann::json({{"success", {{{"/lights/1/name", newName}}}}}))); | ||
| 311 | + test_light_1.setName(newName); | ||
| 312 | + hue_bridge_state["lights"]["1"]["name"] = newName; | ||
| 313 | + | ||
| 314 | + EXPECT_CALL(*handler, | ||
| 315 | + GETJson("/api/" + getBridgeUsername() + "/lights/1", nlohmann::json::object(), getBridgeIp(), getBridgePort())) | ||
| 316 | + .Times(1) | ||
| 317 | + .WillOnce(Return(hue_bridge_state["lights"]["1"])); | ||
| 318 | + test_light_1.refresh(true); | ||
| 319 | + | ||
| 320 | + EXPECT_EQ(newName, test_light_copy.getName()); | ||
| 321 | +} | ||
| 322 | + | ||
| 284 | TEST(Bridge, removeLight) | 323 | TEST(Bridge, removeLight) |
| 285 | { | 324 | { |
| 286 | using namespace ::testing; | 325 | using namespace ::testing; |
| @@ -396,7 +435,6 @@ TEST(Bridge, getGroup) | @@ -396,7 +435,6 @@ TEST(Bridge, getGroup) | ||
| 396 | .Times(1) | 435 | .Times(1) |
| 397 | .WillOnce(Return(hue_bridge_state)); | 436 | .WillOnce(Return(hue_bridge_state)); |
| 398 | 437 | ||
| 399 | - | ||
| 400 | // Refresh cache | 438 | // Refresh cache |
| 401 | test_bridge = Bridge(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler); | 439 | test_bridge = Bridge(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler); |
| 402 | 440 |