Commit 0db0a5ddab550312601358d7419c818911f221aa
Committed by
Moritz Wirger
1 parent
2dc5770a
Update ip and username in HueCommandAPI when bridge is added
- Also added tests to verify it
Showing
2 changed files
with
29 additions
and
11 deletions
hueplusplus/Hue.cpp
| @@ -74,16 +74,16 @@ std::vector<HueFinder::HueIdentification> HueFinder::FindBridges() const | @@ -74,16 +74,16 @@ std::vector<HueFinder::HueIdentification> HueFinder::FindBridges() const | ||
| 74 | 74 | ||
| 75 | Hue HueFinder::GetBridge(const HueIdentification& identification) | 75 | Hue HueFinder::GetBridge(const HueIdentification& identification) |
| 76 | { | 76 | { |
| 77 | - Hue bridge(identification.ip, "", http_handler); | ||
| 78 | auto pos = usernames.find(identification.mac); | 77 | auto pos = usernames.find(identification.mac); |
| 79 | if (pos != usernames.end()) | 78 | if (pos != usernames.end()) |
| 80 | { | 79 | { |
| 81 | - bridge.username = pos->second; | 80 | + return Hue(identification.ip, pos->second, http_handler); |
| 82 | } | 81 | } |
| 83 | else | 82 | else |
| 84 | { | 83 | { |
| 84 | + Hue bridge(identification.ip, "", http_handler); | ||
| 85 | bridge.requestUsername(identification.ip); | 85 | bridge.requestUsername(identification.ip); |
| 86 | - if (bridge.getUsername().empty() || bridge.getUsername() == "") | 86 | + if (bridge.getUsername().empty()) |
| 87 | { | 87 | { |
| 88 | std::cerr << "Failed to request username for ip " << identification.ip << std::endl; | 88 | std::cerr << "Failed to request username for ip " << identification.ip << std::endl; |
| 89 | throw std::runtime_error("Failed to request username!"); | 89 | throw std::runtime_error("Failed to request username!"); |
| @@ -92,8 +92,8 @@ Hue HueFinder::GetBridge(const HueIdentification& identification) | @@ -92,8 +92,8 @@ Hue HueFinder::GetBridge(const HueIdentification& identification) | ||
| 92 | { | 92 | { |
| 93 | AddUsername(identification.mac, bridge.getUsername()); | 93 | AddUsername(identification.mac, bridge.getUsername()); |
| 94 | } | 94 | } |
| 95 | + return bridge; | ||
| 95 | } | 96 | } |
| 96 | - return bridge; | ||
| 97 | } | 97 | } |
| 98 | 98 | ||
| 99 | void HueFinder::AddUsername(const std::string& mac, const std::string& username) | 99 | void HueFinder::AddUsername(const std::string& mac, const std::string& username) |
| @@ -147,6 +147,8 @@ std::string Hue::requestUsername(const std::string& ip) | @@ -147,6 +147,8 @@ std::string Hue::requestUsername(const std::string& ip) | ||
| 147 | // [{"success":{"username": "<username>"}}] | 147 | // [{"success":{"username": "<username>"}}] |
| 148 | username = answer[0]["success"]["username"].asString(); | 148 | username = answer[0]["success"]["username"].asString(); |
| 149 | this->ip = ip; | 149 | this->ip = ip; |
| 150 | + //Update commands with new username and ip | ||
| 151 | + commands = HueCommandAPI(ip, username, http_handler); | ||
| 150 | std::cout << "Success! Link button was pressed!\n"; | 152 | std::cout << "Success! Link button was pressed!\n"; |
| 151 | std::cout << "Username is \"" << username << "\"\n"; | 153 | std::cout << "Username is \"" << username << "\"\n"; |
| 152 | break; | 154 | break; |
hueplusplus/test/test_Hue.cpp
| @@ -22,17 +22,17 @@ protected: | @@ -22,17 +22,17 @@ protected: | ||
| 22 | handler = std::make_shared<MockHttpHandler>(); | 22 | handler = std::make_shared<MockHttpHandler>(); |
| 23 | 23 | ||
| 24 | 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)) | 24 | 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)) |
| 25 | - .Times(AtLeast(1)) | ||
| 26 | - .WillRepeatedly(Return(multicast_reply)); | 25 | + .Times(AtLeast(1)) |
| 26 | + .WillRepeatedly(Return(multicast_reply)); | ||
| 27 | 27 | ||
| 28 | EXPECT_CALL(*handler, GETString("/description.xml", "application/xml", "", "192.168.2.1", 80)) | 28 | EXPECT_CALL(*handler, GETString("/description.xml", "application/xml", "", "192.168.2.1", 80)) |
| 29 | - .Times(0); | 29 | + .Times(0); |
| 30 | 30 | ||
| 31 | EXPECT_CALL(*handler, GETString("/description.xml", "application/xml", "", bridge_ip, 80)) | 31 | EXPECT_CALL(*handler, GETString("/description.xml", "application/xml", "", bridge_ip, 80)) |
| 32 | - .Times(AtLeast(1)) | ||
| 33 | - .WillRepeatedly(Return(brige_xml)); | 32 | + .Times(AtLeast(1)) |
| 33 | + .WillRepeatedly(Return(brige_xml)); | ||
| 34 | } | 34 | } |
| 35 | - ~HueFinderTest(){}; | 35 | + ~HueFinderTest() {}; |
| 36 | }; | 36 | }; |
| 37 | 37 | ||
| 38 | TEST_F(HueFinderTest, FindBridges) | 38 | TEST_F(HueFinderTest, FindBridges) |
| @@ -90,7 +90,15 @@ TEST_F(HueFinderTest, GetBridge) | @@ -90,7 +90,15 @@ TEST_F(HueFinderTest, GetBridge) | ||
| 90 | EXPECT_EQ(test_bridge.getBridgeIP(), bridge_ip) << "Bridge IP not matching"; | 90 | EXPECT_EQ(test_bridge.getBridgeIP(), bridge_ip) << "Bridge IP not matching"; |
| 91 | EXPECT_EQ(test_bridge.getUsername(), bridge_username) << "Bridge username not matching"; | 91 | EXPECT_EQ(test_bridge.getUsername(), bridge_username) << "Bridge username not matching"; |
| 92 | 92 | ||
| 93 | + //Verify that username is correctly set in api requests | ||
| 94 | + Json::Value hue_bridge_state; | ||
| 95 | + hue_bridge_state["lights"] = Json::objectValue; | ||
| 96 | + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username, Json::Value(Json::objectValue), bridge_ip, 80)) | ||
| 97 | + .Times(1).WillOnce(Return(hue_bridge_state)); | ||
| 93 | 98 | ||
| 99 | + test_bridge.getAllLights(); | ||
| 100 | + | ||
| 101 | + Mock::VerifyAndClearExpectations(handler.get()); | ||
| 94 | } | 102 | } |
| 95 | 103 | ||
| 96 | TEST_F(HueFinderTest, AddUsername) | 104 | TEST_F(HueFinderTest, AddUsername) |
| @@ -164,6 +172,14 @@ TEST(Hue, requestUsername) | @@ -164,6 +172,14 @@ TEST(Hue, requestUsername) | ||
| 164 | 172 | ||
| 165 | EXPECT_EQ(test_bridge.getBridgeIP(), bridge_ip) << "Bridge IP not matching"; | 173 | EXPECT_EQ(test_bridge.getBridgeIP(), bridge_ip) << "Bridge IP not matching"; |
| 166 | EXPECT_EQ(test_bridge.getUsername(), bridge_username) << "Bridge username not matching"; | 174 | EXPECT_EQ(test_bridge.getUsername(), bridge_username) << "Bridge username not matching"; |
| 175 | + | ||
| 176 | + //Verify that username is correctly set in api requests | ||
| 177 | + Json::Value hue_bridge_state; | ||
| 178 | + hue_bridge_state["lights"] = Json::objectValue; | ||
| 179 | + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username, Json::Value(Json::objectValue), bridge_ip, 80)) | ||
| 180 | + .Times(1).WillOnce(Return(hue_bridge_state)); | ||
| 181 | + | ||
| 182 | + test_bridge.getAllLights(); | ||
| 167 | } | 183 | } |
| 168 | 184 | ||
| 169 | TEST(Hue, setIP) | 185 | TEST(Hue, setIP) |
| @@ -179,7 +195,7 @@ TEST(Hue, getLight) | @@ -179,7 +195,7 @@ TEST(Hue, getLight) | ||
| 179 | { | 195 | { |
| 180 | using namespace ::testing; | 196 | using namespace ::testing; |
| 181 | std::shared_ptr<MockHttpHandler> handler = std::make_shared<MockHttpHandler>(); | 197 | std::shared_ptr<MockHttpHandler> handler = std::make_shared<MockHttpHandler>(); |
| 182 | - EXPECT_CALL(*handler, GETJson("/api/"+bridge_username, Json::Value(Json::objectValue), bridge_ip, 80)) | 198 | + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username, Json::Value(Json::objectValue), bridge_ip, 80)) |
| 183 | .Times(1); | 199 | .Times(1); |
| 184 | 200 | ||
| 185 | Hue test_bridge(bridge_ip, bridge_username, handler); | 201 | Hue test_bridge(bridge_ip, bridge_username, handler); |