From c04fb6a0aed0f1412cc30fb25903a78b108e50ea Mon Sep 17 00:00:00 2001 From: Jojo-1000 <33495614+Jojo-1000@users.noreply.github.com> Date: Wed, 10 Mar 2021 20:38:32 +0100 Subject: [PATCH] Change APICache to allow initial value, remove cached resources from ResourceList. Change reference to value return types. --- include/hueplusplus/APICache.h | 3 ++- include/hueplusplus/BaseDevice.h | 3 ++- include/hueplusplus/Group.h | 3 ++- include/hueplusplus/Light.h | 7 +++---- include/hueplusplus/ResourceList.h | 72 +++++++++++++++++------------------------------------------------------- include/hueplusplus/Rule.h | 3 ++- include/hueplusplus/Scene.h | 3 ++- include/hueplusplus/Schedule.h | 3 ++- include/hueplusplus/Sensor.h | 3 ++- src/APICache.cpp | 13 +++++++++---- src/BaseDevice.cpp | 7 ++++--- src/Bridge.cpp | 4 ++-- src/Group.cpp | 7 ++++--- src/HueDeviceTypes.cpp | 2 +- src/Light.cpp | 9 ++++++--- src/Rule.cpp | 6 +++--- src/Scene.cpp | 4 ++-- src/Schedule.cpp | 4 ++-- src/Sensor.cpp | 4 ++-- test/mocks/mock_Light.h | 2 +- test/test_APICache.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++++-------------------- test/test_BaseDevice.cpp | 6 +++--- test/test_Bridge.cpp | 43 +++++++------------------------------------ test/test_BridgeConfig.cpp | 18 +++++++++--------- test/test_Group.cpp | 38 +++++++++++++++++++------------------- test/test_Light.cpp | 12 ------------ test/test_LightFactory.cpp | 76 ---------------------------------------------------------------------------- test/test_ResourceList.cpp | 18 +++++++++--------- test/test_Rule.cpp | 2 +- test/test_Scene.cpp | 48 ++++++++++++++++++++++++------------------------ test/test_Schedule.cpp | 34 +++++++++++++++++----------------- test/test_Sensor.cpp | 2 +- test/test_SensorImpls.cpp | 2 +- test/test_SensorList.cpp | 13 ------------- 34 files changed, 208 insertions(+), 333 deletions(-) diff --git a/include/hueplusplus/APICache.h b/include/hueplusplus/APICache.h index 5b28846..038085c 100644 --- a/include/hueplusplus/APICache.h +++ b/include/hueplusplus/APICache.h @@ -48,7 +48,8 @@ public: //! \param path URL appended after username, may be empty. //! \param commands HueCommandAPI for making API requests. //! \param refresh Interval between cache refreshing. May be 0 to always refresh. - APICache(const std::string& path, const HueCommandAPI& commands, std::chrono::steady_clock::duration refresh); + //! \param initial Initial value, may be null. If present, assumes the value is up to date. + APICache(const std::string& path, const HueCommandAPI& commands, std::chrono::steady_clock::duration refresh, const nlohmann::json& initial); //! \brief Refresh cache now. //! \throws std::system_error when system or socket operations fail diff --git a/include/hueplusplus/BaseDevice.h b/include/hueplusplus/BaseDevice.h index e4e68b8..e6d311d 100644 --- a/include/hueplusplus/BaseDevice.h +++ b/include/hueplusplus/BaseDevice.h @@ -127,8 +127,9 @@ protected: //! \param commands HueCommandAPI for communication with the bridge //! \param path Base path for the resource type, ending with a '/'. Example: \c "/lights/" //! \param refreshDuration Time between refreshing the cached state. + //! \param currentState Current state of the device, may be null. BaseDevice(int id, const HueCommandAPI& commands, const std::string& path, - std::chrono::steady_clock::duration refreshDuration); + std::chrono::steady_clock::duration refreshDuration, const nlohmann::json& currentState); //! \brief Utility function to send a put request to the device. //! diff --git a/include/hueplusplus/Group.h b/include/hueplusplus/Group.h index ace78d5..c6920ef 100644 --- a/include/hueplusplus/Group.h +++ b/include/hueplusplus/Group.h @@ -46,7 +46,8 @@ public: //! \param id Group id in the bridge //! \param commands HueCommandAPI for requests //! \param refreshDuration Time between refreshing the cached state. - Group(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration); + //! \param currentState The current state, may be null. + Group(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration, const nlohmann::json& currentState); //! \brief Refreshes internal cached state. //! \param force \c true forces a refresh, regardless of how long the last refresh was ago. diff --git a/include/hueplusplus/Light.h b/include/hueplusplus/Light.h index b55c916..8072bfc 100644 --- a/include/hueplusplus/Light.h +++ b/include/hueplusplus/Light.h @@ -554,7 +554,6 @@ public: ///@} protected: - //! \brief Protected ctor that is used by \ref Bridge class. //! //! \param id Integer that specifies the id of this light @@ -563,7 +562,6 @@ protected: //! leaves strategies unset Light(int id, const HueCommandAPI& commands); - Light(int id, const std::shared_ptr& baseCache); //! \brief Protected ctor that is used by \ref Bridge class, also sets @@ -576,14 +574,15 @@ protected: //! \param colorHueStrategy Strategy for color hue/saturation. May be nullptr. //! \param refreshDuration Time between refreshing the cached state. //! Can be 0 to always refresh, or steady_clock::duration::max() to never refresh. + //! \param currentState The current light state, may be null. //! \throws std::system_error when system or socket operations fail //! \throws HueException when response contained no body //! \throws HueAPIResponseException when response contains an error //! \throws nlohmann::json::parse_error when response could not be parsed Light(int id, const HueCommandAPI& commands, std::shared_ptr brightnessStrategy, std::shared_ptr colorTempStrategy, - std::shared_ptr colorHueStrategy, - std::chrono::steady_clock::duration refreshDuration = std::chrono::seconds(10)); + std::shared_ptr colorHueStrategy, std::chrono::steady_clock::duration refreshDuration, + const nlohmann::json& currentState); //! \brief Protected function that sets the brightness strategy. //! diff --git a/include/hueplusplus/ResourceList.h b/include/hueplusplus/ResourceList.h index c63ec6f..4931f26 100644 --- a/include/hueplusplus/ResourceList.h +++ b/include/hueplusplus/ResourceList.h @@ -39,7 +39,7 @@ namespace hueplusplus //! \tparam IdT Type of the resource id. int or std::string //! //! The resources are assumed to be in an object with ids as keys. -//! The Resource class needs a constructor that accepts \c id, HueCommandAPI and \c refreshDuration; +//! The Resource class needs a constructor that accepts \c id, HueCommandAPI, \c refreshDuration and \c state; //! otherwise a factory function needs to be provided that takes \c id and the JSON state. template class ResourceList @@ -78,7 +78,7 @@ public: std::chrono::steady_clock::duration refreshDuration, const std::function&)>& factory = nullptr) - : stateCache(std::make_shared(path, commands, refreshDuration)), + : stateCache(std::make_shared(path, commands, refreshDuration, nullptr)), factory(factory), path(path + '/'), sharedState(false) @@ -98,18 +98,14 @@ public: //! \throws HueException when response contains no body //! \throws HueAPIResponseException when response contains an error //! \throws nlohmann::json::parse_error when response could not be parsed - std::vector> getAll() + std::vector getAll() { - nlohmann::json state = stateCache->getValue(); - for (auto it = state.begin(); it != state.end(); ++it) - { - get(maybeStoi(it.key())); - } - std::vector> result; + nlohmann::json& state = stateCache->getValue(); + std::vector result; result.reserve(state.size()); - for (auto& entry : resources) + for (auto it = state.begin(); it != state.end(); ++it) { - result.emplace_back(entry.second); + result.emplace_back(construct(maybeStoi(it.key()), it.value())); } return result; } @@ -121,21 +117,15 @@ public: //! \throws HueException when id does not exist //! \throws HueAPIResponseException when response contains an error //! \throws nlohmann::json::parse_error when response could not be parsed - Resource& get(const IdType& id) + Resource get(const IdType& id) { - auto pos = resources.find(id); - if (pos != resources.end()) - { - pos->second.refresh(true); - return pos->second; - } const nlohmann::json& state = stateCache->getValue(); std::string key = maybeToString(id); if (!state.count(key)) { throw HueException(FileInfo {__FILE__, __LINE__, __func__}, "Resource id is not valid"); } - return resources.emplace(id, construct(id, state[key])).first->second; + return construct(id, state[key]); } //! \brief Checks whether resource with id exists @@ -145,30 +135,14 @@ public: //! \throws HueException when response contains no body //! \throws HueAPIResponseException when response contains an error //! \throws nlohmann::json::parse_error when response could not be parsed - bool exists(const IdType& id) - { - auto pos = resources.find(id); - if (pos != resources.end()) - { - return true; - } - return stateCache->getValue().count(maybeToString(id)) != 0; - } + bool exists(const IdType& id) { return stateCache->getValue().count(maybeToString(id)) != 0; } //! \brief Checks whether resource with id exists //! \param id Identifier of the resource to check //! \returns true when the resource with given id exists //! \note This will not update the cache //! \throws HueException when the cache is empty - bool exists(const IdType& id) const - { - auto pos = resources.find(id); - if (pos != resources.end()) - { - return true; - } - return stateCache->getValue().count(maybeToString(id)) != 0; - } + bool exists(const IdType& id) const { return stateCache->getValue().count(maybeToString(id)) != 0; } //! \brief Removes the resource //! \param id Identifier of the resource to remove @@ -185,11 +159,6 @@ public: nlohmann::json result = stateCache->getCommandAPI().DELETERequest( requestPath, nlohmann::json::object(), FileInfo {__FILE__, __LINE__, __func__}); bool success = utils::safeGetMember(result, 0, "success") == requestPath + " deleted"; - auto it = resources.find(id); - if (success && it != resources.end()) - { - resources.erase(it); - } return success; } @@ -205,7 +174,7 @@ protected: Resource construct(const IdType& id, const nlohmann::json& state) { return construct( - id, state, std::is_constructible {}); + id, state, std::is_constructible {}); } //! \brief Protected defaulted move constructor @@ -214,7 +183,7 @@ protected: ResourceList& operator=(ResourceList&&) = default; private: - // Resource is constructable + // Resource is constructible Resource construct(const IdType& id, const nlohmann::json& state, std::true_type) { if (factory) @@ -229,11 +198,11 @@ private: } else { - return Resource(id, stateCache->getCommandAPI(), stateCache->getRefreshDuration()); + return Resource(id, stateCache->getCommandAPI(), stateCache->getRefreshDuration(), state); } } } - // Resource is not constructable + // Resource is not constructible Resource construct(const IdType& id, const nlohmann::json& state, std::false_type) { if (!factory) @@ -254,7 +223,6 @@ protected: std::shared_ptr stateCache; std::function&)> factory; std::string path; - std::map resources; bool sharedState; }; @@ -361,21 +329,15 @@ public: using Base::Base; //! \brief Get group, specially handles group 0 //! \see ResourceList::get - Resource& get(const int& id) + Resource get(const int& id) { - auto pos = this->resources.find(id); - if (pos != this->resources.end()) - { - pos->second.refresh(); - return pos->second; - } const nlohmann::json& state = this->stateCache->getValue(); std::string key = this->maybeToString(id); if (!state.count(key) && id != 0) { throw HueException(FileInfo {__FILE__, __LINE__, __func__}, "Resource id is not valid"); } - return this->resources.emplace(id, this->construct(id, state[key])).first->second; + return this->construct(id, id == 0 ? nlohmann::json{ nullptr } : state[key]); } //! \brief Get group, specially handles group 0 //! \see ResourceList::exists diff --git a/include/hueplusplus/Rule.h b/include/hueplusplus/Rule.h index 07d6f94..4bd786a 100644 --- a/include/hueplusplus/Rule.h +++ b/include/hueplusplus/Rule.h @@ -46,7 +46,8 @@ public: //! \param id Rule id in the bridge //! \param commands HueCommandAPI for requests //! \param refreshDuration Time between refreshing the cached state. - Rule(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration); + //! \param currentState The current state, may be null. + Rule(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration, const nlohmann::json& currentState); //! \brief Refreshes internal cached state. //! \param force \c true forces a refresh, regardless of how long the last refresh was ago. diff --git a/include/hueplusplus/Scene.h b/include/hueplusplus/Scene.h index 5df95de..626d768 100644 --- a/include/hueplusplus/Scene.h +++ b/include/hueplusplus/Scene.h @@ -127,7 +127,8 @@ public: //! \param id Scene id //! \param commands HueCommandAPI for requests //! \param refreshDuration Time between refreshing the cached state - Scene(const std::string& id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration); + //! \param currentState The current state, may be null. + Scene(const std::string& id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration, const nlohmann::json& currentState); //! \brief Refreshes internal cached state //! \param force \c true forces a refresh, regardless of how long the last refresh was ago. diff --git a/include/hueplusplus/Schedule.h b/include/hueplusplus/Schedule.h index 80c861e..6b2cc1b 100644 --- a/include/hueplusplus/Schedule.h +++ b/include/hueplusplus/Schedule.h @@ -38,7 +38,8 @@ public: //! \param id Schedule ID //! \param commands HueCommandAPI for requests //! \param refreshDuration Time between refreshing the cached state - Schedule(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration); + //! \param currentState The current state, may be null. + Schedule(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration, const nlohmann::json& currentState); //! \brief Refreshes internal cached state //! \throws std::system_error when system or socket operations fail diff --git a/include/hueplusplus/Sensor.h b/include/hueplusplus/Sensor.h index a39f20f..ea1587f 100644 --- a/include/hueplusplus/Sensor.h +++ b/include/hueplusplus/Sensor.h @@ -65,7 +65,8 @@ public: //! \param id Integer that specifies the id of this sensor //! \param commands HueCommandAPI for communication with the bridge //! \param refreshDuration Time between refreshing the cached state. - Sensor(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration); + //! \param currentState The current state, may be null. + Sensor(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration, const nlohmann::json& currentState); //!\name Config attributes ///@{ diff --git a/src/APICache.cpp b/src/APICache.cpp index 347419c..1983a3d 100644 --- a/src/APICache.cpp +++ b/src/APICache.cpp @@ -32,11 +32,16 @@ APICache::APICache( commands(baseCache->commands), refreshDuration(refresh), lastRefresh(baseCache->lastRefresh) -{} +{ } -APICache::APICache(const std::string& path, const HueCommandAPI& commands, std::chrono::steady_clock::duration refresh) - : path(path), commands(commands), refreshDuration(refresh), lastRefresh(std::chrono::steady_clock::duration::zero()) -{} +APICache::APICache(const std::string& path, const HueCommandAPI& commands, std::chrono::steady_clock::duration refresh, + const nlohmann::json& initial) + : path(path), + commands(commands), + refreshDuration(refresh), + lastRefresh(initial.is_null() ? std::chrono::steady_clock::time_point() : std::chrono::steady_clock::now()), + value(initial) +{ } void APICache::refresh() { diff --git a/src/BaseDevice.cpp b/src/BaseDevice.cpp index 1a1ff75..02ba3d5 100644 --- a/src/BaseDevice.cpp +++ b/src/BaseDevice.cpp @@ -96,10 +96,11 @@ BaseDevice::BaseDevice(int id, const std::shared_ptr& baseCache) { } BaseDevice::BaseDevice( - int id, const HueCommandAPI& commands, const std::string& path, std::chrono::steady_clock::duration refreshDuration) - : id(id), path(path), state(path + std::to_string(id), commands, refreshDuration) + int id, const HueCommandAPI& commands, const std::string& path, std::chrono::steady_clock::duration refreshDuration, const nlohmann::json& currentState) + : id(id), path(path), state(path + std::to_string(id), commands, refreshDuration, currentState) { - state.refresh(); + // Initialize value if not null + state.getValue(); } nlohmann::json BaseDevice::sendPutRequest(const std::string& subPath, const nlohmann::json& request, FileInfo fileInfo) diff --git a/src/Bridge.cpp b/src/Bridge.cpp index 87cb972..7a561a6 100644 --- a/src/Bridge.cpp +++ b/src/Bridge.cpp @@ -145,7 +145,7 @@ Bridge::Bridge(const std::string& ip, const int port, const std::string& usernam http_handler(std::move(handler)), refreshDuration(refreshDuration), stateCache(std::make_shared( - "", HueCommandAPI(ip, port, username, http_handler), std::chrono::steady_clock::duration::max())), + "", HueCommandAPI(ip, port, username, http_handler), std::chrono::steady_clock::duration::max(), nullptr)), lightList(stateCache, "lights", refreshDuration, sharedState, [factory = LightFactory(stateCache->getCommandAPI(), refreshDuration)]( int id, const nlohmann::json& state, const std::shared_ptr& baseCache) mutable { @@ -306,7 +306,7 @@ const Bridge::RuleList& Bridge::rules() const void Bridge::setHttpHandler(std::shared_ptr handler) { http_handler = handler; - stateCache = std::make_shared("", HueCommandAPI(ip, port, username, handler), refreshDuration); + stateCache = std::make_shared("", HueCommandAPI(ip, port, username, handler), refreshDuration, nullptr); lightList = LightList(stateCache, "lights", refreshDuration,sharedState, [factory = LightFactory(stateCache->getCommandAPI(), refreshDuration)](int id, const nlohmann::json& state, const std::shared_ptr& baseCache) mutable { return factory.createLight(state, id, baseCache); }); diff --git a/src/Group.cpp b/src/Group.cpp index 7573afd..9847e6e 100644 --- a/src/Group.cpp +++ b/src/Group.cpp @@ -8,10 +8,11 @@ Group::Group(int id, const std::shared_ptr& baseCache) : id(id), state(baseCache, std::to_string(id), baseCache->getRefreshDuration()) { } -Group::Group(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration) - : id(id), state("/groups/" + std::to_string(id), commands, refreshDuration) +Group::Group(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration, const nlohmann::json& currentState) + : id(id), state("/groups/" + std::to_string(id), commands, refreshDuration, currentState) { - state.refresh(); + // Initialize value if not null + state.getValue(); } void Group::refresh(bool force) diff --git a/src/HueDeviceTypes.cpp b/src/HueDeviceTypes.cpp index 050fe56..c43e31e 100644 --- a/src/HueDeviceTypes.cpp +++ b/src/HueDeviceTypes.cpp @@ -75,7 +75,7 @@ Light LightFactory::createLight(const nlohmann::json& lightState, int id, const // Ignore case std::transform(type.begin(), type.end(), type.begin(), [](char c) { return std::tolower(c); }); - Light light = baseCache ? Light(id, baseCache) : Light(id, commands, nullptr, nullptr, nullptr, refreshDuration); + Light light = baseCache ? Light(id, baseCache) : Light(id, commands, nullptr, nullptr, nullptr, refreshDuration, lightState); if (type == "on/off light" || type == "on/off plug-in unit") { diff --git a/src/Light.cpp b/src/Light.cpp index 49bf570..353d06d 100644 --- a/src/Light.cpp +++ b/src/Light.cpp @@ -112,15 +112,18 @@ StateTransaction Light::transaction() state.getCommandAPI(), "/lights/" + std::to_string(id) + "/state", &state.getValue().at("state")); } -Light::Light(int id, const HueCommandAPI& commands) : Light(id, commands, nullptr, nullptr, nullptr) { } +Light::Light(int id, const HueCommandAPI& commands) + : Light(id, commands, nullptr, nullptr, nullptr, std::chrono::seconds(10), nullptr) +{ } Light::Light(int id, const std::shared_ptr& baseCache) : BaseDevice(id, baseCache), colorType(ColorType::NONE) { } Light::Light(int id, const HueCommandAPI& commands, std::shared_ptr brightnessStrategy, std::shared_ptr colorTempStrategy, - std::shared_ptr colorHueStrategy, std::chrono::steady_clock::duration refreshDuration) - : BaseDevice(id, commands, "/lights/", refreshDuration), + std::shared_ptr colorHueStrategy, std::chrono::steady_clock::duration refreshDuration, + const nlohmann::json& currentState) + : BaseDevice(id, commands, "/lights/", refreshDuration, currentState), colorType(ColorType::NONE), brightnessStrategy(std::move(brightnessStrategy)), colorTemperatureStrategy(std::move(colorTempStrategy)), diff --git a/src/Rule.cpp b/src/Rule.cpp index 5a50489..f81e318 100644 --- a/src/Rule.cpp +++ b/src/Rule.cpp @@ -130,10 +130,10 @@ 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) - : id(id), state("/rules/" + std::to_string(id), commands, refreshDuration) +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) { - state.refresh(); + refresh(); } void Rule::refresh(bool force) diff --git a/src/Scene.cpp b/src/Scene.cpp index 1328f1c..c5fac14 100644 --- a/src/Scene.cpp +++ b/src/Scene.cpp @@ -150,8 +150,8 @@ LightState LightStateBuilder::create() return LightState(state); } -Scene::Scene(const std::string& id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration) - : id(id), state("/scenes/" + id, commands, refreshDuration) +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 2e25518..888a281 100644 --- a/src/Schedule.cpp +++ b/src/Schedule.cpp @@ -24,8 +24,8 @@ namespace hueplusplus { -Schedule::Schedule(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration) - : id(id), state("/schedules/" + std::to_string(id), commands, refreshDuration) +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(); } diff --git a/src/Sensor.cpp b/src/Sensor.cpp index 30d1195..4c90e9d 100644 --- a/src/Sensor.cpp +++ b/src/Sensor.cpp @@ -235,8 +235,8 @@ Sensor::Sensor(int id, const std::shared_ptr& baseCache) { } -Sensor::Sensor(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration) - : BaseDevice(id, commands, "/sensors/", refreshDuration) +Sensor::Sensor(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration, const nlohmann::json& currentState) + : BaseDevice(id, commands, "/sensors/", refreshDuration, currentState) { } CreateSensor::CreateSensor(const std::string& name, const std::string& modelid, const std::string& swversion, diff --git a/test/mocks/mock_Light.h b/test/mocks/mock_Light.h index b532510..ca8b01e 100644 --- a/test/mocks/mock_Light.h +++ b/test/mocks/mock_Light.h @@ -38,7 +38,7 @@ class MockLight : public hueplusplus::Light public: MockLight(std::shared_ptr handler) : Light(1, hueplusplus::HueCommandAPI(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler), nullptr, - nullptr, nullptr, std::chrono::steady_clock::duration::max()) + nullptr, nullptr, std::chrono::steady_clock::duration::max(), nullptr) { // Set refresh duration to max, so random refreshes do not hinder the test setups } diff --git a/test/test_APICache.cpp b/test/test_APICache.cpp index 199e4a8..b6ef77d 100644 --- a/test/test_APICache.cpp +++ b/test/test_APICache.cpp @@ -35,23 +35,23 @@ TEST(APICache, getRefreshDuration) HueCommandAPI commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler); { std::chrono::steady_clock::duration refresh = std::chrono::seconds(20); - APICache cache("", commands, refresh); + APICache cache("", commands, refresh, nullptr); EXPECT_EQ(refresh, cache.getRefreshDuration()); } { std::chrono::steady_clock::duration refresh = std::chrono::seconds(0); - APICache cache("", commands, refresh); + APICache cache("", commands, refresh, nullptr); EXPECT_EQ(refresh, cache.getRefreshDuration()); } { std::chrono::steady_clock::duration refresh = std::chrono::steady_clock::duration::max(); - APICache cache("", commands, refresh); + APICache cache("", commands, refresh, nullptr); EXPECT_EQ(refresh, cache.getRefreshDuration()); } // With base cache, still independent duration { auto duration = std::chrono::seconds(5); - auto baseCache = std::make_shared("/test", commands, std::chrono::seconds(0)); + auto baseCache = std::make_shared("/test", commands, std::chrono::seconds(0), nullptr); APICache c(baseCache, "api", duration); EXPECT_EQ(duration, c.getRefreshDuration()); } @@ -65,7 +65,7 @@ TEST(APICache, refresh) { std::string path = "/test/abc"; - APICache cache(path, commands, std::chrono::seconds(10)); + APICache cache(path, commands, std::chrono::seconds(10), nullptr); EXPECT_CALL(*handler, GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort())) .WillOnce(Return(nlohmann::json::object())); @@ -74,7 +74,7 @@ TEST(APICache, refresh) } { std::string path = ""; - APICache cache(path, commands, std::chrono::seconds(10)); + APICache cache(path, commands, std::chrono::seconds(10), nullptr); EXPECT_CALL(*handler, GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort())) .Times(2) @@ -94,7 +94,8 @@ TEST(APICache, refreshBase) const std::string childPath = "/test/abc"; // Base cache with max duration { - auto baseCache = std::make_shared(basePath, commands, std::chrono::steady_clock::duration::max()); + auto baseCache + = std::make_shared(basePath, commands, std::chrono::steady_clock::duration::max(), nullptr); APICache cache(baseCache, "abc", std::chrono::seconds(0)); // First call refreshes base, second call only child @@ -112,7 +113,7 @@ TEST(APICache, refreshBase) } // Base cache with min duration { - auto baseCache = std::make_shared(basePath, commands, std::chrono::seconds(0)); + auto baseCache = std::make_shared(basePath, commands, std::chrono::seconds(0), nullptr); APICache cache(baseCache, "abc", std::chrono::seconds(0)); // Both calls refresh base @@ -135,7 +136,7 @@ TEST(APICache, getValue) // Always refresh { std::string path = "/test/abc"; - APICache cache(path, commands, std::chrono::seconds(0)); + APICache cache(path, commands, std::chrono::seconds(0), nullptr); nlohmann::json value = {{"a", "b"}}; EXPECT_CALL(*handler, GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort())) @@ -148,7 +149,7 @@ TEST(APICache, getValue) // Only refresh once { std::string path = "/test/abc"; - APICache cache(path, commands, std::chrono::steady_clock::duration::max()); + APICache cache(path, commands, std::chrono::steady_clock::duration::max(), nullptr); nlohmann::json value = {{"a", "b"}}; EXPECT_CALL(*handler, GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort())) @@ -160,7 +161,7 @@ TEST(APICache, getValue) // Only refresh once { std::string path = "/test/abc"; - APICache cache(path, commands, std::chrono::seconds(0)); + APICache cache(path, commands, std::chrono::seconds(0), nullptr); nlohmann::json value = {{"a", "b"}}; EXPECT_CALL(*handler, GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort())) @@ -172,7 +173,7 @@ TEST(APICache, getValue) // No refresh with const throws exception { std::string path = "/test/abc"; - const APICache cache(path, commands, std::chrono::steady_clock::duration::max()); + const APICache cache(path, commands, std::chrono::steady_clock::duration::max(), nullptr); nlohmann::json value = {{"a", "b"}}; EXPECT_CALL(*handler, GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort())) @@ -180,6 +181,29 @@ TEST(APICache, getValue) EXPECT_THROW(cache.getValue(), HueException); Mock::VerifyAndClearExpectations(handler.get()); } + // No refresh with initial value + { + std::string path = "/test/abc"; + nlohmann::json value = {{"a", "b"}}; + APICache cache(path, commands, std::chrono::steady_clock::duration::max(), value); + EXPECT_CALL(*handler, + GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort())) + .Times(0); + EXPECT_EQ(value, cache.getValue()); + EXPECT_EQ(value, cache.getValue()); + Mock::VerifyAndClearExpectations(handler.get()); + } + // No refresh with const but initial value + { + std::string path = "/test/abc"; + nlohmann::json value = {{"a", "b"}}; + const APICache cache(path, commands, std::chrono::steady_clock::duration::max(), value); + EXPECT_CALL(*handler, + GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort())) + .Times(0); + EXPECT_EQ(value, cache.getValue()); + Mock::VerifyAndClearExpectations(handler.get()); + } } TEST(APICache, getValueBase) @@ -194,7 +218,7 @@ TEST(APICache, getValueBase) const nlohmann::json baseValue = {{"abc", childValue}}; // Always refresh base { - auto baseCache = std::make_shared(basePath, commands, std::chrono::seconds(0)); + auto baseCache = std::make_shared(basePath, commands, std::chrono::seconds(0), nullptr); APICache cache(baseCache, "abc", std::chrono::seconds(0)); EXPECT_CALL(*handler, GETJson("/api/" + getBridgeUsername() + basePath, nlohmann::json::object(), getBridgeIp(), getBridgePort())) @@ -206,7 +230,7 @@ TEST(APICache, getValueBase) } // Child duration > base duration { - auto baseCache = std::make_shared(basePath, commands, std::chrono::seconds(0)); + auto baseCache = std::make_shared(basePath, commands, std::chrono::seconds(0), nullptr); APICache cache(baseCache, "abc", std::chrono::steady_clock::duration::max()); EXPECT_CALL(*handler, GETJson("/api/" + getBridgeUsername() + basePath, nlohmann::json::object(), getBridgeIp(), getBridgePort())) @@ -218,16 +242,18 @@ TEST(APICache, getValueBase) } // Child duration < base duration { - auto baseCache = std::make_shared(basePath, commands, std::chrono::steady_clock::duration::max()); + auto baseCache + = std::make_shared(basePath, commands, std::chrono::steady_clock::duration::max(), nullptr); APICache cache(baseCache, "abc", std::chrono::seconds(0)); - const nlohmann::json updateChildValue = { {"test", "updated"} }; + const nlohmann::json updateChildValue = {{"test", "updated"}}; InSequence s; EXPECT_CALL(*handler, GETJson("/api/" + getBridgeUsername() + basePath, nlohmann::json::object(), getBridgeIp(), getBridgePort())) .Times(1) .WillRepeatedly(Return(baseValue)); EXPECT_CALL(*handler, - GETJson("/api/" + getBridgeUsername() + childPath, nlohmann::json::object(), getBridgeIp(), getBridgePort())) + GETJson( + "/api/" + getBridgeUsername() + childPath, nlohmann::json::object(), getBridgeIp(), getBridgePort())) .Times(1) .WillRepeatedly(Return(updateChildValue)); EXPECT_EQ(childValue, cache.getValue()); @@ -238,7 +264,8 @@ TEST(APICache, getValueBase) } // Only refresh once { - auto baseCache = std::make_shared(basePath, commands, std::chrono::steady_clock::duration::max()); + auto baseCache + = std::make_shared(basePath, commands, std::chrono::steady_clock::duration::max(), nullptr); APICache cache(baseCache, "abc", std::chrono::steady_clock::duration::max()); EXPECT_CALL(*handler, GETJson("/api/" + getBridgeUsername() + basePath, nlohmann::json::object(), getBridgeIp(), getBridgePort())) @@ -259,12 +286,12 @@ TEST(APICache, getRequestPath) // No base cache { std::string path = "/test/api"; - APICache c(path, commands, std::chrono::seconds(0)); + APICache c(path, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(path, c.getRequestPath()); } // With base cache { - auto baseCache = std::make_shared("/test", commands, std::chrono::seconds(0)); + auto baseCache = std::make_shared("/test", commands, std::chrono::seconds(0), nullptr); APICache c(baseCache, "api", std::chrono::seconds(0)); EXPECT_EQ("/test/api", c.getRequestPath()); } diff --git a/test/test_BaseDevice.cpp b/test/test_BaseDevice.cpp index d43bdd1..dc82d59 100644 --- a/test/test_BaseDevice.cpp +++ b/test/test_BaseDevice.cpp @@ -34,8 +34,8 @@ class TestDevice : public BaseDevice { public: TestDevice(int id, const HueCommandAPI& commands, const std::string& path, - std::chrono::steady_clock::duration refreshDuration) - : BaseDevice(id, commands, path, refreshDuration) + std::chrono::steady_clock::duration refreshDuration, const nlohmann::json& currentState) + : BaseDevice(id, commands, path, refreshDuration, currentState) { } }; @@ -61,7 +61,7 @@ protected: EXPECT_CALL(*handler, GETJson("/api/" + getBridgeUsername() + path + std::to_string(id), _, getBridgeIp(), getBridgePort())) .WillOnce(Return(state)); - return TestDevice(id, commands, path, std::chrono::steady_clock::duration::max()); + return TestDevice(id, commands, path, std::chrono::steady_clock::duration::max(), nullptr); } }; diff --git a/test/test_Bridge.cpp b/test/test_Bridge.cpp index 2bbdf71..6e9980e 100644 --- a/test/test_Bridge.cpp +++ b/test/test_Bridge.cpp @@ -265,12 +265,7 @@ TEST(Bridge, getLight) *handler, GETJson("/api/" + getBridgeUsername(), nlohmann::json::object(), getBridgeIp(), getBridgePort())) .Times(1) .WillOnce(Return(hue_bridge_state)); - - EXPECT_CALL(*handler, - GETJson("/api/" + getBridgeUsername() + "/lights/1", nlohmann::json::object(), getBridgeIp(), getBridgePort())) - .Times(AtLeast(1)) - .WillRepeatedly(Return(hue_bridge_state["lights"]["1"])); - + // Refresh cache test_bridge = Bridge(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler); @@ -305,11 +300,6 @@ TEST(Bridge, removeLight) Bridge test_bridge(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler); - EXPECT_CALL(*handler, - GETJson("/api/" + getBridgeUsername() + "/lights/1", nlohmann::json::object(), getBridgeIp(), getBridgePort())) - .Times(1) - .WillRepeatedly(Return(hue_bridge_state["lights"]["1"])); - nlohmann::json return_answer; return_answer = nlohmann::json::array(); return_answer[0] = nlohmann::json::object(); @@ -347,17 +337,12 @@ TEST(Bridge, getAllLights) .Times(AtLeast(1)) .WillRepeatedly(Return(hue_bridge_state)); - EXPECT_CALL(*handler, - GETJson("/api/" + getBridgeUsername() + "/lights/1", nlohmann::json::object(), getBridgeIp(), getBridgePort())) - .Times(AtLeast(1)) - .WillRepeatedly(Return(hue_bridge_state["lights"]["1"])); - Bridge test_bridge(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler); - std::vector> test_lights = test_bridge.lights().getAll(); + std::vector test_lights = test_bridge.lights().getAll(); ASSERT_EQ(1, test_lights.size()); - EXPECT_EQ(test_lights[0].get().getName(), "Hue ambiance lamp 1"); - EXPECT_EQ(test_lights[0].get().getColorType(), ColorType::TEMPERATURE); + EXPECT_EQ(test_lights[0].getName(), "Hue ambiance lamp 1"); + EXPECT_EQ(test_lights[0].getColorType(), ColorType::TEMPERATURE); } TEST(Bridge, lightExists) @@ -411,10 +396,6 @@ TEST(Bridge, getGroup) .Times(1) .WillOnce(Return(hue_bridge_state)); - EXPECT_CALL(*handler, - GETJson("/api/" + getBridgeUsername() + "/groups/1", nlohmann::json::object(), getBridgeIp(), getBridgePort())) - .Times(AtLeast(1)) - .WillRepeatedly(Return(hue_bridge_state["groups"]["1"])); // Refresh cache test_bridge = Bridge(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler); @@ -448,11 +429,6 @@ TEST(Bridge, removeGroup) Bridge test_bridge(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler); - EXPECT_CALL(*handler, - GETJson("/api/" + getBridgeUsername() + "/groups/1", nlohmann::json::object(), getBridgeIp(), getBridgePort())) - .Times(1) - .WillRepeatedly(Return(hue_bridge_state["groups"]["1"])); - nlohmann::json return_answer; return_answer = nlohmann::json::array(); return_answer[0] = nlohmann::json::object(); @@ -513,17 +489,12 @@ TEST(Bridge, getAllGroups) .Times(AtLeast(1)) .WillRepeatedly(Return(hue_bridge_state)); - EXPECT_CALL(*handler, - GETJson("/api/" + getBridgeUsername() + "/groups/1", nlohmann::json::object(), getBridgeIp(), getBridgePort())) - .Times(AtLeast(1)) - .WillRepeatedly(Return(hue_bridge_state["groups"]["1"])); - Bridge test_bridge(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler); - std::vector> test_groups = test_bridge.groups().getAll(); + std::vector test_groups = test_bridge.groups().getAll(); ASSERT_EQ(1, test_groups.size()); - EXPECT_EQ(test_groups[0].get().getName(), "Group 1"); - EXPECT_EQ(test_groups[0].get().getType(), "LightGroup"); + EXPECT_EQ(test_groups[0].getName(), "Group 1"); + EXPECT_EQ(test_groups[0].getType(), "LightGroup"); } TEST(Bridge, createGroup) diff --git a/test/test_BridgeConfig.cpp b/test/test_BridgeConfig.cpp index dd4568b..dce46fa 100644 --- a/test/test_BridgeConfig.cpp +++ b/test/test_BridgeConfig.cpp @@ -34,7 +34,7 @@ TEST(BridgeConfig, refresh) { auto handler = std::make_shared(); HueCommandAPI commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler); - auto baseCache = std::make_shared("", commands, std::chrono::steady_clock::duration::max()); + auto baseCache = std::make_shared("", commands, std::chrono::steady_clock::duration::max(), nullptr); EXPECT_CALL( *handler, GETJson("/api/" + getBridgeUsername(), nlohmann::json::object(), getBridgeIp(), getBridgePort())) .WillOnce(Return(nlohmann::json::object())); @@ -59,7 +59,7 @@ TEST(BridgeConfig, getWhitelistedUsers) {"create date", "2020-02-01T02:03:40"}}}}}}}}; auto handler = std::make_shared(); HueCommandAPI commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler); - auto baseCache = std::make_shared("", commands, std::chrono::steady_clock::duration::max()); + auto baseCache = std::make_shared("", commands, std::chrono::steady_clock::duration::max(), nullptr); EXPECT_CALL( *handler, GETJson("/api/" + getBridgeUsername(), nlohmann::json::object(), getBridgeIp(), getBridgePort())) .WillOnce(Return(state)); @@ -76,7 +76,7 @@ TEST(BridgeConfig, removeUser) { auto handler = std::make_shared(); HueCommandAPI commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler); - auto baseCache = std::make_shared("", commands, std::chrono::steady_clock::duration::max()); + auto baseCache = std::make_shared("", commands, std::chrono::steady_clock::duration::max(), nullptr); EXPECT_CALL( *handler, GETJson("/api/" + getBridgeUsername(), nlohmann::json::object(), getBridgeIp(), getBridgePort())) .WillOnce(Return(nlohmann::json::object())); @@ -96,7 +96,7 @@ TEST(BridgeConfig, getLinkButton) const nlohmann::json state {{"config", {{"linkbutton", true}}}}; auto handler = std::make_shared(); HueCommandAPI commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler); - auto baseCache = std::make_shared("", commands, std::chrono::steady_clock::duration::max()); + auto baseCache = std::make_shared("", commands, std::chrono::steady_clock::duration::max(), nullptr); EXPECT_CALL( *handler, GETJson("/api/" + getBridgeUsername(), nlohmann::json::object(), getBridgeIp(), getBridgePort())) .WillOnce(Return(state)); @@ -115,7 +115,7 @@ TEST(BridgeConfig, pressLinkButton) { auto handler = std::make_shared(); HueCommandAPI commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler); - auto baseCache = std::make_shared("", commands, std::chrono::steady_clock::duration::max()); + auto baseCache = std::make_shared("", commands, std::chrono::steady_clock::duration::max(), nullptr); EXPECT_CALL( *handler, GETJson("/api/" + getBridgeUsername(), nlohmann::json::object(), getBridgeIp(), getBridgePort())) .WillOnce(Return(nlohmann::json::object())); @@ -133,7 +133,7 @@ TEST(BridgeConfig, touchLink) { auto handler = std::make_shared(); HueCommandAPI commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler); - auto baseCache = std::make_shared("", commands, std::chrono::steady_clock::duration::max()); + auto baseCache = std::make_shared("", commands, std::chrono::steady_clock::duration::max(), nullptr); EXPECT_CALL( *handler, GETJson("/api/" + getBridgeUsername(), nlohmann::json::object(), getBridgeIp(), getBridgePort())) .WillOnce(Return(nlohmann::json::object())); @@ -152,7 +152,7 @@ TEST(BridgeConfig, getMACAddress) const nlohmann::json state {{"config", {{"mac", getBridgeMac()}}}}; auto handler = std::make_shared(); HueCommandAPI commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler); - auto baseCache = std::make_shared("", commands, std::chrono::steady_clock::duration::max()); + auto baseCache = std::make_shared("", commands, std::chrono::steady_clock::duration::max(), nullptr); EXPECT_CALL( *handler, GETJson("/api/" + getBridgeUsername(), nlohmann::json::object(), getBridgeIp(), getBridgePort())) .WillOnce(Return(state)); @@ -168,7 +168,7 @@ TEST(BridgeConfig, getUTCTime) const nlohmann::json state {{"config", {{"UTC", utc}}}}; auto handler = std::make_shared(); HueCommandAPI commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler); - auto baseCache = std::make_shared("", commands, std::chrono::steady_clock::duration::max()); + auto baseCache = std::make_shared("", commands, std::chrono::steady_clock::duration::max(), nullptr); EXPECT_CALL( *handler, GETJson("/api/" + getBridgeUsername(), nlohmann::json::object(), getBridgeIp(), getBridgePort())) .WillOnce(Return(state)); @@ -184,7 +184,7 @@ TEST(BridgeConfig, getTimezone) const nlohmann::json state {{"config", {{"timezone", timezone}}}}; auto handler = std::make_shared(); HueCommandAPI commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler); - auto baseCache = std::make_shared("", commands, std::chrono::steady_clock::duration::max()); + auto baseCache = std::make_shared("", commands, std::chrono::steady_clock::duration::max(), nullptr); EXPECT_CALL( *handler, GETJson("/api/" + getBridgeUsername(), nlohmann::json::object(), getBridgeIp(), getBridgePort())) .WillOnce(Return(state)); diff --git a/test/test_Group.cpp b/test/test_Group.cpp index 5d69bab..4bd43ea 100644 --- a/test/test_Group.cpp +++ b/test/test_Group.cpp @@ -78,14 +78,14 @@ TEST_F(GroupTest, Construtor) { const int id = 12; expectGetState(id); - Group group(id, commands, std::chrono::seconds(0)); + Group group(id, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(id, group.getId()); Mock::VerifyAndClearExpectations(handler.get()); } { const int id = 0; expectGetState(id); - Group group(id, commands, std::chrono::seconds(0)); + Group group(id, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(id, group.getId()); Mock::VerifyAndClearExpectations(handler.get()); } @@ -95,7 +95,7 @@ TEST_F(GroupTest, getName) { const int id = 1; expectGetState(id); - Group group(id, commands, std::chrono::seconds(0)); + Group group(id, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(groupName, Const(group).getName()); } @@ -103,7 +103,7 @@ TEST_F(GroupTest, getType) { const int id = 1; expectGetState(id); - Group group(id, commands, std::chrono::seconds(0)); + Group group(id, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(type, Const(group).getType()); } @@ -111,7 +111,7 @@ TEST_F(GroupTest, getLightIds) { const int id = 1; expectGetState(id); - Group group(id, commands, std::chrono::seconds(0)); + Group group(id, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(std::vector({1, 2, 4}), Const(group).getLightIds()); } @@ -119,7 +119,7 @@ TEST_F(GroupTest, getRoomType) { const int id = 1; expectGetState(id); - Group group(id, commands, std::chrono::seconds(0)); + Group group(id, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(roomType, Const(group).getRoomType()); } @@ -127,7 +127,7 @@ TEST_F(GroupTest, getAllOn) { const int id = 1; expectGetState(id); - Group group(id, commands, std::chrono::steady_clock::duration::max()); + Group group(id, commands, std::chrono::steady_clock::duration::max(), nullptr); EXPECT_EQ(all_on, group.getAllOn()); EXPECT_EQ(all_on, Const(group).getAllOn()); } @@ -136,7 +136,7 @@ TEST_F(GroupTest, getAnyOn) { const int id = 1; expectGetState(id); - Group group(id, commands, std::chrono::steady_clock::duration::max()); + Group group(id, commands, std::chrono::steady_clock::duration::max(), nullptr); EXPECT_EQ(any_on, group.getAnyOn()); EXPECT_EQ(any_on, Const(group).getAnyOn()); } @@ -145,7 +145,7 @@ TEST_F(GroupTest, getActionOn) { const int id = 1; expectGetState(id); - Group group(id, commands, std::chrono::steady_clock::duration::max()); + Group group(id, commands, std::chrono::steady_clock::duration::max(), nullptr); EXPECT_EQ(on, group.getActionOn()); EXPECT_EQ(on, Const(group).getActionOn()); } @@ -154,7 +154,7 @@ TEST_F(GroupTest, getActionHueSaturation) { const int id = 1; expectGetState(id); - Group group(id, commands, std::chrono::steady_clock::duration::max()); + Group group(id, commands, std::chrono::steady_clock::duration::max(), nullptr); std::pair hueSat {hue, sat}; EXPECT_EQ(hueSat, group.getActionHueSaturation()); EXPECT_EQ(hueSat, Const(group).getActionHueSaturation()); @@ -164,7 +164,7 @@ TEST_F(GroupTest, getActionBrightness) { const int id = 1; expectGetState(id); - Group group(id, commands, std::chrono::steady_clock::duration::max()); + Group group(id, commands, std::chrono::steady_clock::duration::max(), nullptr); EXPECT_EQ(bri, group.getActionBrightness()); EXPECT_EQ(bri, Const(group).getActionBrightness()); } @@ -173,7 +173,7 @@ TEST_F(GroupTest, getActionColorTemperature) { const int id = 1; expectGetState(id); - Group group(id, commands, std::chrono::steady_clock::duration::max()); + Group group(id, commands, std::chrono::steady_clock::duration::max(), nullptr); EXPECT_EQ(ct, group.getActionColorTemperature()); EXPECT_EQ(ct, Const(group).getActionColorTemperature()); } @@ -182,7 +182,7 @@ TEST_F(GroupTest, getActionColorXY) { const int id = 1; expectGetState(id); - Group group(id, commands, std::chrono::steady_clock::duration::max()); + Group group(id, commands, std::chrono::steady_clock::duration::max(), nullptr); std::pair xy {x, y}; EXPECT_EQ(xy, group.getActionColorXY()); EXPECT_EQ(xy, Const(group).getActionColorXY()); @@ -192,7 +192,7 @@ TEST_F(GroupTest, getActionColorMode) { const int id = 1; expectGetState(id); - Group group(id, commands, std::chrono::steady_clock::duration::max()); + Group group(id, commands, std::chrono::steady_clock::duration::max(), nullptr); EXPECT_EQ(colormode, group.getActionColorMode()); EXPECT_EQ(colormode, Const(group).getActionColorMode()); } @@ -201,7 +201,7 @@ TEST_F(GroupTest, setName) { const int id = 1; expectGetState(id); - Group group(id, commands, std::chrono::steady_clock::duration::max()); + Group group(id, commands, std::chrono::steady_clock::duration::max(), nullptr); const std::string name = "Test group"; nlohmann::json request = {{"name", name}}; nlohmann::json response = {{"success", {"/groups/1/name", name}}}; @@ -215,7 +215,7 @@ TEST_F(GroupTest, setLights) { const int id = 1; expectGetState(id); - Group group(id, commands, std::chrono::steady_clock::duration::max()); + Group group(id, commands, std::chrono::steady_clock::duration::max(), nullptr); const nlohmann::json lights = {"2", "4", "5"}; nlohmann::json request = {{"lights", lights}}; nlohmann::json response = {{"success", {"/groups/1/lights", lights}}}; @@ -229,7 +229,7 @@ TEST_F(GroupTest, setRoomType) { const int id = 1; expectGetState(id); - Group group(id, commands, std::chrono::steady_clock::duration::max()); + Group group(id, commands, std::chrono::steady_clock::duration::max(), nullptr); const std::string type = "LivingRoom"; nlohmann::json request = {{"class", type}}; nlohmann::json response = {{"success", {"/groups/1/class", type}}}; @@ -243,7 +243,7 @@ TEST_F(GroupTest, setScene) { const int id = 1; expectGetState(id); - Group group(id, commands, std::chrono::steady_clock::duration::max()); + Group group(id, commands, std::chrono::steady_clock::duration::max(), nullptr); const std::string scene = "testScene"; nlohmann::json request = {{"scene", scene}}; nlohmann::json response = {{"success", {"/groups/1/action/scene", scene}}}; @@ -257,7 +257,7 @@ TEST_F(GroupTest, createSceneAction) { const int id = 1; expectGetState(id); - const Group group(id, commands, std::chrono::steady_clock::duration::max()); + const Group group(id, commands, std::chrono::steady_clock::duration::max(), nullptr); const std::string scene = "testScene"; nlohmann::json request = { {"scene", scene} }; hueplusplus::Action command = group.createSceneAction(scene); diff --git a/test/test_Light.cpp b/test/test_Light.cpp index f344969..614699c 100644 --- a/test/test_Light.cpp +++ b/test/test_Light.cpp @@ -75,18 +75,6 @@ protected: EXPECT_CALL(*handler, GETJson("/api/" + getBridgeUsername(), nlohmann::json::object(), getBridgeIp(), 80)) .Times(AtLeast(1)) .WillRepeatedly(Return(hue_bridge_state)); - EXPECT_CALL( - *handler, GETJson("/api/" + getBridgeUsername() + "/lights/1", nlohmann::json::object(), getBridgeIp(), 80)) - .Times(AtLeast(1)) - .WillRepeatedly(Return(hue_bridge_state["lights"]["1"])); - EXPECT_CALL( - *handler, GETJson("/api/" + getBridgeUsername() + "/lights/2", nlohmann::json::object(), getBridgeIp(), 80)) - .Times(AtLeast(1)) - .WillRepeatedly(Return(hue_bridge_state["lights"]["2"])); - EXPECT_CALL( - *handler, GETJson("/api/" + getBridgeUsername() + "/lights/3", nlohmann::json::object(), getBridgeIp(), 80)) - .Times(AtLeast(1)) - .WillRepeatedly(Return(hue_bridge_state["lights"]["3"])); } ~HueLightTest() {}; }; diff --git a/test/test_LightFactory.cpp b/test/test_LightFactory.cpp index 22fc00f..6b985d7 100644 --- a/test/test_LightFactory.cpp +++ b/test/test_LightFactory.cpp @@ -44,31 +44,16 @@ TEST(LightFactory, createLight_noGamut) {"name", "Hue ambiance lamp 1"}, {"modelid", "LTW001"}, {"manufacturername", "Philips"}, {"uniqueid", "00:00:00:00:00:00:00:00-00"}, {"swversion", "5.50.1.19085"}}; - EXPECT_CALL(*handler, - GETJson("/api/" + getBridgeUsername() + "/lights/1", nlohmann::json::object(), getBridgeIp(), getBridgePort())) - .Times(AtLeast(1)) - .WillRepeatedly(Return(lightState)); - Light test_light_1 = factory.createLight(lightState, 1); EXPECT_EQ(test_light_1.getColorType(), ColorType::TEMPERATURE); lightState["type"] = "Dimmable light"; - EXPECT_CALL(*handler, - GETJson("/api/" + getBridgeUsername() + "/lights/1", nlohmann::json::object(), getBridgeIp(), getBridgePort())) - .Times(AtLeast(1)) - .WillRepeatedly(Return(lightState)); - test_light_1 = factory.createLight(lightState, 1); EXPECT_EQ(test_light_1.getColorType(), ColorType::NONE); lightState["type"] = "On/Off light"; - EXPECT_CALL(*handler, - GETJson("/api/" + getBridgeUsername() + "/lights/1", nlohmann::json::object(), getBridgeIp(), getBridgePort())) - .Times(AtLeast(1)) - .WillRepeatedly(Return(lightState)); - test_light_1 = factory.createLight(lightState, 1); EXPECT_EQ(test_light_1.getColorType(), ColorType::NONE); @@ -92,69 +77,37 @@ TEST(LightFactory, createLight_gamutCapabilities) {"uniqueid", "00:00:00:00:00:00:00:00-00"}, {"swversion", "5.50.1.19085"}, {"capabilities", {{"control", {{"colorgamuttype", "A"}}}}} }; - EXPECT_CALL(*handler, - GETJson("/api/" + getBridgeUsername() + "/lights/1", nlohmann::json::object(), getBridgeIp(), getBridgePort())) - .Times(AtLeast(1)) - .WillRepeatedly(Return(lightState)); - Light test_light_1 = factory.createLight(lightState, 1); EXPECT_EQ(test_light_1.getColorType(), ColorType::GAMUT_A); lightState["capabilities"]["control"]["colorgamuttype"] = "B"; - EXPECT_CALL(*handler, - GETJson("/api/" + getBridgeUsername() + "/lights/1", nlohmann::json::object(), getBridgeIp(), getBridgePort())) - .Times(AtLeast(1)) - .WillRepeatedly(Return(lightState)); - test_light_1 = factory.createLight(lightState, 1); EXPECT_EQ(test_light_1.getColorType(), ColorType::GAMUT_B); lightState["capabilities"]["control"]["colorgamuttype"] = "C"; - EXPECT_CALL(*handler, - GETJson("/api/" + getBridgeUsername() + "/lights/1", nlohmann::json::object(), getBridgeIp(), getBridgePort())) - .Times(AtLeast(1)) - .WillRepeatedly(Return(lightState)); - test_light_1 = factory.createLight(lightState, 1); EXPECT_EQ(test_light_1.getColorType(), ColorType::GAMUT_C); lightState["capabilities"]["control"]["colorgamuttype"] = "Other"; - EXPECT_CALL(*handler, - GETJson("/api/" + getBridgeUsername() + "/lights/1", nlohmann::json::object(), getBridgeIp(), getBridgePort())) - .Times(AtLeast(1)) - .WillRepeatedly(Return(lightState)); - test_light_1 = factory.createLight(lightState, 1); EXPECT_EQ(test_light_1.getColorType(), ColorType::UNDEFINED); // With color temperature lightState["type"] = "Extended color light"; lightState["capabilities"]["control"]["colorgamuttype"] = "A"; - EXPECT_CALL(*handler, - GETJson("/api/" + getBridgeUsername() + "/lights/1", nlohmann::json::object(), getBridgeIp(), getBridgePort())) - .Times(AtLeast(1)) - .WillRepeatedly(Return(lightState)); test_light_1 = factory.createLight(lightState, 1); EXPECT_EQ(test_light_1.getColorType(), ColorType::GAMUT_A_TEMPERATURE); lightState["capabilities"]["control"]["colorgamuttype"] = "B"; - EXPECT_CALL(*handler, - GETJson("/api/" + getBridgeUsername() + "/lights/1", nlohmann::json::object(), getBridgeIp(), getBridgePort())) - .Times(AtLeast(1)) - .WillRepeatedly(Return(lightState)); test_light_1 = factory.createLight(lightState, 1); EXPECT_EQ(test_light_1.getColorType(), ColorType::GAMUT_B_TEMPERATURE); lightState["capabilities"]["control"]["colorgamuttype"] = "C"; - EXPECT_CALL(*handler, - GETJson("/api/" + getBridgeUsername() + "/lights/1", nlohmann::json::object(), getBridgeIp(), getBridgePort())) - .Times(AtLeast(1)) - .WillRepeatedly(Return(lightState)); test_light_1 = factory.createLight(lightState, 1); EXPECT_EQ(test_light_1.getColorType(), ColorType::GAMUT_C_TEMPERATURE); @@ -179,30 +132,15 @@ TEST(LightFactory, createLight_gamutModelid) {"name", "Hue ambiance lamp 1"}, {"modelid", gamutAModel}, {"manufacturername", "Philips"}, {"uniqueid", "00:00:00:00:00:00:00:00-00"}, {"swversion", "5.50.1.19085"}}; - EXPECT_CALL(*handler, - GETJson("/api/" + getBridgeUsername() + "/lights/1", nlohmann::json::object(), getBridgeIp(), getBridgePort())) - .Times(AtLeast(1)) - .WillRepeatedly(Return(lightState)); - Light test_light_1 = factory.createLight(lightState, 1); EXPECT_EQ(test_light_1.getColorType(), ColorType::GAMUT_A); lightState["modelid"] = gamutBModel; - EXPECT_CALL(*handler, - GETJson("/api/" + getBridgeUsername() + "/lights/1", nlohmann::json::object(), getBridgeIp(), getBridgePort())) - .Times(AtLeast(1)) - .WillRepeatedly(Return(lightState)); - test_light_1 = factory.createLight(lightState, 1); EXPECT_EQ(test_light_1.getColorType(), ColorType::GAMUT_B); lightState["modelid"] = gamutCModel; - EXPECT_CALL(*handler, - GETJson("/api/" + getBridgeUsername() + "/lights/1", nlohmann::json::object(), getBridgeIp(), getBridgePort())) - .Times(AtLeast(1)) - .WillRepeatedly(Return(lightState)); - test_light_1 = factory.createLight(lightState, 1); EXPECT_EQ(test_light_1.getColorType(), ColorType::GAMUT_C); @@ -210,30 +148,16 @@ TEST(LightFactory, createLight_gamutModelid) lightState["type"] = "Extended color light"; lightState["modelid"] = gamutAModel; - EXPECT_CALL(*handler, - GETJson("/api/" + getBridgeUsername() + "/lights/1", nlohmann::json::object(), getBridgeIp(), getBridgePort())) - .Times(AtLeast(1)) - .WillRepeatedly(Return(lightState)); - test_light_1 = factory.createLight(lightState, 1); EXPECT_EQ(test_light_1.getColorType(), ColorType::GAMUT_A_TEMPERATURE); lightState["modelid"] = gamutBModel; - EXPECT_CALL(*handler, - GETJson("/api/" + getBridgeUsername() + "/lights/1", nlohmann::json::object(), getBridgeIp(), getBridgePort())) - .Times(AtLeast(1)) - .WillRepeatedly(Return(lightState)); test_light_1 = factory.createLight(lightState, 1); EXPECT_EQ(test_light_1.getColorType(), ColorType::GAMUT_B_TEMPERATURE); lightState["modelid"] = gamutCModel; - EXPECT_CALL(*handler, - GETJson("/api/" + getBridgeUsername() + "/lights/1", nlohmann::json::object(), getBridgeIp(), getBridgePort())) - .Times(AtLeast(1)) - .WillRepeatedly(Return(lightState)); - test_light_1 = factory.createLight(lightState, 1); EXPECT_EQ(test_light_1.getColorType(), ColorType::GAMUT_C_TEMPERATURE); diff --git a/test/test_ResourceList.cpp b/test/test_ResourceList.cpp index 7f5e3c0..bb5b42e 100644 --- a/test/test_ResourceList.cpp +++ b/test/test_ResourceList.cpp @@ -33,7 +33,7 @@ class TestResource { public: TestResource(int id, std::shared_ptr baseCache) {} - TestResource(int id, HueCommandAPI api, std::chrono::steady_clock::duration refreshDuration) : id(id) { } + TestResource(int id, HueCommandAPI api, std::chrono::steady_clock::duration refreshDuration, const nlohmann::json& currentState) : id(id) { } void refresh(bool force = false) { } @@ -49,7 +49,7 @@ class TestStringResource { public: TestStringResource(const std::string& id, std::shared_ptr baseCache) {} - TestStringResource(const std::string& id, HueCommandAPI api, std::chrono::steady_clock::duration refreshDuration) + TestStringResource(const std::string& id, HueCommandAPI api, std::chrono::steady_clock::duration refreshDuration, const nlohmann::json& currentState) : id(id) { } void refresh(bool force = false) { } @@ -81,7 +81,7 @@ TEST(ResourceList, refresh) Mock::VerifyAndClearExpectations(handler.get()); } { - auto baseCache = std::make_shared("", commands, std::chrono::steady_clock::duration::max()); + auto baseCache = std::make_shared("", commands, std::chrono::steady_clock::duration::max(), nullptr); ResourceList list(baseCache, "resources", std::chrono::steady_clock::duration::max()); InSequence s; EXPECT_CALL( @@ -113,9 +113,9 @@ TEST(ResourceList, get) GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort())) .WillOnce(Return(response)); - TestResource& r = list.get(id); + TestResource r = list.get(id); EXPECT_EQ(id, r.id); - TestResource& r2 = list.get(id); + TestResource r2 = list.get(id); EXPECT_EQ(id, r2.id); } // With factory @@ -126,7 +126,7 @@ TEST(ResourceList, get) MockFunction&)> factory; EXPECT_CALL(factory, Call(id, state, std::shared_ptr())) - .WillOnce(Return(TestResource(id, commands, std::chrono::steady_clock::duration::max()))); + .WillOnce(Return(TestResource(id, commands, std::chrono::steady_clock::duration::max(), nullptr))); ResourceList list( commands, path, std::chrono::steady_clock::duration::max(), factory.AsStdFunction()); @@ -134,7 +134,7 @@ TEST(ResourceList, get) GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort())) .WillOnce(Return(response)); - TestResource& r = list.get(id); + TestResource r = list.get(id); EXPECT_EQ(id, r.id); } // String id without factory @@ -146,9 +146,9 @@ TEST(ResourceList, get) GETJson("/api/" + getBridgeUsername() + path, nlohmann::json::object(), getBridgeIp(), getBridgePort())) .WillOnce(Return(response)); - TestStringResource& r = list.get(id); + TestStringResource r = list.get(id); EXPECT_EQ(id, r.id); - TestStringResource& r2 = list.get(id); + TestStringResource r2 = list.get(id); EXPECT_EQ(id, r2.id); } diff --git a/test/test_Rule.cpp b/test/test_Rule.cpp index e78d9bd..253acd7 100644 --- a/test/test_Rule.cpp +++ b/test/test_Rule.cpp @@ -99,7 +99,7 @@ protected: Rule getRule(int id = 1) { expectGetState(id); - return Rule(id, commands, std::chrono::steady_clock::duration::max()); + return Rule(id, commands, std::chrono::steady_clock::duration::max(), nullptr); } }; diff --git a/test/test_Scene.cpp b/test/test_Scene.cpp index 46ac93f..6b38971 100644 --- a/test/test_Scene.cpp +++ b/test/test_Scene.cpp @@ -156,7 +156,7 @@ TEST_F(SceneTest, Constructor) { const std::string id = "asd89263"; expectGetState(id); - const Scene scene(id, commands, std::chrono::seconds(0)); + const Scene scene(id, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(id, scene.getId()); } @@ -166,7 +166,7 @@ TEST_F(SceneTest, getName) const std::string name = "Scene name"; sceneState["name"] = name; expectGetState(id); - const Scene scene(id, commands, std::chrono::seconds(0)); + const Scene scene(id, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(name, scene.getName()); } @@ -176,13 +176,13 @@ TEST_F(SceneTest, getType) { sceneState["type"] = "GroupScene"; expectGetState(id); - const Scene scene(id, commands, std::chrono::seconds(0)); + const Scene scene(id, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(Scene::Type::groupScene, scene.getType()); } { sceneState["type"] = "LightScene"; expectGetState(id); - const Scene scene(id, commands, std::chrono::seconds(0)); + const Scene scene(id, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(Scene::Type::lightScene, scene.getType()); } } @@ -193,14 +193,14 @@ TEST_F(SceneTest, getGroupId) { sceneState["group"] = "3"; expectGetState(id); - const Scene scene(id, commands, std::chrono::seconds(0)); + const Scene scene(id, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(3, scene.getGroupId()); } { sceneState["type"] = "LightScene"; sceneState.erase("group"); expectGetState(id); - const Scene scene(id, commands, std::chrono::seconds(0)); + const Scene scene(id, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(0, scene.getGroupId()); } } @@ -210,7 +210,7 @@ TEST_F(SceneTest, getLightIds) const std::string id = "125asav3"; sceneState["lights"] = {"3", "4", "5"}; expectGetState(id); - const Scene scene(id, commands, std::chrono::seconds(0)); + const Scene scene(id, commands, std::chrono::seconds(0), nullptr); EXPECT_THAT(scene.getLightIds(), UnorderedElementsAre(3, 4, 5)); } @@ -220,7 +220,7 @@ TEST_F(SceneTest, getOwner) const std::string owner = "testowner"; sceneState["owner"] = owner; expectGetState(id); - const Scene scene(id, commands, std::chrono::seconds(0)); + const Scene scene(id, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(owner, scene.getOwner()); } @@ -230,7 +230,7 @@ TEST_F(SceneTest, getRecycle) const bool recycle = true; sceneState["recycle"] = recycle; expectGetState(id); - const Scene scene(id, commands, std::chrono::seconds(0)); + const Scene scene(id, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(recycle, scene.getRecycle()); } @@ -240,7 +240,7 @@ TEST_F(SceneTest, isLocked) const bool locked = true; sceneState["locked"] = locked; expectGetState(id); - const Scene scene(id, commands, std::chrono::seconds(0)); + const Scene scene(id, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(locked, scene.isLocked()); } @@ -251,7 +251,7 @@ TEST_F(SceneTest, getAppdata) const int version = 10; sceneState["appdata"] = {{"version", version}, {"data", appdata}}; expectGetState(id); - const Scene scene(id, commands, std::chrono::seconds(0)); + const Scene scene(id, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(version, scene.getAppdataVersion()); EXPECT_EQ(appdata, scene.getAppdata()); } @@ -262,7 +262,7 @@ TEST_F(SceneTest, getPicture) const std::string picture = "abcpicture"; sceneState["picture"] = picture; expectGetState(id); - const Scene scene(id, commands, std::chrono::seconds(0)); + const Scene scene(id, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(picture, scene.getPicture()); } @@ -270,7 +270,7 @@ TEST_F(SceneTest, getLastUpdated) { const std::string id = "125asav3"; expectGetState(id); - const Scene scene(id, commands, std::chrono::seconds(0)); + const Scene scene(id, commands, std::chrono::seconds(0), nullptr); const time::AbsoluteTime lastUpdated = scene.getLastUpdated(); EXPECT_EQ(time::parseUTCTimestamp("2020-04-23T12:00:04"), lastUpdated.getBaseTime()); } @@ -281,7 +281,7 @@ TEST_F(SceneTest, getVersion) const int version = 2; sceneState["version"] = version; expectGetState(id); - const Scene scene(id, commands, std::chrono::seconds(0)); + const Scene scene(id, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(version, scene.getVersion()); } @@ -300,7 +300,7 @@ TEST_F(SceneTest, getLightstates) } sceneState["lightstates"] = lightstatesJson; expectGetState(id); - const Scene scene(id, commands, std::chrono::seconds(0)); + const Scene scene(id, commands, std::chrono::seconds(0), nullptr); const std::map result = scene.getLightStates(); EXPECT_EQ(lightstates, result); } @@ -308,7 +308,7 @@ TEST_F(SceneTest, getLightstates) { sceneState.erase("lightstates"); expectGetState(id); - const Scene scene(id, commands, std::chrono::seconds(0)); + const Scene scene(id, commands, std::chrono::seconds(0), nullptr); EXPECT_TRUE(scene.getLightStates().empty()); } } @@ -317,7 +317,7 @@ TEST_F(SceneTest, refresh) { const std::string id = "125asav3"; expectGetState(id); - Scene scene(id, commands, std::chrono::seconds(0)); + Scene scene(id, commands, std::chrono::seconds(0), nullptr); expectGetState(id); scene.refresh(true); } @@ -326,7 +326,7 @@ TEST_F(SceneTest, setName) { const std::string id = "125asav3"; expectGetState(id); - Scene scene(id, commands, std::chrono::seconds(0)); + Scene scene(id, commands, std::chrono::seconds(0), nullptr); const std::string name = "Scene name"; nlohmann::json request = {{"name", name}}; nlohmann::json response = {{"success", {"/scenes/" + id + "/name", name}}}; @@ -341,7 +341,7 @@ TEST_F(SceneTest, setLightIds) { const std::string id = "125asav3"; expectGetState(id); - Scene scene(id, commands, std::chrono::seconds(0)); + Scene scene(id, commands, std::chrono::seconds(0), nullptr); const std::vector lightIds = {3, 4, 6, 8}; nlohmann::json request = {{"lights", {"3", "4", "6", "8"}}}; nlohmann::json response = {{"success", {"/scenes/" + id + "/lights", {"3", "4", "6", "8"}}}}; @@ -356,7 +356,7 @@ TEST_F(SceneTest, setAppdata) { const std::string id = "125asav3"; expectGetState(id); - Scene scene(id, commands, std::chrono::seconds(0)); + Scene scene(id, commands, std::chrono::seconds(0), nullptr); const std::string appdata = "New appdata"; const int version = 3; nlohmann::json request = {{"appdata", {{"version", version}, {"data", appdata}}}}; @@ -381,7 +381,7 @@ TEST_F(SceneTest, setLightStates) lightstatesJson[std::to_string(entry.first)] = entry.second.toJson(); } expectGetState(id); - Scene scene(id, commands, std::chrono::seconds(0)); + Scene scene(id, commands, std::chrono::seconds(0), nullptr); nlohmann::json request = {{"lightstates", lightstatesJson}}; nlohmann::json response = {{"success", {"/scenes/" + id + "/lights/3/state/on", false}}, {"success", {"/scenes/" + id + "/lights/3/state/bri", 100}}, @@ -400,7 +400,7 @@ TEST_F(SceneTest, storeCurrentLightState) { const std::string id = "125asav3"; expectGetState(id); - Scene scene(id, commands, std::chrono::seconds(0)); + Scene scene(id, commands, std::chrono::seconds(0), nullptr); { nlohmann::json request = {{"storelightstate", true}}; nlohmann::json response = {{"success", {"/scenes/" + id + "/storelightstate", true}}}; @@ -431,7 +431,7 @@ TEST_F(SceneTest, recall) expectGetState(id); nlohmann::json request = {{"scene", id}}; nlohmann::json response = {{"success", {"/groups/0/action/scene", id}}}; - Scene scene(id, commands, std::chrono::seconds(0)); + Scene scene(id, commands, std::chrono::seconds(0), nullptr); EXPECT_CALL(*handler, PUTJson("/api/" + getBridgeUsername() + "/groups/0/action", request, getBridgeIp(), getBridgePort())) .WillOnce(Return(response)); @@ -446,7 +446,7 @@ TEST_F(SceneTest, recall) expectGetState(id); nlohmann::json request = {{"scene", id}}; nlohmann::json response = {{"success", {"/groups/" + groupId + "/action/scene", id}}}; - Scene scene(id, commands, std::chrono::seconds(0)); + Scene scene(id, commands, std::chrono::seconds(0), nullptr); EXPECT_CALL(*handler, PUTJson("/api/" + getBridgeUsername() + "/groups/" + groupId + "/action", request, getBridgeIp(), getBridgePort())) diff --git a/test/test_Schedule.cpp b/test/test_Schedule.cpp index c6cb7b6..4037a70 100644 --- a/test/test_Schedule.cpp +++ b/test/test_Schedule.cpp @@ -59,14 +59,14 @@ TEST_F(ScheduleTest, Constructor) { const int id = 13; expectGetState(id); - const Schedule schedule(id, commands, std::chrono::seconds(0)); + const Schedule schedule(id, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(id, schedule.getId()); Mock::VerifyAndClearExpectations(handler.get()); } { const int id = 0; expectGetState(id); - const Schedule schedule(id, commands, std::chrono::seconds(0)); + const Schedule schedule(id, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(id, schedule.getId()); Mock::VerifyAndClearExpectations(handler.get()); } @@ -78,7 +78,7 @@ TEST_F(ScheduleTest, getName) const std::string name = "Schedule name"; scheduleState["name"] = name; expectGetState(id); - const Schedule schedule(id, commands, std::chrono::seconds(0)); + const Schedule schedule(id, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(name, schedule.getName()); } @@ -88,7 +88,7 @@ TEST_F(ScheduleTest, getDescription) const std::string description = "Schedule description"; scheduleState["description"] = description; expectGetState(id); - const Schedule schedule(id, commands, std::chrono::seconds(0)); + const Schedule schedule(id, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(description, schedule.getDescription()); } @@ -99,7 +99,7 @@ TEST_F(ScheduleTest, getCommand) const nlohmann::json body = {{"test", "value"}}; scheduleState["command"] = {{"address", addr}, {"body", body}, {"method", "PUT"}}; expectGetState(id); - const Schedule schedule(id, commands, std::chrono::seconds(0)); + const Schedule schedule(id, commands, std::chrono::seconds(0), nullptr); hueplusplus::Action command = schedule.getCommand(); EXPECT_EQ(addr, command.getAddress()); EXPECT_EQ(body, command.getBody()); @@ -112,7 +112,7 @@ TEST_F(ScheduleTest, getTime) const std::string time = "T13:00:00/T14:00:00"; scheduleState["localtime"] = time; expectGetState(id); - const Schedule schedule(id, commands, std::chrono::seconds(0)); + const Schedule schedule(id, commands, std::chrono::seconds(0), nullptr); time::TimePattern pattern = schedule.getTime(); EXPECT_EQ(time, pattern.toString()); } @@ -123,13 +123,13 @@ TEST_F(ScheduleTest, getStatus) { scheduleState["status"] = "enabled"; expectGetState(id); - const Schedule schedule(id, commands, std::chrono::seconds(0)); + const Schedule schedule(id, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(true, schedule.isEnabled()); } { scheduleState["status"] = "disabled"; expectGetState(id); - const Schedule schedule(id, commands, std::chrono::seconds(0)); + const Schedule schedule(id, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(false, schedule.isEnabled()); } } @@ -140,7 +140,7 @@ TEST_F(ScheduleTest, getAutodelete) const bool autodelete = true; scheduleState["autodelete"] = autodelete; expectGetState(id); - const Schedule schedule(id, commands, std::chrono::seconds(0)); + const Schedule schedule(id, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(autodelete, schedule.getAutodelete()); } @@ -150,7 +150,7 @@ TEST_F(ScheduleTest, getCreated) const std::string created = "2020-03-03T08:20:53"; scheduleState["created"] = created; expectGetState(id); - const Schedule schedule(id, commands, std::chrono::seconds(0)); + const Schedule schedule(id, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(created, schedule.getCreated().toString()); } @@ -160,7 +160,7 @@ TEST_F(ScheduleTest, getStartTime) const std::string starttime = "2020-03-03T08:20:53"; scheduleState["starttime"] = starttime; expectGetState(id); - const Schedule schedule(id, commands, std::chrono::seconds(0)); + const Schedule schedule(id, commands, std::chrono::seconds(0), nullptr); EXPECT_EQ(starttime, schedule.getStartTime().toString()); } @@ -168,7 +168,7 @@ TEST_F(ScheduleTest, setName) { const int id = 1; expectGetState(id); - Schedule schedule(id, commands, std::chrono::steady_clock::duration::max()); + Schedule schedule(id, commands, std::chrono::steady_clock::duration::max(), nullptr); const std::string name = "Test schedule"; nlohmann::json request = {{"name", name}}; nlohmann::json response = {{"success", {"/schedules/1/name", name}}}; @@ -183,7 +183,7 @@ TEST_F(ScheduleTest, setDescription) { const int id = 1; expectGetState(id); - Schedule schedule(id, commands, std::chrono::steady_clock::duration::max()); + Schedule schedule(id, commands, std::chrono::steady_clock::duration::max(), nullptr); const std::string description = "Test schedule description"; nlohmann::json request = {{"description", description}}; nlohmann::json response = {{"success", {"/schedules/1/description", description}}}; @@ -198,7 +198,7 @@ TEST_F(ScheduleTest, setCommand) { const int id = 1; expectGetState(id); - Schedule schedule(id, commands, std::chrono::steady_clock::duration::max()); + Schedule schedule(id, commands, std::chrono::steady_clock::duration::max(), nullptr); const hueplusplus::Action command({{"address", "abcd"}, {"body", {}}, {"method", "PUT"}}); nlohmann::json request = {{"command", command.toJson()}}; nlohmann::json response = {{"success", {"/schedules/1/command", command.toJson()}}}; @@ -213,7 +213,7 @@ TEST_F(ScheduleTest, setTime) { const int id = 1; expectGetState(id); - Schedule schedule(id, commands, std::chrono::steady_clock::duration::max()); + Schedule schedule(id, commands, std::chrono::steady_clock::duration::max(), nullptr); time::TimePattern time {time::AbsoluteVariedTime(std::chrono::system_clock::now())}; nlohmann::json request = {{"localtime", time.toString()}}; nlohmann::json response = {{"success", {"/schedules/1/localtime", time.toString()}}}; @@ -228,7 +228,7 @@ TEST_F(ScheduleTest, setStatus) { const int id = 1; expectGetState(id); - Schedule schedule(id, commands, std::chrono::steady_clock::duration::max()); + Schedule schedule(id, commands, std::chrono::steady_clock::duration::max(), nullptr); { nlohmann::json request = {{"status", "enabled"}}; nlohmann::json response = {{"success", {"/schedules/1/status", "enabled"}}}; @@ -253,7 +253,7 @@ TEST_F(ScheduleTest, setAutodelete) { const int id = 1; expectGetState(id); - Schedule schedule(id, commands, std::chrono::steady_clock::duration::max()); + Schedule schedule(id, commands, std::chrono::steady_clock::duration::max(), nullptr); const bool autodelete = false; nlohmann::json request = {{"autodelete", autodelete}}; nlohmann::json response = {{"success", {"/schedules/1/autodelete", autodelete}}}; diff --git a/test/test_Sensor.cpp b/test/test_Sensor.cpp index 14a464a..130c608 100644 --- a/test/test_Sensor.cpp +++ b/test/test_Sensor.cpp @@ -53,7 +53,7 @@ protected: GETJson( "/api/" + getBridgeUsername() + "/sensors/" + std::to_string(id), _, getBridgeIp(), getBridgePort())) .WillOnce(Return(state)); - return Sensor(id, commands, std::chrono::steady_clock::duration::max()); + return Sensor(id, commands, std::chrono::steady_clock::duration::max(), nullptr); } }; diff --git a/test/test_SensorImpls.cpp b/test/test_SensorImpls.cpp index 56d3d4e..2b8948d 100644 --- a/test/test_SensorImpls.cpp +++ b/test/test_SensorImpls.cpp @@ -68,7 +68,7 @@ protected: { EXPECT_CALL(*handler, GETJson("/api/" + getBridgeUsername() + "/sensors/1", _, getBridgeIp(), getBridgePort())) .WillOnce(Return(state)); - return T(Sensor(1, commands, std::chrono::steady_clock::duration::max())); + return T(Sensor(1, commands, std::chrono::steady_clock::duration::max(), nullptr)); } }; diff --git a/test/test_SensorList.cpp b/test/test_SensorList.cpp index 8461579..321496e 100644 --- a/test/test_SensorList.cpp +++ b/test/test_SensorList.cpp @@ -50,11 +50,6 @@ TEST(SensorList, getAsType) EXPECT_CALL(*handler, GETJson("/api/" + getBridgeUsername() + "/sensors", nlohmann::json::object(), getBridgeIp(), getBridgePort())) .WillOnce(Return(response)); - EXPECT_CALL(*handler, - GETJson("/api/" + getBridgeUsername() + "/sensors/" + std::to_string(id), nlohmann::json::object(), - getBridgeIp(), getBridgePort())) - .Times(2) - .WillRepeatedly(Return(nlohmann::json {{"type", "Daylight"}})); sensors::DaylightSensor daylightSensor = sensors.getAsType(id); EXPECT_THROW(sensors.getAsType(2), HueException); @@ -96,14 +91,6 @@ TEST(SensorList, getAllByType) GETJson( "/api/" + getBridgeUsername() + "/sensors", nlohmann::json::object(), getBridgeIp(), getBridgePort())) .WillOnce(Return(response)); - EXPECT_CALL(*handler, - GETJson( - "/api/" + getBridgeUsername() + "/sensors/2", nlohmann::json::object(), getBridgeIp(), getBridgePort())) - .WillOnce(Return(response["2"])); - EXPECT_CALL(*handler, - GETJson( - "/api/" + getBridgeUsername() + "/sensors/4", nlohmann::json::object(), getBridgeIp(), getBridgePort())) - .WillOnce(Return(response["4"])); sensors.refresh(); std::vector result = sensors.getAllByType(); EXPECT_THAT(result, -- libgit2 0.21.4