Commit 0db0a5ddab550312601358d7419c818911f221aa

Authored by Jojo-1000
Committed by Moritz Wirger
1 parent 2dc5770a

Update ip and username in HueCommandAPI when bridge is added

- Also added tests to verify it
hueplusplus/Hue.cpp
... ... @@ -74,16 +74,16 @@ std::vector<HueFinder::HueIdentification> HueFinder::FindBridges() const
74 74  
75 75 Hue HueFinder::GetBridge(const HueIdentification& identification)
76 76 {
77   - Hue bridge(identification.ip, "", http_handler);
78 77 auto pos = usernames.find(identification.mac);
79 78 if (pos != usernames.end())
80 79 {
81   - bridge.username = pos->second;
  80 + return Hue(identification.ip, pos->second, http_handler);
82 81 }
83 82 else
84 83 {
  84 + Hue bridge(identification.ip, "", http_handler);
85 85 bridge.requestUsername(identification.ip);
86   - if (bridge.getUsername().empty() || bridge.getUsername() == "")
  86 + if (bridge.getUsername().empty())
87 87 {
88 88 std::cerr << "Failed to request username for ip " << identification.ip << std::endl;
89 89 throw std::runtime_error("Failed to request username!");
... ... @@ -92,8 +92,8 @@ Hue HueFinder::GetBridge(const HueIdentification&amp; identification)
92 92 {
93 93 AddUsername(identification.mac, bridge.getUsername());
94 94 }
  95 + return bridge;
95 96 }
96   - return bridge;
97 97 }
98 98  
99 99 void HueFinder::AddUsername(const std::string& mac, const std::string& username)
... ... @@ -147,6 +147,8 @@ std::string Hue::requestUsername(const std::string&amp; ip)
147 147 // [{"success":{"username": "<username>"}}]
148 148 username = answer[0]["success"]["username"].asString();
149 149 this->ip = ip;
  150 + //Update commands with new username and ip
  151 + commands = HueCommandAPI(ip, username, http_handler);
150 152 std::cout << "Success! Link button was pressed!\n";
151 153 std::cout << "Username is \"" << username << "\"\n";
152 154 break;
... ...
hueplusplus/test/test_Hue.cpp
... ... @@ -22,17 +22,17 @@ protected:
22 22 handler = std::make_shared<MockHttpHandler>();
23 23  
24 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 28 EXPECT_CALL(*handler, GETString("/description.xml", "application/xml", "", "192.168.2.1", 80))
29   - .Times(0);
  29 + .Times(0);
30 30  
31 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 38 TEST_F(HueFinderTest, FindBridges)
... ... @@ -90,7 +90,15 @@ TEST_F(HueFinderTest, GetBridge)
90 90 EXPECT_EQ(test_bridge.getBridgeIP(), bridge_ip) << "Bridge IP not matching";
91 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 104 TEST_F(HueFinderTest, AddUsername)
... ... @@ -164,6 +172,14 @@ TEST(Hue, requestUsername)
164 172  
165 173 EXPECT_EQ(test_bridge.getBridgeIP(), bridge_ip) << "Bridge IP not matching";
166 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 185 TEST(Hue, setIP)
... ... @@ -179,7 +195,7 @@ TEST(Hue, getLight)
179 195 {
180 196 using namespace ::testing;
181 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 199 .Times(1);
184 200  
185 201 Hue test_bridge(bridge_ip, bridge_username, handler);
... ...