From 58186067b298db97bcb21a723cb07305b9402abf Mon Sep 17 00:00:00 2001 From: Jojo-1000 <33495614+Jojo-1000@users.noreply.github.com> Date: Sat, 20 Mar 2021 22:02:49 +0100 Subject: [PATCH] Fix constructors for Rule,Scene,Schedule with shared state. --- include/hueplusplus/Rule.h | 4 ++++ include/hueplusplus/Scene.h | 4 ++++ include/hueplusplus/Schedule.h | 4 ++++ src/Rule.cpp | 10 +++++++--- src/Scene.cpp | 7 ++++++- src/Schedule.cpp | 7 +++++-- test/test_Bridge.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 96 insertions(+), 6 deletions(-) diff --git a/include/hueplusplus/Rule.h b/include/hueplusplus/Rule.h index 518f8fd..62ab93d 100644 --- a/include/hueplusplus/Rule.h +++ b/include/hueplusplus/Rule.h @@ -42,6 +42,10 @@ namespace hueplusplus class Rule { public: + //! \brief Creates rule with shared cache + //! \param id Rule id in the bridge + //! \param baseCache Cache of the rule list. + Rule(int id, const std::shared_ptr& baseCache); //! \brief Creates rule with id //! \param id Rule id in the bridge //! \param commands HueCommandAPI for requests diff --git a/include/hueplusplus/Scene.h b/include/hueplusplus/Scene.h index 162cf3b..a80411d 100644 --- a/include/hueplusplus/Scene.h +++ b/include/hueplusplus/Scene.h @@ -123,6 +123,10 @@ public: }; public: + //! \brief Creates scene with shared cache + //! \param id Scene id in the bridge + //! \param baseCache Cache of the scene list. + Scene(const std::string& id, const std::shared_ptr& baseCache); //! \brief Construct existing Scene //! \param id Scene id //! \param commands HueCommandAPI for requests diff --git a/include/hueplusplus/Schedule.h b/include/hueplusplus/Schedule.h index f272002..ecc4ab9 100644 --- a/include/hueplusplus/Schedule.h +++ b/include/hueplusplus/Schedule.h @@ -34,6 +34,10 @@ namespace hueplusplus class Schedule { public: + //! \brief Creates schedule with shared cache + //! \param id Schedule id in the bridge + //! \param baseCache Cache of the schedule list. + Schedule(int id, const std::shared_ptr& baseCache); //! \brief Construct Schedule that exists in the bridge //! \param id Schedule ID //! \param commands HueCommandAPI for requests diff --git a/src/Rule.cpp b/src/Rule.cpp index 8d85aaa..72e064b 100644 --- a/src/Rule.cpp +++ b/src/Rule.cpp @@ -122,7 +122,7 @@ Condition Condition::parse(const nlohmann::json& json) { op = Operator::notIn; } - else if(opStr != "eq") + else if (opStr != "eq") { throw HueException(CURRENT_FILE_INFO, "Unknown condition operator: " + opStr); } @@ -130,9 +130,13 @@ Condition Condition::parse(const nlohmann::json& json) return Condition(address, op, value); } -Rule::Rule(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration, const nlohmann::json& currentState) +Rule::Rule(int id, const std::shared_ptr& baseCache) + : id(id), state(baseCache, std::to_string(id), baseCache->getRefreshDuration()) +{ } +Rule::Rule(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration, + const nlohmann::json& currentState) : id(id), state("/rules/" + std::to_string(id), commands, refreshDuration, currentState) -{ +{ refresh(); } diff --git a/src/Scene.cpp b/src/Scene.cpp index 5be95d6..a993a40 100644 --- a/src/Scene.cpp +++ b/src/Scene.cpp @@ -150,7 +150,12 @@ LightState LightStateBuilder::create() return LightState(state); } -Scene::Scene(const std::string& id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration, const nlohmann::json& currentState) +Scene::Scene(const std::string& id, const std::shared_ptr& baseCache) + : id(id), state(baseCache, id, baseCache->getRefreshDuration()) +{ } + +Scene::Scene(const std::string& id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration, + const nlohmann::json& currentState) : id(id), state("/scenes/" + id, commands, refreshDuration, currentState) { refresh(); diff --git a/src/Schedule.cpp b/src/Schedule.cpp index 1ec862a..2ab04bb 100644 --- a/src/Schedule.cpp +++ b/src/Schedule.cpp @@ -24,7 +24,11 @@ namespace hueplusplus { -Schedule::Schedule(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration, const nlohmann::json& currentState) +Schedule::Schedule(int id, const std::shared_ptr& baseCache) + : id(id), state(baseCache, std::to_string(id), baseCache->getRefreshDuration()) +{ } +Schedule::Schedule(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration, + const nlohmann::json& currentState) : id(id), state("/schedules/" + std::to_string(id), commands, refreshDuration, currentState) { state.refresh(); @@ -40,7 +44,6 @@ void Schedule::setRefreshDuration(std::chrono::steady_clock::duration refreshDur state.setRefreshDuration(refreshDuration); } - int Schedule::getId() const { return id; diff --git a/test/test_Bridge.cpp b/test/test_Bridge.cpp index 3561502..bd24ac9 100644 --- a/test/test_Bridge.cpp +++ b/test/test_Bridge.cpp @@ -557,3 +557,69 @@ TEST(Bridge, createGroup) .WillOnce(Return(response)); EXPECT_EQ(0, test_bridge.groups().create(create)); } + +#define IGNORE_EXCEPTIONS(statement) \ + try \ + { \ + statement; \ + } \ + catch (...) \ + { } + +TEST(Bridge, instantiateResourceLists) +{ + // Instantiate all methods on the resource lists, so that compile errors become visible + using namespace ::testing; + nlohmann::json bridgeState {{"lights", nlohmann::json::object()}, {"groups", nlohmann::json::object()}, + {"schedules", nlohmann::json::object()}, {"scenes", nlohmann::json::object()}, + {"sensors", nlohmann::json::object()}, {"rules", nlohmann::json::object()}}; + std::shared_ptr handler = std::make_shared(); + EXPECT_CALL(*handler, GETJson(_, _, getBridgeIp(), getBridgePort())).Times(AnyNumber()); + EXPECT_CALL(*handler, POSTJson(_, _, getBridgeIp(), getBridgePort())).Times(AnyNumber()); + EXPECT_CALL(*handler, DELETEJson(_, _, getBridgeIp(), getBridgePort())).Times(AnyNumber()); + EXPECT_CALL( + *handler, GETJson("/api/" + getBridgeUsername(), nlohmann::json::object(), getBridgeIp(), getBridgePort())) + .Times(AtLeast(1)) + .WillRepeatedly(Return(bridgeState)); + + Bridge bridge(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler); + + IGNORE_EXCEPTIONS(bridge.lights().getAll()); + IGNORE_EXCEPTIONS(bridge.lights().get(1)); + IGNORE_EXCEPTIONS(bridge.lights().exists(1)); + IGNORE_EXCEPTIONS(bridge.lights().search()); + IGNORE_EXCEPTIONS(bridge.lights().getNewDevices()); + IGNORE_EXCEPTIONS(bridge.lights().remove(1)); + + IGNORE_EXCEPTIONS(bridge.groups().getAll()); + IGNORE_EXCEPTIONS(bridge.groups().get(1)); + IGNORE_EXCEPTIONS(bridge.groups().exists(1)); + IGNORE_EXCEPTIONS(bridge.groups().create(CreateGroup::Entertainment({}, ""))); + IGNORE_EXCEPTIONS(bridge.groups().remove(1)); + + IGNORE_EXCEPTIONS(bridge.schedules().getAll()); + IGNORE_EXCEPTIONS(bridge.schedules().get(1)); + IGNORE_EXCEPTIONS(bridge.schedules().exists(1)); + IGNORE_EXCEPTIONS(bridge.schedules().create(CreateSchedule())); + IGNORE_EXCEPTIONS(bridge.schedules().remove(1)); + + IGNORE_EXCEPTIONS(bridge.scenes().getAll()); + IGNORE_EXCEPTIONS(bridge.scenes().get("1")); + IGNORE_EXCEPTIONS(bridge.scenes().exists("1")); + IGNORE_EXCEPTIONS(bridge.scenes().create(CreateScene())); + IGNORE_EXCEPTIONS(bridge.scenes().remove("1")); + + IGNORE_EXCEPTIONS(bridge.sensors().getAll()); + IGNORE_EXCEPTIONS(bridge.sensors().get(1)); + IGNORE_EXCEPTIONS(bridge.sensors().exists(1)); + IGNORE_EXCEPTIONS(bridge.sensors().create(CreateSensor("", "", "", "", "", ""))); + IGNORE_EXCEPTIONS(bridge.sensors().search()); + IGNORE_EXCEPTIONS(bridge.sensors().getNewDevices()); + IGNORE_EXCEPTIONS(bridge.sensors().remove(1)); + + IGNORE_EXCEPTIONS(bridge.rules().getAll()); + IGNORE_EXCEPTIONS(bridge.rules().get(1)); + IGNORE_EXCEPTIONS(bridge.rules().exists(1)); + IGNORE_EXCEPTIONS(bridge.rules().create(CreateRule({}, {}))); + IGNORE_EXCEPTIONS(bridge.rules().remove(1)); +} -- libgit2 0.21.4