Commit 58186067b298db97bcb21a723cb07305b9402abf

Authored by Jojo-1000
Committed by Moritz Wirger
1 parent 137804f7

Fix constructors for Rule,Scene,Schedule with shared state.

include/hueplusplus/Rule.h
@@ -42,6 +42,10 @@ namespace hueplusplus @@ -42,6 +42,10 @@ namespace hueplusplus
42 class Rule 42 class Rule
43 { 43 {
44 public: 44 public:
  45 + //! \brief Creates rule with shared cache
  46 + //! \param id Rule id in the bridge
  47 + //! \param baseCache Cache of the rule list.
  48 + Rule(int id, const std::shared_ptr<APICache>& baseCache);
45 //! \brief Creates rule with id 49 //! \brief Creates rule with id
46 //! \param id Rule id in the bridge 50 //! \param id Rule id in the bridge
47 //! \param commands HueCommandAPI for requests 51 //! \param commands HueCommandAPI for requests
include/hueplusplus/Scene.h
@@ -123,6 +123,10 @@ public: @@ -123,6 +123,10 @@ public:
123 }; 123 };
124 124
125 public: 125 public:
  126 + //! \brief Creates scene with shared cache
  127 + //! \param id Scene id in the bridge
  128 + //! \param baseCache Cache of the scene list.
  129 + Scene(const std::string& id, const std::shared_ptr<APICache>& baseCache);
126 //! \brief Construct existing Scene 130 //! \brief Construct existing Scene
127 //! \param id Scene id 131 //! \param id Scene id
128 //! \param commands HueCommandAPI for requests 132 //! \param commands HueCommandAPI for requests
include/hueplusplus/Schedule.h
@@ -34,6 +34,10 @@ namespace hueplusplus @@ -34,6 +34,10 @@ namespace hueplusplus
34 class Schedule 34 class Schedule
35 { 35 {
36 public: 36 public:
  37 + //! \brief Creates schedule with shared cache
  38 + //! \param id Schedule id in the bridge
  39 + //! \param baseCache Cache of the schedule list.
  40 + Schedule(int id, const std::shared_ptr<APICache>& baseCache);
37 //! \brief Construct Schedule that exists in the bridge 41 //! \brief Construct Schedule that exists in the bridge
38 //! \param id Schedule ID 42 //! \param id Schedule ID
39 //! \param commands HueCommandAPI for requests 43 //! \param commands HueCommandAPI for requests
src/Rule.cpp
@@ -122,7 +122,7 @@ Condition Condition::parse(const nlohmann::json&amp; json) @@ -122,7 +122,7 @@ Condition Condition::parse(const nlohmann::json&amp; json)
122 { 122 {
123 op = Operator::notIn; 123 op = Operator::notIn;
124 } 124 }
125 - else if(opStr != "eq") 125 + else if (opStr != "eq")
126 { 126 {
127 throw HueException(CURRENT_FILE_INFO, "Unknown condition operator: " + opStr); 127 throw HueException(CURRENT_FILE_INFO, "Unknown condition operator: " + opStr);
128 } 128 }
@@ -130,9 +130,13 @@ Condition Condition::parse(const nlohmann::json&amp; json) @@ -130,9 +130,13 @@ Condition Condition::parse(const nlohmann::json&amp; json)
130 return Condition(address, op, value); 130 return Condition(address, op, value);
131 } 131 }
132 132
133 -Rule::Rule(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration, const nlohmann::json& currentState) 133 +Rule::Rule(int id, const std::shared_ptr<APICache>& baseCache)
  134 + : id(id), state(baseCache, std::to_string(id), baseCache->getRefreshDuration())
  135 +{ }
  136 +Rule::Rule(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration,
  137 + const nlohmann::json& currentState)
134 : id(id), state("/rules/" + std::to_string(id), commands, refreshDuration, currentState) 138 : id(id), state("/rules/" + std::to_string(id), commands, refreshDuration, currentState)
135 -{ 139 +{
136 refresh(); 140 refresh();
137 } 141 }
138 142
src/Scene.cpp
@@ -150,7 +150,12 @@ LightState LightStateBuilder::create() @@ -150,7 +150,12 @@ LightState LightStateBuilder::create()
150 return LightState(state); 150 return LightState(state);
151 } 151 }
152 152
153 -Scene::Scene(const std::string& id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration, const nlohmann::json& currentState) 153 +Scene::Scene(const std::string& id, const std::shared_ptr<APICache>& baseCache)
  154 + : id(id), state(baseCache, id, baseCache->getRefreshDuration())
  155 +{ }
  156 +
  157 +Scene::Scene(const std::string& id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration,
  158 + const nlohmann::json& currentState)
