From 0db0a5ddab550312601358d7419c818911f221aa Mon Sep 17 00:00:00 2001 From: Jojo-1000 <33495614+Jojo-1000@users.noreply.github.com> Date: Sun, 22 Apr 2018 00:19:20 +0200 Subject: [PATCH] Update ip and username in HueCommandAPI when bridge is added - Also added tests to verify it --- hueplusplus/Hue.cpp | 10 ++++++---- hueplusplus/test/test_Hue.cpp | 30 +++++++++++++++++++++++------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/hueplusplus/Hue.cpp b/hueplusplus/Hue.cpp index 147eac4..f44ea3c 100755 --- a/hueplusplus/Hue.cpp +++ b/hueplusplus/Hue.cpp @@ -74,16 +74,16 @@ std::vector HueFinder::FindBridges() const Hue HueFinder::GetBridge(const HueIdentification& identification) { - Hue bridge(identification.ip, "", http_handler); auto pos = usernames.find(identification.mac); if (pos != usernames.end()) { - bridge.username = pos->second; + return Hue(identification.ip, pos->second, http_handler); } else { + Hue bridge(identification.ip, "", http_handler); bridge.requestUsername(identification.ip); - if (bridge.getUsername().empty() || bridge.getUsername() == "") + if (bridge.getUsername().empty()) { std::cerr << "Failed to request username for ip " << identification.ip << std::endl; throw std::runtime_error("Failed to request username!"); @@ -92,8 +92,8 @@ Hue HueFinder::GetBridge(const HueIdentification& identification) { AddUsername(identification.mac, bridge.getUsername()); } + return bridge; } - return bridge; } void HueFinder::AddUsername(const std::string& mac, const std::string& username) @@ -147,6 +147,8 @@ std::string Hue::requestUsername(const std::string& ip) // [{"success":{"username": ""}}] username = answer[0]["success"]["username"].asString(); this->ip = ip; + //Update commands with new username and ip + commands = HueCommandAPI(ip, username, http_handler); std::cout << "Success! Link button was pressed!\n"; std::cout << "Username is \"" << username << "\"\n"; break; diff --git a/hueplusplus/test/test_Hue.cpp b/hueplusplus/test/test_Hue.cpp index 1438ec7..6c92e51 100755 --- a/hueplusplus/test/test_Hue.cpp +++ b/hueplusplus/test/test_Hue.cpp @@ -22,17 +22,17 @@ protected: handler = std::make_shared(); EXPECT_CALL(*handler, sendMulticast("M-SEARCH * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\nMAN: \"ssdp:discover\"\r\nMX: 5\r\nST: ssdp:all\r\n\r\n", "239.255.255.250", 1900, 5)) - .Times(AtLeast(1)) - .WillRepeatedly(Return(multicast_reply)); + .Times(AtLeast(1)) + .WillRepeatedly(Return(multicast_reply)); EXPECT_CALL(*handler, GETString("/description.xml", "application/xml", "", "192.168.2.1", 80)) - .Times(0); + .Times(0); EXPECT_CALL(*handler, GETString("/description.xml", "application/xml", "", bridge_ip, 80)) - .Times(AtLeast(1)) - .WillRepeatedly(Return(brige_xml)); + .Times(AtLeast(1)) + .WillRepeatedly(Return(brige_xml)); } - ~HueFinderTest(){}; + ~HueFinderTest() {}; }; TEST_F(HueFinderTest, FindBridges) @@ -90,7 +90,15 @@ TEST_F(HueFinderTest, GetBridge) EXPECT_EQ(test_bridge.getBridgeIP(), bridge_ip) << "Bridge IP not matching"; EXPECT_EQ(test_bridge.getUsername(), bridge_username) << "Bridge username not matching"; + //Verify that username is correctly set in api requests + Json::Value hue_bridge_state; + hue_bridge_state["lights"] = Json::objectValue; + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username, Json::Value(Json::objectValue), bridge_ip, 80)) + .Times(1).WillOnce(Return(hue_bridge_state)); + test_bridge.getAllLights(); + + Mock::VerifyAndClearExpectations(handler.get()); } TEST_F(HueFinderTest, AddUsername) @@ -164,6 +172,14 @@ TEST(Hue, requestUsername) EXPECT_EQ(test_bridge.getBridgeIP(), bridge_ip) << "Bridge IP not matching"; EXPECT_EQ(test_bridge.getUsername(), bridge_username) << "Bridge username not matching"; + + //Verify that username is correctly set in api requests + Json::Value hue_bridge_state; + hue_bridge_state["lights"] = Json::objectValue; + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username, Json::Value(Json::objectValue), bridge_ip, 80)) + .Times(1).WillOnce(Return(hue_bridge_state)); + + test_bridge.getAllLights(); } TEST(Hue, setIP) @@ -179,7 +195,7 @@ TEST(Hue, getLight) { using namespace ::testing; std::shared_ptr handler = std::make_shared(); - EXPECT_CALL(*handler, GETJson("/api/"+bridge_username, Json::Value(Json::objectValue), bridge_ip, 80)) + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username, Json::Value(Json::objectValue), bridge_ip, 80)) .Times(1); Hue test_bridge(bridge_ip, bridge_username, handler); -- libgit2 0.21.4