154 : id(id), state("/scenes/" + id, commands, refreshDuration, currentState) 159 : id(id), state("/scenes/" + id, commands, refreshDuration, currentState)
155 { 160 {
156 refresh(); 161 refresh();
src/Schedule.cpp
@@ -24,7 +24,11 @@ @@ -24,7 +24,11 @@
24 24
25 namespace hueplusplus 25 namespace hueplusplus
26 { 26 {
27 -Schedule::Schedule(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration, const nlohmann::json& currentState) 27 +Schedule::Schedule(int id, const std::shared_ptr<APICache>& baseCache)
  28 + : id(id), state(baseCache, std::to_string(id), baseCache->getRefreshDuration())
  29 +{ }
  30 +Schedule::Schedule(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration,
  31 + const nlohmann::json& currentState)
28 : id(id), state("/schedules/" + std::to_string(id), commands, refreshDuration, currentState) 32 : id(id), state("/schedules/" + std::to_string(id), commands, refreshDuration, currentState)
29 { 33 {
30 state.refresh(); 34 state.refresh();
@@ -40,7 +44,6 @@ void Schedule::setRefreshDuration(std::chrono::steady_clock::duration refreshDur @@ -40,7 +44,6 @@ void Schedule::setRefreshDuration(std::chrono::steady_clock::duration refreshDur
40 state.setRefreshDuration(refreshDuration); 44 state.setRefreshDuration(refreshDuration);
41 } 45 }
42 46
43 -  
44 int Schedule::getId() const 47 int Schedule::getId() const
45 { 48 {
46 return id; 49 return id;
test/test_Bridge.cpp
@@ -557,3 +557,69 @@ TEST(Bridge, createGroup) @@ -557,3 +557,69 @@ TEST(Bridge, createGroup)
557 .WillOnce(Return(response)); 557 .WillOnce(Return(response));
558 EXPECT_EQ(0, test_bridge.groups().create(create)); 558 EXPECT_EQ(0, test_bridge.groups().create(create));
559 } 559 }
  560 +
  561 +#define IGNORE_EXCEPTIONS(statement) \
  562 + try \
  563 + { \
  564 + statement; \
  565 + } \
  566 + catch (...) \
  567 + { }
  568 +
  569 +TEST(Bridge, instantiateResourceLists)
  570 +{
  571 + // Instantiate all methods on the resource lists, so that compile errors become visible
  572 + using namespace ::testing;
  573 + nlohmann::json bridgeState {{"lights", nlohmann::json::object()}, {"groups", nlohmann::json::object()},
  574 + {"schedules", nlohmann::json::object()}, {"scenes", nlohmann::json::object()},
  575 + {"sensors", nlohmann::json::object()}, {"rules", nlohmann::json::object()}};
  576 + std::shared_ptr<MockHttpHandler> handler = std::make_shared<MockHttpHandler>();
  577 + EXPECT_CALL(*handler, GETJson(_, _, getBridgeIp(), getBridgePort())).Times(AnyNumber());
  578 + EXPECT_CALL(*handler, POSTJson(_, _, getBridgeIp(), getBridgePort())).Times(AnyNumber());
  579 + EXPECT_CALL(*handler, DELETEJson(_, _, getBridgeIp(), getBridgePort())).Times(AnyNumber());
  580 + EXPECT_CALL(
  581 + *handler, GETJson("/api/" + getBridgeUsername(), nlohmann::json::object(), getBridgeIp(), getBridgePort()))
  582 + .Times(AtLeast(1))
  583 + .WillRepeatedly(Return(bridgeState));
  584 +
  585 + Bridge bridge(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);
  586 +
  587 + IGNORE_EXCEPTIONS(bridge.lights().getAll());
  588 + IGNORE_EXCEPTIONS(bridge.lights().get(1));
  589 + IGNORE_EXCEPTIONS(bridge.lights().exists(1));
  590 + IGNORE_EXCEPTIONS(bridge.lights().search());
  591 + IGNORE_EXCEPTIONS(bridge.lights().getNewDevices());
  592 + IGNORE_EXCEPTIONS(bridge.lights().remove(1));
  593 +
  594 + IGNORE_EXCEPTIONS(bridge.groups().getAll());
  595 + IGNORE_EXCEPTIONS(bridge.groups().get(1));
  596 + IGNORE_EXCEPTIONS(bridge.groups().exists(1));
  597 + IGNORE_EXCEPTIONS(bridge.groups().create(CreateGroup::Entertainment({}, "")));
  598 + IGNORE_EXCEPTIONS(bridge.groups().remove(1));
  599 +
  600 + IGNORE_EXCEPTIONS(bridge.schedules().getAll());
  601 + IGNORE_EXCEPTIONS(bridge.schedules().get(1));
  602 + IGNORE_EXCEPTIONS(bridge.schedules().exists(1));
  603 + IGNORE_EXCEPTIONS(bridge.schedules().create(CreateSchedule()));
  604 + IGNORE_EXCEPTIONS(bridge.schedules().remove(1));
  605 +
  606 + IGNORE_EXCEPTIONS(bridge.scenes().getAll());
  607 + IGNORE_EXCEPTIONS(bridge.scenes().get("1"));
  608 + IGNORE_EXCEPTIONS(bridge.scenes().exists("1"));
  609 + IGNORE_EXCEPTIONS(bridge.scenes().create(CreateScene()));
  610 + IGNORE_EXCEPTIONS(bridge.scenes().remove("1"));
  611 +
  612 + IGNORE_EXCEPTIONS(bridge.sensors().getAll());
  613 + IGNORE_EXCEPTIONS(bridge.sensors().get(1));
  614 + IGNORE_EXCEPTIONS(bridge.sensors().exists(1));
  615 + IGNORE_EXCEPTIONS(bridge.sensors().create(CreateSensor("", "", "", "", "", "")));
  616 + IGNORE_EXCEPTIONS(bridge.sensors().search());
  617 + IGNORE_EXCEPTIONS(bridge.sensors().getNewDevices());
  618 + IGNORE_EXCEPTIONS(bridge.sensors().remove(1));
  619 +
  620 + IGNORE_EXCEPTIONS(bridge.rules().getAll());
  621 + IGNORE_EXCEPTIONS(bridge.rules().get(1));
  622 + IGNORE_EXCEPTIONS(bridge.rules().exists(1));
  623 + IGNORE_EXCEPTIONS(bridge.rules().create(CreateRule({}, {})));
  624 + IGNORE_EXCEPTIONS(bridge.rules().remove(1));
  625 +}